]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconvdata/iso-2022-jp.c
In conversion to ISO-2022-JP, add two missing buffer size checks.
[thirdparty/glibc.git] / iconvdata / iso-2022-jp.c
1 /* Conversion module for ISO-2022-JP.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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 <stdlib.h>
25 #include <string.h>
26 #include "jis0201.h"
27 #include "jis0208.h"
28 #include "jis0212.h"
29 #include "gb2312.h"
30 #include "ksc5601.h"
31
32 struct gap
33 {
34 uint16_t start;
35 uint16_t end;
36 int32_t idx;
37 };
38
39 #include "iso8859-7jp.h"
40
41 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
42 #define ESC 0x1b
43
44 /* We provide our own initialization and destructor function. */
45 #define DEFINE_INIT 0
46 #define DEFINE_FINI 0
47
48 /* Definitions used in the body of the `gconv' function. */
49 #define FROM_LOOP from_iso2022jp_loop
50 #define TO_LOOP to_iso2022jp_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 FROM_DIRECTION (dir == from_iso2022jp)
56 #define PREPARE_LOOP \
57 enum direction dir = ((struct iso2022jp_data *) step->__data)->dir; \
58 enum variant var = ((struct iso2022jp_data *) step->__data)->var; \
59 int save_set; \
60 int *setp = &data->__statep->__count;
61 #define EXTRA_LOOP_ARGS , var, setp
62
63
64 /* Direction of the transformation. */
65 enum direction
66 {
67 illegal_dir,
68 to_iso2022jp,
69 from_iso2022jp
70 };
71
72 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
73 enum variant
74 {
75 illegal_var,
76 iso2022jp,
77 iso2022jp2
78 };
79
80
81 struct iso2022jp_data
82 {
83 enum direction dir;
84 enum variant var;
85 };
86
87
88 /* The COUNT element of the state keeps track of the currently selected
89 character set. The possible values are: */
90 enum
91 {
92 ASCII_set = 0,
93 JISX0208_1978_set = 8,
94 JISX0208_1983_set = 16,
95 JISX0201_Roman_set = 24,
96 JISX0201_Kana_set = 32,
97 GB2312_set = 40,
98 KSC5601_set = 48,
99 JISX0212_set = 56,
100 CURRENT_SEL_MASK = 56
101 };
102
103 /* The second value stored is the designation of the G2 set. The following
104 values are possible: */
105 enum
106 {
107 UNSPECIFIED_set = 0,
108 ISO88591_set = 64,
109 ISO88597_set = 128,
110 CURRENT_ASSIGN_MASK = 192
111 };
112
113
114 int
115 gconv_init (struct __gconv_step *step)
116 {
117 /* Determine which direction. */
118 struct iso2022jp_data *new_data;
119 enum direction dir = illegal_dir;
120 enum variant var = illegal_var;
121 int result;
122
123 if (__strcasecmp (step->__from_name, "ISO-2022-JP//") == 0)
124 {
125 dir = from_iso2022jp;
126 var = iso2022jp;
127 }
128 else if (__strcasecmp (step->__to_name, "ISO-2022-JP//") == 0)
129 {
130 dir = to_iso2022jp;
131 var = iso2022jp;
132 }
133 else if (__strcasecmp (step->__from_name, "ISO-2022-JP-2//") == 0)
134 {
135 dir = from_iso2022jp;
136 var = iso2022jp2;
137 }
138 else if (__strcasecmp (step->__to_name, "ISO-2022-JP-2//") == 0)
139 {
140 dir = to_iso2022jp;
141 var = iso2022jp2;
142 }
143
144 result = __GCONV_NOCONV;
145 if (__builtin_expect (dir, from_iso2022jp) != illegal_dir)
146 {
147 new_data
148 = (struct iso2022jp_data *) malloc (sizeof (struct iso2022jp_data));
149
150 result = __GCONV_NOMEM;
151 if (new_data != NULL)
152 {
153 new_data->dir = dir;
154 new_data->var = var;
155 step->__data = new_data;
156
157 if (dir == from_iso2022jp)
158 {
159 step->__min_needed_from = MIN_NEEDED_FROM;
160 step->__max_needed_from = MAX_NEEDED_FROM;
161 step->__min_needed_to = MIN_NEEDED_TO;
162 step->__max_needed_to = MAX_NEEDED_TO;
163 }
164 else
165 {
166 step->__min_needed_from = MIN_NEEDED_TO;
167 step->__max_needed_from = MAX_NEEDED_TO;
168 step->__min_needed_to = MIN_NEEDED_FROM;
169 step->__max_needed_to = MAX_NEEDED_FROM + 2;
170 }
171
172 /* Yes, this is a stateful encoding. */
173 step->__stateful = 1;
174
175 result = __GCONV_OK;
176 }
177 }
178
179 return result;
180 }
181
182
183 void
184 gconv_end (struct __gconv_step *data)
185 {
186 free (data->__data);
187 }
188
189
190 /* Since this is a stateful encoding we have to provide code which resets
191 the output state to the initial state. This has to be done during the
192 flushing. */
193 #define EMIT_SHIFT_TO_INIT \
194 if ((data->__statep->__count & ~7) != ASCII_set) \
195 { \
196 enum direction dir = ((struct iso2022jp_data *) step->__data)->dir; \
197 \
198 if (dir == from_iso2022jp) \
199 { \
200 /* It's easy, we don't have to emit anything, we just reset the \
201 state for the input. Note that this also clears the G2 \
202 designation. */ \
203 data->__statep->__count &= 7; \
204 data->__statep->__count |= ASCII_set; \
205 } \
206 else \
207 { \
208 unsigned char *outbuf = data->__outbuf; \
209 \
210 /* We are not in the initial state. To switch back we have \
211 to emit the sequence `Esc ( B'. */ \
212 if (__builtin_expect (outbuf + 3 > data->__outbufend, 0)) \
213 /* We don't have enough room in the output buffer. */ \
214 status = __GCONV_FULL_OUTPUT; \
215 else \
216 { \
217 /* Write out the shift sequence. */ \
218 *outbuf++ = ESC; \
219 *outbuf++ = '('; \
220 *outbuf++ = 'B'; \
221 data->__outbuf = outbuf; \
222 /* Note that this also clears the G2 designation. */ \
223 data->__statep->__count &= ~7; \
224 data->__statep->__count |= ASCII_set; \
225 } \
226 } \
227 }
228
229
230 /* Since we might have to reset input pointer we must be able to save
231 and retore the state. */
232 #define SAVE_RESET_STATE(Save) \
233 if (Save) \
234 save_set = *setp; \
235 else \
236 *setp = save_set
237
238
239 /* First define the conversion function from ISO-2022-JP to UCS4. */
240 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
241 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
242 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
243 #define LOOPFCT FROM_LOOP
244 #define BODY \
245 { \
246 uint32_t ch = *inptr; \
247 \
248 /* Recognize escape sequences. */ \
249 if (__builtin_expect (ch, 0) == ESC) \
250 { \
251 /* We now must be prepared to read two to three more \
252 chracters. If we have a match in the first character but \
253 then the input buffer ends we terminate with an error since \
254 we must not risk missing an escape sequence just because it \
255 is not entirely in the current input buffer. */ \
256 if (__builtin_expect (inptr + 2 >= inend, 0) \
257 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
258 && __builtin_expect (inptr + 3 >= inend, 0))) \
259 { \
260 /* Not enough input available. */ \
261 result = __GCONV_EMPTY_INPUT; \
262 break; \
263 } \
264 \
265 if (inptr[1] == '(') \
266 { \
267 if (inptr[2] == 'B') \
268 { \
269 /* ASCII selected. */ \
270 set = ASCII_set; \
271 inptr += 3; \
272 continue; \
273 } \
274 else if (inptr[2] == 'J') \
275 { \
276 /* JIS X 0201 selected. */ \
277 set = JISX0201_Roman_set; \
278 inptr += 3; \
279 continue; \
280 } \
281 else if (var == iso2022jp2 && inptr[2] == 'I') \
282 { \
283 /* JIS X 0201 selected. */ \
284 set = JISX0201_Kana_set; \
285 inptr += 3; \
286 continue; \
287 } \
288 } \
289 else if (inptr[1] == '$') \
290 { \
291 if (inptr[2] == '@') \
292 { \
293 /* JIS X 0208-1978 selected. */ \
294 set = JISX0208_1978_set; \
295 inptr += 3; \
296 continue; \
297 } \
298 else if (inptr[2] == 'B') \
299 { \
300 /* JIS X 0208-1983 selected. */ \
301 set = JISX0208_1983_set; \
302 inptr += 3; \
303 continue; \
304 } \
305 else if (var == iso2022jp2) \
306 { \
307 if (inptr[2] == 'A') \
308 { \
309 /* GB 2312-1980 selected. */ \
310 set = GB2312_set; \
311 inptr += 3; \
312 continue; \
313 } \
314 else if (inptr[2] == '(') \
315 { \
316 if (inptr[3] == 'C') \
317 { \
318 /* KSC 5601-1987 selected. */ \
319 set = KSC5601_set; \
320 inptr += 4; \
321 continue; \
322 } \
323 else if (inptr[3] == 'D') \
324 { \
325 /* JIS X 0212-1990 selected. */ \
326 set = JISX0212_set; \
327 inptr += 4; \
328 continue; \
329 } \
330 } \
331 } \
332 } \
333 else if (var == iso2022jp2 && inptr[1] == '.') \
334 { \
335 if (inptr[2] == 'A') \
336 { \
337 /* ISO 8859-1-GR selected. */ \
338 set2 = ISO88591_set; \
339 inptr += 3; \
340 continue; \
341 } \
342 else if (inptr[2] == 'F') \
343 { \
344 /* ISO 8859-7-GR selected. */ \
345 set2 = ISO88597_set; \
346 inptr += 3; \
347 continue; \
348 } \
349 } \
350 } \
351 \
352 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
353 { \
354 if (set2 == ISO88591_set) \
355 { \
356 ch = inptr[2] | 0x80; \
357 inptr += 3; \
358 } \
359 else if (__builtin_expect (set2, ISO88597_set) == ISO88597_set) \
360 { \
361 /* We use the table from the ISO 8859-7 module. */ \
362 if (inptr[2] < 0x20 || inptr[2] > 0x80) \
363 { \
364 if (! ignore_errors_p ()) \
365 { \
366 result = __GCONV_ILLEGAL_INPUT; \
367 break; \
368 } \
369 \
370 ++inptr; \
371 ++*irreversible; \
372 continue; \
373 } \
374 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
375 if (ch == 0) \
376 { \
377 if (! ignore_errors_p ()) \
378 { \
379 result = __GCONV_ILLEGAL_INPUT; \
380 break; \
381 } \
382 \
383 inptr += 3; \
384 ++*irreversible; \
385 continue; \
386 } \
387 inptr += 3; \
388 } \
389 else \
390 { \
391 if (! ignore_errors_p ()) \
392 { \
393 result = __GCONV_ILLEGAL_INPUT; \
394 break; \
395 } \
396 \
397 ++inptr; \
398 ++*irreversible; \
399 continue; \
400 } \
401 } \
402 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
403 /* Almost done, just advance the input pointer. */ \
404 ++inptr; \
405 else if (set == JISX0201_Roman_set) \
406 { \
407 /* Use the JIS X 0201 table. */ \
408 ch = jisx0201_to_ucs4 (ch); \
409 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
410 { \
411 if (! ignore_errors_p ()) \
412 { \
413 result = __GCONV_ILLEGAL_INPUT; \
414 break; \
415 } \
416 \
417 ++inptr; \
418 ++*irreversible; \
419 continue; \
420 } \
421 ++inptr; \
422 } \
423 else if (set == JISX0201_Kana_set) \
424 { \
425 /* Use the JIS X 0201 table. */ \
426 ch = jisx0201_to_ucs4 (ch + 0x80); \
427 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
428 { \
429 if (! ignore_errors_p ()) \
430 { \
431 result = __GCONV_ILLEGAL_INPUT; \
432 break; \
433 } \
434 \
435 ++inptr; \
436 ++*irreversible; \
437 continue; \
438 } \
439 ++inptr; \
440 } \
441 else \
442 { \
443 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
444 /* XXX I don't have the tables for these two old variants of \
445 JIS X 0208. Therefore I'm using the tables for JIS X \
446 0208-1990. If somebody has problems with this please \
447 provide the appropriate tables. */ \
448 ch = jisx0208_to_ucs4 (&inptr, inend - inptr, 0); \
449 else if (set == JISX0212_set) \
450 /* Use the JIS X 0212 table. */ \
451 ch = jisx0212_to_ucs4 (&inptr, inend - inptr, 0); \
452 else if (set == GB2312_set) \
453 /* Use the GB 2312 table. */ \
454 ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0); \
455 else \
456 { \
457 assert (set == KSC5601_set); \
458 \
459 /* Use the KSC 5601 table. */ \
460 ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0); \
461 } \
462 \
463 if (__builtin_expect (ch, 1) == 0) \
464 { \
465 result = __GCONV_EMPTY_INPUT; \
466 break; \
467 } \
468 else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
469 { \
470 if (! ignore_errors_p ()) \
471 { \
472 result = __GCONV_ILLEGAL_INPUT; \
473 break; \
474 } \
475 \
476 ++inptr; \
477 ++*irreversible; \
478 continue; \
479 } \
480 } \
481 \
482 put32 (outptr, ch); \
483 outptr += 4; \
484 }
485 #define LOOP_NEED_FLAGS
486 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
487 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
488 int set2 = *setp & CURRENT_ASSIGN_MASK
489 #define UPDATE_PARAMS *setp = set | set2
490 #include <iconv/loop.c>
491
492
493 /* Next, define the other direction. */
494 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
495 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
496 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
497 #define LOOPFCT TO_LOOP
498 #define BODY \
499 { \
500 uint32_t ch; \
501 size_t written = 0; \
502 \
503 ch = get32 (inptr); \
504 \
505 /* First see whether we can write the character using the currently \
506 selected character set. */ \
507 if (set == ASCII_set) \
508 { \
509 /* Please note that the NUL byte is *not* matched if we are not \
510 currently using the ASCII charset. This is because we must \
511 switch to the initial state whenever a NUL byte is written. */ \
512 if (ch <= 0x7f) \
513 { \
514 *outptr++ = ch; \
515 written = 1; \
516 } \
517 /* At the beginning of a line, G2 designation is cleared. */ \
518 if (var == iso2022jp2 && ch == 0x0a) \
519 set2 = UNSPECIFIED_set; \
520 } \
521 /* ISO-2022-JP recommends to encode the newline character always in \
522 ASCII since this allows a context-free interpretation of the \
523 characters at the beginning of the next line. Otherwise it would \
524 have to be known whether the last line ended using ASCII or \
525 JIS X 0201. */ \
526 else if (set == JISX0201_Roman_set) \
527 { \
528 unsigned char buf[2]; \
529 written = ucs4_to_jisx0201 (ch, buf); \
530 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0x20 \
531 && buf[0] < 0x80) \
532 { \
533 *outptr++ = buf[0]; \
534 written = 1; \
535 } \
536 else \
537 written = __UNKNOWN_10646_CHAR; \
538 } \
539 else if (set == JISX0201_Kana_set) \
540 { \
541 unsigned char buf[2]; \
542 written = ucs4_to_jisx0201 (ch, buf); \
543 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0xa0 \
544 && buf[0] < 0xe0) \
545 { \
546 *outptr++ = buf[0] - 0x80; \
547 written = 1; \
548 } \
549 else \
550 written = __UNKNOWN_10646_CHAR; \
551 } \
552 else \
553 { \
554 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
555 written = ucs4_to_jisx0208 (ch, outptr, outend - outptr); \
556 else if (set == JISX0212_set) \
557 written = ucs4_to_jisx0212 (ch, outptr, outend - outptr); \
558 else if (set == GB2312_set) \
559 written = ucs4_to_gb2312 (ch, outptr, outend - outptr); \
560 else \
561 { \
562 assert (set == KSC5601_set); \
563 \
564 written = ucs4_to_ksc5601 (ch, outptr, outend - outptr); \
565 } \
566 \
567 if (__builtin_expect (written, 1) == 0) \
568 { \
569 result = __GCONV_FULL_OUTPUT; \
570 break; \
571 } \
572 else if (written != __UNKNOWN_10646_CHAR) \
573 outptr += written; \
574 } \
575 \
576 if (written == __UNKNOWN_10646_CHAR || written == 0) \
577 { \
578 if (set2 == ISO88591_set) \
579 { \
580 if (__builtin_expect (outptr + 3 > outend, 0)) \
581 { \
582 result = __GCONV_FULL_OUTPUT; \
583 break; \
584 } \
585 \
586 if (ch >= 0x80 && ch <= 0xff) \
587 { \
588 *outptr++ = ESC; \
589 *outptr++ = 'N'; \
590 *outptr++ = ch & 0x7f; \
591 written = 3; \
592 } \
593 } \
594 else if (set2 == ISO88597_set) \
595 { \
596 const struct gap *rp = from_idx; \
597 \
598 while (ch > rp->end) \
599 ++rp; \
600 if (ch >= rp->start) \
601 { \
602 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
603 if (res != '\0') \
604 { \
605 if (__builtin_expect (outptr + 3 > outend, 0)) \
606 { \
607 result = __GCONV_FULL_OUTPUT; \
608 break; \
609 } \
610 \
611 *outptr++ = ESC; \
612 *outptr++ = 'N'; \
613 *outptr++ = res; \
614 written = 3; \
615 } \
616 } \
617 } \
618 } \
619 \
620 if (written == __UNKNOWN_10646_CHAR || written == 0) \
621 { \
622 /* Either this is an unknown character or we have to switch \
623 the currently selected character set. The character sets \
624 do not code entirely separate parts of ISO 10646 and \
625 therefore there is no single correct result. If we choose \
626 the character set to use wrong we might be end up with \
627 using yet another character set for the next character \
628 though the current and the next could be encoded with one \
629 character set. We leave this kind of optimization for \
630 later and now simply use a fixed order in which we test for \
631 availability */ \
632 \
633 if (ch <= 0x7f) \
634 { \
635 /* We must encode using ASCII. First write out the \
636 escape sequence. */ \
637 if (__builtin_expect (outptr + 3 > outend, 0)) \
638 { \
639 result = __GCONV_FULL_OUTPUT; \
640 break; \
641 } \
642 \
643 *outptr++ = ESC; \
644 *outptr++ = '('; \
645 *outptr++ = 'B'; \
646 set = ASCII_set; \
647 \
648 if (__builtin_expect (outptr + 1 > outend, 0)) \
649 { \
650 result = __GCONV_FULL_OUTPUT; \
651 break; \
652 } \
653 *outptr++ = ch; \
654 \
655 /* At the beginning of a line, G2 designation is cleared. */ \
656 if (var == iso2022jp2 && ch == 0x0a) \
657 set2 = UNSPECIFIED_set; \
658 } \
659 else \
660 { \
661 /* Now it becomes difficult. We must search the other \
662 character sets one by one and we cannot use simple \
663 arithmetic to determine whether the character can be \
664 encoded using this set. */ \
665 size_t written; \
666 unsigned char buf[2]; \
667 \
668 written = ucs4_to_jisx0201 (ch, buf); \
669 if (written != __UNKNOWN_10646_CHAR && buf[0] < 0x80) \
670 { \
671 /* We use JIS X 0201. */ \
672 if (__builtin_expect (outptr + 3 > outend, 0)) \
673 { \
674 result = __GCONV_FULL_OUTPUT; \
675 break; \
676 } \
677 \
678 *outptr++ = ESC; \
679 *outptr++ = '('; \
680 *outptr++ = 'J'; \
681 set = JISX0201_Roman_set; \
682 \
683 if (__builtin_expect (outptr + 1 > outend, 0)) \
684 { \
685 result = __GCONV_FULL_OUTPUT; \
686 break; \
687 } \
688 *outptr++ = buf[0]; \
689 } \
690 else \
691 { \
692 written = ucs4_to_jisx0208 (ch, buf, 2); \
693 if (written != __UNKNOWN_10646_CHAR) \
694 { \
695 /* We use JIS X 0208. */ \
696 if (__builtin_expect (outptr + 3 > outend, 0)) \
697 { \
698 result = __GCONV_FULL_OUTPUT; \
699 break; \
700 } \
701 \
702 *outptr++ = ESC; \
703 *outptr++ = '$'; \
704 *outptr++ = 'B'; \
705 set = JISX0208_1983_set; \
706 \
707 if (__builtin_expect (outptr + 2 > outend, 0)) \
708 { \
709 result = __GCONV_FULL_OUTPUT; \
710 break; \
711 } \
712 *outptr++ = buf[0]; \
713 *outptr++ = buf[1]; \
714 } \
715 else if (__builtin_expect (var, iso2022jp2) == iso2022jp) \
716 { \
717 /* We have no other choice. */ \
718 STANDARD_ERR_HANDLER (4); \
719 } \
720 else \
721 { \
722 written = ucs4_to_jisx0212 (ch, buf, 2); \
723 if (written != __UNKNOWN_10646_CHAR) \
724 { \
725 /* We use JIS X 0212. */ \
726 if (__builtin_expect (outptr + 4 > outend, 0)) \
727 { \
728 result = __GCONV_FULL_OUTPUT; \
729 break; \
730 } \
731 *outptr++ = ESC; \
732 *outptr++ = '$'; \
733 *outptr++ = '('; \
734 *outptr++ = 'D'; \
735 set = JISX0212_set; \
736 \
737 if (__builtin_expect (outptr + 2 > outend, 0)) \
738 { \
739 result = __GCONV_FULL_OUTPUT; \
740 break; \
741 } \
742 *outptr++ = buf[0]; \
743 *outptr++ = buf[1]; \
744 } \
745 else \
746 { \
747 written = ucs4_to_jisx0201 (ch, buf); \
748 if (written != __UNKNOWN_10646_CHAR \
749 && buf[0] >= 0x80) \
750 { \
751 /* We use JIS X 0201. */ \
752 if (__builtin_expect (outptr + 3 > outend, 0)) \
753 { \
754 result = __GCONV_FULL_OUTPUT; \
755 break; \
756 } \
757 \
758 *outptr++ = ESC; \
759 *outptr++ = '('; \
760 *outptr++ = 'I'; \
761 set = JISX0201_Kana_set; \
762 \
763 if (__builtin_expect (outptr + 1 > outend, 0)) \
764 { \
765 result = __GCONV_FULL_OUTPUT; \
766 break; \
767 } \
768 *outptr++ = buf[0] - 0x80; \
769 } \
770 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
771 { \
772 /* ISO 8859-1 upper half. */ \
773 if (__builtin_expect (outptr + 3 > outend, 0)) \
774 { \
775 result = __GCONV_FULL_OUTPUT; \
776 break; \
777 } \
778 \
779 *outptr++ = ESC; \
780 *outptr++ = '.'; \
781 *outptr++ = 'A'; \
782 set2 = ISO88591_set; \
783 \
784 if (__builtin_expect (outptr + 3 > outend, 0)) \
785 { \
786 result = __GCONV_FULL_OUTPUT; \
787 break; \
788 } \
789 *outptr++ = ESC; \
790 *outptr++ = 'N'; \
791 *outptr++ = ch; \
792 } \
793 else \
794 { \
795 written = ucs4_to_gb2312 (ch, buf, 2); \
796 if (written != __UNKNOWN_10646_CHAR) \
797 { \
798 /* We use GB 2312. */ \
799 if (__builtin_expect (outptr + 3 > outend, 0))\
800 { \
801 result = __GCONV_FULL_OUTPUT; \
802 break; \
803 } \
804 \
805 *outptr++ = ESC; \
806 *outptr++ = '$'; \
807 *outptr++ = 'A'; \
808 set = GB2312_set; \
809 \
810 if (__builtin_expect (outptr + 2 > outend, 0))\
811 { \
812 result = __GCONV_FULL_OUTPUT; \
813 break; \
814 } \
815 *outptr++ = buf[0]; \
816 *outptr++ = buf[1]; \
817 } \
818 else \
819 { \
820 written = ucs4_to_ksc5601 (ch, buf, 2); \
821 if (written != __UNKNOWN_10646_CHAR) \
822 { \
823 /* We use KSC 5601. */ \
824 if (__builtin_expect (outptr + 4 > outend,\
825 0)) \
826 { \
827 result = __GCONV_FULL_OUTPUT; \
828 break; \
829 } \
830 *outptr++ = ESC; \
831 *outptr++ = '$'; \
832 *outptr++ = '('; \
833 *outptr++ = 'C'; \
834 set = KSC5601_set; \
835 \
836 if (__builtin_expect (outptr + 2 > outend,\
837 0)) \
838 { \
839 result = __GCONV_FULL_OUTPUT; \
840 break; \
841 } \
842 *outptr++ = buf[0]; \
843 *outptr++ = buf[1]; \
844 } \
845 else \
846 { \
847 const struct gap *rp = from_idx; \
848 unsigned char gch = 0; \
849 \
850 while (ch > rp->end) \
851 ++rp; \
852 if (ch >= rp->start) \
853 { \
854 ch = ch - 0xa0 + rp->idx; \
855 gch = iso88597_from_ucs4[ch]; \
856 } \
857 \
858 if (__builtin_expect (gch, 1) != 0) \
859 { \
860 /* We use ISO 8859-7 greek. */ \
861 if (__builtin_expect (outptr + 3 \
862 > outend, 0)) \
863 { \
864 result = __GCONV_FULL_OUTPUT; \
865 break; \
866 } \
867 *outptr++ = ESC; \
868 *outptr++ = '.'; \
869 *outptr++ = 'F'; \
870 set2 = ISO88597_set; \
871 \
872 if (__builtin_expect (outptr + 3 \
873 > outend, 0)) \
874 { \
875 result = __GCONV_FULL_OUTPUT; \
876 break; \
877 } \
878 *outptr++ = ESC; \
879 *outptr++ = 'N'; \
880 *outptr++ = gch; \
881 } \
882 else \
883 { \
884 STANDARD_ERR_HANDLER (4); \
885 } \
886 } \
887 } \
888 } \
889 } \
890 } \
891 } \
892 } \
893 } \
894 \
895 /* Now that we wrote the output increment the input pointer. */ \
896 inptr += 4; \
897 }
898 #define LOOP_NEED_FLAGS
899 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
900 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
901 int set2 = *setp & CURRENT_ASSIGN_MASK
902 #define UPDATE_PARAMS *setp = set | set2
903 #include <iconv/loop.c>
904
905
906 /* Now define the toplevel functions. */
907 #include <iconv/skeleton.c>