big5hkscs1999.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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:1999
  21. */
  22. /*
  23. * BIG5-HKSCS:1999 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 (without the rows 0xC6..0xC7) through the ranges
  28. *
  29. * 0x{88..8D}{40..7E,A1..FE} 641 characters
  30. * 0x{8E..A0}{40..7E,A1..FE} 2898 characters
  31. * 0x{C6..C8}{40..7E,A1..FE} 359 characters
  32. * 0xF9{D6..FE} 41 characters
  33. * 0x{FA..FE}{40..7E,A1..FE} 763 characters
  34. *
  35. * Note that some HKSCS characters are not contained in Unicode 3.2
  36. * and are therefore best represented as sequences of Unicode characters:
  37. * 0x8862 U+00CA U+0304 LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND MACRON
  38. * 0x8864 U+00CA U+030C LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND CARON
  39. * 0x88A3 U+00EA U+0304 LATIN SMALL LETTER E WITH CIRCUMFLEX AND MACRON
  40. * 0x88A5 U+00EA U+030C LATIN SMALL LETTER E WITH CIRCUMFLEX AND CARON
  41. */
  42. #include "hkscs1999.h"
  43. #include "flushwc.h"
  44. static int
  45. big5hkscs1999_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  46. {
  47. ucs4_t last_wc = conv->istate;
  48. if (last_wc) {
  49. /* Output the buffered character. */
  50. conv->istate = 0;
  51. *pwc = last_wc;
  52. return 0; /* Don't advance the input pointer. */
  53. } else {
  54. unsigned char c = *s;
  55. /* Code set 0 (ASCII) */
  56. if (c < 0x80)
  57. return ascii_mbtowc(conv,pwc,s,n);
  58. /* Code set 1 (BIG5 extended) */
  59. if (c >= 0xa1 && c < 0xff) {
  60. if (n < 2)
  61. return RET_TOOFEW(0);
  62. {
  63. unsigned char c2 = s[1];
  64. if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0xa1 && c2 < 0xff)) {
  65. if (!((c == 0xc6 && c2 >= 0xa1) || c == 0xc7)) {
  66. int ret = big5_mbtowc(conv,pwc,s,2);
  67. if (ret != RET_ILSEQ)
  68. return ret;
  69. }
  70. }
  71. }
  72. }
  73. {
  74. int ret = hkscs1999_mbtowc(conv,pwc,s,n);
  75. if (ret != RET_ILSEQ)
  76. return ret;
  77. }
  78. if (c == 0x88) {
  79. if (n < 2)
  80. return RET_TOOFEW(0);
  81. {
  82. unsigned char c2 = s[1];
  83. if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
  84. /* It's a composed character. */
  85. ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
  86. ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
  87. /* We cannot output two Unicode characters at once. So,
  88. output the first character and buffer the second one. */
  89. *pwc = wc1;
  90. conv->istate = wc2;
  91. return 2;
  92. }
  93. }
  94. }
  95. return RET_ILSEQ;
  96. }
  97. }
  98. #define big5hkscs1999_flushwc normal_flushwc
  99. static int
  100. big5hkscs1999_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  101. {
  102. int count = 0;
  103. unsigned char last = conv->ostate;
  104. if (last) {
  105. /* last is = 0x66 or = 0xa7. */
  106. if (wc == 0x0304 || wc == 0x030c) {
  107. /* Output the combined character. */
  108. if (n >= 2) {
  109. r[0] = 0x88;
  110. r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
  111. conv->ostate = 0;
  112. return 2;
  113. } else
  114. return RET_TOOSMALL;
  115. }
  116. /* Output the buffered character. */
  117. if (n < 2)
  118. return RET_TOOSMALL;
  119. r[0] = 0x88;
  120. r[1] = last;
  121. r += 2;
  122. count = 2;
  123. }
  124. /* Code set 0 (ASCII) */
  125. if (wc < 0x0080) {
  126. /* Plain ASCII character. */
  127. if (n > count) {
  128. r[0] = (unsigned char) wc;
  129. conv->ostate = 0;
  130. return count+1;
  131. } else
  132. return RET_TOOSMALL;
  133. } else {
  134. unsigned char buf[2];
  135. int ret;
  136. /* Code set 1 (BIG5 extended) */
  137. ret = big5_wctomb(conv,buf,wc,2);
  138. if (ret != RET_ILUNI) {
  139. if (ret != 2) abort();
  140. if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
  141. if (n >= count+2) {
  142. r[0] = buf[0];
  143. r[1] = buf[1];
  144. conv->ostate = 0;
  145. return count+2;
  146. } else
  147. return RET_TOOSMALL;
  148. }
  149. }
  150. ret = hkscs1999_wctomb(conv,buf,wc,2);
  151. if (ret != RET_ILUNI) {
  152. if (ret != 2) abort();
  153. if ((wc & ~0x0020) == 0x00ca) {
  154. /* A possible first character of a multi-character sequence. We have to
  155. buffer it. */
  156. if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
  157. conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
  158. return count+0;
  159. }
  160. if (n >= count+2) {
  161. r[0] = buf[0];
  162. r[1] = buf[1];
  163. conv->ostate = 0;
  164. return count+2;
  165. } else
  166. return RET_TOOSMALL;
  167. }
  168. return RET_ILUNI;
  169. }
  170. }
  171. static int
  172. big5hkscs1999_reset (conv_t conv, unsigned char *r, size_t n)
  173. {
  174. unsigned char last = conv->ostate;
  175. if (last) {
  176. if (n < 2)
  177. return RET_TOOSMALL;
  178. r[0] = 0x88;
  179. r[1] = last;
  180. /* conv->ostate = 0; will be done by the caller */
  181. return 2;
  182. } else
  183. return 0;
  184. }