1 /* Conversion module for ISO-2022-CN-EXT.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
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 <http://www.gnu.org/licenses/>. */
26 #include "iso-ir-165.h"
28 #include "cns11643l1.h"
29 #include "cns11643l2.h"
30 #include <libc-diag.h>
34 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
37 /* We have single-byte shift-in and shift-out sequences, and the single
38 shift sequences SS2 and SS3 which replaces the SS2/SS3 designation for
39 the next two bytes. */
47 /* Definitions used in the body of the `gconv' function. */
48 #define CHARSET_NAME "ISO-2022-CN-EXT//"
51 #define ONE_DIRECTION 0
52 #define FROM_LOOP from_iso2022cn_ext_loop
53 #define TO_LOOP to_iso2022cn_ext_loop
54 #define FROM_LOOP_MIN_NEEDED_FROM 1
55 #define FROM_LOOP_MAX_NEEDED_FROM 4
56 #define FROM_LOOP_MIN_NEEDED_TO 4
57 #define FROM_LOOP_MAX_NEEDED_TO 4
58 #define TO_LOOP_MIN_NEEDED_FROM 4
59 #define TO_LOOP_MAX_NEEDED_FROM 4
60 #define TO_LOOP_MIN_NEEDED_TO 1
61 #define TO_LOOP_MAX_NEEDED_TO 6
62 #define PREPARE_LOOP \
64 int *setp = &data->__statep->__count;
65 #define EXTRA_LOOP_ARGS , setp
68 /* The charsets GB/T 12345-90, GB 7589-87, GB/T 13131-9X, GB 7590-87,
69 and GB/T 13132-9X are not registered to the best of my knowledge and
70 therefore have no escape sequence assigned. We cannot handle them
71 for this reason. Tell the implementation about this. */
79 /* The COUNT element of the state keeps track of the currently selected
80 character set. The possible values are: */
92 CNS11643_2_set
= 3 << 3,
97 CNS11643_3_set
= 3 << 5,
98 CNS11643_4_set
= 4 << 5,
99 CNS11643_5_set
= 5 << 5,
100 CNS11643_6_set
= 6 << 5,
101 CNS11643_7_set
= 7 << 5,
104 #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask)
107 GB12345_ann
= 2 << 8,
108 CNS11643_1_ann
= 3 << 8,
109 ISO_IR_165_ann
= 4 << 8,
112 GB7589_ann
= 1 << 11,
113 GB13131_ann
= 2 << 11,
114 CNS11643_2_ann
= 3 << 11,
117 GB7590_ann
= 1 << 13,
118 GB13132_ann
= 2 << 13,
119 CNS11643_3_ann
= 3 << 13,
120 CNS11643_4_ann
= 4 << 13,
121 CNS11643_5_ann
= 5 << 13,
122 CNS11643_6_ann
= 6 << 13,
123 CNS11643_7_ann
= 7 << 13,
128 /* Since this is a stateful encoding we have to provide code which resets
129 the output state to the initial state. This has to be done during the
131 #define EMIT_SHIFT_TO_INIT \
132 if (data->__statep->__count >> 3 != ASCII_set) \
134 if (FROM_DIRECTION) \
135 /* It's easy, we don't have to emit anything, we just reset the \
136 state for the input. */ \
137 data->__statep->__count = ASCII_set << 3; \
140 /* We are not in the initial state. To switch back we have \
142 if (__glibc_unlikely (outbuf == outend)) \
143 /* We don't have enough room in the output buffer. */ \
144 status = __GCONV_FULL_OUTPUT; \
147 /* Write out the shift sequence. */ \
149 if (data->__flags & __GCONV_IS_LAST) \
150 *irreversible += 1; \
151 data->__statep->__count = ASCII_set << 3; \
157 /* Since we might have to reset input pointer we must be able to save
158 and retore the state. */
159 #define SAVE_RESET_STATE(Save) \
166 /* First define the conversion function from ISO-2022-CN to UCS4. */
167 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
168 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
169 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
170 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
171 #define LOOPFCT FROM_LOOP
174 uint32_t ch = *inptr; \
176 /* This is a 7bit character set, disallow all 8bit characters. */ \
178 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
180 /* Recognize escape sequences. */ \
183 /* There are three kinds of escape sequences we have to handle: \
184 - those announcing the use of GB and CNS characters on the \
185 line; we can simply ignore them \
186 - the initial byte of the SS2 sequence. \
187 - the initial byte of the SS3 sequence. \
189 if (inptr + 2 > inend \
190 || (inptr[1] == '$' \
191 && (inptr + 3 > inend \
192 || (inptr[2] == ')' && inptr + 4 > inend) \
193 || (inptr[2] == '*' && inptr + 4 > inend) \
194 || (inptr[2] == '+' && inptr + 4 > inend))) \
195 || (inptr[1] == SS2_1 && inptr + 4 > inend) \
196 || (inptr[1] == SS3_1 && inptr + 4 > inend)) \
198 result = __GCONV_INCOMPLETE_INPUT; \
201 if (inptr[1] == '$' \
202 && ((inptr[2] == ')' \
203 && (inptr[3] == 'A' \
204 || (X12345 != '\0' && inptr[3] == X12345) \
205 || inptr[3] == 'E' || inptr[3] == 'G')) \
206 || (inptr[2] == '*' \
207 && ((X7589 != '\0' && inptr[3] == X7589) \
208 || (X13131 != '\0' && inptr[3] == X13131) \
209 || inptr[3] == 'H')) \
210 || (inptr[2] == '+' \
211 && ((X7590 != '\0' && inptr[3] == X7590) \
212 || (X13132 != '\0' && inptr[3] == X13132) \
213 || inptr[3] == 'I' || inptr[3] == 'J' \
214 || inptr[3] == 'K' || inptr[3] == 'L' \
215 || inptr[3] == 'M')))) \
217 /* OK, we accept those character sets. */ \
218 if (inptr[3] == 'A') \
219 ann = (ann & ~SO_ann) | GB2312_ann; \
220 else if (inptr[3] == 'G') \
221 ann = (ann & ~SO_ann) | CNS11643_1_ann; \
222 else if (inptr[3] == 'E') \
223 ann = (ann & ~SO_ann) | ISO_IR_165_ann; \
224 else if (X12345 != '\0' && inptr[3] == X12345) \
225 ann = (ann & ~SO_ann) | GB12345_ann; \
226 else if (inptr[3] == 'H') \
227 ann = (ann & ~SS2_ann) | CNS11643_2_ann; \
228 else if (X7589 != '\0' && inptr[3] == X7589) \
229 ann = (ann & ~SS2_ann) | GB7589_ann; \
230 else if (X13131 != '\0' && inptr[3] == X13131) \
231 ann = (ann & ~SS2_ann) | GB13131_ann; \
232 else if (inptr[3] == 'I') \
233 ann = (ann & ~SS3_ann) | CNS11643_3_ann; \
234 else if (inptr[3] == 'J') \
235 ann = (ann & ~SS3_ann) | CNS11643_4_ann; \
236 else if (inptr[3] == 'K') \
237 ann = (ann & ~SS3_ann) | CNS11643_5_ann; \
238 else if (inptr[3] == 'L') \
239 ann = (ann & ~SS3_ann) | CNS11643_6_ann; \
240 else if (inptr[3] == 'M') \
241 ann = (ann & ~SS3_ann) | CNS11643_7_ann; \
242 else if (X7590 != '\0' && inptr[3] == X7590) \
243 ann = (ann & ~SS3_ann) | GB7590_ann; \
244 else if (X13132 != '\0' && inptr[3] == X13132) \
245 ann = (ann & ~SS3_ann) | GB13132_ann; \
252 /* Switch to use GB2312, GB12345, CNS 11643 plane 1, or ISO-IR-165, \
253 depending on which S0 designation came last. The only problem \
254 is what to do with faulty input files where no designator came. \
255 XXX For now I'll default to use GB2312. If this is not the \
256 best behavior (e.g., we should flag an error) let me know. */ \
258 if ((ann & SO_ann) != 0) \
259 switch (ann & SO_ann) \
267 case CNS11643_1_ann: \
268 set = CNS11643_1_set; \
270 case ISO_IR_165_ann: \
271 set = ISO_IR_165_set; \
278 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
284 /* Switch to use ASCII. */ \
290 if (ch == ESC && inptr[1] == SS2_1) \
292 /* This is a character from CNS 11643 plane 2. \
293 XXX We could test here whether the use of this character \
295 XXX Currently GB7589 and GB13131 are not supported. */ \
297 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
298 if (ch == __UNKNOWN_10646_CHAR) \
299 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
301 /* Note that we can assume here that at least 4 bytes are available if \
302 the first byte is ESC since otherwise the first if would have been \
304 else if (ch == ESC && inptr[1] == SS3_1) \
306 /* This is a character from CNS 11643 plane 3 or higher. \
307 XXX Currently GB7590 and GB13132 are not supported. */ \
308 unsigned char buf[3]; \
309 const unsigned char *tmp = buf; \
313 switch (ann & SS3_ann) \
315 case CNS11643_3_ann: \
317 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
319 case CNS11643_4_ann: \
321 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
323 case CNS11643_5_ann: \
325 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
327 case CNS11643_6_ann: \
329 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
331 case CNS11643_7_ann: \
333 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
336 /* XXX Currently GB7590 and GB13132 are not supported. */ \
337 ch = __UNKNOWN_10646_CHAR; \
340 if (ch == __UNKNOWN_10646_CHAR) \
342 STANDARD_FROM_LOOP_ERR_HANDLER (4); \
344 assert (tmp == buf + 3); \
347 else if (set == ASCII_set) \
349 /* Almost done, just advance the input pointer. */ \
354 /* That's pretty easy, we have a dedicated functions for this. */ \
355 if (inend - inptr < 2) \
357 result = __GCONV_INCOMPLETE_INPUT; \
360 if (set == GB2312_set) \
361 ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0); \
362 else if (set == ISO_IR_165_set) \
363 ch = isoir165_to_ucs4 (&inptr, inend - inptr); \
366 assert (set == CNS11643_1_set); \
367 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
372 result = __GCONV_INCOMPLETE_INPUT; \
375 else if (ch == __UNKNOWN_10646_CHAR) \
377 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
381 *((uint32_t *) outptr) = ch; \
382 outptr += sizeof (uint32_t); \
384 #define EXTRA_LOOP_DECLS , int *setp
385 #define INIT_PARAMS int set = (*setp >> 3) & CURRENT_MASK; \
386 int ann = (*setp >> 3) & ~CURRENT_MASK
387 #define UPDATE_PARAMS *setp = (set | ann) << 3
388 #define LOOP_NEED_FLAGS
389 #include <iconv/loop.c>
392 /* Next, define the other direction. */
393 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
394 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
395 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
396 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
397 #define LOOPFCT TO_LOOP
398 /* With GCC 5.3 when compiling with -Os the compiler emits a warning
399 that buf[0] and buf[1] may be used uninitialized. This can only
400 happen in the case where tmpbuf[3] is used, and in that case the
401 write to the tmpbuf[1] and tmpbuf[2] was assured because
402 ucs4_to_cns11643 would have filled in those entries. The difficulty
403 is in getting the compiler to see this logic because tmpbuf[0] is
404 involved in determining the code page and is the indicator that
405 tmpbuf[2] is initialized. */
406 DIAG_PUSH_NEEDS_COMMENT
;
407 DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
411 size_t written = 0; \
413 ch = *((const uint32_t *) inptr); \
415 /* First see whether we can write the character using the currently \
416 selected character set. */ \
419 if (set != ASCII_set) \
423 if (outptr == outend) \
425 result = __GCONV_FULL_OUTPUT; \
433 /* At the end of the line we have to clear the `ann' flags since \
434 every line must contain this information again. */ \
440 unsigned char buf[2] = { 0, 0 }; \
443 if (set == GB2312_set || ((ann & SO_ann) != CNS11643_1_ann \
444 && (ann & SO_ann) != ISO_IR_165_ann)) \
446 written = ucs4_to_gb2312 (ch, buf, 2); \
449 else if (set == ISO_IR_165_set || (ann & SO_ann) == ISO_IR_165_set) \
451 written = ucs4_to_isoir165 (ch, buf, 2); \
452 used = ISO_IR_165_set; \
456 written = ucs4_to_cns11643l1 (ch, buf, 2); \
457 used = CNS11643_1_set; \
460 if (written == __UNKNOWN_10646_CHAR) \
462 /* Cannot convert it using the currently selected SO set. \
463 Next try the SS2 set. */ \
464 written = ucs4_to_cns11643l2 (ch, buf, 2); \
465 if (written != __UNKNOWN_10646_CHAR) \
466 /* Yep, that worked. */ \
467 used = CNS11643_2_set; \
470 unsigned char tmpbuf[3]; \
475 /* Well, see whether we have to change the SO set. */ \
477 if (used != GB2312_set) \
479 written = ucs4_to_gb2312 (ch, buf, 2); \
480 if (written != __UNKNOWN_10646_CHAR) \
487 if (used != ISO_IR_165_set) \
489 written = ucs4_to_isoir165 (ch, buf, 2); \
490 if (written != __UNKNOWN_10646_CHAR) \
492 used = ISO_IR_165_set; \
497 if (used != CNS11643_1_set) \
499 written = ucs4_to_cns11643l1 (ch, buf, 2); \
500 if (written != __UNKNOWN_10646_CHAR) \
502 used = CNS11643_1_set; \
507 written = ucs4_to_cns11643 (ch, tmpbuf, 3); \
508 if (written == 3 && tmpbuf[0] >= 3 && tmpbuf[0] <= 7) \
510 buf[0] = tmpbuf[1]; \
511 buf[1] = tmpbuf[2]; \
515 used = CNS11643_3_set; \
518 used = CNS11643_4_set; \
521 used = CNS11643_5_set; \
524 used = CNS11643_6_set; \
527 used = CNS11643_7_set; \
536 /* XXX Currently GB7590 and GB13132 are not supported. */\
538 /* Even this does not work. Error. */ \
541 if (used == ASCII_set) \
543 UNICODE_TAG_HANDLER (ch, 4); \
544 STANDARD_TO_LOOP_ERR_HANDLER (4); \
548 assert (written == 2); \
550 /* See whether we have to emit an escape sequence. */ \
553 /* First see whether we announced that we use this \
555 if ((used & SO_mask) != 0 && (ann & SO_ann) != (used << 8)) \
557 const char *escseq; \
559 if (outptr + 4 > outend) \
561 result = __GCONV_FULL_OUTPUT; \
565 assert (used >= 1 && used <= 4); \
566 escseq = ")A\0\0)G)E" + (used - 1) * 2; \
569 *outptr++ = *escseq++; \
570 *outptr++ = *escseq++; \
572 ann = (ann & ~SO_ann) | (used << 8); \
574 else if ((used & SS2_mask) != 0 && (ann & SS2_ann) != (used << 8))\
576 const char *escseq; \
578 assert (used == CNS11643_2_set); /* XXX */ \
582 *outptr++ = *escseq++; \
583 *outptr++ = *escseq++; \
585 ann = (ann & ~SS2_ann) | (used << 8); \
587 else if ((used & SS3_mask) != 0 && (ann & SS3_ann) != (used << 8))\
589 const char *escseq; \
591 assert ((used >> 5) >= 3 && (used >> 5) <= 7); \
592 escseq = "+I+J+K+L+M" + ((used >> 5) - 3) * 2; \
595 *outptr++ = *escseq++; \
596 *outptr++ = *escseq++; \
598 ann = (ann & ~SS3_ann) | (used << 8); \
601 if (used == CNS11643_2_set) \
603 if (outptr + 2 > outend) \
605 result = __GCONV_FULL_OUTPUT; \
611 else if (used >= CNS11643_3_set && used <= CNS11643_7_set) \
613 if (outptr + 2 > outend) \
615 result = __GCONV_FULL_OUTPUT; \
623 /* We only have to emit something if currently ASCII is \
624 selected. Otherwise we are switching within the \
626 if (set == ASCII_set) \
628 if (outptr + 1 > outend) \
630 result = __GCONV_FULL_OUTPUT; \
637 /* Always test the length here since we have used up all the \
638 guaranteed output buffer slots. */ \
639 if (outptr + 2 > outend) \
641 result = __GCONV_FULL_OUTPUT; \
645 else if (outptr + 2 > outend) \
647 result = __GCONV_FULL_OUTPUT; \
651 *outptr++ = buf[0]; \
652 *outptr++ = buf[1]; \
656 /* Now that we wrote the output increment the input pointer. */ \
659 DIAG_POP_NEEDS_COMMENT
;
660 #define EXTRA_LOOP_DECLS , int *setp
661 #define INIT_PARAMS int set = (*setp >> 3) & CURRENT_MASK; \
662 int ann = (*setp >> 3) & ~CURRENT_MASK
663 #define REINIT_PARAMS do \
665 set = (*setp >> 3) & CURRENT_MASK; \
666 ann = (*setp >> 3) & ~CURRENT_MASK; \
669 #define UPDATE_PARAMS *setp = (set | ann) << 3
670 #define LOOP_NEED_FLAGS
671 #include <iconv/loop.c>
674 /* Now define the toplevel functions. */
675 #include <iconv/skeleton.c>