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: */
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
;
112 if (__strcasecmp (step
->from_name
, "ISO-2022-JP//") == 0)
114 dir
= from_iso2022jp
;
117 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP//") == 0)
122 else if (__strcasecmp (step
->from_name
, "ISO-2022-JP-2//") == 0)
124 dir
= from_iso2022jp
;
127 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP-2//") == 0)
133 result
= GCONV_NOCONV
;
134 if (dir
!= illegal_dir
136 = (struct iso2022jp_data
*) malloc (sizeof (struct iso2022jp_data
)))
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
;
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. */
169 gconv_end (struct gconv_step
*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
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; \
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; \
198 /* Write out the shift sequence. */ \
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) \
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
225 uint32_t ch = *inptr; \
227 /* This is a 7bit character set, disallow all 8bit characters. */ \
230 result = GCONV_ILLEGAL_INPUT; \
234 /* Recognize escape sequences. */ \
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; \
251 if (inptr[1] == '(') \
253 if (inptr[2] == 'B') \
255 /* ASCII selected. */ \
260 else if (inptr[2] == 'J') \
262 /* JIS X 0201 selected. */ \
263 set = JISX0201_set; \
268 else if (inptr[1] == '$') \
270 if (inptr[2] == '@') \
272 /* JIS X 0208-1978 selected. */ \
273 set = JISX0208_1978_set; \
277 else if (inptr[2] == 'B') \
279 /* JIS X 0208-1983 selected. */ \
280 set = JISX0208_1983_set; \
284 else if (var == iso2022jp2) \
286 if (inptr[2] == 'A') \
288 /* GB 2312-1980 selected. */ \
293 else if (inptr[2] == '(') \
295 if (inptr[3] == 'C') \
297 /* KSC 5601-1987 selected. */ \
302 else if (inptr[3] == 'D') \
304 /* JIS X 0212-1990 selected. */ \
305 set = JISX0212_set; \
312 else if (var == iso2022jp2 && inptr[1] == '.') \
314 if (inptr[2] == 'A') \
316 /* ISO 8859-1-GR selected. */ \
317 set = ISO88591_set; \
321 else if (inptr[2] == 'F') \
323 /* ISO 8859-7-GR selected. */ \
324 set = ISO88597_set; \
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. */ \
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; \
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. */ \
354 else if (set == ISO88597_set) \
356 /* We use the table from the ISO 8859-7 module. */ \
357 ch = iso88597_to_ucs4[ch - 0x20]; \
360 result = GCONV_ILLEGAL_INPUT; \
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); \
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; \
396 else if (ch == UNKNOWN_10646_CHAR) \
398 result = GCONV_ILLEGAL_INPUT; \
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
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. */ \
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; \
448 else if (set == ISO88597_set) \
450 const struct gap *rp = from_idx; \
452 while (ch > rp->end) \
454 if (ch >= rp->start) \
456 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
466 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
467 written = ucs4_to_jisx0208 (ch, outptr, \
469 ? outend - outptr : 2)); \
470 else if (set == JISX0212_set) \
471 written = ucs4_to_jisx0212 (ch, outptr, \
473 ? outend - outptr : 2)); \
474 else if (set == GB2312_set) \
475 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
476 ? outend - outptr : 2)); \
479 assert (set == KSC5601_set); \
481 written = ucs4_to_ksc5601 (ch, outptr, \
483 ? outend - outptr : 2)); \
486 if (NEED_LENGTH_TEST && written == 0) \
488 result = GCONV_FULL_OUTPUT; \
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 \
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; \
517 /* We must encode using ASCII. First write out the \
518 escape sequence. */ \
524 if (NEED_LENGTH_TEST && outptr == outend) \
526 result = GCONV_FULL_OUTPUT; \
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; \
541 /* We must use the ISO 8859-1 upper half. */ \
545 set = ISO88591_set; \
547 if (NEED_LENGTH_TEST && outptr == outend) \
549 result = GCONV_FULL_OUTPUT; \
553 *outptr++ = ch - 0x80; \
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. */ \
562 unsigned char buf[2]; \
564 written = ucs4_to_jisx0201 (ch, buf); \
565 if (written != UNKNOWN_10646_CHAR) \
567 /* We use JIS X 0201. */ \
571 set = JISX0201_set; \
573 if (NEED_LENGTH_TEST && outptr == outend) \
575 result = GCONV_FULL_OUTPUT; \
579 *outptr++ = buf[0]; \
583 written = ucs4_to_jisx0208 (ch, buf, 2); \
584 if (written != UNKNOWN_10646_CHAR) \
586 /* We use JIS X 0208. */ \
590 set = JISX0208_1983_set; \
592 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
594 result = GCONV_FULL_OUTPUT; \
598 *outptr++ = buf[0]; \
599 *outptr++ = buf[1]; \
601 else if (var == iso2022jp) \
603 /* We have no other choice. */ \
604 result = GCONV_ILLEGAL_INPUT; \
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; \
622 set = JISX0212_set; \
624 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
626 result = GCONV_FULL_OUTPUT; \
630 *outptr++ = buf[0]; \
631 *outptr++ = buf[1]; \
635 written = ucs4_to_gb2312 (ch, buf, 2); \
636 if (written != UNKNOWN_10646_CHAR) \
638 /* We use GB 2312. */ \
644 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
646 result = GCONV_FULL_OUTPUT; \
650 *outptr++ = buf[0]; \
651 *outptr++ = buf[1]; \
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; \
670 if (NEED_LENGTH_TEST \
671 && outptr + 2 > outend) \
673 result = GCONV_FULL_OUTPUT; \
677 *outptr++ = buf[0]; \
678 *outptr++ = buf[1]; \
682 result = GCONV_ILLEGAL_INPUT; \
692 /* Now that we wrote the output increment the input pointer. */ \
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>