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