2 * Copyright (C) 1999-2001 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 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.
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.
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.
25 /* Specification: RFC 1922 */
32 * The state is composed of one of the following values
35 #define STATE_TWOBYTE 1
37 * and one of the following values, << 8
40 #define STATE2_DESIGNATED_GB2312 1
41 #define STATE2_DESIGNATED_CNS11643_1 2
42 #define STATE2_DESIGNATED_ISO_IR_165 3
44 * and one of the following values, << 16
47 #define STATE3_DESIGNATED_CNS11643_2 1
49 * and one of the following values, << 24
52 #define STATE4_DESIGNATED_CNS11643_3 1
53 #define STATE4_DESIGNATED_CNS11643_4 2
54 #define STATE4_DESIGNATED_CNS11643_5 3
55 #define STATE4_DESIGNATED_CNS11643_6 4
56 #define STATE4_DESIGNATED_CNS11643_7 5
59 unsigned int state1 = state & 0xff, state2 = (state >> 8) & 0xff, state3 = (state >> 16) & 0xff, state4 = state >> 24
60 #define COMBINE_STATE \
61 state = (state4 << 24) | (state3 << 16) | (state2 << 8) | state1
64 iso2022_cn_ext_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, int n
)
66 state_t state
= conv
->istate
;
78 state2
= STATE2_DESIGNATED_GB2312
;
85 state2
= STATE2_DESIGNATED_CNS11643_1
;
92 state2
= STATE2_DESIGNATED_ISO_IR_165
;
101 state3
= STATE3_DESIGNATED_CNS11643_2
;
110 state4
= STATE4_DESIGNATED_CNS11643_3
;
117 state4
= STATE4_DESIGNATED_CNS11643_4
;
124 state4
= STATE4_DESIGNATED_CNS11643_5
;
131 state4
= STATE4_DESIGNATED_CNS11643_6
;
138 state4
= STATE4_DESIGNATED_CNS11643_7
;
150 case STATE3_DESIGNATED_CNS11643_2
:
151 if (s
[2] < 0x80 && s
[3] < 0x80) {
152 int ret
= cns11643_2_mbtowc(conv
,pwc
,s
+2,2);
153 if (ret
== RET_ILSEQ
)
155 if (ret
!= 2) abort();
157 conv
->istate
= state
;
168 case STATE4_DESIGNATED_CNS11643_3
:
169 if (s
[2] < 0x80 && s
[3] < 0x80) {
170 int ret
= cns11643_3_mbtowc(conv
,pwc
,s
+2,2);
171 if (ret
== RET_ILSEQ
)
173 if (ret
!= 2) abort();
175 conv
->istate
= state
;
179 case STATE4_DESIGNATED_CNS11643_4
:
180 if (s
[2] < 0x80 && s
[3] < 0x80) {
181 int ret
= cns11643_4_mbtowc(conv
,pwc
,s
+2,2);
182 if (ret
== RET_ILSEQ
)
184 if (ret
!= 2) abort();
186 conv
->istate
= state
;
190 case STATE4_DESIGNATED_CNS11643_5
:
191 if (s
[2] < 0x80 && s
[3] < 0x80) {
192 int ret
= cns11643_5_mbtowc(conv
,pwc
,s
+2,2);
193 if (ret
== RET_ILSEQ
)
195 if (ret
!= 2) abort();
197 conv
->istate
= state
;
201 case STATE4_DESIGNATED_CNS11643_6
:
202 if (s
[2] < 0x80 && s
[3] < 0x80) {
203 int ret
= cns11643_6_mbtowc(conv
,pwc
,s
+2,2);
204 if (ret
== RET_ILSEQ
)
206 if (ret
!= 2) abort();
208 conv
->istate
= state
;
212 case STATE4_DESIGNATED_CNS11643_7
:
213 if (s
[2] < 0x80 && s
[3] < 0x80) {
214 int ret
= cns11643_7_mbtowc(conv
,pwc
,s
+2,2);
215 if (ret
== RET_ILSEQ
)
217 if (ret
!= 2) abort();
219 conv
->istate
= state
;
229 if (state2
!= STATE2_DESIGNATED_GB2312
&& state2
!= STATE2_DESIGNATED_CNS11643_1
&& state2
!= STATE2_DESIGNATED_ISO_IR_165
)
231 state1
= STATE_TWOBYTE
;
238 state1
= STATE_ASCII
;
249 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
250 if (ret
== RET_ILSEQ
)
252 if (ret
!= 1) abort();
253 if (*pwc
== 0x000a || *pwc
== 0x000d) {
254 state2
= STATE2_NONE
; state3
= STATE3_NONE
; state4
= STATE3_NONE
;
257 conv
->istate
= state
;
264 if (s
[0] < 0x80 && s
[1] < 0x80) {
269 case STATE2_DESIGNATED_GB2312
:
270 ret
= gb2312_mbtowc(conv
,pwc
,s
,2); break;
271 case STATE2_DESIGNATED_CNS11643_1
:
272 ret
= cns11643_1_mbtowc(conv
,pwc
,s
,2); break;
273 case STATE2_DESIGNATED_ISO_IR_165
:
274 ret
= isoir165_mbtowc(conv
,pwc
,s
,2); break;
277 if (ret
== RET_ILSEQ
)
279 if (ret
!= 2) abort();
281 conv
->istate
= state
;
290 conv
->istate
= state
;
291 return RET_TOOFEW(count
);
295 iso2022_cn_ext_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
297 state_t state
= conv
->ostate
;
299 unsigned char buf
[3];
302 /* There is no need to handle Unicode 3.1 tag characters and to look for
303 "zh-CN" or "zh-TW" tags, because GB2312 and CNS11643 are disjoint. */
306 ret
= ascii_wctomb(conv
,buf
,wc
,1);
307 if (ret
!= RET_ILUNI
) {
308 if (ret
!= 1) abort();
310 int count
= (state1
== STATE_ASCII
? 1 : 2);
313 if (state1
!= STATE_ASCII
) {
316 state1
= STATE_ASCII
;
319 if (wc
== 0x000a || wc
== 0x000d) {
320 state2
= STATE2_NONE
; state3
= STATE3_NONE
; state4
= STATE3_NONE
;
323 conv
->ostate
= state
;
328 /* Try GB 2312-1980. */
329 ret
= gb2312_wctomb(conv
,buf
,wc
,2);
330 if (ret
!= RET_ILUNI
) {
331 if (ret
!= 2) abort();
332 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
333 int count
= (state2
== STATE2_DESIGNATED_GB2312
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
336 if (state2
!= STATE2_DESIGNATED_GB2312
) {
342 state2
= STATE2_DESIGNATED_GB2312
;
344 if (state1
!= STATE_TWOBYTE
) {
347 state1
= STATE_TWOBYTE
;
352 conv
->ostate
= state
;
357 ret
= cns11643_wctomb(conv
,buf
,wc
,3);
358 if (ret
!= RET_ILUNI
) {
359 if (ret
!= 3) abort();
361 /* Try CNS 11643-1992 Plane 1. */
362 if (buf
[0] == 1 && buf
[1] < 0x80 && buf
[2] < 0x80) {
363 int count
= (state2
== STATE2_DESIGNATED_CNS11643_1
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
366 if (state2
!= STATE2_DESIGNATED_CNS11643_1
) {
372 state2
= STATE2_DESIGNATED_CNS11643_1
;
374 if (state1
!= STATE_TWOBYTE
) {
377 state1
= STATE_TWOBYTE
;
382 conv
->ostate
= state
;
386 /* Try CNS 11643-1992 Plane 2. */
387 if (buf
[0] == 2 && buf
[1] < 0x80 && buf
[2] < 0x80) {
388 int count
= (state3
== STATE3_DESIGNATED_CNS11643_2
? 0 : 4) + 4;
391 if (state3
!= STATE3_DESIGNATED_CNS11643_2
) {
397 state3
= STATE3_DESIGNATED_CNS11643_2
;
404 conv
->ostate
= state
;
408 /* Try CNS 11643-1992 Plane 3. */
409 if (buf
[0] == 3 && buf
[1] < 0x80 && buf
[2] < 0x80) {
410 int count
= (state4
== STATE4_DESIGNATED_CNS11643_3
? 0 : 4) + 4;
413 if (state4
!= STATE4_DESIGNATED_CNS11643_3
) {
419 state4
= STATE4_DESIGNATED_CNS11643_3
;
426 conv
->ostate
= state
;
430 /* Try CNS 11643-1992 Plane 4. */
431 if (buf
[0] == 4 && buf
[1] < 0x80 && buf
[2] < 0x80) {
432 int count
= (state4
== STATE4_DESIGNATED_CNS11643_4
? 0 : 4) + 4;
435 if (state4
!= STATE4_DESIGNATED_CNS11643_4
) {
441 state4
= STATE4_DESIGNATED_CNS11643_4
;
448 conv
->ostate
= state
;
452 /* Try CNS 11643-1992 Plane 5. */
453 if (buf
[0] == 5 && buf
[1] < 0x80 && buf
[2] < 0x80) {
454 int count
= (state4
== STATE4_DESIGNATED_CNS11643_5
? 0 : 4) + 4;
457 if (state4
!= STATE4_DESIGNATED_CNS11643_5
) {
463 state4
= STATE4_DESIGNATED_CNS11643_5
;
470 conv
->ostate
= state
;
474 /* Try CNS 11643-1992 Plane 6. */
475 if (buf
[0] == 6 && buf
[1] < 0x80 && buf
[2] < 0x80) {
476 int count
= (state4
== STATE4_DESIGNATED_CNS11643_6
? 0 : 4) + 4;
479 if (state4
!= STATE4_DESIGNATED_CNS11643_6
) {
485 state4
= STATE4_DESIGNATED_CNS11643_6
;
492 conv
->ostate
= state
;
496 /* Try CNS 11643-1992 Plane 7. */
497 if (buf
[0] == 7 && buf
[1] < 0x80 && buf
[2] < 0x80) {
498 int count
= (state4
== STATE4_DESIGNATED_CNS11643_7
? 0 : 4) + 4;
501 if (state4
!= STATE4_DESIGNATED_CNS11643_7
) {
507 state4
= STATE4_DESIGNATED_CNS11643_7
;
514 conv
->ostate
= state
;
520 /* Try ISO-IR-165. */
521 ret
= isoir165_wctomb(conv
,buf
,wc
,2);
522 if (ret
!= RET_ILUNI
) {
523 if (ret
!= 2) abort();
524 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
525 int count
= (state2
== STATE2_DESIGNATED_ISO_IR_165
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
528 if (state2
!= STATE2_DESIGNATED_ISO_IR_165
) {
534 state2
= STATE2_DESIGNATED_ISO_IR_165
;
536 if (state1
!= STATE_TWOBYTE
) {
539 state1
= STATE_TWOBYTE
;
544 conv
->ostate
= state
;
553 iso2022_cn_ext_reset (conv_t conv
, unsigned char *r
, int n
)
555 state_t state
= conv
->ostate
;
560 if (state1
!= STATE_ASCII
) {
564 /* conv->ostate = 0; will be done by the caller */
572 #undef STATE4_DESIGNATED_CNS11643_7
573 #undef STATE4_DESIGNATED_CNS11643_6
574 #undef STATE4_DESIGNATED_CNS11643_5
575 #undef STATE4_DESIGNATED_CNS11643_4
576 #undef STATE4_DESIGNATED_CNS11643_3
578 #undef STATE3_DESIGNATED_CNS11643_2
580 #undef STATE2_DESIGNATED_ISO_IR_165
581 #undef STATE2_DESIGNATED_CNS11643_1
582 #undef STATE2_DESIGNATED_GB2312