Update.
[glibc.git] / iconvdata / iso-2022-jp.c
blobe888d310b722f3e469c09e49fe241859acf96697
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. */
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,
99 ISO88591_set,
100 ISO88597_set
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;
111 int result;
113 if (__strcasecmp (step->from_name, "ISO-2022-JP//") == 0)
115 dir = from_iso2022jp;
116 var = iso2022jp;
118 else if (__strcasecmp (step->to_name, "ISO-2022-JP//") == 0)
120 dir = to_iso2022jp;
121 var = iso2022jp;
123 else if (__strcasecmp (step->from_name, "ISO-2022-JP-2//") == 0)
125 dir = from_iso2022jp;
126 var = iso2022jp2;
128 else if (__strcasecmp (step->to_name, "ISO-2022-JP-2//") == 0)
130 dir = to_iso2022jp;
131 var = iso2022jp2;
134 result = GCONV_NOCONV;
135 if (dir != illegal_dir
136 && ((new_data
137 = (struct iso2022jp_data *) malloc (sizeof (struct iso2022jp_data)))
138 != NULL))
140 new_data->dir = dir;
141 new_data->var = var;
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;
151 else
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. */
160 step->stateful = 1;
162 result = GCONV_OK;
165 return result;
169 void
170 gconv_end (struct gconv_step *data)
172 free (data->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
178 flushing. */
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; \
188 else \
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; \
197 else \
199 /* Write out the shift sequence. */ \
200 *outbuf++ = ESC; \
201 *outbuf++ = '('; \
202 *outbuf++ = 'B'; \
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) \
213 if (Save) \
214 save_set = *setp; \
215 else \
216 *setp = save_set
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
224 #define BODY \
226 uint32_t ch = *inptr; \
228 /* Recognize escape sequences. */ \
229 if (ch == ESC) \
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; \
242 break; \
245 if (inptr[1] == '(') \
247 if (inptr[2] == 'B') \
249 /* ASCII selected. */ \
250 set = ASCII_set; \
251 inptr += 3; \
252 continue; \
254 else if (inptr[2] == 'J') \
256 /* JIS X 0201 selected. */ \
257 set = JISX0201_Roman_set; \
258 inptr += 3; \
259 continue; \
261 else if (var == iso2022jp2 && inptr[2] == 'I') \
263 /* JIS X 0201 selected. */ \
264 set = JISX0201_Kana_set; \
265 inptr += 3; \
266 continue; \
269 else if (inptr[1] == '$') \
271 if (inptr[2] == '@') \
273 /* JIS X 0208-1978 selected. */ \
274 set = JISX0208_1978_set; \
275 inptr += 3; \
276 continue; \
278 else if (inptr[2] == 'B') \
280 /* JIS X 0208-1983 selected. */ \
281 set = JISX0208_1983_set; \
282 inptr += 3; \
283 continue; \
285 else if (var == iso2022jp2) \
287 if (inptr[2] == 'A') \
289 /* GB 2312-1980 selected. */ \
290 set = GB2312_set; \
291 inptr += 3; \
292 continue; \
294 else if (inptr[2] == '(') \
296 if (inptr[3] == 'C') \
298 /* KSC 5601-1987 selected. */ \
299 set = KSC5601_set; \
300 inptr += 4; \
301 continue; \
303 else if (inptr[3] == 'D') \
305 /* JIS X 0212-1990 selected. */ \
306 set = JISX0212_set; \
307 inptr += 4; \
308 continue; \
313 else if (var == iso2022jp2 && inptr[1] == '.') \
315 if (inptr[2] == 'A') \
317 /* ISO 8859-1-GR selected. */ \
318 set = ISO88591_set; \
319 inptr += 3; \
320 continue; \
322 else if (inptr[2] == 'F') \
324 /* ISO 8859-7-GR selected. */ \
325 set = ISO88597_set; \
326 inptr += 3; \
327 continue; \
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. */ \
336 ++inptr; \
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; \
344 break; \
346 ++inptr; \
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; \
355 break; \
357 ++inptr; \
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. */ \
363 ch |= 0x80; \
364 ++inptr; \
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]; \
370 if (ch == 0) \
372 result = GCONV_ILLEGAL_INPUT; \
373 break; \
375 ++inptr; \
377 else \
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); \
394 else \
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; \
406 break; \
408 else if (ch == UNKNOWN_10646_CHAR) \
410 result = GCONV_ILLEGAL_INPUT; \
411 break; \
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
428 #define BODY \
430 uint32_t ch; \
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. */ \
442 if (ch <= 0x7f) \
444 *outptr++ = ch; \
445 written = 1; \
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]; \
455 written = 1; \
457 else \
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; \
467 written = 1; \
469 else \
470 written = UNKNOWN_10646_CHAR; \
472 else if (set == ISO88591_set) \
474 if (ch >= 0x80 && ch <= 0xff) \
476 *outptr++ = ch; \
477 written = 1; \
480 else if (set == ISO88597_set) \
482 const struct gap *rp = from_idx; \
484 while (ch > rp->end) \
485 ++rp; \
486 if (ch >= rp->start) \
488 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
489 if (res != '\0') \
491 *outptr++ = res | 0x80; \
492 written = 1; \
496 else \
498 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
499 written = ucs4_to_jisx0208 (ch, outptr, \
500 (NEED_LENGTH_TEST \
501 ? outend - outptr : 2)); \
502 else if (set == JISX0212_set) \
503 written = ucs4_to_jisx0212 (ch, outptr, \
504 (NEED_LENGTH_TEST \
505 ? outend - outptr : 2)); \
506 else if (set == GB2312_set) \
507 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
508 ? outend - outptr : 2)); \
509 else \
511 assert (set == KSC5601_set); \
513 written = ucs4_to_ksc5601 (ch, outptr, \
514 (NEED_LENGTH_TEST \
515 ? outend - outptr : 2)); \
518 if (NEED_LENGTH_TEST && written == 0) \
520 result = GCONV_FULL_OUTPUT; \
521 break; \
523 else if (written != UNKNOWN_10646_CHAR) \
524 outptr += written; \
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 \
538 availability */ \
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; \
546 break; \
549 if (ch <= 0x7f) \
551 /* We must encode using ASCII. First write out the \
552 escape sequence. */ \
553 *outptr++ = ESC; \
554 *outptr++ = '('; \
555 *outptr++ = 'B'; \
556 set = ASCII_set; \
558 if (NEED_LENGTH_TEST && outptr == outend) \
560 result = GCONV_FULL_OUTPUT; \
561 break; \
564 *outptr++ = ch; \
566 else \
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. */ \
572 size_t written; \
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. */ \
579 *outptr++ = ESC; \
580 *outptr++ = '('; \
581 *outptr++ = 'J'; \
582 set = JISX0201_Roman_set; \
584 if (NEED_LENGTH_TEST && outptr == outend) \
586 result = GCONV_FULL_OUTPUT; \
587 break; \
590 *outptr++ = buf[0]; \
592 else \
594 written = ucs4_to_jisx0208 (ch, buf, 2); \
595 if (written != UNKNOWN_10646_CHAR) \
597 /* We use JIS X 0208. */ \
598 *outptr++ = ESC; \
599 *outptr++ = '$'; \
600 *outptr++ = 'B'; \
601 set = JISX0208_1983_set; \
603 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
605 result = GCONV_FULL_OUTPUT; \
606 break; \
609 *outptr++ = buf[0]; \
610 *outptr++ = buf[1]; \
612 else if (var == iso2022jp) \
614 /* We have no other choice. */ \
615 result = GCONV_ILLEGAL_INPUT; \
616 break; \
618 else \
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; \
627 break; \
629 *outptr++ = ESC; \
630 *outptr++ = '$'; \
631 *outptr++ = '('; \
632 *outptr++ = 'D'; \
633 set = JISX0212_set; \
635 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
637 result = GCONV_FULL_OUTPUT; \
638 break; \
641 *outptr++ = buf[0]; \
642 *outptr++ = buf[1]; \
644 else \
646 written = ucs4_to_jisx0201 (ch, buf); \
647 if (written != UNKNOWN_10646_CHAR && buf[0] >= 0x80) \
649 /* We use JIS X 0201. */ \
650 *outptr++ = ESC; \
651 *outptr++ = '('; \
652 *outptr++ = 'I'; \
653 set = JISX0201_Kana_set; \
655 if (NEED_LENGTH_TEST && outptr == outend) \
657 result = GCONV_FULL_OUTPUT; \
658 break; \
661 *outptr++ = buf[0] - 0x80; \
663 else if (ch != 0xa5 && ch >= 0x80 && ch <= 0xff) \
665 /* ISO 8859-1 upper half. */ \
666 *outptr++ = ESC; \
667 *outptr++ = '.'; \
668 *outptr++ = 'A'; \
669 set = ISO88591_set; \
671 if (NEED_LENGTH_TEST && outptr == outend) \
673 result = GCONV_FULL_OUTPUT; \
674 break; \
677 *outptr++ = ch; \
679 else \
681 written = ucs4_to_gb2312 (ch, buf, 2); \
682 if (written != UNKNOWN_10646_CHAR) \
684 /* We use GB 2312. */ \
685 *outptr++ = ESC; \
686 *outptr++ = '$'; \
687 *outptr++ = 'A'; \
688 set = GB2312_set; \
690 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
692 result = GCONV_FULL_OUTPUT; \
693 break; \
696 *outptr++ = buf[0]; \
697 *outptr++ = buf[1]; \
699 else \
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; \
709 break; \
711 *outptr++ = ESC; \
712 *outptr++ = '$'; \
713 *outptr++ = '('; \
714 *outptr++ = 'C'; \
715 set = KSC5601_set; \
717 if (NEED_LENGTH_TEST \
718 && outptr + 2 > outend) \
720 result = GCONV_FULL_OUTPUT; \
721 break; \
724 *outptr++ = buf[0]; \
725 *outptr++ = buf[1]; \
727 else \
729 result = GCONV_ILLEGAL_INPUT; \
730 break; \
740 /* Now that we wrote the output increment the input pointer. */ \
741 inptr += 4; \
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>