1 /* Conversion module for ISO-2022-JP-3.
2 Copyright (C) 1998-2023 Free Software Foundation, Inc.
3 Copyright The GNU Toolchain Authors.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
30 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
33 /* Definitions used in the body of the `gconv' function. */
34 #define CHARSET_NAME "ISO-2022-JP-3//"
35 #define FROM_LOOP from_iso2022jp3_loop
36 #define TO_LOOP to_iso2022jp3_loop
39 #define ONE_DIRECTION 0
40 #define FROM_LOOP_MIN_NEEDED_FROM 1
41 #define FROM_LOOP_MAX_NEEDED_FROM 4
42 #define FROM_LOOP_MIN_NEEDED_TO 4
43 #define FROM_LOOP_MAX_NEEDED_TO 8
44 #define TO_LOOP_MIN_NEEDED_FROM 4
45 #define TO_LOOP_MAX_NEEDED_FROM 4
46 #define TO_LOOP_MIN_NEEDED_TO 1
47 #define TO_LOOP_MAX_NEEDED_TO 6
48 #define PREPARE_LOOP \
50 int *statep = &data->__statep->__count;
51 #define EXTRA_LOOP_ARGS , statep
54 /* The COUNT element of the state keeps track of the currently selected
55 character set. The possible values are: */
58 ASCII_set
= 0, /* Esc ( B */
59 JISX0208_1978_set
= 1 << 3, /* Esc $ @ */
60 JISX0208_1983_set
= 2 << 3, /* Esc $ B */
61 JISX0201_Roman_set
= 3 << 3, /* Esc ( J */
62 JISX0201_Kana_set
= 4 << 3, /* Esc ( I */
63 JISX0213_1_2000_set
= 5 << 3, /* Esc $ ( O */
64 JISX0213_2_set
= 6 << 3, /* Esc $ ( P */
65 JISX0213_1_2004_set
= 7 << 3, /* Esc $ ( Q */
66 CURRENT_SEL_MASK
= 7 << 3
69 /* During UCS-4 to ISO-2022-JP-3 conversion, the COUNT element of the
70 state also contains the last two bytes to be output, shifted by 6
71 bits, and a one-bit indicator whether they must be preceded by the
72 shift sequence, in bit 22. During ISO-2022-JP-3 to UCS-4
73 conversion, COUNT may also contain a non-zero pending wide
74 character, shifted by six bits. This happens for certain inputs in
75 JISX0213_1_2004_set and JISX0213_2_set if the second wide character
76 in a combining sequence cannot be written because the buffer is
79 /* Since this is a stateful encoding we have to provide code which resets
80 the output state to the initial state. This has to be done during the
82 #define EMIT_SHIFT_TO_INIT \
83 if ((data->__statep->__count & ~7) != ASCII_set) \
87 uint32_t ch = data->__statep->__count >> 6; \
89 if (__glibc_unlikely (ch != 0)) \
91 if (__glibc_likely (outbuf + 4 <= outend)) \
93 /* Write out the last character. */ \
96 data->__statep->__count &= 7; \
97 data->__statep->__count |= ASCII_set; \
100 /* We don't have enough room in the output buffer. */ \
101 status = __GCONV_FULL_OUTPUT; \
105 data->__statep->__count &= 7; \
106 data->__statep->__count |= ASCII_set; \
111 /* We are not in the initial state. To switch back we have \
112 to write out the buffered character and/or emit the sequence \
115 (data->__statep->__count >> 6 \
116 ? (data->__statep->__count >> 22 ? 3 : 0) + 2 \
118 + ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set \
121 if (__glibc_unlikely (outbuf + need > outend)) \
122 /* We don't have enough room in the output buffer. */ \
123 status = __GCONV_FULL_OUTPUT; \
126 if (data->__statep->__count >> 6) \
128 uint32_t lasttwo = data->__statep->__count >> 6; \
132 /* Write out the shift sequence before the last \
134 assert ((data->__statep->__count & CURRENT_SEL_MASK) \
135 == JISX0208_1983_set); \
140 /* Write out the last character. */ \
141 *outbuf++ = (lasttwo >> 8) & 0xff; \
142 *outbuf++ = lasttwo & 0xff; \
144 if ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set) \
146 /* Write out the shift sequence. */ \
151 data->__statep->__count &= 7; \
152 data->__statep->__count |= ASCII_set; \
158 /* Since we might have to reset input pointer we must be able to save
159 and restore the state. */
160 #define SAVE_RESET_STATE(Save) \
162 saved_state = *statep; \
164 *statep = saved_state
167 /* First define the conversion function from ISO-2022-JP-3 to UCS-4. */
168 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
169 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
170 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
171 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
172 #define LOOPFCT FROM_LOOP
177 /* Output any pending character. */ \
179 if (__glibc_unlikely (ch != 0)) \
181 put32 (outptr, ch); \
183 /* Remove the pending character, but preserve state bits. */ \
184 set &= (1 << 6) - 1; \
188 /* Otherwise read the next input byte. */ \
191 /* Recognize escape sequences. */ \
192 if (__glibc_unlikely (ch == ESC)) \
194 /* We now must be prepared to read two to three more bytes. \
195 If we have a match in the first byte but then the input buffer \
196 ends we terminate with an error since we must not risk missing \
197 an escape sequence just because it is not entirely in the \
198 current input buffer. */ \
199 if (__builtin_expect (inptr + 2 >= inend, 0) \
200 || (inptr[1] == '$' && inptr[2] == '(' \
201 && __builtin_expect (inptr + 3 >= inend, 0))) \
203 /* Not enough input available. */ \
204 result = __GCONV_INCOMPLETE_INPUT; \
208 if (inptr[1] == '(') \
210 if (inptr[2] == 'B') \
212 /* ASCII selected. */ \
217 else if (inptr[2] == 'J') \
219 /* JIS X 0201 selected. */ \
220 set = JISX0201_Roman_set; \
224 else if (inptr[2] == 'I') \
226 /* JIS X 0201 selected. */ \
227 set = JISX0201_Kana_set; \
232 else if (inptr[1] == '$') \
234 if (inptr[2] == '@') \
236 /* JIS X 0208-1978 selected. */ \
237 set = JISX0208_1978_set; \
241 else if (inptr[2] == 'B') \
243 /* JIS X 0208-1983 selected. */ \
244 set = JISX0208_1983_set; \
248 else if (inptr[2] == '(') \
250 if (inptr[3] == 'O' || inptr[3] == 'Q') \
252 /* JIS X 0213 plane 1 selected. */ \
253 /* In this direction we don't need to distinguish the \
254 versions from 2000 and 2004. */ \
255 set = JISX0213_1_2004_set; \
259 else if (inptr[3] == 'P') \
261 /* JIS X 0213 plane 2 selected. */ \
262 set = JISX0213_2_set; \
272 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
274 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
275 /* Almost done, just advance the input pointer. */ \
277 else if (set == JISX0201_Roman_set) \
279 /* Use the JIS X 0201 table. */ \
280 ch = jisx0201_to_ucs4 (ch); \
281 if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \
283 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
287 else if (set == JISX0201_Kana_set) \
289 /* Use the JIS X 0201 table. */ \
290 ch = jisx0201_to_ucs4 (ch + 0x80); \
291 if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \
293 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
297 else if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
299 /* XXX I don't have the tables for these two old variants of \
300 JIS X 0208. Therefore I'm using the tables for JIS X \
301 0208-1990. If somebody has problems with this please \
302 provide the appropriate tables. */ \
303 ch = jisx0208_to_ucs4 (&inptr, inend - inptr, 0); \
305 if (__glibc_unlikely (ch == 0)) \
307 result = __GCONV_INCOMPLETE_INPUT; \
310 else if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \
312 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
315 else /* (set == JISX0213_1_2004_set || set == JISX0213_2_set) */ \
317 if (__glibc_unlikely (inptr + 1 >= inend)) \
319 result = __GCONV_INCOMPLETE_INPUT; \
323 ch = jisx0213_to_ucs4 ( \
324 ((JISX0213_1_2004_set - set + (1 << 3)) << 5) + ch, \
327 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
331 /* It's a combining character. */ \
332 uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0]; \
333 uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1]; \
337 put32 (outptr, u1); \
340 /* See whether we have room for two characters. */ \
341 if (outptr + 4 <= outend) \
343 put32 (outptr, u2); \
348 /* Otherwise store only the first character now, and \
349 put the second one into the queue. */ \
351 /* Tell the caller why we terminate the loop. */ \
352 result = __GCONV_FULL_OUTPUT; \
359 put32 (outptr, ch); \
362 #define LOOP_NEED_FLAGS
363 #define EXTRA_LOOP_DECLS , int *statep
364 #define INIT_PARAMS int set = *statep
365 #define UPDATE_PARAMS *statep = set
366 #include <iconv/loop.c>
369 /* Next, define the other direction, from UCS-4 to ISO-2022-JP-3. */
371 /* Composition tables for each of the relevant combining characters. */
376 } comp_table_data
[] =
378 #define COMP_TABLE_IDX_02E5 0
379 #define COMP_TABLE_LEN_02E5 1
380 { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */
381 #define COMP_TABLE_IDX_02E9 (COMP_TABLE_IDX_02E5 + COMP_TABLE_LEN_02E5)
382 #define COMP_TABLE_LEN_02E9 1
383 { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */
384 #define COMP_TABLE_IDX_0300 (COMP_TABLE_IDX_02E9 + COMP_TABLE_LEN_02E9)
385 #define COMP_TABLE_LEN_0300 5
386 { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */
387 { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */
388 { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */
389 { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */
390 { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */
391 #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300)
392 #define COMP_TABLE_LEN_0301 4
393 { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */
394 { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */
395 { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */
396 { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */
397 #define COMP_TABLE_IDX_309A (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301)
398 #define COMP_TABLE_LEN_309A 14
399 { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */
400 { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */
401 { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */
402 { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */
403 { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */
404 { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */
405 { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */
406 { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */
407 { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */
408 { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */
409 { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */
410 { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */
411 { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */
412 { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */
415 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
416 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
417 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
418 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
419 #define LOOPFCT TO_LOOP
422 uint32_t ch = get32 (inptr); \
426 /* Attempt to combine the last character with this one. */ \
431 idx = COMP_TABLE_IDX_02E5, len = COMP_TABLE_LEN_02E5; \
432 else if (ch == 0x02e9) \
433 idx = COMP_TABLE_IDX_02E9, len = COMP_TABLE_LEN_02E9; \
434 else if (ch == 0x0300) \
435 idx = COMP_TABLE_IDX_0300, len = COMP_TABLE_LEN_0300; \
436 else if (ch == 0x0301) \
437 idx = COMP_TABLE_IDX_0301, len = COMP_TABLE_LEN_0301; \
438 else if (ch == 0x309a) \
439 idx = COMP_TABLE_IDX_309A, len = COMP_TABLE_LEN_309A; \
441 goto not_combining; \
444 if (comp_table_data[idx].base == (uint16_t) lasttwo) \
446 while (++idx, --len > 0); \
450 /* Output the combined character. */ \
451 /* We know the combined character is in JISX0213 plane 1, \
452 but the buffered character may have been in JISX0208 or in \
453 JISX0213 plane 1. */ \
456 || (set != JISX0213_1_2000_set && set != JISX0213_1_2004_set) \
459 if (__glibc_unlikely (outptr + need + 2 > outend)) \
461 result = __GCONV_FULL_OUTPUT; \
466 /* But first, output the escape sequence. */ \
471 set = JISX0213_1_2000_set; \
473 lasttwo = comp_table_data[idx].composed; \
474 *outptr++ = (lasttwo >> 8) & 0xff; \
475 *outptr++ = lasttwo & 0xff; \
482 /* Output the buffered character. */ \
483 /* We know it is in JISX0208 or in JISX0213 plane 1. */ \
485 size_t need = (lasttwo >> 16 ? 3 : 0); \
487 if (__glibc_unlikely (outptr + need + 2 > outend)) \
489 result = __GCONV_FULL_OUTPUT; \
494 /* But first, output the escape sequence. */ \
495 assert (set == JISX0208_1983_set); \
500 *outptr++ = (lasttwo >> 8) & 0xff; \
501 *outptr++ = lasttwo & 0xff; \
507 /* First see whether we can write the character using the currently \
508 selected character set. */ \
509 if (set == ASCII_set) \
511 /* Please note that the NUL byte is *not* matched if we are not \
512 currently using the ASCII charset. This is because we must \
513 switch to the initial state whenever a NUL byte is written. */ \
521 /* ISO-2022-JP recommends to encode the newline character always in \
522 ASCII since this allows a context-free interpretation of the \
523 characters at the beginning of the next line. Otherwise it would \
524 have to be known whether the last line ended using ASCII or \
526 else if (set == JISX0201_Roman_set) \
528 unsigned char buf[1]; \
529 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
530 && buf[0] > 0x20 && buf[0] < 0x80) \
532 *outptr++ = buf[0]; \
537 else if (set == JISX0201_Kana_set) \
539 unsigned char buf[1]; \
540 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
543 *outptr++ = buf[0] - 0x80; \
548 else if (/*set == JISX0208_1978_set || */ set == JISX0208_1983_set) \
550 size_t written = ucs4_to_jisx0208 (ch, outptr, outend - outptr); \
552 if (written != __UNKNOWN_10646_CHAR) \
554 uint32_t jch = ucs4_to_jisx0213 (ch); \
558 /* A possible match in comp_table_data. Buffer it. */ \
559 lasttwo = jch & 0x7f7f; \
563 if (__glibc_unlikely (written == 0)) \
565 result = __GCONV_FULL_OUTPUT; \
578 /* (set == JISX0213_1_2000_set || set == JISX0213_1_2004_set \
579 || set == JISX0213_2_set) */ \
580 uint32_t jch = ucs4_to_jisx0213 (ch); \
584 ? set == JISX0213_2_set \
585 : (set == JISX0213_1_2004_set \
586 || (set == JISX0213_1_2000_set \
587 && !jisx0213_added_in_2004_p (jch))))) \
591 /* A possible match in comp_table_data. Buffer it. */ \
593 /* We know it's a JISX 0213 plane 1 character. */ \
594 assert ((jch & 0x8000) == 0); \
596 lasttwo = jch & 0x7f7f; \
601 if (__glibc_unlikely (outptr + 1 >= outend)) \
603 result = __GCONV_FULL_OUTPUT; \
606 *outptr++ = (jch >> 8) & 0x7f; \
607 *outptr++ = jch & 0x7f; \
613 /* The attempts to use the currently selected character set failed, \
614 either because the character requires a different character set, \
615 or because the character is unknown. */ \
619 /* We must encode using ASCII. First write out the escape \
621 if (__glibc_unlikely (outptr + 3 > outend)) \
623 result = __GCONV_FULL_OUTPUT; \
632 if (__glibc_unlikely (outptr >= outend)) \
634 result = __GCONV_FULL_OUTPUT; \
641 unsigned char buf[2]; \
643 /* Try JIS X 0201 Roman. */ \
644 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
645 && buf[0] > 0x20 && buf[0] < 0x80) \
647 if (set != JISX0201_Roman_set) \
649 if (__glibc_unlikely (outptr + 3 > outend)) \
651 result = __GCONV_FULL_OUTPUT; \
657 set = JISX0201_Roman_set; \
660 if (__glibc_unlikely (outptr >= outend)) \
662 result = __GCONV_FULL_OUTPUT; \
665 *outptr++ = buf[0]; \
669 uint32_t jch = ucs4_to_jisx0213 (ch); \
671 /* Try JIS X 0208. */ \
672 size_t written = ucs4_to_jisx0208 (ch, buf, 2); \
673 if (written != __UNKNOWN_10646_CHAR) \
677 /* A possible match in comp_table_data. Buffer it. */ \
678 lasttwo = ((set != JISX0208_1983_set ? 1 : 0) << 16) \
680 set = JISX0208_1983_set; \
685 if (set != JISX0208_1983_set) \
687 if (__glibc_unlikely (outptr + 3 > outend)) \
689 result = __GCONV_FULL_OUTPUT; \
695 set = JISX0208_1983_set; \
698 if (__glibc_unlikely (outptr + 2 > outend)) \
700 result = __GCONV_FULL_OUTPUT; \
703 *outptr++ = buf[0]; \
704 *outptr++ = buf[1]; \
708 /* Try JIS X 0213. */ \
714 : jisx0213_added_in_2004_p (jch) \
715 ? JISX0213_1_2004_set \
716 : JISX0213_1_2000_set); \
718 if (set != new_set) \
720 if (__glibc_unlikely (outptr + 4 > outend)) \
722 result = __GCONV_FULL_OUTPUT; \
729 ((new_set - JISX0213_1_2000_set) >> 3) + 'O'; \
735 /* A possible match in comp_table_data. \
738 /* We know it's a JIS X 0213 plane 1 character. */ \
739 assert ((jch & 0x8000) == 0); \
741 lasttwo = jch & 0x7f7f; \
746 if (__glibc_unlikely (outptr + 1 >= outend)) \
748 result = __GCONV_FULL_OUTPUT; \
751 *outptr++ = (jch >> 8) & 0x7f; \
752 *outptr++ = jch & 0x7f; \
756 /* Try JIS X 0201 Katakana. This is officially not part \
757 of ISO-2022-JP-3. Therefore we try it after all other \
759 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
762 if (set != JISX0201_Kana_set) \
764 if (__builtin_expect (outptr + 3 > outend, 0)) \
766 result = __GCONV_FULL_OUTPUT; \
772 set = JISX0201_Kana_set; \
775 if (__glibc_unlikely (outptr >= outend)) \
777 result = __GCONV_FULL_OUTPUT; \
780 *outptr++ = buf[0] - 0x80; \
784 UNICODE_TAG_HANDLER (ch, 4); \
786 /* Illegal character. */ \
787 STANDARD_TO_LOOP_ERR_HANDLER (4); \
794 /* Now that we wrote the output increment the input pointer. */ \
797 #define LOOP_NEED_FLAGS
798 #define EXTRA_LOOP_DECLS , int *statep
799 #define INIT_PARAMS int set = *statep & CURRENT_SEL_MASK; \
800 uint32_t lasttwo = *statep >> 6
801 #define REINIT_PARAMS do \
803 set = *statep & CURRENT_SEL_MASK; \
804 lasttwo = *statep >> 6; \
807 #define UPDATE_PARAMS *statep = set | (lasttwo << 6)
808 #include <iconv/loop.c>
811 /* Now define the toplevel functions. */
812 #include <iconv/skeleton.c>