1 /* Conversion module for ISO-2022-CN.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
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 "cns11643l1.h"
27 #include "cns11643l2.h"
31 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
34 /* We have single-byte shift-in and shift-out sequences, and the single
35 shift sequence SS2 which replaces the SS2 designation for the next
42 /* Definitions used in the body of the `gconv' function. */
43 #define CHARSET_NAME "ISO-2022-CN//"
46 #define FROM_LOOP from_iso2022cn_loop
47 #define TO_LOOP to_iso2022cn_loop
48 #define MIN_NEEDED_FROM 1
49 #define MAX_NEEDED_FROM 4
50 #define MIN_NEEDED_TO 4
51 #define MAX_NEEDED_TO 4
52 #define PREPARE_LOOP \
54 int *setp = &data->__statep->__count;
55 #define EXTRA_LOOP_ARGS , setp
58 /* The COUNT element of the state keeps track of the currently selected
59 character set. The possible values are: */
66 CURRENT_SEL_MASK
= 24,
70 CURRENT_ANN_MASK
= 224
74 /* Since this is a stateful encoding we have to provide code which resets
75 the output state to the initial state. This has to be done during the
77 #define EMIT_SHIFT_TO_INIT \
78 if (data->__statep->__count != ASCII_set) \
81 /* It's easy, we don't have to emit anything, we just reset the \
82 state for the input. */ \
83 data->__statep->__count = ASCII_set; \
86 unsigned char *outbuf = data->__outbuf; \
88 /* We are not in the initial state. To switch back we have \
90 if (__builtin_expect (outbuf == data->__outbufend, 0)) \
91 /* We don't have enough room in the output buffer. */ \
92 status = __GCONV_FULL_OUTPUT; \
95 /* Write out the shift sequence. */ \
97 data->__outbuf = outbuf; \
98 data->__statep->__count = ASCII_set; \
104 /* Since we might have to reset input pointer we must be able to save
105 and retore the state. */
106 #define SAVE_RESET_STATE(Save) \
113 /* First define the conversion function from ISO-2022-CN to UCS4. */
114 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
115 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
116 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
117 #define LOOPFCT FROM_LOOP
120 uint32_t ch = *inptr; \
122 /* This is a 7bit character set, disallow all 8bit characters. */ \
123 if (__builtin_expect (ch, 0) > 0x7f) \
125 if (! ignore_errors_p ()) \
127 result = __GCONV_ILLEGAL_INPUT; \
136 /* Recognize escape sequences. */ \
137 if (__builtin_expect (ch, 0) == ESC) \
139 /* There are two kinds of escape sequences we have to handle: \
140 - those announcing the use of GB and CNS characters on the \
141 line; we can simply ignore them \
142 - the initial byte of the SS2 sequence. \
144 if (__builtin_expect (inptr + 2 > inend, 0) \
145 || (inptr[1] == '$' \
146 && (__builtin_expect (inptr + 3 > inend, 0) \
147 || (inptr[2] == ')' \
148 && __builtin_expect (inptr + 4 > inend, 0)) \
149 || (inptr[2] == '*' \
150 && __builtin_expect (inptr + 4 > inend, 0)))) \
151 || (inptr[1] == SS2_1 \
152 && __builtin_expect (inptr + 4 > inend, 0))) \
154 result = __GCONV_INCOMPLETE_INPUT; \
157 if (inptr[1] == '$' \
158 && ((inptr[2] == ')' && (inptr[3] == 'A' || inptr[3] == 'G')) \
159 || (inptr[2] == '*' && inptr[3] == 'H'))) \
161 /* OK, we accept those character sets. */ \
162 if (inptr[3] == 'A') \
164 else if (inptr[3] == 'G') \
165 ann = CNS11643_1_ann; \
170 else if (__builtin_expect (ch, 0) == SO) \
172 /* Switch to use GB2312 or CNS 11643 plane 1, depending on which \
173 S0 designation came last. The only problem is what to do with \
174 faulty input files where no designator came. \
175 XXX For now I'll default to use GB2312. If this is not the \
176 best behaviour (e.g., we should flag an error) let me know. */ \
178 set = ann == CNS11643_1_ann ? CNS11643_1_set : GB2312_set; \
181 else if (__builtin_expect (ch, 0) == SI) \
183 /* Switch to use ASCII. */ \
189 if (__builtin_expect (ch, 0) == ESC && inptr[1] == SS2_1) \
191 /* This is a character from CNS 11643 plane 2. \
192 XXX We could test here whether the use of this character \
193 set was announced. */ \
195 ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \
196 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
198 if (! ignore_errors_p ()) \
200 /* This is an illegal character. */ \
202 result = __GCONV_ILLEGAL_INPUT; \
210 else if (set == ASCII_set) \
212 /* Almost done, just advance the input pointer. */ \
217 /* That's pretty easy, we have a dedicated functions for this. */ \
218 if (set == GB2312_set) \
219 ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0); \
222 assert (set == CNS11643_1_set); \
223 ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \
226 if (__builtin_expect (ch, 1) == 0) \
228 result = __GCONV_INCOMPLETE_INPUT; \
231 else if (__builtin_expect (ch, 1) == __UNKNOWN_10646_CHAR) \
233 if (! ignore_errors_p ()) \
235 /* This is an illegal character. */ \
236 result = __GCONV_ILLEGAL_INPUT; \
246 put32 (outptr, ch); \
249 #define LOOP_NEED_FLAGS
250 #define EXTRA_LOOP_DECLS , int *setp
251 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
252 int ann = *setp & CURRENT_ANN_MASK
253 #define UPDATE_PARAMS *setp = set | ann
254 #include <iconv/loop.c>
257 /* Next, define the other direction. */
258 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
259 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
260 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
261 #define LOOPFCT TO_LOOP
264 uint32_t ch = get32 (inptr); \
266 /* First see whether we can write the character using the currently \
267 selected character set. */ \
270 if (set != ASCII_set) \
274 if (__builtin_expect (outptr == outend, 0)) \
276 result = __GCONV_FULL_OUTPUT; \
283 /* At the end of the line we have to clear the `ann' flags since \
284 every line must contain this information again. */ \
292 size_t written = 0; \
294 if (set == GB2312_set || (ann & CNS11643_1_ann) == 0) \
296 written = ucs4_to_gb2312 (ch, buf, 2); \
301 written = ucs4_to_cns11643l1 (ch, buf, 2); \
302 used = CNS11643_1_set; \
305 if (written == __UNKNOWN_10646_CHAR) \
307 /* Cannot convert it using the currently selected SO set. \
308 Next try the SS2 set. */ \
309 written = ucs4_to_cns11643l2 (ch, buf, 2); \
310 if (written != __UNKNOWN_10646_CHAR) \
311 /* Yep, that worked. */ \
312 used = CNS11643_2_set; \
315 /* Well, see whether we have to change the SO set. */ \
316 if (used == GB2312_set) \
317 written = ucs4_to_cns11643l1 (ch, buf, 2); \
319 written = ucs4_to_gb2312 (ch, buf, 2); \
321 if (__builtin_expect (written, 0) != __UNKNOWN_10646_CHAR) \
322 /* Oh well, then switch SO. */ \
323 used = GB2312_set + CNS11643_1_set - used; \
326 /* Even this does not work. Error. */ \
327 STANDARD_ERR_HANDLER (4); \
331 assert (written == 2); \
333 /* See whether we have to emit an escape sequence. */ \
336 /* First see whether we announced that we use this \
338 if ((ann & (16 << (used >> 3))) == 0) \
340 const char *escseq; \
342 if (__builtin_expect (outptr + 4 > outend, 0)) \
344 result = __GCONV_FULL_OUTPUT; \
348 assert ((used >> 3) >= 1 && (used >> 3) <= 3); \
349 escseq = ")A)G*H" + ((used >> 3) - 1) * 2; \
352 *outptr++ = *escseq++; \
353 *outptr++ = *escseq++; \
355 if (used == GB2312_set) \
356 ann = (ann & CNS11643_2_ann) | GB2312_ann; \
357 else if (used == CNS11643_1_set) \
358 ann = (ann & CNS11643_2_ann) | CNS11643_1_ann; \
360 ann |= CNS11643_2_ann; \
363 if (used == CNS11643_2_set) \
365 if (__builtin_expect (outptr + 2 > outend, 0)) \
367 result = __GCONV_FULL_OUTPUT; \
375 /* We only have to emit something is currently ASCII is \
376 selected. Otherwise we are switching within the \
378 if (set == ASCII_set) \
380 if (__builtin_expect (outptr + 1 > outend, 0)) \
382 result = __GCONV_FULL_OUTPUT; \
389 /* Always test the length here since we have used up all the \
390 guaranteed output buffer slots. */ \
391 if (__builtin_expect (outptr + 2 > outend, 0)) \
393 result = __GCONV_FULL_OUTPUT; \
397 else if (__builtin_expect (outptr + 2 > outend, 0)) \
399 result = __GCONV_FULL_OUTPUT; \
403 *outptr++ = buf[0]; \
404 *outptr++ = buf[1]; \
408 /* Now that we wrote the output increment the input pointer. */ \
411 #define LOOP_NEED_FLAGS
412 #define EXTRA_LOOP_DECLS , int *setp
413 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
414 int ann = *setp & CURRENT_ANN_MASK
415 #define UPDATE_PARAMS *setp = set | ann
416 #include <iconv/loop.c>
419 /* Now define the toplevel functions. */
420 #include <iconv/skeleton.c>