]> 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 save_set; \
58 int set = data->statep->count;
59 #define END_LOOP \
60 data->statep->count = set;
61 #define EXTRA_LOOP_ARGS , var, set
62
63
64 /* Direction of the transformation. */
65 enum direction
66 {
67 illegal_dir,
68 to_iso2022jp,
69 from_iso2022jp
70 };
71
72 /* We handle ISO-2022-jp and ISO-2022-JP-2 here. */
73 enum variant
74 {
75 illegal_var,
76 iso2022jp,
77 iso2022jp2
78 };
79
80
81 struct iso2022jp_data
82 {
83 enum direction dir;
84 enum variant var;
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 save_set = set; \
215 else \
216 set = save_set
217
218
219 /* First define the conversion function from ISO-2022-JP to UCS4. */
220 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
221 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
222 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
223 #define LOOPFCT FROM_LOOP
224 #define BODY \
225 { \
226 uint32_t ch = *inptr; \
227 \
228 /* This is a 7bit character set, disallow all 8bit characters. */ \
229 if (ch > 0x7f) \
230 { \
231 result = GCONV_ILLEGAL_INPUT; \
232 break; \
233 } \
234 \
235 /* Recognize escape sequences. */ \
236 if (ch == ESC) \
237 { \
238 /* We now must be prepared to read two to three more \
239 chracters. If we have a match in the first character but \
240 then the input buffer ends we terminate with an error since \
241 we must not risk missing an escape sequence just because it \
242 is not entirely in the current input buffer. */ \
243 if (inptr + 2 >= inend \
244 || (var == iso2022jp2 && inptr[1] == '$' && inptr[2] == '(' \
245 && inptr + 3 >= inend)) \
246 { \
247 /* Not enough input available. */ \
248 result = GCONV_EMPTY_INPUT; \
249 break; \
250 } \
251 \
252 if (inptr[1] == '(') \
253 { \
254 if (inptr[2] == 'B') \
255 { \
256 /* ASCII selected. */ \
257 set = ASCII_set; \
258 inptr += 3; \
259 continue; \
260 } \
261 else if (inptr[2] == 'J') \
262 { \
263 /* JIS X 0201 selected. */ \
264 set = JISX0201_set; \
265 inptr += 3; \
266 continue; \
267 } \
268 } \
269 else if (inptr[1] == '$') \
270 { \
271 if (inptr[2] == '@') \
272 { \
273 /* JIS X 0208-1978 selected. */ \
274 set = JISX0208_1978_set; \
275 inptr += 3; \
276 continue; \
277 } \
278 else if (inptr[2] == 'B') \
279 { \
280 /* JIS X 0208-1983 selected. */ \
281 set = JISX0208_1983_set; \
282 inptr += 3; \
283 continue; \
284 } \
285 else if (var == iso2022jp2) \
286 { \
287 if (inptr[2] == 'A') \
288 { \
289 /* GB 2312-1980 selected. */ \
290 set = GB2312_set; \
291 inptr += 3; \
292 continue; \
293 } \
294 else if (inptr[2] == '(') \
295 { \
296 if (inptr[3] == 'C') \
297 { \
298 /* KSC 5601-1987 selected. */ \
299 set = KSC5601_set; \
300 inptr += 4; \
301 continue; \
302 } \
303 else if (inptr[3] == 'D') \
304 { \
305 /* JIS X 0212-1990 selected. */ \
306 set = JISX0212_set; \
307 inptr += 4; \
308 continue; \
309 } \
310 } \
311 } \
312 } \
313 else if (var == iso2022jp2 && inptr[1] == '.') \
314 { \
315 if (inptr[2] == 'A') \
316 { \
317 /* ISO 8859-1-GR selected. */ \
318 set = ISO88591_set; \
319 inptr += 3; \
320 continue; \
321 } \
322 else if (inptr[2] == 'F') \
323 { \
324 /* ISO 8859-7-GR selected. */ \
325 set = ISO88597_set; \
326 inptr += 3; \
327 continue; \
328 } \
329 } \
330 } \
331 \
332 if (set == ASCII_set \
333 || (var < ISO88591_set && (ch < 0x21 || ch == 0x7f)) \
334 || (var >= ISO88591_set && ch < 0x20)) \
335 /* Almost done, just advance the input pointer. */ \
336 ++inptr; \
337 else if (set == JISX0201_set) \
338 { \
339 /* Use the JIS X 0201 table. */ \
340 ch = jisx0201_to_ucs4 (ch + 0x80); \
341 if (ch == UNKNOWN_10646_CHAR) \
342 { \
343 result = GCONV_ILLEGAL_INPUT; \
344 break; \
345 } \
346 ++inptr; \
347 } \
348 else if (set == ISO88591_set) \
349 { \
350 /* This is quite easy. All characters are defined and the \
351 ISO 10646 value is computed by adding 0x80. */ \
352 ch += 0x80; \
353 ++inptr; \
354 } \
355 else if (set == ISO88597_set) \
356 { \
357 /* We use the table from the ISO 8859-7 module. */ \
358 ch = iso88597_to_ucs4[ch - 0x20]; \
359 if (ch == 0) \
360 { \
361 result = GCONV_ILLEGAL_INPUT; \
362 break; \
363 } \
364 ++inptr; \
365 } \
366 else \
367 { \
368 if (set == JISX0208_1978_set || set == JISX0208_1983_set) \
369 /* XXX I don't have the tables for these two old variants of \
370 JIS X 0208. Therefore I'm using the tables for JIS X \
371 0208-1990. If somebody has problems with this please \
372 provide the appropriate tables. */ \
373 ch = jisx0208_to_ucs4 (&inptr, \
374 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
375 else if (set == JISX0212_set) \
376 /* Use the JIS X 0212 table. */ \
377 ch = jisx0212_to_ucs4 (&inptr, \
378 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
379 else if (set == GB2312_set) \
380 /* Use the GB 2312 table. */ \
381 ch = gb2312_to_ucs4 (&inptr, \
382 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
383 else \
384 { \
385 assert (set == KSC5601_set); \
386 \
387 /* Use the KSC 5601 table. */ \
388 ch = ksc5601_to_ucs4 (&inptr, \
389 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
390 } \
391 \
392 if (NEED_LENGTH_TEST && ch == 0) \
393 { \
394 result = GCONV_EMPTY_INPUT; \
395 break; \
396 } \
397 else if (ch == UNKNOWN_10646_CHAR) \
398 { \
399 result = GCONV_ILLEGAL_INPUT; \
400 break; \
401 } \
402 } \
403 \
404 *((uint32_t *) outptr)++ = ch; \
405 }
406 #define EXTRA_LOOP_DECLS , enum variant var, int set
407 #include <iconv/loop.c>
408
409
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 \
416 { \
417 unsigned char ch; \
418 size_t written = 0; \
419 \
420 ch = *((uint32_t *) inptr); \
421 \
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)))) \
427 { \
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) \
432 { \
433 *outptr++ = ch; \
434 written = 1; \
435 } \
436 } \
437 else if (set == JISX0201_set) \
438 written = ucs4_to_jisx0201 (ch, outptr); \
439 else if (set == ISO88591_set) \
440 { \
441 if (ch >= 0xa0 && ch <= 0xff) \
442 { \
443 *outptr++ = ch - 0x80; \
444 written = 1; \
445 } \
446 } \
447 else if (set == ISO88597_set) \
448 { \
449 const struct gap *rp = from_idx; \
450 \
451 while (ch > rp->end) \
452 ++rp; \
453 if (ch >= rp->start) \
454 { \
455 unsigned char res = iso88597_from_ucs4[ch + rp->idx]; \
456 if (res != '\0') \
457 { \
458 *outptr++ = res; \
459 written = 1; \
460 } \
461 } \
462 } \
463 else \
464 { \
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 \
477 { \
478 assert (set == KSC5601_set); \
479 \
480 written = ucs4_to_ksc5601 (ch, outptr, \
481 (NEED_LENGTH_TEST \
482 ? outend - outptr : 2)); \
483 } \
484 \
485 if (NEED_LENGTH_TEST && written == 0) \
486 { \
487 result = GCONV_FULL_OUTPUT; \
488 break; \
489 } \
490 } \
491 \
492 if (written == UNKNOWN_10646_CHAR) \
493 { \
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 */ \
504 \
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) \
509 { \
510 result = GCONV_FULL_OUTPUT; \
511 break; \
512 } \
513 \
514 if (ch <= 0x7f) \
515 { \
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; \
522 \
523 if (NEED_LENGTH_TEST && outptr == outend) \
524 { \
525 result = GCONV_FULL_OUTPUT; \
526 break; \
527 } \
528 \
529 *outptr++ = ch; \
530 } \
531 else if (ch >= 0xa0 && ch <= 0xff) \
532 { \
533 /* This character set is not available in ISO-2022-JP. */ \
534 if (var == iso2022jp) \
535 { \
536 result = GCONV_ILLEGAL_INPUT; \
537 break; \
538 } \
539 \
540 /* We must use the ISO 8859-1 upper half. */ \
541 *outptr++ = ESC; \
542 *outptr++ = '.'; \
543 *outptr++ = 'A'; \
544 set = ISO88591_set; \
545 \
546 if (NEED_LENGTH_TEST && outptr == outend) \
547 { \
548 result = GCONV_FULL_OUTPUT; \
549 break; \
550 } \
551 \
552 *outptr++ = ch - 0x80; \
553 } \
554 else \
555 { \
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]; \
562 \
563 written = ucs4_to_jisx0201 (ch, buf); \
564 if (written != UNKNOWN_10646_CHAR) \
565 { \
566 /* We use JIS X 0201. */ \
567 *outptr++ = ESC; \
568 *outptr++ = '$'; \
569 *outptr++ = '@'; \
570 set = JISX0201_set; \
571 \
572 if (NEED_LENGTH_TEST && outptr == outend) \
573 { \
574 result = GCONV_FULL_OUTPUT; \
575 break; \
576 } \
577 \
578 *outptr++ = buf[0]; \
579 } \
580 else \
581 { \
582 written = ucs4_to_jisx0208 (ch, buf, 2); \
583 if (written != UNKNOWN_10646_CHAR) \
584 { \
585 /* We use JIS X 0208. */ \
586 *outptr++ = ESC; \
587 *outptr++ = '$'; \
588 *outptr++ = 'B'; \
589 set = JISX0208_1983_set; \
590 \
591 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
592 { \
593 result = GCONV_FULL_OUTPUT; \
594 break; \
595 } \
596 \
597 *outptr++ = buf[0]; \
598 *outptr++ = buf[1]; \
599 } \
600 else if (var == iso2022jp) \
601 { \
602 /* We have no other choice. */ \
603 result = GCONV_ILLEGAL_INPUT; \
604 break; \
605 } \
606 else \
607 { \
608 written = ucs4_to_jisx0208 (ch, buf, 2); \
609 if (written != UNKNOWN_10646_CHAR) \
610 { \
611 /* We use JIS X 0212. */ \
612 if (outptr + 4 > outend) \
613 { \
614 result = GCONV_FULL_OUTPUT; \
615 break; \
616 } \
617 *outptr++ = ESC; \
618 *outptr++ = '$'; \
619 *outptr++ = '('; \
620 *outptr++ = 'D'; \
621 set = JISX0212_set; \
622 \
623 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
624 { \
625 result = GCONV_FULL_OUTPUT; \
626 break; \
627 } \
628 \
629 *outptr++ = buf[0]; \
630 *outptr++ = buf[1]; \
631 } \
632 else \
633 { \
634 written = ucs4_to_gb2312 (ch, buf, 2); \
635 if (written != UNKNOWN_10646_CHAR) \
636 { \
637 /* We use GB 2312. */ \
638 *outptr++ = ESC; \
639 *outptr++ = '$'; \
640 *outptr++ = 'A'; \
641 set = GB2312_set; \
642 \
643 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
644 { \
645 result = GCONV_FULL_OUTPUT; \
646 break; \
647 } \
648 \
649 *outptr++ = buf[0]; \
650 *outptr++ = buf[1]; \
651 } \
652 else \
653 { \
654 written = ucs4_to_ksc5601 (ch, buf, 2); \
655 if (written != UNKNOWN_10646_CHAR) \
656 { \
657 /* We use KSC 5601. */ \
658 if (outptr + 4 > outend) \
659 { \
660 result = GCONV_FULL_OUTPUT; \
661 break; \
662 } \
663 *outptr++ = ESC; \
664 *outptr++ = '$'; \
665 *outptr++ = '('; \
666 *outptr++ = 'C'; \
667 set = KSC5601_set; \
668 \
669 if (NEED_LENGTH_TEST \
670 && outptr + 2 > outend) \
671 { \
672 result = GCONV_FULL_OUTPUT; \
673 break; \
674 } \
675 \
676 *outptr++ = buf[0]; \
677 *outptr++ = buf[1]; \
678 } \
679 else \
680 { \
681 result = GCONV_ILLEGAL_INPUT; \
682 break; \
683 } \
684 } \
685 } \
686 } \
687 } \
688 } \
689 } \
690 \
691 /* Now that we wrote the output increment the input pointer. */ \
692 inptr += 4; \
693 }
694 #define EXTRA_LOOP_DECLS , enum variant var, int set
695 #include <iconv/loop.c>
696
697
698 /* Now define the toplevel functions. */
699 #include <iconv/skeleton.c>