Prepare for glibc 2.34 release.
[glibc.git] / iconvdata / iso-2022-jp-3.c
blobc8ba88cdc9fe9200c1f0cc91337371892dcff5ac
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/>. */
21 #include <assert.h>
22 #include <dlfcn.h>
23 #include <gconv.h>
24 #include <stdint.h>
25 #include <string.h>
27 #include "jis0201.h"
28 #include "jis0208.h"
29 #include "jisx0213.h"
31 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
32 #define ESC 0x1b
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
38 #define DEFINE_INIT 1
39 #define DEFINE_FINI 1
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 \
50 int saved_state; \
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: */
57 enum
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
78 full. */
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
82 flushing. */
83 #define EMIT_SHIFT_TO_INIT \
84 if (data->__statep->__count != ASCII_set) \
85 { \
86 if (FROM_DIRECTION) \
87 { \
88 if (__glibc_likely (outbuf + 4 <= outend)) \
89 { \
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; \
94 } \
95 else \
96 /* We don't have enough room in the output buffer. */ \
97 status = __GCONV_FULL_OUTPUT; \
98 } \
99 else \
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 \
103 `Esc ( B'. */ \
104 size_t need = \
105 (data->__statep->__count >> 6 \
106 ? (data->__statep->__count >> 22 ? 3 : 0) + 2 \
107 : 0) \
108 + ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set \
109 ? 3 : 0); \
111 if (__glibc_unlikely (outbuf + need > outend)) \
112 /* We don't have enough room in the output buffer. */ \
113 status = __GCONV_FULL_OUTPUT; \
114 else \
116 if (data->__statep->__count >> 6) \
118 uint32_t lasttwo = data->__statep->__count >> 6; \
120 if (lasttwo >> 16) \
122 /* Write out the shift sequence before the last \
123 character. */ \
124 assert ((data->__statep->__count & CURRENT_SEL_MASK) \
125 == JISX0208_1983_set); \
126 *outbuf++ = ESC; \
127 *outbuf++ = '$'; \
128 *outbuf++ = 'B'; \
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. */ \
137 *outbuf++ = ESC; \
138 *outbuf++ = '('; \
139 *outbuf++ = 'B'; \
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) \
151 if (Save) \
152 saved_state = *statep; \
153 else \
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
163 #define BODY \
165 uint32_t ch; \
167 /* Output any pending character. */ \
168 ch = set >> 6; \
169 if (__glibc_unlikely (ch != 0)) \
171 put32 (outptr, ch); \
172 outptr += 4; \
173 /* Remove the pending character, but preserve state bits. */ \
174 set &= (1 << 6) - 1; \
175 continue; \
178 /* Otherwise read the next input byte. */ \
179 ch = *inptr; \
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; \
195 break; \
198 if (inptr[1] == '(') \
200 if (inptr[2] == 'B') \
202 /* ASCII selected. */ \
203 set = ASCII_set; \
204 inptr += 3; \
205 continue; \
207 else if (inptr[2] == 'J') \
209 /* JIS X 0201 selected. */ \
210 set = JISX0201_Roman_set; \
211 inptr += 3; \
212 continue; \
214 else if (inptr[2] == 'I') \
216 /* JIS X 0201 selected. */ \
217 set = JISX0201_Kana_set; \
218 inptr += 3; \
219 continue; \
222 else if (inptr[1] == '$') \
224 if (inptr[2] == '@') \
226 /* JIS X 0208-1978 selected. */ \
227 set = JISX0208_1978_set; \
228 inptr += 3; \
229 continue; \
231 else if (inptr[2] == 'B') \
233 /* JIS X 0208-1983 selected. */ \
234 set = JISX0208_1983_set; \
235 inptr += 3; \
236 continue; \
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; \
246 inptr += 4; \
247 continue; \
249 else if (inptr[3] == 'P') \
251 /* JIS X 0213 plane 2 selected. */ \
252 set = JISX0213_2_set; \
253 inptr += 4; \
254 continue; \
260 if (ch >= 0x80) \
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. */ \
266 ++inptr; \
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); \
275 ++inptr; \
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); \
285 ++inptr; \
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; \
298 break; \
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; \
310 break; \
313 ch = jisx0213_to_ucs4 ( \
314 ((JISX0213_1_2004_set - set + (1 << 3)) << 5) + ch, \
315 inptr[1]); \
316 if (ch == 0) \
317 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
319 if (ch < 0x80) \
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]; \
325 inptr += 2; \
327 put32 (outptr, u1); \
328 outptr += 4; \
330 /* See whether we have room for two characters. */ \
331 if (outptr + 4 <= outend) \
333 put32 (outptr, u2); \
334 outptr += 4; \
335 continue; \
338 /* Otherwise store only the first character now, and \
339 put the second one into the queue. */ \
340 set |= u2 << 6; \
341 /* Tell the caller why we terminate the loop. */ \
342 result = __GCONV_FULL_OUTPUT; \
343 break; \
346 inptr += 2; \
349 put32 (outptr, ch); \
350 outptr += 4; \
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. */
362 static const struct
364 uint16_t base;
365 uint16_t composed;
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
410 #define BODY \
412 uint32_t ch = get32 (inptr); \
414 if (lasttwo != 0) \
416 /* Attempt to combine the last character with this one. */ \
417 unsigned int idx; \
418 unsigned int len; \
420 if (ch == 0x02e5) \
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; \
430 else \
431 goto not_combining; \
433 do \
434 if (comp_table_data[idx].base == (uint16_t) lasttwo) \
435 break; \
436 while (++idx, --len > 0); \
438 if (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. */ \
444 size_t need = \
445 (lasttwo >> 16 \
446 || (set != JISX0213_1_2000_set && set != JISX0213_1_2004_set) \
447 ? 4 : 0); \
449 if (__glibc_unlikely (outptr + need + 2 > outend)) \
451 result = __GCONV_FULL_OUTPUT; \
452 break; \
454 if (need) \
456 /* But first, output the escape sequence. */ \
457 *outptr++ = ESC; \
458 *outptr++ = '$'; \
459 *outptr++ = '('; \
460 *outptr++ = 'O'; \
461 set = JISX0213_1_2000_set; \
463 lasttwo = comp_table_data[idx].composed; \
464 *outptr++ = (lasttwo >> 8) & 0xff; \
465 *outptr++ = lasttwo & 0xff; \
466 lasttwo = 0; \
467 inptr += 4; \
468 continue; \
471 not_combining: \
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; \
480 break; \
482 if (need) \
484 /* But first, output the escape sequence. */ \
485 assert (set == JISX0208_1983_set); \
486 *outptr++ = ESC; \
487 *outptr++ = '$'; \
488 *outptr++ = 'B'; \
490 *outptr++ = (lasttwo >> 8) & 0xff; \
491 *outptr++ = lasttwo & 0xff; \
492 lasttwo = 0; \
493 continue; \
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. */ \
504 if (ch <= 0x7f) \
506 *outptr++ = ch; \
507 inptr += 4; \
508 continue; \
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 \
515 JIS X 0201. */ \
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]; \
523 inptr += 4; \
524 continue; \
527 else if (set == JISX0201_Kana_set) \
529 unsigned char buf[1]; \
530 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
531 && buf[0] >= 0x80) \
533 *outptr++ = buf[0] - 0x80; \
534 inptr += 4; \
535 continue; \
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); \
546 if (jch & 0x0080) \
548 /* A possible match in comp_table_data. Buffer it. */ \
549 lasttwo = jch & 0x7f7f; \
550 inptr += 4; \
551 continue; \
553 if (__glibc_unlikely (written == 0)) \
555 result = __GCONV_FULL_OUTPUT; \
556 break; \
558 else \
560 outptr += written; \
561 inptr += 4; \
562 continue; \
566 else \
568 /* (set == JISX0213_1_2000_set || set == JISX0213_1_2004_set \
569 || set == JISX0213_2_set) */ \
570 uint32_t jch = ucs4_to_jisx0213 (ch); \
572 if (jch != 0 \
573 && (jch & 0x8000 \
574 ? set == JISX0213_2_set \
575 : (set == JISX0213_1_2004_set \
576 || (set == JISX0213_1_2000_set \
577 && !jisx0213_added_in_2004_p (jch))))) \
579 if (jch & 0x0080) \
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; \
587 inptr += 4; \
588 continue; \
591 if (__glibc_unlikely (outptr + 1 >= outend)) \
593 result = __GCONV_FULL_OUTPUT; \
594 break; \
596 *outptr++ = (jch >> 8) & 0x7f; \
597 *outptr++ = jch & 0x7f; \
598 inptr += 4; \
599 continue; \
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. */ \
607 if (ch <= 0x7f) \
609 /* We must encode using ASCII. First write out the escape \
610 sequence. */ \
611 if (__glibc_unlikely (outptr + 3 > outend)) \
613 result = __GCONV_FULL_OUTPUT; \
614 break; \
617 *outptr++ = ESC; \
618 *outptr++ = '('; \
619 *outptr++ = 'B'; \
620 set = ASCII_set; \
622 if (__glibc_unlikely (outptr >= outend)) \
624 result = __GCONV_FULL_OUTPUT; \
625 break; \
627 *outptr++ = ch; \
629 else \
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; \
642 break; \
644 *outptr++ = ESC; \
645 *outptr++ = '('; \
646 *outptr++ = 'J'; \
647 set = JISX0201_Roman_set; \
650 if (__glibc_unlikely (outptr >= outend)) \
652 result = __GCONV_FULL_OUTPUT; \
653 break; \
655 *outptr++ = buf[0]; \
657 else \
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) \
665 if (jch & 0x0080) \
667 /* A possible match in comp_table_data. Buffer it. */ \
668 lasttwo = ((set != JISX0208_1983_set ? 1 : 0) << 16) \
669 | (jch & 0x7f7f); \
670 set = JISX0208_1983_set; \
671 inptr += 4; \
672 continue; \
675 if (set != JISX0208_1983_set) \
677 if (__glibc_unlikely (outptr + 3 > outend)) \
679 result = __GCONV_FULL_OUTPUT; \
680 break; \
682 *outptr++ = ESC; \
683 *outptr++ = '$'; \
684 *outptr++ = 'B'; \
685 set = JISX0208_1983_set; \
688 if (__glibc_unlikely (outptr + 2 > outend)) \
690 result = __GCONV_FULL_OUTPUT; \
691 break; \
693 *outptr++ = buf[0]; \
694 *outptr++ = buf[1]; \
696 else \
698 /* Try JIS X 0213. */ \
699 if (jch != 0) \
701 int new_set = \
702 (jch & 0x8000 \
703 ? JISX0213_2_set \
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; \
713 break; \
715 *outptr++ = ESC; \
716 *outptr++ = '$'; \
717 *outptr++ = '('; \
718 *outptr++ = \
719 ((new_set - JISX0213_1_2000_set) >> 3) + 'O'; \
720 set = new_set; \
723 if (jch & 0x0080) \
725 /* A possible match in comp_table_data. \
726 Buffer it. */ \
728 /* We know it's a JIS X 0213 plane 1 character. */ \
729 assert ((jch & 0x8000) == 0); \
731 lasttwo = jch & 0x7f7f; \
732 inptr += 4; \
733 continue; \
736 if (__glibc_unlikely (outptr + 1 >= outend)) \
738 result = __GCONV_FULL_OUTPUT; \
739 break; \
741 *outptr++ = (jch >> 8) & 0x7f; \
742 *outptr++ = jch & 0x7f; \
744 else \
746 /* Try JIS X 0201 Katakana. This is officially not part \
747 of ISO-2022-JP-3. Therefore we try it after all other \
748 attempts. */ \
749 if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \
750 && buf[0] >= 0x80) \
752 if (set != JISX0201_Kana_set) \
754 if (__builtin_expect (outptr + 3 > outend, 0)) \
756 result = __GCONV_FULL_OUTPUT; \
757 break; \
759 *outptr++ = ESC; \
760 *outptr++ = '('; \
761 *outptr++ = 'I'; \
762 set = JISX0201_Kana_set; \
765 if (__glibc_unlikely (outptr >= outend)) \
767 result = __GCONV_FULL_OUTPUT; \
768 break; \
770 *outptr++ = buf[0] - 0x80; \
772 else \
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. */ \
785 inptr += 4; \
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; \
796 while (0)
797 #define UPDATE_PARAMS *statep = set | (lasttwo << 6)
798 #include <iconv/loop.c>
801 /* Now define the toplevel functions. */
802 #include <iconv/skeleton.c>