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. */
39 #include "iso8859-7jp.h"
41 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
44 /* We provide our own initialization and destructor function. */
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; \
60 int *setp = &data->__statep->__count;
61 #define EXTRA_LOOP_ARGS , var, setp
64 /* Direction of the transformation. */
72 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
88 /* The COUNT element of the state keeps track of the currently selected
89 character set. The possible values are: */
93 JISX0208_1978_set
= 8,
94 JISX0208_1983_set
= 16,
95 JISX0201_Roman_set
= 24,
96 JISX0201_Kana_set
= 32,
100 CURRENT_SEL_MASK
= 56
103 /* The second value stored is the designation of the G2 set. The following
104 values are possible: */
110 CURRENT_ASSIGN_MASK
= 192
114 extern int gconv_init (struct __gconv_step
*step
);
116 gconv_init (struct __gconv_step
*step
)
118 /* Determine which direction. */
119 struct iso2022jp_data
*new_data
;
120 enum direction dir
= illegal_dir
;
121 enum variant var
= illegal_var
;
124 if (__strcasecmp (step
->__from_name
, "ISO-2022-JP//") == 0)
126 dir
= from_iso2022jp
;
129 else if (__strcasecmp (step
->__to_name
, "ISO-2022-JP//") == 0)
134 else if (__strcasecmp (step
->__from_name
, "ISO-2022-JP-2//") == 0)
136 dir
= from_iso2022jp
;
139 else if (__strcasecmp (step
->__to_name
, "ISO-2022-JP-2//") == 0)
145 result
= __GCONV_NOCONV
;
146 if (__builtin_expect (dir
, from_iso2022jp
) != illegal_dir
)
149 = (struct iso2022jp_data
*) malloc (sizeof (struct iso2022jp_data
));
151 result
= __GCONV_NOMEM
;
152 if (new_data
!= NULL
)
156 step
->__data
= new_data
;
158 if (dir
== from_iso2022jp
)
160 step
->__min_needed_from
= MIN_NEEDED_FROM
;
161 step
->__max_needed_from
= MAX_NEEDED_FROM
;
162 step
->__min_needed_to
= MIN_NEEDED_TO
;
163 step
->__max_needed_to
= MAX_NEEDED_TO
;
167 step
->__min_needed_from
= MIN_NEEDED_TO
;
168 step
->__max_needed_from
= MAX_NEEDED_TO
;
169 step
->__min_needed_to
= MIN_NEEDED_FROM
;
170 step
->__max_needed_to
= MAX_NEEDED_FROM
+ 2;
173 /* Yes, this is a stateful encoding. */
174 step
->__stateful
= 1;
184 extern void gconv_end (struct __gconv_step
*data
);
186 gconv_end (struct __gconv_step
*data
)
192 /* Since this is a stateful encoding we have to provide code which resets
193 the output state to the initial state. This has to be done during the
195 #define EMIT_SHIFT_TO_INIT \
196 if ((data->__statep->__count & ~7) != ASCII_set) \
198 enum direction dir = ((struct iso2022jp_data *) step->__data)->dir; \
200 if (dir == from_iso2022jp) \
202 /* It's easy, we don't have to emit anything, we just reset the \
203 state for the input. Note that this also clears the G2 \
205 data->__statep->__count &= 7; \
206 data->__statep->__count |= ASCII_set; \
210 unsigned char *outbuf = data->__outbuf; \
212 /* We are not in the initial state. To switch back we have \
213 to emit the sequence `Esc ( B'. */ \
214 if (__builtin_expect (outbuf + 3 > data->__outbufend, 0)) \
215 /* We don't have enough room in the output buffer. */ \
216 status = __GCONV_FULL_OUTPUT; \
219 /* Write out the shift sequence. */ \
223 data->__outbuf = outbuf; \
224 /* Note that this also clears the G2 designation. */ \
225 data->__statep->__count &= ~7; \
226 data->__statep->__count |= ASCII_set; \
232 /* Since we might have to reset input pointer we must be able to save
233 and retore the state. */
234 #define SAVE_RESET_STATE(Save) \
241 /* First define the conversion function from ISO-2022-JP to UCS4. */
242 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
243 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
244 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
245 #define LOOPFCT FROM_LOOP
248 uint32_t ch = *inptr; \
250 /* Recognize escape sequences. */ \
251 if (__builtin_expect (ch, 0) == ESC) \
253 /* We now must be prepared to read two to three more \
254 chracters. If we have a match in the first character but \
255 then the input buffer ends we terminate with an error since \
256 we must not risk missing an escape sequence just because it \
257 is not entirely in the current input buffer. */ \
258 if (__builtin_expect (inptr + 2 >= inend, 0) \
259 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
260 && __builtin_expect (inptr + 3 >= inend, 0))) \
262 /* Not enough input available. */ \
263 result = __GCONV_INCOMPLETE_INPUT; \
267 if (inptr[1] == '(') \
269 if (inptr[2] == 'B') \
271 /* ASCII selected. */ \
276 else if (inptr[2] == 'J') \
278 /* JIS X 0201 selected. */ \
279 set = JISX0201_Roman_set; \
283 else if (var == iso2022jp2 && inptr[2] == 'I') \
285 /* JIS X 0201 selected. */ \
286 set = JISX0201_Kana_set; \
291 else if (inptr[1] == '$') \
293 if (inptr[2] == '@') \
295 /* JIS X 0208-1978 selected. */ \
296 set = JISX0208_1978_set; \
300 else if (inptr[2] == 'B') \
302 /* JIS X 0208-1983 selected. */ \
303 set = JISX0208_1983_set; \
307 else if (var == iso2022jp2) \
309 if (inptr[2] == 'A') \
311 /* GB 2312-1980 selected. */ \
316 else if (inptr[2] == '(') \
318 if (inptr[3] == 'C') \
320 /* KSC 5601-1987 selected. */ \
325 else if (inptr[3] == 'D') \
327 /* JIS X 0212-1990 selected. */ \
328 set = JISX0212_set; \
335 else if (var == iso2022jp2 && inptr[1] == '.') \
337 if (inptr[2] == 'A') \
339 /* ISO 8859-1-GR selected. */ \
340 set2 = ISO88591_set; \
344 else if (inptr[2] == 'F') \
346 /* ISO 8859-7-GR selected. */ \
347 set2 = ISO88597_set; \
354 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
356 if (set2 == ISO88591_set) \
358 ch = inptr[2] | 0x80; \
361 else if (__builtin_expect (set2, ISO88597_set) == ISO88597_set) \
363 /* We use the table from the ISO 8859-7 module. */ \
364 if (inptr[2] < 0x20 || inptr[2] >= 0x80) \
366 if (! ignore_errors_p ()) \
368 result = __GCONV_ILLEGAL_INPUT; \
376 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
379 if (! ignore_errors_p ()) \
381 result = __GCONV_ILLEGAL_INPUT; \
393 if (! ignore_errors_p ()) \
395 result = __GCONV_ILLEGAL_INPUT; \
404 else if (ch >= 0x80) \
406 if (! ignore_errors_p ()) \
408 result = __GCONV_ILLEGAL_INPUT; \
416 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
417 /* Almost done, just advance the input pointer. */ \
419 else if (set == JISX0201_Roman_set) \
421 /* Use the JIS X 0201 table. */ \
422 ch = jisx0201_to_ucs4 (ch); \
423 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
425 if (! ignore_errors_p ()) \
427 result = __GCONV_ILLEGAL_INPUT; \
437 else if (set == JISX0201_Kana_set) \
439 /* Use the JIS X 0201 table. */ \
440 ch = jisx0201_to_ucs4 (ch + 0x80); \
441 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
443 if (! ignore_errors_p ()) \
445 result = __GCONV_ILLEGAL_INPUT; \
457 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
458 /* XXX I don't have the tables for these two old variants of \
459 JIS X 0208. Therefore I'm using the tables for JIS X \
460 0208-1990. If somebody has problems with this please \
461 provide the appropriate tables. */ \
462 ch = jisx0208_to_ucs4 (&inptr, inend - inptr, 0); \
463 else if (set == JISX0212_set) \
464 /* Use the JIS X 0212 table. */ \
465 ch = jisx0212_to_ucs4 (&inptr, inend - inptr, 0); \
466 else if (set == GB2312_set) \
467 /* Use the GB 2312 table. */ \
468 ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0); \
471 assert (set == KSC5601_set); \
473 /* Use the KSC 5601 table. */ \
474 ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0); \
477 if (__builtin_expect (ch, 1) == 0) \
479 result = __GCONV_INCOMPLETE_INPUT; \
482 else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
484 if (! ignore_errors_p ()) \
486 result = __GCONV_ILLEGAL_INPUT; \
496 put32 (outptr, ch); \
499 #define LOOP_NEED_FLAGS
500 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
501 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
502 int set2 = *setp & CURRENT_ASSIGN_MASK
503 #define UPDATE_PARAMS *setp = set | set2
504 #include <iconv/loop.c>
507 /* Next, define the other direction. */
508 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
509 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
510 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
511 #define LOOPFCT TO_LOOP
515 size_t written = 0; \
517 ch = get32 (inptr); \
519 /* First see whether we can write the character using the currently \
520 selected character set. */ \
521 if (set == ASCII_set) \
523 /* Please note that the NUL byte is *not* matched if we are not \
524 currently using the ASCII charset. This is because we must \
525 switch to the initial state whenever a NUL byte is written. */ \
531 /* At the beginning of a line, G2 designation is cleared. */ \
532 if (var == iso2022jp2 && ch == 0x0a) \
533 set2 = UNSPECIFIED_set; \
535 /* ISO-2022-JP recommends to encode the newline character always in \
536 ASCII since this allows a context-free interpretation of the \
537 characters at the beginning of the next line. Otherwise it would \
538 have to be known whether the last line ended using ASCII or \
540 else if (set == JISX0201_Roman_set) \
542 unsigned char buf[2]; \
543 written = ucs4_to_jisx0201 (ch, buf); \
544 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0x20 \
547 *outptr++ = buf[0]; \
551 written = __UNKNOWN_10646_CHAR; \
553 else if (set == JISX0201_Kana_set) \
555 unsigned char buf[2]; \
556 written = ucs4_to_jisx0201 (ch, buf); \
557 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0xa0 \
560 *outptr++ = buf[0] - 0x80; \
564 written = __UNKNOWN_10646_CHAR; \
568 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
569 written = ucs4_to_jisx0208 (ch, outptr, outend - outptr); \
570 else if (set == JISX0212_set) \
571 written = ucs4_to_jisx0212 (ch, outptr, outend - outptr); \
572 else if (set == GB2312_set) \
573 written = ucs4_to_gb2312 (ch, outptr, outend - outptr); \
576 assert (set == KSC5601_set); \
578 written = ucs4_to_ksc5601 (ch, outptr, outend - outptr); \
581 if (__builtin_expect (written, 1) == 0) \
583 result = __GCONV_FULL_OUTPUT; \
586 else if (written != __UNKNOWN_10646_CHAR) \
590 if (written == __UNKNOWN_10646_CHAR || written == 0) \
592 if (set2 == ISO88591_set) \
594 if (__builtin_expect (outptr + 3 > outend, 0)) \
596 result = __GCONV_FULL_OUTPUT; \
600 if (ch >= 0x80 && ch <= 0xff) \
604 *outptr++ = ch & 0x7f; \
608 else if (set2 == ISO88597_set) \
610 const struct gap *rp = from_idx; \
612 while (ch > rp->end) \
614 if (ch >= rp->start) \
616 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
619 if (__builtin_expect (outptr + 3 > outend, 0)) \
621 result = __GCONV_FULL_OUTPUT; \
634 if (written == __UNKNOWN_10646_CHAR || written == 0) \
636 /* Either this is an unknown character or we have to switch \
637 the currently selected character set. The character sets \
638 do not code entirely separate parts of ISO 10646 and \
639 therefore there is no single correct result. If we choose \
640 the character set to use wrong we might be end up with \
641 using yet another character set for the next character \
642 though the current and the next could be encoded with one \
643 character set. We leave this kind of optimization for \
644 later and now simply use a fixed order in which we test for \
649 /* We must encode using ASCII. First write out the \
650 escape sequence. */ \
651 if (__builtin_expect (outptr + 3 > outend, 0)) \
653 result = __GCONV_FULL_OUTPUT; \
662 if (__builtin_expect (outptr + 1 > outend, 0)) \
664 result = __GCONV_FULL_OUTPUT; \
669 /* At the beginning of a line, G2 designation is cleared. */ \
670 if (var == iso2022jp2 && ch == 0x0a) \
671 set2 = UNSPECIFIED_set; \
675 /* Now it becomes difficult. We must search the other \
676 character sets one by one and we cannot use simple \
677 arithmetic to determine whether the character can be \
678 encoded using this set. */ \
680 unsigned char buf[2]; \
682 written = ucs4_to_jisx0201 (ch, buf); \
683 if (written != __UNKNOWN_10646_CHAR && buf[0] < 0x80) \
685 /* We use JIS X 0201. */ \
686 if (__builtin_expect (outptr + 3 > outend, 0)) \
688 result = __GCONV_FULL_OUTPUT; \
695 set = JISX0201_Roman_set; \
697 if (__builtin_expect (outptr + 1 > outend, 0)) \
699 result = __GCONV_FULL_OUTPUT; \
702 *outptr++ = buf[0]; \
706 written = ucs4_to_jisx0208 (ch, buf, 2); \
707 if (written != __UNKNOWN_10646_CHAR) \
709 /* We use JIS X 0208. */ \
710 if (__builtin_expect (outptr + 3 > outend, 0)) \
712 result = __GCONV_FULL_OUTPUT; \
719 set = JISX0208_1983_set; \
721 if (__builtin_expect (outptr + 2 > outend, 0)) \
723 result = __GCONV_FULL_OUTPUT; \
726 *outptr++ = buf[0]; \
727 *outptr++ = buf[1]; \
729 else if (__builtin_expect (var, iso2022jp2) == iso2022jp) \
731 /* We have no other choice. */ \
732 STANDARD_ERR_HANDLER (4); \
736 written = ucs4_to_jisx0212 (ch, buf, 2); \
737 if (written != __UNKNOWN_10646_CHAR) \
739 /* We use JIS X 0212. */ \
740 if (__builtin_expect (outptr + 4 > outend, 0)) \
742 result = __GCONV_FULL_OUTPUT; \
749 set = JISX0212_set; \
751 if (__builtin_expect (outptr + 2 > outend, 0)) \
753 result = __GCONV_FULL_OUTPUT; \
756 *outptr++ = buf[0]; \
757 *outptr++ = buf[1]; \
761 written = ucs4_to_jisx0201 (ch, buf); \
762 if (written != __UNKNOWN_10646_CHAR \
765 /* We use JIS X 0201. */ \
766 if (__builtin_expect (outptr + 3 > outend, 0)) \
768 result = __GCONV_FULL_OUTPUT; \
775 set = JISX0201_Kana_set; \
777 if (__builtin_expect (outptr + 1 > outend, 0)) \
779 result = __GCONV_FULL_OUTPUT; \
782 *outptr++ = buf[0] - 0x80; \
784 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
786 /* ISO 8859-1 upper half. */ \
787 if (__builtin_expect (outptr + 3 > outend, 0)) \
789 result = __GCONV_FULL_OUTPUT; \
796 set2 = ISO88591_set; \
798 if (__builtin_expect (outptr + 3 > outend, 0)) \
800 result = __GCONV_FULL_OUTPUT; \
805 *outptr++ = ch - 0x80; \
809 written = ucs4_to_gb2312 (ch, buf, 2); \
810 if (written != __UNKNOWN_10646_CHAR) \
812 /* We use GB 2312. */ \
813 if (__builtin_expect (outptr + 3 > outend, 0))\
815 result = __GCONV_FULL_OUTPUT; \
824 if (__builtin_expect (outptr + 2 > outend, 0))\
826 result = __GCONV_FULL_OUTPUT; \
829 *outptr++ = buf[0]; \
830 *outptr++ = buf[1]; \
834 written = ucs4_to_ksc5601 (ch, buf, 2); \
835 if (written != __UNKNOWN_10646_CHAR) \
837 /* We use KSC 5601. */ \
838 if (__builtin_expect (outptr + 4 > outend,\
841 result = __GCONV_FULL_OUTPUT; \
850 if (__builtin_expect (outptr + 2 > outend,\
853 result = __GCONV_FULL_OUTPUT; \
856 *outptr++ = buf[0]; \
857 *outptr++ = buf[1]; \
861 const struct gap *rp = from_idx; \
862 unsigned char gch = 0; \
864 while (ch > rp->end) \
866 if (ch >= rp->start) \
868 ch = ch - 0xa0 + rp->idx; \
869 gch = iso88597_from_ucs4[ch]; \
872 if (__builtin_expect (gch, 1) != 0) \
874 /* We use ISO 8859-7 greek. */ \
875 if (__builtin_expect (outptr + 3 \
878 result = __GCONV_FULL_OUTPUT; \
884 set2 = ISO88597_set; \
886 if (__builtin_expect (outptr + 3 \
889 result = __GCONV_FULL_OUTPUT; \
898 STANDARD_ERR_HANDLER (4); \
909 /* Now that we wrote the output increment the input pointer. */ \
912 #define LOOP_NEED_FLAGS
913 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
914 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
915 int set2 = *setp & CURRENT_ASSIGN_MASK
916 #define UPDATE_PARAMS *setp = set | set2
917 #include <iconv/loop.c>
920 /* Now define the toplevel functions. */
921 #include <iconv/skeleton.c>