Update after gnulib changed.
[libiconv.git] / lib / big5hkscs2008.h
blobc9818e4d1268ddd7acb15090e21e4e0fac7ed6a0
1 /*
2 * Copyright (C) 1999-2002, 2006, 2010 Free Software Foundation, Inc.
3 * This file is part of the GNU LIBICONV Library.
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.
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.
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/>.
21 * BIG5-HKSCS:2008
25 * BIG5-HKSCS:2008 can be downloaded from
26 * http://www.ogcio.gov.hk/ccli/eng/hkscs/download.html
27 * http://www.ogcio.gov.hk/ccli/eng/hkscs/introduction.html
29 * It extends BIG5-HKSCS:2004 through 68 characters.
31 * It extends BIG5 (without the rows 0xC6..0xC7) through the ranges
33 * 0x{87..8D}{40..7E,A1..FE} 880 characters
34 * 0x{8E..A0}{40..7E,A1..FE} 2898 characters
35 * 0x{C6..C8}{40..7E,A1..FE} 359 characters
36 * 0xF9{D6..FE} 41 characters
37 * 0x{FA..FE}{40..7E,A1..FE} 763 characters
39 * Note that some HKSCS characters are not contained in Unicode 3.2
40 * and are therefore best represented as sequences of Unicode characters:
41 * 0x8862 U+00CA U+0304 LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND MACRON
42 * 0x8864 U+00CA U+030C LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND CARON
43 * 0x88A3 U+00EA U+0304 LATIN SMALL LETTER E WITH CIRCUMFLEX AND MACRON
44 * 0x88A5 U+00EA U+030C LATIN SMALL LETTER E WITH CIRCUMFLEX AND CARON
47 #include "hkscs2008.h"
48 #include "flushwc.h"
50 static int
51 big5hkscs2008_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
53 ucs4_t last_wc = conv->istate;
54 if (last_wc) {
55 /* Output the buffered character. */
56 conv->istate = 0;
57 *pwc = last_wc;
58 return 0; /* Don't advance the input pointer. */
59 } else {
60 unsigned char c = *s;
61 /* Code set 0 (ASCII) */
62 if (c < 0x80)
63 return ascii_mbtowc(conv,pwc,s,n);
64 /* Code set 1 (BIG5 extended) */
65 if (c >= 0xa1 && c < 0xff) {
66 if (n < 2)
67 return RET_TOOFEW(0);
69 unsigned char c2 = s[1];
70 if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0xa1 && c2 < 0xff)) {
71 if (!((c == 0xc6 && c2 >= 0xa1) || c == 0xc7)) {
72 int ret = big5_mbtowc(conv,pwc,s,2);
73 if (ret != RET_ILSEQ)
74 return ret;
80 int ret = hkscs1999_mbtowc(conv,pwc,s,n);
81 if (ret != RET_ILSEQ)
82 return ret;
85 int ret = hkscs2001_mbtowc(conv,pwc,s,n);
86 if (ret != RET_ILSEQ)
87 return ret;
90 int ret = hkscs2004_mbtowc(conv,pwc,s,n);
91 if (ret != RET_ILSEQ)
92 return ret;
95 int ret = hkscs2008_mbtowc(conv,pwc,s,n);
96 if (ret != RET_ILSEQ)
97 return ret;
99 if (c == 0x88) {
100 if (n < 2)
101 return RET_TOOFEW(0);
103 unsigned char c2 = s[1];
104 if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
105 /* It's a composed character. */
106 ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
107 ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
108 /* We cannot output two Unicode characters at once. So,
109 output the first character and buffer the second one. */
110 *pwc = wc1;
111 conv->istate = wc2;
112 return 2;
116 return RET_ILSEQ;
120 #define big5hkscs2008_flushwc normal_flushwc
122 static int
123 big5hkscs2008_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
125 int count = 0;
126 unsigned char last = conv->ostate;
128 if (last) {
129 /* last is = 0x66 or = 0xa7. */
130 if (wc == 0x0304 || wc == 0x030c) {
131 /* Output the combined character. */
132 if (n >= 2) {
133 r[0] = 0x88;
134 r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
135 conv->ostate = 0;
136 return 2;
137 } else
138 return RET_TOOSMALL;
141 /* Output the buffered character. */
142 if (n < 2)
143 return RET_TOOSMALL;
144 r[0] = 0x88;
145 r[1] = last;
146 r += 2;
147 count = 2;
150 /* Code set 0 (ASCII) */
151 if (wc < 0x0080) {
152 /* Plain ASCII character. */
153 if (n > count) {
154 r[0] = (unsigned char) wc;
155 conv->ostate = 0;
156 return count+1;
157 } else
158 return RET_TOOSMALL;
159 } else {
160 unsigned char buf[2];
161 int ret;
163 /* Code set 1 (BIG5 extended) */
164 ret = big5_wctomb(conv,buf,wc,2);
165 if (ret != RET_ILUNI) {
166 if (ret != 2) abort();
167 if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
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;
177 ret = hkscs1999_wctomb(conv,buf,wc,2);
178 if (ret != RET_ILUNI) {
179 if (ret != 2) abort();
180 if ((wc & ~0x0020) == 0x00ca) {
181 /* A possible first character of a multi-character sequence. We have to
182 buffer it. */
183 if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
184 conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
185 return count+0;
187 if (n >= count+2) {
188 r[0] = buf[0];
189 r[1] = buf[1];
190 conv->ostate = 0;
191 return count+2;
192 } else
193 return RET_TOOSMALL;
195 ret = hkscs2001_wctomb(conv,buf,wc,2);
196 if (ret != RET_ILUNI) {
197 if (ret != 2) abort();
198 if (n >= count+2) {
199 r[0] = buf[0];
200 r[1] = buf[1];
201 conv->ostate = 0;
202 return count+2;
203 } else
204 return RET_TOOSMALL;
206 ret = hkscs2004_wctomb(conv,buf,wc,2);
207 if (ret != RET_ILUNI) {
208 if (ret != 2) abort();
209 if (n >= count+2) {
210 r[0] = buf[0];
211 r[1] = buf[1];
212 conv->ostate = 0;
213 return count+2;
214 } else
215 return RET_TOOSMALL;
217 ret = hkscs2008_wctomb(conv,buf,wc,2);
218 if (ret != RET_ILUNI) {
219 if (ret != 2) abort();
220 if (n >= count+2) {
221 r[0] = buf[0];
222 r[1] = buf[1];
223 conv->ostate = 0;
224 return count+2;
225 } else
226 return RET_TOOSMALL;
228 return RET_ILUNI;
232 static int
233 big5hkscs2008_reset (conv_t conv, unsigned char *r, size_t n)
235 unsigned char last = conv->ostate;
237 if (last) {
238 if (n < 2)
239 return RET_TOOSMALL;
240 r[0] = 0x88;
241 r[1] = last;
242 /* conv->ostate = 0; will be done by the caller */
243 return 2;
244 } else
245 return 0;