2 * Copyright (C) 1999-2000 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., 59 Temple Place -
18 * Suite 330, Boston, MA 02111-1307, USA.
25 /* Specification: RFC 1554 */
26 /* ESC '(' 'I' for JISX0201 Katakana is an extension not found in RFC 1554 or
27 CJK.INF, but implemented in glibc-2.1 and qt-2.0. */
32 * The state is composed of one of the following values
35 #define STATE_JISX0201ROMAN 1
36 #define STATE_JISX0201KATAKANA 2
37 #define STATE_JISX0208 3
38 #define STATE_JISX0212 4
39 #define STATE_GB2312 5
40 #define STATE_KSC5601 6
42 * and one of the following values, << 8
44 #define STATE_G2_NONE 0
45 #define STATE_G2_ISO8859_1 1
46 #define STATE_G2_ISO8859_7 2
49 unsigned int state1 = state & 0xff, state2 = state >> 8
50 #define COMBINE_STATE \
51 state = (state2 << 8) | state1
54 iso2022_jp2_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, int n
)
56 state_t state
= conv
->istate
;
74 state1
= STATE_JISX0201ROMAN
;
81 state1
= STATE_JISX0201KATAKANA
;
90 if (s
[2] == '@' || s
[2] == 'B') {
91 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
92 state1
= STATE_JISX0208
;
99 state1
= STATE_GB2312
;
109 state1
= STATE_JISX0212
;
116 state1
= STATE_KSC5601
;
130 state2
= STATE_G2_ISO8859_1
;
137 state2
= STATE_G2_ISO8859_7
;
149 case STATE_G2_ISO8859_1
:
151 unsigned char buf
= s
[2]+0x80;
152 int ret
= iso8859_1_mbtowc(conv
,pwc
,&buf
,1);
153 if (ret
== RET_ILSEQ
)
155 if (ret
!= 1) abort();
157 conv
->istate
= state
;
161 case STATE_G2_ISO8859_7
:
163 unsigned char buf
= s
[2]+0x80;
164 int ret
= iso8859_7_mbtowc(conv
,pwc
,&buf
,1);
165 if (ret
== RET_ILSEQ
)
167 if (ret
!= 1) abort();
169 conv
->istate
= state
;
183 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
184 if (ret
== RET_ILSEQ
)
186 if (ret
!= 1) abort();
187 if (*pwc
== 0x000a || *pwc
== 0x000d)
188 state2
= STATE_G2_NONE
;
190 conv
->istate
= state
;
194 case STATE_JISX0201ROMAN
:
196 int ret
= jisx0201_mbtowc(conv
,pwc
,s
,1);
197 if (ret
== RET_ILSEQ
)
199 if (ret
!= 1) abort();
200 if (*pwc
== 0x000a || *pwc
== 0x000d)
201 state2
= STATE_G2_NONE
;
203 conv
->istate
= state
;
207 case STATE_JISX0201KATAKANA
:
209 unsigned char buf
= c
+0x80;
210 int ret
= jisx0201_mbtowc(conv
,pwc
,&buf
,1);
211 if (ret
== RET_ILSEQ
)
213 if (ret
!= 1) abort();
215 conv
->istate
= state
;
222 if (s
[0] < 0x80 && s
[1] < 0x80) {
223 int ret
= jisx0208_mbtowc(conv
,pwc
,s
,2);
224 if (ret
== RET_ILSEQ
)
226 if (ret
!= 2) abort();
228 conv
->istate
= state
;
235 if (s
[0] < 0x80 && s
[1] < 0x80) {
236 int ret
= jisx0212_mbtowc(conv
,pwc
,s
,2);
237 if (ret
== RET_ILSEQ
)
239 if (ret
!= 2) abort();
241 conv
->istate
= state
;
248 if (s
[0] < 0x80 && s
[1] < 0x80) {
249 int ret
= gb2312_mbtowc(conv
,pwc
,s
,2);
250 if (ret
== RET_ILSEQ
)
252 if (ret
!= 2) abort();
254 conv
->istate
= state
;
261 if (s
[0] < 0x80 && s
[1] < 0x80) {
262 int ret
= ksc5601_mbtowc(conv
,pwc
,s
,2);
263 if (ret
== RET_ILSEQ
)
265 if (ret
!= 2) abort();
267 conv
->istate
= state
;
276 conv
->istate
= state
;
277 return RET_TOOFEW(count
);
281 iso2022_jp2_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
283 state_t state
= conv
->ostate
;
285 unsigned char buf
[2];
289 ret
= ascii_wctomb(conv
,buf
,wc
,1);
290 if (ret
!= RET_ILSEQ
) {
291 if (ret
!= 1) abort();
293 int count
= (state1
== STATE_ASCII
? 1 : 4);
296 if (state1
!= STATE_ASCII
) {
301 state1
= STATE_ASCII
;
304 if (wc
== 0x000a || wc
== 0x000d)
305 state2
= STATE_G2_NONE
;
307 conv
->ostate
= state
;
312 /* Try ISO-8859-1. */
313 ret
= iso8859_1_wctomb(conv
,buf
,wc
,1);
314 if (ret
!= RET_ILSEQ
) {
315 if (ret
!= 1) abort();
316 if (buf
[0] >= 0x80) {
317 int count
= (state2
== STATE_G2_ISO8859_1
? 3 : 6);
320 if (state2
!= STATE_G2_ISO8859_1
) {
325 state2
= STATE_G2_ISO8859_1
;
331 conv
->ostate
= state
;
336 /* Try ISO-8859-7. */
337 ret
= iso8859_7_wctomb(conv
,buf
,wc
,1);
338 if (ret
!= RET_ILSEQ
) {
339 if (ret
!= 1) abort();
340 if (buf
[0] >= 0x80) {
341 int count
= (state2
== STATE_G2_ISO8859_7
? 3 : 6);
344 if (state2
!= STATE_G2_ISO8859_7
) {
349 state2
= STATE_G2_ISO8859_7
;
355 conv
->ostate
= state
;
360 /* Try JIS X 0201-1976 Roman and Kana. */
361 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
362 if (ret
!= RET_ILSEQ
) {
363 if (ret
!= 1) abort();
365 int count
= (state1
== STATE_JISX0201ROMAN
? 1 : 4);
368 if (state1
!= STATE_JISX0201ROMAN
) {
373 state1
= STATE_JISX0201ROMAN
;
376 if (wc
== 0x000a || wc
== 0x000d)
377 state2
= STATE_G2_NONE
;
379 conv
->ostate
= state
;
382 int count
= (state1
== STATE_JISX0201KATAKANA
? 1 : 4);
385 if (state1
!= STATE_JISX0201KATAKANA
) {
390 state1
= STATE_JISX0201KATAKANA
;
394 conv
->ostate
= state
;
399 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
400 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
401 if (ret
!= RET_ILSEQ
) {
402 if (ret
!= 2) abort();
403 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
404 int count
= (state1
== STATE_JISX0208
? 2 : 5);
407 if (state1
!= STATE_JISX0208
) {
412 state1
= STATE_JISX0208
;
417 conv
->ostate
= state
;
422 /* Try JIS X 0212-1990. */
423 ret
= jisx0212_wctomb(conv
,buf
,wc
,2);
424 if (ret
!= RET_ILSEQ
) {
425 if (ret
!= 2) abort();
426 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
427 int count
= (state1
== STATE_JISX0212
? 2 : 6);
430 if (state1
!= STATE_JISX0212
) {
436 state1
= STATE_JISX0212
;
441 conv
->ostate
= state
;
446 /* Try GB 2312-1980. */
447 ret
= gb2312_wctomb(conv
,buf
,wc
,2);
448 if (ret
!= RET_ILSEQ
) {
449 if (ret
!= 2) abort();
450 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
451 int count
= (state1
== STATE_GB2312
? 2 : 5);
454 if (state1
!= STATE_GB2312
) {
459 state1
= STATE_GB2312
;
464 conv
->ostate
= state
;
469 /* Try KS C 5601-1992. */
470 ret
= ksc5601_wctomb(conv
,buf
,wc
,2);
471 if (ret
!= RET_ILSEQ
) {
472 if (ret
!= 2) abort();
473 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
474 int count
= (state1
== STATE_KSC5601
? 2 : 6);
477 if (state1
!= STATE_KSC5601
) {
483 state1
= STATE_KSC5601
;
488 conv
->ostate
= state
;
497 iso2022_jp2_reset (conv_t conv
, unsigned char *r
, int n
)
499 state_t state
= conv
->ostate
;
502 if (state1
!= STATE_ASCII
) {
508 /* conv->ostate = 0; will be done by the caller */
516 #undef STATE_G2_ISO8859_7
517 #undef STATE_G2_ISO8859_1
521 #undef STATE_JISX0212
522 #undef STATE_JISX0208
523 #undef STATE_JISX0201KATAKANA
524 #undef STATE_JISX0201ROMAN