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 (dir
!= 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 (outbuf + 3 > data->__outbufend) \
212 /* We don't have enough room in the output buffer. */ \
213 status = __GCONV_FULL_OUTPUT; \
216 /* Write out the shift sequence. */ \
220 if (data->__is_last) \
222 data->__outbuf = outbuf; \
223 /* Note that this also clears the G2 designation. */ \
224 data->__statep->__count &= ~7; \
225 data->__statep->__count |= ASCII_set; \
231 /* Since we might have to reset input pointer we must be able to save
232 and retore the state. */
233 #define SAVE_RESET_STATE(Save) \
240 /* First define the conversion function from ISO-2022-JP to UCS4. */
241 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
242 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
243 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
244 #define LOOPFCT FROM_LOOP
247 uint32_t ch = *inptr; \
249 /* Recognize escape sequences. */ \
252 /* We now must be prepared to read two to three more \
253 chracters. If we have a match in the first character but \
254 then the input buffer ends we terminate with an error since \
255 we must not risk missing an escape sequence just because it \
256 is not entirely in the current input buffer. */ \
257 if (inptr + 2 >= inend \
258 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
259 && inptr + 3 >= inend)) \
261 /* Not enough input available. */ \
262 result = __GCONV_EMPTY_INPUT; \
266 if (inptr[1] == '(') \
268 if (inptr[2] == 'B') \
270 /* ASCII selected. */ \
275 else if (inptr[2] == 'J') \
277 /* JIS X 0201 selected. */ \
278 set = JISX0201_Roman_set; \
282 else if (var == iso2022jp2 && inptr[2] == 'I') \
284 /* JIS X 0201 selected. */ \
285 set = JISX0201_Kana_set; \
290 else if (inptr[1] == '$') \
292 if (inptr[2] == '@') \
294 /* JIS X 0208-1978 selected. */ \
295 set = JISX0208_1978_set; \
299 else if (inptr[2] == 'B') \
301 /* JIS X 0208-1983 selected. */ \
302 set = JISX0208_1983_set; \
306 else if (var == iso2022jp2) \
308 if (inptr[2] == 'A') \
310 /* GB 2312-1980 selected. */ \
315 else if (inptr[2] == '(') \
317 if (inptr[3] == 'C') \
319 /* KSC 5601-1987 selected. */ \
324 else if (inptr[3] == 'D') \
326 /* JIS X 0212-1990 selected. */ \
327 set = JISX0212_set; \
334 else if (var == iso2022jp2 && inptr[1] == '.') \
336 if (inptr[2] == 'A') \
338 /* ISO 8859-1-GR selected. */ \
339 set2 = ISO88591_set; \
343 else if (inptr[2] == 'F') \
345 /* ISO 8859-7-GR selected. */ \
346 set2 = ISO88597_set; \
353 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
355 if (set2 == ISO88591_set) \
357 ch = inptr[2] | 0x80; \
360 else if (set2 == ISO88597_set) \
362 /* We use the table from the ISO 8859-7 module. */ \
363 if (inptr[2] < 0x20 || inptr[2] > 0x80) \
365 result = __GCONV_ILLEGAL_INPUT; \
368 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
371 result = __GCONV_ILLEGAL_INPUT; \
378 result = __GCONV_ILLEGAL_INPUT; \
382 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
383 /* Almost done, just advance the input pointer. */ \
385 else if (set == JISX0201_Roman_set) \
387 /* Use the JIS X 0201 table. */ \
388 ch = jisx0201_to_ucs4 (ch); \
389 if (ch == __UNKNOWN_10646_CHAR) \
391 result = __GCONV_ILLEGAL_INPUT; \
396 else if (set == JISX0201_Kana_set) \
398 /* Use the JIS X 0201 table. */ \
399 ch = jisx0201_to_ucs4 (ch + 0x80); \
400 if (ch == __UNKNOWN_10646_CHAR) \
402 result = __GCONV_ILLEGAL_INPUT; \
409 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
410 /* XXX I don't have the tables for these two old variants of \
411 JIS X 0208. Therefore I'm using the tables for JIS X \
412 0208-1990. If somebody has problems with this please \
413 provide the appropriate tables. */ \
414 ch = jisx0208_to_ucs4 (&inptr, \
415 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
416 else if (set == JISX0212_set) \
417 /* Use the JIS X 0212 table. */ \
418 ch = jisx0212_to_ucs4 (&inptr, \
419 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
420 else if (set == GB2312_set) \
421 /* Use the GB 2312 table. */ \
422 ch = gb2312_to_ucs4 (&inptr, \
423 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
426 assert (set == KSC5601_set); \
428 /* Use the KSC 5601 table. */ \
429 ch = ksc5601_to_ucs4 (&inptr, \
430 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
433 if (NEED_LENGTH_TEST && ch == 0) \
435 result = __GCONV_EMPTY_INPUT; \
438 else if (ch == __UNKNOWN_10646_CHAR) \
440 result = __GCONV_ILLEGAL_INPUT; \
445 put32 (outptr, ch); \
448 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
449 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
450 int set2 = *setp & CURRENT_ASSIGN_MASK
451 #define UPDATE_PARAMS *setp = set | set2
452 #include <iconv/loop.c>
455 /* Next, define the other direction. */
456 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
457 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
458 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
459 #define LOOPFCT TO_LOOP
463 size_t written = 0; \
465 ch = get32 (inptr); \
467 /* First see whether we can write the character using the currently \
468 selected character set. */ \
469 if (set == ASCII_set) \
471 /* Please note that the NUL byte is *not* matched if we are not \
472 currently using the ASCII charset. This is because we must \
473 switch to the initial state whenever a NUL byte is written. */ \
479 /* At the beginning of a line, G2 designation is cleared. */ \
480 if (var == iso2022jp2 && ch == 0x0a) \
481 set2 = UNSPECIFIED_set; \
483 /* ISO-2022-JP recommends to encode the newline character always in \
484 ASCII since this allows a context-free interpretation of the \
485 characters at the beginning of the next line. Otherwise it would \
486 have to be known whether the last line ended using ASCII or \
488 else if (set == JISX0201_Roman_set) \
490 unsigned char buf[2]; \
491 written = ucs4_to_jisx0201 (ch, buf); \
492 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0x20 \
495 *outptr++ = buf[0]; \
499 written = __UNKNOWN_10646_CHAR; \
501 else if (set == JISX0201_Kana_set) \
503 unsigned char buf[2]; \
504 written = ucs4_to_jisx0201 (ch, buf); \
505 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0xa0 \
508 *outptr++ = buf[0] - 0x80; \
512 written = __UNKNOWN_10646_CHAR; \
516 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
517 written = ucs4_to_jisx0208 (ch, outptr, \
519 ? outend - outptr : 2)); \
520 else if (set == JISX0212_set) \
521 written = ucs4_to_jisx0212 (ch, outptr, \
523 ? outend - outptr : 2)); \
524 else if (set == GB2312_set) \
525 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
526 ? outend - outptr : 2)); \
529 assert (set == KSC5601_set); \
531 written = ucs4_to_ksc5601 (ch, outptr, \
533 ? outend - outptr : 2)); \
536 if (NEED_LENGTH_TEST && written == 0) \
538 result = __GCONV_FULL_OUTPUT; \
541 else if (written != __UNKNOWN_10646_CHAR) \
545 if (written == __UNKNOWN_10646_CHAR || written == 0) \
547 if (set2 == ISO88591_set) \
549 if (ch >= 0x80 && ch <= 0xff) \
553 *outptr++ = ch & 0x7f; \
557 else if (set2 == ISO88597_set) \
559 const struct gap *rp = from_idx; \
561 while (ch > rp->end) \
563 if (ch >= rp->start) \
565 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
577 if (written == __UNKNOWN_10646_CHAR || written == 0) \
579 /* Either this is an unknown character or we have to switch \
580 the currently selected character set. The character sets \
581 do not code entirely separate parts of ISO 10646 and \
582 therefore there is no single correct result. If we choose \
583 the character set to use wrong we might be end up with \
584 using yet another character set for the next character \
585 though the current and the next could be encoded with one \
586 character set. We leave this kind of optimization for \
587 later and now simply use a fixed order in which we test for \
592 /* We must encode using ASCII. First write out the \
593 escape sequence. */ \
594 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
596 result = __GCONV_FULL_OUTPUT; \
605 if (NEED_LENGTH_TEST && outptr + 1 > outend) \
607 result = __GCONV_FULL_OUTPUT; \
612 /* At the beginning of a line, G2 designation is cleared. */ \
613 if (var == iso2022jp2 && ch == 0x0a) \
614 set2 = UNSPECIFIED_set; \
618 /* Now it becomes difficult. We must search the other \
619 character sets one by one and we cannot use simple \
620 arithmetic to determine whether the character can be \
621 encoded using this set. */ \
623 unsigned char buf[2]; \
625 written = ucs4_to_jisx0201 (ch, buf); \
626 if (written != __UNKNOWN_10646_CHAR && buf[0] < 0x80) \
628 /* We use JIS X 0201. */ \
629 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
631 result = __GCONV_FULL_OUTPUT; \
638 set = JISX0201_Roman_set; \
640 if (NEED_LENGTH_TEST && outptr + 1 > outend) \
642 result = __GCONV_FULL_OUTPUT; \
645 *outptr++ = buf[0]; \
649 written = ucs4_to_jisx0208 (ch, buf, 2); \
650 if (written != __UNKNOWN_10646_CHAR) \
652 /* We use JIS X 0208. */ \
653 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
655 result = __GCONV_FULL_OUTPUT; \
662 set = JISX0208_1983_set; \
664 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
666 result = __GCONV_FULL_OUTPUT; \
669 *outptr++ = buf[0]; \
670 *outptr++ = buf[1]; \
672 else if (var == iso2022jp) \
674 /* We have no other choice. */ \
675 result = __GCONV_ILLEGAL_INPUT; \
680 written = ucs4_to_jisx0212 (ch, buf, 2); \
681 if (written != __UNKNOWN_10646_CHAR) \
683 /* We use JIS X 0212. */ \
684 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
686 result = __GCONV_FULL_OUTPUT; \
693 set = JISX0212_set; \
695 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
697 result = __GCONV_FULL_OUTPUT; \
700 *outptr++ = buf[0]; \
701 *outptr++ = buf[1]; \
705 written = ucs4_to_jisx0201 (ch, buf); \
706 if (written != __UNKNOWN_10646_CHAR \
709 /* We use JIS X 0201. */ \
710 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
712 result = __GCONV_FULL_OUTPUT; \
719 set = JISX0201_Kana_set; \
721 if (NEED_LENGTH_TEST && outptr + 1 > outend) \
723 result = __GCONV_FULL_OUTPUT; \
726 *outptr++ = buf[0] - 0x80; \
728 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
730 /* ISO 8859-1 upper half. */ \
731 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
733 result = __GCONV_FULL_OUTPUT; \
740 set2 = ISO88591_set; \
742 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
744 result = __GCONV_FULL_OUTPUT; \
753 written = ucs4_to_gb2312 (ch, buf, 2); \
754 if (written != __UNKNOWN_10646_CHAR) \
756 /* We use GB 2312. */ \
757 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
759 result = __GCONV_FULL_OUTPUT; \
768 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
770 result = __GCONV_FULL_OUTPUT; \
773 *outptr++ = buf[0]; \
774 *outptr++ = buf[1]; \
778 written = ucs4_to_ksc5601 (ch, buf, 2); \
779 if (written != __UNKNOWN_10646_CHAR) \
781 /* We use KSC 5601. */ \
782 if (NEED_LENGTH_TEST \
783 && outptr + 4 > outend) \
785 result = __GCONV_FULL_OUTPUT; \
794 if (NEED_LENGTH_TEST \
795 && outptr + 2 > outend) \
797 result = __GCONV_FULL_OUTPUT; \
800 *outptr++ = buf[0]; \
801 *outptr++ = buf[1]; \
805 const struct gap *rp = from_idx; \
806 unsigned char gch = 0; \
808 while (ch > rp->end) \
810 if (ch >= rp->start) \
812 ch = ch - 0xa0 + rp->idx; \
813 gch = iso88597_from_ucs4[ch]; \
818 /* We use ISO 8859-7 greek. */ \
819 if (NEED_LENGTH_TEST \
820 && outptr + 3 > outend) \
822 result = __GCONV_FULL_OUTPUT; \
828 set2 = ISO88597_set; \
830 if (NEED_LENGTH_TEST \
831 && outptr + 3 > outend) \
833 result = __GCONV_FULL_OUTPUT; \
842 result = __GCONV_ILLEGAL_INPUT; \
854 /* Now that we wrote the output increment the input pointer. */ \
857 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
858 #define INIT_PARAMS int set = *setp & CURRENT_SEL_MASK; \
859 int set2 = *setp & CURRENT_ASSIGN_MASK
860 #define UPDATE_PARAMS *setp = set | set2
861 #include <iconv/loop.c>
864 /* Now define the toplevel functions. */
865 #include <iconv/skeleton.c>