]>
Commit | Line | Data |
---|---|---|
c0d99958 UD |
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. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. */ | |
20 | ||
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" | |
755104ed | 27 | #include "cns11643.h" |
c0d99958 UD |
28 | #include "cns11643l1.h" |
29 | #include "cns11643l2.h" | |
30 | ||
31 | #include <assert.h> | |
32 | ||
33 | /* This makes obvious what everybody knows: 0x1b is the Esc character. */ | |
34 | #define ESC 0x1b | |
35 | ||
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 | |
45 | ||
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 | |
60 | ||
61 | ||
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' | |
71 | ||
72 | ||
73 | /* The COUNT element of the state keeps track of the currently selected | |
74 | character set. The possible values are: */ | |
75 | enum | |
76 | { | |
77 | ASCII_set = 0, | |
78 | GB2312_set, | |
79 | GB12345_set, | |
80 | CNS11643_1_set, | |
81 | ISO_IR_165_set, | |
82 | SO_mask = 7, | |
83 | ||
755104ed UD |
84 | GB7589_set = 1 << 3, |
85 | GB13131_set = 2 << 3, | |
86 | CNS11643_2_set = 3 << 3, | |
87 | SS2_mask = 3 << 3, | |
c0d99958 | 88 | |
755104ed UD |
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, | |
c0d99958 UD |
97 | |
98 | #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask) | |
99 | ||
755104ed UD |
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, | |
c0d99958 | 105 | |
755104ed UD |
106 | GB7589_ann = 1 << 11, |
107 | GB13131_ann = 2 << 11, | |
108 | CNS11643_2_ann = 3 << 11, | |
109 | SS2_ann = 3 << 11, | |
c0d99958 | 110 | |
755104ed UD |
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 | |
c0d99958 UD |
119 | }; |
120 | ||
121 | ||
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 \ | |
655de5fd | 126 | if (data->__statep->__count >> 3 != ASCII_set) \ |
c0d99958 UD |
127 | { \ |
128 | if (FROM_DIRECTION) \ | |
129 | /* It's easy, we don't have to emit anything, we just reset the \ | |
130 | state for the input. */ \ | |
655de5fd | 131 | data->__statep->__count = ASCII_set << 3; \ |
c0d99958 UD |
132 | else \ |
133 | { \ | |
134 | unsigned char *outbuf = data->__outbuf; \ | |
135 | \ | |
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 \ | |
142 | { \ | |
143 | /* Write out the shift sequence. */ \ | |
144 | *outbuf++ = SI; \ | |
145 | if (data->__flags & __GCONV_IS_LAST) \ | |
146 | *irreversible += 1; \ | |
147 | data->__outbuf = outbuf; \ | |
655de5fd | 148 | data->__statep->__count = ASCII_set << 3; \ |
c0d99958 UD |
149 | } \ |
150 | } \ | |
151 | } | |
152 | ||
153 | ||
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 | |
161 | ||
162 | ||
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 \ | |
169 | { \ | |
170 | uint32_t ch = *inptr; \ | |
171 | \ | |
172 | /* This is a 7bit character set, disallow all 8bit characters. */ \ | |
173 | if (ch > 0x7f) \ | |
174 | { \ | |
175 | if (! ignore_errors_p ()) \ | |
176 | { \ | |
177 | result = __GCONV_ILLEGAL_INPUT; \ | |
178 | break; \ | |
179 | } \ | |
180 | ++inptr; \ | |
181 | ++*irreversible; \ | |
182 | continue; \ | |
183 | } \ | |
184 | \ | |
185 | /* Recognize escape sequences. */ \ | |
186 | if (ch == ESC) \ | |
187 | { \ | |
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 | */ \ | |
755104ed | 194 | if (inptr + 2 > inend \ |
c0d99958 | 195 | || (inptr[1] == '$' \ |
755104ed UD |
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)) \ | |
c0d99958 | 202 | { \ |
755104ed | 203 | result = __GCONV_INCOMPLETE_INPUT; \ |
c0d99958 UD |
204 | break; \ |
205 | } \ | |
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')))) \ | |
221 | { \ | |
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; \ | |
253 | } \ | |
254 | } \ | |
255 | else if (ch == SO) \ | |
256 | { \ | |
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) \ | |
264 | { \ | |
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; \ | |
278 | } \ | |
279 | continue; \ | |
280 | } \ | |
281 | else if (ch == SI) \ | |
282 | { \ | |
283 | /* Switch to use ASCII. */ \ | |
284 | ++inptr; \ | |
285 | set = ASCII_set; \ | |
286 | continue; \ | |
287 | } \ | |
288 | \ | |
755104ed | 289 | if (ch == ESC && inptr[1] == SS2_1) \ |
c0d99958 UD |
290 | { \ |
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. */ \ | |
c0d99958 UD |
295 | inptr += 2; \ |
296 | ch = cns11643l2_to_ucs4 (&inptr, 2, 0); \ | |
297 | if (ch == __UNKNOWN_10646_CHAR) \ | |
298 | { \ | |
299 | if (! ignore_errors_p ()) \ | |
300 | { \ | |
301 | inptr -= 2; \ | |
302 | result = __GCONV_ILLEGAL_INPUT; \ | |
303 | break; \ | |
304 | } \ | |
755104ed | 305 | inptr += 2; \ |
c0d99958 UD |
306 | ++*irreversible; \ |
307 | continue; \ | |
308 | } \ | |
309 | } \ | |
755104ed | 310 | /* Note that we can assume here that at least 4 bytes are available if \ |
c0d99958 UD |
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) \ | |
314 | { \ | |
315 | /* This is a character from CNS 11643 plane 3 or higher. \ | |
755104ed UD |
316 | XXX Currently GB7590 and GB13132 are not supported. */ \ |
317 | char buf[3]; \ | |
318 | const char *tmp = buf; \ | |
319 | \ | |
320 | buf[1] = inptr[2]; \ | |
321 | buf[2] = inptr[3]; \ | |
322 | switch (ann & SS3_ann) \ | |
c0d99958 | 323 | { \ |
755104ed UD |
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)) \ | |
327 | { \ | |
328 | buf[0] = 0x2e; \ | |
329 | ch = cns11643_to_ucs4 (&tmp, 3, 0); \ | |
330 | } \ | |
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; \ | |
c0d99958 UD |
337 | break; \ |
338 | } \ | |
c0d99958 UD |
339 | if (ch == __UNKNOWN_10646_CHAR) \ |
340 | { \ | |
341 | if (! ignore_errors_p ()) \ | |
342 | { \ | |
c0d99958 UD |
343 | result = __GCONV_ILLEGAL_INPUT; \ |
344 | break; \ | |
345 | } \ | |
755104ed | 346 | inptr += 4; \ |
c0d99958 UD |
347 | ++*irreversible; \ |
348 | continue; \ | |
349 | } \ | |
755104ed UD |
350 | assert (tmp == buf + 3); \ |
351 | inptr += 4; \ | |
c0d99958 UD |
352 | } \ |
353 | else if (set == ASCII_set) \ | |
354 | { \ | |
355 | /* Almost done, just advance the input pointer. */ \ | |
356 | ++inptr; \ | |
357 | } \ | |
358 | else \ | |
359 | { \ | |
360 | /* That's pretty easy, we have a dedicated functions for this. */ \ | |
361 | if (inend - inptr < 2) \ | |
362 | { \ | |
363 | result = __GCONV_INCOMPLETE_INPUT; \ | |
364 | break; \ | |
365 | } \ | |
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 \ | |
371 | { \ | |
372 | assert (set == CNS11643_1_set); \ | |
373 | ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0); \ | |
374 | } \ | |
375 | \ | |
376 | if (ch == 0) \ | |
377 | { \ | |
755104ed | 378 | result = __GCONV_INCOMPLETE_INPUT; \ |
c0d99958 UD |
379 | break; \ |
380 | } \ | |
381 | else if (ch == __UNKNOWN_10646_CHAR) \ | |
382 | { \ | |
383 | if (! ignore_errors_p ()) \ | |
384 | { \ | |
385 | result = __GCONV_ILLEGAL_INPUT; \ | |
386 | break; \ | |
387 | } \ | |
388 | inptr += 2; \ | |
389 | ++*irreversible; \ | |
390 | continue; \ | |
391 | } \ | |
392 | } \ | |
393 | \ | |
394 | *((uint32_t *) outptr)++ = ch; \ | |
395 | } | |
396 | #define EXTRA_LOOP_DECLS , int *setp | |
655de5fd UD |
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 | |
c0d99958 UD |
400 | #define LOOP_NEED_FLAGS |
401 | #include <iconv/loop.c> | |
402 | ||
403 | ||
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 \ | |
410 | { \ | |
411 | uint32_t ch; \ | |
412 | size_t written = 0; \ | |
413 | \ | |
8c0b7170 | 414 | ch = *((const uint32_t *) inptr); \ |
c0d99958 UD |
415 | \ |
416 | /* First see whether we can write the character using the currently \ | |
417 | selected character set. */ \ | |
418 | if (ch < 0x80) \ | |
419 | { \ | |
420 | if (set != ASCII_set) \ | |
421 | { \ | |
422 | *outptr++ = SI; \ | |
423 | set = ASCII_set; \ | |
424 | if (outptr == outend) \ | |
425 | { \ | |
426 | result = __GCONV_FULL_OUTPUT; \ | |
427 | break; \ | |
428 | } \ | |
429 | } \ | |
430 | \ | |
431 | *outptr++ = ch; \ | |
432 | written = 1; \ | |
433 | \ | |
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; \ | |
438 | } \ | |
439 | else \ | |
440 | { \ | |
441 | char buf[2]; \ | |
442 | int used; \ | |
443 | \ | |
755104ed UD |
444 | if (set == GB2312_set || ((ann & SO_ann) != CNS11643_1_ann \ |
445 | && (ann & SO_ann) != ISO_IR_165_ann)) \ | |
c0d99958 UD |
446 | { \ |
447 | written = ucs4_to_gb2312 (ch, buf, 2); \ | |
448 | used = GB2312_set; \ | |
449 | } \ | |
755104ed | 450 | else if (set == ISO_IR_165_set || (ann & SO_ann) == ISO_IR_165_set) \ |
c0d99958 | 451 | { \ |
755104ed UD |
452 | written = ucs4_to_isoir165 (ch, buf, 2); \ |
453 | used = ISO_IR_165_set; \ | |
c0d99958 UD |
454 | } \ |
455 | else \ | |
456 | { \ | |
457 | written = ucs4_to_cns11643l1 (ch, buf, 2); \ | |
458 | used = CNS11643_1_set; \ | |
459 | } \ | |
460 | \ | |
461 | if (written == __UNKNOWN_10646_CHAR) \ | |
462 | { \ | |
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 \ | |
470 | { \ | |
755104ed | 471 | char tmpbuf[3]; \ |
c0d99958 | 472 | \ |
755104ed | 473 | switch (0) \ |
c0d99958 | 474 | { \ |
755104ed UD |
475 | default: \ |
476 | /* Well, see whether we have to change the SO set. */ \ | |
477 | \ | |
478 | if (used != GB2312_set) \ | |
479 | { \ | |
480 | written = ucs4_to_gb2312 (ch, buf, 2); \ | |
481 | if (written != __UNKNOWN_10646_CHAR) \ | |
482 | { \ | |
483 | used = GB2312_set; \ | |
484 | break; \ | |
485 | } \ | |
486 | } \ | |
487 | \ | |
488 | if (used != ISO_IR_165_set) \ | |
489 | { \ | |
490 | written = ucs4_to_isoir165 (ch, buf, 2); \ | |
491 | if (written != __UNKNOWN_10646_CHAR) \ | |
492 | { \ | |
493 | used = ISO_IR_165_set; \ | |
494 | break; \ | |
495 | } \ | |
496 | } \ | |
497 | \ | |
498 | if (used != CNS11643_1_set) \ | |
499 | { \ | |
500 | written = ucs4_to_cns11643l1 (ch, buf, 2); \ | |
501 | if (written != __UNKNOWN_10646_CHAR) \ | |
502 | { \ | |
503 | used = CNS11643_1_set; \ | |
504 | break; \ | |
505 | } \ | |
506 | } \ | |
507 | \ | |
508 | written = ucs4_to_cns11643 (ch, tmpbuf, 3); \ | |
509 | if (written == 3 && tmpbuf[0] != 1 && tmpbuf[0] != 2) \ | |
510 | { \ | |
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))) \ | |
520 | { \ | |
521 | used = CNS11643_3_set; \ | |
522 | break; \ | |
523 | } \ | |
524 | } \ | |
525 | \ | |
c0d99958 | 526 | /* Even this does not work. Error. */ \ |
755104ed UD |
527 | used = ASCII_set; \ |
528 | } \ | |
529 | if (used == ASCII_set) \ | |
530 | { \ | |
c0d99958 UD |
531 | STANDARD_ERR_HANDLER (4); \ |
532 | } \ | |
533 | } \ | |
534 | } \ | |
535 | assert (written == 2); \ | |
536 | \ | |
537 | /* See whether we have to emit an escape sequence. */ \ | |
538 | if (set != used) \ | |
539 | { \ | |
540 | /* First see whether we announced that we use this \ | |
541 | character set. */ \ | |
755104ed | 542 | if ((used & SO_mask) != 0 && (ann & SO_ann) != (used << 8)) \ |
c0d99958 UD |
543 | { \ |
544 | const char *escseq; \ | |
545 | \ | |
546 | if (outptr + 4 > outend) \ | |
547 | { \ | |
548 | result = __GCONV_FULL_OUTPUT; \ | |
549 | break; \ | |
550 | } \ | |
551 | \ | |
552 | assert (used >= 1 && used <= 4); \ | |
755104ed UD |
553 | escseq = ")A\0\0)G)E" + (used - 1) * 2; \ |
554 | *outptr++ = ESC; \ | |
555 | *outptr++ = '$'; \ | |
556 | *outptr++ = *escseq++; \ | |
557 | *outptr++ = *escseq++; \ | |
558 | \ | |
559 | ann = (ann & ~SO_ann) | (used << 8); \ | |
560 | } \ | |
561 | else if ((used & SS2_mask) != 0 && (ann & SS2_ann) != (used << 8))\ | |
562 | { \ | |
563 | const char *escseq; \ | |
564 | \ | |
565 | assert (used == CNS11643_2_set); /* XXX */ \ | |
566 | escseq = "*H"; \ | |
567 | *outptr++ = ESC; \ | |
568 | *outptr++ = '$'; \ | |
c0d99958 UD |
569 | *outptr++ = *escseq++; \ |
570 | *outptr++ = *escseq++; \ | |
755104ed UD |
571 | \ |
572 | ann = (ann & ~SS2_ann) | (used << 8); \ | |
573 | } \ | |
574 | else if ((used & SS3_mask) != 0 && (ann & SS3_ann) != (used << 8))\ | |
575 | { \ | |
576 | const char *escseq; \ | |
577 | \ | |
578 | assert ((used >> 5) >= 3 && (used >> 5) <= 7); \ | |
579 | escseq = "+I+J+K+L+M" + ((used >> 5) - 3) * 2; \ | |
580 | *outptr++ = ESC; \ | |
581 | *outptr++ = '$'; \ | |
c0d99958 UD |
582 | *outptr++ = *escseq++; \ |
583 | *outptr++ = *escseq++; \ | |
584 | \ | |
755104ed | 585 | ann = (ann & ~SS3_ann) | (used << 8); \ |
c0d99958 UD |
586 | } \ |
587 | \ | |
588 | if (used == CNS11643_2_set) \ | |
589 | { \ | |
590 | if (outptr + 2 > outend) \ | |
591 | { \ | |
592 | result = __GCONV_FULL_OUTPUT; \ | |
593 | break; \ | |
594 | } \ | |
595 | *outptr++ = SS2_0; \ | |
596 | *outptr++ = SS2_1; \ | |
597 | } \ | |
755104ed UD |
598 | else if (used >= CNS11643_3_set && used <= CNS11643_7_set) \ |
599 | { \ | |
600 | if (outptr + 2 > outend) \ | |
601 | { \ | |
602 | result = __GCONV_FULL_OUTPUT; \ | |
603 | break; \ | |
604 | } \ | |
605 | *outptr++ = SS3_0; \ | |
606 | *outptr++ = SS3_1; \ | |
607 | } \ | |
c0d99958 UD |
608 | else \ |
609 | { \ | |
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) \ | |
614 | { \ | |
615 | if (outptr + 1 > outend) \ | |
616 | { \ | |
617 | result = __GCONV_FULL_OUTPUT; \ | |
618 | break; \ | |
619 | } \ | |
620 | *outptr++ = SO; \ | |
621 | } \ | |
622 | } \ | |
623 | \ | |
624 | /* Always test the length here since we have used up all the \ | |
625 | guaranteed output buffer slots. */ \ | |
626 | if (outptr + 2 > outend) \ | |
627 | { \ | |
628 | result = __GCONV_FULL_OUTPUT; \ | |
629 | break; \ | |
630 | } \ | |
631 | } \ | |
632 | else if (outptr + 2 > outend) \ | |
633 | { \ | |
634 | result = __GCONV_FULL_OUTPUT; \ | |
635 | break; \ | |
636 | } \ | |
637 | \ | |
638 | *outptr++ = buf[0]; \ | |
639 | *outptr++ = buf[1]; \ | |
755104ed | 640 | set = used; \ |
c0d99958 UD |
641 | } \ |
642 | \ | |
643 | /* Now that we wrote the output increment the input pointer. */ \ | |
644 | inptr += 4; \ | |
645 | } | |
646 | #define EXTRA_LOOP_DECLS , int *setp | |
655de5fd UD |
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 | |
c0d99958 UD |
650 | #define LOOP_NEED_FLAGS |
651 | #include <iconv/loop.c> | |
652 | ||
653 | ||
654 | /* Now define the toplevel functions. */ | |
655 | #include <iconv/skeleton.c> |