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: */
101 /* The second value stored is the designation of the G2 set. The following
102 values are possible: */
112 gconv_init (struct __gconv_step
*step
)
114 /* Determine which direction. */
115 struct iso2022jp_data
*new_data
;
116 enum direction dir
= illegal_dir
;
117 enum variant var
= illegal_var
;
120 if (__strcasecmp (step
->__from_name
, "ISO-2022-JP//") == 0)
122 dir
= from_iso2022jp
;
125 else if (__strcasecmp (step
->__to_name
, "ISO-2022-JP//") == 0)
130 else if (__strcasecmp (step
->__from_name
, "ISO-2022-JP-2//") == 0)
132 dir
= from_iso2022jp
;
135 else if (__strcasecmp (step
->__to_name
, "ISO-2022-JP-2//") == 0)
141 result
= __GCONV_NOCONV
;
142 if (dir
!= illegal_dir
)
145 = (struct iso2022jp_data
*) malloc (sizeof (struct iso2022jp_data
));
147 result
= __GCONV_NOMEM
;
148 if (new_data
!= NULL
)
152 step
->__data
= new_data
;
154 if (dir
== from_iso2022jp
)
156 step
->__min_needed_from
= MIN_NEEDED_FROM
;
157 step
->__max_needed_from
= MAX_NEEDED_FROM
;
158 step
->__min_needed_to
= MIN_NEEDED_TO
;
159 step
->__max_needed_to
= MAX_NEEDED_TO
;
163 step
->__min_needed_from
= MIN_NEEDED_TO
;
164 step
->__max_needed_from
= MAX_NEEDED_TO
;
165 step
->__min_needed_to
= MIN_NEEDED_FROM
;
166 step
->__max_needed_to
= MAX_NEEDED_FROM
+ 2;
169 /* Yes, this is a stateful encoding. */
170 step
->__stateful
= 1;
181 gconv_end (struct __gconv_step
*data
)
187 /* Since this is a stateful encoding we have to provide code which resets
188 the output state to the initial state. This has to be done during the
190 #define EMIT_SHIFT_TO_INIT \
191 if (data->__statep->count != ASCII_set) \
193 enum direction dir = ((struct iso2022jp_data *) step->__data)->dir; \
195 if (dir == from_iso2022jp) \
196 /* It's easy, we don't have to emit anything, we just reset the \
197 state for the input. Note that this also clears the G2 \
199 data->__statep->count = ASCII_set; \
202 unsigned char *outbuf = data->__outbuf; \
204 /* We are not in the initial state. To switch back we have \
205 to emit the sequence `Esc ( B'. */ \
206 if (outbuf + 3 > data->__outbufend) \
207 /* We don't have enough room in the output buffer. */ \
208 status = __GCONV_FULL_OUTPUT; \
211 /* Write out the shift sequence. */ \
215 if (data->__is_last) \
217 data->__outbuf = outbuf; \
218 /* Note that this also clears the G2 designation. */ \
219 data->__statep->count = ASCII_set; \
225 /* Since we might have to reset input pointer we must be able to save
226 and retore the state. */
227 #define SAVE_RESET_STATE(Save) \
234 /* First define the conversion function from ISO-2022-JP to UCS4. */
235 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
236 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
237 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
238 #define LOOPFCT FROM_LOOP
241 uint32_t ch = *inptr; \
243 /* Recognize escape sequences. */ \
246 /* We now must be prepared to read two to three more \
247 chracters. If we have a match in the first character but \
248 then the input buffer ends we terminate with an error since \
249 we must not risk missing an escape sequence just because it \
250 is not entirely in the current input buffer. */ \
251 if (inptr + 2 >= inend \
252 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
253 && inptr + 3 >= inend)) \
255 /* Not enough input available. */ \
256 result = __GCONV_EMPTY_INPUT; \
260 if (inptr[1] == '(') \
262 if (inptr[2] == 'B') \
264 /* ASCII selected. */ \
269 else if (inptr[2] == 'J') \
271 /* JIS X 0201 selected. */ \
272 set = JISX0201_Roman_set; \
276 else if (var == iso2022jp2 && inptr[2] == 'I') \
278 /* JIS X 0201 selected. */ \
279 set = JISX0201_Kana_set; \
284 else if (inptr[1] == '$') \
286 if (inptr[2] == '@') \
288 /* JIS X 0208-1978 selected. */ \
289 set = JISX0208_1978_set; \
293 else if (inptr[2] == 'B') \
295 /* JIS X 0208-1983 selected. */ \
296 set = JISX0208_1983_set; \
300 else if (var == iso2022jp2) \
302 if (inptr[2] == 'A') \
304 /* GB 2312-1980 selected. */ \
309 else if (inptr[2] == '(') \
311 if (inptr[3] == 'C') \
313 /* KSC 5601-1987 selected. */ \
318 else if (inptr[3] == 'D') \
320 /* JIS X 0212-1990 selected. */ \
321 set = JISX0212_set; \
328 else if (var == iso2022jp2 && inptr[1] == '.') \
330 if (inptr[2] == 'A') \
332 /* ISO 8859-1-GR selected. */ \
333 set2 = ISO88591_set; \
337 else if (inptr[2] == 'F') \
339 /* ISO 8859-7-GR selected. */ \
340 set2 = ISO88597_set; \
347 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
349 if (set2 == ISO88591_set) \
351 ch = inptr[2] | 0x80; \
354 else if (set2 == ISO88597_set) \
356 /* We use the table from the ISO 8859-7 module. */ \
357 if (inptr[2] < 0x20 || inptr[2] > 0x80) \
359 result = __GCONV_ILLEGAL_INPUT; \
362 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
365 result = __GCONV_ILLEGAL_INPUT; \
372 result = __GCONV_ILLEGAL_INPUT; \
376 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
377 /* Almost done, just advance the input pointer. */ \
379 else if (set == JISX0201_Roman_set) \
381 /* Use the JIS X 0201 table. */ \
382 ch = jisx0201_to_ucs4 (ch); \
383 if (ch == __UNKNOWN_10646_CHAR) \
385 result = __GCONV_ILLEGAL_INPUT; \
390 else if (set == JISX0201_Kana_set) \
392 /* Use the JIS X 0201 table. */ \
393 ch = jisx0201_to_ucs4 (ch + 0x80); \
394 if (ch == __UNKNOWN_10646_CHAR) \
396 result = __GCONV_ILLEGAL_INPUT; \
403 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
404 /* XXX I don't have the tables for these two old variants of \
405 JIS X 0208. Therefore I'm using the tables for JIS X \
406 0208-1990. If somebody has problems with this please \
407 provide the appropriate tables. */ \
408 ch = jisx0208_to_ucs4 (&inptr, \
409 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
410 else if (set == JISX0212_set) \
411 /* Use the JIS X 0212 table. */ \
412 ch = jisx0212_to_ucs4 (&inptr, \
413 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
414 else if (set == GB2312_set) \
415 /* Use the GB 2312 table. */ \
416 ch = gb2312_to_ucs4 (&inptr, \
417 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
420 assert (set == KSC5601_set); \
422 /* Use the KSC 5601 table. */ \
423 ch = ksc5601_to_ucs4 (&inptr, \
424 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
427 if (NEED_LENGTH_TEST && ch == 0) \
429 result = __GCONV_EMPTY_INPUT; \
432 else if (ch == __UNKNOWN_10646_CHAR) \
434 result = __GCONV_ILLEGAL_INPUT; \
439 put32 (outptr, ch); \
442 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
443 #define INIT_PARAMS int set = *setp % 0x100, set2 = *setp / 0x100
444 #define UPDATE_PARAMS *setp = (set2 << 8) + set
445 #include <iconv/loop.c>
448 /* Next, define the other direction. */
449 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
450 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
451 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
452 #define LOOPFCT TO_LOOP
456 size_t written = 0; \
458 ch = get32 (inptr); \
460 /* First see whether we can write the character using the currently \
461 selected character set. */ \
462 if (set == ASCII_set) \
464 /* Please note that the NUL byte is *not* matched if we are not \
465 currently using the ASCII charset. This is because we must \
466 switch to the initial state whenever a NUL byte is written. */ \
472 /* At the beginning of a line, G2 designation is cleared. */ \
473 if (var == iso2022jp2 && ch == 0x0a) \
474 set2 = UNSPECIFIED_set; \
476 /* ISO-2022-JP recommends to encode the newline character always in \
477 ASCII since this allows a context-free interpretation of the \
478 characters at the beginning of the next line. Otherwise it would \
479 have to be known whether the last line ended using ASCII or \
481 else if (set == JISX0201_Roman_set) \
483 unsigned char buf[2]; \
484 written = ucs4_to_jisx0201 (ch, buf); \
485 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0x20 \
488 *outptr++ = buf[0]; \
492 written = __UNKNOWN_10646_CHAR; \
494 else if (set == JISX0201_Kana_set) \
496 unsigned char buf[2]; \
497 written = ucs4_to_jisx0201 (ch, buf); \
498 if (written != __UNKNOWN_10646_CHAR && buf[0] > 0xa0 \
501 *outptr++ = buf[0] - 0x80; \
505 written = __UNKNOWN_10646_CHAR; \
509 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
510 written = ucs4_to_jisx0208 (ch, outptr, \
512 ? outend - outptr : 2)); \
513 else if (set == JISX0212_set) \
514 written = ucs4_to_jisx0212 (ch, outptr, \
516 ? outend - outptr : 2)); \
517 else if (set == GB2312_set) \
518 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
519 ? outend - outptr : 2)); \
522 assert (set == KSC5601_set); \
524 written = ucs4_to_ksc5601 (ch, outptr, \
526 ? outend - outptr : 2)); \
529 if (NEED_LENGTH_TEST && written == 0) \
531 result = __GCONV_FULL_OUTPUT; \
534 else if (written != __UNKNOWN_10646_CHAR) \
538 if (written == __UNKNOWN_10646_CHAR || written == 0) \
540 if (set2 == ISO88591_set) \
542 if (ch >= 0x80 && ch <= 0xff) \
546 *outptr++ = ch & 0x7f; \
550 else if (set2 == ISO88597_set) \
552 const struct gap *rp = from_idx; \
554 while (ch > rp->end) \
556 if (ch >= rp->start) \
558 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
570 if (written == __UNKNOWN_10646_CHAR || written == 0) \
572 /* Either this is an unknown character or we have to switch \
573 the currently selected character set. The character sets \
574 do not code entirely separate parts of ISO 10646 and \
575 therefore there is no single correct result. If we choose \
576 the character set to use wrong we might be end up with \
577 using yet another character set for the next character \
578 though the current and the next could be encoded with one \
579 character set. We leave this kind of optimization for \
580 later and now simply use a fixed order in which we test for \
585 /* We must encode using ASCII. First write out the \
586 escape sequence. */ \
587 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
589 result = __GCONV_FULL_OUTPUT; \
598 if (NEED_LENGTH_TEST && outptr + 1 > outend) \
600 result = __GCONV_FULL_OUTPUT; \
605 /* At the beginning of a line, G2 designation is cleared. */ \
606 if (var == iso2022jp2 && ch == 0x0a) \
607 set2 = UNSPECIFIED_set; \
611 /* Now it becomes difficult. We must search the other \
612 character sets one by one and we cannot use simple \
613 arithmetic to determine whether the character can be \
614 encoded using this set. */ \
616 unsigned char buf[2]; \
618 written = ucs4_to_jisx0201 (ch, buf); \
619 if (written != __UNKNOWN_10646_CHAR && buf[0] < 0x80) \
621 /* We use JIS X 0201. */ \
622 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
624 result = __GCONV_FULL_OUTPUT; \
631 set = JISX0201_Roman_set; \
633 if (NEED_LENGTH_TEST && outptr + 1 > outend) \
635 result = __GCONV_FULL_OUTPUT; \
638 *outptr++ = buf[0]; \
642 written = ucs4_to_jisx0208 (ch, buf, 2); \
643 if (written != __UNKNOWN_10646_CHAR) \
645 /* We use JIS X 0208. */ \
646 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
648 result = __GCONV_FULL_OUTPUT; \
655 set = JISX0208_1983_set; \
657 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
659 result = __GCONV_FULL_OUTPUT; \
662 *outptr++ = buf[0]; \
663 *outptr++ = buf[1]; \
665 else if (var == iso2022jp) \
667 /* We have no other choice. */ \
668 result = __GCONV_ILLEGAL_INPUT; \
673 written = ucs4_to_jisx0212 (ch, buf, 2); \
674 if (written != __UNKNOWN_10646_CHAR) \
676 /* We use JIS X 0212. */ \
677 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
679 result = __GCONV_FULL_OUTPUT; \
686 set = JISX0212_set; \
688 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
690 result = __GCONV_FULL_OUTPUT; \
693 *outptr++ = buf[0]; \
694 *outptr++ = buf[1]; \
698 written = ucs4_to_jisx0201 (ch, buf); \
699 if (written != __UNKNOWN_10646_CHAR \
702 /* We use JIS X 0201. */ \
703 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
705 result = __GCONV_FULL_OUTPUT; \
712 set = JISX0201_Kana_set; \
714 if (NEED_LENGTH_TEST && outptr + 1 > outend) \
716 result = __GCONV_FULL_OUTPUT; \
719 *outptr++ = buf[0] - 0x80; \
721 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
723 /* ISO 8859-1 upper half. */ \
724 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
726 result = __GCONV_FULL_OUTPUT; \
733 set2 = ISO88591_set; \
735 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
737 result = __GCONV_FULL_OUTPUT; \
746 written = ucs4_to_gb2312 (ch, buf, 2); \
747 if (written != __UNKNOWN_10646_CHAR) \
749 /* We use GB 2312. */ \
750 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
752 result = __GCONV_FULL_OUTPUT; \
761 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
763 result = __GCONV_FULL_OUTPUT; \
766 *outptr++ = buf[0]; \
767 *outptr++ = buf[1]; \
771 written = ucs4_to_ksc5601 (ch, buf, 2); \
772 if (written != __UNKNOWN_10646_CHAR) \
774 /* We use KSC 5601. */ \
775 if (NEED_LENGTH_TEST \
776 && outptr + 4 > outend) \
778 result = __GCONV_FULL_OUTPUT; \
787 if (NEED_LENGTH_TEST \
788 && outptr + 2 > outend) \
790 result = __GCONV_FULL_OUTPUT; \
793 *outptr++ = buf[0]; \
794 *outptr++ = buf[1]; \
798 const struct gap *rp = from_idx; \
799 unsigned char gch = 0; \
801 while (ch > rp->end) \
803 if (ch >= rp->start) \
805 ch = ch - 0xa0 + rp->idx; \
806 gch = iso88597_from_ucs4[ch]; \
811 /* We use ISO 8859-7 greek. */ \
812 if (NEED_LENGTH_TEST \
813 && outptr + 3 > outend) \
815 result = __GCONV_FULL_OUTPUT; \
821 set2 = ISO88597_set; \
823 if (NEED_LENGTH_TEST \
824 && outptr + 3 > outend) \
826 result = __GCONV_FULL_OUTPUT; \
835 result = __GCONV_ILLEGAL_INPUT; \
847 /* Now that we wrote the output increment the input pointer. */ \
850 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
851 #define INIT_PARAMS int set = *setp % 0x100, set2 = *setp / 0x100
852 #define UPDATE_PARAMS *setp = (set2 << 8) + set
853 #include <iconv/loop.c>
856 /* Now define the toplevel functions. */
857 #include <iconv/skeleton.c>