1 /* Conversion module for ISO-2022-JP-3.
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998,
5 and Bruno Haible <bruno@clisp.org>, 2002.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
31 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
34 /* Definitions used in the body of the `gconv' function. */
35 #define CHARSET_NAME "ISO-2022-JP-3//"
36 #define FROM_LOOP from_iso2022jp3_loop
37 #define TO_LOOP to_iso2022jp3_loop
40 #define ONE_DIRECTION 0
41 #define FROM_LOOP_MIN_NEEDED_FROM 1
42 #define FROM_LOOP_MAX_NEEDED_FROM 4
43 #define FROM_LOOP_MIN_NEEDED_TO 4
44 #define FROM_LOOP_MAX_NEEDED_TO 8
45 #define TO_LOOP_MIN_NEEDED_FROM 4
46 #define TO_LOOP_MAX_NEEDED_FROM 4
47 #define TO_LOOP_MIN_NEEDED_TO 1
48 #define TO_LOOP_MAX_NEEDED_TO 6
49 #define PREPARE_LOOP \
51 int *statep = &data->__statep->__count;
52 #define EXTRA_LOOP_ARGS , statep
55 /* The COUNT element of the state keeps track of the currently selected
56 character set. The possible values are: */
59 ASCII_set
= 0, /* Esc ( B */
60 JISX0208_1978_set
= 1 << 3, /* Esc $ @ */
61 JISX0208_1983_set
= 2 << 3, /* Esc $ B */
62 JISX0201_Roman_set
= 3 << 3, /* Esc ( J */
63 JISX0201_Kana_set
= 4 << 3, /* Esc ( I */
64 JISX0213_1_2000_set
= 5 << 3, /* Esc $ ( O */
65 JISX0213_2_set
= 6 << 3, /* Esc $ ( P */
66 JISX0213_1_2004_set
= 7 << 3, /* Esc $ ( Q */
67 CURRENT_SEL_MASK
= 7 << 3
70 /* During UCS-4 to ISO-2022-JP-3 conversion, the COUNT element of the
71 state also contains the last two bytes to be output, shifted by 6
72 bits, and a one-bit indicator whether they must be preceded by the
73 shift sequence, in bit 22. During ISO-2022-JP-3 to UCS-4
74 conversion, COUNT may also contain a non-zero pending wide
75 character, shifted by six bits. This happens for certain inputs in
76 JISX0213_1_2004_set and JISX0213_2_set if the second wide character
77 in a combining sequence cannot be written because the buffer is
80 /* Since this is a stateful encoding we have to provide code which resets
81 the output state to the initial state. This has to be done during the
83 #define EMIT_SHIFT_TO_INIT \
84 if (data->__statep->__count != ASCII_set) \
88 if (__glibc_likely (outbuf + 4 <= outend)) \
90 /* Write out the last character. */ \
91 *((uint32_t *) outbuf) = data->__statep->__count >> 6; \
92 outbuf += sizeof (uint32_t); \
93 data->__statep->__count = ASCII_set; \
96 /* We don't have enough room in the output buffer. */ \
97 status = __GCONV_FULL_OUTPUT; \
101 /* We are not in the initial state. To switch back we have \
102 to write out the buffered character and/or emit the sequence \
105 (data->__statep->__count >> 6 \
106 ? (data->__statep->__count >> 22 ? 3 : 0) + 2 \
108 + ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set \
111 if (__glibc_unlikely (outbuf + need > outend)) \
112 /* We don't have enough room in the output buffer. */ \
113 status = __GCONV_FULL_OUTPUT; \
116 if (data->__statep->__count >> 6) \
118 uint32_t lasttwo = data->__statep->__count >> 6; \
122 /* Write out the shift sequence before the last \
124 assert ((data->__statep->__count & CURRENT_SEL_MASK) \
125 == JISX0208_1983_set); \
130 /* Write out the last character. */ \
131 *outbuf++ = (lasttwo >> 8) & 0xff; \
132 *outbuf++ = lasttwo & 0xff; \
134 if ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set) \
136 /* Write out the shift sequence. */ \
141 data->__statep->__count &= 7; \
142 data->__statep->__count |= ASCII_set; \
148 /* Since we might have to reset input pointer we must be able to save
149 and retore the state. */
150 #define SAVE_RESET_STATE(Save) \
152 saved_state = *statep; \
154 *statep = saved_state
157 /* First define the conversion function from ISO-2022-JP-3 to UCS-4. */
158 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
159 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
160 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
161 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
162 #define LOOPFCT FROM_LOOP
167 /* Output any pending character. */ \
169 if (__glibc_unlikely (ch != 0)) \
171 put32 (outptr, ch); \
173 /* Remove the pending character, but preserve state bits. */ \
174 set &= (1 << 6) - 1; \
178 /* Otherwise read the next input byte. */ \
181 /* Recognize escape sequences. */ \
182 if (__glibc_unlikely (ch == ESC)) \
184 /* We now must be prepared to read two to three more bytes. \
185 If we have a match in the first byte but then the input buffer \
186 ends we terminate with an error since we must not risk missing \
187 an escape sequence just because it is not entirely in the \
188 current input buffer. */ \
189 if (__builtin_expect (inptr + 2 >= inend, 0) \
190 || (inptr[1] == '$' && inptr[2] == '(' \
191 && __builtin_expect (inptr + 3 >= inend, 0))) \
193 /* Not enough input available. */ \
194 result = __GCONV_INCOMPLETE_INPUT; \
198 if (inptr[1] == '(') \
200 if (inptr[2] == 'B') \
202 /* ASCII selected. */ \
207 else if (inptr[2] == 'J') \
209 /* JIS X 0201 selected. */ \
210 set = JISX0201_Roman_set; \
214 else if (inptr[2] == 'I') \
216 /* JIS X 0201 selected. */ \
217 set = JISX0201_Kana_set; \
222 else if (inptr[1] == '$') \
224 if (inptr[2] == '@') \
226 /* JIS X 0208-1978 selected. */ \
227 set = JISX0208_1978_set; \
231 else if (inptr[2] == 'B') \
233 /* JIS X 0208-1983 selected. */ \
234 set = JISX0208_1983_set; \
238 else if (inptr[2] == '(') \
240 if (inptr[3] == 'O' || inptr[3] == 'Q') \
242 /* JIS X 0213 plane 1 selected. */ \
243 /* In this direction we don't need to distinguish the \
244 versions from 2000 and 2004. */ \
245 set = JISX0213_1_2004_set; \
249 else if (inptr[3] == 'P') \
251 /* JIS X 0213 plane 2 selected. */ \
252 set = JISX0213_2_set; \
262 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
264 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
265 /* Almost done, just advance the input pointer. */ \
267 else if (set == JISX0201_Roman_set) \
269 /* Use the JIS X 0201 table. */ \
270 ch = jisx0201_to_ucs4 (ch); \
271 if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \
273 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
277 else if (set == JISX0201_Kana_set) \
279 /* Use the JIS X 0201 table. */ \
280 ch = jisx0201_to_ucs4 (ch + 0x80); \
281 if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \
283 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
287 else if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
289 /* XXX I don't have the tables for these two old variants of \
290 JIS X 0208. Therefore I'm using the tables for JIS X \
291 0208-1990. If somebody has problems with this please \
292 provide the appropriate tables. */ \
293 ch = jisx0208_to_ucs4 (&inptr, inend - inptr, 0); \
295 if (__glibc_unlikely (ch == 0)) \
297 result = __GCONV_INCOMPLETE_INPUT; \
300 else if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \
302 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
305 else /* (set == JISX0213_1_2004_set || set == JISX0213_2_set) */ \
307 if (__glibc_unlikely (inptr + 1 >= inend)) \
309 result = __GCONV_INCOMPLETE_INPUT; \
313 ch = jisx0213_to_ucs4 ( \
314 ((JISX0213_1_2004_set - set + (1 << 3)) << 5) + ch, \
317 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
321 /* It's a combining character. */ \
322 uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0]; \
323 uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1]; \
327 put32 (outptr, u1); \
330 /* See whether we have room for two characters. */ \
331 if (outptr + 4 <= outend) \
333 put32 (outptr, u2); \
338 /* Otherwise store only the first character now, and \
339 put the second one into the queue. */ \
341 /* Tell the caller why we terminate the loop. */ \
342 result = __GCONV_FULL_OUTPUT; \
349 put32 (outptr, ch); \
352 #define LOOP_NEED_FLAGS
353 #define EXTRA_LOOP_DECLS , int *statep
354 #define INIT_PARAMS int set = *statep
355 #define UPDATE_PARAMS *statep = set
356 #include <iconv/loop.c>
359 /* Next, define the other direction, from UCS-4 to ISO-2022-JP-3. */
361 /* Composition tables for each of the relevant combining characters. */
366 } comp_table_data
[] =
368 #define COMP_TABLE_IDX_02E5 0
369 #define COMP_TABLE_LEN_02E5 1
370 { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */
371 #define COMP_TABLE_IDX_02E9 (COMP_TABLE_IDX_02E5 + COMP_TABLE_LEN_02E5)
372 #define COMP_TABLE_LEN_02E9 1
373 { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */
374 #define COMP_TABLE_IDX_0300 (COMP_TABLE_IDX_02E9 + COMP_TABLE_LEN_02E9)
375 #define COMP_TABLE_LEN_0300 5
376 { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */
377 { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */
378 { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */
379 { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */
380 { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */
381 #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300)
382 #define COMP_TABLE_LEN_0301 4
383 { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */
384 { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */
385 { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */
386 { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */
387 #define COMP_TABLE_IDX_309A (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301)
388 #define COMP_TABLE_LEN_309A 14
389 { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */
390 { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */
391 { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */
392 { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */
393 { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */
394 { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */
395 { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */
396 { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */
397 { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */
398 { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */
399 { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */
400 { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */
401 { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */
402 { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */
405 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
406 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
407 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
408 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
409 #define LOOPFCT TO_LOOP
412 uint32_t ch = get32 (inptr); \
416 /* Attempt to combine the last character with this one. */ \
421 idx = COMP_TABLE_IDX_02E5, len = COMP_TABLE_LEN_02E5; \
422 else if (ch == 0x02e9) \
423 idx = COMP_TABLE_IDX_02E9, len = COMP_TABLE_LEN_02E9; \
424 else if (ch == 0x0300) \
425 idx = COMP_TABLE_IDX_0300, len = COMP_TABLE_LEN_0300; \
426 else if (ch == 0x0301) \
427 idx = COMP_TABLE_IDX_0301, len = COMP_TABLE_LEN_0301; \
428 else if (ch == 0x309a) \
429 idx = COMP_TABLE_IDX_309A, len = COMP_TABLE_LEN_309A; \
431 goto not_combining; \
434 if (comp_table_data[idx].base == (uint16_t) lasttwo) \
436 while (++idx, --len > 0); \
440 /* Output the combined character. */ \
441 /* We know the combined character is in JISX0213 plane 1, \
442 but the buffered character may have been in JISX0208 or in \
443 JISX0213 plane 1. */ \
446 || (set != JISX0213_1_2000_set && set != JISX0213_1_2004_set) \
449 if (__glibc_unlikely (outptr + need + 2 > outend)) \
451 result = __GCONV_FULL_OUTPUT; \
456 /* But first, output the escape sequence. */ \
461 set = JISX0213_1_2000_set; \
463 lasttwo = comp_table_data[idx].composed; \
464 *outptr++ = (lasttwo >> 8) & 0xff; \
465 *outptr++ = lasttwo & 0xff; \
472 /* Output the buffered character. */ \
473 /* We know it is in JISX0208 or in JISX0213 plane 1. */ \
475 size_t need = (lasttwo >> 16 ? 3 : 0); \
477 if (__glibc_unlikely (outptr + need + 2 > outend)) \
479 result = __GCONV_FULL_OUTPUT; \
484 /* But first, output the escape sequence. */ \
485 assert (set == JISX0208_1983_set); \
490 *outptr++ = (lasttwo >> 8) & 0xff; \
491 *outptr++ = lasttwo & 0xff; \
497 /* First see whether we can write the character using the currently \
498 selected character set. */ \
499 if (set == ASCII_set) \
501 /* Please note that the NUL byte is *not* matched if we are not \
502 currently using the ASCII charset. This is because we must \
503 switch to the initial state whenever a NUL byte is written. */ \
511 /* ISO-2022-JP recommends to encode the newline character always in \
512 ASCII since this allows a context-free interpretation of the \
513 characters at the beginning of the next line. Otherwise it would \
514 have to be known whether the last line ended using ASCII or \
516 else if (set == JISX0201_Roman_set) \
518 unsigned char buf[1]; \
519 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
520 && buf[0] > 0x20 && buf[0] < 0x80) \
522 *outptr++ = buf[0]; \
527 else if (set == JISX0201_Kana_set) \
529 unsigned char buf[1]; \
530 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
533 *outptr++ = buf[0] - 0x80; \
538 else if (/*set == JISX0208_1978_set || */ set == JISX0208_1983_set) \
540 size_t written = ucs4_to_jisx0208 (ch, outptr, outend - outptr); \
542 if (written != __UNKNOWN_10646_CHAR) \
544 uint32_t jch = ucs4_to_jisx0213 (ch); \
548 /* A possible match in comp_table_data. Buffer it. */ \
549 lasttwo = jch & 0x7f7f; \
553 if (__glibc_unlikely (written == 0)) \
555 result = __GCONV_FULL_OUTPUT; \
568 /* (set == JISX0213_1_2000_set || set == JISX0213_1_2004_set \
569 || set == JISX0213_2_set) */ \
570 uint32_t jch = ucs4_to_jisx0213 (ch); \
574 ? set == JISX0213_2_set \
575 : (set == JISX0213_1_2004_set \
576 || (set == JISX0213_1_2000_set \
577 && !jisx0213_added_in_2004_p (jch))))) \
581 /* A possible match in comp_table_data. Buffer it. */ \
583 /* We know it's a JISX 0213 plane 1 character. */ \
584 assert ((jch & 0x8000) == 0); \
586 lasttwo = jch & 0x7f7f; \
591 if (__glibc_unlikely (outptr + 1 >= outend)) \
593 result = __GCONV_FULL_OUTPUT; \
596 *outptr++ = (jch >> 8) & 0x7f; \
597 *outptr++ = jch & 0x7f; \
603 /* The attempts to use the currently selected character set failed, \
604 either because the character requires a different character set, \
605 or because the character is unknown. */ \
609 /* We must encode using ASCII. First write out the escape \
611 if (__glibc_unlikely (outptr + 3 > outend)) \
613 result = __GCONV_FULL_OUTPUT; \
622 if (__glibc_unlikely (outptr >= outend)) \
624 result = __GCONV_FULL_OUTPUT; \
631 unsigned char buf[2]; \
633 /* Try JIS X 0201 Roman. */ \
634 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
635 && buf[0] > 0x20 && buf[0] < 0x80) \
637 if (set != JISX0201_Roman_set) \
639 if (__glibc_unlikely (outptr + 3 > outend)) \
641 result = __GCONV_FULL_OUTPUT; \
647 set = JISX0201_Roman_set; \
650 if (__glibc_unlikely (outptr >= outend)) \
652 result = __GCONV_FULL_OUTPUT; \
655 *outptr++ = buf[0]; \
659 uint32_t jch = ucs4_to_jisx0213 (ch); \
661 /* Try JIS X 0208. */ \
662 size_t written = ucs4_to_jisx0208 (ch, buf, 2); \
663 if (written != __UNKNOWN_10646_CHAR) \
667 /* A possible match in comp_table_data. Buffer it. */ \
668 lasttwo = ((set != JISX0208_1983_set ? 1 : 0) << 16) \
670 set = JISX0208_1983_set; \
675 if (set != JISX0208_1983_set) \
677 if (__glibc_unlikely (outptr + 3 > outend)) \
679 result = __GCONV_FULL_OUTPUT; \
685 set = JISX0208_1983_set; \
688 if (__glibc_unlikely (outptr + 2 > outend)) \
690 result = __GCONV_FULL_OUTPUT; \
693 *outptr++ = buf[0]; \
694 *outptr++ = buf[1]; \
698 /* Try JIS X 0213. */ \
704 : jisx0213_added_in_2004_p (jch) \
705 ? JISX0213_1_2004_set \
706 : JISX0213_1_2000_set); \
708 if (set != new_set) \
710 if (__glibc_unlikely (outptr + 4 > outend)) \
712 result = __GCONV_FULL_OUTPUT; \
719 ((new_set - JISX0213_1_2000_set) >> 3) + 'O'; \
725 /* A possible match in comp_table_data. \
728 /* We know it's a JIS X 0213 plane 1 character. */ \
729 assert ((jch & 0x8000) == 0); \
731 lasttwo = jch & 0x7f7f; \
736 if (__glibc_unlikely (outptr + 1 >= outend)) \
738 result = __GCONV_FULL_OUTPUT; \
741 *outptr++ = (jch >> 8) & 0x7f; \
742 *outptr++ = jch & 0x7f; \
746 /* Try JIS X 0201 Katakana. This is officially not part \
747 of ISO-2022-JP-3. Therefore we try it after all other \
749 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
752 if (set != JISX0201_Kana_set) \
754 if (__builtin_expect (outptr + 3 > outend, 0)) \
756 result = __GCONV_FULL_OUTPUT; \
762 set = JISX0201_Kana_set; \
765 if (__glibc_unlikely (outptr >= outend)) \
767 result = __GCONV_FULL_OUTPUT; \
770 *outptr++ = buf[0] - 0x80; \
774 UNICODE_TAG_HANDLER (ch, 4); \
776 /* Illegal character. */ \
777 STANDARD_TO_LOOP_ERR_HANDLER (4); \
784 /* Now that we wrote the output increment the input pointer. */ \
787 #define LOOP_NEED_FLAGS
788 #define EXTRA_LOOP_DECLS , int *statep
789 #define INIT_PARAMS int set = *statep & CURRENT_SEL_MASK; \
790 uint32_t lasttwo = *statep >> 6
791 #define REINIT_PARAMS do \
793 set = *statep & CURRENT_SEL_MASK; \
794 lasttwo = *statep >> 6; \
797 #define UPDATE_PARAMS *statep = set | (lasttwo << 6)
798 #include <iconv/loop.c>
801 /* Now define the toplevel functions. */
802 #include <iconv/skeleton.c>