b97e5427 |
1 | /* |
2 | * iso2022.c - support for ISO/IEC 2022 (alias ECMA-35). |
3 | * |
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. |
10 | * |
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 |
16 | * necessary. |
17 | * |
c69732bb |
18 | * DOCS to UTF-8 works. Other DOCS sequences are ignored, which will |
19 | * produce surprising results. |
b97e5427 |
20 | */ |
21 | |
22 | #ifndef ENUM_CHARSETS |
23 | |
24 | #include <assert.h> |
25 | |
26 | #include "charset.h" |
27 | #include "internal.h" |
28 | #include "sbcsdat.h" |
29 | |
30 | #define LS1 (0x0E) |
31 | #define LS0 (0x0F) |
32 | #define ESC (0x1B) |
33 | #define SS2 (0x8E) |
34 | #define SS3 (0x8F) |
35 | |
36 | enum {S4, S6, M4, M6}; |
37 | |
38 | static long int null_dbcs_to_unicode(int, int); |
39 | |
40 | const struct iso2022_subcharset { |
41 | char type, i, f; |
42 | int offset; |
43 | const sbcs_data *sbcs_base; |
44 | long int (*dbcs_fn)(int, int); |
45 | } iso2022_subcharsets[] = { |
294941fa |
46 | { S4, 0, '0', 0x00, &sbcsdata_CS_DEC_GRAPHICS }, |
b97e5427 |
47 | { S4, 0, '<', 0x80, &sbcsdata_CS_DEC_MCS }, |
294941fa |
48 | { S4, 0, 'A', 0x00, &sbcsdata_CS_BS4730 }, |
49 | { S4, 0, 'B', 0x00, &sbcsdata_CS_ASCII }, |
b97e5427 |
50 | { S4, 0, 'I', 0x80, &sbcsdata_CS_JISX0201 }, |
51 | { S4, 0, 'J', 0x00, &sbcsdata_CS_JISX0201 }, |
52 | { S4, 0, '~' }, |
53 | { S6, 0, 'A', 0x80, &sbcsdata_CS_ISO8859_1 }, |
54 | { S6, 0, 'B', 0x80, &sbcsdata_CS_ISO8859_2 }, |
55 | { S6, 0, 'C', 0x80, &sbcsdata_CS_ISO8859_3 }, |
56 | { S6, 0, 'D', 0x80, &sbcsdata_CS_ISO8859_4 }, |
57 | { S6, 0, 'F', 0x80, &sbcsdata_CS_ISO8859_7 }, |
58 | { S6, 0, 'G', 0x80, &sbcsdata_CS_ISO8859_6 }, |
59 | { S6, 0, 'H', 0x80, &sbcsdata_CS_ISO8859_8 }, |
60 | { S6, 0, 'L', 0x80, &sbcsdata_CS_ISO8859_5 }, |
61 | { S6, 0, 'M', 0x80, &sbcsdata_CS_ISO8859_9 }, |
62 | { S6, 0, 'T', 0x80, &sbcsdata_CS_ISO8859_11 }, |
63 | { S6, 0, 'V', 0x80, &sbcsdata_CS_ISO8859_10 }, |
64 | { S6, 0, 'Y', 0x80, &sbcsdata_CS_ISO8859_13 }, |
65 | { S6, 0, '_', 0x80, &sbcsdata_CS_ISO8859_14 }, |
66 | { S6, 0, 'b', 0x80, &sbcsdata_CS_ISO8859_15 }, |
67 | { S6, 0, 'f', 0x80, &sbcsdata_CS_ISO8859_16 }, |
68 | { S6, 0, '~' }, /* empty 96-set */ |
69 | #if 0 |
70 | { M4, 0, '@' }, /* JIS C 6226-1978 */ |
71 | #endif |
72 | { M4, 0, 'A', -0x21, 0, &gb2312_to_unicode }, |
73 | { M4, 0, 'B', -0x21, 0, &jisx0208_to_unicode }, |
74 | { M4, 0, 'C', -0x21, 0, &ksx1001_to_unicode }, |
75 | { M4, 0, 'D', -0x21, 0, &jisx0212_to_unicode }, |
76 | { M4, 0, '~', 0, 0, &null_dbcs_to_unicode }, /* empty 94^n-set */ |
77 | { M6, 0, '~', 0, 0, &null_dbcs_to_unicode }, /* empty 96^n-set */ |
78 | }; |
79 | |
80 | static long int null_dbcs_to_unicode(int r, int c) |
81 | { |
82 | return ERROR; |
83 | } |
84 | |
85 | /* States, or "what we're currently accumulating". */ |
86 | enum { |
87 | IDLE, /* None of the below */ |
88 | SS2CHAR, /* Accumulating a character after SS2 */ |
89 | SS3CHAR, /* Accumulating a character after SS3 */ |
90 | ESCSEQ, /* Accumulating an escape sequence */ |
91 | ESCDROP, /* Discarding an escape sequence */ |
a89fe3cf |
92 | ESCPASS, /* Passing through an escape sequence */ |
93 | DOCSUTF8 /* DOCSed into UTF-8 */ |
b97e5427 |
94 | }; |
95 | |
a89fe3cf |
96 | #if 1 |
b97e5427 |
97 | #include <stdio.h> |
98 | static void dump_state(charset_state *s) |
99 | { |
100 | unsigned s0 = s->s0, s1 = s->s1; |
101 | char const * const modes[] = { "IDLE", "SS2CHAR", "SS3CHAR", |
a89fe3cf |
102 | "ESCSEQ", "ESCDROP", "ESCPASS", |
103 | "DOCSUTF8" }; |
b97e5427 |
104 | |
105 | fprintf(stderr, "s0: %s", modes[s0 >> 29]); |
106 | fprintf(stderr, " %02x %02x %02x ", (s0 >> 16) & 0xff, (s0 >> 8) & 0xff, |
107 | s0 & 0xff); |
108 | fprintf(stderr, "s1: LS%d LS%dR", (s1 >> 30) & 3, (s1 >> 28) & 3); |
109 | fprintf(stderr, " %d %d %d %d\n", s1 & 0x7f, (s1 >> 7) & 0x7f, |
110 | (s1 >> 14) & 0x7f, (s1 >> 21) & 0x7f); |
111 | } |
112 | #endif |
113 | |
114 | static void designate(charset_state *state, int container, |
115 | int type, int ibyte, int fbyte) |
116 | { |
117 | unsigned long i; |
118 | |
119 | assert(container >= 0 && container <= 3); |
120 | assert(type == S4 || type == S6 || type == M4 || type == M6); |
121 | |
122 | for (i = 0; i <= lenof(iso2022_subcharsets); i++) { |
123 | if (iso2022_subcharsets[i].type == type && |
124 | iso2022_subcharsets[i].i == ibyte && |
125 | iso2022_subcharsets[i].f == fbyte) { |
126 | state->s1 &= ~(0x7fL << (container * 7)); |
127 | state->s1 |= (i << (container * 7)); |
128 | return; |
129 | } |
130 | } |
131 | /* |
132 | * If we don't find the charset, invoke the empty one, so we |
133 | * output ERROR rather than garbage. |
134 | */ |
135 | designate(state, container, type, 0, '~'); |
136 | } |
137 | |
a89fe3cf |
138 | static void do_utf8(long int input_chr, |
139 | charset_state *state, |
140 | void (*emit)(void *ctx, long int output), |
141 | void *emitctx) |
142 | { |
143 | charset_state ustate; |
144 | charset_spec const *utf8; |
145 | |
146 | ustate.s1 = 0; |
147 | ustate.s0 = state->s0 & 0x03ffffffL; |
7a7dc0a7 |
148 | read_utf8(NULL, input_chr, &ustate, emit, emitctx); |
a89fe3cf |
149 | state->s0 = (state->s0 & ~0x03ffffffL) | (ustate.s0 & 0x03ffffffL); |
150 | } |
151 | |
152 | static void docs_utf8(long int input_chr, |
153 | charset_state *state, |
154 | void (*emit)(void *ctx, long int output), |
155 | void *emitctx) |
156 | { |
157 | int retstate; |
158 | |
159 | /* |
160 | * Bits [25:0] of s0 are reserved for read_utf8(). |
161 | * Bits [27:26] are a tiny state machine to recognise ESC % @. |
162 | */ |
163 | retstate = (state->s0 & 0x0c000000L) >> 26; |
164 | if (retstate == 1 && input_chr == '%') |
165 | retstate = 2; |
166 | else if (retstate == 2 && input_chr == '@') { |
167 | /* If we've got a partial UTF-8 sequence, complain. */ |
168 | if (state->s0 & 0x03ffffffL) |
169 | emit(emitctx, ERROR); |
170 | state->s0 = 0; |
171 | return; |
172 | } else { |
173 | if (retstate >= 1) do_utf8(ESC, state, emit, emitctx); |
174 | if (retstate >= 2) do_utf8('%', state, emit, emitctx); |
175 | retstate = 0; |
176 | if (input_chr == ESC) |
177 | retstate = 1; |
178 | else { |
179 | do_utf8(input_chr, state, emit, emitctx); |
180 | } |
181 | } |
182 | state->s0 = (state->s0 & ~0x0c000000L) | (retstate << 26); |
183 | } |
184 | |
185 | |
b97e5427 |
186 | static void read_iso2022(charset_spec const *charset, long int input_chr, |
187 | charset_state *state, |
188 | void (*emit)(void *ctx, long int output), |
189 | void *emitctx) |
190 | { |
191 | |
a89fe3cf |
192 | /* dump_state(state); */ |
b97e5427 |
193 | /* |
04c24cbb |
194 | * We have to make fairly efficient use of the 64 bits of state |
0fab6a2b |
195 | * available to us. Long-term state goes in s1, and consists of |
04c24cbb |
196 | * the identities of the character sets designated as G0/G1/G2/G3 |
197 | * and the locking-shift states for GL and GR. Short-term state |
0fab6a2b |
198 | * goes in s0: The bottom half of s0 accumulates characters for an |
04c24cbb |
199 | * escape sequence or a multi-byte character, while the top three |
200 | * bits indicate what they're being accumulated for. After DOCS, |
201 | * the bottom 29 bits of state are available for the DOCS function |
202 | * to use -- the UTF-8 one uses the bottom 26 for UTF-8 decoding |
203 | * and the top two to recognised ESC % @. |
b97e5427 |
204 | * |
205 | * s0[31:29] = state enum |
206 | * s0[24:0] = accumulated bytes |
207 | * s1[31:30] = GL locking-shift state |
208 | * s1[29:28] = GR locking-shift state |
209 | * s1[27:21] = G3 charset |
210 | * s1[20:14] = G2 charset |
211 | * s1[13:7] = G1 charset |
212 | * s1[6:0] = G0 charset |
213 | */ |
214 | |
215 | #define LEFT 30 |
216 | #define RIGHT 28 |
217 | #define LOCKING_SHIFT(n,side) \ |
218 | (state->s1 = (state->s1 & ~(3L<<(side))) | ((n ## L)<<(side))) |
219 | #define MODE ((state->s0 & 0xe0000000L) >> 29) |
220 | #define ENTER_MODE(m) (state->s0 = (state->s0 & ~0xe0000000L) | ((m)<<29)) |
221 | #define SINGLE_SHIFT(n) ENTER_MODE(SS2CHAR - 2 + (n)) |
222 | #define ASSERT_IDLE do { \ |
223 | if (state->s0 != 0) emit(emitctx, ERROR); \ |
224 | state->s0 = 0; \ |
225 | } while (0) |
226 | |
227 | if (state->s1 == 0) { |
228 | /* |
229 | * Since there's no LS0R, this means we must just have started. |
230 | * Set up a sane initial state (LS0, LS1R, ASCII in G0/G1/G2/G3). |
231 | */ |
232 | LOCKING_SHIFT(0, LEFT); |
233 | LOCKING_SHIFT(1, RIGHT); |
234 | designate(state, 0, S4, 0, 'B'); |
235 | designate(state, 1, S4, 0, 'B'); |
236 | designate(state, 2, S4, 0, 'B'); |
237 | designate(state, 3, S4, 0, 'B'); |
238 | } |
239 | |
a89fe3cf |
240 | if (MODE == DOCSUTF8) { |
241 | docs_utf8(input_chr, state, emit, emitctx); |
242 | return; |
243 | } |
244 | |
b97e5427 |
245 | if ((input_chr & 0x60) == 0x00) { |
246 | /* C0 or C1 control */ |
247 | ASSERT_IDLE; |
248 | switch (input_chr) { |
249 | case ESC: |
250 | ENTER_MODE(ESCSEQ); |
251 | break; |
252 | case LS0: |
253 | LOCKING_SHIFT(0, LEFT); |
254 | break; |
255 | case LS1: |
256 | LOCKING_SHIFT(1, LEFT); |
257 | break; |
258 | case SS2: |
259 | SINGLE_SHIFT(2); |
260 | break; |
261 | case SS3: |
262 | SINGLE_SHIFT(3); |
263 | break; |
264 | default: |
265 | emit(emitctx, input_chr); |
266 | break; |
267 | } |
268 | } else if ((input_chr & 0x80) || MODE < ESCSEQ) { |
269 | int is_gl = 0; |
270 | struct iso2022_subcharset const *subcs; |
271 | unsigned container; |
272 | long input_7bit; |
273 | /* |
274 | * Actual data. |
275 | * Force idle state if we're in mid escape sequence, or in a |
276 | * multi-byte character with a different top bit. |
277 | */ |
278 | if (MODE >= ESCSEQ || |
279 | ((state->s0 & 0x00ff0000L) != 0 && |
280 | (((state->s0 >> 16) ^ input_chr) & 0x80))) |
281 | ASSERT_IDLE; |
282 | if (MODE == SS2CHAR || MODE == SS3CHAR) /* Single-shift */ |
283 | container = MODE - SS2CHAR + 2; |
284 | else if (input_chr >= 0x80) /* GR */ |
285 | container = (state->s1 >> 28) & 3; |
286 | else { /* GL */ |
287 | container = state->s1 >> 30; |
288 | is_gl = 1; |
289 | } |
290 | input_7bit = input_chr & ~0x80; |
291 | subcs = &iso2022_subcharsets[(state->s1 >> (container * 7)) & 0x7f]; |
292 | if ((subcs->type == S4 || subcs->type == M4) && |
293 | (input_7bit == 0x20 || input_7bit == 0x7f)) { |
294 | /* characters not in 94-char set */ |
295 | if (is_gl) emit(emitctx, input_7bit); |
296 | else emit(emitctx, ERROR); |
297 | } else if (subcs->type == M4 || subcs->type == M6) { |
298 | if ((state->s0 & 0x00ff0000L) == 0) { |
299 | state->s0 |= input_chr << 16; |
300 | return; |
301 | } else { |
302 | emit(emitctx, |
303 | subcs->dbcs_fn(((state->s0 >> 16) & 0x7f) + subcs->offset, |
304 | input_7bit + subcs->offset)); |
305 | } |
306 | } else { |
307 | if ((state->s0 & 0x00ff0000L) != 0) |
308 | emit(emitctx, ERROR); |
309 | emit(emitctx, subcs->sbcs_base ? |
310 | sbcs_to_unicode(subcs->sbcs_base, input_7bit + subcs->offset): |
311 | ERROR); |
312 | } |
313 | state->s0 = 0; |
314 | } else { |
315 | unsigned i1, i2; |
316 | if (MODE == ESCPASS) { |
317 | emit(emitctx, input_chr); |
318 | if ((input_chr & 0xf0) != 0x20) |
319 | ENTER_MODE(IDLE); |
320 | return; |
321 | } |
322 | |
323 | /* |
324 | * Intermediate bytes shall be any of the 16 positions of |
325 | * column 02 of the code table; they are denoted by the symbol |
326 | * I. |
327 | */ |
328 | if ((input_chr & 0xf0) == 0x20) { |
329 | if (((state->s0 >> 16) & 0xff) == 0) |
330 | state->s0 |= input_chr << 16; |
331 | else if (((state->s0 >> 8) & 0xff) == 0) |
332 | state->s0 |= input_chr << 8; |
333 | else { |
334 | /* Long escape sequence. Switch to ESCPASS or ESCDROP. */ |
335 | i1 = (state->s0 >> 16) & 0xff; |
336 | i2 = (state->s0 >> 8) & 0xff; |
337 | switch (i1) { |
338 | case '(': case ')': case '*': case '+': |
339 | case '-': case '.': case '/': |
340 | case '$': |
341 | ENTER_MODE(ESCDROP); |
342 | break; |
343 | default: |
344 | emit(emitctx, ESC); |
345 | emit(emitctx, i1); |
346 | emit(emitctx, i2); |
347 | emit(emitctx, input_chr); |
348 | state->s0 = 0; |
349 | ENTER_MODE(ESCPASS); |
350 | break; |
351 | } |
352 | } |
353 | return; |
354 | } |
355 | |
356 | /* |
357 | * Final bytes shall be any of the 79 positions of columns 03 |
358 | * to 07 of the code table excluding position 07/15; they are |
359 | * denoted by the symbol F. |
360 | */ |
361 | i1 = (state->s0 >> 16) & 0xff; |
362 | i2 = (state->s0 >> 8) & 0xff; |
363 | if (MODE == ESCDROP) |
364 | input_chr = 0; /* Make sure it won't match. */ |
365 | state->s0 = 0; |
366 | switch (i1) { |
367 | case 0: /* No intermediate bytes */ |
368 | switch (input_chr) { |
369 | case 'N': /* SS2 */ |
370 | SINGLE_SHIFT(2); |
371 | break; |
372 | case 'O': /* SS3 */ |
373 | SINGLE_SHIFT(3); |
374 | break; |
375 | case 'n': /* LS2 */ |
376 | LOCKING_SHIFT(2, LEFT); |
377 | break; |
378 | case 'o': /* LS3 */ |
379 | LOCKING_SHIFT(3, LEFT); |
380 | break; |
381 | case '|': /* LS3R */ |
382 | LOCKING_SHIFT(3, RIGHT); |
383 | break; |
384 | case '}': /* LS2R */ |
385 | LOCKING_SHIFT(2, RIGHT); |
386 | break; |
387 | case '~': /* LS1R */ |
388 | LOCKING_SHIFT(1, RIGHT); |
389 | break; |
390 | default: |
391 | /* Unsupported escape sequence. Spit it back out. */ |
392 | emit(emitctx, ESC); |
393 | emit(emitctx, input_chr); |
394 | } |
395 | break; |
396 | case ' ': /* ACS */ |
397 | /* |
398 | * Various coding structure facilities specify that designating |
399 | * a code element also invokes it. As far as I can see, invoking |
400 | * it now will have the same practical effect, since those |
401 | * facilities also ban the use of locking shifts. |
402 | */ |
403 | switch (input_chr) { |
404 | case 'A': /* G0 element used and invoked into GL */ |
405 | LOCKING_SHIFT(0, LEFT); |
406 | break; |
407 | case 'C': /* G0 in GL, G1 in GR */ |
408 | case 'D': /* Ditto, at least for 8-bit codes */ |
409 | case 'L': /* ISO 4873 (ECMA-43) level 1 */ |
410 | case 'M': /* ISO 4873 (ECMA-43) level 2 */ |
411 | LOCKING_SHIFT(0, LEFT); |
412 | LOCKING_SHIFT(1, RIGHT); |
413 | break; |
414 | } |
415 | break; |
416 | case '&': /* IRR */ |
417 | /* |
418 | * IRR (Identify Revised Registration) is ignored here, |
419 | * since any revised registration must be |
420 | * upward-compatible with the old one, so either we'll |
421 | * support the new one or we'll emit ERROR when we run |
422 | * into a new character. In either case, there's nothing |
423 | * to be done here. |
424 | */ |
425 | break; |
426 | case '(': /* GZD4 */ case ')': /* G1D4 */ |
427 | case '*': /* G2D4 */ case '+': /* G3D4 */ |
428 | designate(state, i1 - '(', S4, i2, input_chr); |
429 | break; |
430 | case '-': /* G1D6 */ case '.': /* G2D6 */ case '/': /* G3D6 */ |
431 | designate(state, i1 - ',', S6, i2, input_chr); |
432 | break; |
433 | case '$': /* G?DM? */ |
434 | switch (i2) { |
435 | case 0: /* Obsolete version of GZDM4 */ |
436 | i2 = '('; |
437 | case '(': /* GZDM4 */ case ')': /* G1DM4 */ |
438 | case '*': /* G2DM4 */ case '+': /* G3DM4 */ |
439 | designate(state, i2 - '(', M4, 0, input_chr); |
440 | break; |
441 | case '-': /* G1DM6 */ |
442 | case '.': /* G2DM6 */ case '/': /* G3DM6 */ |
443 | designate(state, i2 - ',', M6, 0, input_chr); |
444 | break; |
445 | default: |
446 | emit(emitctx, ERROR); |
447 | break; |
448 | } |
449 | case '%': /* DOCS */ |
a89fe3cf |
450 | /* XXX What's a reasonable way to handle an unrecognised DOCS? */ |
451 | switch (i2) { |
452 | case 0: |
453 | switch (input_chr) { |
454 | case 'G': |
455 | ENTER_MODE(DOCSUTF8); |
456 | break; |
457 | } |
458 | break; |
459 | } |
b97e5427 |
460 | break; |
461 | default: |
462 | /* Unsupported nF escape sequence. Re-emit it. */ |
463 | emit(emitctx, ESC); |
464 | emit(emitctx, i1); |
465 | if (i2) emit(emitctx, i2); |
466 | emit(emitctx, input_chr); |
467 | break; |
468 | } |
469 | } |
470 | } |
471 | |
04c24cbb |
472 | static int write_iso2022(charset_spec const *charset, long int input_chr, |
473 | charset_state *state, |
474 | void (*emit)(void *ctx, long int output), |
475 | void *emitctx) |
476 | { |
477 | return FALSE; |
478 | } |
479 | |
b97e5427 |
480 | const charset_spec charset_CS_ISO2022 = { |
04c24cbb |
481 | CS_ISO2022, read_iso2022, write_iso2022, NULL |
b97e5427 |
482 | }; |
483 | |
484 | #ifdef TESTMODE |
485 | |
486 | #include <stdio.h> |
487 | #include <stdarg.h> |
488 | #include <string.h> |
489 | |
490 | int total_errs = 0; |
491 | |
492 | void iso2022_emit(void *ctx, long output) |
493 | { |
494 | wchar_t **p = (wchar_t **)ctx; |
495 | *(*p)++ = output; |
496 | } |
497 | |
498 | void iso2022_read_test(int line, char *input, int inlen, ...) |
499 | { |
500 | va_list ap; |
501 | wchar_t *p, str[512]; |
502 | int i; |
503 | charset_state state; |
504 | unsigned long l; |
505 | |
506 | state.s0 = state.s1 = 0; |
507 | p = str; |
508 | |
509 | for (i = 0; i < inlen; i++) |
510 | read_iso2022(NULL, input[i] & 0xFF, &state, iso2022_emit, &p); |
511 | |
512 | va_start(ap, inlen); |
513 | l = 0; |
514 | for (i = 0; i < p - str; i++) { |
515 | l = va_arg(ap, long int); |
516 | if (l == -1) { |
517 | printf("%d: correct string shorter than output\n", line); |
518 | total_errs++; |
519 | break; |
520 | } |
521 | if (l != str[i]) { |
522 | printf("%d: char %d came out as %08x, should be %08lx\n", |
523 | line, i, str[i], l); |
524 | total_errs++; |
525 | } |
526 | } |
527 | if (l != -1) { |
528 | l = va_arg(ap, long int); |
529 | if (l != -1) { |
530 | printf("%d: correct string longer than output\n", line); |
531 | total_errs++; |
532 | } |
533 | } |
534 | va_end(ap); |
535 | } |
536 | |
537 | /* Macro to concoct the first three parameters of iso2022_read_test. */ |
538 | #define TESTSTR(x) __LINE__, x, lenof(x) |
539 | |
540 | int main(void) |
541 | { |
542 | printf("read tests beginning\n"); |
543 | /* Simple test (Emacs sample text for Japanese, in ISO-2022-JP) */ |
544 | iso2022_read_test(TESTSTR("Japanese (\x1b$BF|K\\8l\x1b(B)\t" |
545 | "\x1b$B$3$s$K$A$O\x1b(B, " |
546 | "\x1b$B%3%s%K%A%O\x1b(B\n"), |
547 | 'J','a','p','a','n','e','s','e',' ','(', |
548 | 0x65E5, 0x672C, 0x8A9E, ')', '\t', |
549 | 0x3053, 0x3093, 0x306b, 0x3061, 0x306f, ',', ' ', |
550 | 0x30b3, 0x30f3, 0x30cb, 0x30c1, 0x30cf, '\n', 0, -1); |
551 | /* Same thing in EUC-JP (with designations, and half-width katakana) */ |
552 | iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D" |
553 | "Japanese (\xc6\xfc\xcb\xdc\xb8\xec)\t" |
554 | "\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4\xcf, " |
555 | "\x8e\xba\x8e\xdd\x8e\xc6\x8e\xc1\x8e\xca\n"), |
556 | 'J','a','p','a','n','e','s','e',' ','(', |
557 | 0x65E5, 0x672C, 0x8A9E, ')', '\t', |
558 | 0x3053, 0x3093, 0x306b, 0x3061, 0x306f, ',', ' ', |
559 | 0xff7a, 0xff9d, 0xff86, 0xff81, 0xff8a, '\n', 0, -1); |
560 | /* Multibyte single-shift */ |
561 | iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\x8f\"/!"), |
562 | 0x02D8, '!', 0, -1); |
563 | /* Non-existent SBCS */ |
564 | iso2022_read_test(TESTSTR("\x1b(!Zfnord\n"), |
565 | ERROR, ERROR, ERROR, ERROR, ERROR, '\n', 0, -1); |
566 | /* Pass-through of ordinary escape sequences, including a long one */ |
567 | iso2022_read_test(TESTSTR("\x1b""b\x1b#5\x1b#!!!5"), |
568 | 0x1B, 'b', 0x1B, '#', '5', |
569 | 0x1B, '#', '!', '!', '!', '5', 0, -1); |
570 | /* Non-existent DBCS (also 5-byte escape sequence) */ |
571 | iso2022_read_test(TESTSTR("\x1b$(!Bfnord!"), |
572 | ERROR, ERROR, ERROR, 0, -1); |
573 | /* Incomplete DB characters */ |
574 | iso2022_read_test(TESTSTR("\x1b$B(,(\x1b(BHi\x1b$B(,(\n"), |
575 | 0x2501, ERROR, 'H', 'i', 0x2501, ERROR, '\n', 0, -1); |
576 | iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\xa4""B"), |
577 | ERROR, 'B', 0, -1); |
578 | iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\x0e\x1b|$\xa2\xaf"), |
579 | ERROR, 0x02D8, 0, -1); |
580 | /* Incomplete escape sequence */ |
581 | iso2022_read_test(TESTSTR("\x1b\n"), ERROR, '\n', 0, -1); |
582 | iso2022_read_test(TESTSTR("\x1b-A\x1b~\x1b\xa1"), ERROR, 0xa1, 0, -1); |
583 | /* Incomplete single-shift */ |
584 | iso2022_read_test(TESTSTR("\x8e\n"), ERROR, '\n', 0, -1); |
585 | iso2022_read_test(TESTSTR("\x1b$*B\x8e(\n"), ERROR, '\n', 0, -1); |
586 | /* Corner cases (02/00 and 07/15) */ |
587 | iso2022_read_test(TESTSTR("\x1b(B\x20\x7f"), 0x20, 0x7f, 0, -1); |
588 | iso2022_read_test(TESTSTR("\x1b(I\x20\x7f"), 0x20, 0x7f, 0, -1); |
589 | iso2022_read_test(TESTSTR("\x1b$B\x20\x7f"), 0x20, 0x7f, 0, -1); |
590 | iso2022_read_test(TESTSTR("\x1b-A\x0e\x20\x7f"), 0xa0, 0xff, 0, -1); |
591 | iso2022_read_test(TESTSTR("\x1b$-~\x0e\x20\x7f"), ERROR, 0, -1); |
592 | iso2022_read_test(TESTSTR("\x1b)B\xa0\xff"), ERROR, ERROR, 0, -1); |
593 | iso2022_read_test(TESTSTR("\x1b)I\xa0\xff"), ERROR, ERROR, 0, -1); |
594 | iso2022_read_test(TESTSTR("\x1b$)B\xa0\xff"), ERROR, ERROR, 0, -1); |
595 | iso2022_read_test(TESTSTR("\x1b-A\x1b~\xa0\xff"), 0xa0, 0xff, 0, -1); |
596 | iso2022_read_test(TESTSTR("\x1b$-~\x1b~\xa0\xff"), ERROR, 0, -1); |
597 | /* Designate control sets */ |
598 | iso2022_read_test(TESTSTR("\x1b!@"), 0x1b, '!', '@', 0, -1); |
a89fe3cf |
599 | /* Designate other coding system */ |
600 | iso2022_read_test(TESTSTR("\x1b%G" |
601 | "\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5"), |
602 | 0x03BA, 0x1F79, 0x03C3, 0x03BC, 0x03B5, 0, -1); |
603 | iso2022_read_test(TESTSTR("\x1b-A\x1b%G\xCE\xBA\x1b%@\xa0"), |
604 | 0x03BA, 0xA0, 0, -1); |
605 | iso2022_read_test(TESTSTR("\x1b%G\xCE\x1b%@"), ERROR, 0, -1); |
606 | iso2022_read_test(TESTSTR("\x1b%G\xCE\xBA\x1b%\x1b%@"), |
607 | 0x03BA, 0x1B, '%', 0, -1); |
b97e5427 |
608 | printf("read tests completed\n"); |
609 | printf("total: %d errors\n", total_errs); |
610 | return (total_errs != 0); |
611 | } |
612 | |
613 | #endif /* TESTMODE */ |
614 | |
615 | #else /* ENUM_CHARSETS */ |
616 | |
617 | ENUM_CHARSET(CS_ISO2022) |
618 | |
619 | #endif |