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