Include CNS 11643 in the cstable diagnostic utility.
[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
28b8e668 38static long int emacs_big5_1_to_unicode(int, int);
39static long int emacs_big5_2_to_unicode(int, int);
b97e5427 40static long int null_dbcs_to_unicode(int, int);
41
42const struct iso2022_subcharset {
43 char type, i, f;
44 int offset;
45 const sbcs_data *sbcs_base;
46 long int (*dbcs_fn)(int, int);
47} iso2022_subcharsets[] = {
294941fa 48 { S4, 0, '0', 0x00, &sbcsdata_CS_DEC_GRAPHICS },
b97e5427 49 { S4, 0, '<', 0x80, &sbcsdata_CS_DEC_MCS },
294941fa 50 { S4, 0, 'A', 0x00, &sbcsdata_CS_BS4730 },
51 { S4, 0, 'B', 0x00, &sbcsdata_CS_ASCII },
b97e5427 52 { S4, 0, 'I', 0x80, &sbcsdata_CS_JISX0201 },
53 { S4, 0, 'J', 0x00, &sbcsdata_CS_JISX0201 },
54 { S4, 0, '~' },
55 { S6, 0, 'A', 0x80, &sbcsdata_CS_ISO8859_1 },
56 { S6, 0, 'B', 0x80, &sbcsdata_CS_ISO8859_2 },
57 { S6, 0, 'C', 0x80, &sbcsdata_CS_ISO8859_3 },
58 { S6, 0, 'D', 0x80, &sbcsdata_CS_ISO8859_4 },
59 { S6, 0, 'F', 0x80, &sbcsdata_CS_ISO8859_7 },
60 { S6, 0, 'G', 0x80, &sbcsdata_CS_ISO8859_6 },
61 { S6, 0, 'H', 0x80, &sbcsdata_CS_ISO8859_8 },
62 { S6, 0, 'L', 0x80, &sbcsdata_CS_ISO8859_5 },
63 { S6, 0, 'M', 0x80, &sbcsdata_CS_ISO8859_9 },
64 { S6, 0, 'T', 0x80, &sbcsdata_CS_ISO8859_11 },
65 { S6, 0, 'V', 0x80, &sbcsdata_CS_ISO8859_10 },
66 { S6, 0, 'Y', 0x80, &sbcsdata_CS_ISO8859_13 },
67 { S6, 0, '_', 0x80, &sbcsdata_CS_ISO8859_14 },
68 { S6, 0, 'b', 0x80, &sbcsdata_CS_ISO8859_15 },
69 { S6, 0, 'f', 0x80, &sbcsdata_CS_ISO8859_16 },
70 { S6, 0, '~' }, /* empty 96-set */
71#if 0
72 { M4, 0, '@' }, /* JIS C 6226-1978 */
73#endif
28b8e668 74 { M4, 0, '0', -0x21, 0, &emacs_big5_1_to_unicode },
75 { M4, 0, '1', -0x21, 0, &emacs_big5_2_to_unicode },
b97e5427 76 { M4, 0, 'A', -0x21, 0, &gb2312_to_unicode },
77 { M4, 0, 'B', -0x21, 0, &jisx0208_to_unicode },
78 { M4, 0, 'C', -0x21, 0, &ksx1001_to_unicode },
79 { M4, 0, 'D', -0x21, 0, &jisx0212_to_unicode },
80 { M4, 0, '~', 0, 0, &null_dbcs_to_unicode }, /* empty 94^n-set */
81 { M6, 0, '~', 0, 0, &null_dbcs_to_unicode }, /* empty 96^n-set */
82};
83
84static long int null_dbcs_to_unicode(int r, int c)
85{
86 return ERROR;
87}
88
28b8e668 89/*
90 * Emacs encodes Big5 in COMPOUND_TEXT as two 94x94 character sets.
91 * We treat Big5 as a 94x191 character set with a bunch of undefined
92 * columns in the middle, so we have to mess around a bit to make
93 * things fit.
94 */
95
96static long int emacs_big5_1_to_unicode(int r, int c)
97{
98 unsigned long s;
99 s = r * 94 + c;
100 r = s / 157;
101 c = s % 157;
102 if (c >= 64) c += 34; /* Skip over the gap */
103 return big5_to_unicode(r, c);
104}
105
106static long int emacs_big5_2_to_unicode(int r, int c)
107{
108 unsigned long s;
109 s = r * 94 + c;
110 r = s / 157 + 40;
111 c = s % 157;
112 if (c >= 64) c += 34; /* Skip over the gap */
113 return big5_to_unicode(r, c);
114}
115
116
b97e5427 117/* States, or "what we're currently accumulating". */
118enum {
119 IDLE, /* None of the below */
120 SS2CHAR, /* Accumulating a character after SS2 */
121 SS3CHAR, /* Accumulating a character after SS3 */
122 ESCSEQ, /* Accumulating an escape sequence */
123 ESCDROP, /* Discarding an escape sequence */
a89fe3cf 124 ESCPASS, /* Passing through an escape sequence */
c6cef4fa 125 DOCSUTF8, /* DOCSed into UTF-8 */
126 DOCSCTEXT /* DOCSed into a COMPOUND_TEXT extended segment */
b97e5427 127};
128
a89fe3cf 129#if 1
b97e5427 130#include <stdio.h>
131static void dump_state(charset_state *s)
132{
133 unsigned s0 = s->s0, s1 = s->s1;
134 char const * const modes[] = { "IDLE", "SS2CHAR", "SS3CHAR",
a89fe3cf 135 "ESCSEQ", "ESCDROP", "ESCPASS",
136 "DOCSUTF8" };
b97e5427 137
138 fprintf(stderr, "s0: %s", modes[s0 >> 29]);
139 fprintf(stderr, " %02x %02x %02x ", (s0 >> 16) & 0xff, (s0 >> 8) & 0xff,
140 s0 & 0xff);
141 fprintf(stderr, "s1: LS%d LS%dR", (s1 >> 30) & 3, (s1 >> 28) & 3);
142 fprintf(stderr, " %d %d %d %d\n", s1 & 0x7f, (s1 >> 7) & 0x7f,
143 (s1 >> 14) & 0x7f, (s1 >> 21) & 0x7f);
144}
145#endif
146
147static void designate(charset_state *state, int container,
148 int type, int ibyte, int fbyte)
149{
150 unsigned long i;
151
152 assert(container >= 0 && container <= 3);
153 assert(type == S4 || type == S6 || type == M4 || type == M6);
154
155 for (i = 0; i <= lenof(iso2022_subcharsets); i++) {
156 if (iso2022_subcharsets[i].type == type &&
157 iso2022_subcharsets[i].i == ibyte &&
158 iso2022_subcharsets[i].f == fbyte) {
159 state->s1 &= ~(0x7fL << (container * 7));
160 state->s1 |= (i << (container * 7));
161 return;
162 }
163 }
164 /*
165 * If we don't find the charset, invoke the empty one, so we
166 * output ERROR rather than garbage.
167 */
168 designate(state, container, type, 0, '~');
169}
170
a89fe3cf 171static void do_utf8(long int input_chr,
172 charset_state *state,
173 void (*emit)(void *ctx, long int output),
174 void *emitctx)
175{
176 charset_state ustate;
177 charset_spec const *utf8;
178
179 ustate.s1 = 0;
180 ustate.s0 = state->s0 & 0x03ffffffL;
7a7dc0a7 181 read_utf8(NULL, input_chr, &ustate, emit, emitctx);
a89fe3cf 182 state->s0 = (state->s0 & ~0x03ffffffL) | (ustate.s0 & 0x03ffffffL);
183}
184
185static void docs_utf8(long int input_chr,
186 charset_state *state,
187 void (*emit)(void *ctx, long int output),
188 void *emitctx)
189{
190 int retstate;
191
192 /*
193 * Bits [25:0] of s0 are reserved for read_utf8().
194 * Bits [27:26] are a tiny state machine to recognise ESC % @.
195 */
196 retstate = (state->s0 & 0x0c000000L) >> 26;
197 if (retstate == 1 && input_chr == '%')
198 retstate = 2;
199 else if (retstate == 2 && input_chr == '@') {
200 /* If we've got a partial UTF-8 sequence, complain. */
201 if (state->s0 & 0x03ffffffL)
202 emit(emitctx, ERROR);
203 state->s0 = 0;
204 return;
205 } else {
206 if (retstate >= 1) do_utf8(ESC, state, emit, emitctx);
207 if (retstate >= 2) do_utf8('%', state, emit, emitctx);
208 retstate = 0;
209 if (input_chr == ESC)
210 retstate = 1;
211 else {
212 do_utf8(input_chr, state, emit, emitctx);
213 }
214 }
215 state->s0 = (state->s0 & ~0x0c000000L) | (retstate << 26);
216}
217
c6cef4fa 218struct ctext_encoding {
219 char const *name;
220 charset_spec const *subcs;
221};
222
223/*
224 * In theory, this list is in <http://ftp.x.org/pub/docs/registry>,
225 * but XLib appears to have its own ideas, and encodes these three
226 * (as of X11R6.8.2)
227 */
228
229extern charset_spec const charset_CS_ISO8859_14;
230extern charset_spec const charset_CS_ISO8859_15;
231extern charset_spec const charset_CS_BIG5;
232
233static struct ctext_encoding const ctext_encodings[] = {
234 { "big5-0\2", &charset_CS_BIG5 },
235 { "iso8859-14\2", &charset_CS_ISO8859_14 },
236 { "iso8859-15\2", &charset_CS_ISO8859_15 }
237};
238
239static void docs_ctext(long int input_chr,
240 charset_state *state,
241 void (*emit)(void *ctx, long int output),
242 void *emitctx)
243{
244 /*
245 * s0[27:26] = first entry in ctext_encodings that matches
246 * s0[25:22] = number of characters successfully matched, 0xf if all
247 * s0[21:8] count the number of octets left in the segment
248 * s0[7:0] are for sub-charset use
249 */
250 int n = (state->s0 >> 22) & 0xf, i = (state->s0 >> 26) & 3, oi = i, j;
251 int length = (state->s0 >> 8) & 0x3fff;
252
253 if (!length) {
254 /* Haven't read length yet */
255 if ((state->s0 & 0xff) == 0)
256 /* ... or even the first byte */
257 state->s0 |= input_chr;
258 else {
259 length = (state->s0 & 0x7f) * 0x80 + (input_chr & 0x7f);
260 if (length == 0)
261 state->s0 = 0;
262 else
263 state->s0 = (state->s0 & 0xf0000000) | (length << 8);
264 }
265 return;
266 }
267
268 j = i;
269 if (n == 0xe) {
270 /* Skipping unknown encoding. Look out for STX. */
271 if (input_chr == 2)
272 state->s0 = (state->s0 & 0xf0000000) | (i << 26) | (0xf << 22);
273 } else if (n != 0xf) {
274 while (j < lenof(ctext_encodings) &&
275 !memcmp(ctext_encodings[j].name,
276 ctext_encodings[oi].name, n)) {
277 if (ctext_encodings[j].name[n] < input_chr)
278 i = ++j;
279 else
280 break;
281 }
282 if (i >= lenof(ctext_encodings) ||
283 memcmp(ctext_encodings[i].name,
284 ctext_encodings[oi].name, n) ||
285 ctext_encodings[i].name[n] != input_chr) {
286 /* Doom! We haven't heard of this encoding */
287 i = lenof(ctext_encodings);
288 n = 0xe;
289 } else {
290 /*
291 * Otherwise, we have found an additional character in our
292 * encoding name. See if we have reached the _end_ of our
293 * name.
294 */
295 n++;
296 if (!ctext_encodings[i].name[n])
297 n = 0xf;
298 }
299 /*
300 * Failing _that_, we simply update our encoding-name-
301 * tracking state.
302 */
303 assert(i < 4 && n < 16);
304 state->s0 = (state->s0 & 0xf0000000) | (i << 26) | (n << 22);
305 } else {
306 if (i >= lenof(ctext_encodings))
307 emit(emitctx, ERROR);
308 else {
309 charset_state substate;
310 charset_spec const *subcs = ctext_encodings[i].subcs;
311 substate.s1 = 0;
312 substate.s0 = state->s0 & 0xff;
313 subcs->read(subcs, input_chr, &substate, emit, emitctx);
314 state->s0 = (state->s0 & ~0xff) | (substate.s0 & 0xff);
315 }
316 }
317 if (!--length)
318 state->s0 = 0;
319 else
320 state->s0 = (state->s0 &~0x003fff00) | (length << 8);
321}
a89fe3cf 322
b97e5427 323static void read_iso2022(charset_spec const *charset, long int input_chr,
324 charset_state *state,
325 void (*emit)(void *ctx, long int output),
326 void *emitctx)
327{
328
a89fe3cf 329 /* dump_state(state); */
b97e5427 330 /*
04c24cbb 331 * We have to make fairly efficient use of the 64 bits of state
0fab6a2b 332 * available to us. Long-term state goes in s1, and consists of
04c24cbb 333 * the identities of the character sets designated as G0/G1/G2/G3
334 * and the locking-shift states for GL and GR. Short-term state
0fab6a2b 335 * goes in s0: The bottom half of s0 accumulates characters for an
04c24cbb 336 * escape sequence or a multi-byte character, while the top three
337 * bits indicate what they're being accumulated for. After DOCS,
338 * the bottom 29 bits of state are available for the DOCS function
339 * to use -- the UTF-8 one uses the bottom 26 for UTF-8 decoding
340 * and the top two to recognised ESC % @.
b97e5427 341 *
342 * s0[31:29] = state enum
343 * s0[24:0] = accumulated bytes
344 * s1[31:30] = GL locking-shift state
345 * s1[29:28] = GR locking-shift state
346 * s1[27:21] = G3 charset
347 * s1[20:14] = G2 charset
348 * s1[13:7] = G1 charset
349 * s1[6:0] = G0 charset
350 */
351
352#define LEFT 30
353#define RIGHT 28
354#define LOCKING_SHIFT(n,side) \
355 (state->s1 = (state->s1 & ~(3L<<(side))) | ((n ## L)<<(side)))
356#define MODE ((state->s0 & 0xe0000000L) >> 29)
357#define ENTER_MODE(m) (state->s0 = (state->s0 & ~0xe0000000L) | ((m)<<29))
358#define SINGLE_SHIFT(n) ENTER_MODE(SS2CHAR - 2 + (n))
359#define ASSERT_IDLE do { \
360 if (state->s0 != 0) emit(emitctx, ERROR); \
361 state->s0 = 0; \
362} while (0)
363
364 if (state->s1 == 0) {
365 /*
366 * Since there's no LS0R, this means we must just have started.
367 * Set up a sane initial state (LS0, LS1R, ASCII in G0/G1/G2/G3).
368 */
369 LOCKING_SHIFT(0, LEFT);
370 LOCKING_SHIFT(1, RIGHT);
371 designate(state, 0, S4, 0, 'B');
372 designate(state, 1, S4, 0, 'B');
373 designate(state, 2, S4, 0, 'B');
374 designate(state, 3, S4, 0, 'B');
375 }
376
a89fe3cf 377 if (MODE == DOCSUTF8) {
378 docs_utf8(input_chr, state, emit, emitctx);
379 return;
380 }
c6cef4fa 381 if (MODE == DOCSCTEXT) {
382 docs_ctext(input_chr, state, emit, emitctx);
383 return;
384 }
a89fe3cf 385
b97e5427 386 if ((input_chr & 0x60) == 0x00) {
387 /* C0 or C1 control */
388 ASSERT_IDLE;
389 switch (input_chr) {
390 case ESC:
391 ENTER_MODE(ESCSEQ);
392 break;
393 case LS0:
394 LOCKING_SHIFT(0, LEFT);
395 break;
396 case LS1:
397 LOCKING_SHIFT(1, LEFT);
398 break;
399 case SS2:
400 SINGLE_SHIFT(2);
401 break;
402 case SS3:
403 SINGLE_SHIFT(3);
404 break;
405 default:
406 emit(emitctx, input_chr);
407 break;
408 }
409 } else if ((input_chr & 0x80) || MODE < ESCSEQ) {
410 int is_gl = 0;
411 struct iso2022_subcharset const *subcs;
412 unsigned container;
413 long input_7bit;
414 /*
415 * Actual data.
416 * Force idle state if we're in mid escape sequence, or in a
417 * multi-byte character with a different top bit.
418 */
419 if (MODE >= ESCSEQ ||
420 ((state->s0 & 0x00ff0000L) != 0 &&
421 (((state->s0 >> 16) ^ input_chr) & 0x80)))
422 ASSERT_IDLE;
423 if (MODE == SS2CHAR || MODE == SS3CHAR) /* Single-shift */
424 container = MODE - SS2CHAR + 2;
425 else if (input_chr >= 0x80) /* GR */
426 container = (state->s1 >> 28) & 3;
427 else { /* GL */
428 container = state->s1 >> 30;
429 is_gl = 1;
430 }
431 input_7bit = input_chr & ~0x80;
432 subcs = &iso2022_subcharsets[(state->s1 >> (container * 7)) & 0x7f];
433 if ((subcs->type == S4 || subcs->type == M4) &&
434 (input_7bit == 0x20 || input_7bit == 0x7f)) {
435 /* characters not in 94-char set */
436 if (is_gl) emit(emitctx, input_7bit);
437 else emit(emitctx, ERROR);
438 } else if (subcs->type == M4 || subcs->type == M6) {
439 if ((state->s0 & 0x00ff0000L) == 0) {
440 state->s0 |= input_chr << 16;
441 return;
442 } else {
443 emit(emitctx,
444 subcs->dbcs_fn(((state->s0 >> 16) & 0x7f) + subcs->offset,
445 input_7bit + subcs->offset));
446 }
447 } else {
448 if ((state->s0 & 0x00ff0000L) != 0)
449 emit(emitctx, ERROR);
450 emit(emitctx, subcs->sbcs_base ?
451 sbcs_to_unicode(subcs->sbcs_base, input_7bit + subcs->offset):
452 ERROR);
453 }
454 state->s0 = 0;
455 } else {
456 unsigned i1, i2;
457 if (MODE == ESCPASS) {
458 emit(emitctx, input_chr);
459 if ((input_chr & 0xf0) != 0x20)
460 ENTER_MODE(IDLE);
461 return;
462 }
463
464 /*
465 * Intermediate bytes shall be any of the 16 positions of
466 * column 02 of the code table; they are denoted by the symbol
467 * I.
468 */
469 if ((input_chr & 0xf0) == 0x20) {
470 if (((state->s0 >> 16) & 0xff) == 0)
471 state->s0 |= input_chr << 16;
472 else if (((state->s0 >> 8) & 0xff) == 0)
473 state->s0 |= input_chr << 8;
474 else {
475 /* Long escape sequence. Switch to ESCPASS or ESCDROP. */
476 i1 = (state->s0 >> 16) & 0xff;
477 i2 = (state->s0 >> 8) & 0xff;
478 switch (i1) {
479 case '(': case ')': case '*': case '+':
480 case '-': case '.': case '/':
481 case '$':
482 ENTER_MODE(ESCDROP);
483 break;
484 default:
485 emit(emitctx, ESC);
486 emit(emitctx, i1);
487 emit(emitctx, i2);
488 emit(emitctx, input_chr);
489 state->s0 = 0;
490 ENTER_MODE(ESCPASS);
491 break;
492 }
493 }
494 return;
495 }
496
497 /*
498 * Final bytes shall be any of the 79 positions of columns 03
499 * to 07 of the code table excluding position 07/15; they are
500 * denoted by the symbol F.
501 */
502 i1 = (state->s0 >> 16) & 0xff;
503 i2 = (state->s0 >> 8) & 0xff;
504 if (MODE == ESCDROP)
505 input_chr = 0; /* Make sure it won't match. */
506 state->s0 = 0;
507 switch (i1) {
508 case 0: /* No intermediate bytes */
509 switch (input_chr) {
510 case 'N': /* SS2 */
511 SINGLE_SHIFT(2);
512 break;
513 case 'O': /* SS3 */
514 SINGLE_SHIFT(3);
515 break;
516 case 'n': /* LS2 */
517 LOCKING_SHIFT(2, LEFT);
518 break;
519 case 'o': /* LS3 */
520 LOCKING_SHIFT(3, LEFT);
521 break;
522 case '|': /* LS3R */
523 LOCKING_SHIFT(3, RIGHT);
524 break;
525 case '}': /* LS2R */
526 LOCKING_SHIFT(2, RIGHT);
527 break;
528 case '~': /* LS1R */
529 LOCKING_SHIFT(1, RIGHT);
530 break;
531 default:
532 /* Unsupported escape sequence. Spit it back out. */
533 emit(emitctx, ESC);
534 emit(emitctx, input_chr);
535 }
536 break;
537 case ' ': /* ACS */
538 /*
539 * Various coding structure facilities specify that designating
540 * a code element also invokes it. As far as I can see, invoking
541 * it now will have the same practical effect, since those
542 * facilities also ban the use of locking shifts.
543 */
544 switch (input_chr) {
545 case 'A': /* G0 element used and invoked into GL */
546 LOCKING_SHIFT(0, LEFT);
547 break;
548 case 'C': /* G0 in GL, G1 in GR */
549 case 'D': /* Ditto, at least for 8-bit codes */
550 case 'L': /* ISO 4873 (ECMA-43) level 1 */
551 case 'M': /* ISO 4873 (ECMA-43) level 2 */
552 LOCKING_SHIFT(0, LEFT);
553 LOCKING_SHIFT(1, RIGHT);
554 break;
555 }
556 break;
557 case '&': /* IRR */
558 /*
559 * IRR (Identify Revised Registration) is ignored here,
560 * since any revised registration must be
561 * upward-compatible with the old one, so either we'll
562 * support the new one or we'll emit ERROR when we run
563 * into a new character. In either case, there's nothing
564 * to be done here.
565 */
566 break;
567 case '(': /* GZD4 */ case ')': /* G1D4 */
568 case '*': /* G2D4 */ case '+': /* G3D4 */
569 designate(state, i1 - '(', S4, i2, input_chr);
570 break;
571 case '-': /* G1D6 */ case '.': /* G2D6 */ case '/': /* G3D6 */
572 designate(state, i1 - ',', S6, i2, input_chr);
573 break;
574 case '$': /* G?DM? */
575 switch (i2) {
576 case 0: /* Obsolete version of GZDM4 */
577 i2 = '(';
578 case '(': /* GZDM4 */ case ')': /* G1DM4 */
579 case '*': /* G2DM4 */ case '+': /* G3DM4 */
580 designate(state, i2 - '(', M4, 0, input_chr);
581 break;
582 case '-': /* G1DM6 */
583 case '.': /* G2DM6 */ case '/': /* G3DM6 */
584 designate(state, i2 - ',', M6, 0, input_chr);
585 break;
586 default:
587 emit(emitctx, ERROR);
588 break;
589 }
590 case '%': /* DOCS */
a89fe3cf 591 /* XXX What's a reasonable way to handle an unrecognised DOCS? */
592 switch (i2) {
593 case 0:
594 switch (input_chr) {
595 case 'G':
596 ENTER_MODE(DOCSUTF8);
597 break;
598 }
599 break;
c6cef4fa 600 case '/':
601 switch (input_chr) {
602 case '1': case '2':
603 ENTER_MODE(DOCSCTEXT);
604 break;
605 }
606 break;
a89fe3cf 607 }
b97e5427 608 break;
609 default:
610 /* Unsupported nF escape sequence. Re-emit it. */
611 emit(emitctx, ESC);
612 emit(emitctx, i1);
613 if (i2) emit(emitctx, i2);
614 emit(emitctx, input_chr);
615 break;
616 }
617 }
618}
619
04c24cbb 620static int write_iso2022(charset_spec const *charset, long int input_chr,
621 charset_state *state,
622 void (*emit)(void *ctx, long int output),
623 void *emitctx)
624{
625 return FALSE;
626}
627
b97e5427 628const charset_spec charset_CS_ISO2022 = {
04c24cbb 629 CS_ISO2022, read_iso2022, write_iso2022, NULL
b97e5427 630};
631
632#ifdef TESTMODE
633
634#include <stdio.h>
635#include <stdarg.h>
636#include <string.h>
637
638int total_errs = 0;
639
640void iso2022_emit(void *ctx, long output)
641{
642 wchar_t **p = (wchar_t **)ctx;
643 *(*p)++ = output;
644}
645
646void iso2022_read_test(int line, char *input, int inlen, ...)
647{
648 va_list ap;
649 wchar_t *p, str[512];
650 int i;
651 charset_state state;
652 unsigned long l;
653
654 state.s0 = state.s1 = 0;
655 p = str;
656
657 for (i = 0; i < inlen; i++)
658 read_iso2022(NULL, input[i] & 0xFF, &state, iso2022_emit, &p);
659
660 va_start(ap, inlen);
661 l = 0;
662 for (i = 0; i < p - str; i++) {
663 l = va_arg(ap, long int);
664 if (l == -1) {
665 printf("%d: correct string shorter than output\n", line);
666 total_errs++;
667 break;
668 }
669 if (l != str[i]) {
670 printf("%d: char %d came out as %08x, should be %08lx\n",
671 line, i, str[i], l);
672 total_errs++;
673 }
674 }
675 if (l != -1) {
676 l = va_arg(ap, long int);
677 if (l != -1) {
678 printf("%d: correct string longer than output\n", line);
679 total_errs++;
680 }
681 }
682 va_end(ap);
683}
684
685/* Macro to concoct the first three parameters of iso2022_read_test. */
686#define TESTSTR(x) __LINE__, x, lenof(x)
687
688int main(void)
689{
690 printf("read tests beginning\n");
691 /* Simple test (Emacs sample text for Japanese, in ISO-2022-JP) */
692 iso2022_read_test(TESTSTR("Japanese (\x1b$BF|K\\8l\x1b(B)\t"
693 "\x1b$B$3$s$K$A$O\x1b(B, "
694 "\x1b$B%3%s%K%A%O\x1b(B\n"),
695 'J','a','p','a','n','e','s','e',' ','(',
696 0x65E5, 0x672C, 0x8A9E, ')', '\t',
697 0x3053, 0x3093, 0x306b, 0x3061, 0x306f, ',', ' ',
698 0x30b3, 0x30f3, 0x30cb, 0x30c1, 0x30cf, '\n', 0, -1);
699 /* Same thing in EUC-JP (with designations, and half-width katakana) */
700 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D"
701 "Japanese (\xc6\xfc\xcb\xdc\xb8\xec)\t"
702 "\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4\xcf, "
703 "\x8e\xba\x8e\xdd\x8e\xc6\x8e\xc1\x8e\xca\n"),
704 'J','a','p','a','n','e','s','e',' ','(',
705 0x65E5, 0x672C, 0x8A9E, ')', '\t',
706 0x3053, 0x3093, 0x306b, 0x3061, 0x306f, ',', ' ',
707 0xff7a, 0xff9d, 0xff86, 0xff81, 0xff8a, '\n', 0, -1);
708 /* Multibyte single-shift */
709 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\x8f\"/!"),
710 0x02D8, '!', 0, -1);
711 /* Non-existent SBCS */
712 iso2022_read_test(TESTSTR("\x1b(!Zfnord\n"),
713 ERROR, ERROR, ERROR, ERROR, ERROR, '\n', 0, -1);
714 /* Pass-through of ordinary escape sequences, including a long one */
715 iso2022_read_test(TESTSTR("\x1b""b\x1b#5\x1b#!!!5"),
716 0x1B, 'b', 0x1B, '#', '5',
717 0x1B, '#', '!', '!', '!', '5', 0, -1);
718 /* Non-existent DBCS (also 5-byte escape sequence) */
719 iso2022_read_test(TESTSTR("\x1b$(!Bfnord!"),
720 ERROR, ERROR, ERROR, 0, -1);
721 /* Incomplete DB characters */
722 iso2022_read_test(TESTSTR("\x1b$B(,(\x1b(BHi\x1b$B(,(\n"),
723 0x2501, ERROR, 'H', 'i', 0x2501, ERROR, '\n', 0, -1);
724 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\xa4""B"),
725 ERROR, 'B', 0, -1);
726 iso2022_read_test(TESTSTR("\x1b$)B\x1b*I\x1b$+D\x0e\x1b|$\xa2\xaf"),
727 ERROR, 0x02D8, 0, -1);
728 /* Incomplete escape sequence */
729 iso2022_read_test(TESTSTR("\x1b\n"), ERROR, '\n', 0, -1);
730 iso2022_read_test(TESTSTR("\x1b-A\x1b~\x1b\xa1"), ERROR, 0xa1, 0, -1);
731 /* Incomplete single-shift */
732 iso2022_read_test(TESTSTR("\x8e\n"), ERROR, '\n', 0, -1);
733 iso2022_read_test(TESTSTR("\x1b$*B\x8e(\n"), ERROR, '\n', 0, -1);
734 /* Corner cases (02/00 and 07/15) */
735 iso2022_read_test(TESTSTR("\x1b(B\x20\x7f"), 0x20, 0x7f, 0, -1);
736 iso2022_read_test(TESTSTR("\x1b(I\x20\x7f"), 0x20, 0x7f, 0, -1);
737 iso2022_read_test(TESTSTR("\x1b$B\x20\x7f"), 0x20, 0x7f, 0, -1);
738 iso2022_read_test(TESTSTR("\x1b-A\x0e\x20\x7f"), 0xa0, 0xff, 0, -1);
739 iso2022_read_test(TESTSTR("\x1b$-~\x0e\x20\x7f"), ERROR, 0, -1);
740 iso2022_read_test(TESTSTR("\x1b)B\xa0\xff"), ERROR, ERROR, 0, -1);
741 iso2022_read_test(TESTSTR("\x1b)I\xa0\xff"), ERROR, ERROR, 0, -1);
742 iso2022_read_test(TESTSTR("\x1b$)B\xa0\xff"), ERROR, ERROR, 0, -1);
743 iso2022_read_test(TESTSTR("\x1b-A\x1b~\xa0\xff"), 0xa0, 0xff, 0, -1);
744 iso2022_read_test(TESTSTR("\x1b$-~\x1b~\xa0\xff"), ERROR, 0, -1);
745 /* Designate control sets */
746 iso2022_read_test(TESTSTR("\x1b!@"), 0x1b, '!', '@', 0, -1);
c6cef4fa 747 /* Designate other coding system (UTF-8) */
a89fe3cf 748 iso2022_read_test(TESTSTR("\x1b%G"
749 "\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5"),
750 0x03BA, 0x1F79, 0x03C3, 0x03BC, 0x03B5, 0, -1);
751 iso2022_read_test(TESTSTR("\x1b-A\x1b%G\xCE\xBA\x1b%@\xa0"),
752 0x03BA, 0xA0, 0, -1);
753 iso2022_read_test(TESTSTR("\x1b%G\xCE\x1b%@"), ERROR, 0, -1);
754 iso2022_read_test(TESTSTR("\x1b%G\xCE\xBA\x1b%\x1b%@"),
755 0x03BA, 0x1B, '%', 0, -1);
c6cef4fa 756 /* DOCS (COMPOUND_TEXT extended segment) */
757 iso2022_read_test(TESTSTR("\x1b%/1\x80\x80"), 0, -1);
758 iso2022_read_test(TESTSTR("\x1b%/1\x80\x8fiso-8859-15\2xyz\x1b(B"),
759 ERROR, ERROR, ERROR, 0, -1);
760 iso2022_read_test(TESTSTR("\x1b%/1\x80\x8eiso8859-15\2xyz\x1b(B"),
761 'x', 'y', 'z', 0, -1);
762 iso2022_read_test(TESTSTR("\x1b-A\x1b%/2\x80\x89"
763 "big5-0\2\xa1\x40\xa1\x40"),
764 0x3000, 0xa1, 0x40, 0, -1);
28b8e668 765 /* Emacs Big5-in-ISO-2022 mapping */
766 iso2022_read_test(TESTSTR("\x1b$(0&x86\x1b(B \x1b$(0DeBv"),
767 0x5143, 0x6c23, ' ', ' ', 0x958b, 0x767c, 0, -1);
b97e5427 768 printf("read tests completed\n");
769 printf("total: %d errors\n", total_errs);
770 return (total_errs != 0);
771}
772
773#endif /* TESTMODE */
774
775#else /* ENUM_CHARSETS */
776
777ENUM_CHARSET(CS_ISO2022)
778
779#endif