Home | History | Annotate | Line # | Download | only in modules
citrus_euc.c revision 1.12
      1 /*	$NetBSD: citrus_euc.c,v 1.12 2008/06/14 16:01:07 tnozaki Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)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 
     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_euc.c,v 1.12 2008/06/14 16:01:07 tnozaki Exp $");
     64 #endif /* LIBC_SCCS and not lint */
     65 
     66 #include <assert.h>
     67 #include <errno.h>
     68 #include <string.h>
     69 #include <stdio.h>
     70 #include <stdlib.h>
     71 #include <stddef.h>
     72 #include <wchar.h>
     73 #include <sys/types.h>
     74 #include <limits.h>
     75 
     76 #include "citrus_namespace.h"
     77 #include "citrus_types.h"
     78 #include "citrus_module.h"
     79 #include "citrus_ctype.h"
     80 #include "citrus_stdenc.h"
     81 #include "citrus_euc.h"
     82 
     83 
     84 /* ----------------------------------------------------------------------
     85  * private stuffs used by templates
     86  */
     87 
     88 typedef struct {
     89 	char ch[3];
     90 	int chlen;
     91 } _EUCState;
     92 
     93 typedef struct {
     94 	unsigned	count[4];
     95 	wchar_t		bits[4];
     96 	wchar_t		mask;
     97 	unsigned	mb_cur_max;
     98 } _EUCEncodingInfo;
     99 
    100 typedef struct {
    101 	_EUCEncodingInfo	ei;
    102 	struct {
    103 		/* for future multi-locale facility */
    104 		_EUCState	s_mblen;
    105 		_EUCState	s_mbrlen;
    106 		_EUCState	s_mbrtowc;
    107 		_EUCState	s_mbtowc;
    108 		_EUCState	s_mbsrtowcs;
    109 		_EUCState	s_wcrtomb;
    110 		_EUCState	s_wcsrtombs;
    111 		_EUCState	s_wctomb;
    112 	} states;
    113 } _EUCCTypeInfo;
    114 
    115 #define	_SS2	0x008e
    116 #define	_SS3	0x008f
    117 
    118 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
    119 #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
    120 
    121 #define _FUNCNAME(m)			_citrus_EUC_##m
    122 #define _ENCODING_INFO			_EUCEncodingInfo
    123 #define _CTYPE_INFO			_EUCCTypeInfo
    124 #define _ENCODING_STATE			_EUCState
    125 #define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
    126 #define _ENCODING_IS_STATE_DEPENDENT	0
    127 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
    128 
    129 
    130 static __inline int
    131 _citrus_EUC_cs(unsigned int c)
    132 {
    133 	c &= 0xff;
    134 
    135 	return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
    136 }
    137 
    138 static __inline int
    139 _citrus_EUC_parse_variable(_EUCEncodingInfo *ei,
    140 			   const void *var, size_t lenvar)
    141 {
    142 	const char *v, *e;
    143 	int x;
    144 
    145 	/* parse variable string */
    146 	if (!var)
    147 		return (EFTYPE);
    148 
    149 	v = (const char *) var;
    150 
    151 	while (*v == ' ' || *v == '\t')
    152 		++v;
    153 
    154 	ei->mb_cur_max = 1;
    155 	for (x = 0; x < 4; ++x) {
    156 		ei->count[x] = (int) strtol(v, (char **)&e, 0);
    157 		if (v == e || !(v = e) || ei->count[x]<1 || ei->count[x]>4) {
    158 			return (EFTYPE);
    159 		}
    160 		if (ei->mb_cur_max < ei->count[x])
    161 			ei->mb_cur_max = ei->count[x];
    162 		while (*v == ' ' || *v == '\t')
    163 			++v;
    164 		ei->bits[x] = (int) strtol(v, (char **)&e, 0);
    165 		if (v == e || !(v = e)) {
    166 			return (EFTYPE);
    167 		}
    168 		while (*v == ' ' || *v == '\t')
    169 			++v;
    170 	}
    171 	ei->mask = (int)strtol(v, (char **)&e, 0);
    172 	if (v == e || !(v = e)) {
    173 		return (EFTYPE);
    174 	}
    175 
    176 	return 0;
    177 }
    178 
    179 
    180 static __inline void
    181 /*ARGSUSED*/
    182 _citrus_EUC_init_state(_EUCEncodingInfo *ei, _EUCState *s)
    183 {
    184 	memset(s, 0, sizeof(*s));
    185 }
    186 
    187 static __inline void
    188 /*ARGSUSED*/
    189 _citrus_EUC_pack_state(_EUCEncodingInfo *ei, void *pspriv, const _EUCState *s)
    190 {
    191 	memcpy(pspriv, (const void *)s, sizeof(*s));
    192 }
    193 
    194 static __inline void
    195 /*ARGSUSED*/
    196 _citrus_EUC_unpack_state(_EUCEncodingInfo *ei, _EUCState *s,
    197 			 const void *pspriv)
    198 {
    199 	memcpy((void *)s, pspriv, sizeof(*s));
    200 }
    201 
    202 static int
    203 _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, const char **s,
    204 			 size_t n, _EUCState *psenc, size_t *nresult)
    205 {
    206 	wchar_t wchar;
    207 	int c, cs, len;
    208 	int chlenbak;
    209 	const char *s0, *s1 = NULL;
    210 
    211 	_DIAGASSERT(nresult != 0);
    212 	_DIAGASSERT(ei != NULL);
    213 	_DIAGASSERT(psenc != NULL);
    214 	_DIAGASSERT(s != NULL);
    215 
    216 	s0 = *s;
    217 
    218 	if (s0 == NULL) {
    219 		_citrus_EUC_init_state(ei, psenc);
    220 		*nresult = 0; /* state independent */
    221 		return (0);
    222 	}
    223 
    224 	chlenbak = psenc->chlen;
    225 
    226 	/* make sure we have the first byte in the buffer */
    227 	switch (psenc->chlen) {
    228 	case 0:
    229 		if (n < 1)
    230 			goto restart;
    231 		psenc->ch[0] = *s0++;
    232 		psenc->chlen = 1;
    233 		n--;
    234 		break;
    235 	case 1:
    236 	case 2:
    237 		break;
    238 	default:
    239 		/* illgeal state */
    240 		goto encoding_error;
    241 	}
    242 
    243 	c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)];
    244 	if (c == 0)
    245 		goto encoding_error;
    246 	while (psenc->chlen < c) {
    247 		if (n < 1)
    248 			goto restart;
    249 		psenc->ch[psenc->chlen] = *s0++;
    250 		psenc->chlen++;
    251 		n--;
    252 	}
    253 	*s = s0;
    254 
    255 	switch (cs) {
    256 	case 3:
    257 	case 2:
    258 		/* skip SS2/SS3 */
    259 		len = c - 1;
    260 		s1 = &psenc->ch[1];
    261 		break;
    262 	case 1:
    263 	case 0:
    264 		len = c;
    265 		s1 = &psenc->ch[0];
    266 		break;
    267 	default:
    268 		goto encoding_error;
    269 	}
    270 	wchar = 0;
    271 	while (len-- > 0)
    272 		wchar = (wchar << 8) | (*s1++ & 0xff);
    273 	wchar = (wchar & ~ei->mask) | ei->bits[cs];
    274 
    275 	psenc->chlen = 0;
    276 	if (pwc)
    277 		*pwc = wchar;
    278 
    279 	if (!wchar) {
    280 		*nresult = 0;
    281 	} else {
    282 		*nresult = (size_t)(c - chlenbak);
    283 	}
    284 
    285 	return 0;
    286 
    287 encoding_error:
    288 	psenc->chlen = 0;
    289 	*nresult = (size_t)-1;
    290 	return (EILSEQ);
    291 
    292 restart:
    293 	*nresult = (size_t)-2;
    294 	*s = s0;
    295 	return (0);
    296 }
    297 
    298 static int
    299 _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc,
    300 			 _EUCState *psenc, size_t *nresult)
    301 {
    302 	wchar_t m, nm;
    303 	int cs, i, ret;
    304 
    305 	_DIAGASSERT(ei != NULL);
    306 	_DIAGASSERT(nresult != 0);
    307 	_DIAGASSERT(s != NULL);
    308 
    309 	m = wc & ei->mask;
    310 	nm = wc & ~m;
    311 
    312 	for (cs = 0;
    313 	     cs < sizeof(ei->count)/sizeof(ei->count[0]);
    314 	     cs++) {
    315 		if (m == ei->bits[cs])
    316 			break;
    317 	}
    318 	/* fallback case - not sure if it is necessary */
    319 	if (cs == sizeof(ei->count)/sizeof(ei->count[0]))
    320 		cs = 1;
    321 
    322 	i = ei->count[cs];
    323 	if (n < i) {
    324 		ret = E2BIG;
    325 		goto err;
    326 	}
    327 	m = (cs) ? 0x80 : 0x00;
    328 	switch (cs) {
    329 	case 2:
    330 		*s++ = _SS2;
    331 		i--;
    332 		break;
    333 	case 3:
    334 		*s++ = _SS3;
    335 		i--;
    336 		break;
    337 	}
    338 
    339 	while (i-- > 0)
    340 		*s++ = ((nm >> (i << 3)) & 0xff) | m;
    341 
    342 	*nresult = (size_t)ei->count[cs];
    343 	return 0;
    344 
    345 err:
    346 	*nresult = (size_t)-1;
    347 	return ret;
    348 }
    349 
    350 static __inline int
    351 /*ARGSUSED*/
    352 _citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,
    353 			  _csid_t * __restrict csid,
    354 			  _index_t * __restrict idx, wchar_t wc)
    355 {
    356 	wchar_t m, nm;
    357 
    358 	_DIAGASSERT(ei != NULL && csid != NULL && idx != NULL);
    359 
    360 	m = wc & ei->mask;
    361 	nm = wc & ~m;
    362 
    363 	*csid = (_citrus_csid_t)m;
    364 	*idx  = (_citrus_index_t)nm;
    365 
    366 	return (0);
    367 }
    368 
    369 static __inline int
    370 /*ARGSUSED*/
    371 _citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,
    372 			  wchar_t * __restrict wc,
    373 			  _csid_t csid, _index_t idx)
    374 {
    375 
    376 	_DIAGASSERT(ei != NULL && wc != NULL);
    377 
    378 	if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0)
    379 		return (EINVAL);
    380 
    381 	*wc = (wchar_t)csid | (wchar_t)idx;
    382 
    383 	return (0);
    384 }
    385 
    386 static __inline int
    387 /*ARGSUSED*/
    388 _citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei,
    389 					  _EUCState * __restrict psenc,
    390 					  int * __restrict rstate)
    391 {
    392 
    393 	if (psenc->chlen == 0)
    394 		*rstate = _STDENC_SDGEN_INITIAL;
    395 	else
    396 		*rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
    397 
    398 	return 0;
    399 }
    400 
    401 static int
    402 /*ARGSUSED*/
    403 _citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,
    404 				 const void * __restrict var, size_t lenvar)
    405 {
    406 
    407 	_DIAGASSERT(ei != NULL);
    408 
    409 	return (_citrus_EUC_parse_variable(ei, var, lenvar));
    410 }
    411 
    412 static void
    413 /*ARGSUSED*/
    414 _citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei)
    415 {
    416 }
    417 
    418 /* ----------------------------------------------------------------------
    419  * public interface for ctype
    420  */
    421 
    422 _CITRUS_CTYPE_DECLS(EUC);
    423 _CITRUS_CTYPE_DEF_OPS(EUC);
    424 
    425 #include "citrus_ctype_template.h"
    426 
    427 /* ----------------------------------------------------------------------
    428  * public interface for stdenc
    429  */
    430 
    431 _CITRUS_STDENC_DECLS(EUC);
    432 _CITRUS_STDENC_DEF_OPS(EUC);
    433 
    434 #include "citrus_stdenc_template.h"
    435