]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconvdata/iso-2022-jp.c
Update.
[thirdparty/glibc.git] / 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.
5
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.
10
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.
15
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. */
20
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"
29
30 struct gap
31 {
32 uint16_t start;
33 uint16_t end;
34 int32_t idx;
35 };
36
37 #include "iso8859-7jp.h"
38
39 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
40 #define ESC 0x1b
41
42 /* We provide our own initialization and destructor function. */
43 #define DEFINE_INIT 0
44 #define DEFINE_FINI 0
45
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;
58 #define END_LOOP \
59 data->statep->count = set;
60 #define EXTRA_LOOP_ARGS , var, set
61
62
63 /* Direction of the transformation. */
64 enum direction
65 {
66 illegal_dir,
67 to_iso2022jp,
68 from_iso2022jp
69 };
70
71 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
72 enum variant
73 {
74 illegal_var,
75 iso2022jp,
76 iso2022jp2
77 };
78
79
80 struct iso2022jp_data
81 {
82 enum direction dir;
83 enum variant var;
84 mbstate_t save_state;
85 };
86
87
88 /* The COUNT element of the state keeps track of the currently selected
89 character set. The possible values are: */
90 enum
91 {
92 ASCII_set = 0,
93 JISX0208_1978_set,
94 JISX0208_1983_set,
95 JISX0201_set,
96 GB2312_set,
97 KSC5601_set,
98 JISX0212_set,
99 ISO88591_set,
100 ISO88597_set
101 };
102
103
104 int
105 gconv_init (struct gconv_step *step)
106 {
107 /* Determine which direction. */
108 struct iso2022jp_data *new_data;
109 enum direction dir = illegal_dir;
110 enum variant var = illegal_var;
111 int result;
112
113 if (__strcasecmp (step->from_name, "ISO-2022-JP//") == 0)
114 {
115 dir = from_iso2022jp;
116 var = iso2022jp;
117 }
118 else if (__strcasecmp (step->to_name, "ISO-2022-JP//") == 0)
119 {
120 dir = to_iso2022jp;
121 var = iso2022jp;
122 }
123 else if (__strcasecmp (step->from_name, "ISO-2022-JP-2//") == 0)
124 {
125 dir = from_iso2022jp;
126 var = iso2022jp2;
127 }
128 else if (__strcasecmp (step->to_name, "ISO-2022-JP-2//") == 0)
129 {
130 dir = to_iso2022jp;
131 var = iso2022jp2;
132 }
133
134 result = GCONV_NOCONV;
135 if (dir != illegal_dir
136 && ((new_data
137 = (struct iso2022jp_data *) malloc (sizeof (struct iso2022jp_data)))
138 != NULL))
139 {
140 new_data->dir = dir;
141 new_data->var = var;
142 step->data = new_data;
143
144 if (dir == from_iso2022jp)
145 {
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;
150 }
151 else
152 {
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;
157 }
158
159 /* Yes, this is a stateful encoding. */
160 step->stateful = 1;
161
162 result = GCONV_OK;
163 }
164
165 return result;
166 }
167
168
169 void
170 gconv_end (struct gconv_step *data)
171 {
172 free (data->data);
173 }
174
175
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
178 flushing. */
179 #define EMIT_SHIFT_TO_INIT \
180 if (data->statep->count != 0) \
181 { \
182 enum direction dir = ((struct iso2022jp_data *) step->data)->dir; \
183 \
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; \
188 else \
189 { \
190 char *outbuf = data->outbuf; \
191 \
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; \
197 else \
198 { \
199 /* Write out the shift sequence. */ \
200 *outbuf++ = ESC; \
201 *outbuf++ = '('; \
202 *outbuf++ = 'B'; \
203 data->outbuf = outbuf; \
204 data->statep->count = 0; \
205 } \
206 } \
207 }
208
209
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) \
213 if (Save) \
214 ((struct iso2022jp_data *) step->data)->save_state.count \
215 = data->statep->count; \
216 else \
217 data->statep->count \
218 = ((struct iso2022jp_data *) step->data)->save_state.count
219
220
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
226 #define BODY \
227 { \
228 uint32_t ch = *inptr; \
229 \
230 /* This is a 7bit character set, disallow all 8bit characters. */ \
231 if (ch > 0x7f) \
232 { \
233 result = GCONV_ILLEGAL_INPUT; \
234 break; \
235 } \
236 \
237 /* Recognize escape sequences. */ \
238 if (ch == ESC) \
239 { \
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)) \
248 { \
249 /* Not enough input available. */ \
250 result = GCONV_EMPTY_INPUT; \
251 break; \
252 } \
253 \
254 if (inptr[1] == '(') \
255 { \
256 if (inptr[2] == 'B') \
257 { \
258 /* ASCII selected. */ \
259 set = ASCII_set; \
260 inptr += 3; \
261 continue; \
262 } \
263 else if (inptr[2] == 'J') \
264 { \
265 /* JIS X 0201 selected. */ \
266 set = JISX0201_set; \
267 inptr += 3; \
268 continue; \
269 } \
270 } \
271 else if (inptr[1] == '$') \
272 { \
273 if (inptr[2] == '@') \
274 { \
275 /* JIS X 0208-1978 selected. */ \
276 set = JISX0208_1978_set; \
277 inptr += 3; \
278 continue; \
279 } \
280 else if (inptr[2] == 'B') \
281 { \
282 /* JIS X 0208-1983 selected. */ \
283 set = JISX0208_1983_set; \
284 inptr += 3; \
285 continue; \
286 } \
287 else if (var == iso2022jp2) \
288 { \
289 if (inptr[2] == 'A') \
290 { \
291 /* GB 2312-1980 selected. */ \
292 set = GB2312_set; \
293 inptr += 3; \
294 continue; \
295 } \
296 else if (inptr[2] == '(') \
297 { \
298 if (inptr[3] == 'C') \
299 { \
300 /* KSC 5601-1987 selected. */ \
301 set = KSC5601_set; \
302 inptr += 4; \
303 continue; \
304 } \
305 else if (inptr[3] == 'D') \
306 { \
307 /* JIS X 0212-1990 selected. */ \
308 set = JISX0212_set; \
309 inptr += 4; \
310 continue; \
311 } \
312 } \
313 } \
314 } \
315 else if (var == iso2022jp2 && inptr[1] == '.') \
316 { \
317 if (inptr[2] == 'A') \
318 { \
319 /* ISO 8859-1-GR selected. */ \
320 set = ISO88591_set; \
321 inptr += 3; \
322 continue; \
323 } \
324 else if (inptr[2] == 'F') \
325 { \
326 /* ISO 8859-7-GR selected. */ \
327 set = ISO88597_set; \
328 inptr += 3; \
329 continue; \
330 } \
331 } \
332 } \
333 \
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. */ \
338 ++inptr; \
339 else if (set == JISX0201_set) \
340 { \
341 /* Use the JIS X 0201 table. */ \
342 ch = jisx0201_to_ucs4 (ch + 0x80); \
343 if (ch == UNKNOWN_10646_CHAR) \
344 { \
345 result = GCONV_ILLEGAL_INPUT; \
346 break; \
347 } \
348 ++inptr; \
349 } \
350 else if (set == ISO88591_set) \
351 { \
352 /* This is quite easy. All characters are defined and the \
353 ISO 10646 value is computed by adding 0x80. */ \
354 ch += 0x80; \
355 ++inptr; \
356 } \
357 else if (set == ISO88597_set) \
358 { \
359 /* We use the table from the ISO 8859-7 module. */ \
360 ch = iso88597_to_ucs4[ch - 0x20]; \
361 if (ch == 0) \
362 { \
363 result = GCONV_ILLEGAL_INPUT; \
364 break; \
365 } \
366 ++inptr; \
367 } \
368 else \
369 { \
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); \
385 else \
386 { \
387 assert (set == KSC5601_set); \
388 \
389 /* Use the KSC 5601 table. */ \
390 ch = ksc5601_to_ucs4 (&inptr, \
391 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
392 } \
393 \
394 if (NEED_LENGTH_TEST && ch == 0) \
395 { \
396 result = GCONV_EMPTY_INPUT; \
397 break; \
398 } \
399 else if (ch == UNKNOWN_10646_CHAR) \
400 { \
401 result = GCONV_ILLEGAL_INPUT; \
402 break; \
403 } \
404 } \
405 \
406 *((uint32_t *) outptr)++ = ch; \
407 }
408 #define EXTRA_LOOP_DECLS , enum variant var, int set
409 #include <iconv/loop.c>
410
411
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
417 #define BODY \
418 { \
419 unsigned char ch; \
420 size_t written = 0; \
421 \
422 ch = *((uint32_t *) inptr); \
423 \
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)))) \
429 { \
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. */ \
433 if (ch <= 0x7f) \
434 { \
435 *outptr++ = ch; \
436 written = 1; \
437 } \
438 } \
439 else if (set == JISX0201_set) \
440 written = ucs4_to_jisx0201 (ch, outptr); \
441 else if (set == ISO88591_set) \
442 { \
443 if (ch >= 0xa0 && ch <= 0xff) \
444 { \
445 *outptr++ = ch - 0x80; \
446 written = 1; \
447 } \
448 } \
449 else if (set == ISO88597_set) \
450 { \
451 const struct gap *rp = from_idx; \
452 \
453 while (ch > rp->end) \
454 ++rp; \
455 if (ch >= rp->start) \
456 { \
457 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
458 if (res != '\0') \
459 { \
460 *outptr++ = res; \
461 written = 1; \
462 } \
463 } \
464 } \
465 else \
466 { \
467 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
468 written = ucs4_to_jisx0208 (ch, outptr, \
469 (NEED_LENGTH_TEST \
470 ? outend - outptr : 2)); \
471 else if (set == JISX0212_set) \
472 written = ucs4_to_jisx0212 (ch, outptr, \
473 (NEED_LENGTH_TEST \
474 ? outend - outptr : 2)); \
475 else if (set == GB2312_set) \
476 written = ucs4_to_gb2312 (ch, outptr, (NEED_LENGTH_TEST \
477 ? outend - outptr : 2)); \
478 else \
479 { \
480 assert (set == KSC5601_set); \
481 \
482 written = ucs4_to_ksc5601 (ch, outptr, \
483 (NEED_LENGTH_TEST \
484 ? outend - outptr : 2)); \
485 } \
486 \
487 if (NEED_LENGTH_TEST && written == 0) \
488 { \
489 result = GCONV_FULL_OUTPUT; \
490 break; \
491 } \
492 } \
493 \
494 if (written == UNKNOWN_10646_CHAR) \
495 { \
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 \
505 availability */ \
506 \
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) \
511 { \
512 result = GCONV_FULL_OUTPUT; \
513 break; \
514 } \
515 \
516 if (ch <= 0x7f) \
517 { \
518 /* We must encode using ASCII. First write out the \
519 escape sequence. */ \
520 *outptr++ = ESC; \
521 *outptr++ = '('; \
522 *outptr++ = 'B'; \
523 set = ASCII_set; \
524 \
525 if (NEED_LENGTH_TEST && outptr == outend) \
526 { \
527 result = GCONV_FULL_OUTPUT; \
528 break; \
529 } \
530 \
531 *outptr++ = ch; \
532 } \
533 else if (ch >= 0xa0 && ch <= 0xff) \
534 { \
535 /* This character set is not available in ISO-2022-JP. */ \
536 if (var == iso2022jp) \
537 { \
538 result = GCONV_ILLEGAL_INPUT; \
539 break; \
540 } \
541 \
542 /* We must use the ISO 8859-1 upper half. */ \
543 *outptr++ = ESC; \
544 *outptr++ = '.'; \
545 *outptr++ = 'A'; \
546 set = ISO88591_set; \
547 \
548 if (NEED_LENGTH_TEST && outptr == outend) \
549 { \
550 result = GCONV_FULL_OUTPUT; \
551 break; \
552 } \
553 \
554 *outptr++ = ch - 0x80; \
555 } \
556 else \
557 { \
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. */ \
562 size_t written; \
563 unsigned char buf[2]; \
564 \
565 written = ucs4_to_jisx0201 (ch, buf); \
566 if (written != UNKNOWN_10646_CHAR) \
567 { \
568 /* We use JIS X 0201. */ \
569 *outptr++ = ESC; \
570 *outptr++ = '$'; \
571 *outptr++ = '@'; \
572 set = JISX0201_set; \
573 \
574 if (NEED_LENGTH_TEST && outptr == outend) \
575 { \
576 result = GCONV_FULL_OUTPUT; \
577 break; \
578 } \
579 \
580 *outptr++ = buf[0]; \
581 } \
582 else \
583 { \
584 written = ucs4_to_jisx0208 (ch, buf, 2); \
585 if (written != UNKNOWN_10646_CHAR) \
586 { \
587 /* We use JIS X 0208. */ \
588 *outptr++ = ESC; \
589 *outptr++ = '$'; \
590 *outptr++ = 'B'; \
591 set = JISX0208_1983_set; \
592 \
593 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
594 { \
595 result = GCONV_FULL_OUTPUT; \
596 break; \
597 } \
598 \
599 *outptr++ = buf[0]; \
600 *outptr++ = buf[1]; \
601 } \
602 else if (var == iso2022jp) \
603 { \
604 /* We have no other choice. */ \
605 result = GCONV_ILLEGAL_INPUT; \
606 break; \
607 } \
608 else \
609 { \
610 written = ucs4_to_jisx0208 (ch, buf, 2); \
611 if (written != UNKNOWN_10646_CHAR) \
612 { \
613 /* We use JIS X 0212. */ \
614 if (outptr + 4 > outend) \
615 { \
616 result = GCONV_FULL_OUTPUT; \
617 break; \
618 } \
619 *outptr++ = ESC; \
620 *outptr++ = '$'; \
621 *outptr++ = '('; \
622 *outptr++ = 'D'; \
623 set = JISX0212_set; \
624 \
625 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
626 { \
627 result = GCONV_FULL_OUTPUT; \
628 break; \
629 } \
630 \
631 *outptr++ = buf[0]; \
632 *outptr++ = buf[1]; \
633 } \
634 else \
635 { \
636 written = ucs4_to_gb2312 (ch, buf, 2); \
637 if (written != UNKNOWN_10646_CHAR) \
638 { \
639 /* We use GB 2312. */ \
640 *outptr++ = ESC; \
641 *outptr++ = '$'; \
642 *outptr++ = 'A'; \
643 set = GB2312_set; \
644 \
645 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
646 { \
647 result = GCONV_FULL_OUTPUT; \
648 break; \
649 } \
650 \
651 *outptr++ = buf[0]; \
652 *outptr++ = buf[1]; \
653 } \
654 else \
655 { \
656 written = ucs4_to_ksc5601 (ch, buf, 2); \
657 if (written != UNKNOWN_10646_CHAR) \
658 { \
659 /* We use KSC 5601. */ \
660 if (outptr + 4 > outend) \
661 { \
662 result = GCONV_FULL_OUTPUT; \
663 break; \
664 } \
665 *outptr++ = ESC; \
666 *outptr++ = '$'; \
667 *outptr++ = '('; \
668 *outptr++ = 'C'; \
669 set = KSC5601_set; \
670 \
671 if (NEED_LENGTH_TEST \
672 && outptr + 2 > outend) \
673 { \
674 result = GCONV_FULL_OUTPUT; \
675 break; \
676 } \
677 \
678 *outptr++ = buf[0]; \
679 *outptr++ = buf[1]; \
680 } \
681 else \
682 { \
683 result = GCONV_ILLEGAL_INPUT; \
684 break; \
685 } \
686 } \
687 } \
688 } \
689 } \
690 } \
691 } \
692 \
693 /* Now that we wrote the output increment the input pointer. */ \
694 inptr += 4; \
695 }
696 #define EXTRA_LOOP_DECLS , enum variant var, int set
697 #include <iconv/loop.c>
698
699
700 /* Now define the toplevel functions. */
701 #include <iconv/skeleton.c>