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