1 /* Conversion module for ISO-2022-CN-EXT.
2 Copyright (C) 2000, 2001 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include "iso-ir-165.h"
29 #include "cns11643l1.h"
30 #include "cns11643l2.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 FROM_LOOP from_iso2022cn_ext_loop
52 #define TO_LOOP to_iso2022cn_ext_loop
53 #define MIN_NEEDED_FROM 1
54 #define MAX_NEEDED_FROM 4
55 #define MIN_NEEDED_TO 4
56 #define MAX_NEEDED_TO 4
57 #define PREPARE_LOOP \
59 int *setp = &data->__statep->__count;
60 #define EXTRA_LOOP_ARGS , setp
63 /* The charsets GB/T 12345-90, GB 7589-87, GB/T 13131-9X, GB 7590-87,
64 and GB/T 13132-9X are not registered to the best of my knowledge and
65 therefore have no escape sequence assigned. We cannot handle them
66 for this reason. Tell the implementation about this. */
74 /* The COUNT element of the state keeps track of the currently selected
75 character set. The possible values are: */
87 CNS11643_2_set
= 3 << 3,
92 CNS11643_3_set
= 3 << 5,
93 CNS11643_4_set
= 4 << 5,
94 CNS11643_5_set
= 5 << 5,
95 CNS11643_6_set
= 6 << 5,
96 CNS11643_7_set
= 7 << 5,
99 #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask)
102 GB12345_ann
= 2 << 8,
103 CNS11643_1_ann
= 3 << 8,
104 ISO_IR_165_ann
= 4 << 8,
107 GB7589_ann
= 1 << 11,
108 GB13131_ann
= 2 << 11,
109 CNS11643_2_ann
= 3 << 11,
112 GB7590_ann
= 1 << 13,
113 GB13132_ann
= 2 << 13,
114 CNS11643_3_ann
= 3 << 13,
115 CNS11643_4_ann
= 4 << 13,
116 CNS11643_5_ann
= 5 << 13,
117 CNS11643_6_ann
= 6 << 13,
118 CNS11643_7_ann
= 7 << 13,
123 /* Since this is a stateful encoding we have to provide code which resets
124 the output state to the initial state. This has to be done during the
126 #define EMIT_SHIFT_TO_INIT \
127 if (data->__statep->__count >> 3 != ASCII_set) \
129 if (FROM_DIRECTION) \
130 /* It's easy, we don't have to emit anything, we just reset the \
131 state for the input. */ \
132 data->__statep->__count = ASCII_set << 3; \
135 /* We are not in the initial state. To switch back we have \
137 if (__builtin_expect (outbuf == outend, 0)) \
138 /* We don't have enough room in the output buffer. */ \
139 status = __GCONV_FULL_OUTPUT; \
142 /* Write out the shift sequence. */ \
144 if (data->__flags & __GCONV_IS_LAST) \
145 *irreversible += 1; \
146 data->__statep->__count = ASCII_set << 3; \
152 /* Since we might have to reset input pointer we must be able to save
153 and retore the state. */
154 #define SAVE_RESET_STATE(Save) \
161 /* First define the conversion function from ISO-2022-CN to UCS4. */
162 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
163 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
164 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
165 #define LOOPFCT FROM_LOOP
168 uint32_t ch = *inptr; \
170 /* This is a 7bit character set, disallow all 8bit characters. */ \
173 if (! ignore_errors_p ()) \
175 result = __GCONV_ILLEGAL_INPUT; \
183 /* Recognize escape sequences. */ \
186 /* There are three kinds of escape sequences we have to handle: \
187 - those announcing the use of GB and CNS characters on the \
188 line; we can simply ignore them \
189 - the initial byte of the SS2 sequence. \
190 - the initial byte of the SS3 sequence. \
192 if (inptr + 2 > inend \
193 || (inptr[1] == '$' \
194 && (inptr + 3 > inend \
195 || (inptr[2] == ')' && inptr + 4 > inend) \
196 || (inptr[2] == '*' && inptr + 4 > inend) \
197 || (inptr[2] == '+' && inptr + 4 > inend))) \
198 || (inptr[1] == SS2_1 && inptr + 4 > inend) \
199 || (inptr[1] == SS3_1 && inptr + 4 > inend)) \
201 result = __GCONV_INCOMPLETE_INPUT; \
204 if (inptr[1] == '$' \
205 && ((inptr[2] == ')' \
206 && (inptr[3] == 'A' \
207 || (X12345 != '\0' && inptr[3] == X12345) \
208 || inptr[3] == 'E' || inptr[3] == 'G')) \
209 || (inptr[2] == '*' \
210 && ((X7589 != '\0' && inptr[3] == X7589) \
211 || (X13131 != '\0' && inptr[3] == X13131) \
212 || inptr[3] == 'H')) \
213 || (inptr[2] == '+' \
214 && ((X7590 != '\0' && inptr[3] == X7590) \
215 || (X13132 != '\0' && inptr[3] == X13132) \
216 || inptr[3] == 'I' || inptr[3] == 'J' \
217 || inptr[3] == 'K' || inptr[3] == 'L' \
218 || inptr[3] == 'M')))) \
220 /* OK, we accept those character sets. */ \
221 if (inptr[3] == 'A') \
222 ann = (ann & ~SO_ann) | GB2312_ann; \
223 else if (inptr[3] == 'G') \
224 ann = (ann & ~SO_ann) | CNS11643_1_ann; \
225 else if (inptr[3] == 'E') \
226 ann = (ann & ~SO_ann) | ISO_IR_165_ann; \
227 else if (X12345 != '\0' && inptr[3] == X12345) \
228 ann = (ann & ~SO_ann) | GB12345_ann; \
229 else if (inptr[3] == 'H') \
230 ann = (ann & ~SS2_ann) | CNS11643_2_ann; \
231 else if (X7589 != '\0' && inptr[3] == X7589) \
232 ann = (ann & ~SS2_ann) | GB7589_ann; \
233 else if (X13131 != '\0' && inptr[3] == X13131) \
234 ann = (ann & ~SS2_ann) | GB13131_ann; \
235 else if (inptr[3] == 'I') \
236 ann = (ann & ~SS3_ann) | CNS11643_3_ann; \
237 else if (inptr[3] == 'J') \
238 ann = (ann & ~SS3_ann) | CNS11643_4_ann; \
239 else if (inptr[3] == 'K') \
240 ann = (ann & ~SS3_ann) | CNS11643_5_ann; \
241 else if (inptr[3] == 'L') \
242 ann = (ann & ~SS3_ann) | CNS11643_6_ann; \
243 else if (inptr[3] == 'M') \
244 ann = (ann & ~SS3_ann) | CNS11643_7_ann; \
245 else if (X7590 != '\0' && inptr[3] == X7590) \
246 ann = (ann & ~SS3_ann) | GB7590_ann; \
247 else if (X13132 != '\0' && inptr[3] == X13132) \
248 ann = (ann & ~SS3_ann) | GB13132_ann; \
255 /* Switch to use GB2312, GB12345, CNS 11643 plane 1, or ISO-IR-165, \
256 depending on which S0 designation came last. The only problem \
257 is what to do with faulty input files where no designator came. \
258 XXX For now I'll default to use GB2312. If this is not the \
259 best behavior (e.g., we should flag an error) let me know. */ \
261 if ((ann & SO_ann) != 0) \
262 switch (ann & SO_ann) \
270 case CNS11643_1_ann: \
271 set = CNS11643_1_set; \
273 case ISO_IR_165_ann: \
274 set = ISO_IR_165_set; \
281 if (! ignore_errors_p ()) \
283 result = __GCONV_ILLEGAL_INPUT; \
293 /* Switch to use ASCII. */ \
299 if (ch == ESC && inptr[1] == SS2_1) \
301 /* This is a character from CNS 11643 plane 2. \
302 XXX We could test here whether the use of this character \
304 XXX Currently GB7589 and GB13131 are not supported. */ \
306 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
307 if (ch == __UNKNOWN_10646_CHAR) \
309 if (! ignore_errors_p ()) \
312 result = __GCONV_ILLEGAL_INPUT; \
320 /* Note that we can assume here that at least 4 bytes are available if \
321 the first byte is ESC since otherwise the first if would have been \
323 else if (ch == ESC && inptr[1] == SS3_1) \
325 /* This is a character from CNS 11643 plane 3 or higher. \
326 XXX Currently GB7590 and GB13132 are not supported. */ \
328 const char *tmp = buf; \
332 switch (ann & SS3_ann) \
334 case CNS11643_3_ann: \
336 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
338 case CNS11643_4_ann: \
340 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
342 case CNS11643_5_ann: \
344 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
346 case CNS11643_6_ann: \
348 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
350 case CNS11643_7_ann: \
352 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
355 /* XXX Currently GB7590 and GB13132 are not supported. */ \
356 ch = __UNKNOWN_10646_CHAR; \
359 if (ch == __UNKNOWN_10646_CHAR) \
361 if (! ignore_errors_p ()) \
363 result = __GCONV_ILLEGAL_INPUT; \
370 assert (tmp == buf + 3); \
373 else if (set == ASCII_set) \
375 /* Almost done, just advance the input pointer. */ \
380 /* That's pretty easy, we have a dedicated functions for this. */ \
381 if (inend - inptr < 2) \
383 result = __GCONV_INCOMPLETE_INPUT; \
386 if (set == GB2312_set) \
387 ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0); \
388 else if (set == ISO_IR_165_set) \
389 ch = isoir165_to_ucs4 (&inptr, inend - inptr); \
392 assert (set == CNS11643_1_set); \
393 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
398 result = __GCONV_INCOMPLETE_INPUT; \
401 else if (ch == __UNKNOWN_10646_CHAR) \
403 if (! ignore_errors_p ()) \
405 result = __GCONV_ILLEGAL_INPUT; \
414 *((uint32_t *) outptr)++ = ch; \
416 #define EXTRA_LOOP_DECLS , int *setp
417 #define INIT_PARAMS int set = (*setp >> 3) & CURRENT_MASK; \
418 int ann = (*setp >> 3) & ~CURRENT_MASK
419 #define UPDATE_PARAMS *setp = (set | ann) << 3
420 #define LOOP_NEED_FLAGS
421 #include <iconv/loop.c>
424 /* Next, define the other direction. */
425 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
426 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
427 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
428 #define LOOPFCT TO_LOOP
432 size_t written = 0; \
434 ch = *((const uint32_t *) inptr); \
436 /* First see whether we can write the character using the currently \
437 selected character set. */ \
440 if (set != ASCII_set) \
444 if (outptr == outend) \
446 result = __GCONV_FULL_OUTPUT; \
454 /* At the end of the line we have to clear the `ann' flags since \
455 every line must contain this information again. */ \
464 if (set == GB2312_set || ((ann & SO_ann) != CNS11643_1_ann \
465 && (ann & SO_ann) != ISO_IR_165_ann)) \
467 written = ucs4_to_gb2312 (ch, buf, 2); \
470 else if (set == ISO_IR_165_set || (ann & SO_ann) == ISO_IR_165_set) \
472 written = ucs4_to_isoir165 (ch, buf, 2); \
473 used = ISO_IR_165_set; \
477 written = ucs4_to_cns11643l1 (ch, buf, 2); \
478 used = CNS11643_1_set; \
481 if (written == __UNKNOWN_10646_CHAR) \
483 /* Cannot convert it using the currently selected SO set. \
484 Next try the SS2 set. */ \
485 written = ucs4_to_cns11643l2 (ch, buf, 2); \
486 if (written != __UNKNOWN_10646_CHAR) \
487 /* Yep, that worked. */ \
488 used = CNS11643_2_set; \
496 /* Well, see whether we have to change the SO set. */ \
498 if (used != GB2312_set) \
500 written = ucs4_to_gb2312 (ch, buf, 2); \
501 if (written != __UNKNOWN_10646_CHAR) \
508 if (used != ISO_IR_165_set) \
510 written = ucs4_to_isoir165 (ch, buf, 2); \
511 if (written != __UNKNOWN_10646_CHAR) \
513 used = ISO_IR_165_set; \
518 if (used != CNS11643_1_set) \
520 written = ucs4_to_cns11643l1 (ch, buf, 2); \
521 if (written != __UNKNOWN_10646_CHAR) \
523 used = CNS11643_1_set; \
528 written = ucs4_to_cns11643 (ch, tmpbuf, 3); \
529 if (written == 3 && tmpbuf[0] >= 3 && tmpbuf[0] <= 7) \
531 buf[0] = tmpbuf[1]; \
532 buf[1] = tmpbuf[2]; \
536 used = CNS11643_3_set; \
539 used = CNS11643_4_set; \
542 used = CNS11643_5_set; \
545 used = CNS11643_6_set; \
548 used = CNS11643_7_set; \
557 /* XXX Currently GB7590 and GB13132 are not supported. */\
559 /* Even this does not work. Error. */ \
562 if (used == ASCII_set) \
564 UNICODE_TAG_HANDLER (ch, 4); \
565 STANDARD_ERR_HANDLER (4); \
569 assert (written == 2); \
571 /* See whether we have to emit an escape sequence. */ \
574 /* First see whether we announced that we use this \
576 if ((used & SO_mask) != 0 && (ann & SO_ann) != (used << 8)) \
578 const char *escseq; \
580 if (outptr + 4 > outend) \
582 result = __GCONV_FULL_OUTPUT; \
586 assert (used >= 1 && used <= 4); \
587 escseq = ")A\0\0)G)E" + (used - 1) * 2; \
590 *outptr++ = *escseq++; \
591 *outptr++ = *escseq++; \
593 ann = (ann & ~SO_ann) | (used << 8); \
595 else if ((used & SS2_mask) != 0 && (ann & SS2_ann) != (used << 8))\
597 const char *escseq; \
599 assert (used == CNS11643_2_set); /* XXX */ \
603 *outptr++ = *escseq++; \
604 *outptr++ = *escseq++; \
606 ann = (ann & ~SS2_ann) | (used << 8); \
608 else if ((used & SS3_mask) != 0 && (ann & SS3_ann) != (used << 8))\
610 const char *escseq; \
612 assert ((used >> 5) >= 3 && (used >> 5) <= 7); \
613 escseq = "+I+J+K+L+M" + ((used >> 5) - 3) * 2; \
616 *outptr++ = *escseq++; \
617 *outptr++ = *escseq++; \
619 ann = (ann & ~SS3_ann) | (used << 8); \
622 if (used == CNS11643_2_set) \
624 if (outptr + 2 > outend) \
626 result = __GCONV_FULL_OUTPUT; \
632 else if (used >= CNS11643_3_set && used <= CNS11643_7_set) \
634 if (outptr + 2 > outend) \
636 result = __GCONV_FULL_OUTPUT; \
644 /* We only have to emit something if currently ASCII is \
645 selected. Otherwise we are switching within the \
647 if (set == ASCII_set) \
649 if (outptr + 1 > outend) \
651 result = __GCONV_FULL_OUTPUT; \
658 /* Always test the length here since we have used up all the \
659 guaranteed output buffer slots. */ \
660 if (outptr + 2 > outend) \
662 result = __GCONV_FULL_OUTPUT; \
666 else if (outptr + 2 > outend) \
668 result = __GCONV_FULL_OUTPUT; \
672 *outptr++ = buf[0]; \
673 *outptr++ = buf[1]; \
677 /* Now that we wrote the output increment the input pointer. */ \
680 #define EXTRA_LOOP_DECLS , int *setp
681 #define INIT_PARAMS int set = (*setp >> 3) & CURRENT_MASK; \
682 int ann = (*setp >> 3) & ~CURRENT_MASK
683 #define UPDATE_PARAMS *setp = (set | ann) << 3
684 #define LOOP_NEED_FLAGS
685 #include <iconv/loop.c>
688 /* Now define the toplevel functions. */
689 #include <iconv/skeleton.c>