Home | History | Annotate | Line # | Download | only in modules
citrus_big5.c revision 1.15.18.3
      1 /*	$NetBSD: citrus_big5.c,v 1.15.18.3 2017/07/31 04:23:35 perseant 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.15.18.3 2017/07/31 04:23:35 perseant 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 <wchar.h>
     76 #include <limits.h>
     77 
     78 #include "citrus_namespace.h"
     79 #include "citrus_types.h"
     80 #include "citrus_bcs.h"
     81 #include "citrus_module.h"
     82 #include "citrus_ctype.h"
     83 #include "citrus_stdenc.h"
     84 #include "citrus_big5.h"
     85 
     86 #include "citrus_prop.h"
     87 
     88 /* ----------------------------------------------------------------------
     89  * private stuffs used by templates
     90  */
     91 
     92 typedef struct {
     93 	char ch[2];
     94 	int chlen;
     95 } _BIG5State;
     96 
     97 typedef struct _BIG5Exclude {
     98 	TAILQ_ENTRY(_BIG5Exclude) entry;
     99 	wint_kuten_t start, end;
    100 } _BIG5Exclude;
    101 
    102 typedef TAILQ_HEAD(_BIG5ExcludeList, _BIG5Exclude) _BIG5ExcludeList;
    103 
    104 typedef struct {
    105 	int cell[0x100];
    106 	_BIG5ExcludeList excludes;
    107 } _BIG5EncodingInfo;
    108 
    109 typedef struct {
    110 	_BIG5EncodingInfo	ei;
    111 	struct {
    112 		/* for future multi-locale facility */
    113 		_BIG5State	s_mblen;
    114 		_BIG5State	s_mbrlen;
    115 		_BIG5State	s_mbrtowc;
    116 		_BIG5State	s_mbtowc;
    117 		_BIG5State	s_mbsrtowcs;
    118 		_BIG5State	s_mbsnrtowcs;
    119 		_BIG5State	s_wcrtomb;
    120 		_BIG5State	s_wcsrtombs;
    121 		_BIG5State	s_wcsnrtombs;
    122 		_BIG5State	s_wctomb;
    123 	} states;
    124 } _BIG5CTypeInfo;
    125 
    126 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
    127 #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
    128 
    129 #define _FUNCNAME(m)			_citrus_BIG5_##m
    130 #define _ENCODING_INFO			_BIG5EncodingInfo
    131 #define _CTYPE_INFO			_BIG5CTypeInfo
    132 #define _ENCODING_STATE			_BIG5State
    133 #define _ENCODING_MB_CUR_MAX(_ei_)	2
    134 #define _ENCODING_IS_STATE_DEPENDENT	0
    135 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
    136 
    137 #ifdef __STDC_ISO_10646__
    138 #include "citrus_big5_data.h"
    139 
    140 static __inline int
    141 /*ARGSUSED*/
    142 _FUNCNAME(ucs2kt)(_ENCODING_INFO * __restrict ei,
    143 		  wchar_kuten_t * __restrict ktp, wchar_ucs4_t wc)
    144 {
    145 	struct unicode2kuten_lookup *uk;
    146 
    147 	_DIAGASSERT(ktp != NULL);
    148 
    149 	/* US-ASCII are not in the list */
    150 	if (wc < 0x80) {
    151 		*ktp = wc;
    152 		return 0;
    153 	}
    154 
    155 	uk = _citrus_uk_bsearch(wc, __big5_table__unicode2kuten_lookup, _BIG5_TABLE__U2K_LIST_LENGTH);
    156 
    157 	if (uk == NULL)
    158 		*ktp = WEOF;
    159 	else
    160 		*ktp = uk->value;
    161 
    162 	return 0;
    163 }
    164 
    165 static __inline int
    166 /*ARGSUSED*/
    167 _FUNCNAME(kt2ucs)(_ENCODING_INFO * __restrict ei,
    168 		  wchar_ucs4_t * __restrict up, wchar_kuten_t kt)
    169 {
    170 	_csid_t csid;
    171 	_index_t idx;
    172 	struct unicode2kuten_lookup *uk, *table;
    173 
    174 	_DIAGASSERT(up != NULL);
    175 
    176 	table = NULL;
    177 
    178 	if (kt < 0x80) {
    179 		*up = kt;
    180 		return 0;
    181 	}
    182 
    183 	uk = _citrus_uk_bsearch(kt, __big5_table__kuten2unicode_lookup, _BIG5_TABLE__K2U_LIST_LENGTH);
    184 
    185 	if (uk == NULL)
    186 		*up = WEOF;
    187 	else
    188 		*up = uk->value;
    189 
    190 	return 0;
    191 }
    192 #else
    193 #include "citrus_u2k_template.h"
    194 #endif
    195 
    196 static __inline void
    197 /*ARGSUSED*/
    198 _citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei,
    199 			_BIG5State * __restrict s)
    200 {
    201 	memset(s, 0, sizeof(*s));
    202 }
    203 
    204 static __inline void
    205 /*ARGSUSED*/
    206 _citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei,
    207 			void * __restrict pspriv,
    208 			const _BIG5State * __restrict s)
    209 {
    210 	memcpy(pspriv, (const void *)s, sizeof(*s));
    211 }
    212 
    213 static __inline void
    214 /*ARGSUSED*/
    215 _citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei,
    216 			  _BIG5State * __restrict s,
    217 			  const void * __restrict pspriv)
    218 {
    219 	memcpy((void *)s, pspriv, sizeof(*s));
    220 }
    221 
    222 static __inline int
    223 _citrus_BIG5_check(_BIG5EncodingInfo *ei, u_int c)
    224 {
    225 	_DIAGASSERT(ei != NULL);
    226 
    227 	return (ei->cell[c & 0xFF] & 0x1) ? 2 : 1;
    228 }
    229 
    230 static __inline int
    231 _citrus_BIG5_check2(_BIG5EncodingInfo *ei, u_int c)
    232 {
    233 	_DIAGASSERT(ei != NULL);
    234 
    235 	return (ei->cell[c & 0xFF] & 0x2) ? 1 : 0;
    236 }
    237 
    238 static __inline int
    239 _citrus_BIG5_check_excludes(_BIG5EncodingInfo *ei, wint_kuten_t c)
    240 {
    241 	_BIG5Exclude *exclude;
    242 
    243 	_DIAGASSERT(ei != NULL);
    244 
    245 	TAILQ_FOREACH(exclude, &ei->excludes, entry) {
    246 		if (c >= exclude->start && c <= exclude->end)
    247 			return EILSEQ;
    248 	}
    249 	return 0;
    250 }
    251 
    252 static int
    253 _citrus_BIG5_fill_rowcol(void * __restrict ctx, const char * __restrict s,
    254 	uint64_t start, uint64_t end)
    255 {
    256 	_BIG5EncodingInfo *ei;
    257 	int i;
    258 	uint64_t n;
    259 
    260 	_DIAGASSERT(ctx != NULL);
    261 
    262 	if (start > 0xFF || end > 0xFF)
    263 		return EINVAL;
    264 	ei = (_BIG5EncodingInfo *)ctx;
    265 	i = strcmp("row", s) ? 1 : 0;
    266 	i = 1 << i;
    267 	for (n = start; n <= end; ++n)
    268 		ei->cell[n & 0xFF] |= i;
    269 	return 0;
    270 }
    271 
    272 static int
    273 /*ARGSUSED*/
    274 _citrus_BIG5_fill_excludes(void * __restrict ctx, const char * __restrict s,
    275 	uint64_t start, uint64_t end)
    276 {
    277 	_BIG5EncodingInfo *ei;
    278 	_BIG5Exclude *exclude;
    279 
    280 	_DIAGASSERT(ctx != NULL);
    281 
    282 	if (start > 0xFFFF || end > 0xFFFF)
    283 		return EINVAL;
    284 	ei = (_BIG5EncodingInfo *)ctx;
    285 	exclude = TAILQ_LAST(&ei->excludes, _BIG5ExcludeList);
    286 	if (exclude != NULL && (wint_kuten_t)start <= exclude->end)
    287 		return EINVAL;
    288 	exclude = (void *)malloc(sizeof(*exclude));
    289 	if (exclude == NULL)
    290 		return ENOMEM;
    291 	exclude->start = (wint_kuten_t)start;
    292 	exclude->end = (wint_kuten_t)end;
    293 	TAILQ_INSERT_TAIL(&ei->excludes, exclude, entry);
    294 
    295 	return 0;
    296 }
    297 
    298 static const _citrus_prop_hint_t root_hints[] = {
    299     _CITRUS_PROP_HINT_NUM("row", &_citrus_BIG5_fill_rowcol),
    300     _CITRUS_PROP_HINT_NUM("col", &_citrus_BIG5_fill_rowcol),
    301     _CITRUS_PROP_HINT_NUM("excludes", &_citrus_BIG5_fill_excludes),
    302     _CITRUS_PROP_HINT_END
    303 };
    304 
    305 static void
    306 /*ARGSUSED*/
    307 _citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei)
    308 {
    309 	_BIG5Exclude *exclude;
    310 
    311 	_DIAGASSERT(ei != NULL);
    312 
    313 	while ((exclude = TAILQ_FIRST(&ei->excludes)) != NULL) {
    314 		TAILQ_REMOVE(&ei->excludes, exclude, entry);
    315 		free(exclude);
    316 	}
    317 }
    318 
    319 static int
    320 /*ARGSUSED*/
    321 _citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei,
    322 				  const void * __restrict var, size_t lenvar)
    323 {
    324 	int err;
    325 	const char *s;
    326 
    327 	_DIAGASSERT(ei != NULL);
    328 
    329 	memset((void *)ei, 0, sizeof(*ei));
    330 	TAILQ_INIT(&ei->excludes);
    331 
    332 	if (lenvar > 0 && var != NULL) {
    333 		s = _bcs_skip_ws_len((const char *)var, &lenvar);
    334 		if (lenvar > 0 && *s != '\0') {
    335 			err = _citrus_prop_parse_variable(
    336 			    root_hints, (void *)ei, s, lenvar);
    337 			if (err == 0)
    338 				return 0;
    339 
    340 			_citrus_BIG5_encoding_module_uninit(ei);
    341 			memset((void *)ei, 0, sizeof(*ei));
    342 			TAILQ_INIT(&ei->excludes);
    343 		}
    344 	}
    345 
    346 	/* fallback Big5-1984, for backward compatibility. */
    347 	_citrus_BIG5_fill_rowcol(ei, "row", 0xA1, 0xFE);
    348 	_citrus_BIG5_fill_rowcol(ei, "col", 0x40, 0x7E);
    349 	_citrus_BIG5_fill_rowcol(ei, "col", 0xA1, 0xFE);
    350 
    351 	return 0;
    352 }
    353 
    354 static int
    355 /*ARGSUSED*/
    356 _citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,
    357 			  wchar_ucs4_t * __restrict pwc,
    358 			  const char ** __restrict s, size_t n,
    359 			  _BIG5State * __restrict psenc,
    360 			  size_t * __restrict nresult)
    361 {
    362 	wchar_kuten_t wchar;
    363 	int c;
    364 	int chlenbak;
    365 	const char *s0;
    366 
    367 	_DIAGASSERT(nresult != 0);
    368 	_DIAGASSERT(ei != NULL);
    369 	_DIAGASSERT(psenc != NULL);
    370 	_DIAGASSERT(s != NULL && *s != NULL);
    371 
    372 	s0 = *s;
    373 
    374 	if (s0 == NULL) {
    375 		_citrus_BIG5_init_state(ei, psenc);
    376 		*nresult = 0;
    377 		return (0);
    378 	}
    379 
    380 	chlenbak = psenc->chlen;
    381 
    382 	/* make sure we have the first byte in the buffer */
    383 	switch (psenc->chlen) {
    384 	case 0:
    385 		if (n < 1)
    386 			goto restart;
    387 		psenc->ch[0] = *s0++;
    388 		psenc->chlen = 1;
    389 		n--;
    390 		break;
    391 	case 1:
    392 		break;
    393 	default:
    394 		/* illegal state */
    395 		goto ilseq;
    396 	}
    397 
    398 	c = _citrus_BIG5_check(ei, psenc->ch[0] & 0xff);
    399 	if (c == 0)
    400 		goto ilseq;
    401 	while (psenc->chlen < c) {
    402 		if (n < 1) {
    403 			goto restart;
    404 		}
    405 		psenc->ch[psenc->chlen] = *s0++;
    406 		psenc->chlen++;
    407 		n--;
    408 	}
    409 
    410 	switch (c) {
    411 	case 1:
    412 		wchar = psenc->ch[0] & 0xff;
    413 		break;
    414 	case 2:
    415 		if (!_citrus_BIG5_check2(ei, psenc->ch[1] & 0xff))
    416 			goto ilseq;
    417 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
    418 		break;
    419 	default:
    420 		/* illegal state */
    421 		goto ilseq;
    422 	}
    423 
    424 	if (_citrus_BIG5_check_excludes(ei, (wint_kuten_t)wchar) != 0)
    425 		goto ilseq;
    426 
    427 	*s = s0;
    428 	psenc->chlen = 0;
    429 	if (pwc)
    430 		_citrus_BIG5_kt2ucs(ei, pwc, wchar);
    431 	if (!wchar)
    432 		*nresult = 0;
    433 	else
    434 		*nresult = c - chlenbak;
    435 
    436 	return (0);
    437 
    438 ilseq:
    439 	psenc->chlen = 0;
    440 	*nresult = (size_t)-1;
    441 	return (EILSEQ);
    442 
    443 restart:
    444 	*s = s0;
    445 	*nresult = (size_t)-2;
    446 	return (0);
    447 }
    448 
    449 static int
    450 /*ARGSUSED*/
    451 _citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,
    452 			  char * __restrict s,
    453 			  size_t n, wchar_ucs4_t wc, _BIG5State * __restrict psenc,
    454 			  size_t * __restrict nresult)
    455 {
    456 	size_t l, ret;
    457 
    458 	_DIAGASSERT(ei != NULL);
    459 	_DIAGASSERT(nresult != 0);
    460 	_DIAGASSERT(s != NULL);
    461 
    462 	_citrus_BIG5_ucs2kt(ei, &wc, wc);
    463 
    464 	/* check invalid sequence */
    465 	if (wc & ~0xffff ||
    466 	    _citrus_BIG5_check_excludes(ei, (wint_kuten_t)wc) != 0) {
    467 		ret = EILSEQ;
    468 		goto err;
    469 	}
    470 
    471 	if (wc & 0x8000) {
    472 		if (_citrus_BIG5_check(ei, (wc >> 8) & 0xff) != 2 ||
    473 		    !_citrus_BIG5_check2(ei, wc & 0xff)) {
    474 			ret = EILSEQ;
    475 			goto err;
    476 		}
    477 		l = 2;
    478 	} else {
    479 		if (wc & ~0xff || !_citrus_BIG5_check(ei, wc & 0xff)) {
    480 			ret = EILSEQ;
    481 			goto err;
    482 		}
    483 		l = 1;
    484 	}
    485 
    486 	if (n < l) {
    487 		/* bound check failure */
    488 		ret = E2BIG;
    489 		goto err;
    490 	}
    491 
    492 	if (l == 2) {
    493 		s[0] = (wc >> 8) & 0xff;
    494 		s[1] = wc & 0xff;
    495 	} else
    496 		s[0] = wc & 0xff;
    497 
    498 	*nresult = l;
    499 
    500 	return 0;
    501 
    502 err:
    503 	*nresult = (size_t)-1;
    504 	return ret;
    505 }
    506 
    507 static int
    508 /*ARGSUSED*/
    509 _citrus_BIG5_stdenc_wctocs(struct _citrus_stdenc *ce,
    510 			   _csid_t * __restrict csid,
    511 			   _index_t * __restrict idx, wchar_kuten_t wc)
    512 {
    513 
    514 	_DIAGASSERT(csid != NULL && idx != NULL);
    515 
    516 	*csid = (wc < 0x100) ? 0 : 1;
    517 	*idx = (_index_t)wc;
    518 
    519 	return 0;
    520 }
    521 
    522 static int
    523 /*ARGSUSED*/
    524 _citrus_BIG5_stdenc_cstowc(struct _citrus_stdenc *ce,
    525 			   wchar_kuten_t * __restrict wc,
    526 			   _csid_t csid, _index_t idx)
    527 {
    528 	_DIAGASSERT(wc != NULL);
    529 
    530 	switch (csid) {
    531 	case 0:
    532 	case 1:
    533 		*wc = (wchar_kuten_t)idx;
    534 		break;
    535 	default:
    536 		return EILSEQ;
    537 	}
    538 
    539 	return 0;
    540 }
    541 
    542 static __inline int
    543 /*ARGSUSED*/
    544 _citrus_BIG5_stdenc_get_state_desc_generic(_BIG5EncodingInfo * __restrict ei,
    545 					   _BIG5State * __restrict psenc,
    546 					   int * __restrict rstate)
    547 {
    548 
    549 	if (psenc->chlen == 0)
    550 		*rstate = _STDENC_SDGEN_INITIAL;
    551 	else
    552 		*rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
    553 
    554 	return 0;
    555 }
    556 
    557 /* ----------------------------------------------------------------------
    558  * public interface for ctype
    559  */
    560 
    561 _CITRUS_CTYPE_DECLS(BIG5);
    562 _CITRUS_CTYPE_DEF_OPS(BIG5);
    563 
    564 #include "citrus_ctype_template.h"
    565 
    566 
    567 /* ----------------------------------------------------------------------
    568  * public interface for stdenc
    569  */
    570 
    571 _CITRUS_STDENC_DECLS(BIG5);
    572 _CITRUS_STDENC_DEF_OPS(BIG5);
    573 
    574 #include "citrus_stdenc_template.h"
    575