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