Include <string.h>.
[glibc.git] / iconvdata / iso-2022-cn-ext.c
blob60e875cf02e45fc1a6965836606fc706dd80184c
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
19 02111-1307 USA. */
21 #include <dlfcn.h>
22 #include <gconv.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "gb2312.h"
27 #include "iso-ir-165.h"
28 #include "cns11643.h"
29 #include "cns11643l1.h"
30 #include "cns11643l2.h"
32 #include <assert.h>
34 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
35 #define ESC 0x1b
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. */
40 #define SI 0x0f
41 #define SO 0x0e
42 #define SS2_0 ESC
43 #define SS2_1 0x4e
44 #define SS3_0 ESC
45 #define SS3_1 0x4f
47 /* Definitions used in the body of the `gconv' function. */
48 #define CHARSET_NAME "ISO-2022-CN-EXT//"
49 #define DEFINE_INIT 1
50 #define DEFINE_FINI 1
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 \
58 int save_set; \
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. */
67 #define X12345 '\0'
68 #define X7589 '\0'
69 #define X13131 '\0'
70 #define X7590 '\0'
71 #define X13132 '\0'
74 /* The COUNT element of the state keeps track of the currently selected
75 character set. The possible values are: */
76 enum
78 ASCII_set = 0,
79 GB2312_set,
80 GB12345_set,
81 CNS11643_1_set,
82 ISO_IR_165_set,
83 SO_mask = 7,
85 GB7589_set = 1 << 3,
86 GB13131_set = 2 << 3,
87 CNS11643_2_set = 3 << 3,
88 SS2_mask = 3 << 3,
90 GB7590_set = 1 << 5,
91 GB13132_set = 2 << 5,
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,
97 SS3_mask = 7 << 5,
99 #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask)
101 GB2312_ann = 1 << 8,
102 GB12345_ann = 2 << 8,
103 CNS11643_1_ann = 3 << 8,
104 ISO_IR_165_ann = 4 << 8,
105 SO_ann = 7 << 8,
107 GB7589_ann = 1 << 11,
108 GB13131_ann = 2 << 11,
109 CNS11643_2_ann = 3 << 11,
110 SS2_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,
119 SS3_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
125 flushing. */
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; \
133 else \
135 /* We are not in the initial state. To switch back we have \
136 to emit `SI'. */ \
137 if (__builtin_expect (outbuf == outend, 0)) \
138 /* We don't have enough room in the output buffer. */ \
139 status = __GCONV_FULL_OUTPUT; \
140 else \
142 /* Write out the shift sequence. */ \
143 *outbuf++ = SI; \
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) \
155 if (Save) \
156 save_set = *setp; \
157 else \
158 *setp = save_set
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
166 #define BODY \
168 uint32_t ch = *inptr; \
170 /* This is a 7bit character set, disallow all 8bit characters. */ \
171 if (ch > 0x7f) \
173 if (! ignore_errors_p ()) \
175 result = __GCONV_ILLEGAL_INPUT; \
176 break; \
178 ++inptr; \
179 ++*irreversible; \
180 continue; \
183 /* Recognize escape sequences. */ \
184 if (ch == ESC) \
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. \
191 */ \
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; \
202 break; \
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; \
249 inptr += 4; \
250 continue; \
253 else if (ch == SO) \
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. */ \
260 ++inptr; \
261 if ((ann & SO_ann) != 0) \
262 switch (ann & SO_ann) \
264 case GB2312_ann: \
265 set = GB2312_set; \
266 break; \
267 case GB12345_ann: \
268 set = GB12345_set; \
269 break; \
270 case CNS11643_1_ann: \
271 set = CNS11643_1_set; \
272 break; \
273 case ISO_IR_165_ann: \
274 set = ISO_IR_165_set; \
275 break; \
276 default: \
277 abort (); \
279 else \
281 if (! ignore_errors_p ()) \
283 result = __GCONV_ILLEGAL_INPUT; \
284 break; \
286 ++inptr; \
287 ++*irreversible; \
289 continue; \
291 else if (ch == SI) \
293 /* Switch to use ASCII. */ \
294 ++inptr; \
295 set = ASCII_set; \
296 continue; \
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 \
303 set was announced. \
304 XXX Currently GB7589 and GB13131 are not supported. */ \
305 inptr += 2; \
306 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
307 if (ch == __UNKNOWN_10646_CHAR) \
309 if (! ignore_errors_p ()) \
311 inptr -= 2; \
312 result = __GCONV_ILLEGAL_INPUT; \
313 break; \
315 inptr += 2; \
316 ++*irreversible; \
317 continue; \
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 \
322 true. */ \
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. */ \
327 char buf[3]; \
328 const char *tmp = buf; \
330 buf[1] = inptr[2]; \
331 buf[2] = inptr[3]; \
332 switch (ann & SS3_ann) \
334 case CNS11643_3_ann: \
335 buf[0] = 0x23; \
336 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
337 break; \
338 case CNS11643_4_ann: \
339 buf[0] = 0x24; \
340 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
341 break; \
342 case CNS11643_5_ann: \
343 buf[0] = 0x25; \
344 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
345 break; \
346 case CNS11643_6_ann: \
347 buf[0] = 0x26; \
348 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
349 break; \
350 case CNS11643_7_ann: \
351 buf[0] = 0x27; \
352 ch = cns11643_to_ucs4 (&tmp, 3, 0); \
353 break; \
354 default: \
355 /* XXX Currently GB7590 and GB13132 are not supported. */ \
356 ch = __UNKNOWN_10646_CHAR; \
357 break; \
359 if (ch == __UNKNOWN_10646_CHAR) \
361 if (! ignore_errors_p ()) \
363 result = __GCONV_ILLEGAL_INPUT; \
364 break; \
366 inptr += 4; \
367 ++*irreversible; \
368 continue; \
370 assert (tmp == buf + 3); \
371 inptr += 4; \
373 else if (set == ASCII_set) \
375 /* Almost done, just advance the input pointer. */ \
376 ++inptr; \
378 else \
380 /* That's pretty easy, we have a dedicated functions for this. */ \
381 if (inend - inptr < 2) \
383 result = __GCONV_INCOMPLETE_INPUT; \
384 break; \
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); \
390 else \
392 assert (set == CNS11643_1_set); \
393 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
396 if (ch == 0) \
398 result = __GCONV_INCOMPLETE_INPUT; \
399 break; \
401 else if (ch == __UNKNOWN_10646_CHAR) \
403 if (! ignore_errors_p ()) \
405 result = __GCONV_ILLEGAL_INPUT; \
406 break; \
408 inptr += 2; \
409 ++*irreversible; \
410 continue; \
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
429 #define BODY \
431 uint32_t ch; \
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. */ \
438 if (ch < 0x80) \
440 if (set != ASCII_set) \
442 *outptr++ = SI; \
443 set = ASCII_set; \
444 if (outptr == outend) \
446 result = __GCONV_FULL_OUTPUT; \
447 break; \
451 *outptr++ = ch; \
452 written = 1; \
454 /* At the end of the line we have to clear the `ann' flags since \
455 every line must contain this information again. */ \
456 if (ch == L'\n') \
457 ann = 0; \
459 else \
461 char buf[2]; \
462 int used; \
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); \
468 used = GB2312_set; \
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; \
475 else \
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; \
489 else \
491 char tmpbuf[3]; \
493 switch (0) \
495 default: \
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) \
503 used = GB2312_set; \
504 break; \
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; \
514 break; \
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; \
524 break; \
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]; \
533 switch (tmpbuf[0]) \
535 case 3: \
536 used = CNS11643_3_set; \
537 break; \
538 case 4: \
539 used = CNS11643_4_set; \
540 break; \
541 case 5: \
542 used = CNS11643_5_set; \
543 break; \
544 case 6: \
545 used = CNS11643_6_set; \
546 break; \
547 case 7: \
548 used = CNS11643_7_set; \
549 break; \
550 default: \
551 abort (); \
553 written = 2; \
554 break; \
557 /* XXX Currently GB7590 and GB13132 are not supported. */\
559 /* Even this does not work. Error. */ \
560 used = ASCII_set; \
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. */ \
572 if (set != used) \
574 /* First see whether we announced that we use this \
575 character set. */ \
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; \
583 break; \
586 assert (used >= 1 && used <= 4); \
587 escseq = ")A\0\0)G)E" + (used - 1) * 2; \
588 *outptr++ = ESC; \
589 *outptr++ = '$'; \
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 */ \
600 escseq = "*H"; \
601 *outptr++ = ESC; \
602 *outptr++ = '$'; \
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; \
614 *outptr++ = ESC; \
615 *outptr++ = '$'; \
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; \
627 break; \
629 *outptr++ = SS2_0; \
630 *outptr++ = SS2_1; \
632 else if (used >= CNS11643_3_set && used <= CNS11643_7_set) \
634 if (outptr + 2 > outend) \
636 result = __GCONV_FULL_OUTPUT; \
637 break; \
639 *outptr++ = SS3_0; \
640 *outptr++ = SS3_1; \
642 else \
644 /* We only have to emit something if currently ASCII is \
645 selected. Otherwise we are switching within the \
646 SO charset. */ \
647 if (set == ASCII_set) \
649 if (outptr + 1 > outend) \
651 result = __GCONV_FULL_OUTPUT; \
652 break; \
654 *outptr++ = SO; \
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; \
663 break; \
666 else if (outptr + 2 > outend) \
668 result = __GCONV_FULL_OUTPUT; \
669 break; \
672 *outptr++ = buf[0]; \
673 *outptr++ = buf[1]; \
674 set = used; \
677 /* Now that we wrote the output increment the input pointer. */ \
678 inptr += 4; \
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>