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