citrus_iso2022.c revision 1.15 1 /* $NetBSD: citrus_iso2022.c,v 1.15 2006/03/19 01:19:32 christos Exp $ */
2
3 /*-
4 * Copyright (c)1999, 2002 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/iso2022.c,v 1.23 2001/06/21 01:51:44 yamt Exp $
29 */
30
31 #include <sys/cdefs.h>
32 #if defined(LIBC_SCCS) && !defined(lint)
33 __RCSID("$NetBSD: citrus_iso2022.c,v 1.15 2006/03/19 01:19:32 christos Exp $");
34 #endif /* LIBC_SCCS and not lint */
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stddef.h>
42 #include <locale.h>
43 #include <wchar.h>
44 #include <sys/types.h>
45 #include <limits.h>
46
47 #include "citrus_namespace.h"
48 #include "citrus_types.h"
49 #include "citrus_module.h"
50 #include "citrus_ctype.h"
51 #include "citrus_stdenc.h"
52 #include "citrus_iso2022.h"
53
54
55 /* ----------------------------------------------------------------------
56 * private stuffs used by templates
57 */
58
59
60 /*
61 * wchar_t mappings:
62 * ASCII (ESC ( B) 00000000 00000000 00000000 0xxxxxxx
63 * iso-8859-1 (ESC , A) 00000000 00000000 00000000 1xxxxxxx
64 * 94 charset (ESC ( F) 0fffffff 00000000 00000000 0xxxxxxx
65 * 94 charset (ESC ( M F) 0fffffff 1mmmmmmm 00000000 0xxxxxxx
66 * 96 charset (ESC , F) 0fffffff 00000000 00000000 1xxxxxxx
67 * 96 charset (ESC , M F) 0fffffff 1mmmmmmm 00000000 1xxxxxxx
68 * 94x94 charset (ESC $ ( F) 0fffffff 00000000 0xxxxxxx 0xxxxxxx
69 * 96x96 charset (ESC $ , F) 0fffffff 00000000 0xxxxxxx 1xxxxxxx
70 * 94x94 charset (ESC & V ESC $ ( F)
71 * 0fffffff 1vvvvvvv 0xxxxxxx 0xxxxxxx
72 * 94x94x94 charset (ESC $ ( F) 0fffffff 0xxxxxxx 0xxxxxxx 0xxxxxxx
73 * 96x96x96 charset (ESC $ , F) 0fffffff 0xxxxxxx 0xxxxxxx 1xxxxxxx
74 * reserved for UCS4 co-existence (UCS4 is 31bit encoding thanks to mohta bit)
75 * 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
76 */
77
78 typedef struct {
79 u_char type;
80 #define CS94 (0U)
81 #define CS96 (1U)
82 #define CS94MULTI (2U)
83 #define CS96MULTI (3U)
84
85 u_char final;
86 u_char interm;
87 u_char vers;
88 } _ISO2022Charset;
89
90 typedef struct {
91 _ISO2022Charset g[4];
92 /* need 3 bits to hold -1, 0, ..., 3 */
93 int gl:3,
94 gr:3,
95 singlegl:3,
96 singlegr:3;
97 char ch[7]; /* longest escape sequence (ESC & V ESC $ ( F) */
98 int chlen;
99 int flags;
100 #define _ISO2022STATE_FLAG_INITIALIZED 1
101 } _ISO2022State;
102
103 typedef struct {
104 _ISO2022Charset *recommend[4];
105 size_t recommendsize[4];
106 _ISO2022Charset initg[4];
107 int maxcharset;
108 int flags;
109 #define F_8BIT 0x0001
110 #define F_NOOLD 0x0002
111 #define F_SI 0x0010 /*0F*/
112 #define F_SO 0x0020 /*0E*/
113 #define F_LS0 0x0010 /*0F*/
114 #define F_LS1 0x0020 /*0E*/
115 #define F_LS2 0x0040 /*ESC n*/
116 #define F_LS3 0x0080 /*ESC o*/
117 #define F_LS1R 0x0100 /*ESC ~*/
118 #define F_LS2R 0x0200 /*ESC }*/
119 #define F_LS3R 0x0400 /*ESC |*/
120 #define F_SS2 0x0800 /*ESC N*/
121 #define F_SS3 0x1000 /*ESC O*/
122 #define F_SS2R 0x2000 /*8E*/
123 #define F_SS3R 0x4000 /*8F*/
124 } _ISO2022EncodingInfo;
125 typedef struct {
126 _ISO2022EncodingInfo ei;
127 struct {
128 /* for future multi-locale facility */
129 _ISO2022State s_mblen;
130 _ISO2022State s_mbrlen;
131 _ISO2022State s_mbrtowc;
132 _ISO2022State s_mbtowc;
133 _ISO2022State s_mbsrtowcs;
134 _ISO2022State s_wcrtomb;
135 _ISO2022State s_wcsrtombs;
136 _ISO2022State s_wctomb;
137 } states;
138 } _ISO2022CTypeInfo;
139
140 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
141 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
142
143 #define _FUNCNAME(m) _citrus_ISO2022_##m
144 #define _ENCODING_INFO _ISO2022EncodingInfo
145 #define _CTYPE_INFO _ISO2022CTypeInfo
146 #define _ENCODING_STATE _ISO2022State
147 #define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX
148 #define _ENCODING_IS_STATE_DEPENDENT 1
149 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) \
150 (!((_ps_)->flags & _ISO2022STATE_FLAG_INITIALIZED))
151
152
153 #define _ISO2022INVALID (wchar_t)-1
154
155 static __inline int isc0(__uint8_t x) { return ((x & 0x1f) == x); }
156 static __inline int isc1(__uint8_t x) { return (0x80 <= x && x <= 0x9f); }
157 static __inline int iscntl(__uint8_t x) { return (isc0(x) || isc1(x) || x == 0x7f); }
158 static __inline int is94(__uint8_t x) { return (0x21 <= x && x <= 0x7e); }
159 static __inline int is96(__uint8_t x) { return (0x20 <= x && x <= 0x7f); }
160 static __inline int isecma(__uint8_t x) { return (0x30 <= x && x <= 0x7f); }
161 static __inline int isinterm(__uint8_t x) { return (0x20 <= x && x <= 0x2f); }
162 static __inline int isthree(__uint8_t x) { return (0x60 <= x && x <= 0x6f); }
163
164 static __inline int
165 getcs(const char * __restrict p, _ISO2022Charset * __restrict cs)
166 {
167
168 _DIAGASSERT(p != NULL);
169 _DIAGASSERT(cs != NULL);
170
171 if (!strncmp(p, "94$", 3) && p[3] && !p[4]) {
172 cs->final = (u_char)(p[3] & 0xff);
173 cs->interm = '\0';
174 cs->vers = '\0';
175 cs->type = CS94MULTI;
176 } else if (!strncmp(p, "96$", 3) && p[3] && !p[4]) {
177 cs->final = (u_char)(p[3] & 0xff);
178 cs->interm = '\0';
179 cs->vers = '\0';
180 cs->type = CS96MULTI;
181 } else if (!strncmp(p, "94", 2) && p[2] && !p[3]) {
182 cs->final = (u_char)(p[2] & 0xff);
183 cs->interm = '\0';
184 cs->vers = '\0';
185 cs->type = CS94;
186 } else if (!strncmp(p, "96", 2) && p[2] && !p[3]) {
187 cs->final = (u_char )(p[2] & 0xff);
188 cs->interm = '\0';
189 cs->vers = '\0';
190 cs->type = CS96;
191 } else {
192 return 1;
193 }
194
195 return 0;
196 }
197
198
199 #define _NOTMATCH 0
200 #define _MATCH 1
201 #define _PARSEFAIL 2
202
203 static __inline int
204 get_recommend(_ISO2022EncodingInfo * __restrict ei,
205 const char * __restrict token)
206 {
207 int i;
208 _ISO2022Charset cs, *p;
209
210 if (!strchr("0123", token[0]) || token[1] != '=')
211 return (_NOTMATCH);
212
213 if (getcs(&token[2], &cs) == 0)
214 ;
215 else if (!strcmp(&token[2], "94")) {
216 cs.final = (u_char)(token[4]);
217 cs.interm = '\0';
218 cs.vers = '\0';
219 cs.type = CS94;
220 } else if (!strcmp(&token[2], "96")) {
221 cs.final = (u_char)(token[4]);
222 cs.interm = '\0';
223 cs.vers = '\0';
224 cs.type = CS96;
225 } else if (!strcmp(&token[2], "94$")) {
226 cs.final = (u_char)(token[5]);
227 cs.interm = '\0';
228 cs.vers = '\0';
229 cs.type = CS94MULTI;
230 } else if (!strcmp(&token[2], "96$")) {
231 cs.final = (u_char)(token[5]);
232 cs.interm = '\0';
233 cs.vers = '\0';
234 cs.type = CS96MULTI;
235 } else {
236 return (_PARSEFAIL);
237 }
238
239 i = token[0] - '0';
240 if (!ei->recommend[i]) {
241 ei->recommend[i] = malloc(sizeof(_ISO2022Charset));
242 } else {
243 p = realloc(ei->recommend[i],
244 sizeof(_ISO2022Charset) * (ei->recommendsize[i] + 1));
245 if (!p)
246 return (_PARSEFAIL);
247 ei->recommend[i] = p;
248 }
249 if (!ei->recommend[i])
250 return (_PARSEFAIL);
251 ei->recommendsize[i]++;
252
253 (ei->recommend[i] + (ei->recommendsize[i] - 1))->final = cs.final;
254 (ei->recommend[i] + (ei->recommendsize[i] - 1))->interm = cs.interm;
255 (ei->recommend[i] + (ei->recommendsize[i] - 1))->vers = cs.vers;
256 (ei->recommend[i] + (ei->recommendsize[i] - 1))->type = cs.type;
257
258 return (_MATCH);
259 }
260
261 static __inline int
262 get_initg(_ISO2022EncodingInfo * __restrict ei,
263 const char * __restrict token)
264 {
265 _ISO2022Charset cs;
266
267 if (strncmp("INIT", &token[0], 4) ||
268 !strchr("0123", token[4]) ||
269 token[5] != '=')
270 return (_NOTMATCH);
271
272 if (getcs(&token[6], &cs) != 0)
273 return (_PARSEFAIL);
274
275 ei->initg[token[4] - '0'].type = cs.type;
276 ei->initg[token[4] - '0'].final = cs.final;
277 ei->initg[token[4] - '0'].interm = cs.interm;
278 ei->initg[token[4] - '0'].vers = cs.vers;
279
280 return (_MATCH);
281 }
282
283 static __inline int
284 get_max(_ISO2022EncodingInfo * __restrict ei,
285 const char * __restrict token)
286 {
287 if (!strcmp(token, "MAX1")) {
288 ei->maxcharset = 1;
289 } else if (!strcmp(token, "MAX2")) {
290 ei->maxcharset = 2;
291 } else if (!strcmp(token, "MAX3")) {
292 ei->maxcharset = 3;
293 } else
294 return (_NOTMATCH);
295
296 return (_MATCH);
297 }
298
299
300 static __inline int
301 get_flags(_ISO2022EncodingInfo * __restrict ei,
302 const char * __restrict token)
303 {
304 int i;
305 static struct {
306 const char *tag;
307 int flag;
308 } const tags[] = {
309 { "DUMMY", 0 },
310 { "8BIT", F_8BIT },
311 { "NOOLD", F_NOOLD },
312 { "SI", F_SI },
313 { "SO", F_SO },
314 { "LS0", F_LS0 },
315 { "LS1", F_LS1 },
316 { "LS2", F_LS2 },
317 { "LS3", F_LS3 },
318 { "LS1R", F_LS1R },
319 { "LS2R", F_LS2R },
320 { "LS3R", F_LS3R },
321 { "SS2", F_SS2 },
322 { "SS3", F_SS3 },
323 { "SS2R", F_SS2R },
324 { "SS3R", F_SS3R },
325 { NULL, 0 }
326 };
327
328 for (i = 0; tags[i].tag; i++) {
329 if (!strcmp(token, tags[i].tag)) {
330 ei->flags |= tags[i].flag;
331 return (_MATCH);
332 }
333 }
334
335 return (_NOTMATCH);
336 }
337
338
339 static __inline int
340 _citrus_ISO2022_parse_variable(_ISO2022EncodingInfo * __restrict ei,
341 const void * __restrict var, size_t lenvar)
342 {
343 char const *v, *e;
344 char buf[20];
345 int i, len, ret;
346
347 _DIAGASSERT(ei != NULL);
348
349
350 /*
351 * parse VARIABLE section.
352 */
353
354 if (!var)
355 return (EFTYPE);
356
357 v = (const char *) var;
358
359 /* initialize structure */
360 ei->maxcharset = 0;
361 for (i = 0; i < 4; i++) {
362 ei->recommend[i] = NULL;
363 ei->recommendsize[i] = 0;
364 }
365 ei->flags = 0;
366
367 while (*v) {
368 while (*v == ' ' || *v == '\t')
369 ++v;
370
371 /* find the token */
372 e = v;
373 while (*e && *e != ' ' && *e != '\t')
374 ++e;
375
376 len = e-v;
377 if (len == 0)
378 break;
379 if (len>=sizeof(buf))
380 goto parsefail;
381 snprintf(buf, sizeof(buf), "%.*s", len, v);
382
383 if ((ret = get_recommend(ei, buf)) != _NOTMATCH)
384 ;
385 else if ((ret = get_initg(ei, buf)) != _NOTMATCH)
386 ;
387 else if ((ret = get_max(ei, buf)) != _NOTMATCH)
388 ;
389 else if ((ret = get_flags(ei, buf)) != _NOTMATCH)
390 ;
391 else
392 ret = _PARSEFAIL;
393 if (ret==_PARSEFAIL)
394 goto parsefail;
395 v = e;
396
397 }
398
399 return (0);
400
401 parsefail:
402 free(ei->recommend[0]);
403 free(ei->recommend[1]);
404 free(ei->recommend[2]);
405 free(ei->recommend[3]);
406
407 return (EFTYPE);
408 }
409
410 static __inline void
411 /*ARGSUSED*/
412 _citrus_ISO2022_init_state(_ISO2022EncodingInfo * __restrict ei,
413 _ISO2022State * __restrict s)
414 {
415 int i;
416
417 memset(s, 0, sizeof(*s));
418 s->gl = 0;
419 s->gr = (ei->flags & F_8BIT) ? 1 : -1;
420
421 for (i = 0; i < 4; i++) {
422 if (ei->initg[i].final) {
423 s->g[i].type = ei->initg[i].type;
424 s->g[i].final = ei->initg[i].final;
425 s->g[i].interm = ei->initg[i].interm;
426 }
427 }
428 s->singlegl = s->singlegr = -1;
429 s->flags |= _ISO2022STATE_FLAG_INITIALIZED;
430 }
431
432 static __inline void
433 /*ARGSUSED*/
434 _citrus_ISO2022_pack_state(_ISO2022EncodingInfo * __restrict ei,
435 void * __restrict pspriv,
436 const _ISO2022State * __restrict s)
437 {
438 memcpy(pspriv, (const void *)s, sizeof(*s));
439 }
440
441 static __inline void
442 /*ARGSUSED*/
443 _citrus_ISO2022_unpack_state(_ISO2022EncodingInfo * __restrict ei,
444 _ISO2022State * __restrict s,
445 const void * __restrict pspriv)
446 {
447 memcpy((void *)s, pspriv, sizeof(*s));
448 }
449
450 static int
451 /*ARGSUSED*/
452 _citrus_ISO2022_encoding_module_init(_ISO2022EncodingInfo * __restrict ei,
453 const void * __restrict var,
454 size_t lenvar)
455 {
456
457 _DIAGASSERT(ei != NULL);
458
459 return _citrus_ISO2022_parse_variable(ei, var, lenvar);
460 }
461
462 static void
463 /*ARGSUSED*/
464 _citrus_ISO2022_encoding_module_uninit(_ISO2022EncodingInfo *ei)
465 {
466 }
467
468 #define ESC '\033'
469 #define ECMA -1
470 #define INTERM -2
471 #define OECMA -3
472 static const struct seqtable {
473 int type;
474 int csoff;
475 int finaloff;
476 int intermoff;
477 int versoff;
478 int len;
479 int chars[10];
480 } seqtable[] = {
481 /* G0 94MULTI special */
482 { CS94MULTI, -1, 2, -1, -1, 3, { ESC, '$', OECMA }, },
483 /* G0 94MULTI special with version identification */
484 { CS94MULTI, -1, 5, -1, 2, 6, { ESC, '&', ECMA, ESC, '$', OECMA }, },
485 /* G? 94 */
486 { CS94, 1, 2, -1, -1, 3, { ESC, CS94, ECMA, }, },
487 /* G? 94 with 2nd intermediate char */
488 { CS94, 1, 3, 2, -1, 4, { ESC, CS94, INTERM, ECMA, }, },
489 /* G? 96 */
490 { CS96, 1, 2, -1, -1, 3, { ESC, CS96, ECMA, }, },
491 /* G? 96 with 2nd intermediate char */
492 { CS96, 1, 3, 2, -1, 4, { ESC, CS96, INTERM, ECMA, }, },
493 /* G? 94MULTI */
494 { CS94MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS94, ECMA, }, },
495 /* G? 96MULTI */
496 { CS96MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS96, ECMA, }, },
497 /* G? 94MULTI with version specification */
498 { CS94MULTI, 5, 6, -1, 2, 7, { ESC, '&', ECMA, ESC, '$', CS94, ECMA, }, },
499 /* LS2/3 */
500 { -1, -1, -1, -1, -1, 2, { ESC, 'n', }, },
501 { -1, -1, -1, -1, -1, 2, { ESC, 'o', }, },
502 /* LS1/2/3R */
503 { -1, -1, -1, -1, -1, 2, { ESC, '~', }, },
504 { -1, -1, -1, -1, -1, 2, { ESC, /*{*/ '}', }, },
505 { -1, -1, -1, -1, -1, 2, { ESC, '|', }, },
506 /* SS2/3 */
507 { -1, -1, -1, -1, -1, 2, { ESC, 'N', }, },
508 { -1, -1, -1, -1, -1, 2, { ESC, 'O', }, },
509 /* end of records */
510 { 0, }
511 };
512
513 static int
514 seqmatch(const char * __restrict s, size_t n,
515 const struct seqtable * __restrict sp)
516 {
517 const int *p;
518
519 _DIAGASSERT(s != NULL);
520 _DIAGASSERT(sp != NULL);
521
522 p = sp->chars;
523 while (p - sp->chars < n && p - sp->chars < sp->len) {
524 switch (*p) {
525 case ECMA:
526 if (!isecma(*s))
527 goto terminate;
528 break;
529 case OECMA:
530 if (*s && strchr("@AB", *s))
531 break;
532 else
533 goto terminate;
534 case INTERM:
535 if (!isinterm(*s))
536 goto terminate;
537 break;
538 case CS94:
539 if (*s && strchr("()*+", *s))
540 break;
541 else
542 goto terminate;
543 case CS96:
544 if (*s && strchr(",-./", *s))
545 break;
546 else
547 goto terminate;
548 default:
549 if (*s != *p)
550 goto terminate;
551 break;
552 }
553
554 p++;
555 s++;
556 }
557
558 terminate:
559 return p - sp->chars;
560 }
561
562 static wchar_t
563 _ISO2022_sgetwchar(_ISO2022EncodingInfo * __restrict ei,
564 const char * __restrict string, size_t n,
565 const char ** __restrict result,
566 _ISO2022State * __restrict psenc)
567 {
568 wchar_t wchar = 0;
569 int cur;
570 const struct seqtable *sp;
571 int nmatch;
572 int i;
573
574 _DIAGASSERT(ei != NULL);
575 _DIAGASSERT(psenc != NULL);
576 _DIAGASSERT(string != NULL);
577 /* result may be NULL */
578
579 while (1) {
580 /* SI/SO */
581 if (1 <= n && string[0] == '\017') {
582 psenc->gl = 0;
583 string++;
584 n--;
585 continue;
586 }
587 if (1 <= n && string[0] == '\016') {
588 psenc->gl = 1;
589 string++;
590 n--;
591 continue;
592 }
593
594 /* SS2/3R */
595 if (1 <= n && string[0] && strchr("\217\216", string[0])) {
596 psenc->singlegl = psenc->singlegr =
597 (string[0] - '\216') + 2;
598 string++;
599 n--;
600 continue;
601 }
602
603 /* eat the letter if this is not ESC */
604 if (1 <= n && string[0] != '\033')
605 break;
606
607 /* look for a perfect match from escape sequences */
608 for (sp = &seqtable[0]; sp->len; sp++) {
609 nmatch = seqmatch(string, n, sp);
610 if (sp->len == nmatch && n >= sp->len)
611 break;
612 }
613
614 if (!sp->len)
615 goto notseq;
616
617 if (sp->type != -1) {
618 if (sp->csoff == -1)
619 i = 0;
620 else {
621 switch (sp->type) {
622 case CS94:
623 case CS94MULTI:
624 i = string[sp->csoff] - '(';
625 break;
626 case CS96:
627 case CS96MULTI:
628 i = string[sp->csoff] - ',';
629 break;
630 default:
631 return (_ISO2022INVALID);
632 }
633 }
634 psenc->g[i].type = sp->type;
635 psenc->g[i].final = '\0';
636 psenc->g[i].interm = '\0';
637 psenc->g[i].vers = '\0';
638 /* sp->finaloff must not be -1 */
639 if (sp->finaloff != -1)
640 psenc->g[i].final = string[sp->finaloff];
641 if (sp->intermoff != -1)
642 psenc->g[i].interm = string[sp->intermoff];
643 if (sp->versoff != -1)
644 psenc->g[i].vers = string[sp->versoff];
645
646 string += sp->len;
647 n -= sp->len;
648 continue;
649 }
650
651 /* LS2/3 */
652 if (2 <= n && string[0] == '\033'
653 && string[1] && strchr("no", string[1])) {
654 psenc->gl = string[1] - 'n' + 2;
655 string += 2;
656 n -= 2;
657 continue;
658 }
659
660 /* LS1/2/3R */
661 /* XXX: { for vi showmatch */
662 if (2 <= n && string[0] == '\033'
663 && string[1] && strchr("~}|", string[1])) {
664 psenc->gr = 3 - (string[1] - '|');
665 string += 2;
666 n -= 2;
667 continue;
668 }
669
670 /* SS2/3 */
671 if (2 <= n && string[0] == '\033'
672 && string[1] && strchr("NO", string[1])) {
673 psenc->singlegl = (string[1] - 'N') + 2;
674 string += 2;
675 n -= 2;
676 continue;
677 }
678
679 notseq:
680 /*
681 * if we've got an unknown escape sequence, eat the ESC at the
682 * head. otherwise, wait till full escape sequence comes.
683 */
684 for (sp = &seqtable[0]; sp->len; sp++) {
685 nmatch = seqmatch(string, n, sp);
686 if (!nmatch)
687 continue;
688
689 /*
690 * if we are in the middle of escape sequence,
691 * we still need to wait for more characters to come
692 */
693 if (n < sp->len) {
694 if (nmatch == n) {
695 if (result)
696 *result = string;
697 return (_ISO2022INVALID);
698 }
699 } else {
700 if (nmatch == sp->len) {
701 /* this case should not happen */
702 goto eat;
703 }
704 }
705 }
706
707 break;
708 }
709
710 eat:
711 /* no letter to eat */
712 if (n < 1) {
713 if (result)
714 *result = string;
715 return (_ISO2022INVALID);
716 }
717
718 /* normal chars. always eat C0/C1 as is. */
719 if (iscntl(*string & 0xff))
720 cur = -1;
721 else if (*string & 0x80) {
722 cur = (psenc->singlegr == -1)
723 ? psenc->gr : psenc->singlegr;
724 } else {
725 cur = (psenc->singlegl == -1)
726 ? psenc->gl : psenc->singlegl;
727 }
728
729 if (cur == -1) {
730 asis:
731 wchar = *string++ & 0xff;
732 if (result)
733 *result = string;
734 /* reset single shift state */
735 psenc->singlegr = psenc->singlegl = -1;
736 return wchar;
737 }
738
739 /* length error check */
740 switch (psenc->g[cur].type) {
741 case CS94MULTI:
742 case CS96MULTI:
743 if (!isthree(psenc->g[cur].final)) {
744 if (2 <= n
745 && (string[0] & 0x80) == (string[1] & 0x80))
746 break;
747 } else {
748 if (3 <= n
749 && (string[0] & 0x80) == (string[1] & 0x80)
750 && (string[0] & 0x80) == (string[2] & 0x80))
751 break;
752 }
753
754 /* we still need to wait for more characters to come */
755 if (result)
756 *result = string;
757 return (_ISO2022INVALID);
758
759 case CS94:
760 case CS96:
761 if (1 <= n)
762 break;
763
764 /* we still need to wait for more characters to come */
765 if (result)
766 *result = string;
767 return (_ISO2022INVALID);
768 }
769
770 /* range check */
771 switch (psenc->g[cur].type) {
772 case CS94:
773 if (!(is94(string[0] & 0x7f)))
774 goto asis;
775 case CS96:
776 if (!(is96(string[0] & 0x7f)))
777 goto asis;
778 break;
779 case CS94MULTI:
780 if (!(is94(string[0] & 0x7f) && is94(string[1] & 0x7f)))
781 goto asis;
782 break;
783 case CS96MULTI:
784 if (!(is96(string[0] & 0x7f) && is96(string[1] & 0x7f)))
785 goto asis;
786 break;
787 }
788
789 /* extract the character. */
790 switch (psenc->g[cur].type) {
791 case CS94:
792 /* special case for ASCII. */
793 if (psenc->g[cur].final == 'B' && !psenc->g[cur].interm) {
794 wchar = *string++;
795 wchar &= 0x7f;
796 break;
797 }
798 wchar = psenc->g[cur].final;
799 wchar = (wchar << 8);
800 wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
801 wchar = (wchar << 8);
802 wchar = (wchar << 8) | (*string++ & 0x7f);
803 break;
804 case CS96:
805 /* special case for ISO-8859-1. */
806 if (psenc->g[cur].final == 'A' && !psenc->g[cur].interm) {
807 wchar = *string++;
808 wchar &= 0x7f;
809 wchar |= 0x80;
810 break;
811 }
812 wchar = psenc->g[cur].final;
813 wchar = (wchar << 8);
814 wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
815 wchar = (wchar << 8);
816 wchar = (wchar << 8) | (*string++ & 0x7f);
817 wchar |= 0x80;
818 break;
819 case CS94MULTI:
820 case CS96MULTI:
821 wchar = psenc->g[cur].final;
822 wchar = (wchar << 8);
823 if (isthree(psenc->g[cur].final))
824 wchar |= (*string++ & 0x7f);
825 wchar = (wchar << 8) | (*string++ & 0x7f);
826 wchar = (wchar << 8) | (*string++ & 0x7f);
827 if (psenc->g[cur].type == CS96MULTI)
828 wchar |= 0x80;
829 break;
830 }
831
832 if (result)
833 *result = string;
834 /* reset single shift state */
835 psenc->singlegr = psenc->singlegl = -1;
836 return wchar;
837 }
838
839
840
841 static int
842 _citrus_ISO2022_mbrtowc_priv(_ISO2022EncodingInfo * __restrict ei,
843 wchar_t * __restrict pwc,
844 const char ** __restrict s,
845 size_t n, _ISO2022State * __restrict psenc,
846 size_t * __restrict nresult)
847 {
848 wchar_t wchar;
849 const char *s0, *p, *result;
850 int c;
851 int chlenbak;
852
853 _DIAGASSERT(nresult != 0);
854 _DIAGASSERT(ei != NULL);
855 _DIAGASSERT(psenc != NULL);
856 _DIAGASSERT(s != NULL);
857
858 s0 = *s;
859 c = 0;
860 chlenbak = psenc->chlen;
861
862 /*
863 * if we have something in buffer, use that.
864 * otherwise, skip here
865 */
866 if (psenc->chlen < 0 || psenc->chlen > sizeof(psenc->ch)) {
867 /* illgeal state */
868 _citrus_ISO2022_init_state(ei, psenc);
869 goto encoding_error;
870 }
871 if (psenc->chlen == 0)
872 goto emptybuf;
873
874 /* buffer is not empty */
875 p = psenc->ch;
876 while (psenc->chlen < sizeof(psenc->ch) && n >= 0) {
877 if (n > 0) {
878 psenc->ch[psenc->chlen++] = *s0++;
879 n--;
880 }
881
882 wchar = _ISO2022_sgetwchar(ei, p, psenc->chlen - (p-psenc->ch),
883 &result, psenc);
884 c += result - p;
885 if (wchar != _ISO2022INVALID) {
886 if (psenc->chlen > c)
887 memmove(psenc->ch, result, psenc->chlen - c);
888 if (psenc->chlen < c)
889 psenc->chlen = 0;
890 else
891 psenc->chlen -= c;
892 goto output;
893 }
894
895 if (n == 0) {
896 if ((result - p) == psenc->chlen)
897 /* complete shift sequence. */
898 psenc->chlen = 0;
899 goto restart;
900 }
901
902 p = result;
903 }
904
905 /* escape sequence too long? */
906 goto encoding_error;
907
908 emptybuf:
909 wchar = _ISO2022_sgetwchar(ei, s0, n, &result, psenc);
910 if (wchar != _ISO2022INVALID) {
911 c += result - s0;
912 psenc->chlen = 0;
913 s0 = result;
914 goto output;
915 }
916 if (result > s0) {
917 c += (result - s0);
918 n -= (result - s0);
919 s0 = result;
920 if (n>0)
921 goto emptybuf;
922 /* complete shift sequence. */
923 goto restart;
924 }
925 n += c;
926 if (n < sizeof(psenc->ch)) {
927 memcpy(psenc->ch, s0 - c, n);
928 psenc->chlen = n;
929 s0 = result;
930 goto restart;
931 }
932
933 /* escape sequence too long? */
934
935 encoding_error:
936 psenc->chlen = 0;
937 *nresult = (size_t)-1;
938 return (EILSEQ);
939
940 output:
941 *s = s0;
942 if (pwc)
943 *pwc = wchar;
944
945 if (!wchar)
946 *nresult = 0;
947 else
948 *nresult = c - chlenbak;
949
950 return (0);
951
952 restart:
953 *s = s0;
954 *nresult = (size_t)-2;
955
956 return (0);
957 }
958
959 static int
960 recommendation(_ISO2022EncodingInfo * __restrict ei,
961 _ISO2022Charset * __restrict cs)
962 {
963 int i, j;
964 _ISO2022Charset *recommend;
965
966 _DIAGASSERT(ei != NULL);
967 _DIAGASSERT(cs != NULL);
968
969 /* first, try a exact match. */
970 for (i = 0; i < 4; i++) {
971 recommend = ei->recommend[i];
972 for (j = 0; j < ei->recommendsize[i]; j++) {
973 if (cs->type != recommend[j].type)
974 continue;
975 if (cs->final != recommend[j].final)
976 continue;
977 if (cs->interm != recommend[j].interm)
978 continue;
979
980 return i;
981 }
982 }
983
984 /* then, try a wildcard match over final char. */
985 for (i = 0; i < 4; i++) {
986 recommend = ei->recommend[i];
987 for (j = 0; j < ei->recommendsize[i]; j++) {
988 if (cs->type != recommend[j].type)
989 continue;
990 if (cs->final && (cs->final != recommend[j].final))
991 continue;
992 if (cs->interm && (cs->interm != recommend[j].interm))
993 continue;
994
995 return i;
996 }
997 }
998
999 /* there's no recommendation. make a guess. */
1000 if (ei->maxcharset == 0) {
1001 return 0;
1002 } else {
1003 switch (cs->type) {
1004 case CS94:
1005 case CS94MULTI:
1006 return 0;
1007 case CS96:
1008 case CS96MULTI:
1009 return 1;
1010 }
1011 }
1012 return 0;
1013 }
1014
1015 static int
1016 _ISO2022_sputwchar(_ISO2022EncodingInfo * __restrict ei, wchar_t wc,
1017 char * __restrict string, size_t n,
1018 char ** __restrict result,
1019 _ISO2022State * __restrict psenc)
1020 {
1021 int i = 0, len;
1022 _ISO2022Charset cs;
1023 char *p;
1024 char tmp[MB_LEN_MAX];
1025 int target;
1026 u_char mask;
1027 int bit8;
1028
1029 _DIAGASSERT(ei != NULL);
1030 _DIAGASSERT(string != NULL);
1031 /* result may be NULL */
1032 /* state appears to be unused */
1033
1034 if (iscntl(wc & 0xff)) {
1035 /* go back to ASCII on control chars */
1036 cs.type = CS94;
1037 cs.final = 'B';
1038 cs.interm = '\0';
1039 } else if (!(wc & ~0xff)) {
1040 if (wc & 0x80) {
1041 /* special treatment for ISO-8859-1 */
1042 cs.type = CS96;
1043 cs.final = 'A';
1044 cs.interm = '\0';
1045 } else {
1046 /* special treatment for ASCII */
1047 cs.type = CS94;
1048 cs.final = 'B';
1049 cs.interm = '\0';
1050 }
1051 } else {
1052 cs.final = (wc >> 24) & 0x7f;
1053 if ((wc >> 16) & 0x80)
1054 cs.interm = (wc >> 16) & 0x7f;
1055 else
1056 cs.interm = '\0';
1057 if (wc & 0x80)
1058 cs.type = (wc & 0x00007f00) ? CS96MULTI : CS96;
1059 else
1060 cs.type = (wc & 0x00007f00) ? CS94MULTI : CS94;
1061 }
1062 target = recommendation(ei, &cs);
1063 p = tmp;
1064 bit8 = ei->flags & F_8BIT;
1065
1066 /* designate the charset onto the target plane(G0/1/2/3). */
1067 if (psenc->g[target].type == cs.type
1068 && psenc->g[target].final == cs.final
1069 && psenc->g[target].interm == cs.interm)
1070 goto planeok;
1071
1072 *p++ = '\033';
1073 if (cs.type == CS94MULTI || cs.type == CS96MULTI)
1074 *p++ = '$';
1075 if (target == 0 && cs.type == CS94MULTI && strchr("@AB", cs.final)
1076 && !cs.interm && !(ei->flags & F_NOOLD))
1077 ;
1078 else if (cs.type == CS94 || cs.type == CS94MULTI)
1079 *p++ = "()*+"[target];
1080 else
1081 *p++ = ",-./"[target];
1082 if (cs.interm)
1083 *p++ = cs.interm;
1084 *p++ = cs.final;
1085
1086 psenc->g[target].type = cs.type;
1087 psenc->g[target].final = cs.final;
1088 psenc->g[target].interm = cs.interm;
1089
1090 planeok:
1091 /* invoke the plane onto GL or GR. */
1092 if (psenc->gl == target)
1093 goto sideok;
1094 if (bit8 && psenc->gr == target)
1095 goto sideok;
1096
1097 if (target == 0 && (ei->flags & F_LS0)) {
1098 *p++ = '\017';
1099 psenc->gl = 0;
1100 } else if (target == 1 && (ei->flags & F_LS1)) {
1101 *p++ = '\016';
1102 psenc->gl = 1;
1103 } else if (target == 2 && (ei->flags & F_LS2)) {
1104 *p++ = '\033';
1105 *p++ = 'n';
1106 psenc->gl = 2;
1107 } else if (target == 3 && (ei->flags & F_LS3)) {
1108 *p++ = '\033';
1109 *p++ = 'o';
1110 psenc->gl = 3;
1111 } else if (bit8 && target == 1 && (ei->flags & F_LS1R)) {
1112 *p++ = '\033';
1113 *p++ = '~';
1114 psenc->gr = 1;
1115 } else if (bit8 && target == 2 && (ei->flags & F_LS2R)) {
1116 *p++ = '\033';
1117 /*{*/
1118 *p++ = '}';
1119 psenc->gr = 2;
1120 } else if (bit8 && target == 3 && (ei->flags & F_LS3R)) {
1121 *p++ = '\033';
1122 *p++ = '|';
1123 psenc->gr = 3;
1124 } else if (target == 2 && (ei->flags & F_SS2)) {
1125 *p++ = '\033';
1126 *p++ = 'N';
1127 psenc->singlegl = 2;
1128 } else if (target == 3 && (ei->flags & F_SS3)) {
1129 *p++ = '\033';
1130 *p++ = 'O';
1131 psenc->singlegl = 3;
1132 } else if (bit8 && target == 2 && (ei->flags & F_SS2R)) {
1133 *p++ = '\216';
1134 *p++ = 'N';
1135 psenc->singlegl = psenc->singlegr = 2;
1136 } else if (bit8 && target == 3 && (ei->flags & F_SS3R)) {
1137 *p++ = '\217';
1138 *p++ = 'O';
1139 psenc->singlegl = psenc->singlegr = 3;
1140 } else
1141 abort();
1142
1143 sideok:
1144 if (psenc->singlegl == target)
1145 mask = 0x00;
1146 else if (psenc->singlegr == target)
1147 mask = 0x80;
1148 else if (psenc->gl == target)
1149 mask = 0x00;
1150 else if ((ei->flags & F_8BIT) && psenc->gr == target)
1151 mask = 0x80;
1152 else
1153 abort();
1154
1155 switch (cs.type) {
1156 case CS94:
1157 case CS96:
1158 i = 1;
1159 break;
1160 case CS94MULTI:
1161 case CS96MULTI:
1162 i = isthree(cs.final) ? 3 : 2;
1163 break;
1164 }
1165 while (i-- > 0)
1166 *p++ = ((wc >> (i << 3)) & 0x7f) | mask;
1167
1168 /* reset single shift state */
1169 psenc->singlegl = psenc->singlegr = -1;
1170
1171 len = p - tmp;
1172 if (n < len) {
1173 if (result)
1174 *result = (char *)0;
1175 } else {
1176 if (result)
1177 *result = string + len;
1178 memcpy(string, tmp, len);
1179 }
1180 return len;
1181 }
1182
1183 static int
1184 _citrus_ISO2022_put_state_reset(_ISO2022EncodingInfo * __restrict ei,
1185 char * __restrict s, size_t n,
1186 _ISO2022State * __restrict psenc,
1187 size_t * __restrict nresult)
1188 {
1189 char buf[MB_LEN_MAX];
1190 char *result;
1191 int len, ret;
1192
1193 _DIAGASSERT(ei != NULL);
1194 _DIAGASSERT(nresult != 0);
1195 _DIAGASSERT(s != NULL);
1196
1197 /* XXX state will be modified after this operation... */
1198 len = _ISO2022_sputwchar(ei, L'\0', buf, sizeof(buf), &result, psenc);
1199 if (len==0) {
1200 ret = EINVAL;
1201 goto err;
1202 }
1203 if (sizeof(buf) < len || n < len-1) {
1204 /* XXX should recover state? */
1205 ret = E2BIG;
1206 goto err;
1207 }
1208
1209 memcpy(s, buf, len-1);
1210 *nresult = (size_t)(len-1);
1211 return (0);
1212
1213 err:
1214 /* bound check failure */
1215 *nresult = (size_t)-1;
1216 return ret;
1217 }
1218
1219 static int
1220 _citrus_ISO2022_wcrtomb_priv(_ISO2022EncodingInfo * __restrict ei,
1221 char * __restrict s, size_t n, wchar_t wc,
1222 _ISO2022State * __restrict psenc,
1223 size_t * __restrict nresult)
1224 {
1225 char buf[MB_LEN_MAX];
1226 char *result;
1227 int len, ret;
1228
1229 _DIAGASSERT(ei != NULL);
1230 _DIAGASSERT(nresult != 0);
1231 _DIAGASSERT(s != NULL);
1232
1233 /* XXX state will be modified after this operation... */
1234 len = _ISO2022_sputwchar(ei, wc, buf, sizeof(buf), &result, psenc);
1235 if (sizeof(buf) < len || n < len) {
1236 /* XXX should recover state? */
1237 ret = E2BIG;
1238 goto err;
1239 }
1240
1241 memcpy(s, buf, len);
1242 *nresult = (size_t)len;
1243 return (0);
1244
1245 err:
1246 /* bound check failure */
1247 *nresult = (size_t)-1;
1248 return ret;
1249 }
1250
1251 static __inline int
1252 /*ARGSUSED*/
1253 _citrus_ISO2022_stdenc_wctocs(_ISO2022EncodingInfo * __restrict ei,
1254 _csid_t * __restrict csid,
1255 _index_t * __restrict idx, wchar_t wc)
1256 {
1257 wchar_t m, nm;
1258
1259 _DIAGASSERT(csid != NULL && idx != NULL);
1260
1261 m = wc & 0x7FFF8080;
1262 nm = wc & 0x007F7F7F;
1263 if (m & 0x00800000) {
1264 nm &= 0x00007F7F;
1265 } else {
1266 m &= 0x7F008080;
1267 }
1268 if (nm & 0x007F0000) {
1269 /* ^3 mark */
1270 m |= 0x007F0000;
1271 } else if (nm & 0x00007F00) {
1272 /* ^2 mark */
1273 m |= 0x00007F00;
1274 }
1275 *csid = (_csid_t)m;
1276 *idx = (_index_t)nm;
1277
1278 return (0);
1279 }
1280
1281 static __inline int
1282 /*ARGSUSED*/
1283 _citrus_ISO2022_stdenc_cstowc(_ISO2022EncodingInfo * __restrict ei,
1284 wchar_t * __restrict wc,
1285 _csid_t csid, _index_t idx)
1286 {
1287
1288 _DIAGASSERT(ei != NULL && wc != NULL);
1289
1290 *wc = (wchar_t)(csid & 0x7F808080) | (wchar_t)idx;
1291
1292 return (0);
1293 }
1294
1295 static __inline int
1296 /*ARGSUSED*/
1297 _citrus_ISO2022_stdenc_get_state_desc_generic(_ISO2022EncodingInfo * __restrict ei,
1298 _ISO2022State * __restrict psenc,
1299 int * __restrict rstate)
1300 {
1301
1302 if (psenc->chlen == 0) {
1303 /* XXX: it should distinguish initial and stable. */
1304 *rstate = _STDENC_SDGEN_STABLE;
1305 } else {
1306 if (psenc->ch[0] == '\033')
1307 *rstate = _STDENC_SDGEN_INCOMPLETE_SHIFT;
1308 else
1309 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
1310 }
1311
1312 return 0;
1313 }
1314
1315 /* ----------------------------------------------------------------------
1316 * public interface for ctype
1317 */
1318
1319 _CITRUS_CTYPE_DECLS(ISO2022);
1320 _CITRUS_CTYPE_DEF_OPS(ISO2022);
1321
1322 #include "citrus_ctype_template.h"
1323
1324 /* ----------------------------------------------------------------------
1325 * public interface for stdenc
1326 */
1327
1328 _CITRUS_STDENC_DECLS(ISO2022);
1329 _CITRUS_STDENC_DEF_OPS(ISO2022);
1330
1331 #include "citrus_stdenc_template.h"
1332