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