1 /* Conversion module for ISO-2022-JP.
2 Copyright (C) 1998 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: */
105 gconv_init (struct gconv_step
*step
)
107 /* Determine which direction. */
108 struct iso2022jp_data
*new_data
;
109 enum direction dir
= illegal_dir
;
110 enum variant var
= illegal_var
;
113 if (__strcasecmp (step
->from_name
, "ISO-2022-JP//") == 0)
115 dir
= from_iso2022jp
;
118 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP//") == 0)
123 else if (__strcasecmp (step
->from_name
, "ISO-2022-JP-2//") == 0)
125 dir
= from_iso2022jp
;
128 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP-2//") == 0)
134 result
= GCONV_NOCONV
;
135 if (dir
!= illegal_dir
137 = (struct iso2022jp_data
*) malloc (sizeof (struct iso2022jp_data
)))
142 step
->data
= new_data
;
144 if (dir
== from_iso2022jp
)
146 step
->min_needed_from
= MIN_NEEDED_FROM
;
147 step
->max_needed_from
= MAX_NEEDED_FROM
;
148 step
->min_needed_to
= MIN_NEEDED_TO
;
149 step
->max_needed_to
= MIN_NEEDED_TO
;
153 step
->min_needed_from
= MIN_NEEDED_TO
;
154 step
->max_needed_from
= MAX_NEEDED_TO
;
155 step
->min_needed_to
= MIN_NEEDED_FROM
;
156 step
->max_needed_to
= MIN_NEEDED_FROM
+ 2;
159 /* Yes, this is a stateful encoding. */
170 gconv_end (struct gconv_step
*data
)
176 /* Since this is a stateful encoding we have to provide code which resets
177 the output state to the initial state. This has to be done during the
179 #define EMIT_SHIFT_TO_INIT \
180 if (data->statep->count != ASCII_set) \
182 enum direction dir = ((struct iso2022jp_data *) step->data)->dir; \
184 if (dir == from_iso2022jp) \
185 /* It's easy, we don't have to emit anything, we just reset the \
186 state for the input. */ \
187 data->statep->count = ASCII_set; \
190 char *outbuf = data->outbuf; \
192 /* We are not in the initial state. To switch back we have \
193 to emit the sequence `Esc ( B'. */ \
194 if (outbuf + 3 > data->outbufend) \
195 /* We don't have enough room in the output buffer. */ \
196 status = GCONV_FULL_OUTPUT; \
199 /* Write out the shift sequence. */ \
203 data->outbuf = outbuf; \
204 data->statep->count = ASCII_set; \
210 /* Since we might have to reset input pointer we must be able to save
211 and retore the state. */
212 #define SAVE_RESET_STATE(Save) \
219 /* First define the conversion function from ISO-2022-JP to UCS4. */
220 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
221 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
222 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
223 #define LOOPFCT FROM_LOOP
226 uint32_t ch = *inptr; \
228 /* Recognize escape sequences. */ \
231 /* We now must be prepared to read two to three more \
232 chracters. If we have a match in the first character but \
233 then the input buffer ends we terminate with an error since \
234 we must not risk missing an escape sequence just because it \
235 is not entirely in the current input buffer. */ \
236 if (inptr + 2 >= inend \
237 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
238 && inptr + 3 >= inend)) \
240 /* Not enough input available. */ \
241 result = GCONV_EMPTY_INPUT; \
245 if (inptr[1] == '(') \
247 if (inptr[2] == 'B') \
249 /* ASCII selected. */ \
254 else if (inptr[2] == 'J') \
256 /* JIS X 0201 selected. */ \
257 set = JISX0201_Roman_set; \
261 else if (var == iso2022jp2 && inptr[2] == 'I') \
263 /* JIS X 0201 selected. */ \
264 set = JISX0201_Kana_set; \
269 else if (inptr[1] == '$') \
271 if (inptr[2] == '@') \
273 /* JIS X 0208-1978 selected. */ \
274 set = JISX0208_1978_set; \
278 else if (inptr[2] == 'B') \
280 /* JIS X 0208-1983 selected. */ \
281 set = JISX0208_1983_set; \
285 else if (var == iso2022jp2) \
287 if (inptr[2] == 'A') \
289 /* GB 2312-1980 selected. */ \
294 else if (inptr[2] == '(') \
296 if (inptr[3] == 'C') \
298 /* KSC 5601-1987 selected. */ \
303 else if (inptr[3] == 'D') \
305 /* JIS X 0212-1990 selected. */ \
306 set = JISX0212_set; \
313 else if (var == iso2022jp2 && inptr[1] == '.') \
315 if (inptr[2] == 'A') \
317 /* ISO 8859-1-GR selected. */ \
318 set = ISO88591_set; \
322 else if (inptr[2] == 'F') \
324 /* ISO 8859-7-GR selected. */ \
325 set = ISO88597_set; \
332 if (set == ASCII_set \
333 || (var < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
334 || (var >= ISO88591_set && ch < 0x20)) \
335 /* Almost done, just advance the input pointer. */ \
337 else if (set == JISX0201_Roman_set) \
339 /* Use the JIS X 0201 table. */ \
340 ch = jisx0201_to_ucs4 (ch); \
341 if (ch == UNKNOWN_10646_CHAR) \
343 result = GCONV_ILLEGAL_INPUT; \
348 else if (set == JISX0201_Kana_set) \
350 /* Use the JIS X 0201 table. */ \
351 ch = jisx0201_to_ucs4 (ch + 0x80); \
352 if (ch == UNKNOWN_10646_CHAR) \
354 result = GCONV_ILLEGAL_INPUT; \
359 else if (set == ISO88591_set) \
361 /* This is quite easy. All characters are defined and the \
362 ISO 10646 value is computed by adding 0x80. */ \
366 else if (set == ISO88597_set) \
368 /* We use the table from the ISO 8859-7 module. */ \
369 ch = iso88597_to_ucs4[(ch & 0x7f) - 0x20]; \
372 result = GCONV_ILLEGAL_INPUT; \
379 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
380 /* XXX I don't have the tables for these two old variants of \
381 JIS X 0208. Therefore I'm using the tables for JIS X \
382 0208-1990. If somebody has problems with this please \
383 provide the appropriate tables. */ \
384 ch = jisx0208_to_ucs4 (&inptr, \
385 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
386 else if (set == JISX0212_set) \
387 /* Use the JIS X 0212 table. */ \
388 ch = jisx0212_to_ucs4 (&inptr, \
389 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
390 else if (set == GB2312_set) \
391 /* Use the GB 2312 table. */ \
392 ch = gb2312_to_ucs4 (&inptr, \
393 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
396 assert (set == KSC5601_set); \
398 /* Use the KSC 5601 table. */ \
399 ch = ksc5601_to_ucs4 (&inptr, \
400 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
403 if (NEED_LENGTH_TEST && ch == 0) \
405 result = GCONV_EMPTY_INPUT; \
408 else if (ch == UNKNOWN_10646_CHAR) \
410 result = GCONV_ILLEGAL_INPUT; \
415 *((uint32_t *) outptr)++ = ch; \
417 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
418 #define INIT_PARAMS int set = *setp
419 #define UPDATE_PARAMS *setp = set
420 #include <iconv/loop.c>
423 /* Next, define the other direction. */
424 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
425 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
426 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
427 #define LOOPFCT TO_LOOP
431 size_t written = 0; \
433 ch = *((uint32_t *) inptr); \
435 /* First see whether we can write the character using the currently \
436 selected character set. */ \
437 if (set == ASCII_set) \
439 /* Please note that the NUL byte is *not* matched if we are not \
440 currently using the ASCII charset. This is because we must \
441 switch to the initial state whenever a NUL byte is written. */ \
448 else if (set == JISX0201_Roman_set) \
450 unsigned char buf[2]; \
451 written = ucs4_to_jisx0201 (ch, buf); \
452 if (written != UNKNOWN_10646_CHAR && buf[0] > 0x20 && buf[0] < 0x80) \
454 *outptr++ = buf[0]; \
458 written = UNKNOWN_10646_CHAR; \
460 else if (set == JISX0201_Kana_set) \
462 unsigned char buf[2]; \
463 written = ucs4_to_jisx0201 (ch, buf); \
464 if (written != UNKNOWN_10646_CHAR && buf[0] > 0xa0 && buf[0] < 0xe0) \
466 *outptr++ = buf[0] - 0x80; \
470 written = UNKNOWN_10646_CHAR; \
472 else if (set == ISO88591_set) \
474 if (ch >= 0x80 && ch <= 0xff) \
480 else if (set == ISO88597_set) \
482 const struct gap *rp = from_idx; \
484 while (ch > rp->end) \
486 if (ch >= rp->start) \
488 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
491 *outptr++ = res | 0x80; \
498 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
499 written = ucs4_to_jisx0208 (ch, outptr, \
501 ? outend - outptr : 2)); \
502 else if (set == JISX0212_set) \
503 written = ucs4_to_jisx0212 (ch, outptr, \
505 ? outend - outptr : 2)); \
506 else if (set == GB2312_set) \
507 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
508 ? outend - outptr : 2)); \
511 assert (set == KSC5601_set); \
513 written = ucs4_to_ksc5601 (ch, outptr, \
515 ? outend - outptr : 2)); \
518 if (NEED_LENGTH_TEST && written == 0) \
520 result = GCONV_FULL_OUTPUT; \
523 else if (written != UNKNOWN_10646_CHAR) \
527 if (written == UNKNOWN_10646_CHAR || written == 0) \
529 /* Either this is an unknown character or we have to switch \
530 the currently selected character set. The character sets \
531 do not code entirely separate parts of ISO 10646 and \
532 therefore there is no single correct result. If we choose \
533 the character set to use wrong we might be end up with \
534 using yet another character set for the next character \
535 though the current and the next could be encoded with one \
536 character set. We leave this kind of optimization for \
537 later and now simply use a fixed order in which we test for \
540 /* First test whether we have at least three more bytes for \
541 the escape sequence. The two charsets which require four \
542 bytes will be handled later. */ \
543 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
545 result = GCONV_FULL_OUTPUT; \
551 /* We must encode using ASCII. First write out the \
552 escape sequence. */ \
558 if (NEED_LENGTH_TEST && outptr == outend) \
560 result = GCONV_FULL_OUTPUT; \
568 /* Now it becomes difficult. We must search the other \
569 character sets one by one and we cannot use simple \
570 arithmetic to determine whether the character can be \
571 encoded using this set. */ \
573 unsigned char buf[2]; \
575 written = ucs4_to_jisx0201 (ch, buf); \
576 if (written != UNKNOWN_10646_CHAR && buf[0] < 0x80) \
578 /* We use JIS X 0201. */ \
582 set = JISX0201_Roman_set; \
584 if (NEED_LENGTH_TEST && outptr == outend) \
586 result = GCONV_FULL_OUTPUT; \
590 *outptr++ = buf[0]; \
594 written = ucs4_to_jisx0208 (ch, buf, 2); \
595 if (written != UNKNOWN_10646_CHAR) \
597 /* We use JIS X 0208. */ \
601 set = JISX0208_1983_set; \
603 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
605 result = GCONV_FULL_OUTPUT; \
609 *outptr++ = buf[0]; \
610 *outptr++ = buf[1]; \
612 else if (var == iso2022jp) \
614 /* We have no other choice. */ \
615 result = GCONV_ILLEGAL_INPUT; \
620 written = ucs4_to_jisx0212 (ch, buf, 2); \
621 if (written != UNKNOWN_10646_CHAR) \
623 /* We use JIS X 0212. */ \
624 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
626 result = GCONV_FULL_OUTPUT; \
633 set = JISX0212_set; \
635 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
637 result = GCONV_FULL_OUTPUT; \
641 *outptr++ = buf[0]; \
642 *outptr++ = buf[1]; \
646 written = ucs4_to_jisx0201 (ch, buf); \
647 if (written != UNKNOWN_10646_CHAR && buf[0] >= 0x80) \
649 /* We use JIS X 0201. */ \
653 set = JISX0201_Kana_set; \
655 if (NEED_LENGTH_TEST && outptr == outend) \
657 result = GCONV_FULL_OUTPUT; \
661 *outptr++ = buf[0] - 0x80; \
663 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
665 /* ISO 8859-1 upper half. */ \
669 set = ISO88591_set; \
671 if (NEED_LENGTH_TEST && outptr == outend) \
673 result = GCONV_FULL_OUTPUT; \
681 written = ucs4_to_gb2312 (ch, buf, 2); \
682 if (written != UNKNOWN_10646_CHAR) \
684 /* We use GB 2312. */ \
690 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
692 result = GCONV_FULL_OUTPUT; \
696 *outptr++ = buf[0]; \
697 *outptr++ = buf[1]; \
701 written = ucs4_to_ksc5601 (ch, buf, 2); \
702 if (written != UNKNOWN_10646_CHAR) \
704 /* We use KSC 5601. */ \
705 if (NEED_LENGTH_TEST \
706 && outptr + 4 > outend) \
708 result = GCONV_FULL_OUTPUT; \
717 if (NEED_LENGTH_TEST \
718 && outptr + 2 > outend) \
720 result = GCONV_FULL_OUTPUT; \
724 *outptr++ = buf[0]; \
725 *outptr++ = buf[1]; \
729 result = GCONV_ILLEGAL_INPUT; \
740 /* Now that we wrote the output increment the input pointer. */ \
743 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
744 #define INIT_PARAMS int set = *setp
745 #define UPDATE_PARAMS *setp = set
746 #include <iconv/loop.c>
749 /* Now define the toplevel functions. */
750 #include <iconv/skeleton.c>