]>
git.ipfire.org Git - thirdparty/glibc.git/blob - iconvdata/iso-2022-jp.c
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; \
57 int set = data->statep->count;
59 data->statep->count = set;
60 #define EXTRA_LOOP_ARGS , var, set
63 /* Direction of the transformation. */
71 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
88 /* The COUNT element of the state keeps track of the currently selected
89 character set. The possible values are: */
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
;
113 if (__strcasecmp (step
->from_name
, "ISO-2022-JP//") == 0)
115 dir
= from_iso2022jp
;
118 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP//") == 0)
123 else if (__strcasecmp (step
->from_name
, "ISO-2022-JP-2//") == 0)
125 dir
= from_iso2022jp
;
128 else if (__strcasecmp (step
->to_name
, "ISO-2022-JP-2//") == 0)
134 result
= GCONV_NOCONV
;
135 if (dir
!= illegal_dir
137 = (struct iso2022jp_data
*) malloc (sizeof (struct iso2022jp_data
)))
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
;
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. */
170 gconv_end (struct gconv_step
*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
179 #define EMIT_SHIFT_TO_INIT \
180 if (data->statep->count != 0) \
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 = 0; \
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; \
199 /* Write out the shift sequence. */ \
203 data->outbuf = outbuf; \
204 data->statep->count = 0; \
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) \
214 ((struct iso2022jp_data *) step->data)->save_state.count \
215 = data->statep->count; \
217 data->statep->count \
218 = ((struct iso2022jp_data *) step->data)->save_state.count
221 /* First define the conversion function from ISO-2022-JP to UCS4. */
222 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
223 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
224 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
225 #define LOOPFCT FROM_LOOP
228 uint32_t ch = *inptr; \
230 /* This is a 7bit character set, disallow all 8bit characters. */ \
233 result = GCONV_ILLEGAL_INPUT; \
237 /* Recognize escape sequences. */ \
240 /* We now must be prepared to read two to three more \
241 chracters. If we have a match in the first character but \
242 then the input buffer ends we terminate with an error since \
243 we must not risk missing an escape sequence just because it \
244 is not entirely in the current input buffer. */ \
245 if (inptr + 2 >= inend \
246 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
247 && inptr + 3 >= inend)) \
249 /* Not enough input available. */ \
250 result = GCONV_EMPTY_INPUT; \
254 if (inptr[1] == '(') \
256 if (inptr[2] == 'B') \
258 /* ASCII selected. */ \
263 else if (inptr[2] == 'J') \
265 /* JIS X 0201 selected. */ \
266 set = JISX0201_set; \
271 else if (inptr[1] == '$') \
273 if (inptr[2] == '@') \
275 /* JIS X 0208-1978 selected. */ \
276 set = JISX0208_1978_set; \
280 else if (inptr[2] == 'B') \
282 /* JIS X 0208-1983 selected. */ \
283 set = JISX0208_1983_set; \
287 else if (var == iso2022jp2) \
289 if (inptr[2] == 'A') \
291 /* GB 2312-1980 selected. */ \
296 else if (inptr[2] == '(') \
298 if (inptr[3] == 'C') \
300 /* KSC 5601-1987 selected. */ \
305 else if (inptr[3] == 'D') \
307 /* JIS X 0212-1990 selected. */ \
308 set = JISX0212_set; \
315 else if (var == iso2022jp2 && inptr[1] == '.') \
317 if (inptr[2] == 'A') \
319 /* ISO 8859-1-GR selected. */ \
320 set = ISO88591_set; \
324 else if (inptr[2] == 'F') \
326 /* ISO 8859-7-GR selected. */ \
327 set = ISO88597_set; \
334 if (set == ASCII_set \
335 || (var < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
336 || (var >= ISO88591_set && ch < 0x20)) \
337 /* Almost done, just advance the input pointer. */ \
339 else if (set == JISX0201_set) \
341 /* Use the JIS X 0201 table. */ \
342 ch = jisx0201_to_ucs4 (ch + 0x80); \
343 if (ch == UNKNOWN_10646_CHAR) \
345 result = GCONV_ILLEGAL_INPUT; \
350 else if (set == ISO88591_set) \
352 /* This is quite easy. All characters are defined and the \
353 ISO 10646 value is computed by adding 0x80. */ \
357 else if (set == ISO88597_set) \
359 /* We use the table from the ISO 8859-7 module. */ \
360 ch = iso88597_to_ucs4[ch - 0x20]; \
363 result = GCONV_ILLEGAL_INPUT; \
370 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
371 /* XXX I don't have the tables for these two old variants of \
372 JIS X 0208. Therefore I'm using the tables for JIS X \
373 0208-1990. If somebody has problems with this please \
374 provide the appropriate tables. */ \
375 ch = jisx0208_to_ucs4 (&inptr, \
376 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
377 else if (set == JISX0212_set) \
378 /* Use the JIS X 0212 table. */ \
379 ch = jisx0212_to_ucs4 (&inptr, \
380 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
381 else if (set == GB2312_set) \
382 /* Use the GB 2312 table. */ \
383 ch = gb2312_to_ucs4 (&inptr, \
384 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
387 assert (set == KSC5601_set); \
389 /* Use the KSC 5601 table. */ \
390 ch = ksc5601_to_ucs4 (&inptr, \
391 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
394 if (NEED_LENGTH_TEST && ch == 0) \
396 result = GCONV_EMPTY_INPUT; \
399 else if (ch == UNKNOWN_10646_CHAR) \
401 result = GCONV_ILLEGAL_INPUT; \
406 *((uint32_t *) outptr)++ = ch; \
408 #define EXTRA_LOOP_DECLS , enum variant var, int set
409 #include <iconv/loop.c>
412 /* Next, define the other direction. */
413 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
414 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
415 #define MAX_NEEDED_OUTPUT (MAX_NEEDED_FROM + 2)
416 #define LOOPFCT TO_LOOP
420 size_t written = 0; \
422 ch = *((uint32_t *) inptr); \
424 /* First see whether we can write the character using the currently \
425 selected character set. */ \
426 if (set == ASCII_set \
427 || (ch >= 0x01 && ((set < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
428 || (set >= ISO88591_set && ch < 0x20)))) \
430 /* Please note that the NUL byte is *not* matched if we are not \
431 currently using the ASCII charset. This is because we must \
432 switch to the initial state whenever a NUL byte is written. */ \
439 else if (set == JISX0201_set) \
440 written = ucs4_to_jisx0201 (ch, outptr); \
441 else if (set == ISO88591_set) \
443 if (ch >= 0xa0 && ch <= 0xff) \
445 *outptr++ = ch - 0x80; \
449 else if (set == ISO88597_set) \
451 const struct gap *rp = from_idx; \
453 while (ch > rp->end) \
455 if (ch >= rp->start) \
457 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
467 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
468 written = ucs4_to_jisx0208 (ch, outptr, \
470 ? outend - outptr : 2)); \
471 else if (set == JISX0212_set) \
472 written = ucs4_to_jisx0212 (ch, outptr, \
474 ? outend - outptr : 2)); \
475 else if (set == GB2312_set) \
476 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
477 ? outend - outptr : 2)); \
480 assert (set == KSC5601_set); \
482 written = ucs4_to_ksc5601 (ch, outptr, \
484 ? outend - outptr : 2)); \
487 if (NEED_LENGTH_TEST && written == 0) \
489 result = GCONV_FULL_OUTPUT; \
494 if (written == UNKNOWN_10646_CHAR) \
496 /* Either this is an unknown character or we have to switch \
497 the currently selected character set. The character sets \
498 do not code entirely separate parts of ISO 10646 and \
499 therefore there is no single correct result. If we choose \
500 the character set to use wrong we might be end up with \
501 using yet another character set for the next character \
502 though the current and the next could be encoded with one \
503 character set. We leave this kind of optimization for \
504 later and now simply use a fixed order in which we test for \
507 /* First test whether we have at least three more bytes for \
508 the escape sequence. The two charsets which require four \
509 bytes will be handled later. */ \
510 if (NEED_LENGTH_TEST && outptr + 3 > outend) \
512 result = GCONV_FULL_OUTPUT; \
518 /* We must encode using ASCII. First write out the \
519 escape sequence. */ \
525 if (NEED_LENGTH_TEST && outptr == outend) \
527 result = GCONV_FULL_OUTPUT; \
533 else if (ch >= 0xa0 && ch <= 0xff) \
535 /* This character set is not available in ISO-2022-JP. */ \
536 if (var == iso2022jp) \
538 result = GCONV_ILLEGAL_INPUT; \
542 /* We must use the ISO 8859-1 upper half. */ \
546 set = ISO88591_set; \
548 if (NEED_LENGTH_TEST && outptr == outend) \
550 result = GCONV_FULL_OUTPUT; \
554 *outptr++ = ch - 0x80; \
558 /* Now it becomes difficult. We must search the other \
559 character sets one by one and we cannot use simple \
560 arithmetic to determine whether the character can be \
561 encoded using this set. */ \
563 unsigned char buf[2]; \
565 written = ucs4_to_jisx0201 (ch, buf); \
566 if (written != UNKNOWN_10646_CHAR) \
568 /* We use JIS X 0201. */ \
572 set = JISX0201_set; \
574 if (NEED_LENGTH_TEST && outptr == outend) \
576 result = GCONV_FULL_OUTPUT; \
580 *outptr++ = buf[0]; \
584 written = ucs4_to_jisx0208 (ch, buf, 2); \
585 if (written != UNKNOWN_10646_CHAR) \
587 /* We use JIS X 0208. */ \
591 set = JISX0208_1983_set; \
593 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
595 result = GCONV_FULL_OUTPUT; \
599 *outptr++ = buf[0]; \
600 *outptr++ = buf[1]; \
602 else if (var == iso2022jp) \
604 /* We have no other choice. */ \
605 result = GCONV_ILLEGAL_INPUT; \
610 written = ucs4_to_jisx0208 (ch, buf, 2); \
611 if (written != UNKNOWN_10646_CHAR) \
613 /* We use JIS X 0212. */ \
614 if (outptr + 4 > outend) \
616 result = GCONV_FULL_OUTPUT; \
623 set = JISX0212_set; \
625 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
627 result = GCONV_FULL_OUTPUT; \
631 *outptr++ = buf[0]; \
632 *outptr++ = buf[1]; \
636 written = ucs4_to_gb2312 (ch, buf, 2); \
637 if (written != UNKNOWN_10646_CHAR) \
639 /* We use GB 2312. */ \
645 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
647 result = GCONV_FULL_OUTPUT; \
651 *outptr++ = buf[0]; \
652 *outptr++ = buf[1]; \
656 written = ucs4_to_ksc5601 (ch, buf, 2); \
657 if (written != UNKNOWN_10646_CHAR) \
659 /* We use KSC 5601. */ \
660 if (outptr + 4 > outend) \
662 result = GCONV_FULL_OUTPUT; \
671 if (NEED_LENGTH_TEST \
672 && outptr + 2 > outend) \
674 result = GCONV_FULL_OUTPUT; \
678 *outptr++ = buf[0]; \
679 *outptr++ = buf[1]; \
683 result = GCONV_ILLEGAL_INPUT; \
693 /* Now that we wrote the output increment the input pointer. */ \
696 #define EXTRA_LOOP_DECLS , enum variant var, int set
697 #include <iconv/loop.c>
700 /* Now define the toplevel functions. */
701 #include <iconv/skeleton.c>