1 /* Conversion module for ISO-2022-JP.
2 Copyright (C) 1998, 1999 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. */
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 data->outbuf = outbuf; \
216 /* Note that this also clears the G2 designation. */ \
217 data->statep->count = ASCII_set; \
223 /* Since we might have to reset input pointer we must be able to save
224 and retore the state. */
225 #define SAVE_RESET_STATE(Save) \
232 /* First define the conversion function from ISO-2022-JP to UCS4. */
233 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
234 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
235 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
236 #define LOOPFCT FROM_LOOP
239 uint32_t ch = *inptr; \
241 /* Recognize escape sequences. */ \
244 /* We now must be prepared to read two to three more \
245 chracters. If we have a match in the first character but \
246 then the input buffer ends we terminate with an error since \
247 we must not risk missing an escape sequence just because it \
248 is not entirely in the current input buffer. */ \
249 if (inptr + 2 >= inend \
250 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
251 && inptr + 3 >= inend)) \
253 /* Not enough input available. */ \
254 result = GCONV_EMPTY_INPUT; \
258 if (inptr[1] == '(') \
260 if (inptr[2] == 'B') \
262 /* ASCII selected. */ \
267 else if (inptr[2] == 'J') \
269 /* JIS X 0201 selected. */ \
270 set = JISX0201_Roman_set; \
274 else if (var == iso2022jp2 && inptr[2] == 'I') \
276 /* JIS X 0201 selected. */ \
277 set = JISX0201_Kana_set; \
282 else if (inptr[1] == '$') \
284 if (inptr[2] == '@') \
286 /* JIS X 0208-1978 selected. */ \
287 set = JISX0208_1978_set; \
291 else if (inptr[2] == 'B') \
293 /* JIS X 0208-1983 selected. */ \
294 set = JISX0208_1983_set; \
298 else if (var == iso2022jp2) \
300 if (inptr[2] == 'A') \
302 /* GB 2312-1980 selected. */ \
307 else if (inptr[2] == '(') \
309 if (inptr[3] == 'C') \
311 /* KSC 5601-1987 selected. */ \
316 else if (inptr[3] == 'D') \
318 /* JIS X 0212-1990 selected. */ \
319 set = JISX0212_set; \
326 else if (var == iso2022jp2 && inptr[1] == '.') \
328 if (inptr[2] == 'A') \
330 /* ISO 8859-1-GR selected. */ \
331 set2 = ISO88591_set; \
335 else if (inptr[2] == 'F') \
337 /* ISO 8859-7-GR selected. */ \
338 set2 = ISO88597_set; \
345 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
347 if (set2 == ISO88591_set) \
349 ch = inptr[2] | 0x80; \
352 else if (set2 == ISO88597_set) \
354 /* We use the table from the ISO 8859-7 module. */ \
355 if (inptr[2] < 0x20 || inptr[2] > 0x80) \
357 result = GCONV_ILLEGAL_INPUT; \
360 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
363 result = GCONV_ILLEGAL_INPUT; \
370 result = GCONV_ILLEGAL_INPUT; \
374 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
375 /* Almost done, just advance the input pointer. */ \
377 else if (set == JISX0201_Roman_set) \
379 /* Use the JIS X 0201 table. */ \
380 ch = jisx0201_to_ucs4 (ch); \
381 if (ch == UNKNOWN_10646_CHAR) \
383 result = GCONV_ILLEGAL_INPUT; \
388 else if (set == JISX0201_Kana_set) \
390 /* Use the JIS X 0201 table. */ \
391 ch = jisx0201_to_ucs4 (ch + 0x80); \
392 if (ch == UNKNOWN_10646_CHAR) \
394 result = GCONV_ILLEGAL_INPUT; \
401 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
402 /* XXX I don't have the tables for these two old variants of \
403 JIS X 0208. Therefore I'm using the tables for JIS X \
404 0208-1990. If somebody has problems with this please \
405 provide the appropriate tables. */ \
406 ch = jisx0208_to_ucs4 (&inptr, \
407 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
408 else if (set == JISX0212_set) \
409 /* Use the JIS X 0212 table. */ \
410 ch = jisx0212_to_ucs4 (&inptr, \
411 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
412 else if (set == GB2312_set) \
413 /* Use the GB 2312 table. */ \
414 ch = gb2312_to_ucs4 (&inptr, \
415 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
418 assert (set == KSC5601_set); \
420 /* Use the KSC 5601 table. */ \
421 ch = ksc5601_to_ucs4 (&inptr, \
422 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
425 if (NEED_LENGTH_TEST && ch == 0) \
427 result = GCONV_EMPTY_INPUT; \
430 else if (ch == UNKNOWN_10646_CHAR) \
432 result = GCONV_ILLEGAL_INPUT; \
437 *((uint32_t *) outptr)++ = ch; \
439 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
440 #define INIT_PARAMS int set = *setp % 0x100, set2 = *setp / 0x100
441 #define UPDATE_PARAMS *setp = (set2 << 8) + set
442 #include <iconv/loop.c>
445 /* Next, define the other direction. */
446 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
447 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
448 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
449 #define LOOPFCT TO_LOOP
453 size_t written = 0; \
455 ch = *((uint32_t *) inptr); \
457 /* First see whether we can write the character using the currently \
458 selected character set. */ \
459 if (set == ASCII_set) \
461 /* Please note that the NUL byte is *not* matched if we are not \
462 currently using the ASCII charset. This is because we must \
463 switch to the initial state whenever a NUL byte is written. */ \
469 /* At the beginning of a line, G2 designation is cleared. */ \
470 if (var == iso2022jp2 && ch == 0x0a) \
471 set2 = UNSPECIFIED_set; \
473 else if (set == JISX0201_Roman_set) \
475 unsigned char buf[2]; \
476 written = ucs4_to_jisx0201 (ch, buf); \
477 if (written != UNKNOWN_10646_CHAR && buf[0] > 0x20 && buf[0] < 0x80) \
479 *outptr++ = buf[0]; \
483 written = UNKNOWN_10646_CHAR; \
485 else if (set == JISX0201_Kana_set) \
487 unsigned char buf[2]; \
488 written = ucs4_to_jisx0201 (ch, buf); \
489 if (written != UNKNOWN_10646_CHAR && buf[0] > 0xa0 && buf[0] < 0xe0) \
491 *outptr++ = buf[0] - 0x80; \
495 written = UNKNOWN_10646_CHAR; \
499 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
500 written = ucs4_to_jisx0208 (ch, outptr, \
502 ? outend - outptr : 2)); \
503 else if (set == JISX0212_set) \
504 written = ucs4_to_jisx0212 (ch, outptr, \
506 ? outend - outptr : 2)); \
507 else if (set == GB2312_set) \
508 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
509 ? outend - outptr : 2)); \
512 assert (set == KSC5601_set); \
514 written = ucs4_to_ksc5601 (ch, outptr, \
516 ? outend - outptr : 2)); \
519 if (NEED_LENGTH_TEST && written == 0) \
521 result = GCONV_FULL_OUTPUT; \
524 else if (written != UNKNOWN_10646_CHAR) \
528 if (written == UNKNOWN_10646_CHAR || written == 0) \
530 if (set2 == ISO88591_set) \
532 if (ch >= 0x80 && ch <= 0xff) \
536 *outptr++ = ch & 0x7f; \
540 else if (set2 == ISO88597_set) \
542 const struct gap *rp = from_idx; \
544 while (ch > rp->end) \
546 if (ch >= rp->start) \
548 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
560 if (written == UNKNOWN_10646_CHAR || written == 0) \
562 /* Either this is an unknown character or we have to switch \
563 the currently selected character set. The character sets \
564 do not code entirely separate parts of ISO 10646 and \
565 therefore there is no single correct result. If we choose \
566 the character set to use wrong we might be end up with \
567 using yet another character set for the next character \
568 though the current and the next could be encoded with one \
569 character set. We leave this kind of optimization for \
570 later and now simply use a fixed order in which we test for \
575 /* We must encode using ASCII. First write out the \
576 escape sequence. */ \
577 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
579 result = GCONV_FULL_OUTPUT; \
589 /* At the beginning of a line, G2 designation is cleared. */ \
590 if (var == iso2022jp2 && ch == 0x0a) \
591 set2 = UNSPECIFIED_set; \
595 /* Now it becomes difficult. We must search the other \
596 character sets one by one and we cannot use simple \
597 arithmetic to determine whether the character can be \
598 encoded using this set. */ \
600 unsigned char buf[2]; \
602 written = ucs4_to_jisx0201 (ch, buf); \
603 if (written != UNKNOWN_10646_CHAR && buf[0] < 0x80) \
605 /* We use JIS X 0201. */ \
606 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
608 result = GCONV_FULL_OUTPUT; \
615 set = JISX0201_Roman_set; \
616 *outptr++ = buf[0]; \
620 written = ucs4_to_jisx0208 (ch, buf, 2); \
621 if (written != UNKNOWN_10646_CHAR) \
623 /* We use JIS X 0208. */ \
624 if (NEED_LENGTH_TEST && outptr + 5 > outend) \
626 result = GCONV_FULL_OUTPUT; \
633 set = JISX0208_1983_set; \
634 *outptr++ = buf[0]; \
635 *outptr++ = buf[1]; \
637 else if (var == iso2022jp) \
639 /* We have no other choice. */ \
640 result = GCONV_ILLEGAL_INPUT; \
645 written = ucs4_to_jisx0212 (ch, buf, 2); \
646 if (written != UNKNOWN_10646_CHAR) \
648 /* We use JIS X 0212. */ \
649 if (NEED_LENGTH_TEST && outptr + 6 > outend) \
651 result = GCONV_FULL_OUTPUT; \
658 set = JISX0212_set; \
659 *outptr++ = buf[0]; \
660 *outptr++ = buf[1]; \
664 written = ucs4_to_jisx0201 (ch, buf); \
665 if (written != UNKNOWN_10646_CHAR && buf[0] >= 0x80) \
667 /* We use JIS X 0201. */ \
668 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
670 result = GCONV_FULL_OUTPUT; \
677 set = JISX0201_Kana_set; \
678 *outptr++ = buf[0] - 0x80; \
680 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
682 /* ISO 8859-1 upper half. */ \
683 if (NEED_LENGTH_TEST && outptr + 6 > outend) \
685 result = GCONV_FULL_OUTPUT; \
692 set2 = ISO88591_set; \
699 written = ucs4_to_gb2312 (ch, buf, 2); \
700 if (written != UNKNOWN_10646_CHAR) \
702 /* We use GB 2312. */ \
703 if (NEED_LENGTH_TEST && outptr + 5 > outend) \
705 result = GCONV_FULL_OUTPUT; \
713 *outptr++ = buf[0]; \
714 *outptr++ = buf[1]; \
718 written = ucs4_to_ksc5601 (ch, buf, 2); \
719 if (written != UNKNOWN_10646_CHAR) \
721 /* We use KSC 5601. */ \
722 if (NEED_LENGTH_TEST \
723 && outptr + 6 > outend) \
725 result = GCONV_FULL_OUTPUT; \
733 *outptr++ = buf[0]; \
734 *outptr++ = buf[1]; \
738 const struct gap *rp = from_idx; \
739 unsigned char gch = 0; \
741 while (ch > rp->end) \
743 if (ch >= rp->start) \
745 ch = ch - 0xa0 + rp->idx; \
746 gch = iso88597_from_ucs4[ch]; \
751 /* We use ISO 8859-7 greek. */ \
752 if (NEED_LENGTH_TEST \
753 && outptr + 6 > outend) \
755 result = GCONV_FULL_OUTPUT; \
761 set2 = ISO88597_set; \
768 result = GCONV_ILLEGAL_INPUT; \
780 /* Now that we wrote the output increment the input pointer. */ \
783 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
784 #define INIT_PARAMS int set = *setp % 0x100, set2 = *setp / 0x100
785 #define UPDATE_PARAMS *setp = (set2 << 8) + set
786 #include <iconv/loop.c>
789 /* Now define the toplevel functions. */
790 #include <iconv/skeleton.c>