1 /* Conversion module for ISO-2022-JP-3.
2 Copyright (C) 1998-1999, 2000-2002, 2004, 2008
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998,
6 and Bruno Haible <bruno@clisp.org>, 2002.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
36 /* Definitions used in the body of the `gconv' function. */
37 #define CHARSET_NAME "ISO-2022-JP-3//"
38 #define FROM_LOOP from_iso2022jp3_loop
39 #define TO_LOOP to_iso2022jp3_loop
42 #define FROM_LOOP_MIN_NEEDED_FROM 1
43 #define FROM_LOOP_MAX_NEEDED_FROM 4
44 #define FROM_LOOP_MIN_NEEDED_TO 4
45 #define FROM_LOOP_MAX_NEEDED_TO 8
46 #define TO_LOOP_MIN_NEEDED_FROM 4
47 #define TO_LOOP_MAX_NEEDED_FROM 4
48 #define TO_LOOP_MIN_NEEDED_TO 1
49 #define TO_LOOP_MAX_NEEDED_TO 6
50 #define PREPARE_LOOP \
52 int *statep = &data->__statep->__count;
53 #define EXTRA_LOOP_ARGS , statep
56 /* The COUNT element of the state keeps track of the currently selected
57 character set. The possible values are: */
60 ASCII_set
= 0, /* Esc ( B */
61 JISX0208_1978_set
= 1 << 3, /* Esc $ @ */
62 JISX0208_1983_set
= 2 << 3, /* Esc $ B */
63 JISX0201_Roman_set
= 3 << 3, /* Esc ( J */
64 JISX0201_Kana_set
= 4 << 3, /* Esc ( I */
65 JISX0213_1_2000_set
= 5 << 3, /* Esc $ ( O */
66 JISX0213_2_set
= 6 << 3, /* Esc $ ( P */
67 JISX0213_1_2004_set
= 7 << 3, /* Esc $ ( Q */
68 CURRENT_SEL_MASK
= 7 << 3
71 /* During UCS-4 to ISO-2022-JP-3 conversion, the COUNT element of the state
72 also contains the last two bytes to be output, shifted by 6 bits, and a
73 one-bit indicator whether they must be preceded by the shift sequence,
76 /* Since this is a stateful encoding we have to provide code which resets
77 the output state to the initial state. This has to be done during the
79 #define EMIT_SHIFT_TO_INIT \
80 if ((data->__statep->__count & ~7) != ASCII_set) \
84 /* It's easy, we don't have to emit anything, we just reset the \
85 state for the input. */ \
86 data->__statep->__count &= 7; \
87 data->__statep->__count |= ASCII_set; \
91 /* We are not in the initial state. To switch back we have \
92 to write out the buffered character and/or emit the sequence \
95 (data->__statep->__count >> 6 \
96 ? (data->__statep->__count >> 22 ? 3 : 0) + 2 \
98 + ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set \
101 if (__builtin_expect (outbuf + need > outend, 0)) \
102 /* We don't have enough room in the output buffer. */ \
103 status = __GCONV_FULL_OUTPUT; \
106 if (data->__statep->__count >> 6) \
108 uint32_t lasttwo = data->__statep->__count >> 6; \
112 /* Write out the shift sequence before the last \
114 assert ((data->__statep->__count & CURRENT_SEL_MASK) \
115 == JISX0208_1983_set); \
120 /* Write out the last character. */ \
121 *outbuf++ = (lasttwo >> 8) & 0xff; \
122 *outbuf++ = lasttwo & 0xff; \
124 if ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set) \
126 /* Write out the shift sequence. */ \
131 data->__statep->__count &= 7; \
132 data->__statep->__count |= ASCII_set; \
138 /* Since we might have to reset input pointer we must be able to save
139 and retore the state. */
140 #define SAVE_RESET_STATE(Save) \
142 saved_state = *statep; \
144 *statep = saved_state
147 /* First define the conversion function from ISO-2022-JP-3 to UCS-4. */
148 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
149 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
150 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
151 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
152 #define LOOPFCT FROM_LOOP
155 uint32_t ch = *inptr; \
157 /* Recognize escape sequences. */ \
158 if (__builtin_expect (ch == ESC, 0)) \
160 /* We now must be prepared to read two to three more bytes. \
161 If we have a match in the first byte but then the input buffer \
162 ends we terminate with an error since we must not risk missing \
163 an escape sequence just because it is not entirely in the \
164 current input buffer. */ \
165 if (__builtin_expect (inptr + 2 >= inend, 0) \
166 || (inptr[1] == '$' && inptr[2] == '(' \
167 && __builtin_expect (inptr + 3 >= inend, 0))) \
169 /* Not enough input available. */ \
170 result = __GCONV_INCOMPLETE_INPUT; \
174 if (inptr[1] == '(') \
176 if (inptr[2] == 'B') \
178 /* ASCII selected. */ \
183 else if (inptr[2] == 'J') \
185 /* JIS X 0201 selected. */ \
186 set = JISX0201_Roman_set; \
190 else if (inptr[2] == 'I') \
192 /* JIS X 0201 selected. */ \
193 set = JISX0201_Kana_set; \
198 else if (inptr[1] == '$') \
200 if (inptr[2] == '@') \
202 /* JIS X 0208-1978 selected. */ \
203 set = JISX0208_1978_set; \
207 else if (inptr[2] == 'B') \
209 /* JIS X 0208-1983 selected. */ \
210 set = JISX0208_1983_set; \
214 else if (inptr[2] == '(') \
216 if (inptr[3] == 'O' || inptr[3] == 'Q') \
218 /* JIS X 0213 plane 1 selected. */ \
219 /* In this direction we don't need to distinguish the \
220 versions from 2000 and 2004. */ \
221 set = JISX0213_1_2004_set; \
225 else if (inptr[3] == 'P') \
227 /* JIS X 0213 plane 2 selected. */ \
228 set = JISX0213_2_set; \
238 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
240 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
241 /* Almost done, just advance the input pointer. */ \
243 else if (set == JISX0201_Roman_set) \
245 /* Use the JIS X 0201 table. */ \
246 ch = jisx0201_to_ucs4 (ch); \
247 if (__builtin_expect (ch == __UNKNOWN_10646_CHAR, 0)) \
249 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
253 else if (set == JISX0201_Kana_set) \
255 /* Use the JIS X 0201 table. */ \
256 ch = jisx0201_to_ucs4 (ch + 0x80); \
257 if (__builtin_expect (ch == __UNKNOWN_10646_CHAR, 0)) \
259 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
263 else if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
265 /* XXX I don't have the tables for these two old variants of \
266 JIS X 0208. Therefore I'm using the tables for JIS X \
267 0208-1990. If somebody has problems with this please \
268 provide the appropriate tables. */ \
269 ch = jisx0208_to_ucs4 (&inptr, inend - inptr, 0); \
271 if (__builtin_expect (ch == 0, 0)) \
273 result = __GCONV_INCOMPLETE_INPUT; \
276 else if (__builtin_expect (ch == __UNKNOWN_10646_CHAR, 0)) \
278 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
281 else /* (set == JISX0213_1_2004_set || set == JISX0213_2_set) */ \
283 if (__builtin_expect (inptr + 1 >= inend, 0)) \
285 result = __GCONV_INCOMPLETE_INPUT; \
289 ch = jisx0213_to_ucs4 ( \
290 ((JISX0213_1_2004_set - set + (1 << 3)) << 5) + ch, \
293 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
297 /* It's a combining character. */ \
298 uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0]; \
299 uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1]; \
301 /* See whether we have room for two characters. */ \
302 if (outptr + 8 <= outend) \
305 put32 (outptr, u1); \
307 put32 (outptr, u2); \
313 result = __GCONV_FULL_OUTPUT; \
321 put32 (outptr, ch); \
324 #define LOOP_NEED_FLAGS
325 #define EXTRA_LOOP_DECLS , int *statep
326 #define INIT_PARAMS int set = *statep
327 #define UPDATE_PARAMS *statep = set
328 #include <iconv/loop.c>
331 /* Next, define the other direction, from UCS-4 to ISO-2022-JP-3. */
333 /* Composition tables for each of the relevant combining characters. */
338 } comp_table_data
[] =
340 #define COMP_TABLE_IDX_02E5 0
341 #define COMP_TABLE_LEN_02E5 1
342 { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */
343 #define COMP_TABLE_IDX_02E9 (COMP_TABLE_IDX_02E5 + COMP_TABLE_LEN_02E5)
344 #define COMP_TABLE_LEN_02E9 1
345 { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */
346 #define COMP_TABLE_IDX_0300 (COMP_TABLE_IDX_02E9 + COMP_TABLE_LEN_02E9)
347 #define COMP_TABLE_LEN_0300 5
348 { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */
349 { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */
350 { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */
351 { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */
352 { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */
353 #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300)
354 #define COMP_TABLE_LEN_0301 4
355 { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */
356 { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */
357 { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */
358 { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */
359 #define COMP_TABLE_IDX_309A (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301)
360 #define COMP_TABLE_LEN_309A 14
361 { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */
362 { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */
363 { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */
364 { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */
365 { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */
366 { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */
367 { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */
368 { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */
369 { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */
370 { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */
371 { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */
372 { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */
373 { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */
374 { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */
377 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
378 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
379 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
380 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
381 #define LOOPFCT TO_LOOP
384 uint32_t ch = get32 (inptr); \
388 /* Attempt to combine the last character with this one. */ \
393 idx = COMP_TABLE_IDX_02E5, len = COMP_TABLE_LEN_02E5; \
394 else if (ch == 0x02e9) \
395 idx = COMP_TABLE_IDX_02E9, len = COMP_TABLE_LEN_02E9; \
396 else if (ch == 0x0300) \
397 idx = COMP_TABLE_IDX_0300, len = COMP_TABLE_LEN_0300; \
398 else if (ch == 0x0301) \
399 idx = COMP_TABLE_IDX_0301, len = COMP_TABLE_LEN_0301; \
400 else if (ch == 0x309a) \
401 idx = COMP_TABLE_IDX_309A, len = COMP_TABLE_LEN_309A; \
403 goto not_combining; \
406 if (comp_table_data[idx].base == (uint16_t) lasttwo) \
408 while (++idx, --len > 0); \
412 /* Output the combined character. */ \
413 /* We know the combined character is in JISX0213 plane 1, \
414 but the buffered character may have been in JISX0208 or in \
415 JISX0213 plane 1. */ \
418 || (set != JISX0213_1_2000_set && set != JISX0213_1_2004_set) \
421 if (__builtin_expect (outptr + need + 2 > outend, 0)) \
423 result = __GCONV_FULL_OUTPUT; \
428 /* But first, output the escape sequence. */ \
433 set = JISX0213_1_2000_set; \
435 lasttwo = comp_table_data[idx].composed; \
436 *outptr++ = (lasttwo >> 8) & 0xff; \
437 *outptr++ = lasttwo & 0xff; \
444 /* Output the buffered character. */ \
445 /* We know it is in JISX0208 or in JISX0213 plane 1. */ \
447 size_t need = (lasttwo >> 16 ? 3 : 0); \
449 if (__builtin_expect (outptr + need + 2 > outend, 0)) \
451 result = __GCONV_FULL_OUTPUT; \
456 /* But first, output the escape sequence. */ \
457 assert (set == JISX0208_1983_set); \
462 *outptr++ = (lasttwo >> 8) & 0xff; \
463 *outptr++ = lasttwo & 0xff; \
469 /* First see whether we can write the character using the currently \
470 selected character set. */ \
471 if (set == ASCII_set) \
473 /* Please note that the NUL byte is *not* matched if we are not \
474 currently using the ASCII charset. This is because we must \
475 switch to the initial state whenever a NUL byte is written. */ \
483 /* ISO-2022-JP recommends to encode the newline character always in \
484 ASCII since this allows a context-free interpretation of the \
485 characters at the beginning of the next line. Otherwise it would \
486 have to be known whether the last line ended using ASCII or \
488 else if (set == JISX0201_Roman_set) \
490 unsigned char buf[1]; \
491 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
492 && buf[0] > 0x20 && buf[0] < 0x80) \
494 *outptr++ = buf[0]; \
499 else if (set == JISX0201_Kana_set) \
501 unsigned char buf[1]; \
502 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
505 *outptr++ = buf[0] - 0x80; \
510 else if (/*set == JISX0208_1978_set || */ set == JISX0208_1983_set) \
512 size_t written = ucs4_to_jisx0208 (ch, outptr, outend - outptr); \
514 if (written != __UNKNOWN_10646_CHAR) \
516 uint32_t jch = ucs4_to_jisx0213 (ch); \
520 /* A possible match in comp_table_data. Buffer it. */ \
521 lasttwo = jch & 0x7f7f; \
525 if (__builtin_expect (written == 0, 0)) \
527 result = __GCONV_FULL_OUTPUT; \
540 /* (set == JISX0213_1_2000_set || set == JISX0213_1_2004_set \
541 || set == JISX0213_2_set) */ \
542 uint32_t jch = ucs4_to_jisx0213 (ch); \
546 ? set == JISX0213_2_set \
547 : (set == JISX0213_1_2004_set \
548 || (set == JISX0213_1_2000_set \
549 && !jisx0213_added_in_2004_p (jch))))) \
553 /* A possible match in comp_table_data. Buffer it. */ \
555 /* We know it's a JISX 0213 plane 1 character. */ \
556 assert ((jch & 0x8000) == 0); \
558 lasttwo = jch & 0x7f7f; \
563 if (__builtin_expect (outptr + 1 >= outend, 0)) \
565 result = __GCONV_FULL_OUTPUT; \
568 *outptr++ = (jch >> 8) & 0x7f; \
569 *outptr++ = jch & 0x7f; \
575 /* The attempts to use the currently selected character set failed, \
576 either because the character requires a different character set, \
577 or because the character is unknown. */ \
581 /* We must encode using ASCII. First write out the escape \
583 if (__builtin_expect (outptr + 3 > outend, 0)) \
585 result = __GCONV_FULL_OUTPUT; \
594 if (__builtin_expect (outptr >= outend, 0)) \
596 result = __GCONV_FULL_OUTPUT; \
603 unsigned char buf[2]; \
605 /* Try JIS X 0201 Roman. */ \
606 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
607 && buf[0] > 0x20 && buf[0] < 0x80) \
609 if (set != JISX0201_Roman_set) \
611 if (__builtin_expect (outptr + 3 > outend, 0)) \
613 result = __GCONV_FULL_OUTPUT; \
619 set = JISX0201_Roman_set; \
622 if (__builtin_expect (outptr >= outend, 0)) \
624 result = __GCONV_FULL_OUTPUT; \
627 *outptr++ = buf[0]; \
631 uint32_t jch = ucs4_to_jisx0213 (ch); \
633 /* Try JIS X 0208. */ \
634 size_t written = ucs4_to_jisx0208 (ch, buf, 2); \
635 if (written != __UNKNOWN_10646_CHAR) \
639 /* A possible match in comp_table_data. Buffer it. */ \
640 lasttwo = ((set != JISX0208_1983_set ? 1 : 0) << 16) \
642 set = JISX0208_1983_set; \
647 if (set != JISX0208_1983_set) \
649 if (__builtin_expect (outptr + 3 > outend, 0)) \
651 result = __GCONV_FULL_OUTPUT; \
657 set = JISX0208_1983_set; \
660 if (__builtin_expect (outptr + 2 > outend, 0)) \
662 result = __GCONV_FULL_OUTPUT; \
665 *outptr++ = buf[0]; \
666 *outptr++ = buf[1]; \
670 /* Try JIS X 0213. */ \
676 : jisx0213_added_in_2004_p (jch) \
677 ? JISX0213_1_2004_set \
678 : JISX0213_1_2000_set); \
680 if (set != new_set) \
682 if (__builtin_expect (outptr + 4 > outend, 0)) \
684 result = __GCONV_FULL_OUTPUT; \
691 ((new_set - JISX0213_1_2000_set) >> 3) + 'O'; \
697 /* A possible match in comp_table_data. \
700 /* We know it's a JIS X 0213 plane 1 character. */ \
701 assert ((jch & 0x8000) == 0); \
703 lasttwo = jch & 0x7f7f; \
708 if (__builtin_expect (outptr + 1 >= outend, 0)) \
710 result = __GCONV_FULL_OUTPUT; \
713 *outptr++ = (jch >> 8) & 0x7f; \
714 *outptr++ = jch & 0x7f; \
718 /* Try JIS X 0201 Katakana. This is officially not part \
719 of ISO-2022-JP-3. Therefore we try it after all other \
721 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
724 if (set != JISX0201_Kana_set) \
726 if (__builtin_expect (outptr + 3 > outend, 0)) \
728 result = __GCONV_FULL_OUTPUT; \
734 set = JISX0201_Kana_set; \
737 if (__builtin_expect (outptr >= outend, 0)) \
739 result = __GCONV_FULL_OUTPUT; \
742 *outptr++ = buf[0] - 0x80; \
746 UNICODE_TAG_HANDLER (ch, 4); \
748 /* Illegal character. */ \
749 STANDARD_TO_LOOP_ERR_HANDLER (4); \
756 /* Now that we wrote the output increment the input pointer. */ \
759 #define LOOP_NEED_FLAGS
760 #define EXTRA_LOOP_DECLS , int *statep
761 #define INIT_PARAMS int set = *statep & CURRENT_SEL_MASK; \
762 uint32_t lasttwo = *statep >> 6
763 #define REINIT_PARAMS do \
765 set = *statep & CURRENT_SEL_MASK; \
766 lasttwo = *statep >> 6; \
769 #define UPDATE_PARAMS *statep = set | (lasttwo << 6)
770 #include <iconv/loop.c>
773 /* Now define the toplevel functions. */
774 #include <iconv/skeleton.c>