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"
27 #include "cns11643l1.h"
28 #include "cns11643l2.h"
32 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
35 /* We have single-byte shift-in and shift-out sequences, and the single
36 shift sequences SS2 and SS3 which replaces the SS2/SS3 designation for
37 the next two bytes. */
45 /* Definitions used in the body of the `gconv' function. */
46 #define CHARSET_NAME "ISO-2022-CN-EXT//"
49 #define FROM_LOOP from_iso2022cn_ext_loop
50 #define TO_LOOP to_iso2022cn_ext_loop
51 #define MIN_NEEDED_FROM 1
52 #define MAX_NEEDED_FROM 4
53 #define MIN_NEEDED_TO 4
54 #define MAX_NEEDED_TO 4
55 #define PREPARE_LOOP \
57 int *setp = &data->__statep->__count;
58 #define EXTRA_LOOP_ARGS , setp
61 /* The charsets GB/T 12345-90, GB 7589-87, GB/T 13131-9X, GB 7590-87,
62 and GB/T 13132-9X are not registered to the best of my knowledge and
63 therefore have no escape sequence assigned. We cannot handle them
64 for this reason. Tell the implementation about this. */
72 /* The COUNT element of the state keeps track of the currently selected
73 character set. The possible values are: */
97 #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask)
101 CNS11643_1_ann
= 768,
102 ISO_IR_165_ann
= 1024,
107 CNS11643_2_ann
= 6144,
112 CNS11643_3_ann
= 24576,
113 CNS11643_4_ann
= 32768,
114 CNS11643_5_ann
= 40960,
115 CNS11643_6_ann
= 49152,
116 CNS11643_7_ann
= 57344,
121 /* Since this is a stateful encoding we have to provide code which resets
122 the output state to the initial state. This has to be done during the
124 #define EMIT_SHIFT_TO_INIT \
125 if (data->__statep->__count >> 2 != ASCII_set) \
127 if (FROM_DIRECTION) \
128 /* It's easy, we don't have to emit anything, we just reset the \
129 state for the input. */ \
130 data->__statep->__count = ASCII_set << 2; \
133 unsigned char *outbuf = data->__outbuf; \
135 /* We are not in the initial state. To switch back we have \
137 if (outbuf == data->__outbufend) \
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->__outbuf = outbuf; \
147 data->__statep->__count = ASCII_set << 2; \
153 /* Since we might have to reset input pointer we must be able to save
154 and retore the state. */
155 #define SAVE_RESET_STATE(Save) \
162 /* First define the conversion function from ISO-2022-CN to UCS4. */
163 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
164 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
165 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
166 #define LOOPFCT FROM_LOOP
169 uint32_t ch = *inptr; \
171 /* This is a 7bit character set, disallow all 8bit characters. */ \
174 if (! ignore_errors_p ()) \
176 result = __GCONV_ILLEGAL_INPUT; \
184 /* Recognize escape sequences. */ \
187 /* There are three kinds of escape sequences we have to handle: \
188 - those announcing the use of GB and CNS characters on the \
189 line; we can simply ignore them \
190 - the initial byte of the SS2 sequence. \
191 - the initial byte of the SS3 sequence. \
193 if (inptr + 1 > inend \
194 || (inptr[1] == '$' \
195 && (inptr + 2 > inend \
196 || (inptr[2] == ')' && inptr + 3 > inend) \
197 || (inptr[2] == '*' && inptr + 3 > inend) \
198 || (inptr[2] == '+' && inptr + 3 > inend))) \
199 || (inptr[1] == SS2_1 && inptr + 3 > inend) \
200 || (inptr[1] == SS3_1 && inptr + 3 > inend)) \
202 result = __GCONV_EMPTY_INPUT; \
205 if (inptr[1] == '$' \
206 && ((inptr[2] == ')' \
207 && (inptr[3] == 'A' \
208 || (X12345 != '\0' && inptr[3] == X12345) \
209 || inptr[3] == 'E' || inptr[3] == 'G')) \
210 || (inptr[2] == '*' \
211 && ((X7589 != '\0' && inptr[3] == X7589) \
212 || (X13131 != '\0' && inptr[3] == X13131) \
213 || inptr[3] == 'H')) \
214 || (inptr[2] == '+' \
215 && ((X7590 != '\0' && inptr[3] == X7590) \
216 || (X13132 != '\0' && inptr[3] == X13132) \
217 || inptr[3] == 'I' || inptr[3] == 'J' \
218 || inptr[3] == 'K' || inptr[3] == 'L' \
219 || inptr[3] == 'M')))) \
221 /* OK, we accept those character sets. */ \
222 if (inptr[3] == 'A') \
223 ann = (ann & ~SO_ann) | GB2312_ann; \
224 else if (inptr[3] == 'G') \
225 ann = (ann & ~SO_ann) | CNS11643_1_ann; \
226 else if (inptr[3] == 'E') \
227 ann = (ann & ~SO_ann) | ISO_IR_165_ann; \
228 else if (X12345 != '\0' && inptr[3] == X12345) \
229 ann = (ann & ~SO_ann) | GB12345_ann; \
230 else if (inptr[3] == 'H') \
231 ann = (ann & ~SS2_ann) | CNS11643_2_ann; \
232 else if (X7589 != '\0' && inptr[3] == X7589) \
233 ann = (ann & ~SS2_ann) | GB7589_ann; \
234 else if (X13131 != '\0' && inptr[3] == X13131) \
235 ann = (ann & ~SS2_ann) | GB13131_ann; \
236 else if (inptr[3] == 'I') \
237 ann = (ann & ~SS3_ann) | CNS11643_3_ann; \
238 else if (inptr[3] == 'J') \
239 ann = (ann & ~SS3_ann) | CNS11643_4_ann; \
240 else if (inptr[3] == 'K') \
241 ann = (ann & ~SS3_ann) | CNS11643_5_ann; \
242 else if (inptr[3] == 'L') \
243 ann = (ann & ~SS3_ann) | CNS11643_6_ann; \
244 else if (inptr[3] == 'M') \
245 ann = (ann & ~SS3_ann) | CNS11643_7_ann; \
246 else if (X7590 != '\0' && inptr[3] == X7590) \
247 ann = (ann & ~SS3_ann) | GB7590_ann; \
248 else if (X13132 != '\0' && inptr[3] == X13132) \
249 ann = (ann & ~SS3_ann) | GB13132_ann; \
256 /* Switch to use GB2312, GB12345, CNS 11643 plane 1, or ISO-IR-165, \
257 depending on which S0 designation came last. The only problem \
258 is what to do with faulty input files where no designator came. \
259 XXX For now I'll default to use GB2312. If this is not the \
260 best behavior (e.g., we should flag an error) let me know. */ \
262 switch (ann & SO_ann) \
270 case CNS11643_1_ann: \
271 set = CNS11643_1_set; \
274 assert ((ann & SO_ann) == ISO_IR_165_ann); \
275 set = ISO_IR_165_set; \
282 /* Switch to use ASCII. */ \
288 if (ch == ESC && (inend - inptr == 1 || inptr[1] == SS2_1)) \
290 /* This is a character from CNS 11643 plane 2. \
291 XXX We could test here whether the use of this character \
293 XXX Current GB7589 and GB13131 are not supported. */ \
294 if (inend - inptr < 4) \
296 result = __GCONV_INCOMPLETE_INPUT; \
300 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
301 if (ch == __UNKNOWN_10646_CHAR) \
303 if (! ignore_errors_p ()) \
306 result = __GCONV_ILLEGAL_INPUT; \
313 /* Note that we can assume here that at least bytes are available if \
314 the first byte is ESC since otherwise the first if would have been \
316 else if (ch == ESC && inptr[1] == SS3_1) \
318 /* This is a character from CNS 11643 plane 3 or higher. \
319 XXX Current GB7590 and GB13132 are not supported. */ \
320 if (inend - inptr < 4) \
322 result = __GCONV_INCOMPLETE_INPUT; \
326 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
327 if (ch == __UNKNOWN_10646_CHAR) \
329 if (! ignore_errors_p ()) \
332 result = __GCONV_ILLEGAL_INPUT; \
339 else if (set == ASCII_set) \
341 /* Almost done, just advance the input pointer. */ \
346 /* That's pretty easy, we have a dedicated functions for this. */ \
347 if (inend - inptr < 2) \
349 result = __GCONV_INCOMPLETE_INPUT; \
352 if (set == GB2312_set) \
353 ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0); \
354 else if (set == ISO_IR_165_set) \
355 ch = isoir165_to_ucs4 (&inptr, inend - inptr); \
358 assert (set == CNS11643_1_set); \
359 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
364 result = __GCONV_EMPTY_INPUT; \
367 else if (ch == __UNKNOWN_10646_CHAR) \
369 if (! ignore_errors_p ()) \
371 result = __GCONV_ILLEGAL_INPUT; \
380 *((uint32_t *) outptr)++ = ch; \
382 #define EXTRA_LOOP_DECLS , int *setp
383 #define INIT_PARAMS int set = (*setp >> 2) & CURRENT_MASK; \
384 int ann = (*setp >> 2) & ~CURRENT_MASK
385 #define UPDATE_PARAMS *setp = (set | ann) << 2
386 #define LOOP_NEED_FLAGS
387 #include <iconv/loop.c>
390 /* Next, define the other direction. */
391 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
392 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
393 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
394 #define LOOPFCT TO_LOOP
398 size_t written = 0; \
400 ch = *((uint32_t *) inptr); \
402 /* First see whether we can write the character using the currently \
403 selected character set. */ \
406 if (set != ASCII_set) \
410 if (outptr == outend) \
412 result = __GCONV_FULL_OUTPUT; \
420 /* At the end of the line we have to clear the `ann' flags since \
421 every line must contain this information again. */ \
430 if (set == GB2312_set || ((ann & CNS11643_1_ann) == 0 \
431 && (ann & ISO_IR_165_ann) == 0)) \
433 written = ucs4_to_gb2312 (ch, buf, 2); \
436 else if (set == ISO_IR_165_set || (ann & ISO_IR_165_set) != 0) \
438 written = ucs4_to_gb2312 (ch, buf, 2); \
443 written = ucs4_to_cns11643l1 (ch, buf, 2); \
444 used = CNS11643_1_set; \
447 if (written == __UNKNOWN_10646_CHAR) \
449 /* Cannot convert it using the currently selected SO set. \
450 Next try the SS2 set. */ \
451 written = ucs4_to_cns11643l2 (ch, buf, 2); \
452 if (written != __UNKNOWN_10646_CHAR) \
453 /* Yep, that worked. */ \
454 used = CNS11643_2_set; \
457 /* Well, see whether we have to change the SO set. */ \
458 if (set != GB2312_set) \
460 written = ucs4_to_gb2312 (ch, buf, 2); \
461 if (written != __UNKNOWN_10646_CHAR) \
464 if (written == __UNKNOWN_10646_CHAR && set != ISO_IR_165_set) \
466 written = ucs4_to_isoir165 (ch, buf, 2); \
467 if (written != __UNKNOWN_10646_CHAR) \
468 used = ISO_IR_165_set; \
470 if (written == __UNKNOWN_10646_CHAR && set != CNS11643_1_set) \
472 written = ucs4_to_cns11643l1 (ch, buf, 2); \
473 if (written != __UNKNOWN_10646_CHAR) \
474 used = CNS11643_1_set; \
477 if (written == __UNKNOWN_10646_CHAR) \
479 /* Even this does not work. Error. */ \
480 STANDARD_ERR_HANDLER (4); \
484 assert (written == 2); \
486 /* See whether we have to emit an escape sequence. */ \
489 /* First see whether we announced that we use this \
491 if ((ann & (2 << used)) == 0) \
493 const char *escseq; \
495 if (outptr + 4 > outend) \
497 result = __GCONV_FULL_OUTPUT; \
501 assert (used >= 1 && used <= 4); \
502 escseq = "\e$)A\e$)G\e$*H\e$)E" + (used - 1) * 4; \
503 *outptr++ = *escseq++; \
504 *outptr++ = *escseq++; \
505 *outptr++ = *escseq++; \
506 *outptr++ = *escseq++; \
508 if (used == GB2312_set) \
509 ann = (ann & CNS11643_2_ann) | GB2312_ann; \
510 else if (used == CNS11643_1_set) \
511 ann = (ann & CNS11643_2_ann) | CNS11643_1_ann; \
513 ann |= CNS11643_2_ann; \
516 if (used == CNS11643_2_set) \
518 if (outptr + 2 > outend) \
520 result = __GCONV_FULL_OUTPUT; \
528 /* We only have to emit something if currently ASCII is \
529 selected. Otherwise we are switching within the \
531 if (set == ASCII_set) \
533 if (outptr + 1 > outend) \
535 result = __GCONV_FULL_OUTPUT; \
542 /* Always test the length here since we have used up all the \
543 guaranteed output buffer slots. */ \
544 if (outptr + 2 > outend) \
546 result = __GCONV_FULL_OUTPUT; \
550 else if (outptr + 2 > outend) \
552 result = __GCONV_FULL_OUTPUT; \
556 *outptr++ = buf[0]; \
557 *outptr++ = buf[1]; \
560 /* Now that we wrote the output increment the input pointer. */ \
563 #define EXTRA_LOOP_DECLS , int *setp
564 #define INIT_PARAMS int set = (*setp >> 2) & CURRENT_MASK; \
565 int ann = (*setp >> 2) & ~CURRENT_MASK
566 #define UPDATE_PARAMS *setp = (set | ann) << 2
567 #define LOOP_NEED_FLAGS
568 #include <iconv/loop.c>
571 /* Now define the toplevel functions. */
572 #include <iconv/skeleton.c>