Update.
[glibc.git] / iconvdata / iso-2022-cn-ext.c
blobd6ab2e8bbe8de9f5727005029ecb54a0c8757f2a
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. */
21 #include <dlfcn.h>
22 #include <gconv.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "gb2312.h"
26 #include "iso-ir-165.h"
27 #include "cns11643.h"
28 #include "cns11643l1.h"
29 #include "cns11643l2.h"
31 #include <assert.h>
33 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
34 #define ESC 0x1b
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. */
39 #define SI 0x0f
40 #define SO 0x0e
41 #define SS2_0 ESC
42 #define SS2_1 0x4e
43 #define SS3_0 ESC
44 #define SS3_1 0x4f
46 /* Definitions used in the body of the `gconv' function. */
47 #define CHARSET_NAME "ISO-2022-CN-EXT//"
48 #define DEFINE_INIT 1
49 #define DEFINE_FINI 1
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 \
57 int save_set; \
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. */
66 #define X12345 '\0'
67 #define X7589 '\0'
68 #define X13131 '\0'
69 #define X7590 '\0'
70 #define X13132 '\0'
73 /* The COUNT element of the state keeps track of the currently selected
74 character set. The possible values are: */
75 enum
77 ASCII_set = 0,
78 GB2312_set,
79 GB12345_set,
80 CNS11643_1_set,
81 ISO_IR_165_set,
82 SO_mask = 7,
84 GB7589_set = 1 << 3,
85 GB13131_set = 2 << 3,
86 CNS11643_2_set = 3 << 3,
87 SS2_mask = 3 << 3,
89 GB7590_set = 1 << 5,
90 GB13132_set = 2 << 5,
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,
96 SS3_mask = 7 << 5,
98 #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask)
100 GB2312_ann = 1 << 8,
101 GB12345_ann = 2 << 8,
102 CNS11643_1_ann = 3 << 8,
103 ISO_IR_165_ann = 4 << 8,
104 SO_ann = 7 << 8,
106 GB7589_ann = 1 << 11,
107 GB13131_ann = 2 << 11,
108 CNS11643_2_ann = 3 << 11,
109 SS2_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,
118 SS3_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
124 flushing. */
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; \
132 else \
134 unsigned char *outbuf = data->__outbuf; \
136 /* We are not in the initial state. To switch back we have \
137 to emit `SI'. */ \
138 if (outbuf == data->__outbufend) \
139 /* We don't have enough room in the output buffer. */ \
140 status = __GCONV_FULL_OUTPUT; \
141 else \
143 /* Write out the shift sequence. */ \
144 *outbuf++ = SI; \
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) \
157 if (Save) \
158 save_set = *setp; \
159 else \
160 *setp = save_set
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
168 #define BODY \
170 uint32_t ch = *inptr; \
172 /* This is a 7bit character set, disallow all 8bit characters. */ \
173 if (ch > 0x7f) \
175 if (! ignore_errors_p ()) \
177 result = __GCONV_ILLEGAL_INPUT; \
178 break; \
180 ++inptr; \
181 ++*irreversible; \
182 continue; \
185 /* Recognize escape sequences. */ \
186 if (ch == ESC) \
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. \
193 */ \
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; \
204 break; \
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; \
251 inptr += 4; \
252 continue; \
255 else if (ch == SO) \
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. */ \
262 ++inptr; \
263 switch (ann & SO_ann) \
265 case GB2312_ann: \
266 set = GB2312_set; \
267 break; \
268 case GB12345_ann: \
269 set = GB12345_set; \
270 break; \
271 case CNS11643_1_ann: \
272 set = CNS11643_1_set; \
273 break; \
274 default: \
275 assert ((ann & SO_ann) == ISO_IR_165_ann); \
276 set = ISO_IR_165_set; \
277 break; \
279 continue; \
281 else if (ch == SI) \
283 /* Switch to use ASCII. */ \
284 ++inptr; \
285 set = ASCII_set; \
286 continue; \
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 \
293 set was announced. \
294 XXX Current GB7589 and GB13131 are not supported. */ \
295 inptr += 2; \
296 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
297 if (ch == __UNKNOWN_10646_CHAR) \
299 if (! ignore_errors_p ()) \
301 inptr -= 2; \
302 result = __GCONV_ILLEGAL_INPUT; \
303 break; \
305 inptr += 2; \
306 ++*irreversible; \
307 continue; \
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 \
312 true. */ \
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. */ \
317 char buf[3]; \
318 const char *tmp = buf; \
320 buf[1] = inptr[2]; \
321 buf[2] = inptr[3]; \
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)) \
328 buf[0] = 0x2e; \
329 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
331 else \
332 ch = __UNKNOWN_10646_CHAR; \
333 break; \
334 default: \
335 /* XXX Currently planes 4 to 7 are not supported. */ \
336 ch = __UNKNOWN_10646_CHAR; \
337 break; \
339 if (ch == __UNKNOWN_10646_CHAR) \
341 if (! ignore_errors_p ()) \
343 result = __GCONV_ILLEGAL_INPUT; \
344 break; \
346 inptr += 4; \
347 ++*irreversible; \
348 continue; \
350 assert (tmp == buf + 3); \
351 inptr += 4; \
353 else if (set == ASCII_set) \
355 /* Almost done, just advance the input pointer. */ \
356 ++inptr; \
358 else \
360 /* That's pretty easy, we have a dedicated functions for this. */ \
361 if (inend - inptr < 2) \
363 result = __GCONV_INCOMPLETE_INPUT; \
364 break; \
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); \
370 else \
372 assert (set == CNS11643_1_set); \
373 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
376 if (ch == 0) \
378 result = __GCONV_INCOMPLETE_INPUT; \
379 break; \
381 else if (ch == __UNKNOWN_10646_CHAR) \
383 if (! ignore_errors_p ()) \
385 result = __GCONV_ILLEGAL_INPUT; \
386 break; \
388 inptr += 2; \
389 ++*irreversible; \
390 continue; \
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
409 #define BODY \
411 uint32_t ch; \
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. */ \
418 if (ch < 0x80) \
420 if (set != ASCII_set) \
422 *outptr++ = SI; \
423 set = ASCII_set; \
424 if (outptr == outend) \
426 result = __GCONV_FULL_OUTPUT; \
427 break; \
431 *outptr++ = ch; \
432 written = 1; \
434 /* At the end of the line we have to clear the `ann' flags since \
435 every line must contain this information again. */ \
436 if (ch == L'\n') \
437 ann = 0; \
439 else \
441 char buf[2]; \
442 int used; \
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); \
448 used = GB2312_set; \
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; \
455 else \
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; \
469 else \
471 char tmpbuf[3]; \
473 switch (0) \
475 default: \
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) \
483 used = GB2312_set; \
484 break; \
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; \
494 break; \
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; \
504 break; \
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]; \
513 written = 2; \
514 /* CNS 11643 plane 3 is part of the old CNS 11643 \
515 plane 14. \
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; \
522 break; \
526 /* Even this does not work. Error. */ \
527 used = ASCII_set; \
529 if (used == ASCII_set) \
531 STANDARD_ERR_HANDLER (4); \
535 assert (written == 2); \
537 /* See whether we have to emit an escape sequence. */ \
538 if (set != used) \
540 /* First see whether we announced that we use this \
541 character set. */ \
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; \
549 break; \
552 assert (used >= 1 && used <= 4); \
553 escseq = ")A\0\0)G)E" + (used - 1) * 2; \
554 *outptr++ = ESC; \
555 *outptr++ = '$'; \
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 */ \
566 escseq = "*H"; \
567 *outptr++ = ESC; \
568 *outptr++ = '$'; \
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; \
580 *outptr++ = ESC; \
581 *outptr++ = '$'; \
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; \
593 break; \
595 *outptr++ = SS2_0; \
596 *outptr++ = SS2_1; \
598 else if (used >= CNS11643_3_set && used <= CNS11643_7_set) \
600 if (outptr + 2 > outend) \
602 result = __GCONV_FULL_OUTPUT; \
603 break; \
605 *outptr++ = SS3_0; \
606 *outptr++ = SS3_1; \
608 else \
610 /* We only have to emit something if currently ASCII is \
611 selected. Otherwise we are switching within the \
612 SO charset. */ \
613 if (set == ASCII_set) \
615 if (outptr + 1 > outend) \
617 result = __GCONV_FULL_OUTPUT; \
618 break; \
620 *outptr++ = SO; \
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; \
629 break; \
632 else if (outptr + 2 > outend) \
634 result = __GCONV_FULL_OUTPUT; \
635 break; \
638 *outptr++ = buf[0]; \
639 *outptr++ = buf[1]; \
640 set = used; \
643 /* Now that we wrote the output increment the input pointer. */ \
644 inptr += 4; \
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>