1999-03-18 Thorsten Kukuk <kukuk@suse.de>
[glibc.git] / iconvdata / iso-2022-jp.c
blobbb158491f24965177e78b0918f6b3edf255051bd
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. */
21 #include <gconv.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "jis0201.h"
26 #include "jis0208.h"
27 #include "jis0212.h"
28 #include "gb2312.h"
29 #include "ksc5601.h"
31 struct gap
33 uint16_t start;
34 uint16_t end;
35 int32_t idx;
38 #include "iso8859-7jp.h"
40 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
41 #define ESC 0x1b
43 /* We provide our own initialization and destructor function. */
44 #define DEFINE_INIT 0
45 #define DEFINE_FINI 0
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; \
58 int save_set; \
59 int *setp = &data->statep->count;
60 #define EXTRA_LOOP_ARGS , var, setp
63 /* Direction of the transformation. */
64 enum direction
66 illegal_dir,
67 to_iso2022jp,
68 from_iso2022jp
71 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
72 enum variant
74 illegal_var,
75 iso2022jp,
76 iso2022jp2
80 struct iso2022jp_data
82 enum direction dir;
83 enum variant var;
87 /* The COUNT element of the state keeps track of the currently selected
88 character set. The possible values are: */
89 enum
91 ASCII_set = 0,
92 JISX0208_1978_set,
93 JISX0208_1983_set,
94 JISX0201_Roman_set,
95 JISX0201_Kana_set,
96 GB2312_set,
97 KSC5601_set,
98 JISX0212_set
101 /* The second value stored is the designation of the G2 set. The following
102 values are possible: */
103 enum
105 UNSPECIFIED_set = 0,
106 ISO88591_set,
107 ISO88597_set
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;
118 int result;
120 if (__strcasecmp (step->from_name, "ISO-2022-JP//") == 0)
122 dir = from_iso2022jp;
123 var = iso2022jp;
125 else if (__strcasecmp (step->to_name, "ISO-2022-JP//") == 0)
127 dir = to_iso2022jp;
128 var = iso2022jp;
130 else if (__strcasecmp (step->from_name, "ISO-2022-JP-2//") == 0)
132 dir = from_iso2022jp;
133 var = iso2022jp2;
135 else if (__strcasecmp (step->to_name, "ISO-2022-JP-2//") == 0)
137 dir = to_iso2022jp;
138 var = iso2022jp2;
141 result = GCONV_NOCONV;
142 if (dir != illegal_dir)
144 new_data
145 = (struct iso2022jp_data *) malloc (sizeof (struct iso2022jp_data));
147 result = GCONV_NOMEM;
148 if (new_data != NULL)
150 new_data->dir = dir;
151 new_data->var = var;
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;
161 else
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;
172 result = GCONV_OK;
176 return result;
180 void
181 gconv_end (struct gconv_step *data)
183 free (data->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
189 flushing. */
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 \
198 designation. */ \
199 data->statep->count = ASCII_set; \
200 else \
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; \
209 else \
211 /* Write out the shift sequence. */ \
212 *outbuf++ = ESC; \
213 *outbuf++ = '('; \
214 *outbuf++ = 'B'; \
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) \
226 if (Save) \
227 save_set = *setp; \
228 else \
229 *setp = save_set
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
237 #define BODY \
239 uint32_t ch = *inptr; \
241 /* Recognize escape sequences. */ \
242 if (ch == ESC) \
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; \
255 break; \
258 if (inptr[1] == '(') \
260 if (inptr[2] == 'B') \
262 /* ASCII selected. */ \
263 set = ASCII_set; \
264 inptr += 3; \
265 continue; \
267 else if (inptr[2] == 'J') \
269 /* JIS X 0201 selected. */ \
270 set = JISX0201_Roman_set; \
271 inptr += 3; \
272 continue; \
274 else if (var == iso2022jp2 && inptr[2] == 'I') \
276 /* JIS X 0201 selected. */ \
277 set = JISX0201_Kana_set; \
278 inptr += 3; \
279 continue; \
282 else if (inptr[1] == '$') \
284 if (inptr[2] == '@') \
286 /* JIS X 0208-1978 selected. */ \
287 set = JISX0208_1978_set; \
288 inptr += 3; \
289 continue; \
291 else if (inptr[2] == 'B') \
293 /* JIS X 0208-1983 selected. */ \
294 set = JISX0208_1983_set; \
295 inptr += 3; \
296 continue; \
298 else if (var == iso2022jp2) \
300 if (inptr[2] == 'A') \
302 /* GB 2312-1980 selected. */ \
303 set = GB2312_set; \
304 inptr += 3; \
305 continue; \
307 else if (inptr[2] == '(') \
309 if (inptr[3] == 'C') \
311 /* KSC 5601-1987 selected. */ \
312 set = KSC5601_set; \
313 inptr += 4; \
314 continue; \
316 else if (inptr[3] == 'D') \
318 /* JIS X 0212-1990 selected. */ \
319 set = JISX0212_set; \
320 inptr += 4; \
321 continue; \
326 else if (var == iso2022jp2 && inptr[1] == '.') \
328 if (inptr[2] == 'A') \
330 /* ISO 8859-1-GR selected. */ \
331 set2 = ISO88591_set; \
332 inptr += 3; \
333 continue; \
335 else if (inptr[2] == 'F') \
337 /* ISO 8859-7-GR selected. */ \
338 set2 = ISO88597_set; \
339 inptr += 3; \
340 continue; \
345 if (ch == ESC && var == iso2022jp2 && inptr[1] == 'N') \
347 if (set2 == ISO88591_set) \
349 ch = inptr[2] | 0x80; \
350 inptr += 3; \
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; \
358 break; \
360 ch = iso88597_to_ucs4[inptr[2] - 0x20]; \
361 if (ch == 0) \
363 result = GCONV_ILLEGAL_INPUT; \
364 break; \
366 inptr += 3; \
368 else \
370 result = GCONV_ILLEGAL_INPUT; \
371 break; \
374 else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \
375 /* Almost done, just advance the input pointer. */ \
376 ++inptr; \
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; \
384 break; \
386 ++inptr; \
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; \
395 break; \
397 ++inptr; \
399 else \
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); \
416 else \
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; \
428 break; \
430 else if (ch == UNKNOWN_10646_CHAR) \
432 result = GCONV_ILLEGAL_INPUT; \
433 break; \
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
450 #define BODY \
452 uint32_t ch; \
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. */ \
464 if (ch <= 0x7f) \
466 *outptr++ = ch; \
467 written = 1; \
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]; \
480 written = 1; \
482 else \
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; \
492 written = 1; \
494 else \
495 written = UNKNOWN_10646_CHAR; \
497 else \
499 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
500 written = ucs4_to_jisx0208 (ch, outptr, \
501 (NEED_LENGTH_TEST \
502 ? outend - outptr : 2)); \
503 else if (set == JISX0212_set) \
504 written = ucs4_to_jisx0212 (ch, outptr, \
505 (NEED_LENGTH_TEST \
506 ? outend - outptr : 2)); \
507 else if (set == GB2312_set) \
508 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
509 ? outend - outptr : 2)); \
510 else \
512 assert (set == KSC5601_set); \
514 written = ucs4_to_ksc5601 (ch, outptr, \
515 (NEED_LENGTH_TEST \
516 ? outend - outptr : 2)); \
519 if (NEED_LENGTH_TEST && written == 0) \
521 result = GCONV_FULL_OUTPUT; \
522 break; \
524 else if (written != UNKNOWN_10646_CHAR) \
525 outptr += written; \
528 if (written == UNKNOWN_10646_CHAR || written == 0) \
530 if (set2 == ISO88591_set) \
532 if (ch >= 0x80 && ch <= 0xff) \
534 *outptr++ = ESC; \
535 *outptr++ = 'N'; \
536 *outptr++ = ch & 0x7f; \
537 written = 3; \
540 else if (set2 == ISO88597_set) \
542 const struct gap *rp = from_idx; \
544 while (ch > rp->end) \
545 ++rp; \
546 if (ch >= rp->start) \
548 unsigned char res = iso88597_from_ucs4[ch - 0xa0 + rp->idx]; \
549 if (res != '\0') \
551 *outptr++ = ESC; \
552 *outptr++ = 'N'; \
553 *outptr++ = res; \
554 written = 3; \
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 \
571 availability */ \
573 if (ch <= 0x7f) \
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; \
580 break; \
583 *outptr++ = ESC; \
584 *outptr++ = '('; \
585 *outptr++ = 'B'; \
586 set = ASCII_set; \
587 *outptr++ = ch; \
589 /* At the beginning of a line, G2 designation is cleared. */ \
590 if (var == iso2022jp2 && ch == 0x0a) \
591 set2 = UNSPECIFIED_set; \
593 else \
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. */ \
599 size_t written; \
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; \
609 break; \
612 *outptr++ = ESC; \
613 *outptr++ = '('; \
614 *outptr++ = 'J'; \
615 set = JISX0201_Roman_set; \
616 *outptr++ = buf[0]; \
618 else \
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; \
627 break; \
630 *outptr++ = ESC; \
631 *outptr++ = '$'; \
632 *outptr++ = 'B'; \
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; \
641 break; \
643 else \
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; \
652 break; \
654 *outptr++ = ESC; \
655 *outptr++ = '$'; \
656 *outptr++ = '('; \
657 *outptr++ = 'D'; \
658 set = JISX0212_set; \
659 *outptr++ = buf[0]; \
660 *outptr++ = buf[1]; \
662 else \
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; \
671 break; \
674 *outptr++ = ESC; \
675 *outptr++ = '('; \
676 *outptr++ = 'I'; \
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; \
686 break; \
689 *outptr++ = ESC; \
690 *outptr++ = '.'; \
691 *outptr++ = 'A'; \
692 set2 = ISO88591_set; \
693 *outptr++ = ESC; \
694 *outptr++ = 'N'; \
695 *outptr++ = ch; \
697 else \
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; \
706 break; \
709 *outptr++ = ESC; \
710 *outptr++ = '$'; \
711 *outptr++ = 'A'; \
712 set = GB2312_set; \
713 *outptr++ = buf[0]; \
714 *outptr++ = buf[1]; \
716 else \
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; \
726 break; \
728 *outptr++ = ESC; \
729 *outptr++ = '$'; \
730 *outptr++ = '('; \
731 *outptr++ = 'C'; \
732 set = KSC5601_set; \
733 *outptr++ = buf[0]; \
734 *outptr++ = buf[1]; \
736 else \
738 const struct gap *rp = from_idx; \
739 unsigned char gch = 0; \
741 while (ch > rp->end) \
742 ++rp; \
743 if (ch >= rp->start) \
745 ch = ch - 0xa0 + rp->idx; \
746 gch = iso88597_from_ucs4[ch]; \
749 if (gch != 0) \
751 /* We use ISO 8859-7 greek. */ \
752 if (NEED_LENGTH_TEST \
753 && outptr + 6 > outend) \
755 result = GCONV_FULL_OUTPUT; \
756 break; \
758 *outptr++ = ESC; \
759 *outptr++ = '.'; \
760 *outptr++ = 'F'; \
761 set2 = ISO88597_set; \
762 *outptr++ = ESC; \
763 *outptr++ = 'N'; \
764 *outptr++ = gch; \
766 else \
768 result = GCONV_ILLEGAL_INPUT; \
769 break; \
780 /* Now that we wrote the output increment the input pointer. */ \
781 inptr += 4; \
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>