big5hkscs2008.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 1999-2002, 2006, 2010 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
  18. * Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /*
  21. * BIG5-HKSCS:2008
  22. */
  23. /*
  24. * BIG5-HKSCS:2008 can be downloaded from
  25. * http://www.ogcio.gov.hk/ccli/eng/hkscs/download.html
  26. * http://www.ogcio.gov.hk/ccli/eng/hkscs/introduction.html
  27. *
  28. * It extends BIG5-HKSCS:2004 through 68 characters.
  29. *
  30. * It extends BIG5 (without the rows 0xC6..0xC7) through the ranges
  31. *
  32. * 0x{87..8D}{40..7E,A1..FE} 880 characters
  33. * 0x{8E..A0}{40..7E,A1..FE} 2898 characters
  34. * 0x{C6..C8}{40..7E,A1..FE} 359 characters
  35. * 0xF9{D6..FE} 41 characters
  36. * 0x{FA..FE}{40..7E,A1..FE} 763 characters
  37. *
  38. * Note that some HKSCS characters are not contained in Unicode 3.2
  39. * and are therefore best represented as sequences of Unicode characters:
  40. * 0x8862 U+00CA U+0304 LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND MACRON
  41. * 0x8864 U+00CA U+030C LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND CARON
  42. * 0x88A3 U+00EA U+0304 LATIN SMALL LETTER E WITH CIRCUMFLEX AND MACRON
  43. * 0x88A5 U+00EA U+030C LATIN SMALL LETTER E WITH CIRCUMFLEX AND CARON
  44. */
  45. #include "hkscs2008.h"
  46. #include "flushwc.h"
  47. static int
  48. big5hkscs2008_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  49. {
  50. ucs4_t last_wc = conv->istate;
  51. if (last_wc) {
  52. /* Output the buffered character. */
  53. conv->istate = 0;
  54. *pwc = last_wc;
  55. return 0; /* Don't advance the input pointer. */
  56. } else {
  57. unsigned char c = *s;
  58. /* Code set 0 (ASCII) */
  59. if (c < 0x80)
  60. return ascii_mbtowc(conv,pwc,s,n);
  61. /* Code set 1 (BIG5 extended) */
  62. if (c >= 0xa1 && c < 0xff) {
  63. if (n < 2)
  64. return RET_TOOFEW(0);
  65. {
  66. unsigned char c2 = s[1];
  67. if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0xa1 && c2 < 0xff)) {
  68. if (!((c == 0xc6 && c2 >= 0xa1) || c == 0xc7)) {
  69. int ret = big5_mbtowc(conv,pwc,s,2);
  70. if (ret != RET_ILSEQ)
  71. return ret;
  72. }
  73. }
  74. }
  75. }
  76. {
  77. int ret = hkscs1999_mbtowc(conv,pwc,s,n);
  78. if (ret != RET_ILSEQ)
  79. return ret;
  80. }
  81. {
  82. int ret = hkscs2001_mbtowc(conv,pwc,s,n);
  83. if (ret != RET_ILSEQ)
  84. return ret;
  85. }
  86. {
  87. int ret = hkscs2004_mbtowc(conv,pwc,s,n);
  88. if (ret != RET_ILSEQ)
  89. return ret;
  90. }
  91. {
  92. int ret = hkscs2008_mbtowc(conv,pwc,s,n);
  93. if (ret != RET_ILSEQ)
  94. return ret;
  95. }
  96. if (c == 0x88) {
  97. if (n < 2)
  98. return RET_TOOFEW(0);
  99. {
  100. unsigned char c2 = s[1];
  101. if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
  102. /* It's a composed character. */
  103. ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
  104. ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
  105. /* We cannot output two Unicode characters at once. So,
  106. output the first character and buffer the second one. */
  107. *pwc = wc1;
  108. conv->istate = wc2;
  109. return 2;
  110. }
  111. }
  112. }
  113. return RET_ILSEQ;
  114. }
  115. }
  116. #define big5hkscs2008_flushwc normal_flushwc
  117. static int
  118. big5hkscs2008_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  119. {
  120. int count = 0;
  121. unsigned char last = conv->ostate;
  122. if (last) {
  123. /* last is = 0x66 or = 0xa7. */
  124. if (wc == 0x0304 || wc == 0x030c) {
  125. /* Output the combined character. */
  126. if (n >= 2) {
  127. r[0] = 0x88;
  128. r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
  129. conv->ostate = 0;
  130. return 2;
  131. } else
  132. return RET_TOOSMALL;
  133. }
  134. /* Output the buffered character. */
  135. if (n < 2)
  136. return RET_TOOSMALL;
  137. r[0] = 0x88;
  138. r[1] = last;
  139. r += 2;
  140. count = 2;
  141. }
  142. /* Code set 0 (ASCII) */
  143. if (wc < 0x0080) {
  144. /* Plain ASCII character. */
  145. if (n > count) {
  146. r[0] = (unsigned char) wc;
  147. conv->ostate = 0;
  148. return count+1;
  149. } else
  150. return RET_TOOSMALL;
  151. } else {
  152. unsigned char buf[2];
  153. int ret;
  154. /* Code set 1 (BIG5 extended) */
  155. ret = big5_wctomb(conv,buf,wc,2);
  156. if (ret != RET_ILUNI) {
  157. if (ret != 2) abort();
  158. if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
  159. if (n >= count+2) {
  160. r[0] = buf[0];
  161. r[1] = buf[1];
  162. conv->ostate = 0;
  163. return count+2;
  164. } else
  165. return RET_TOOSMALL;
  166. }
  167. }
  168. ret = hkscs1999_wctomb(conv,buf,wc,2);
  169. if (ret != RET_ILUNI) {
  170. if (ret != 2) abort();
  171. if ((wc & ~0x0020) == 0x00ca) {
  172. /* A possible first character of a multi-character sequence. We have to
  173. buffer it. */
  174. if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
  175. conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
  176. return count+0;
  177. }
  178. if (n >= count+2) {
  179. r[0] = buf[0];
  180. r[1] = buf[1];
  181. conv->ostate = 0;
  182. return count+2;
  183. } else
  184. return RET_TOOSMALL;
  185. }
  186. ret = hkscs2001_wctomb(conv,buf,wc,2);
  187. if (ret != RET_ILUNI) {
  188. if (ret != 2) abort();
  189. if (n >= count+2) {
  190. r[0] = buf[0];
  191. r[1] = buf[1];
  192. conv->ostate = 0;
  193. return count+2;
  194. } else
  195. return RET_TOOSMALL;
  196. }
  197. ret = hkscs2004_wctomb(conv,buf,wc,2);
  198. if (ret != RET_ILUNI) {
  199. if (ret != 2) abort();
  200. if (n >= count+2) {
  201. r[0] = buf[0];
  202. r[1] = buf[1];
  203. conv->ostate = 0;
  204. return count+2;
  205. } else
  206. return RET_TOOSMALL;
  207. }
  208. ret = hkscs2008_wctomb(conv,buf,wc,2);
  209. if (ret != RET_ILUNI) {
  210. if (ret != 2) abort();
  211. if (n >= count+2) {
  212. r[0] = buf[0];
  213. r[1] = buf[1];
  214. conv->ostate = 0;
  215. return count+2;
  216. } else
  217. return RET_TOOSMALL;
  218. }
  219. return RET_ILUNI;
  220. }
  221. }
  222. static int
  223. big5hkscs2008_reset (conv_t conv, unsigned char *r, int n)
  224. {
  225. unsigned char last = conv->ostate;
  226. if (last) {
  227. if (n < 2)
  228. return RET_TOOSMALL;
  229. r[0] = 0x88;
  230. r[1] = last;
  231. /* conv->ostate = 0; will be done by the caller */
  232. return 2;
  233. } else
  234. return 0;
  235. }