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 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 conv
->istate
= state
;
282 return RET_SHIFT_ILSEQ(count
);
289 * The state can also contain one of the following values, << 16.
290 * Values >= STATE_TAG_LANGUAGE are temporary tag parsing states.
292 #define STATE_TAG_NONE 0
293 #define STATE_TAG_LANGUAGE 4
294 #define STATE_TAG_LANGUAGE_j 5
295 #define STATE_TAG_LANGUAGE_ja 1
296 #define STATE_TAG_LANGUAGE_k 6
297 #define STATE_TAG_LANGUAGE_ko 2
298 #define STATE_TAG_LANGUAGE_z 7
299 #define STATE_TAG_LANGUAGE_zh 3
301 #define SPLIT_STATE \
302 unsigned int state1 = state & 0xff, state2 = (state >> 8) & 0xff, state3 = state >> 16
303 #define COMBINE_STATE \
304 state = (state3 << 16) | (state2 << 8) | state1
307 iso2022_jp2_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
309 state_t state
= conv
->ostate
;
311 unsigned char buf
[2];
313 /* This defines the conversion preferences depending on the current
315 enum conversion
{ none
= 0, european
, japanese
, chinese
, korean
, other
};
316 static const unsigned int conversion_lists
[STATE_TAG_LANGUAGE
] = {
318 japanese
+ (european
<< 3) + (chinese
<< 6) + (korean
<< 9) + (other
<< 12),
319 /* STATE_TAG_LANGUAGE_ja */
320 japanese
+ (european
<< 3) + (chinese
<< 6) + (korean
<< 9) + (other
<< 12),
321 /* STATE_TAG_LANGUAGE_ko */
322 korean
+ (european
<< 3) + (japanese
<< 6) + (chinese
<< 9) + (other
<< 12),
323 /* STATE_TAG_LANGUAGE_zh */
324 chinese
+ (european
<< 3) + (japanese
<< 6) + (korean
<< 9) + (other
<< 12)
326 unsigned int conversion_list
;
328 /* Handle Unicode tag characters (range U+E0000..U+E007F). */
329 if ((wc
>> 7) == (0xe0000 >> 7)) {
331 if (c
>= 'A' && c
<= 'Z')
335 state3
= STATE_TAG_LANGUAGE
;
337 conv
->ostate
= state
;
340 if (state3
== STATE_TAG_LANGUAGE
) {
341 state3
= STATE_TAG_LANGUAGE_j
;
343 conv
->ostate
= state
;
348 if (state3
== STATE_TAG_LANGUAGE_j
) {
349 state3
= STATE_TAG_LANGUAGE_ja
;
351 conv
->ostate
= state
;
356 if (state3
== STATE_TAG_LANGUAGE
) {
357 state3
= STATE_TAG_LANGUAGE_k
;
359 conv
->ostate
= state
;
364 if (state3
== STATE_TAG_LANGUAGE_k
) {
365 state3
= STATE_TAG_LANGUAGE_ko
;
367 conv
->ostate
= state
;
372 if (state3
== STATE_TAG_LANGUAGE
) {
373 state3
= STATE_TAG_LANGUAGE_z
;
375 conv
->ostate
= state
;
380 if (state3
== STATE_TAG_LANGUAGE_z
) {
381 state3
= STATE_TAG_LANGUAGE_zh
;
383 conv
->ostate
= state
;
388 state3
= STATE_TAG_NONE
;
390 conv
->ostate
= state
;
395 /* Other tag characters reset the tag parsing state or are ignored. */
396 if (state3
>= STATE_TAG_LANGUAGE
)
397 state3
= STATE_TAG_NONE
;
399 conv
->ostate
= state
;
402 if (state3
>= STATE_TAG_LANGUAGE
)
403 state3
= STATE_TAG_NONE
;
406 ret
= ascii_wctomb(conv
,buf
,wc
,1);
407 if (ret
!= RET_ILUNI
) {
408 if (ret
!= 1) abort();
410 int count
= (state1
== STATE_ASCII
? 1 : 4);
413 if (state1
!= STATE_ASCII
) {
418 state1
= STATE_ASCII
;
421 if (wc
== 0x000a || wc
== 0x000d)
422 state2
= STATE_G2_NONE
;
424 conv
->ostate
= state
;
429 conversion_list
= conversion_lists
[state3
];
432 switch (conversion_list
& ((1 << 3) - 1)) {
436 /* Try ISO-8859-1. */
437 ret
= iso8859_1_wctomb(conv
,buf
,wc
,1);
438 if (ret
!= RET_ILUNI
) {
439 if (ret
!= 1) abort();
440 if (buf
[0] >= 0x80) {
441 int count
= (state2
== STATE_G2_ISO8859_1
? 3 : 6);
444 if (state2
!= STATE_G2_ISO8859_1
) {
449 state2
= STATE_G2_ISO8859_1
;
455 conv
->ostate
= state
;
460 /* Try ISO-8859-7. */
461 ret
= iso8859_7_wctomb(conv
,buf
,wc
,1);
462 if (ret
!= RET_ILUNI
) {
463 if (ret
!= 1) abort();
464 if (buf
[0] >= 0x80) {
465 int count
= (state2
== STATE_G2_ISO8859_7
? 3 : 6);
468 if (state2
!= STATE_G2_ISO8859_7
) {
473 state2
= STATE_G2_ISO8859_7
;
479 conv
->ostate
= state
;
488 /* Try JIS X 0201-1976 Roman. */
489 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
490 if (ret
!= RET_ILUNI
) {
491 if (ret
!= 1) abort();
493 int count
= (state1
== STATE_JISX0201ROMAN
? 1 : 4);
496 if (state1
!= STATE_JISX0201ROMAN
) {
501 state1
= STATE_JISX0201ROMAN
;
504 if (wc
== 0x000a || wc
== 0x000d)
505 state2
= STATE_G2_NONE
;
507 conv
->ostate
= state
;
512 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and
514 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
515 if (ret
!= RET_ILUNI
) {
516 if (ret
!= 2) abort();
517 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
518 int count
= (state1
== STATE_JISX0208
? 2 : 5);
521 if (state1
!= STATE_JISX0208
) {
526 state1
= STATE_JISX0208
;
531 conv
->ostate
= state
;
536 /* Try JIS X 0212-1990. */
537 ret
= jisx0212_wctomb(conv
,buf
,wc
,2);
538 if (ret
!= RET_ILUNI
) {
539 if (ret
!= 2) abort();
540 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
541 int count
= (state1
== STATE_JISX0212
? 2 : 6);
544 if (state1
!= STATE_JISX0212
) {
550 state1
= STATE_JISX0212
;
555 conv
->ostate
= state
;
564 /* Try GB 2312-1980. */
565 ret
= gb2312_wctomb(conv
,buf
,wc
,2);
566 if (ret
!= RET_ILUNI
) {
567 if (ret
!= 2) abort();
568 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
569 int count
= (state1
== STATE_GB2312
? 2 : 5);
572 if (state1
!= STATE_GB2312
) {
577 state1
= STATE_GB2312
;
582 conv
->ostate
= state
;
591 /* Try KS C 5601-1992. */
592 ret
= ksc5601_wctomb(conv
,buf
,wc
,2);
593 if (ret
!= RET_ILUNI
) {
594 if (ret
!= 2) abort();
595 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
596 int count
= (state1
== STATE_KSC5601
? 2 : 6);
599 if (state1
!= STATE_KSC5601
) {
605 state1
= STATE_KSC5601
;
610 conv
->ostate
= state
;
619 /* Try JIS X 0201-1976 Kana. This is not officially part of
620 ISO-2022-JP-2, according to RFC 1554. Therefore we try this
621 only after all other attempts. */
622 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
623 if (ret
!= RET_ILUNI
) {
624 if (ret
!= 1) abort();
625 if (buf
[0] >= 0x80) {
626 int count
= (state1
== STATE_JISX0201KATAKANA
? 1 : 4);
629 if (state1
!= STATE_JISX0201KATAKANA
) {
634 state1
= STATE_JISX0201KATAKANA
;
638 conv
->ostate
= state
;
649 conversion_list
= conversion_list
>> 3;
650 } while (conversion_list
!= 0);
656 iso2022_jp2_reset (conv_t conv
, unsigned char *r
, int n
)
658 state_t state
= conv
->ostate
;
662 if (state1
!= STATE_ASCII
) {
668 /* conv->ostate = 0; will be done by the caller */
676 #undef STATE_TAG_LANGUAGE_zh
677 #undef STATE_TAG_LANGUAGE_z
678 #undef STATE_TAG_LANGUAGE_ko
679 #undef STATE_TAG_LANGUAGE_k
680 #undef STATE_TAG_LANGUAGE_ja
681 #undef STATE_TAG_LANGUAGE_j
682 #undef STATE_TAG_LANGUAGE
683 #undef STATE_TAG_NONE
684 #undef STATE_G2_ISO8859_7
685 #undef STATE_G2_ISO8859_1
689 #undef STATE_JISX0212
690 #undef STATE_JISX0208
691 #undef STATE_JISX0201KATAKANA
692 #undef STATE_JISX0201ROMAN