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
115 gconv_init (struct __gconv_step
*step
)
117 /* Determine which direction. */
118 struct iso2022jp_data
*new_data
;
119 enum direction dir
= illegal_dir
;
120 enum variant var
= illegal_var
;
123 if (__strcasecmp (step
->__from_name
, "ISO-2022-JP//") == 0)
125 dir
= from_iso2022jp
;
128 else if (__strcasecmp (step
->__to_name
, "ISO-2022-JP//") == 0)
133 else if (__strcasecmp (step
->__from_name
, "ISO-2022-JP-2//") == 0)
135 dir
= from_iso2022jp
;
138 else if (__strcasecmp (step
->__to_name
, "ISO-2022-JP-2//") == 0)
144 result
= __GCONV_NOCONV
;
145 if (__builtin_expect (dir
, from_iso2022jp
) != illegal_dir
)
148 = (struct iso2022jp_data
*) malloc (sizeof (struct iso2022jp_data
));
150 result
= __GCONV_NOMEM
;
151 if (new_data
!= NULL
)
155 step
->__data
= new_data
;
157 if (dir
== from_iso2022jp
)
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
;
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;
172 /* Yes, this is a stateful encoding. */
173 step
->__stateful
= 1;
184 gconv_end (struct __gconv_step
*data
)
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
193 #define EMIT_SHIFT_TO_INIT \
194 if ((data->__statep->__count & ~7) != ASCII_set) \
196 enum direction dir = ((struct iso2022jp_data *) step->__data)->dir; \
198 if (dir == from_iso2022jp) \
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 \
203 data->__statep->__count &= 7; \
204 data->__statep->__count |= ASCII_set; \
208 unsigned char *outbuf = data->__outbuf; \
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; \
217 /* Write out the shift sequence. */ \
221 data->__outbuf = outbuf; \
222 /* Note that this also clears the G2 designation. */ \
223 data->__statep->__count &= ~7; \
224 data->__statep->__count |= ASCII_set; \
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) \
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
246 uint32_t ch = *inptr; \
248 /* Recognize escape sequences. */ \
249 if (__builtin_expect (ch, 0) == ESC) \
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))) \
260 /* Not enough input available. */ \
261 result = __GCONV_EMPTY_INPUT; \
265 if (inptr[1] == '(') \
267 if (inptr[2] == 'B') \
269 /* ASCII selected. */ \
274 else if (inptr[2] == 'J') \
276 /* JIS X 0201 selected. */ \
277 set = JISX0201_Roman_set; \
281 else if (var == iso2022jp2 && inptr[2] == 'I') \
283 /* JIS X 0201 selected. */ \
284 set = JISX0201_Kana_set; \
289 else if (inptr[1] == '$') \
291 if (inptr[2] == '@') \
293 /* JIS X 0208-1978 selected. */ \
294 set = JISX0208_1978_set; \
298 else if (inptr[2] == 'B') \
300 /* JIS X 0208-1983 selected. */ \
301 set = JISX0208_1983_set; \
305 else if (var == iso2022jp2) \
307 if (inptr[2] == 'A') \
309 /* GB 2312-1980 selected. */ \
314 else if (inptr[2] == '(') \
316 if (inptr[3] == 'C') \
318 /* KSC 5601-1987 selected. */ \
323 else if (inptr[3] == 'D') \
325 /* JIS X 0212-1990 selected. */ \
326 set = JISX0212_set; \
333 else if (var == iso2022jp2 && inptr[1] == '.') \
335 if (inptr[2] == 'A') \
337 /* ISO 8859-1-GR selected. */ \
338 set2 = ISO88591_set; \
342 else if (inptr[2] == 'F') \
344 /* ISO 8859-7-GR selected. */ \
345 set2 = ISO88597_set; \
352 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
354 if (set2 == ISO88591_set) \
356 ch = inptr[2] | 0x80; \
359 else if (__builtin_expect (set2, ISO88597_set) == ISO88597_set) \
361 /* We use the table from the ISO 8859-7 module. */ \
362 if (inptr[2] < 0x20 || inptr[2] > 0x80) \
364 if (! ignore_errors_p ()) \
366 result = __GCONV_ILLEGAL_INPUT; \
374 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
377 if (! ignore_errors_p ()) \
379 result = __GCONV_ILLEGAL_INPUT; \
391 if (! ignore_errors_p ()) \
393 result = __GCONV_ILLEGAL_INPUT; \
402 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
403 /* Almost done, just advance the input pointer. */ \
405 else if (set == JISX0201_Roman_set) \
407 /* Use the JIS X 0201 table. */ \
408 ch = jisx0201_to_ucs4 (ch); \
409 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
411 if (! ignore_errors_p ()) \
413 result = __GCONV_ILLEGAL_INPUT; \
423 else if (set == JISX0201_Kana_set) \
425 /* Use the JIS X 0201 table. */ \
426 ch = jisx0201_to_ucs4 (ch + 0x80); \
427 if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
429 if (! ignore_errors_p ()) \
431 result = __GCONV_ILLEGAL_INPUT; \
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); \
457 assert (set == KSC5601_set); \
459 /* Use the KSC 5601 table. */ \
460 ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0); \
463 if (__builtin_expect (ch, 1) == 0) \
465 result = __GCONV_EMPTY_INPUT; \
468 else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
470 if (! ignore_errors_p ()) \
472 result = __GCONV_ILLEGAL_INPUT; \
482 put32 (outptr, ch); \
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>
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
501 size_t written = 0; \
503 ch = get32 (inptr); \
505 /* First see whether we can write the character using the currently \
506 selected character set. */ \
507 if (set == ASCII_set) \
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. */ \
517 /* At the beginning of a line, G2 designation is cleared. */ \
518 if (var == iso2022jp2 && ch == 0x0a) \
519 set2 = UNSPECIFIED_set; \
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 \
526 else if (set == JISX0201_Roman_set) \
528 unsigned char buf[2]; \
529 written = ucs4_to_jisx0201 (ch, buf); \
530 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0x20 \
533 *outptr++ = buf[0]; \
537 written = __UNKNOWN_10646_CHAR; \
539 else if (set == JISX0201_Kana_set) \
541 unsigned char buf[2]; \
542 written = ucs4_to_jisx0201 (ch, buf); \
543 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0xa0 \
546 *outptr++ = buf[0] - 0x80; \
550 written = __UNKNOWN_10646_CHAR; \
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); \
562 assert (set == KSC5601_set); \
564 written = ucs4_to_ksc5601 (ch, outptr, outend - outptr); \
567 if (__builtin_expect (written, 1) == 0) \
569 result = __GCONV_FULL_OUTPUT; \
572 else if (written != __UNKNOWN_10646_CHAR) \
576 if (written == __UNKNOWN_10646_CHAR || written == 0) \
578 if (set2 == ISO88591_set) \
580 if (ch >= 0x80 && ch <= 0xff) \
584 *outptr++ = ch & 0x7f; \
588 else if (set2 == ISO88597_set) \
590 const struct gap *rp = from_idx; \
592 while (ch > rp->end) \
594 if (ch >= rp->start) \
596 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
608 if (written == __UNKNOWN_10646_CHAR || written == 0) \
610 /* Either this is an unknown character or we have to switch \
611 the currently selected character set. The character sets \
612 do not code entirely separate parts of ISO 10646 and \
613 therefore there is no single correct result. If we choose \
614 the character set to use wrong we might be end up with \
615 using yet another character set for the next character \
616 though the current and the next could be encoded with one \
617 character set. We leave this kind of optimization for \
618 later and now simply use a fixed order in which we test for \
623 /* We must encode using ASCII. First write out the \
624 escape sequence. */ \
625 if (__builtin_expect (outptr + 3 > outend, 0)) \
627 result = __GCONV_FULL_OUTPUT; \
636 if (__builtin_expect (outptr + 1 > outend, 0)) \
638 result = __GCONV_FULL_OUTPUT; \
643 /* At the beginning of a line, G2 designation is cleared. */ \
644 if (var == iso2022jp2 && ch == 0x0a) \
645 set2 = UNSPECIFIED_set; \
649 /* Now it becomes difficult. We must search the other \
650 character sets one by one and we cannot use simple \
651 arithmetic to determine whether the character can be \
652 encoded using this set. */ \
654 unsigned char buf[2]; \
656 written = ucs4_to_jisx0201 (ch, buf); \
657 if (written != __UNKNOWN_10646_CHAR && buf[0] < 0x80) \
659 /* We use JIS X 0201. */ \
660 if (__builtin_expect (outptr + 3 > outend, 0)) \
662 result = __GCONV_FULL_OUTPUT; \
669 set = JISX0201_Roman_set; \
671 if (__builtin_expect (outptr + 1 > outend, 0)) \
673 result = __GCONV_FULL_OUTPUT; \
676 *outptr++ = buf[0]; \
680 written = ucs4_to_jisx0208 (ch, buf, 2); \
681 if (written != __UNKNOWN_10646_CHAR) \
683 /* We use JIS X 0208. */ \
684 if (__builtin_expect (outptr + 3 > outend, 0)) \
686 result = __GCONV_FULL_OUTPUT; \
693 set = JISX0208_1983_set; \
695 if (__builtin_expect (outptr + 2 > outend, 0)) \
697 result = __GCONV_FULL_OUTPUT; \
700 *outptr++ = buf[0]; \
701 *outptr++ = buf[1]; \
703 else if (__builtin_expect (var, iso2022jp2) == iso2022jp) \
705 /* We have no other choice. */ \
706 if (step_data->__trans.__trans_fct != NULL) \
708 result = DL_CALL_FCT (step_data->__trans.__trans_fct, \
709 (step, step_data, *inptrp, \
710 &inptr, inend, *outptrp, \
713 if (result != __GCONV_OK) \
716 else if (! ignore_errors_p ()) \
718 result = __GCONV_ILLEGAL_INPUT; \
730 written = ucs4_to_jisx0212 (ch, buf, 2); \
731 if (written != __UNKNOWN_10646_CHAR) \
733 /* We use JIS X 0212. */ \
734 if (__builtin_expect (outptr + 4 > outend, 0)) \
736 result = __GCONV_FULL_OUTPUT; \
743 set = JISX0212_set; \
745 if (__builtin_expect (outptr + 2 > outend, 0)) \
747 result = __GCONV_FULL_OUTPUT; \
750 *outptr++ = buf[0]; \
751 *outptr++ = buf[1]; \
755 written = ucs4_to_jisx0201 (ch, buf); \
756 if (written != __UNKNOWN_10646_CHAR \
759 /* We use JIS X 0201. */ \
760 if (__builtin_expect (outptr + 3 > outend, 0)) \
762 result = __GCONV_FULL_OUTPUT; \
769 set = JISX0201_Kana_set; \
771 if (__builtin_expect (outptr + 1 > outend, 0)) \
773 result = __GCONV_FULL_OUTPUT; \
776 *outptr++ = buf[0] - 0x80; \
778 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
780 /* ISO 8859-1 upper half. */ \
781 if (__builtin_expect (outptr + 3 > outend, 0)) \
783 result = __GCONV_FULL_OUTPUT; \
790 set2 = ISO88591_set; \
792 if (__builtin_expect (outptr + 3 > outend, 0)) \
794 result = __GCONV_FULL_OUTPUT; \
803 written = ucs4_to_gb2312 (ch, buf, 2); \
804 if (written != __UNKNOWN_10646_CHAR) \
806 /* We use GB 2312. */ \
807 if (__builtin_expect (outptr + 3 > outend, 0))\
809 result = __GCONV_FULL_OUTPUT; \
818 if (__builtin_expect (outptr + 2 > outend, 0))\
820 result = __GCONV_FULL_OUTPUT; \
823 *outptr++ = buf[0]; \
824 *outptr++ = buf[1]; \
828 written = ucs4_to_ksc5601 (ch, buf, 2); \
829 if (written != __UNKNOWN_10646_CHAR) \
831 /* We use KSC 5601. */ \
832 if (__builtin_expect (outptr + 4 > outend,\
835 result = __GCONV_FULL_OUTPUT; \
844 if (__builtin_expect (outptr + 2 > outend,\
847 result = __GCONV_FULL_OUTPUT; \
850 *outptr++ = buf[0]; \
851 *outptr++ = buf[1]; \
855 const struct gap *rp = from_idx; \
856 unsigned char gch = 0; \
858 while (ch > rp->end) \
860 if (ch >= rp->start) \
862 ch = ch - 0xa0 + rp->idx; \
863 gch = iso88597_from_ucs4[ch]; \
866 if (__builtin_expect (gch, 1) != 0) \
868 /* We use ISO 8859-7 greek. */ \
869 if (__builtin_expect (outptr + 3 \
872 result = __GCONV_FULL_OUTPUT; \
878 set2 = ISO88597_set; \
880 if (__builtin_expect (outptr + 3 \
883 result = __GCONV_FULL_OUTPUT; \
892 if (step_data->__trans.__trans_fct \
895 result = DL_CALL_FCT \
896 (step_data->__trans.__trans_fct,\
897 (step, step_data, *inptrp, \
898 &inptr, inend, *outptrp, \
901 if (result != __GCONV_OK) \
904 else if (! ignore_errors_p ()) \
906 result = __GCONV_ILLEGAL_INPUT; \
925 /* Now that we wrote the output increment the input pointer. */ \
928 #define LOOP_NEED_FLAGS
929 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
930 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
931 int set2 = *setp & CURRENT_ASSIGN_MASK
932 #define UPDATE_PARAMS *setp = set | set2
933 #include <iconv/loop.c>
936 /* Now define the toplevel functions. */
937 #include <iconv/skeleton.c>