1 /* Conversion module for ISO-2022-CN-EXT.
2 Copyright (C) 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include "iso-ir-165.h"
28 #include "cns11643l1.h"
29 #include "cns11643l2.h"
33 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
36 /* We have single-byte shift-in and shift-out sequences, and the single
37 shift sequences SS2 and SS3 which replaces the SS2/SS3 designation for
38 the next two bytes. */
46 /* Definitions used in the body of the `gconv' function. */
47 #define CHARSET_NAME "ISO-2022-CN-EXT//"
50 #define FROM_LOOP from_iso2022cn_ext_loop
51 #define TO_LOOP to_iso2022cn_ext_loop
52 #define MIN_NEEDED_FROM 1
53 #define MAX_NEEDED_FROM 4
54 #define MIN_NEEDED_TO 4
55 #define MAX_NEEDED_TO 4
56 #define PREPARE_LOOP \
58 int *setp = &data->__statep->__count;
59 #define EXTRA_LOOP_ARGS , setp
62 /* The charsets GB/T 12345-90, GB 7589-87, GB/T 13131-9X, GB 7590-87,
63 and GB/T 13132-9X are not registered to the best of my knowledge and
64 therefore have no escape sequence assigned. We cannot handle them
65 for this reason. Tell the implementation about this. */
73 /* The COUNT element of the state keeps track of the currently selected
74 character set. The possible values are: */
86 CNS11643_2_set
= 3 << 3,
91 CNS11643_3_set
= 3 << 5,
92 CNS11643_4_set
= 4 << 5,
93 CNS11643_5_set
= 5 << 5,
94 CNS11643_6_set
= 6 << 5,
95 CNS11643_7_set
= 7 << 5,
98 #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask)
101 GB12345_ann
= 2 << 8,
102 CNS11643_1_ann
= 3 << 8,
103 ISO_IR_165_ann
= 4 << 8,
106 GB7589_ann
= 1 << 11,
107 GB13131_ann
= 2 << 11,
108 CNS11643_2_ann
= 3 << 11,
111 GB7590_ann
= 1 << 13,
112 GB13132_ann
= 2 << 13,
113 CNS11643_3_ann
= 3 << 13,
114 CNS11643_4_ann
= 4 << 13,
115 CNS11643_5_ann
= 5 << 13,
116 CNS11643_6_ann
= 6 << 13,
117 CNS11643_7_ann
= 7 << 13,
122 /* Since this is a stateful encoding we have to provide code which resets
123 the output state to the initial state. This has to be done during the
125 #define EMIT_SHIFT_TO_INIT \
126 if (data->__statep->__count >> 3 != ASCII_set) \
128 if (FROM_DIRECTION) \
129 /* It's easy, we don't have to emit anything, we just reset the \
130 state for the input. */ \
131 data->__statep->__count = ASCII_set << 3; \
134 unsigned char *outbuf = data->__outbuf; \
136 /* We are not in the initial state. To switch back we have \
138 if (outbuf == data->__outbufend) \
139 /* We don't have enough room in the output buffer. */ \
140 status = __GCONV_FULL_OUTPUT; \
143 /* Write out the shift sequence. */ \
145 if (data->__flags & __GCONV_IS_LAST) \
146 *irreversible += 1; \
147 data->__outbuf = outbuf; \
148 data->__statep->__count = ASCII_set << 3; \
154 /* Since we might have to reset input pointer we must be able to save
155 and retore the state. */
156 #define SAVE_RESET_STATE(Save) \
163 /* First define the conversion function from ISO-2022-CN to UCS4. */
164 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
165 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
166 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
167 #define LOOPFCT FROM_LOOP
170 uint32_t ch = *inptr; \
172 /* This is a 7bit character set, disallow all 8bit characters. */ \
175 if (! ignore_errors_p ()) \
177 result = __GCONV_ILLEGAL_INPUT; \
185 /* Recognize escape sequences. */ \
188 /* There are three kinds of escape sequences we have to handle: \
189 - those announcing the use of GB and CNS characters on the \
190 line; we can simply ignore them \
191 - the initial byte of the SS2 sequence. \
192 - the initial byte of the SS3 sequence. \
194 if (inptr + 2 > inend \
195 || (inptr[1] == '$' \
196 && (inptr + 3 > inend \
197 || (inptr[2] == ')' && inptr + 4 > inend) \
198 || (inptr[2] == '*' && inptr + 4 > inend) \
199 || (inptr[2] == '+' && inptr + 4 > inend))) \
200 || (inptr[1] == SS2_1 && inptr + 4 > inend) \
201 || (inptr[1] == SS3_1 && inptr + 4 > inend)) \
203 result = __GCONV_INCOMPLETE_INPUT; \
206 if (inptr[1] == '$' \
207 && ((inptr[2] == ')' \
208 && (inptr[3] == 'A' \
209 || (X12345 != '\0' && inptr[3] == X12345) \
210 || inptr[3] == 'E' || inptr[3] == 'G')) \
211 || (inptr[2] == '*' \
212 && ((X7589 != '\0' && inptr[3] == X7589) \
213 || (X13131 != '\0' && inptr[3] == X13131) \
214 || inptr[3] == 'H')) \
215 || (inptr[2] == '+' \
216 && ((X7590 != '\0' && inptr[3] == X7590) \
217 || (X13132 != '\0' && inptr[3] == X13132) \
218 || inptr[3] == 'I' || inptr[3] == 'J' \
219 || inptr[3] == 'K' || inptr[3] == 'L' \
220 || inptr[3] == 'M')))) \
222 /* OK, we accept those character sets. */ \
223 if (inptr[3] == 'A') \
224 ann = (ann & ~SO_ann) | GB2312_ann; \
225 else if (inptr[3] == 'G') \
226 ann = (ann & ~SO_ann) | CNS11643_1_ann; \
227 else if (inptr[3] == 'E') \
228 ann = (ann & ~SO_ann) | ISO_IR_165_ann; \
229 else if (X12345 != '\0' && inptr[3] == X12345) \
230 ann = (ann & ~SO_ann) | GB12345_ann; \
231 else if (inptr[3] == 'H') \
232 ann = (ann & ~SS2_ann) | CNS11643_2_ann; \
233 else if (X7589 != '\0' && inptr[3] == X7589) \
234 ann = (ann & ~SS2_ann) | GB7589_ann; \
235 else if (X13131 != '\0' && inptr[3] == X13131) \
236 ann = (ann & ~SS2_ann) | GB13131_ann; \
237 else if (inptr[3] == 'I') \
238 ann = (ann & ~SS3_ann) | CNS11643_3_ann; \
239 else if (inptr[3] == 'J') \
240 ann = (ann & ~SS3_ann) | CNS11643_4_ann; \
241 else if (inptr[3] == 'K') \
242 ann = (ann & ~SS3_ann) | CNS11643_5_ann; \
243 else if (inptr[3] == 'L') \
244 ann = (ann & ~SS3_ann) | CNS11643_6_ann; \
245 else if (inptr[3] == 'M') \
246 ann = (ann & ~SS3_ann) | CNS11643_7_ann; \
247 else if (X7590 != '\0' && inptr[3] == X7590) \
248 ann = (ann & ~SS3_ann) | GB7590_ann; \
249 else if (X13132 != '\0' && inptr[3] == X13132) \
250 ann = (ann & ~SS3_ann) | GB13132_ann; \
257 /* Switch to use GB2312, GB12345, CNS 11643 plane 1, or ISO-IR-165, \
258 depending on which S0 designation came last. The only problem \
259 is what to do with faulty input files where no designator came. \
260 XXX For now I'll default to use GB2312. If this is not the \
261 best behavior (e.g., we should flag an error) let me know. */ \
263 switch (ann & SO_ann) \
271 case CNS11643_1_ann: \
272 set = CNS11643_1_set; \
275 assert ((ann & SO_ann) == ISO_IR_165_ann); \
276 set = ISO_IR_165_set; \
283 /* Switch to use ASCII. */ \
289 if (ch == ESC && inptr[1] == SS2_1) \
291 /* This is a character from CNS 11643 plane 2. \
292 XXX We could test here whether the use of this character \
294 XXX Current GB7589 and GB13131 are not supported. */ \
296 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
297 if (ch == __UNKNOWN_10646_CHAR) \
299 if (! ignore_errors_p ()) \
302 result = __GCONV_ILLEGAL_INPUT; \
310 /* Note that we can assume here that at least 4 bytes are available if \
311 the first byte is ESC since otherwise the first if would have been \
313 else if (ch == ESC && inptr[1] == SS3_1) \
315 /* This is a character from CNS 11643 plane 3 or higher. \
316 XXX Currently GB7590 and GB13132 are not supported. */ \
318 const char *tmp = buf; \
322 switch (ann & SS3_ann) \
324 case CNS11643_3_ann: \
325 /* CNS 11643 plane 3 is part of the old CNS 11643 plane 14. */ \
326 if (buf[1] < 0x62 || (buf[1] == 0x62 && buf[2] <= 0x45)) \
329 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
332 ch = __UNKNOWN_10646_CHAR; \
335 /* XXX Currently planes 4 to 7 are not supported. */ \
336 ch = __UNKNOWN_10646_CHAR; \
339 if (ch == __UNKNOWN_10646_CHAR) \
341 if (! ignore_errors_p ()) \
343 result = __GCONV_ILLEGAL_INPUT; \
350 assert (tmp == buf + 3); \
353 else if (set == ASCII_set) \
355 /* Almost done, just advance the input pointer. */ \
360 /* That's pretty easy, we have a dedicated functions for this. */ \
361 if (inend - inptr < 2) \
363 result = __GCONV_INCOMPLETE_INPUT; \
366 if (set == GB2312_set) \
367 ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0); \
368 else if (set == ISO_IR_165_set) \
369 ch = isoir165_to_ucs4 (&inptr, inend - inptr); \
372 assert (set == CNS11643_1_set); \
373 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
378 result = __GCONV_INCOMPLETE_INPUT; \
381 else if (ch == __UNKNOWN_10646_CHAR) \
383 if (! ignore_errors_p ()) \
385 result = __GCONV_ILLEGAL_INPUT; \
394 *((uint32_t *) outptr)++ = ch; \
396 #define EXTRA_LOOP_DECLS , int *setp
397 #define INIT_PARAMS int set = (*setp >> 3) & CURRENT_MASK; \
398 int ann = (*setp >> 3) & ~CURRENT_MASK
399 #define UPDATE_PARAMS *setp = (set | ann) << 3
400 #define LOOP_NEED_FLAGS
401 #include <iconv/loop.c>
404 /* Next, define the other direction. */
405 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
406 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
407 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
408 #define LOOPFCT TO_LOOP
412 size_t written = 0; \
414 ch = *((const uint32_t *) inptr); \
416 /* First see whether we can write the character using the currently \
417 selected character set. */ \
420 if (set != ASCII_set) \
424 if (outptr == outend) \
426 result = __GCONV_FULL_OUTPUT; \
434 /* At the end of the line we have to clear the `ann' flags since \
435 every line must contain this information again. */ \
444 if (set == GB2312_set || ((ann & SO_ann) != CNS11643_1_ann \
445 && (ann & SO_ann) != ISO_IR_165_ann)) \
447 written = ucs4_to_gb2312 (ch, buf, 2); \
450 else if (set == ISO_IR_165_set || (ann & SO_ann) == ISO_IR_165_set) \
452 written = ucs4_to_isoir165 (ch, buf, 2); \
453 used = ISO_IR_165_set; \
457 written = ucs4_to_cns11643l1 (ch, buf, 2); \
458 used = CNS11643_1_set; \
461 if (written == __UNKNOWN_10646_CHAR) \
463 /* Cannot convert it using the currently selected SO set. \
464 Next try the SS2 set. */ \
465 written = ucs4_to_cns11643l2 (ch, buf, 2); \
466 if (written != __UNKNOWN_10646_CHAR) \
467 /* Yep, that worked. */ \
468 used = CNS11643_2_set; \
476 /* Well, see whether we have to change the SO set. */ \
478 if (used != GB2312_set) \
480 written = ucs4_to_gb2312 (ch, buf, 2); \
481 if (written != __UNKNOWN_10646_CHAR) \
488 if (used != ISO_IR_165_set) \
490 written = ucs4_to_isoir165 (ch, buf, 2); \
491 if (written != __UNKNOWN_10646_CHAR) \
493 used = ISO_IR_165_set; \
498 if (used != CNS11643_1_set) \
500 written = ucs4_to_cns11643l1 (ch, buf, 2); \
501 if (written != __UNKNOWN_10646_CHAR) \
503 used = CNS11643_1_set; \
508 written = ucs4_to_cns11643 (ch, tmpbuf, 3); \
509 if (written == 3 && tmpbuf[0] != 1 && tmpbuf[0] != 2) \
511 buf[0] = tmpbuf[1]; \
512 buf[1] = tmpbuf[2]; \
514 /* CNS 11643 plane 3 is part of the old CNS 11643 \
516 XXX Currently planes 4 to 7 are not supported. */ \
517 if (tmpbuf[0] == 14 \
518 && (tmpbuf[1] < 0x62 \
519 || (tmpbuf[1] == 0x62 && tmpbuf[2] <= 0x45))) \
521 used = CNS11643_3_set; \
526 /* Even this does not work. Error. */ \
529 if (used == ASCII_set) \
531 STANDARD_ERR_HANDLER (4); \
535 assert (written == 2); \
537 /* See whether we have to emit an escape sequence. */ \
540 /* First see whether we announced that we use this \
542 if ((used & SO_mask) != 0 && (ann & SO_ann) != (used << 8)) \
544 const char *escseq; \
546 if (outptr + 4 > outend) \
548 result = __GCONV_FULL_OUTPUT; \
552 assert (used >= 1 && used <= 4); \
553 escseq = ")A\0\0)G)E" + (used - 1) * 2; \
556 *outptr++ = *escseq++; \
557 *outptr++ = *escseq++; \
559 ann = (ann & ~SO_ann) | (used << 8); \
561 else if ((used & SS2_mask) != 0 && (ann & SS2_ann) != (used << 8))\
563 const char *escseq; \
565 assert (used == CNS11643_2_set); /* XXX */ \
569 *outptr++ = *escseq++; \
570 *outptr++ = *escseq++; \
572 ann = (ann & ~SS2_ann) | (used << 8); \
574 else if ((used & SS3_mask) != 0 && (ann & SS3_ann) != (used << 8))\
576 const char *escseq; \
578 assert ((used >> 5) >= 3 && (used >> 5) <= 7); \
579 escseq = "+I+J+K+L+M" + ((used >> 5) - 3) * 2; \
582 *outptr++ = *escseq++; \
583 *outptr++ = *escseq++; \
585 ann = (ann & ~SS3_ann) | (used << 8); \
588 if (used == CNS11643_2_set) \
590 if (outptr + 2 > outend) \
592 result = __GCONV_FULL_OUTPUT; \
598 else if (used >= CNS11643_3_set && used <= CNS11643_7_set) \
600 if (outptr + 2 > outend) \
602 result = __GCONV_FULL_OUTPUT; \
610 /* We only have to emit something if currently ASCII is \
611 selected. Otherwise we are switching within the \
613 if (set == ASCII_set) \
615 if (outptr + 1 > outend) \
617 result = __GCONV_FULL_OUTPUT; \
624 /* Always test the length here since we have used up all the \
625 guaranteed output buffer slots. */ \
626 if (outptr + 2 > outend) \
628 result = __GCONV_FULL_OUTPUT; \
632 else if (outptr + 2 > outend) \
634 result = __GCONV_FULL_OUTPUT; \
638 *outptr++ = buf[0]; \
639 *outptr++ = buf[1]; \
643 /* Now that we wrote the output increment the input pointer. */ \
646 #define EXTRA_LOOP_DECLS , int *setp
647 #define INIT_PARAMS int set = (*setp >> 3) & CURRENT_MASK; \
648 int ann = (*setp >> 3) & ~CURRENT_MASK
649 #define UPDATE_PARAMS *setp = (set | ann) << 3
650 #define LOOP_NEED_FLAGS
651 #include <iconv/loop.c>
654 /* Now define the toplevel functions. */
655 #include <iconv/skeleton.c>