big5hkscs1999.h 5.6 KB

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