Update.
[glibc.git] / iconvdata / iso-2022-jp.c
blob9aeaad6c3e8ec3bbf25e5f7705ad04dcd0940b27
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_set,
95 GB2312_set,
96 KSC5601_set,
97 JISX0212_set,
98 ISO88591_set,
99 ISO88597_set
104 gconv_init (struct gconv_step *step)
106 /* Determine which direction. */
107 struct iso2022jp_data *new_data;
108 enum direction dir = illegal_dir;
109 enum variant var = illegal_var;
110 int result;
112 if (__strcasecmp (step->from_name, "ISO-2022-JP//") == 0)
114 dir = from_iso2022jp;
115 var = iso2022jp;
117 else if (__strcasecmp (step->to_name, "ISO-2022-JP//") == 0)
119 dir = to_iso2022jp;
120 var = iso2022jp;
122 else if (__strcasecmp (step->from_name, "ISO-2022-JP-2//") == 0)
124 dir = from_iso2022jp;
125 var = iso2022jp2;
127 else if (__strcasecmp (step->to_name, "ISO-2022-JP-2//") == 0)
129 dir = to_iso2022jp;
130 var = iso2022jp2;
133 result = GCONV_NOCONV;
134 if (dir != illegal_dir
135 && ((new_data
136 = (struct iso2022jp_data *) malloc (sizeof (struct iso2022jp_data)))
137 != NULL))
139 new_data->dir = dir;
140 new_data->var = var;
141 step->data = new_data;
143 if (dir == from_iso2022jp)
145 step->min_needed_from = MIN_NEEDED_FROM;
146 step->max_needed_from = MAX_NEEDED_FROM;
147 step->min_needed_to = MIN_NEEDED_TO;
148 step->max_needed_to = MIN_NEEDED_TO;
150 else
152 step->min_needed_from = MIN_NEEDED_TO;
153 step->max_needed_from = MAX_NEEDED_TO;
154 step->min_needed_to = MIN_NEEDED_FROM;
155 step->max_needed_to = MIN_NEEDED_FROM + 2;
158 /* Yes, this is a stateful encoding. */
159 step->stateful = 1;
161 result = GCONV_OK;
164 return result;
168 void
169 gconv_end (struct gconv_step *data)
171 free (data->data);
175 /* Since this is a stateful encoding we have to provide code which resets
176 the output state to the initial state. This has to be done during the
177 flushing. */
178 #define EMIT_SHIFT_TO_INIT \
179 if (data->statep->count != ASCII_set) \
181 enum direction dir = ((struct iso2022jp_data *) step->data)->dir; \
183 if (dir == from_iso2022jp) \
184 /* It's easy, we don't have to emit anything, we just reset the \
185 state for the input. */ \
186 data->statep->count = ASCII_set; \
187 else \
189 char *outbuf = data->outbuf; \
191 /* We are not in the initial state. To switch back we have \
192 to emit the sequence `Esc ( B'. */ \
193 if (outbuf + 3 > data->outbufend) \
194 /* We don't have enough room in the output buffer. */ \
195 status = GCONV_FULL_OUTPUT; \
196 else \
198 /* Write out the shift sequence. */ \
199 *outbuf++ = ESC; \
200 *outbuf++ = '('; \
201 *outbuf++ = 'B'; \
202 data->outbuf = outbuf; \
203 data->statep->count = ASCII_set; \
209 /* Since we might have to reset input pointer we must be able to save
210 and retore the state. */
211 #define SAVE_RESET_STATE(Save) \
212 if (Save) \
213 save_set = *setp; \
214 else \
215 *setp = save_set
218 /* First define the conversion function from ISO-2022-JP to UCS4. */
219 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
220 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
221 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
222 #define LOOPFCT FROM_LOOP
223 #define BODY \
225 uint32_t ch = *inptr; \
227 /* This is a 7bit character set, disallow all 8bit characters. */ \
228 if (ch > 0x7f) \
230 result = GCONV_ILLEGAL_INPUT; \
231 break; \
234 /* Recognize escape sequences. */ \
235 if (ch == ESC) \
237 /* We now must be prepared to read two to three more \
238 chracters. If we have a match in the first character but \
239 then the input buffer ends we terminate with an error since \
240 we must not risk missing an escape sequence just because it \
241 is not entirely in the current input buffer. */ \
242 if (inptr + 2 >= inend \
243 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
244 && inptr + 3 >= inend)) \
246 /* Not enough input available. */ \
247 result = GCONV_EMPTY_INPUT; \
248 break; \
251 if (inptr[1] == '(') \
253 if (inptr[2] == 'B') \
255 /* ASCII selected. */ \
256 set = ASCII_set; \
257 inptr += 3; \
258 continue; \
260 else if (inptr[2] == 'J') \
262 /* JIS X 0201 selected. */ \
263 set = JISX0201_set; \
264 inptr += 3; \
265 continue; \
268 else if (inptr[1] == '$') \
270 if (inptr[2] == '@') \
272 /* JIS X 0208-1978 selected. */ \
273 set = JISX0208_1978_set; \
274 inptr += 3; \
275 continue; \
277 else if (inptr[2] == 'B') \
279 /* JIS X 0208-1983 selected. */ \
280 set = JISX0208_1983_set; \
281 inptr += 3; \
282 continue; \
284 else if (var == iso2022jp2) \
286 if (inptr[2] == 'A') \
288 /* GB 2312-1980 selected. */ \
289 set = GB2312_set; \
290 inptr += 3; \
291 continue; \
293 else if (inptr[2] == '(') \
295 if (inptr[3] == 'C') \
297 /* KSC 5601-1987 selected. */ \
298 set = KSC5601_set; \
299 inptr += 4; \
300 continue; \
302 else if (inptr[3] == 'D') \
304 /* JIS X 0212-1990 selected. */ \
305 set = JISX0212_set; \
306 inptr += 4; \
307 continue; \
312 else if (var == iso2022jp2 && inptr[1] == '.') \
314 if (inptr[2] == 'A') \
316 /* ISO 8859-1-GR selected. */ \
317 set = ISO88591_set; \
318 inptr += 3; \
319 continue; \
321 else if (inptr[2] == 'F') \
323 /* ISO 8859-7-GR selected. */ \
324 set = ISO88597_set; \
325 inptr += 3; \
326 continue; \
331 if (set == ASCII_set \
332 || (var < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
333 || (var >= ISO88591_set && ch < 0x20)) \
334 /* Almost done, just advance the input pointer. */ \
335 ++inptr; \
336 else if (set == JISX0201_set) \
338 /* Use the JIS X 0201 table. */ \
339 ch = jisx0201_to_ucs4 (ch + 0x80); \
340 if (ch == UNKNOWN_10646_CHAR) \
342 result = GCONV_ILLEGAL_INPUT; \
343 break; \
345 ++inptr; \
347 else if (set == ISO88591_set) \
349 /* This is quite easy. All characters are defined and the \
350 ISO 10646 value is computed by adding 0x80. */ \
351 ch += 0x80; \
352 ++inptr; \
354 else if (set == ISO88597_set) \
356 /* We use the table from the ISO 8859-7 module. */ \
357 ch = iso88597_to_ucs4[ch - 0x20]; \
358 if (ch == 0) \
360 result = GCONV_ILLEGAL_INPUT; \
361 break; \
363 ++inptr; \
365 else \
367 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
368 /* XXX I don't have the tables for these two old variants of \
369 JIS X 0208. Therefore I'm using the tables for JIS X \
370 0208-1990. If somebody has problems with this please \
371 provide the appropriate tables. */ \
372 ch = jisx0208_to_ucs4 (&inptr, \
373 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
374 else if (set == JISX0212_set) \
375 /* Use the JIS X 0212 table. */ \
376 ch = jisx0212_to_ucs4 (&inptr, \
377 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
378 else if (set == GB2312_set) \
379 /* Use the GB 2312 table. */ \
380 ch = gb2312_to_ucs4 (&inptr, \
381 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
382 else \
384 assert (set == KSC5601_set); \
386 /* Use the KSC 5601 table. */ \
387 ch = ksc5601_to_ucs4 (&inptr, \
388 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
391 if (NEED_LENGTH_TEST && ch == 0) \
393 result = GCONV_EMPTY_INPUT; \
394 break; \
396 else if (ch == UNKNOWN_10646_CHAR) \
398 result = GCONV_ILLEGAL_INPUT; \
399 break; \
403 *((uint32_t *) outptr)++ = ch; \
405 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
406 #define INIT_PARAMS int set = *setp
407 #define UPDATE_PARAMS *setp = set
408 #include <iconv/loop.c>
411 /* Next, define the other direction. */
412 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
413 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
414 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
415 #define LOOPFCT TO_LOOP
416 #define BODY \
418 unsigned char ch; \
419 size_t written = 0; \
421 ch = *((uint32_t *) inptr); \
423 /* First see whether we can write the character using the currently \
424 selected character set. */ \
425 if (set == ASCII_set \
426 || (ch >= 0x01 && ((set < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
427 || (set >= ISO88591_set && ch < 0x20)))) \
429 /* Please note that the NUL byte is *not* matched if we are not \
430 currently using the ASCII charset. This is because we must \
431 switch to the initial state whenever a NUL byte is written. */ \
432 if (ch <= 0x7f) \
434 *outptr++ = ch; \
435 written = 1; \
438 else if (set == JISX0201_set) \
439 written = ucs4_to_jisx0201 (ch, outptr); \
440 else if (set == ISO88591_set) \
442 if (ch >= 0xa0 && ch <= 0xff) \
444 *outptr++ = ch - 0x80; \
445 written = 1; \
448 else if (set == ISO88597_set) \
450 const struct gap *rp = from_idx; \
452 while (ch > rp->end) \
453 ++rp; \
454 if (ch >= rp->start) \
456 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
457 if (res != '\0') \
459 *outptr++ = res; \
460 written = 1; \
464 else \
466 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
467 written = ucs4_to_jisx0208 (ch, outptr, \
468 (NEED_LENGTH_TEST \
469 ? outend - outptr : 2)); \
470 else if (set == JISX0212_set) \
471 written = ucs4_to_jisx0212 (ch, outptr, \
472 (NEED_LENGTH_TEST \
473 ? outend - outptr : 2)); \
474 else if (set == GB2312_set) \
475 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
476 ? outend - outptr : 2)); \
477 else \
479 assert (set == KSC5601_set); \
481 written = ucs4_to_ksc5601 (ch, outptr, \
482 (NEED_LENGTH_TEST \
483 ? outend - outptr : 2)); \
486 if (NEED_LENGTH_TEST && written == 0) \
488 result = GCONV_FULL_OUTPUT; \
489 break; \
493 if (written == UNKNOWN_10646_CHAR) \
495 /* Either this is an unknown character or we have to switch \
496 the currently selected character set. The character sets \
497 do not code entirely separate parts of ISO 10646 and \
498 therefore there is no single correct result. If we choose \
499 the character set to use wrong we might be end up with \
500 using yet another character set for the next character \
501 though the current and the next could be encoded with one \
502 character set. We leave this kind of optimization for \
503 later and now simply use a fixed order in which we test for \
504 availability */ \
506 /* First test whether we have at least three more bytes for \
507 the escape sequence. The two charsets which require four \
508 bytes will be handled later. */ \
509 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
511 result = GCONV_FULL_OUTPUT; \
512 break; \
515 if (ch <= 0x7f) \
517 /* We must encode using ASCII. First write out the \
518 escape sequence. */ \
519 *outptr++ = ESC; \
520 *outptr++ = '('; \
521 *outptr++ = 'B'; \
522 set = ASCII_set; \
524 if (NEED_LENGTH_TEST && outptr == outend) \
526 result = GCONV_FULL_OUTPUT; \
527 break; \
530 *outptr++ = ch; \
532 else if (ch >= 0xa0 && ch <= 0xff) \
534 /* This character set is not available in ISO-2022-JP. */ \
535 if (var == iso2022jp) \
537 result = GCONV_ILLEGAL_INPUT; \
538 break; \
541 /* We must use the ISO 8859-1 upper half. */ \
542 *outptr++ = ESC; \
543 *outptr++ = '.'; \
544 *outptr++ = 'A'; \
545 set = ISO88591_set; \
547 if (NEED_LENGTH_TEST && outptr == outend) \
549 result = GCONV_FULL_OUTPUT; \
550 break; \
553 *outptr++ = ch - 0x80; \
555 else \
557 /* Now it becomes difficult. We must search the other \
558 character sets one by one and we cannot use simple \
559 arithmetic to determine whether the character can be \
560 encoded using this set. */ \
561 size_t written; \
562 unsigned char buf[2]; \
564 written = ucs4_to_jisx0201 (ch, buf); \
565 if (written != UNKNOWN_10646_CHAR) \
567 /* We use JIS X 0201. */ \
568 *outptr++ = ESC; \
569 *outptr++ = '$'; \
570 *outptr++ = '@'; \
571 set = JISX0201_set; \
573 if (NEED_LENGTH_TEST && outptr == outend) \
575 result = GCONV_FULL_OUTPUT; \
576 break; \
579 *outptr++ = buf[0]; \
581 else \
583 written = ucs4_to_jisx0208 (ch, buf, 2); \
584 if (written != UNKNOWN_10646_CHAR) \
586 /* We use JIS X 0208. */ \
587 *outptr++ = ESC; \
588 *outptr++ = '$'; \
589 *outptr++ = 'B'; \
590 set = JISX0208_1983_set; \
592 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
594 result = GCONV_FULL_OUTPUT; \
595 break; \
598 *outptr++ = buf[0]; \
599 *outptr++ = buf[1]; \
601 else if (var == iso2022jp) \
603 /* We have no other choice. */ \
604 result = GCONV_ILLEGAL_INPUT; \
605 break; \
607 else \
609 written = ucs4_to_jisx0208 (ch, buf, 2); \
610 if (written != UNKNOWN_10646_CHAR) \
612 /* We use JIS X 0212. */ \
613 if (outptr + 4 > outend) \
615 result = GCONV_FULL_OUTPUT; \
616 break; \
618 *outptr++ = ESC; \
619 *outptr++ = '$'; \
620 *outptr++ = '('; \
621 *outptr++ = 'D'; \
622 set = JISX0212_set; \
624 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
626 result = GCONV_FULL_OUTPUT; \
627 break; \
630 *outptr++ = buf[0]; \
631 *outptr++ = buf[1]; \
633 else \
635 written = ucs4_to_gb2312 (ch, buf, 2); \
636 if (written != UNKNOWN_10646_CHAR) \
638 /* We use GB 2312. */ \
639 *outptr++ = ESC; \
640 *outptr++ = '$'; \
641 *outptr++ = 'A'; \
642 set = GB2312_set; \
644 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
646 result = GCONV_FULL_OUTPUT; \
647 break; \
650 *outptr++ = buf[0]; \
651 *outptr++ = buf[1]; \
653 else \
655 written = ucs4_to_ksc5601 (ch, buf, 2); \
656 if (written != UNKNOWN_10646_CHAR) \
658 /* We use KSC 5601. */ \
659 if (outptr + 4 > outend) \
661 result = GCONV_FULL_OUTPUT; \
662 break; \
664 *outptr++ = ESC; \
665 *outptr++ = '$'; \
666 *outptr++ = '('; \
667 *outptr++ = 'C'; \
668 set = KSC5601_set; \
670 if (NEED_LENGTH_TEST \
671 && outptr + 2 > outend) \
673 result = GCONV_FULL_OUTPUT; \
674 break; \
677 *outptr++ = buf[0]; \
678 *outptr++ = buf[1]; \
680 else \
682 result = GCONV_ILLEGAL_INPUT; \
683 break; \
692 /* Now that we wrote the output increment the input pointer. */ \
693 inptr += 4; \
695 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
696 #define INIT_PARAMS int set = *setp
697 #define UPDATE_PARAMS *setp = set
698 #include <iconv/loop.c>
701 /* Now define the toplevel functions. */
702 #include <iconv/skeleton.c>