2 * iso2022.c - support for ISO/IEC 2022 (alias ECMA-35).
4 * This isn't a complete implementation of ISO/IEC 2022, but it's
5 * close. It only handles decoding, because a fully general encoder
6 * isn't really useful. It can decode 8-bit and 7-bit versions, with
7 * support for single-byte and multi-byte character sets, all four
8 * containers (G0, G1, G2, and G3), using both single-shift and
9 * locking-shift sequences.
11 * The general principle is that any valid ISO/IEC 2022 sequence
12 * should either be correctly decoded or should emit an ERROR. The
13 * only exception to this is that the C0 and C1 sets are fixed as
14 * those of ISO/IEC 6429. Escape sequences for designating control
15 * sets are passed through, so a post-processor could fix them up if
18 * DOCS is not currently supported. It will be one day.
35 enum {S4
, S6
, M4
, M6
};
37 static long int null_dbcs_to_unicode(int, int);
39 const struct iso2022_subcharset
{
42 const sbcs_data
*sbcs_base
;
43 long int (*dbcs_fn
)(int, int);
44 } iso2022_subcharsets
[] = {
45 { S4
, 0, 'B', 0x00, &sbcsdata_CS_ASCII
},
47 { S4
, 0, '<', 0x80, &sbcsdata_CS_DEC_MCS
},
48 { S4
, 0, 'I', 0x80, &sbcsdata_CS_JISX0201
},
49 { S4
, 0, 'J', 0x00, &sbcsdata_CS_JISX0201
},
51 { S6
, 0, 'A', 0x80, &sbcsdata_CS_ISO8859_1
},
52 { S6
, 0, 'B', 0x80, &sbcsdata_CS_ISO8859_2
},
53 { S6
, 0, 'C', 0x80, &sbcsdata_CS_ISO8859_3
},
54 { S6
, 0, 'D', 0x80, &sbcsdata_CS_ISO8859_4
},
55 { S6
, 0, 'F', 0x80, &sbcsdata_CS_ISO8859_7
},
56 { S6
, 0, 'G', 0x80, &sbcsdata_CS_ISO8859_6
},
57 { S6
, 0, 'H', 0x80, &sbcsdata_CS_ISO8859_8
},
58 { S6
, 0, 'L', 0x80, &sbcsdata_CS_ISO8859_5
},
59 { S6
, 0, 'M', 0x80, &sbcsdata_CS_ISO8859_9
},
60 { S6
, 0, 'T', 0x80, &sbcsdata_CS_ISO8859_11
},
61 { S6
, 0, 'V', 0x80, &sbcsdata_CS_ISO8859_10
},
62 { S6
, 0, 'Y', 0x80, &sbcsdata_CS_ISO8859_13
},
63 { S6
, 0, '_', 0x80, &sbcsdata_CS_ISO8859_14
},
64 { S6
, 0, 'b', 0x80, &sbcsdata_CS_ISO8859_15
},
65 { S6
, 0, 'f', 0x80, &sbcsdata_CS_ISO8859_16
},
66 { S6
, 0, '~' }, /* empty 96-set */
68 { M4
, 0, '@' }, /* JIS C 6226-1978 */
70 { M4
, 0, 'A', -0x21, 0, &gb2312_to_unicode
},
71 { M4
, 0, 'B', -0x21, 0, &jisx0208_to_unicode
},
72 { M4
, 0, 'C', -0x21, 0, &ksx1001_to_unicode
},
73 { M4
, 0, 'D', -0x21, 0, &jisx0212_to_unicode
},
74 { M4
, 0, '~', 0, 0, &null_dbcs_to_unicode
}, /* empty 94^n-set */
75 { M6
, 0, '~', 0, 0, &null_dbcs_to_unicode
}, /* empty 96^n-set */
78 static long int null_dbcs_to_unicode(int r
, int c
)
83 /* States, or "what we're currently accumulating". */
85 IDLE
, /* None of the below */
86 SS2CHAR
, /* Accumulating a character after SS2 */
87 SS3CHAR
, /* Accumulating a character after SS3 */
88 ESCSEQ
, /* Accumulating an escape sequence */
89 ESCDROP
, /* Discarding an escape sequence */
90 ESCPASS
, /* Passing through an escape sequence */
91 DOCSUTF8
/* DOCSed into UTF-8 */
96 static void dump_state(charset_state
*s
)
98 unsigned s0
= s
->s0
, s1
= s
->s1
;
99 char const * const modes
[] = { "IDLE", "SS2CHAR", "SS3CHAR",
100 "ESCSEQ", "ESCDROP", "ESCPASS",
103 fprintf(stderr
, "s0: %s", modes
[s0
>> 29]);
104 fprintf(stderr
, " %02x %02x %02x ", (s0
>> 16) & 0xff, (s0
>> 8) & 0xff,
106 fprintf(stderr
, "s1: LS%d LS%dR", (s1
>> 30) & 3, (s1
>> 28) & 3);
107 fprintf(stderr
, " %d %d %d %d\n", s1
& 0x7f, (s1
>> 7) & 0x7f,
108 (s1
>> 14) & 0x7f, (s1
>> 21) & 0x7f);
112 static void designate(charset_state
*state
, int container
,
113 int type
, int ibyte
, int fbyte
)
117 assert(container
>= 0 && container
<= 3);
118 assert(type
== S4
|| type
== S6
|| type
== M4
|| type
== M6
);
120 for (i
= 0; i
<= lenof(iso2022_subcharsets
); i
++) {
121 if (iso2022_subcharsets
[i
].type
== type
&&
122 iso2022_subcharsets
[i
].i
== ibyte
&&
123 iso2022_subcharsets
[i
].f
== fbyte
) {
124 state
->s1
&= ~(0x7fL
<< (container
* 7));
125 state
->s1
|= (i
<< (container
* 7));
130 * If we don't find the charset, invoke the empty one, so we
131 * output ERROR rather than garbage.
133 designate(state
, container
, type
, 0, '~');
136 static void do_utf8(long int input_chr
,
137 charset_state
*state
,
138 void (*emit
)(void *ctx
, long int output
),
141 charset_state ustate
;
142 charset_spec
const *utf8
;
145 ustate
.s0
= state
->s0
& 0x03ffffffL
;
146 utf8
= charset_find_spec(CS_UTF8
);
147 utf8
->read(utf8
, input_chr
, &ustate
, emit
, emitctx
);
148 state
->s0
= (state
->s0
& ~0x03ffffffL
) | (ustate
.s0
& 0x03ffffffL
);
151 static void docs_utf8(long int input_chr
,
152 charset_state
*state
,
153 void (*emit
)(void *ctx
, long int output
),
159 * Bits [25:0] of s0 are reserved for read_utf8().
160 * Bits [27:26] are a tiny state machine to recognise ESC % @.
162 retstate
= (state
->s0
& 0x0c000000L
) >> 26;
163 if (retstate
== 1 && input_chr
== '%')
165 else if (retstate
== 2 && input_chr
== '@') {
166 /* If we've got a partial UTF-8 sequence, complain. */
167 if (state
->s0
& 0x03ffffffL
)
168 emit(emitctx
, ERROR
);
172 if (retstate
>= 1) do_utf8(ESC
, state
, emit
, emitctx
);
173 if (retstate
>= 2) do_utf8('%', state
, emit
, emitctx
);
175 if (input_chr
== ESC
)
178 do_utf8(input_chr
, state
, emit
, emitctx
);
181 state
->s0
= (state
->s0
& ~0x0c000000L
) | (retstate
<< 26);
185 static void read_iso2022(charset_spec
const *charset
, long int input_chr
,
186 charset_state
*state
,
187 void (*emit
)(void *ctx
, long int output
),
191 /* dump_state(state); */
193 * We've got 64 bits of state to play with.
195 * Locking-shift state: 2 bits each GL/GR
196 * Single-shift state: 2 bits
197 * Charset designation state: n bits each G0/G1/G2/G3
198 * MBCS/esc seq accumulation: 14 bits (assume max 4-byte sets)
199 * MBCS state: 2 bits (off, ESC, GL, GR)
200 * For no good reason, put long-term state in s1, short term in s0.
202 * s0[31:29] = state enum
203 * s0[24:0] = accumulated bytes
204 * s1[31:30] = GL locking-shift state
205 * s1[29:28] = GR locking-shift state
206 * s1[27:21] = G3 charset
207 * s1[20:14] = G2 charset
208 * s1[13:7] = G1 charset
209 * s1[6:0] = G0 charset
214 #define LOCKING_SHIFT(n,side) \
215 (state->s1 = (state->s1 & ~(3L<<(side))) | ((n ## L)<<(side)))
216 #define MODE ((state->s0 & 0xe0000000L) >> 29)
217 #define ENTER_MODE(m) (state->s0 = (state->s0 & ~0xe0000000L) | ((m)<<29))
218 #define SINGLE_SHIFT(n) ENTER_MODE(SS2CHAR - 2 + (n))
219 #define ASSERT_IDLE do { \
220 if (state->s0 != 0) emit(emitctx, ERROR); \
224 if (state
->s1
== 0) {
226 * Since there's no LS0R, this means we must just have started.
227 * Set up a sane initial state (LS0, LS1R, ASCII in G0/G1/G2/G3).
229 LOCKING_SHIFT(0, LEFT
);
230 LOCKING_SHIFT(1, RIGHT
);
231 designate(state
, 0, S4
, 0, 'B');
232 designate(state
, 1, S4
, 0, 'B');
233 designate(state
, 2, S4
, 0, 'B');
234 designate(state
, 3, S4
, 0, 'B');
237 if (MODE
== DOCSUTF8
) {
238 docs_utf8(input_chr
, state
, emit
, emitctx
);
242 if ((input_chr
& 0x60) == 0x00) {
243 /* C0 or C1 control */
250 LOCKING_SHIFT(0, LEFT
);
253 LOCKING_SHIFT(1, LEFT
);
262 emit(emitctx
, input_chr
);
265 } else if ((input_chr
& 0x80) || MODE
< ESCSEQ
) {
267 struct iso2022_subcharset
const *subcs
;
272 * Force idle state if we're in mid escape sequence, or in a
273 * multi-byte character with a different top bit.
275 if (MODE
>= ESCSEQ
||
276 ((state
->s0
& 0x00ff0000L
) != 0 &&
277 (((state
->s0
>> 16) ^ input_chr
) & 0x80)))
279 if (MODE
== SS2CHAR
|| MODE
== SS3CHAR
) /* Single-shift */
280 container
= MODE
- SS2CHAR
+ 2;
281 else if (input_chr
>= 0x80) /* GR */
282 container
= (state
->s1
>> 28) & 3;
284 container
= state
->s1
>> 30;
287 input_7bit
= input_chr
& ~0x80;
288 subcs
= &iso2022_subcharsets
[(state
->s1
>> (container
* 7)) & 0x7f];
289 if ((subcs
->type
== S4
|| subcs
->type
== M4
) &&
290 (input_7bit
== 0x20 || input_7bit
== 0x7f)) {
291 /* characters not in 94-char set */
292 if (is_gl
) emit(emitctx
, input_7bit
);
293 else emit(emitctx
, ERROR
);
294 } else if (subcs
->type
== M4
|| subcs
->type
== M6
) {
295 if ((state
->s0
& 0x00ff0000L
) == 0) {
296 state
->s0
|= input_chr
<< 16;
300 subcs
->dbcs_fn(((state
->s0
>> 16) & 0x7f) + subcs
->offset
,
301 input_7bit
+ subcs
->offset
));
304 if ((state
->s0
& 0x00ff0000L
) != 0)
305 emit(emitctx
, ERROR
);
306 emit(emitctx
, subcs
->sbcs_base ?
307 sbcs_to_unicode(subcs
->sbcs_base
, input_7bit
+ subcs
->offset
):
313 if (MODE
== ESCPASS
) {
314 emit(emitctx
, input_chr
);
315 if ((input_chr
& 0xf0) != 0x20)
321 * Intermediate bytes shall be any of the 16 positions of
322 * column 02 of the code table; they are denoted by the symbol
325 if ((input_chr
& 0xf0) == 0x20) {
326 if (((state
->s0
>> 16) & 0xff) == 0)
327 state
->s0
|= input_chr
<< 16;
328 else if (((state
->s0
>> 8) & 0xff) == 0)
329 state
->s0
|= input_chr
<< 8;
331 /* Long escape sequence. Switch to ESCPASS or ESCDROP. */
332 i1
= (state
->s0
>> 16) & 0xff;
333 i2
= (state
->s0
>> 8) & 0xff;
335 case '(': case ')': case '*': case '+':
336 case '-': case '.': case '/':
344 emit(emitctx
, input_chr
);
354 * Final bytes shall be any of the 79 positions of columns 03
355 * to 07 of the code table excluding position 07/15; they are
356 * denoted by the symbol F.
358 i1
= (state
->s0
>> 16) & 0xff;
359 i2
= (state
->s0
>> 8) & 0xff;
361 input_chr
= 0; /* Make sure it won't match. */
364 case 0: /* No intermediate bytes */
373 LOCKING_SHIFT(2, LEFT
);
376 LOCKING_SHIFT(3, LEFT
);
379 LOCKING_SHIFT(3, RIGHT
);
382 LOCKING_SHIFT(2, RIGHT
);
385 LOCKING_SHIFT(1, RIGHT
);
388 /* Unsupported escape sequence. Spit it back out. */
390 emit(emitctx
, input_chr
);
395 * Various coding structure facilities specify that designating
396 * a code element also invokes it. As far as I can see, invoking
397 * it now will have the same practical effect, since those
398 * facilities also ban the use of locking shifts.
401 case 'A': /* G0 element used and invoked into GL */
402 LOCKING_SHIFT(0, LEFT
);
404 case 'C': /* G0 in GL, G1 in GR */
405 case 'D': /* Ditto, at least for 8-bit codes */
406 case 'L': /* ISO 4873 (ECMA-43) level 1 */
407 case 'M': /* ISO 4873 (ECMA-43) level 2 */
408 LOCKING_SHIFT(0, LEFT
);
409 LOCKING_SHIFT(1, RIGHT
);
415 * IRR (Identify Revised Registration) is ignored here,
416 * since any revised registration must be
417 * upward-compatible with the old one, so either we'll
418 * support the new one or we'll emit ERROR when we run
419 * into a new character. In either case, there's nothing
423 case '(': /* GZD4 */ case ')': /* G1D4 */
424 case '*': /* G2D4 */ case '+': /* G3D4 */
425 designate(state
, i1
- '(', S4
, i2
, input_chr
);
427 case '-': /* G1D6 */ case '.': /* G2D6 */ case '/': /* G3D6 */
428 designate(state
, i1
- ',', S6
, i2
, input_chr
);
430 case '$': /* G?DM? */
432 case 0: /* Obsolete version of GZDM4 */
434 case '(': /* GZDM4 */ case ')': /* G1DM4 */
435 case '*': /* G2DM4 */ case '+': /* G3DM4 */
436 designate(state
, i2
- '(', M4
, 0, input_chr
);
438 case '-': /* G1DM6 */
439 case '.': /* G2DM6 */ case '/': /* G3DM6 */
440 designate(state
, i2
- ',', M6
, 0, input_chr
);
443 emit(emitctx
, ERROR
);
447 /* XXX What's a reasonable way to handle an unrecognised DOCS? */
452 ENTER_MODE(DOCSUTF8
);
459 /* Unsupported nF escape sequence. Re-emit it. */
462 if (i2
) emit(emitctx
, i2
);
463 emit(emitctx
, input_chr
);
469 const charset_spec charset_CS_ISO2022
= {
470 CS_ISO2022
, read_iso2022
, NULL
, NULL
481 void iso2022_emit(void *ctx
, long output
)
483 wchar_t **p
= (wchar_t **)ctx
;
487 void iso2022_read_test(int line
, char *input
, int inlen
, ...)
490 wchar_t *p
, str
[512];
495 state
.s0
= state
.s1
= 0;
498 for (i
= 0; i
< inlen
; i
++)
499 read_iso2022(NULL
, input
[i
] & 0xFF, &state
, iso2022_emit
, &p
);
503 for (i
= 0; i
< p
- str
; i
++) {
504 l
= va_arg(ap
, long int);
506 printf("%d: correct string shorter than output\n", line
);
511 printf("%d: char %d came out as %08x, should be %08lx\n",
517 l
= va_arg(ap
, long int);
519 printf("%d: correct string longer than output\n", line
);
526 /* Macro to concoct the first three parameters of iso2022_read_test. */
527 #define TESTSTR(x) __LINE__, x, lenof(x)
531 printf("read tests beginning\n");
532 /* Simple test (Emacs sample text for Japanese, in ISO-2022-JP) */
533 iso2022_read_test(TESTSTR("Japanese (\x1b$BF|K\\8l\x1b(B)\t"
534 "\x1b$B$3$s$K$A$O\x1b(B, "
535 "\x1b$B%3%s%K%A%O\x1b(B\n"),
536 'J','a','p','a','n','e','s','e',' ','(',
537 0x65E5, 0x672C, 0x8A9E, ')', '\t',
538 0x3053, 0x3093, 0x306b, 0x3061, 0x306f, ',', ' ',
539 0x30b3, 0x30f3, 0x30cb, 0x30c1, 0x30cf, '\n', 0, -1);
540 /* Same thing in EUC-JP (with designations, and half-width katakana) */
541 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D"
542 "Japanese (\xc6\xfc\xcb\xdc\xb8\xec)\t"
543 "\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4\xcf, "
544 "\x8e\xba\x8e\xdd\x8e\xc6\x8e\xc1\x8e\xca\n"),
545 'J','a','p','a','n','e','s','e',' ','(',
546 0x65E5, 0x672C, 0x8A9E, ')', '\t',
547 0x3053, 0x3093, 0x306b, 0x3061, 0x306f, ',', ' ',
548 0xff7a, 0xff9d, 0xff86, 0xff81, 0xff8a, '\n', 0, -1);
549 /* Multibyte single-shift */
550 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\x8f\"/!"),
552 /* Non-existent SBCS */
553 iso2022_read_test(TESTSTR("\x1b(!Zfnord\n"),
554 ERROR
, ERROR
, ERROR
, ERROR
, ERROR
, '\n', 0, -1);
555 /* Pass-through of ordinary escape sequences, including a long one */
556 iso2022_read_test(TESTSTR("\x1b""b\x1b#5\x1b#!!!5"),
557 0x1B, 'b', 0x1B, '#', '5',
558 0x1B, '#', '!', '!', '!', '5', 0, -1);
559 /* Non-existent DBCS (also 5-byte escape sequence) */
560 iso2022_read_test(TESTSTR("\x1b$(!Bfnord!"),
561 ERROR
, ERROR
, ERROR
, 0, -1);
562 /* Incomplete DB characters */
563 iso2022_read_test(TESTSTR("\x1b$B(,(\x1b(BHi\x1b$B(,(\n"),
564 0x2501, ERROR
, 'H', 'i', 0x2501, ERROR
, '\n', 0, -1);
565 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\xa4""B"),
567 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\x0e\x1b|$\xa2\xaf"),
568 ERROR
, 0x02D8, 0, -1);
569 /* Incomplete escape sequence */
570 iso2022_read_test(TESTSTR("\x1b\n"), ERROR
, '\n', 0, -1);
571 iso2022_read_test(TESTSTR("\x1b-A\x1b~\x1b\xa1"), ERROR
, 0xa1, 0, -1);
572 /* Incomplete single-shift */
573 iso2022_read_test(TESTSTR("\x8e\n"), ERROR
, '\n', 0, -1);
574 iso2022_read_test(TESTSTR("\x1b$*B\x8e(\n"), ERROR
, '\n', 0, -1);
575 /* Corner cases (02/00 and 07/15) */
576 iso2022_read_test(TESTSTR("\x1b(B\x20\x7f"), 0x20, 0x7f, 0, -1);
577 iso2022_read_test(TESTSTR("\x1b(I\x20\x7f"), 0x20, 0x7f, 0, -1);
578 iso2022_read_test(TESTSTR("\x1b$B\x20\x7f"), 0x20, 0x7f, 0, -1);
579 iso2022_read_test(TESTSTR("\x1b-A\x0e\x20\x7f"), 0xa0, 0xff, 0, -1);
580 iso2022_read_test(TESTSTR("\x1b$-~\x0e\x20\x7f"), ERROR
, 0, -1);
581 iso2022_read_test(TESTSTR("\x1b)B\xa0\xff"), ERROR
, ERROR
, 0, -1);
582 iso2022_read_test(TESTSTR("\x1b)I\xa0\xff"), ERROR
, ERROR
, 0, -1);
583 iso2022_read_test(TESTSTR("\x1b$)B\xa0\xff"), ERROR
, ERROR
, 0, -1);
584 iso2022_read_test(TESTSTR("\x1b-A\x1b~\xa0\xff"), 0xa0, 0xff, 0, -1);
585 iso2022_read_test(TESTSTR("\x1b$-~\x1b~\xa0\xff"), ERROR
, 0, -1);
586 /* Designate control sets */
587 iso2022_read_test(TESTSTR("\x1b!@"), 0x1b, '!', '@', 0, -1);
588 /* Designate other coding system */
589 iso2022_read_test(TESTSTR("\x1b%G"
590 "\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5"),
591 0x03BA, 0x1F79, 0x03C3, 0x03BC, 0x03B5, 0, -1);
592 iso2022_read_test(TESTSTR("\x1b-A\x1b%G\xCE\xBA\x1b%@\xa0"),
593 0x03BA, 0xA0, 0, -1);
594 iso2022_read_test(TESTSTR("\x1b%G\xCE\x1b%@"), ERROR
, 0, -1);
595 iso2022_read_test(TESTSTR("\x1b%G\xCE\xBA\x1b%\x1b%@"),
596 0x03BA, 0x1B, '%', 0, -1);
597 printf("read tests completed\n");
598 printf("total: %d errors\n", total_errs
);
599 return (total_errs
!= 0);
602 #endif /* TESTMODE */
604 #else /* ENUM_CHARSETS */
606 ENUM_CHARSET(CS_ISO2022
)