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.
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. */
38 #include "iso8859-7jp.h"
40 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
43 /* We provide our own initialization and destructor function. */
47 /* Definitions used in the body of the `gconv' function. */
48 #define FROM_LOOP from_iso2022jp_loop
49 #define TO_LOOP to_iso2022jp_loop
50 #define MIN_NEEDED_FROM 1
51 #define MAX_NEEDED_FROM 4
52 #define MIN_NEEDED_TO 4
53 #define MAX_NEEDED_TO 4
54 #define FROM_DIRECTION (dir == from_iso2022jp)
55 #define PREPARE_LOOP \
56 enum direction dir = ((struct iso2022jp_data *) step->__data)->dir; \
57 enum variant var = ((struct iso2022jp_data *) step->__data)->var; \
59 int *setp = &data->__statep->__count;
60 #define EXTRA_LOOP_ARGS , var, setp
63 /* Direction of the transformation. */
71 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
87 /* The COUNT element of the state keeps track of the currently selected
88 character set. The possible values are: */
92 JISX0208_1978_set = 8,
93 JISX0208_1983_set = 16,
94 JISX0201_Roman_set = 24,
95 JISX0201_Kana_set = 32,
102 /* The second value stored is the designation of the G2 set. The following
103 values are possible: */
109 CURRENT_ASSIGN_MASK = 192
114 gconv_init (struct __gconv_step *step)
116 /* Determine which direction. */
117 struct iso2022jp_data *new_data;
118 enum direction dir = illegal_dir;
119 enum variant var = illegal_var;
122 if (__strcasecmp (step->__from_name, "ISO-2022-JP//") == 0)
124 dir = from_iso2022jp;
127 else if (__strcasecmp (step->__to_name, "ISO-2022-JP//") == 0)
132 else if (__strcasecmp (step->__from_name, "ISO-2022-JP-2//") == 0)
134 dir = from_iso2022jp;
137 else if (__strcasecmp (step->__to_name, "ISO-2022-JP-2//") == 0)
143 result = __GCONV_NOCONV;
144 if (__builtin_expect (dir, from_iso2022jp) != illegal_dir)
147 = (struct iso2022jp_data *) malloc (sizeof (struct iso2022jp_data));
149 result = __GCONV_NOMEM;
150 if (new_data != NULL)
154 step->__data = new_data;
156 if (dir == from_iso2022jp)
158 step->__min_needed_from = MIN_NEEDED_FROM;
159 step->__max_needed_from = MAX_NEEDED_FROM;
160 step->__min_needed_to = MIN_NEEDED_TO;
161 step->__max_needed_to = MAX_NEEDED_TO;
165 step->__min_needed_from = MIN_NEEDED_TO;
166 step->__max_needed_from = MAX_NEEDED_TO;
167 step->__min_needed_to = MIN_NEEDED_FROM;
168 step->__max_needed_to = MAX_NEEDED_FROM + 2;
171 /* Yes, this is a stateful encoding. */
172 step->__stateful = 1;
183 gconv_end (struct __gconv_step *data)
189 /* Since this is a stateful encoding we have to provide code which resets
190 the output state to the initial state. This has to be done during the
192 #define EMIT_SHIFT_TO_INIT \
193 if ((data->__statep->__count & ~7) != ASCII_set) \
195 enum direction dir = ((struct iso2022jp_data *) step->__data)->dir; \
197 if (dir == from_iso2022jp) \
199 /* It's easy, we don't have to emit anything, we just reset the \
200 state for the input. Note that this also clears the G2 \
202 data->__statep->__count &= 7; \
203 data->__statep->__count |= ASCII_set; \
207 unsigned char *outbuf = data->__outbuf; \
209 /* We are not in the initial state. To switch back we have \
210 to emit the sequence `Esc ( B'. */ \
211 if (__builtin_expect (outbuf + 3 > data->__outbufend, 0)) \
212 /* We don't have enough room in the output buffer. */ \
213 status = __GCONV_FULL_OUTPUT; \
216 /* Write out the shift sequence. */ \
220 data->__outbuf = outbuf; \
221 /* Note that this also clears the G2 designation. */ \
222 data->__statep->__count &= ~7; \
223 data->__statep->__count |= ASCII_set; \
229 /* Since we might have to reset input pointer we must be able to save
230 and retore the state. */
231 #define SAVE_RESET_STATE(Save) \
238 /* First define the conversion function from ISO-2022-JP to UCS4. */
239 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
240 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
241 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
242 #define LOOPFCT FROM_LOOP
245 uint32_t ch = *inptr; \
247 /* Recognize escape sequences. */ \
248 if (__builtin_expect (ch, 0) == ESC) \
250 /* We now must be prepared to read two to three more \
251 chracters. If we have a match in the first character but \
252 then the input buffer ends we terminate with an error since \
253 we must not risk missing an escape sequence just because it \
254 is not entirely in the current input buffer. */ \
255 if (__builtin_expect (inptr + 2 >= inend, 0) \
256 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
257 && __builtin_expect (inptr + 3 >= inend, 0))) \
259 /* Not enough input available. */ \
260 result = __GCONV_EMPTY_INPUT; \
264 if (inptr[1] == '(') \
266 if (inptr[2] == 'B') \
268 /* ASCII selected. */ \
273 else if (inptr[2] == 'J') \
275 /* JIS X 0201 selected. */ \
276 set = JISX0201_Roman_set; \
280 else if (var == iso2022jp2 && inptr[2] == 'I') \
282 /* JIS X 0201 selected. */ \
283 set = JISX0201_Kana_set; \
288 else if (inptr[1] == '$') \
290 if (inptr[2] == '@') \
292 /* JIS X 0208-1978 selected. */ \
293 set = JISX0208_1978_set; \
297 else if (inptr[2] == 'B') \
299 /* JIS X 0208-1983 selected. */ \
300 set = JISX0208_1983_set; \
304 else if (var == iso2022jp2) \
306 if (inptr[2] == 'A') \
308 /* GB 2312-1980 selected. */ \
313 else if (inptr[2] == '(') \
315 if (inptr[3] == 'C') \
317 /* KSC 5601-1987 selected. */ \
322 else if (inptr[3] == 'D') \
324 /* JIS X 0212-1990 selected. */ \
325 set = JISX0212_set; \
332 else if (var == iso2022jp2 && inptr[1] == '.') \
334 if (inptr[2] == 'A') \
336 /* ISO 8859-1-GR selected. */ \
337 set2 = ISO88591_set; \
341 else if (inptr[2] == 'F') \
343 /* ISO 8859-7-GR selected. */ \
344 set2 = ISO88597_set; \
351 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
353 if (set2 == ISO88591_set) \
355 ch = inptr[2] | 0x80; \
358 else if (__builtin_expect (set2, ISO88597_set) == ISO88597_set) \
360 /* We use the table from the ISO 8859-7 module. */ \
361 if (inptr[2] < 0x20 || inptr[2] > 0x80) \
363 if (! ignore_errors_p ()) \
365 result = __GCONV_ILLEGAL_INPUT; \
373 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
376 if (! ignore_errors_p ()) \
378 result = __GCONV_ILLEGAL_INPUT; \
390 if (! ignore_errors_p ()) \
392 result = __GCONV_ILLEGAL_INPUT; \
401 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
402 /* Almost done, just advance the input pointer. */ \
404 else if (set == JISX0201_Roman_set) \
406 /* Use the JIS X 0201 table. */ \
407 ch = jisx0201_to_ucs4 (ch); \
408 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
410 if (! ignore_errors_p ()) \
412 result = __GCONV_ILLEGAL_INPUT; \
422 else if (set == JISX0201_Kana_set) \
424 /* Use the JIS X 0201 table. */ \
425 ch = jisx0201_to_ucs4 (ch + 0x80); \
426 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
428 if (! ignore_errors_p ()) \
430 result = __GCONV_ILLEGAL_INPUT; \
442 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
443 /* XXX I don't have the tables for these two old variants of \
444 JIS X 0208. Therefore I'm using the tables for JIS X \
445 0208-1990. If somebody has problems with this please \
446 provide the appropriate tables. */ \
447 ch = jisx0208_to_ucs4 (&inptr, \
448 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
449 else if (set == JISX0212_set) \
450 /* Use the JIS X 0212 table. */ \
451 ch = jisx0212_to_ucs4 (&inptr, \
452 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
453 else if (set == GB2312_set) \
454 /* Use the GB 2312 table. */ \
455 ch = gb2312_to_ucs4 (&inptr, \
456 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
459 assert (set == KSC5601_set); \
461 /* Use the KSC 5601 table. */ \
462 ch = ksc5601_to_ucs4 (&inptr, \
463 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
466 if (NEED_LENGTH_TEST && __builtin_expect (ch, 1) == 0) \
468 result = __GCONV_EMPTY_INPUT; \
471 else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
473 if (! ignore_errors_p ()) \
475 result = __GCONV_ILLEGAL_INPUT; \
485 put32 (outptr, ch); \
488 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
489 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
490 int set2 = *setp & CURRENT_ASSIGN_MASK
491 #define UPDATE_PARAMS *setp = set | set2
492 #include <iconv/loop.c>
495 /* Next, define the other direction. */
496 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
497 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
498 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
499 #define LOOPFCT TO_LOOP
503 size_t written = 0; \
505 ch = get32 (inptr); \
507 /* First see whether we can write the character using the currently \
508 selected character set. */ \
509 if (set == ASCII_set) \
511 /* Please note that the NUL byte is *not* matched if we are not \
512 currently using the ASCII charset. This is because we must \
513 switch to the initial state whenever a NUL byte is written. */ \
519 /* At the beginning of a line, G2 designation is cleared. */ \
520 if (var == iso2022jp2 && ch == 0x0a) \
521 set2 = UNSPECIFIED_set; \
523 /* ISO-2022-JP recommends to encode the newline character always in \
524 ASCII since this allows a context-free interpretation of the \
525 characters at the beginning of the next line. Otherwise it would \
526 have to be known whether the last line ended using ASCII or \
528 else if (set == JISX0201_Roman_set) \
530 unsigned char buf[2]; \
531 written = ucs4_to_jisx0201 (ch, buf); \
532 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0x20 \
535 *outptr++ = buf[0]; \
539 written = __UNKNOWN_10646_CHAR; \
541 else if (set == JISX0201_Kana_set) \
543 unsigned char buf[2]; \
544 written = ucs4_to_jisx0201 (ch, buf); \
545 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0xa0 \
548 *outptr++ = buf[0] - 0x80; \
552 written = __UNKNOWN_10646_CHAR; \
556 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
557 written = ucs4_to_jisx0208 (ch, outptr, \
559 ? outend - outptr : 2)); \
560 else if (set == JISX0212_set) \
561 written = ucs4_to_jisx0212 (ch, outptr, \
563 ? outend - outptr : 2)); \
564 else if (set == GB2312_set) \
565 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
566 ? outend - outptr : 2)); \
569 assert (set == KSC5601_set); \
571 written = ucs4_to_ksc5601 (ch, outptr, \
573 ? outend - outptr : 2)); \
576 if (NEED_LENGTH_TEST && __builtin_expect (written, 1) == 0) \
578 result = __GCONV_FULL_OUTPUT; \
581 else if (written != __UNKNOWN_10646_CHAR) \
585 if (written == __UNKNOWN_10646_CHAR || written == 0) \
587 if (set2 == ISO88591_set) \
589 if (ch >= 0x80 && ch <= 0xff) \
593 *outptr++ = ch & 0x7f; \
597 else if (set2 == ISO88597_set) \
599 const struct gap *rp = from_idx; \
601 while (ch > rp->end) \
603 if (ch >= rp->start) \
605 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
617 if (written == __UNKNOWN_10646_CHAR || written == 0) \
619 /* Either this is an unknown character or we have to switch \
620 the currently selected character set. The character sets \
621 do not code entirely separate parts of ISO 10646 and \
622 therefore there is no single correct result. If we choose \
623 the character set to use wrong we might be end up with \
624 using yet another character set for the next character \
625 though the current and the next could be encoded with one \
626 character set. We leave this kind of optimization for \
627 later and now simply use a fixed order in which we test for \
632 /* We must encode using ASCII. First write out the \
633 escape sequence. */ \
634 if (NEED_LENGTH_TEST && __builtin_expect (outptr + 3 > outend, 0))\
636 result = __GCONV_FULL_OUTPUT; \
645 if (NEED_LENGTH_TEST && __builtin_expect (outptr + 1 > outend, 0))\
647 result = __GCONV_FULL_OUTPUT; \
652 /* At the beginning of a line, G2 designation is cleared. */ \
653 if (var == iso2022jp2 && ch == 0x0a) \
654 set2 = UNSPECIFIED_set; \
658 /* Now it becomes difficult. We must search the other \
659 character sets one by one and we cannot use simple \
660 arithmetic to determine whether the character can be \
661 encoded using this set. */ \
663 unsigned char buf[2]; \
665 written = ucs4_to_jisx0201 (ch, buf); \
666 if (written != __UNKNOWN_10646_CHAR && buf[0] < 0x80) \
668 /* We use JIS X 0201. */ \
669 if (NEED_LENGTH_TEST \
670 && __builtin_expect (outptr + 3 > outend, 0)) \
672 result = __GCONV_FULL_OUTPUT; \
679 set = JISX0201_Roman_set; \
681 if (NEED_LENGTH_TEST \
682 && __builtin_expect (outptr + 1 > outend, 0)) \
684 result = __GCONV_FULL_OUTPUT; \
687 *outptr++ = buf[0]; \
691 written = ucs4_to_jisx0208 (ch, buf, 2); \
692 if (written != __UNKNOWN_10646_CHAR) \
694 /* We use JIS X 0208. */ \
695 if (NEED_LENGTH_TEST \
696 && __builtin_expect (outptr + 3 > outend, 0)) \
698 result = __GCONV_FULL_OUTPUT; \
705 set = JISX0208_1983_set; \
707 if (NEED_LENGTH_TEST \
708 && __builtin_expect (outptr + 2 > outend, 0)) \
710 result = __GCONV_FULL_OUTPUT; \
713 *outptr++ = buf[0]; \
714 *outptr++ = buf[1]; \
716 else if (__builtin_expect (var, iso2022jp2) == iso2022jp) \
718 /* We have no other choice. */ \
719 if (! ignore_errors_p ()) \
721 result = __GCONV_ILLEGAL_INPUT; \
729 written = ucs4_to_jisx0212 (ch, buf, 2); \
730 if (written != __UNKNOWN_10646_CHAR) \
732 /* We use JIS X 0212. */ \
733 if (NEED_LENGTH_TEST \
734 && __builtin_expect (outptr + 4 > outend, 0)) \
736 result = __GCONV_FULL_OUTPUT; \
743 set = JISX0212_set; \
745 if (NEED_LENGTH_TEST \
746 && __builtin_expect (outptr + 2 > outend, 0)) \
748 result = __GCONV_FULL_OUTPUT; \
751 *outptr++ = buf[0]; \
752 *outptr++ = buf[1]; \
756 written = ucs4_to_jisx0201 (ch, buf); \
757 if (written != __UNKNOWN_10646_CHAR \
760 /* We use JIS X 0201. */ \
761 if (NEED_LENGTH_TEST \
762 && __builtin_expect (outptr + 3 > outend, 0)) \
764 result = __GCONV_FULL_OUTPUT; \
771 set = JISX0201_Kana_set; \
773 if (NEED_LENGTH_TEST \
774 && __builtin_expect (outptr + 1 > outend, 0)) \
776 result = __GCONV_FULL_OUTPUT; \
779 *outptr++ = buf[0] - 0x80; \
781 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
783 /* ISO 8859-1 upper half. */ \
784 if (NEED_LENGTH_TEST \
785 && __builtin_expect (outptr + 3 > outend, 0)) \
787 result = __GCONV_FULL_OUTPUT; \
794 set2 = ISO88591_set; \
796 if (NEED_LENGTH_TEST \
797 && __builtin_expect (outptr + 3 > outend, 0)) \
799 result = __GCONV_FULL_OUTPUT; \
808 written = ucs4_to_gb2312 (ch, buf, 2); \
809 if (written != __UNKNOWN_10646_CHAR) \
811 /* We use GB 2312. */ \
812 if (NEED_LENGTH_TEST \
813 && __builtin_expect (outptr + 3 > outend, \
816 result = __GCONV_FULL_OUTPUT; \
825 if (NEED_LENGTH_TEST \
826 && __builtin_expect (outptr + 2 > outend, \
829 result = __GCONV_FULL_OUTPUT; \
832 *outptr++ = buf[0]; \
833 *outptr++ = buf[1]; \
837 written = ucs4_to_ksc5601 (ch, buf, 2); \
838 if (written != __UNKNOWN_10646_CHAR) \
840 /* We use KSC 5601. */ \
841 if (NEED_LENGTH_TEST \
842 && __builtin_expect (outptr + 4 \
845 result = __GCONV_FULL_OUTPUT; \
854 if (NEED_LENGTH_TEST \
855 && __builtin_expect (outptr + 2 \
858 result = __GCONV_FULL_OUTPUT; \
861 *outptr++ = buf[0]; \
862 *outptr++ = buf[1]; \
866 const struct gap *rp = from_idx; \
867 unsigned char gch = 0; \
869 while (ch > rp->end) \
871 if (ch >= rp->start) \
873 ch = ch - 0xa0 + rp->idx; \
874 gch = iso88597_from_ucs4[ch]; \
877 if (__builtin_expect (gch, 1) != 0) \
879 /* We use ISO 8859-7 greek. */ \
880 if (NEED_LENGTH_TEST \
881 && __builtin_expect (outptr + 3 \
884 result = __GCONV_FULL_OUTPUT; \
890 set2 = ISO88597_set; \
892 if (NEED_LENGTH_TEST \
893 && __builtin_expect (outptr + 3 \
896 result = __GCONV_FULL_OUTPUT; \
905 if (! ignore_errors_p ()) \
907 result = __GCONV_ILLEGAL_INPUT; \
922 /* Now that we wrote the output increment the input pointer. */ \
925 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
926 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
927 int set2 = *setp & CURRENT_ASSIGN_MASK
928 #define UPDATE_PARAMS *setp = set | set2
929 #include <iconv/loop.c>
932 /* Now define the toplevel functions. */
933 #include <iconv/skeleton.c>