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
43 * and one of the following values, << 16
46 #define STATE3_DESIGNATED_CNS11643_2 1
49 unsigned int state1 = state & 0xff, state2 = (state >> 8) & 0xff, state3 = state >> 16
50 #define COMBINE_STATE \
51 state = (state3 << 16) | (state2 << 8) | state1
54 iso2022_cn_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, int n
)
56 state_t state
= conv
->istate
;
68 state2
= STATE2_DESIGNATED_GB2312
;
75 state2
= STATE2_DESIGNATED_CNS11643_1
;
84 state3
= STATE3_DESIGNATED_CNS11643_2
;
96 case STATE3_DESIGNATED_CNS11643_2
:
97 if (s
[2] < 0x80 && s
[3] < 0x80) {
98 int ret
= cns11643_2_mbtowc(conv
,pwc
,s
+2,2);
101 if (ret
!= 2) abort();
103 conv
->istate
= state
;
113 if (state2
!= STATE2_DESIGNATED_GB2312
&& state2
!= STATE2_DESIGNATED_CNS11643_1
)
115 state1
= STATE_TWOBYTE
;
122 state1
= STATE_ASCII
;
133 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
134 if (ret
== RET_ILSEQ
)
136 if (ret
!= 1) abort();
137 if (*pwc
== 0x000a || *pwc
== 0x000d) {
138 state2
= STATE2_NONE
; state3
= STATE3_NONE
;
141 conv
->istate
= state
;
148 if (s
[0] < 0x80 && s
[1] < 0x80) {
153 case STATE2_DESIGNATED_GB2312
:
154 ret
= gb2312_mbtowc(conv
,pwc
,s
,2); break;
155 case STATE2_DESIGNATED_CNS11643_1
:
156 ret
= cns11643_1_mbtowc(conv
,pwc
,s
,2); break;
159 if (ret
== RET_ILSEQ
)
161 if (ret
!= 2) abort();
163 conv
->istate
= state
;
172 conv
->istate
= state
;
173 return RET_TOOFEW(count
);
177 iso2022_cn_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
179 state_t state
= conv
->ostate
;
181 unsigned char buf
[3];
184 /* There is no need to handle Unicode 3.1 tag characters and to look for
185 "zh-CN" or "zh-TW" tags, because GB2312 and CNS11643 are disjoint. */
188 ret
= ascii_wctomb(conv
,buf
,wc
,1);
189 if (ret
!= RET_ILUNI
) {
190 if (ret
!= 1) abort();
192 int count
= (state1
== STATE_ASCII
? 1 : 2);
195 if (state1
!= STATE_ASCII
) {
198 state1
= STATE_ASCII
;
201 if (wc
== 0x000a || wc
== 0x000d) {
202 state2
= STATE2_NONE
; state3
= STATE3_NONE
;
205 conv
->ostate
= state
;
210 /* Try GB 2312-1980. */
211 ret
= gb2312_wctomb(conv
,buf
,wc
,2);
212 if (ret
!= RET_ILUNI
) {
213 if (ret
!= 2) abort();
214 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
215 int count
= (state2
== STATE2_DESIGNATED_GB2312
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
218 if (state2
!= STATE2_DESIGNATED_GB2312
) {
224 state2
= STATE2_DESIGNATED_GB2312
;
226 if (state1
!= STATE_TWOBYTE
) {
229 state1
= STATE_TWOBYTE
;
234 conv
->ostate
= state
;
239 ret
= cns11643_wctomb(conv
,buf
,wc
,3);
240 if (ret
!= RET_ILUNI
) {
241 if (ret
!= 3) abort();
243 /* Try CNS 11643-1992 Plane 1. */
244 if (buf
[0] == 1 && buf
[1] < 0x80 && buf
[2] < 0x80) {
245 int count
= (state2
== STATE2_DESIGNATED_CNS11643_1
? 0 : 4) + (state1
== STATE_TWOBYTE
? 0 : 1) + 2;
248 if (state2
!= STATE2_DESIGNATED_CNS11643_1
) {
254 state2
= STATE2_DESIGNATED_CNS11643_1
;
256 if (state1
!= STATE_TWOBYTE
) {
259 state1
= STATE_TWOBYTE
;
264 conv
->ostate
= state
;
268 /* Try CNS 11643-1992 Plane 2. */
269 if (buf
[0] == 2 && buf
[1] < 0x80 && buf
[2] < 0x80) {
270 int count
= (state3
== STATE3_DESIGNATED_CNS11643_2
? 0 : 4) + 4;
273 if (state3
!= STATE3_DESIGNATED_CNS11643_2
) {
279 state3
= STATE3_DESIGNATED_CNS11643_2
;
286 conv
->ostate
= state
;
295 iso2022_cn_reset (conv_t conv
, unsigned char *r
, int n
)
297 state_t state
= conv
->ostate
;
301 if (state1
!= STATE_ASCII
) {
305 /* conv->ostate = 0; will be done by the caller */
313 #undef STATE3_DESIGNATED_CNS11643_2
315 #undef STATE2_DESIGNATED_CNS11643_1
316 #undef STATE2_DESIGNATED_GB2312