Update.
[glibc.git] / iconvdata / iso-2022-cn-ext.c
blobc1bd7ac1f0e566330d2a4b892eeca6b363ee18a4
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 "cns11643l1.h"
28 #include "cns11643l2.h"
30 #include <assert.h>
32 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
33 #define ESC 0x1b
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. */
38 #define SI 0x0f
39 #define SO 0x0e
40 #define SS2_0 ESC
41 #define SS2_1 0x4e
42 #define SS3_0 ESC
43 #define SS3_1 0x4f
45 /* Definitions used in the body of the `gconv' function. */
46 #define CHARSET_NAME "ISO-2022-CN-EXT//"
47 #define DEFINE_INIT 1
48 #define DEFINE_FINI 1
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 \
56 int save_set; \
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. */
65 #define X12345 '\0'
66 #define X7589 '\0'
67 #define X13131 '\0'
68 #define X7590 '\0'
69 #define X13132 '\0'
72 /* The COUNT element of the state keeps track of the currently selected
73 character set. The possible values are: */
74 enum
76 ASCII_set = 0,
77 GB2312_set,
78 GB12345_set,
79 CNS11643_1_set,
80 ISO_IR_165_set,
81 SO_mask = 7,
83 GB7589_set = 8,
84 GB13131_set = 16,
85 CNS11643_2_set = 24,
86 SS2_mask = 24,
88 GB7590_set = 0,
89 GB13132_set = 32,
90 CNS11643_3_set = 64,
91 CNS11643_4_set = 96,
92 CNS11643_5_set = 128,
93 CNS11643_6_set = 160,
94 CNS11643_7_set = 192,
95 SS3_mask = 224,
97 #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask)
99 GB2312_ann = 256,
100 GB12345_ann = 512,
101 CNS11643_1_ann = 768,
102 ISO_IR_165_ann = 1024,
103 SO_ann = 1792,
105 GB7589_ann = 2048,
106 GB13131_ann = 4096,
107 CNS11643_2_ann = 6144,
108 SS2_ann = 6144,
110 GB7590_ann = 8192,
111 GB13132_ann = 16384,
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,
117 SS3_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
123 flushing. */
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; \
131 else \
133 unsigned char *outbuf = data->__outbuf; \
135 /* We are not in the initial state. To switch back we have \
136 to emit `SI'. */ \
137 if (outbuf == data->__outbufend) \
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->__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) \
156 if (Save) \
157 save_set = *setp; \
158 else \
159 *setp = save_set
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
167 #define BODY \
169 uint32_t ch = *inptr; \
171 /* This is a 7bit character set, disallow all 8bit characters. */ \
172 if (ch > 0x7f) \
174 if (! ignore_errors_p ()) \
176 result = __GCONV_ILLEGAL_INPUT; \
177 break; \
179 ++inptr; \
180 ++*irreversible; \
181 continue; \
184 /* Recognize escape sequences. */ \
185 if (ch == ESC) \
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. \
192 */ \
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; \
203 break; \
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; \
250 inptr += 4; \
251 continue; \
254 else if (ch == SO) \
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. */ \
261 ++inptr; \
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 default: \
274 assert ((ann & SO_ann) == ISO_IR_165_ann); \
275 set = ISO_IR_165_set; \
276 break; \
278 continue; \
280 else if (ch == SI) \
282 /* Switch to use ASCII. */ \
283 ++inptr; \
284 set = ASCII_set; \
285 continue; \
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 \
292 set was announced. \
293 XXX Current GB7589 and GB13131 are not supported. */ \
294 if (inend - inptr < 4) \
296 result = __GCONV_INCOMPLETE_INPUT; \
297 break; \
299 inptr += 2; \
300 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
301 if (ch == __UNKNOWN_10646_CHAR) \
303 if (! ignore_errors_p ()) \
305 inptr -= 2; \
306 result = __GCONV_ILLEGAL_INPUT; \
307 break; \
309 ++*irreversible; \
310 continue; \
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 \
315 true. */ \
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; \
323 break; \
325 inptr += 2; \
326 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
327 if (ch == __UNKNOWN_10646_CHAR) \
329 if (! ignore_errors_p ()) \
331 inptr -= 2; \
332 result = __GCONV_ILLEGAL_INPUT; \
333 break; \
335 ++*irreversible; \
336 continue; \
339 else if (set == ASCII_set) \
341 /* Almost done, just advance the input pointer. */ \
342 ++inptr; \
344 else \
346 /* That's pretty easy, we have a dedicated functions for this. */ \
347 if (inend - inptr < 2) \
349 result = __GCONV_INCOMPLETE_INPUT; \
350 break; \
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); \
356 else \
358 assert (set == CNS11643_1_set); \
359 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
362 if (ch == 0) \
364 result = __GCONV_EMPTY_INPUT; \
365 break; \
367 else if (ch == __UNKNOWN_10646_CHAR) \
369 if (! ignore_errors_p ()) \
371 result = __GCONV_ILLEGAL_INPUT; \
372 break; \
374 inptr += 2; \
375 ++*irreversible; \
376 continue; \
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
395 #define BODY \
397 uint32_t ch; \
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. */ \
404 if (ch < 0x80) \
406 if (set != ASCII_set) \
408 *outptr++ = SI; \
409 set = ASCII_set; \
410 if (outptr == outend) \
412 result = __GCONV_FULL_OUTPUT; \
413 break; \
417 *outptr++ = ch; \
418 written = 1; \
420 /* At the end of the line we have to clear the `ann' flags since \
421 every line must contain this information again. */ \
422 if (ch == L'\n') \
423 ann = 0; \
425 else \
427 char buf[2]; \
428 int used; \
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); \
434 used = GB2312_set; \
436 else if (set == ISO_IR_165_set || (ann & ISO_IR_165_set) != 0) \
438 written = ucs4_to_gb2312 (ch, buf, 2); \
439 used = GB2312_set; \
441 else \
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; \
455 else \
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) \
462 used = GB2312_set; \
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. */ \
487 if (set != used) \
489 /* First see whether we announced that we use this \
490 character set. */ \
491 if ((ann & (2 << used)) == 0) \
493 const char *escseq; \
495 if (outptr + 4 > outend) \
497 result = __GCONV_FULL_OUTPUT; \
498 break; \
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; \
512 else \
513 ann |= CNS11643_2_ann; \
516 if (used == CNS11643_2_set) \
518 if (outptr + 2 > outend) \
520 result = __GCONV_FULL_OUTPUT; \
521 break; \
523 *outptr++ = SS2_0; \
524 *outptr++ = SS2_1; \
526 else \
528 /* We only have to emit something if currently ASCII is \
529 selected. Otherwise we are switching within the \
530 SO charset. */ \
531 if (set == ASCII_set) \
533 if (outptr + 1 > outend) \
535 result = __GCONV_FULL_OUTPUT; \
536 break; \
538 *outptr++ = SO; \
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; \
547 break; \
550 else if (outptr + 2 > outend) \
552 result = __GCONV_FULL_OUTPUT; \
553 break; \
556 *outptr++ = buf[0]; \
557 *outptr++ = buf[1]; \
560 /* Now that we wrote the output increment the input pointer. */ \
561 inptr += 4; \
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>