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. */
37 #include "iso8859-7jp.h"
39 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
42 /* We provide our own initialization and destructor function. */
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; \
58 int *setp = &data->statep->count;
59 #define EXTRA_LOOP_ARGS , var, setp
62 /* Direction of the transformation. */
70 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
86 /* The COUNT element of the state keeps track of the currently selected
87 character set. The possible values are: */
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
;
111 if (__strcasecmp (step
->from_name
, "ISO-2022-JP//") == 0)
113 dir
= from_iso2022jp
;
116 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP//") == 0)
121 else if (__strcasecmp (step
->from_name
, "ISO-2022-JP-2//") == 0)
123 dir
= from_iso2022jp
;
126 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP-2//") == 0)
132 result
= GCONV_NOCONV
;
133 if (dir
!= illegal_dir
135 = (struct iso2022jp_data
*) malloc (sizeof (struct iso2022jp_data
)))
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
;
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. */
168 gconv_end (struct gconv_step
*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
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; \
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; \
197 /* Write out the shift sequence. */ \
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) \
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
224 uint32_t ch = *inptr; \
226 /* This is a 7bit character set, disallow all 8bit characters. */ \
229 result = GCONV_ILLEGAL_INPUT; \
233 /* Recognize escape sequences. */ \
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; \
250 if (inptr[1] == '(') \
252 if (inptr[2] == 'B') \
254 /* ASCII selected. */ \
259 else if (inptr[2] == 'J') \
261 /* JIS X 0201 selected. */ \
262 set = JISX0201_set; \
267 else if (inptr[1] == '$') \
269 if (inptr[2] == '@') \
271 /* JIS X 0208-1978 selected. */ \
272 set = JISX0208_1978_set; \
276 else if (inptr[2] == 'B') \
278 /* JIS X 0208-1983 selected. */ \
279 set = JISX0208_1983_set; \
283 else if (var == iso2022jp2) \
285 if (inptr[2] == 'A') \
287 /* GB 2312-1980 selected. */ \
292 else if (inptr[2] == '(') \
294 if (inptr[3] == 'C') \
296 /* KSC 5601-1987 selected. */ \
301 else if (inptr[3] == 'D') \
303 /* JIS X 0212-1990 selected. */ \
304 set = JISX0212_set; \
311 else if (var == iso2022jp2 && inptr[1] == '.') \
313 if (inptr[2] == 'A') \
315 /* ISO 8859-1-GR selected. */ \
316 set = ISO88591_set; \
320 else if (inptr[2] == 'F') \
322 /* ISO 8859-7-GR selected. */ \
323 set = ISO88597_set; \
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. */ \
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; \
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. */ \
353 else if (set == ISO88597_set) \
355 /* We use the table from the ISO 8859-7 module. */ \
356 ch = iso88597_to_ucs4[ch - 0x20]; \
359 result = GCONV_ILLEGAL_INPUT; \
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); \
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; \
395 else if (ch == UNKNOWN_10646_CHAR) \
397 result = GCONV_ILLEGAL_INPUT; \
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
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. */ \
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; \
447 else if (set == ISO88597_set) \
449 const struct gap *rp = from_idx; \
451 while (ch > rp->end) \
453 if (ch >= rp->start) \
455 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
465 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
466 written = ucs4_to_jisx0208 (ch, outptr, \
468 ? outend - outptr : 2)); \
469 else if (set == JISX0212_set) \
470 written = ucs4_to_jisx0212 (ch, outptr, \
472 ? outend - outptr : 2)); \
473 else if (set == GB2312_set) \
474 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
475 ? outend - outptr : 2)); \
478 assert (set == KSC5601_set); \
480 written = ucs4_to_ksc5601 (ch, outptr, \
482 ? outend - outptr : 2)); \
485 if (NEED_LENGTH_TEST && written == 0) \
487 result = GCONV_FULL_OUTPUT; \
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 \
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; \
516 /* We must encode using ASCII. First write out the \
517 escape sequence. */ \
523 if (NEED_LENGTH_TEST && outptr == outend) \
525 result = GCONV_FULL_OUTPUT; \
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; \
540 /* We must use the ISO 8859-1 upper half. */ \
544 set = ISO88591_set; \
546 if (NEED_LENGTH_TEST && outptr == outend) \
548 result = GCONV_FULL_OUTPUT; \
552 *outptr++ = ch - 0x80; \
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. */ \
561 unsigned char buf[2]; \
563 written = ucs4_to_jisx0201 (ch, buf); \
564 if (written != UNKNOWN_10646_CHAR) \
566 /* We use JIS X 0201. */ \
570 set = JISX0201_set; \
572 if (NEED_LENGTH_TEST && outptr == outend) \
574 result = GCONV_FULL_OUTPUT; \
578 *outptr++ = buf[0]; \
582 written = ucs4_to_jisx0208 (ch, buf, 2); \
583 if (written != UNKNOWN_10646_CHAR) \
585 /* We use JIS X 0208. */ \
589 set = JISX0208_1983_set; \
591 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
593 result = GCONV_FULL_OUTPUT; \
597 *outptr++ = buf[0]; \
598 *outptr++ = buf[1]; \
600 else if (var == iso2022jp) \
602 /* We have no other choice. */ \
603 result = GCONV_ILLEGAL_INPUT; \
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; \
621 set = JISX0212_set; \
623 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
625 result = GCONV_FULL_OUTPUT; \
629 *outptr++ = buf[0]; \
630 *outptr++ = buf[1]; \
634 written = ucs4_to_gb2312 (ch, buf, 2); \
635 if (written != UNKNOWN_10646_CHAR) \
637 /* We use GB 2312. */ \
643 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
645 result = GCONV_FULL_OUTPUT; \
649 *outptr++ = buf[0]; \
650 *outptr++ = buf[1]; \
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; \
669 if (NEED_LENGTH_TEST \
670 && outptr + 2 > outend) \
672 result = GCONV_FULL_OUTPUT; \
676 *outptr++ = buf[0]; \
677 *outptr++ = buf[1]; \
681 result = GCONV_ILLEGAL_INPUT; \
691 /* Now that we wrote the output increment the input pointer. */ \
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>