2 * Copyright (C) 1999-2004, 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.
30 * The state is composed of one of the following values
32 #define STATE_ASCII 0 /* Esc ( B */
33 #define STATE_JISX0201ROMAN 1 /* Esc ( J */
34 #define STATE_JISX0201KATAKANA 2 /* Esc ( I */
35 #define STATE_JISX0208 3 /* Esc $ @ or Esc $ B */
36 #define STATE_JISX02131 4 /* Esc $ ( O or Esc $ ( Q*/
37 #define STATE_JISX02132 5 /* Esc $ ( P */
40 * In the ISO-2022-JP-3 to UCS-4 direction, the state also holds the last
41 * character to be output, shifted by 3 bits.
45 iso2022_jp3_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, int n
)
47 ucs4_t last_wc
= conv
->istate
>> 3;
49 /* Output the buffered character. */
52 return 0; /* Don't advance the input pointer. */
54 state_t state
= conv
->istate
;
71 state
= STATE_JISX0201ROMAN
;
78 state
= STATE_JISX0201KATAKANA
;
87 if (s
[2] == '@' || s
[2] == 'B') {
88 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
89 state
= STATE_JISX0208
;
98 if (s
[3] == 'O' || s
[3] == 'Q') {
99 state
= STATE_JISX02131
;
106 state
= STATE_JISX02132
;
122 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
123 if (ret
== RET_ILSEQ
)
125 if (ret
!= 1) abort();
126 conv
->istate
= state
;
130 case STATE_JISX0201ROMAN
:
132 int ret
= jisx0201_mbtowc(conv
,pwc
,s
,1);
133 if (ret
== RET_ILSEQ
)
135 if (ret
!= 1) abort();
136 conv
->istate
= state
;
140 case STATE_JISX0201KATAKANA
:
142 unsigned char buf
= c
+0x80;
143 int ret
= jisx0201_mbtowc(conv
,pwc
,&buf
,1);
144 if (ret
== RET_ILSEQ
)
146 if (ret
!= 1) abort();
147 conv
->istate
= state
;
154 if (s
[0] < 0x80 && s
[1] < 0x80) {
155 int ret
= jisx0208_mbtowc(conv
,pwc
,s
,2);
156 if (ret
== RET_ILSEQ
)
158 if (ret
!= 2) abort();
159 conv
->istate
= state
;
163 case STATE_JISX02131
:
164 case STATE_JISX02132
:
167 if (s
[0] < 0x80 && s
[1] < 0x80) {
168 ucs4_t wc
= jisx0213_to_ucs4(((state
-STATE_JISX02131
+1)<<8)+s
[0],s
[1]);
171 /* It's a combining character. */
172 ucs4_t wc1
= jisx0213_to_ucs_combining
[wc
- 1][0];
173 ucs4_t wc2
= jisx0213_to_ucs_combining
[wc
- 1][1];
174 /* We cannot output two Unicode characters at once. So,
175 output the first character and buffer the second one. */
177 conv
->istate
= (wc2
<< 3) | state
;
180 conv
->istate
= state
;
189 conv
->istate
= state
;
190 return RET_TOOFEW(count
);
193 conv
->istate
= state
;
194 return RET_SHIFT_ILSEQ(count
);
199 iso2022_jp3_flushwc (conv_t conv
, ucs4_t
*pwc
)
201 ucs4_t last_wc
= conv
->istate
>> 3;
203 /* Output the buffered character. */
212 * In the UCS-4 to ISO-2022-JP-3 direction, the state also holds the last two
213 * bytes to be output, shifted by 3 bits, and the STATE_xxxxx value that was
214 * effective before this buffered character, shifted by 19 bits.
217 /* Composition tables for each of the relevant combining characters. */
218 static const struct { unsigned short base
; unsigned short composed
; } iso2022_jp3_comp_table_data
[] = {
219 #define iso2022_jp3_comp_table02e5_idx 0
220 #define iso2022_jp3_comp_table02e5_len 1
221 { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */
222 #define iso2022_jp3_comp_table02e9_idx (iso2022_jp3_comp_table02e5_idx+iso2022_jp3_comp_table02e5_len)
223 #define iso2022_jp3_comp_table02e9_len 1
224 { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */
225 #define iso2022_jp3_comp_table0300_idx (iso2022_jp3_comp_table02e9_idx+iso2022_jp3_comp_table02e9_len)
226 #define iso2022_jp3_comp_table0300_len 5
227 { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */
228 { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */
229 { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */
230 { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */
231 { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */
232 #define iso2022_jp3_comp_table0301_idx (iso2022_jp3_comp_table0300_idx+iso2022_jp3_comp_table0300_len)
233 #define iso2022_jp3_comp_table0301_len 4
234 { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */
235 { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */
236 { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */
237 { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */
238 #define iso2022_jp3_comp_table309a_idx (iso2022_jp3_comp_table0301_idx+iso2022_jp3_comp_table0301_len)
239 #define iso2022_jp3_comp_table309a_len 14
240 { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */
241 { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */
242 { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */
243 { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */
244 { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */
245 { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */
246 { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */
247 { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */
248 { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */
249 { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */
250 { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */
251 { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */
252 { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */
253 { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */
256 #define SPLIT_STATE \
257 unsigned short lasttwo = state >> 3; state_t prevstate = state >> 19; state &= 7
258 #define COMBINE_STATE \
259 state |= (prevstate << 19) | (lasttwo << 3)
260 #define COMBINE_STATE_NO_LASTTWO \
261 /* assume lasttwo == 0, then prevstate is ignored */
264 iso2022_jp3_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
267 unsigned char buf
[2];
270 state_t state
= conv
->ostate
;
274 /* Attempt to combine the last character with this one. */
279 idx
= iso2022_jp3_comp_table02e5_idx
,
280 len
= iso2022_jp3_comp_table02e5_len
;
281 else if (wc
== 0x02e9)
282 idx
= iso2022_jp3_comp_table02e9_idx
,
283 len
= iso2022_jp3_comp_table02e9_len
;
284 else if (wc
== 0x0300)
285 idx
= iso2022_jp3_comp_table0300_idx
,
286 len
= iso2022_jp3_comp_table0300_len
;
287 else if (wc
== 0x0301)
288 idx
= iso2022_jp3_comp_table0301_idx
,
289 len
= iso2022_jp3_comp_table0301_len
;
290 else if (wc
== 0x309a)
291 idx
= iso2022_jp3_comp_table309a_idx
,
292 len
= iso2022_jp3_comp_table309a_len
;
297 if (iso2022_jp3_comp_table_data
[idx
].base
== lasttwo
)
299 while (++idx
, --len
> 0);
302 /* Output the combined character. */
303 /* We know the combined character is in JISX0213 plane 1, but
304 the buffered character may have been in JISX0208 or in
306 count
= (state
!= STATE_JISX02131
? 4 : 0) + 2;
309 if (state
!= STATE_JISX02131
) {
315 state
= STATE_JISX02131
;
317 lasttwo
= iso2022_jp3_comp_table_data
[idx
].composed
;
318 r
[0] = (lasttwo
>> 8) & 0xff;
319 r
[1] = lasttwo
& 0xff;
320 COMBINE_STATE_NO_LASTTWO
;
321 conv
->ostate
= state
;
326 /* Output the buffered character. */
327 /* We know it is in JISX0208 or in JISX0213 plane 1. */
328 count
= (prevstate
!= state
? 3 : 0) + 2;
331 if (prevstate
!= state
) {
332 if (state
!= STATE_JISX0208
) abort();
338 r
[0] = (lasttwo
>> 8) & 0xff;
339 r
[1] = lasttwo
& 0xff;
344 ret
= ascii_wctomb(conv
,buf
,wc
,1);
345 if (ret
!= RET_ILUNI
) {
346 if (ret
!= 1) abort();
348 count
+= (state
== STATE_ASCII
? 1 : 4);
351 if (state
!= STATE_ASCII
) {
359 COMBINE_STATE_NO_LASTTWO
;
360 conv
->ostate
= state
;
365 /* Try JIS X 0201-1976 Roman. */
366 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
367 if (ret
!= RET_ILUNI
) {
368 if (ret
!= 1) abort();
370 count
+= (state
== STATE_JISX0201ROMAN
? 1 : 4);
373 if (state
!= STATE_JISX0201ROMAN
) {
378 state
= STATE_JISX0201ROMAN
;
381 COMBINE_STATE_NO_LASTTWO
;
382 conv
->ostate
= state
;
387 jch
= ucs4_to_jisx0213(wc
);
389 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
390 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
391 if (ret
!= RET_ILUNI
) {
392 if (ret
!= 2) abort();
393 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
395 /* A possible match in comp_table_data. Buffer it. */
397 lasttwo
= jch
& 0x7f7f;
398 state
= STATE_JISX0208
;
400 conv
->ostate
= state
;
403 count
+= (state
== STATE_JISX0208
? 2 : 5);
406 if (state
!= STATE_JISX0208
) {
411 state
= STATE_JISX0208
;
415 COMBINE_STATE_NO_LASTTWO
;
416 conv
->ostate
= state
;
422 /* Try JISX 0213 plane 1 and JISX 0213 plane 2. */
425 /* JISX 0213 plane 2. */
426 if (state
!= STATE_JISX02132
) {
435 state
= STATE_JISX02132
;
438 /* JISX 0213 plane 1. */
439 if (state
!= STATE_JISX02131
) {
448 state
= STATE_JISX02131
;
452 /* A possible match in comp_table_data. We have to buffer it. */
453 /* We know it's a JISX 0213 plane 1 character. */
454 if (jch
& 0x8000) abort();
456 lasttwo
= jch
& 0x7f7f;
458 conv
->ostate
= state
;
464 r
[0] = (jch
>> 8) & 0x7f;
466 COMBINE_STATE_NO_LASTTWO
;
467 conv
->ostate
= state
;
471 /* Try JIS X 0201-1976 Katakana. This is not officially part of
472 ISO-2022-JP-3. Therefore we try it after all other attempts. */
473 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
474 if (ret
!= RET_ILUNI
) {
475 if (ret
!= 1) abort();
476 if (buf
[0] >= 0x80) {
477 count
+= (state
== STATE_JISX0201KATAKANA
? 1 : 4);
480 if (state
!= STATE_JISX0201KATAKANA
) {
485 state
= STATE_JISX0201KATAKANA
;
488 COMBINE_STATE_NO_LASTTWO
;
489 conv
->ostate
= state
;
498 iso2022_jp3_reset (conv_t conv
, unsigned char *r
, int n
)
500 state_t state
= conv
->ostate
;
504 (lasttwo
? (prevstate
!= state
? 3 : 0) + 2 : 0)
505 + (state
!= STATE_ASCII
? 3 : 0);
509 if (prevstate
!= state
) {
510 if (state
!= STATE_JISX0208
) abort();
516 r
[0] = (lasttwo
>> 8) & 0xff;
517 r
[1] = lasttwo
& 0xff;
520 if (state
!= STATE_ASCII
) {
525 /* conv->ostate = 0; will be done by the caller */
530 #undef COMBINE_STATE_NO_LASTTWO
533 #undef STATE_JISX02132
534 #undef STATE_JISX02131
535 #undef STATE_JISX0208
536 #undef STATE_JISX0201KATAKANA
537 #undef STATE_JISX0201ROMAN