big5hkscs2001.h 6.1 KB

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