Update.
[glibc.git] / iconvdata / iso-2022-jp.c
blob387298803ffb4f7a5fd47fcb072b069b4520cee2
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 <string.h>
24 #include "jis0201.h"
25 #include "jis0208.h"
26 #include "jis0212.h"
27 #include "gb2312.h"
28 #include "ksc5601.h"
30 struct gap
32 uint16_t start;
33 uint16_t end;
34 int32_t idx;
37 #include "iso8859-7jp.h"
39 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
40 #define ESC 0x1b
42 /* We provide our own initialization and destructor function. */
43 #define DEFINE_INIT 0
44 #define DEFINE_FINI 0
46 /* Definitions used in the body of the `gconv' function. */
47 #define FROM_LOOP from_iso2022jp_loop
48 #define TO_LOOP to_iso2022jp_loop
49 #define MIN_NEEDED_FROM 1
50 #define MAX_NEEDED_FROM 4
51 #define MIN_NEEDED_TO 4
52 #define MAX_NEEDED_TO 4
53 #define FROM_DIRECTION (dir == from_iso2022jp)
54 #define PREPARE_LOOP \
55 enum direction dir = ((struct iso2022jp_data *) step->data)->dir; \
56 enum variant var = ((struct iso2022jp_data *) step->data)->var; \
57 int save_set; \
58 int *setp = &data->statep->count;
59 #define EXTRA_LOOP_ARGS , var, setp
62 /* Direction of the transformation. */
63 enum direction
65 illegal_dir,
66 to_iso2022jp,
67 from_iso2022jp
70 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
71 enum variant
73 illegal_var,
74 iso2022jp,
75 iso2022jp2
79 struct iso2022jp_data
81 enum direction dir;
82 enum variant var;
86 /* The COUNT element of the state keeps track of the currently selected
87 character set. The possible values are: */
88 enum
90 ASCII_set = 0,
91 JISX0208_1978_set,
92 JISX0208_1983_set,
93 JISX0201_set,
94 GB2312_set,
95 KSC5601_set,
96 JISX0212_set,
97 ISO88591_set,
98 ISO88597_set
103 gconv_init (struct gconv_step *step)
105 /* Determine which direction. */
106 struct iso2022jp_data *new_data;
107 enum direction dir = illegal_dir;
108 enum variant var = illegal_var;
109 int result;
111 if (__strcasecmp (step->from_name, "ISO-2022-JP//") == 0)
113 dir = from_iso2022jp;
114 var = iso2022jp;
116 else if (__strcasecmp (step->to_name, "ISO-2022-JP//") == 0)
118 dir = to_iso2022jp;
119 var = iso2022jp;
121 else if (__strcasecmp (step->from_name, "ISO-2022-JP-2//") == 0)
123 dir = from_iso2022jp;
124 var = iso2022jp2;
126 else if (__strcasecmp (step->to_name, "ISO-2022-JP-2//") == 0)
128 dir = to_iso2022jp;
129 var = iso2022jp2;
132 result = GCONV_NOCONV;
133 if (dir != illegal_dir
134 && ((new_data
135 = (struct iso2022jp_data *) malloc (sizeof (struct iso2022jp_data)))
136 != NULL))
138 new_data->dir = dir;
139 new_data->var = var;
140 step->data = new_data;
142 if (dir == from_iso2022jp)
144 step->min_needed_from = MIN_NEEDED_FROM;
145 step->max_needed_from = MAX_NEEDED_FROM;
146 step->min_needed_to = MIN_NEEDED_TO;
147 step->max_needed_to = MIN_NEEDED_TO;
149 else
151 step->min_needed_from = MIN_NEEDED_TO;
152 step->max_needed_from = MAX_NEEDED_TO;
153 step->min_needed_to = MIN_NEEDED_FROM;
154 step->max_needed_to = MIN_NEEDED_FROM + 2;
157 /* Yes, this is a stateful encoding. */
158 step->stateful = 1;
160 result = GCONV_OK;
163 return result;
167 void
168 gconv_end (struct gconv_step *data)
170 free (data->data);
174 /* Since this is a stateful encoding we have to provide code which resets
175 the output state to the initial state. This has to be done during the
176 flushing. */
177 #define EMIT_SHIFT_TO_INIT \
178 if (data->statep->count != ASCII_set) \
180 enum direction dir = ((struct iso2022jp_data *) step->data)->dir; \
182 if (dir == from_iso2022jp) \
183 /* It's easy, we don't have to emit anything, we just reset the \
184 state for the input. */ \
185 data->statep->count = ASCII_set; \
186 else \
188 char *outbuf = data->outbuf; \
190 /* We are not in the initial state. To switch back we have \
191 to emit the sequence `Esc ( B'. */ \
192 if (outbuf + 3 > data->outbufend) \
193 /* We don't have enough room in the output buffer. */ \
194 status = GCONV_FULL_OUTPUT; \
195 else \
197 /* Write out the shift sequence. */ \
198 *outbuf++ = ESC; \
199 *outbuf++ = '('; \
200 *outbuf++ = 'B'; \
201 data->outbuf = outbuf; \
202 data->statep->count = ASCII_set; \
208 /* Since we might have to reset input pointer we must be able to save
209 and retore the state. */
210 #define SAVE_RESET_STATE(Save) \
211 if (Save) \
212 save_set = *setp; \
213 else \
214 *setp = save_set
217 /* First define the conversion function from ISO-2022-JP to UCS4. */
218 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
219 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
220 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
221 #define LOOPFCT FROM_LOOP
222 #define BODY \
224 uint32_t ch = *inptr; \
226 /* This is a 7bit character set, disallow all 8bit characters. */ \
227 if (ch > 0x7f) \
229 result = GCONV_ILLEGAL_INPUT; \
230 break; \
233 /* Recognize escape sequences. */ \
234 if (ch == ESC) \
236 /* We now must be prepared to read two to three more \
237 chracters. If we have a match in the first character but \
238 then the input buffer ends we terminate with an error since \
239 we must not risk missing an escape sequence just because it \
240 is not entirely in the current input buffer. */ \
241 if (inptr + 2 >= inend \
242 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
243 && inptr + 3 >= inend)) \
245 /* Not enough input available. */ \
246 result = GCONV_EMPTY_INPUT; \
247 break; \
250 if (inptr[1] == '(') \
252 if (inptr[2] == 'B') \
254 /* ASCII selected. */ \
255 set = ASCII_set; \
256 inptr += 3; \
257 continue; \
259 else if (inptr[2] == 'J') \
261 /* JIS X 0201 selected. */ \
262 set = JISX0201_set; \
263 inptr += 3; \
264 continue; \
267 else if (inptr[1] == '$') \
269 if (inptr[2] == '@') \
271 /* JIS X 0208-1978 selected. */ \
272 set = JISX0208_1978_set; \
273 inptr += 3; \
274 continue; \
276 else if (inptr[2] == 'B') \
278 /* JIS X 0208-1983 selected. */ \
279 set = JISX0208_1983_set; \
280 inptr += 3; \
281 continue; \
283 else if (var == iso2022jp2) \
285 if (inptr[2] == 'A') \
287 /* GB 2312-1980 selected. */ \
288 set = GB2312_set; \
289 inptr += 3; \
290 continue; \
292 else if (inptr[2] == '(') \
294 if (inptr[3] == 'C') \
296 /* KSC 5601-1987 selected. */ \
297 set = KSC5601_set; \
298 inptr += 4; \
299 continue; \
301 else if (inptr[3] == 'D') \
303 /* JIS X 0212-1990 selected. */ \
304 set = JISX0212_set; \
305 inptr += 4; \
306 continue; \
311 else if (var == iso2022jp2 && inptr[1] == '.') \
313 if (inptr[2] == 'A') \
315 /* ISO 8859-1-GR selected. */ \
316 set = ISO88591_set; \
317 inptr += 3; \
318 continue; \
320 else if (inptr[2] == 'F') \
322 /* ISO 8859-7-GR selected. */ \
323 set = ISO88597_set; \
324 inptr += 3; \
325 continue; \
330 if (set == ASCII_set \
331 || (var < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
332 || (var >= ISO88591_set && ch < 0x20)) \
333 /* Almost done, just advance the input pointer. */ \
334 ++inptr; \
335 else if (set == JISX0201_set) \
337 /* Use the JIS X 0201 table. */ \
338 ch = jisx0201_to_ucs4 (ch + 0x80); \
339 if (ch == UNKNOWN_10646_CHAR) \
341 result = GCONV_ILLEGAL_INPUT; \
342 break; \
344 ++inptr; \
346 else if (set == ISO88591_set) \
348 /* This is quite easy. All characters are defined and the \
349 ISO 10646 value is computed by adding 0x80. */ \
350 ch += 0x80; \
351 ++inptr; \
353 else if (set == ISO88597_set) \
355 /* We use the table from the ISO 8859-7 module. */ \
356 ch = iso88597_to_ucs4[ch - 0x20]; \
357 if (ch == 0) \
359 result = GCONV_ILLEGAL_INPUT; \
360 break; \
362 ++inptr; \
364 else \
366 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
367 /* XXX I don't have the tables for these two old variants of \
368 JIS X 0208. Therefore I'm using the tables for JIS X \
369 0208-1990. If somebody has problems with this please \
370 provide the appropriate tables. */ \
371 ch = jisx0208_to_ucs4 (&inptr, \
372 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
373 else if (set == JISX0212_set) \
374 /* Use the JIS X 0212 table. */ \
375 ch = jisx0212_to_ucs4 (&inptr, \
376 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
377 else if (set == GB2312_set) \
378 /* Use the GB 2312 table. */ \
379 ch = gb2312_to_ucs4 (&inptr, \
380 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
381 else \
383 assert (set == KSC5601_set); \
385 /* Use the KSC 5601 table. */ \
386 ch = ksc5601_to_ucs4 (&inptr, \
387 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
390 if (NEED_LENGTH_TEST && ch == 0) \
392 result = GCONV_EMPTY_INPUT; \
393 break; \
395 else if (ch == UNKNOWN_10646_CHAR) \
397 result = GCONV_ILLEGAL_INPUT; \
398 break; \
402 *((uint32_t *) outptr)++ = ch; \
404 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
405 #define INIT_PARAMS int set = *setp
406 #define UPDATE_PARAMS *setp = set
407 #include <iconv/loop.c>
410 /* Next, define the other direction. */
411 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
412 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
413 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
414 #define LOOPFCT TO_LOOP
415 #define BODY \
417 unsigned char ch; \
418 size_t written = 0; \
420 ch = *((uint32_t *) inptr); \
422 /* First see whether we can write the character using the currently \
423 selected character set. */ \
424 if (set == ASCII_set \
425 || (ch >= 0x01 && ((set < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
426 || (set >= ISO88591_set && ch < 0x20)))) \
428 /* Please note that the NUL byte is *not* matched if we are not \
429 currently using the ASCII charset. This is because we must \
430 switch to the initial state whenever a NUL byte is written. */ \
431 if (ch <= 0x7f) \
433 *outptr++ = ch; \
434 written = 1; \
437 else if (set == JISX0201_set) \
438 written = ucs4_to_jisx0201 (ch, outptr); \
439 else if (set == ISO88591_set) \
441 if (ch >= 0xa0 && ch <= 0xff) \
443 *outptr++ = ch - 0x80; \
444 written = 1; \
447 else if (set == ISO88597_set) \
449 const struct gap *rp = from_idx; \
451 while (ch > rp->end) \
452 ++rp; \
453 if (ch >= rp->start) \
455 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
456 if (res != '\0') \
458 *outptr++ = res; \
459 written = 1; \
463 else \
465 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
466 written = ucs4_to_jisx0208 (ch, outptr, \
467 (NEED_LENGTH_TEST \
468 ? outend - outptr : 2)); \
469 else if (set == JISX0212_set) \
470 written = ucs4_to_jisx0212 (ch, outptr, \
471 (NEED_LENGTH_TEST \
472 ? outend - outptr : 2)); \
473 else if (set == GB2312_set) \
474 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
475 ? outend - outptr : 2)); \
476 else \
478 assert (set == KSC5601_set); \
480 written = ucs4_to_ksc5601 (ch, outptr, \
481 (NEED_LENGTH_TEST \
482 ? outend - outptr : 2)); \
485 if (NEED_LENGTH_TEST && written == 0) \
487 result = GCONV_FULL_OUTPUT; \
488 break; \
492 if (written == UNKNOWN_10646_CHAR) \
494 /* Either this is an unknown character or we have to switch \
495 the currently selected character set. The character sets \
496 do not code entirely separate parts of ISO 10646 and \
497 therefore there is no single correct result. If we choose \
498 the character set to use wrong we might be end up with \
499 using yet another character set for the next character \
500 though the current and the next could be encoded with one \
501 character set. We leave this kind of optimization for \
502 later and now simply use a fixed order in which we test for \
503 availability */ \
505 /* First test whether we have at least three more bytes for \
506 the escape sequence. The two charsets which require four \
507 bytes will be handled later. */ \
508 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
510 result = GCONV_FULL_OUTPUT; \
511 break; \
514 if (ch <= 0x7f) \
516 /* We must encode using ASCII. First write out the \
517 escape sequence. */ \
518 *outptr++ = ESC; \
519 *outptr++ = '('; \
520 *outptr++ = 'B'; \
521 set = ASCII_set; \
523 if (NEED_LENGTH_TEST && outptr == outend) \
525 result = GCONV_FULL_OUTPUT; \
526 break; \
529 *outptr++ = ch; \
531 else if (ch >= 0xa0 && ch <= 0xff) \
533 /* This character set is not available in ISO-2022-JP. */ \
534 if (var == iso2022jp) \
536 result = GCONV_ILLEGAL_INPUT; \
537 break; \
540 /* We must use the ISO 8859-1 upper half. */ \
541 *outptr++ = ESC; \
542 *outptr++ = '.'; \
543 *outptr++ = 'A'; \
544 set = ISO88591_set; \
546 if (NEED_LENGTH_TEST && outptr == outend) \
548 result = GCONV_FULL_OUTPUT; \
549 break; \
552 *outptr++ = ch - 0x80; \
554 else \
556 /* Now it becomes difficult. We must search the other \
557 character sets one by one and we cannot use simple \
558 arithmetic to determine whether the character can be \
559 encoded using this set. */ \
560 size_t written; \
561 unsigned char buf[2]; \
563 written = ucs4_to_jisx0201 (ch, buf); \
564 if (written != UNKNOWN_10646_CHAR) \
566 /* We use JIS X 0201. */ \
567 *outptr++ = ESC; \
568 *outptr++ = '$'; \
569 *outptr++ = '@'; \
570 set = JISX0201_set; \
572 if (NEED_LENGTH_TEST && outptr == outend) \
574 result = GCONV_FULL_OUTPUT; \
575 break; \
578 *outptr++ = buf[0]; \
580 else \
582 written = ucs4_to_jisx0208 (ch, buf, 2); \
583 if (written != UNKNOWN_10646_CHAR) \
585 /* We use JIS X 0208. */ \
586 *outptr++ = ESC; \
587 *outptr++ = '$'; \
588 *outptr++ = 'B'; \
589 set = JISX0208_1983_set; \
591 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
593 result = GCONV_FULL_OUTPUT; \
594 break; \
597 *outptr++ = buf[0]; \
598 *outptr++ = buf[1]; \
600 else if (var == iso2022jp) \
602 /* We have no other choice. */ \
603 result = GCONV_ILLEGAL_INPUT; \
604 break; \
606 else \
608 written = ucs4_to_jisx0208 (ch, buf, 2); \
609 if (written != UNKNOWN_10646_CHAR) \
611 /* We use JIS X 0212. */ \
612 if (outptr + 4 > outend) \
614 result = GCONV_FULL_OUTPUT; \
615 break; \
617 *outptr++ = ESC; \
618 *outptr++ = '$'; \
619 *outptr++ = '('; \
620 *outptr++ = 'D'; \
621 set = JISX0212_set; \
623 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
625 result = GCONV_FULL_OUTPUT; \
626 break; \
629 *outptr++ = buf[0]; \
630 *outptr++ = buf[1]; \
632 else \
634 written = ucs4_to_gb2312 (ch, buf, 2); \
635 if (written != UNKNOWN_10646_CHAR) \
637 /* We use GB 2312. */ \
638 *outptr++ = ESC; \
639 *outptr++ = '$'; \
640 *outptr++ = 'A'; \
641 set = GB2312_set; \
643 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
645 result = GCONV_FULL_OUTPUT; \
646 break; \
649 *outptr++ = buf[0]; \
650 *outptr++ = buf[1]; \
652 else \
654 written = ucs4_to_ksc5601 (ch, buf, 2); \
655 if (written != UNKNOWN_10646_CHAR) \
657 /* We use KSC 5601. */ \
658 if (outptr + 4 > outend) \
660 result = GCONV_FULL_OUTPUT; \
661 break; \
663 *outptr++ = ESC; \
664 *outptr++ = '$'; \
665 *outptr++ = '('; \
666 *outptr++ = 'C'; \
667 set = KSC5601_set; \
669 if (NEED_LENGTH_TEST \
670 && outptr + 2 > outend) \
672 result = GCONV_FULL_OUTPUT; \
673 break; \
676 *outptr++ = buf[0]; \
677 *outptr++ = buf[1]; \
679 else \
681 result = GCONV_ILLEGAL_INPUT; \
682 break; \
691 /* Now that we wrote the output increment the input pointer. */ \
692 inptr += 4; \
694 #define EXTRA_LOOP_DECLS , enum variant var, int *setp
695 #define INIT_PARAMS int set = *setp
696 #define UPDATE_PARAMS *setp = set
697 #include <iconv/loop.c>
700 /* Now define the toplevel functions. */
701 #include <iconv/skeleton.c>