Home | History | Annotate | Line # | Download | only in modules
citrus_big5.c revision 1.10
      1 /*	$NetBSD: citrus_big5.c,v 1.10 2006/06/19 17:28:24 tnozaki Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2002, 2006 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 
     29 /*-
     30  * Copyright (c) 1993
     31  *	The Regents of the University of California.  All rights reserved.
     32  *
     33  * This code is derived from software contributed to Berkeley by
     34  * Paul Borman at Krystal Technologies.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 #if defined(LIBC_SCCS) && !defined(lint)
     63 __RCSID("$NetBSD: citrus_big5.c,v 1.10 2006/06/19 17:28:24 tnozaki Exp $");
     64 #endif /* LIBC_SCCS and not lint */
     65 
     66 #include <sys/queue.h>
     67 #include <sys/types.h>
     68 #include <assert.h>
     69 #include <errno.h>
     70 #include <string.h>
     71 #include <stdint.h>
     72 #include <stdio.h>
     73 #include <stdlib.h>
     74 #include <stddef.h>
     75 #include <locale.h>
     76 #include <wchar.h>
     77 #include <limits.h>
     78 
     79 #include "citrus_namespace.h"
     80 #include "citrus_types.h"
     81 #include "citrus_bcs.h"
     82 #include "citrus_module.h"
     83 #include "citrus_ctype.h"
     84 #include "citrus_stdenc.h"
     85 #include "citrus_big5.h"
     86 
     87 /* ----------------------------------------------------------------------
     88  * private stuffs used by templates
     89  */
     90 
     91 typedef struct {
     92 	char ch[2];
     93 	int chlen;
     94 } _BIG5State;
     95 
     96 typedef struct _BIG5Exclude {
     97 	TAILQ_ENTRY(_BIG5Exclude) entry;
     98 	wint_t start, end;
     99 } _BIG5Exclude;
    100 
    101 typedef TAILQ_HEAD(_BIG5ExcludeList, _BIG5Exclude) _BIG5ExcludeList;
    102 
    103 typedef struct {
    104 	int cell[0x100];
    105 	_BIG5ExcludeList excludes;
    106 } _BIG5EncodingInfo;
    107 
    108 typedef struct {
    109 	_BIG5EncodingInfo	ei;
    110 	struct {
    111 		/* for future multi-locale facility */
    112 		_BIG5State	s_mblen;
    113 		_BIG5State	s_mbrlen;
    114 		_BIG5State	s_mbrtowc;
    115 		_BIG5State	s_mbtowc;
    116 		_BIG5State	s_mbsrtowcs;
    117 		_BIG5State	s_wcrtomb;
    118 		_BIG5State	s_wcsrtombs;
    119 		_BIG5State	s_wctomb;
    120 	} states;
    121 } _BIG5CTypeInfo;
    122 
    123 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
    124 #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
    125 
    126 #define _FUNCNAME(m)			_citrus_BIG5_##m
    127 #define _ENCODING_INFO			_BIG5EncodingInfo
    128 #define _CTYPE_INFO			_BIG5CTypeInfo
    129 #define _ENCODING_STATE			_BIG5State
    130 #define _ENCODING_MB_CUR_MAX(_ei_)	2
    131 #define _ENCODING_IS_STATE_DEPENDENT	0
    132 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
    133 
    134 
    135 static __inline void
    136 /*ARGSUSED*/
    137 _citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei,
    138 			_BIG5State * __restrict s)
    139 {
    140 	memset(s, 0, sizeof(*s));
    141 }
    142 
    143 static __inline void
    144 /*ARGSUSED*/
    145 _citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei,
    146 			void * __restrict pspriv,
    147 			const _BIG5State * __restrict s)
    148 {
    149 	memcpy(pspriv, (const void *)s, sizeof(*s));
    150 }
    151 
    152 static __inline void
    153 /*ARGSUSED*/
    154 _citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei,
    155 			  _BIG5State * __restrict s,
    156 			  const void * __restrict pspriv)
    157 {
    158 	memcpy((void *)s, pspriv, sizeof(*s));
    159 }
    160 
    161 static __inline int
    162 _citrus_BIG5_check(_BIG5EncodingInfo *ei, u_int c)
    163 {
    164 	_DIAGASSERT(ei != NULL);
    165 
    166 	return (ei->cell[c & 0xFF] & 0x1) ? 2 : 1;
    167 }
    168 
    169 static __inline int
    170 _citrus_BIG5_check2(_BIG5EncodingInfo *ei, u_int c)
    171 {
    172 	_DIAGASSERT(ei != NULL);
    173 
    174 	return (ei->cell[c & 0xFF] & 0x2) ? 1 : 0;
    175 }
    176 
    177 static __inline int
    178 _citrus_BIG5_check_excludes(_BIG5EncodingInfo *ei, wint_t c)
    179 {
    180 	_BIG5Exclude *exclude;
    181 
    182 	_DIAGASSERT(ei != NULL);
    183 
    184 	TAILQ_FOREACH(exclude, &ei->excludes, entry) {
    185 		if (c >= exclude->start && c <= exclude->end)
    186 			return EILSEQ;
    187 	}
    188 	return 0;
    189 }
    190 
    191 #define _STRTOL_LEN(_func_, _type_, _limit_)		\
    192 static int						\
    193 _prop_##_func_##_len(const char *s, size_t *n,		\
    194 	char **endptr, int base, _type_ *result)	\
    195 {							\
    196 	const char *ptr;				\
    197 	char buf[_limit_ + 1], *tail;			\
    198 	size_t siz;					\
    199 	int save_errno, err;				\
    200 							\
    201 	_DIAGASSERT(s != NULL);				\
    202 	_DIAGASSERT(n != NULL);				\
    203 	/* endptr may be null */			\
    204 	_DIAGASSERT(result != NULL);			\
    205 							\
    206 	ptr = _bcs_skip_ws_len(s, n);			\
    207 	if (*n < 1) {					\
    208 		*result = 0;				\
    209 		if (endptr != NULL)			\
    210 			*endptr = __UNCONST(s);		\
    211 		return 0;				\
    212 	}						\
    213 	siz = _limit_ < *n ? _limit_ : *n;		\
    214 	memcpy(buf, ptr, siz);				\
    215 	buf[siz] = '\0';				\
    216 							\
    217 	save_errno = errno;				\
    218 	errno = 0;					\
    219 	*result = _func_(buf, &tail, base);		\
    220 	err = errno;					\
    221 	errno = save_errno;				\
    222 	if (err == 0) {					\
    223 		siz = (size_t)(tail - buf);		\
    224 		if (endptr != NULL)			\
    225 			*endptr = __UNCONST(ptr + siz);	\
    226 		*n -= siz;				\
    227 	}						\
    228 	return (err);					\
    229 }
    230 _STRTOL_LEN(strtoul, unsigned long, 13)
    231 
    232 #define _PROP_READ_UINT(_func_, _type_, _max_)			\
    233 static int							\
    234 _prop_read_##_func_(const char **s, size_t *n, _type_ *result)	\
    235 {								\
    236 	const char *s0;						\
    237 	char *t;						\
    238 	size_t n0;						\
    239 	unsigned long l;					\
    240 								\
    241 	_DIAGASSERT(s != NULL);					\
    242 	_DIAGASSERT(n != NULL);					\
    243 	_DIAGASSERT(result != NULL);				\
    244 								\
    245 	s0 = *s;						\
    246 	n0 = *n;						\
    247 	if (_prop_strtoul_len(s0, &n0, &t, 0, &l) != 0 ||	\
    248 	    s0 == t)						\
    249 		return EINVAL;					\
    250 	if (l > _max_)						\
    251 		return ERANGE;					\
    252 	*result = (_type_)l;					\
    253 	*s = (const char *)t;					\
    254 	*n = n0;						\
    255 								\
    256 	return 0;						\
    257 }
    258 _PROP_READ_UINT( u8,  uint8_t,  UINT8_MAX)
    259 _PROP_READ_UINT(u16, uint16_t, UINT16_MAX)
    260 
    261 #define _PROP_HANDLE_TYPE_T(_func_, _type_)			\
    262 typedef int (*_prop_handle_##_func_##_t)			\
    263 	(void *, const char *, _type_, _type_);
    264 _PROP_HANDLE_TYPE_T( u8,  uint8_t)
    265 _PROP_HANDLE_TYPE_T(u16, uint16_t)
    266 
    267 typedef struct _prop_key_t _prop_key_t;
    268 
    269 typedef union {
    270 #define _PROP_HANDLE_TYPE_OPS(_func_) \
    271 	_prop_handle_##_func_##_t _func_
    272 
    273 	_PROP_HANDLE_TYPE_OPS( u8);
    274 	_PROP_HANDLE_TYPE_OPS(u16);
    275 } _prop_handler_t;
    276 
    277 typedef enum {
    278 	_PROP_U8, _PROP_U16,
    279 } _prop_type_t;
    280 
    281 struct _prop_key_t {
    282 	const char *name;
    283 	_prop_type_t type;
    284 	_prop_handler_t handler;
    285 };
    286 
    287 static int
    288 _prop_parse_variable(void *ctx, const char **s, size_t *n,
    289 	const _prop_key_t *keys)
    290 {
    291 	const char *s0, *s1;
    292 	size_t n0, n1, nlen;
    293 	const _prop_key_t *key;
    294 	const _prop_handler_t *ptr;
    295 	int ch0, ret;
    296 
    297 	s0 = *s;
    298 	n0 = *n;
    299 
    300 #define PARSE(_func_, _type_)					\
    301 do {								\
    302 	_type_ x, y;						\
    303 	if (ptr->_func_ == NULL)				\
    304 		return EINVAL;					\
    305 	for (ch0 = 0; ch0 != ';';) {				\
    306 		ret = _prop_read_##_func_(&s0, &n0, &x);	\
    307 		if (ret != 0)					\
    308 			return ret;				\
    309 		s0 = _bcs_skip_ws_len(s0, &n0);			\
    310 		if (n0 < 1 || (ch0 = (int)*s0) == '\0')		\
    311 			return EINVAL;				\
    312 		if(ch0 == '-') {				\
    313 			++s0, --n0;				\
    314 			ret = _prop_read_##_func_(&s0, &n0, &y);\
    315 			if (ret != 0)				\
    316 				return ret;			\
    317 			if (x >= y)				\
    318 				return EINVAL;			\
    319 		} else						\
    320 			y = x;					\
    321 		s0 = _bcs_skip_ws_len(s0, &n0);			\
    322 		if (n0 < 1)					\
    323 			return EINVAL;				\
    324 		ch0 = (int)*s0;					\
    325 		if (ch0 != ',' && ch0 != ';')			\
    326 			return EINVAL;				\
    327 		++s0, --n0;					\
    328 		_DIAGASSERT(ctx != NULL);			\
    329 		ret = (*ptr->_func_)(ctx, key->name, x, y);	\
    330 		if (ret != 0)					\
    331 			return ret;				\
    332 	}							\
    333 } while (/*CONSTCOND*/0)
    334 
    335 	for (;;) {
    336 		s0 = _bcs_skip_ws_len(s0, &n0);
    337 		if (n0 < 1 || *s0 == '\0')
    338 			break;
    339 		for (key = keys; key->name != NULL; ++key) {
    340 			s1 = s0, n1 = n0;
    341 			nlen = strlen(key->name);
    342 			if (n1 <= nlen || strncmp(s1, key->name, nlen) != 0)
    343 				continue;
    344 			s1 += nlen, n1 -= nlen;
    345 			s1 = _bcs_skip_ws_len(s1, &n1);
    346 			if (n1 < 1)
    347 				continue;
    348 			ptr = (const _prop_handler_t *)&key->handler;
    349 			if (*s1 == '=') {
    350 				s0 = ++s1, n0 = --n1;
    351 				switch (key->type) {
    352 				case _PROP_U8:
    353 					PARSE( u8,  uint8_t);
    354 					break;
    355 				case _PROP_U16:
    356 					PARSE(u16, uint16_t);
    357 						break;
    358 				default:
    359 					goto invalid;
    360 				}
    361 				break;
    362 			}
    363 		}
    364 		if (key->name == NULL)
    365 			goto invalid;
    366 	}
    367 	*s = s0;
    368 	*n = n0;
    369 
    370 	return 0;
    371 
    372 invalid:
    373 	return EINVAL;
    374 }
    375 
    376 static int
    377 _citrus_BIG5_fill_rowcol(void * __restrict ctx, const char * __restrict s,
    378 	uint8_t start, uint8_t end)
    379 {
    380 	_BIG5EncodingInfo *ei;
    381 	int i;
    382 	uint8_t n;
    383 
    384 	_DIAGASSERT(ctx != NULL);
    385 
    386 	ei = (_BIG5EncodingInfo *)ctx;
    387 	i = strcmp("row", s) ? 1 : 0;
    388 	i = 1 << i;
    389 	for (n = start; n <= end; ++n)
    390 		ei->cell[n & 0xFF] |= i;
    391 	return 0;
    392 }
    393 
    394 static int
    395 /*ARGSUSED*/
    396 _citrus_BIG5_fill_excludes(void * __restrict ctx, const char * __restrict s,
    397 	uint16_t start, uint16_t end)
    398 {
    399 	_BIG5EncodingInfo *ei;
    400 	_BIG5Exclude *exclude;
    401 
    402 	_DIAGASSERT(ctx != NULL);
    403 
    404 	ei = (_BIG5EncodingInfo *)ctx;
    405 	exclude = TAILQ_LAST(&ei->excludes, _BIG5ExcludeList);
    406 	if (exclude != NULL && (wint_t)start <= exclude->end)
    407 		return EINVAL;
    408 	exclude = (void *)malloc(sizeof(*exclude));
    409 	if (exclude == NULL)
    410 		return ENOMEM;
    411 	exclude->start = (wint_t)start;
    412 	exclude->end = (wint_t)end;
    413 	TAILQ_INSERT_TAIL(&ei->excludes, exclude, entry);
    414 
    415 	return 0;
    416 }
    417 
    418 static const _prop_key_t rootkeys[] = {
    419 	{ "row",      _PROP_U8,  { u8:  &_citrus_BIG5_fill_rowcol   } },
    420 	{ "col",      _PROP_U8,  { u8:  &_citrus_BIG5_fill_rowcol   } },
    421 	{ "excludes", _PROP_U16, { u16: &_citrus_BIG5_fill_excludes } },
    422 	{ NULL },
    423 };
    424 
    425 static void
    426 /*ARGSUSED*/
    427 _citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei)
    428 {
    429 	_BIG5Exclude *exclude;
    430 
    431 	_DIAGASSERT(ei != NULL);
    432 
    433 	while ((exclude = TAILQ_FIRST(&ei->excludes)) != NULL) {
    434 		TAILQ_REMOVE(&ei->excludes, exclude, entry);
    435 		free(exclude);
    436 	}
    437 }
    438 
    439 static int
    440 /*ARGSUSED*/
    441 _citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei,
    442 				  const void * __restrict var, size_t lenvar)
    443 {
    444 	int err;
    445 	const char *s;
    446 
    447 	_DIAGASSERT(ei != NULL);
    448 
    449 	memset((void *)ei, 0, sizeof(*ei));
    450 	TAILQ_INIT(&ei->excludes);
    451 
    452 	if (lenvar > 0 && var != NULL) {
    453 		s = _bcs_skip_ws_len((const char *)var, &lenvar);
    454 		if (lenvar > 0 && *s != '\0') {
    455 			err = _prop_parse_variable(ei, &s, &lenvar, rootkeys);
    456 			if (err == 0)
    457 				return 0;
    458 
    459 			_citrus_BIG5_encoding_module_uninit(ei);
    460 			memset((void *)ei, 0, sizeof(*ei));
    461 			TAILQ_INIT(&ei->excludes);
    462 		}
    463 	}
    464 
    465 	/* fallback Big5-1984, for backward compatibility. */
    466 	_citrus_BIG5_fill_rowcol(ei, "row", 0xA1, 0xFE);
    467 	_citrus_BIG5_fill_rowcol(ei, "col", 0x40, 0x7E);
    468 	_citrus_BIG5_fill_rowcol(ei, "col", 0xA1, 0xFE);
    469 
    470 	return 0;
    471 }
    472 
    473 static int
    474 /*ARGSUSED*/
    475 _citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,
    476 			  wchar_t * __restrict pwc,
    477 			  const char ** __restrict s, size_t n,
    478 			  _BIG5State * __restrict psenc,
    479 			  size_t * __restrict nresult)
    480 {
    481 	wchar_t wchar;
    482 	int c;
    483 	int chlenbak;
    484 	const char *s0;
    485 
    486 	_DIAGASSERT(nresult != 0);
    487 	_DIAGASSERT(ei != NULL);
    488 	_DIAGASSERT(psenc != NULL);
    489 	_DIAGASSERT(s != NULL && *s != NULL);
    490 
    491 	s0 = *s;
    492 
    493 	if (s0 == NULL) {
    494 		_citrus_BIG5_init_state(ei, psenc);
    495 		*nresult = 0;
    496 		return (0);
    497 	}
    498 
    499 	chlenbak = psenc->chlen;
    500 
    501 	/* make sure we have the first byte in the buffer */
    502 	switch (psenc->chlen) {
    503 	case 0:
    504 		if (n < 1)
    505 			goto restart;
    506 		psenc->ch[0] = *s0++;
    507 		psenc->chlen = 1;
    508 		n--;
    509 		break;
    510 	case 1:
    511 		break;
    512 	default:
    513 		/* illegal state */
    514 		goto ilseq;
    515 	}
    516 
    517 	c = _citrus_BIG5_check(ei, psenc->ch[0] & 0xff);
    518 	if (c == 0)
    519 		goto ilseq;
    520 	while (psenc->chlen < c) {
    521 		if (n < 1) {
    522 			goto restart;
    523 		}
    524 		psenc->ch[psenc->chlen] = *s0++;
    525 		psenc->chlen++;
    526 		n--;
    527 	}
    528 
    529 	switch (c) {
    530 	case 1:
    531 		wchar = psenc->ch[0] & 0xff;
    532 		break;
    533 	case 2:
    534 		if (!_citrus_BIG5_check2(ei, psenc->ch[1] & 0xff))
    535 			goto ilseq;
    536 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
    537 		break;
    538 	default:
    539 		/* illegal state */
    540 		goto ilseq;
    541 	}
    542 
    543 	if (_citrus_BIG5_check_excludes(ei, (wint_t)wchar) != 0)
    544 		goto ilseq;
    545 
    546 	*s = s0;
    547 	psenc->chlen = 0;
    548 	if (pwc)
    549 		*pwc = wchar;
    550 	if (!wchar)
    551 		*nresult = 0;
    552 	else
    553 		*nresult = c - chlenbak;
    554 
    555 	return (0);
    556 
    557 ilseq:
    558 	psenc->chlen = 0;
    559 	*nresult = (size_t)-1;
    560 	return (EILSEQ);
    561 
    562 restart:
    563 	*s = s0;
    564 	*nresult = (size_t)-2;
    565 	return (0);
    566 }
    567 
    568 static int
    569 /*ARGSUSED*/
    570 _citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,
    571 			  char * __restrict s,
    572 			  size_t n, wchar_t wc, _BIG5State * __restrict psenc,
    573 			  size_t * __restrict nresult)
    574 {
    575 	int l, ret;
    576 
    577 	_DIAGASSERT(ei != NULL);
    578 	_DIAGASSERT(nresult != 0);
    579 	_DIAGASSERT(s != NULL);
    580 
    581 	/* check invalid sequence */
    582 	if (wc & ~0xffff ||
    583 	    _citrus_BIG5_check_excludes(ei, (wint_t)wc) != 0) {
    584 		ret = EILSEQ;
    585 		goto err;
    586 	}
    587 
    588 	if (wc & 0x8000) {
    589 		if (_citrus_BIG5_check(ei, (wc >> 8) & 0xff) != 2 ||
    590 		    !_citrus_BIG5_check2(ei, wc & 0xff)) {
    591 			ret = EILSEQ;
    592 			goto err;
    593 		}
    594 		l = 2;
    595 	} else {
    596 		if (wc & ~0xff || !_citrus_BIG5_check(ei, wc & 0xff)) {
    597 			ret = EILSEQ;
    598 			goto err;
    599 		}
    600 		l = 1;
    601 	}
    602 
    603 	if (n < l) {
    604 		/* bound check failure */
    605 		ret = E2BIG;
    606 		goto err;
    607 	}
    608 
    609 	if (l == 2) {
    610 		s[0] = (wc >> 8) & 0xff;
    611 		s[1] = wc & 0xff;
    612 	} else
    613 		s[0] = wc & 0xff;
    614 
    615 	*nresult = l;
    616 
    617 	return 0;
    618 
    619 err:
    620 	*nresult = (size_t)-1;
    621 	return ret;
    622 }
    623 
    624 static __inline int
    625 /*ARGSUSED*/
    626 _citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei,
    627 			   _csid_t * __restrict csid,
    628 			   _index_t * __restrict idx, wchar_t wc)
    629 {
    630 
    631 	_DIAGASSERT(csid != NULL && idx != NULL);
    632 
    633 	*csid = (wc < 0x100) ? 0 : 1;
    634 	*idx = (_index_t)wc;
    635 
    636 	return 0;
    637 }
    638 
    639 static __inline int
    640 /*ARGSUSED*/
    641 _citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei,
    642 			   wchar_t * __restrict wc,
    643 			   _csid_t csid, _index_t idx)
    644 {
    645 	_DIAGASSERT(wc != NULL);
    646 
    647 	switch (csid) {
    648 	case 0:
    649 	case 1:
    650 		*wc = (wchar_t)idx;
    651 		break;
    652 	default:
    653 		return EILSEQ;
    654 	}
    655 
    656 	return 0;
    657 }
    658 
    659 static __inline int
    660 /*ARGSUSED*/
    661 _citrus_BIG5_stdenc_get_state_desc_generic(_BIG5EncodingInfo * __restrict ei,
    662 					   _BIG5State * __restrict psenc,
    663 					   int * __restrict rstate)
    664 {
    665 
    666 	if (psenc->chlen == 0)
    667 		*rstate = _STDENC_SDGEN_INITIAL;
    668 	else
    669 		*rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
    670 
    671 	return 0;
    672 }
    673 
    674 /* ----------------------------------------------------------------------
    675  * public interface for ctype
    676  */
    677 
    678 _CITRUS_CTYPE_DECLS(BIG5);
    679 _CITRUS_CTYPE_DEF_OPS(BIG5);
    680 
    681 #include "citrus_ctype_template.h"
    682 
    683 
    684 /* ----------------------------------------------------------------------
    685  * public interface for stdenc
    686  */
    687 
    688 _CITRUS_STDENC_DECLS(BIG5);
    689 _CITRUS_STDENC_DEF_OPS(BIG5);
    690 
    691 #include "citrus_stdenc_template.h"
    692