big5hkscs2004.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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:2004
  22. */
  23. /*
  24. * BIG5-HKSCS:2004 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:2001 through 123 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 "hkscs2004.h"
  46. #include "flushwc.h"
  47. static int
  48. big5hkscs2004_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. if (c == 0x88) {
  92. if (n < 2)
  93. return RET_TOOFEW(0);
  94. {
  95. unsigned char c2 = s[1];
  96. if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
  97. /* It's a composed character. */
  98. ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
  99. ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
  100. /* We cannot output two Unicode characters at once. So,
  101. output the first character and buffer the second one. */
  102. *pwc = wc1;
  103. conv->istate = wc2;
  104. return 2;
  105. }
  106. }
  107. }
  108. return RET_ILSEQ;
  109. }
  110. }
  111. #define big5hkscs2004_flushwc normal_flushwc
  112. static int
  113. big5hkscs2004_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  114. {
  115. int count = 0;
  116. unsigned char last = conv->ostate;
  117. if (last) {
  118. /* last is = 0x66 or = 0xa7. */
  119. if (wc == 0x0304 || wc == 0x030c) {
  120. /* Output the combined character. */
  121. if (n >= 2) {
  122. r[0] = 0x88;
  123. r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
  124. conv->ostate = 0;
  125. return 2;
  126. } else
  127. return RET_TOOSMALL;
  128. }
  129. /* Output the buffered character. */
  130. if (n < 2)
  131. return RET_TOOSMALL;
  132. r[0] = 0x88;
  133. r[1] = last;
  134. r += 2;
  135. count = 2;
  136. }
  137. /* Code set 0 (ASCII) */
  138. if (wc < 0x0080) {
  139. /* Plain ASCII character. */
  140. if (n > count) {
  141. r[0] = (unsigned char) wc;
  142. conv->ostate = 0;
  143. return count+1;
  144. } else
  145. return RET_TOOSMALL;
  146. } else {
  147. unsigned char buf[2];
  148. int ret;
  149. /* Code set 1 (BIG5 extended) */
  150. ret = big5_wctomb(conv,buf,wc,2);
  151. if (ret != RET_ILUNI) {
  152. if (ret != 2) abort();
  153. if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
  154. if (n >= count+2) {
  155. r[0] = buf[0];
  156. r[1] = buf[1];
  157. conv->ostate = 0;
  158. return count+2;
  159. } else
  160. return RET_TOOSMALL;
  161. }
  162. }
  163. ret = hkscs1999_wctomb(conv,buf,wc,2);
  164. if (ret != RET_ILUNI) {
  165. if (ret != 2) abort();
  166. if ((wc & ~0x0020) == 0x00ca) {
  167. /* A possible first character of a multi-character sequence. We have to
  168. buffer it. */
  169. if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
  170. conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
  171. return count+0;
  172. }
  173. if (n >= count+2) {
  174. r[0] = buf[0];
  175. r[1] = buf[1];
  176. conv->ostate = 0;
  177. return count+2;
  178. } else
  179. return RET_TOOSMALL;
  180. }
  181. ret = hkscs2001_wctomb(conv,buf,wc,2);
  182. if (ret != RET_ILUNI) {
  183. if (ret != 2) abort();
  184. if (n >= count+2) {
  185. r[0] = buf[0];
  186. r[1] = buf[1];
  187. conv->ostate = 0;
  188. return count+2;
  189. } else
  190. return RET_TOOSMALL;
  191. }
  192. ret = hkscs2004_wctomb(conv,buf,wc,2);
  193. if (ret != RET_ILUNI) {
  194. if (ret != 2) abort();
  195. if (n >= count+2) {
  196. r[0] = buf[0];
  197. r[1] = buf[1];
  198. conv->ostate = 0;
  199. return count+2;
  200. } else
  201. return RET_TOOSMALL;
  202. }
  203. return RET_ILUNI;
  204. }
  205. }
  206. static int
  207. big5hkscs2004_reset (conv_t conv, unsigned char *r, int n)
  208. {
  209. unsigned char last = conv->ostate;
  210. if (last) {
  211. if (n < 2)
  212. return RET_TOOSMALL;
  213. r[0] = 0x88;
  214. r[1] = last;
  215. /* conv->ostate = 0; will be done by the caller */
  216. return 2;
  217. } else
  218. return 0;
  219. }