big5hkscs2004.h 6.4 KB

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