2 * Copyright (C) 1999-2001, 2008 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 conv
->istate
= state
;
296 return RET_SHIFT_ILSEQ(count
);
300 iso2022_cn_ext_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
302 state_t state
= conv
->ostate
;
304 unsigned char buf
[3];
307 /* There is no need to handle Unicode 3.1 tag characters and to look for
308 "zh-CN" or "zh-TW" tags, because GB2312 and CNS11643 are disjoint. */
311 ret
= ascii_wctomb(conv
,buf
,wc
,1);
312 if (ret
!= RET_ILUNI
) {
313 if (ret
!= 1) abort();
315 int count
= (state1
== STATE_ASCII
? 1 : 2);
318 if (state1
!= STATE_ASCII
) {
321 state1
= STATE_ASCII
;
324 if (wc
== 0x000a || wc
== 0x000d) {
325 state2
= STATE2_NONE
; state3
= STATE3_NONE
; state4
= STATE3_NONE
;
328 conv
->ostate
= state
;
333 /* Try GB 2312-1980. */
334 ret
= gb2312_wctomb(conv
,buf
,wc
,2);
335 if (ret
!= RET_ILUNI
) {
336 if (ret
!= 2) abort();
337 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
338 int count
= (state2
== STATE2_DESIGNATED_GB2312
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
341 if (state2
!= STATE2_DESIGNATED_GB2312
) {
347 state2
= STATE2_DESIGNATED_GB2312
;
349 if (state1
!= STATE_TWOBYTE
) {
352 state1
= STATE_TWOBYTE
;
357 conv
->ostate
= state
;
362 ret
= cns11643_wctomb(conv
,buf
,wc
,3);
363 if (ret
!= RET_ILUNI
) {
364 if (ret
!= 3) abort();
366 /* Try CNS 11643-1992 Plane 1. */
367 if (buf
[0] == 1 && buf
[1] < 0x80 && buf
[2] < 0x80) {
368 int count
= (state2
== STATE2_DESIGNATED_CNS11643_1
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
371 if (state2
!= STATE2_DESIGNATED_CNS11643_1
) {
377 state2
= STATE2_DESIGNATED_CNS11643_1
;
379 if (state1
!= STATE_TWOBYTE
) {
382 state1
= STATE_TWOBYTE
;
387 conv
->ostate
= state
;
391 /* Try CNS 11643-1992 Plane 2. */
392 if (buf
[0] == 2 && buf
[1] < 0x80 && buf
[2] < 0x80) {
393 int count
= (state3
== STATE3_DESIGNATED_CNS11643_2
? 0 : 4) + 4;
396 if (state3
!= STATE3_DESIGNATED_CNS11643_2
) {
402 state3
= STATE3_DESIGNATED_CNS11643_2
;
409 conv
->ostate
= state
;
413 /* Try CNS 11643-1992 Plane 3. */
414 if (buf
[0] == 3 && buf
[1] < 0x80 && buf
[2] < 0x80) {
415 int count
= (state4
== STATE4_DESIGNATED_CNS11643_3
? 0 : 4) + 4;
418 if (state4
!= STATE4_DESIGNATED_CNS11643_3
) {
424 state4
= STATE4_DESIGNATED_CNS11643_3
;
431 conv
->ostate
= state
;
435 /* Try CNS 11643-1992 Plane 4. */
436 if (buf
[0] == 4 && buf
[1] < 0x80 && buf
[2] < 0x80) {
437 int count
= (state4
== STATE4_DESIGNATED_CNS11643_4
? 0 : 4) + 4;
440 if (state4
!= STATE4_DESIGNATED_CNS11643_4
) {
446 state4
= STATE4_DESIGNATED_CNS11643_4
;
453 conv
->ostate
= state
;
457 /* Try CNS 11643-1992 Plane 5. */
458 if (buf
[0] == 5 && buf
[1] < 0x80 && buf
[2] < 0x80) {
459 int count
= (state4
== STATE4_DESIGNATED_CNS11643_5
? 0 : 4) + 4;
462 if (state4
!= STATE4_DESIGNATED_CNS11643_5
) {
468 state4
= STATE4_DESIGNATED_CNS11643_5
;
475 conv
->ostate
= state
;
479 /* Try CNS 11643-1992 Plane 6. */
480 if (buf
[0] == 6 && buf
[1] < 0x80 && buf
[2] < 0x80) {
481 int count
= (state4
== STATE4_DESIGNATED_CNS11643_6
? 0 : 4) + 4;
484 if (state4
!= STATE4_DESIGNATED_CNS11643_6
) {
490 state4
= STATE4_DESIGNATED_CNS11643_6
;
497 conv
->ostate
= state
;
501 /* Try CNS 11643-1992 Plane 7. */
502 if (buf
[0] == 7 && buf
[1] < 0x80 && buf
[2] < 0x80) {
503 int count
= (state4
== STATE4_DESIGNATED_CNS11643_7
? 0 : 4) + 4;
506 if (state4
!= STATE4_DESIGNATED_CNS11643_7
) {
512 state4
= STATE4_DESIGNATED_CNS11643_7
;
519 conv
->ostate
= state
;
525 /* Try ISO-IR-165. */
526 ret
= isoir165_wctomb(conv
,buf
,wc
,2);
527 if (ret
!= RET_ILUNI
) {
528 if (ret
!= 2) abort();
529 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
530 int count
= (state2
== STATE2_DESIGNATED_ISO_IR_165
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
533 if (state2
!= STATE2_DESIGNATED_ISO_IR_165
) {
539 state2
= STATE2_DESIGNATED_ISO_IR_165
;
541 if (state1
!= STATE_TWOBYTE
) {
544 state1
= STATE_TWOBYTE
;
549 conv
->ostate
= state
;
558 iso2022_cn_ext_reset (conv_t conv
, unsigned char *r
, int n
)
560 state_t state
= conv
->ostate
;
565 if (state1
!= STATE_ASCII
) {
569 /* conv->ostate = 0; will be done by the caller */
577 #undef STATE4_DESIGNATED_CNS11643_7
578 #undef STATE4_DESIGNATED_CNS11643_6
579 #undef STATE4_DESIGNATED_CNS11643_5
580 #undef STATE4_DESIGNATED_CNS11643_4
581 #undef STATE4_DESIGNATED_CNS11643_3
583 #undef STATE3_DESIGNATED_CNS11643_2
585 #undef STATE2_DESIGNATED_ISO_IR_165
586 #undef STATE2_DESIGNATED_CNS11643_1
587 #undef STATE2_DESIGNATED_GB2312