big5hkscs2001.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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:2001
  21. */
  22. /*
  23. * BIG5-HKSCS:2001 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:1999 through 116 characters.
  28. *
  29. * It extends BIG5 (without the rows 0xC6..0xC7) through the ranges
  30. *
  31. * 0x{88..8D}{40..7E,A1..FE} 757 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 "hkscs2001.h"
  45. #include "flushwc.h"
  46. static int
  47. big5hkscs2001_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. if (c == 0x88) {
  86. if (n < 2)
  87. return RET_TOOFEW(0);
  88. {
  89. unsigned char c2 = s[1];
  90. if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
  91. /* It's a composed character. */
  92. ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
  93. ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
  94. /* We cannot output two Unicode characters at once. So,
  95. output the first character and buffer the second one. */
  96. *pwc = wc1;
  97. conv->istate = wc2;
  98. return 2;
  99. }
  100. }
  101. }
  102. return RET_ILSEQ;
  103. }
  104. }
  105. #define big5hkscs2001_flushwc normal_flushwc
  106. static int
  107. big5hkscs2001_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  108. {
  109. int count = 0;
  110. unsigned char last = conv->ostate;
  111. if (last) {
  112. /* last is = 0x66 or = 0xa7. */
  113. if (wc == 0x0304 || wc == 0x030c) {
  114. /* Output the combined character. */
  115. if (n >= 2) {
  116. r[0] = 0x88;
  117. r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
  118. conv->ostate = 0;
  119. return 2;
  120. } else
  121. return RET_TOOSMALL;
  122. }
  123. /* Output the buffered character. */
  124. if (n < 2)
  125. return RET_TOOSMALL;
  126. r[0] = 0x88;
  127. r[1] = last;
  128. r += 2;
  129. count = 2;
  130. }
  131. /* Code set 0 (ASCII) */
  132. if (wc < 0x0080) {
  133. /* Plain ASCII character. */
  134. if (n > count) {
  135. r[0] = (unsigned char) wc;
  136. conv->ostate = 0;
  137. return count+1;
  138. } else
  139. return RET_TOOSMALL;
  140. } else {
  141. unsigned char buf[2];
  142. int ret;
  143. /* Code set 1 (BIG5 extended) */
  144. ret = big5_wctomb(conv,buf,wc,2);
  145. if (ret != RET_ILUNI) {
  146. if (ret != 2) abort();
  147. if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
  148. if (n >= count+2) {
  149. r[0] = buf[0];
  150. r[1] = buf[1];
  151. conv->ostate = 0;
  152. return count+2;
  153. } else
  154. return RET_TOOSMALL;
  155. }
  156. }
  157. ret = hkscs1999_wctomb(conv,buf,wc,2);
  158. if (ret != RET_ILUNI) {
  159. if (ret != 2) abort();
  160. if ((wc & ~0x0020) == 0x00ca) {
  161. /* A possible first character of a multi-character sequence. We have to
  162. buffer it. */
  163. if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
  164. conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
  165. return count+0;
  166. }
  167. if (n >= count+2) {
  168. r[0] = buf[0];
  169. r[1] = buf[1];
  170. conv->ostate = 0;
  171. return count+2;
  172. } else
  173. return RET_TOOSMALL;
  174. }
  175. ret = hkscs2001_wctomb(conv,buf,wc,2);
  176. if (ret != RET_ILUNI) {
  177. if (ret != 2) abort();
  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. return RET_ILUNI;
  187. }
  188. }
  189. static int
  190. big5hkscs2001_reset (conv_t conv, unsigned char *r, size_t n)
  191. {
  192. unsigned char last = conv->ostate;
  193. if (last) {
  194. if (n < 2)
  195. return RET_TOOSMALL;
  196. r[0] = 0x88;
  197. r[1] = last;
  198. /* conv->ostate = 0; will be done by the caller */
  199. return 2;
  200. } else
  201. return 0;
  202. }