Home | History | Annotate | Line # | Download | only in modules
citrus_utf8.c revision 1.4
      1 /*	$NetBSD: citrus_utf8.c,v 1.4 2002/03/27 17:54:42 yamt 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. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 #include <sys/cdefs.h>
     66 #if defined(LIBC_SCCS) && !defined(lint)
     67 __RCSID("$NetBSD: citrus_utf8.c,v 1.4 2002/03/27 17:54:42 yamt Exp $");
     68 #endif /* LIBC_SCCS and not lint */
     69 
     70 #include <assert.h>
     71 #include <errno.h>
     72 #include <string.h>
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <stddef.h>
     76 #include <locale.h>
     77 #include <wchar.h>
     78 #include <sys/types.h>
     79 #include <limits.h>
     80 #include "citrus_module.h"
     81 #include "citrus_ctype.h"
     82 #include "citrus_utf8.h"
     83 
     84 
     85 /* ----------------------------------------------------------------------
     86  * private stuffs used by templates
     87  */
     88 
     89 static int _UTF8_count_array[256];
     90 static int const *_UTF8_count = NULL;
     91 
     92 static u_int32_t _UTF8_range[] = {
     93 	0,	/*dummy*/
     94 	0x00000000, 0x00000080, 0x00000800, 0x00010000,
     95 	0x00200000, 0x04000000, 0x80000000,
     96 };
     97 
     98 typedef struct {
     99 	char ch[6];
    100 	int chlen;
    101 } _UTF8State;
    102 
    103 typedef struct {
    104 } _UTF8EncodingInfo;
    105 typedef struct {
    106 	_UTF8EncodingInfo	ei;
    107 	struct {
    108 		/* for future multi-locale facility */
    109 		_UTF8State	s_mblen;
    110 		_UTF8State	s_mbrlen;
    111 		_UTF8State	s_mbrtowc;
    112 		_UTF8State	s_mbtowc;
    113 		_UTF8State	s_mbsrtowcs;
    114 		_UTF8State	s_wcrtomb;
    115 		_UTF8State	s_wcsrtombs;
    116 		_UTF8State	s_wctomb;
    117 	} states;
    118 } _UTF8CTypeInfo;
    119 
    120 #define	_TO_EI(_cl_)			((_UTF8EncodingInfo *)(_cl_))
    121 #define	_TO_CEI(_cl_)			((_UTF8CTypeInfo *)(_cl_))
    122 #define _TO_STATE(_ps_)			((_UTF8State *)(_ps_))
    123 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
    124 #define _CEI_TO_STATE(_ei_, _func_)	(_ei_)->states.s_##_func_
    125 
    126 #define _FUNCNAME(m)			_citrus_UTF8_##m
    127 #define _ENCODING_INFO			_UTF8EncodingInfo
    128 #define _CTYPE_INFO			_UTF8CTypeInfo
    129 #define _ENCODING_STATE			_UTF8State
    130 #define _ENCODING_MB_CUR_MAX(_ei_)	6
    131 #define _ENCODING_IS_STATE_DEPENDENT	0
    132 
    133 
    134 static __inline void
    135 _UTF8_init_count(void)
    136 {
    137 	int i;
    138 	if (!_UTF8_count) {
    139 		memset(_UTF8_count_array, 0, sizeof(_UTF8_count_array));
    140 		for (i = 0; i <= 0x7f; i++)
    141 			_UTF8_count_array[i] = 1;
    142 		for (i = 0xc0; i <= 0xdf; i++)
    143 			_UTF8_count_array[i] = 2;
    144 		for (i = 0xe0; i <= 0xef; i++)
    145 			_UTF8_count_array[i] = 3;
    146 		for (i = 0xf0; i <= 0xf7; i++)
    147 			_UTF8_count_array[i] = 4;
    148 		for (i = 0xf8; i <= 0xfb; i++)
    149 			_UTF8_count_array[i] = 5;
    150 		for (i = 0xfc; i <= 0xfd; i++)
    151 			_UTF8_count_array[i] = 6;
    152 		_UTF8_count = _UTF8_count_array;
    153 	}
    154 }
    155 
    156 static int
    157 _UTF8_findlen(wchar_t v)
    158 {
    159 	int i;
    160 	u_int32_t c;
    161 
    162 	c = (u_int32_t)v;	/*XXX*/
    163 	for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]); i++)
    164 		if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1])
    165 			return i;
    166 
    167 	return -1;	/*out of range*/
    168 }
    169 
    170 static __inline void
    171 /*ARGSUSED*/
    172 _citrus_UTF8_init_state(_UTF8EncodingInfo *ei, _UTF8State *s)
    173 {
    174 	memset(s, 0, sizeof(*s));
    175 }
    176 
    177 static __inline void
    178 /*ARGSUSED*/
    179 _citrus_UTF8_pack_state(_UTF8EncodingInfo *ei, void *pspriv,
    180 			const _UTF8State *s)
    181 {
    182 	memcpy(pspriv, (const void *)s, sizeof(*s));
    183 }
    184 
    185 static __inline void
    186 /*ARGSUSED*/
    187 _citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei, _UTF8State *s,
    188 			  const void *pspriv)
    189 {
    190 	memcpy((void *)s, pspriv, sizeof(*s));
    191 }
    192 
    193 static int
    194 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
    195 			  size_t n, _UTF8State *psenc, size_t *nresult)
    196 {
    197 	wchar_t wchar;
    198 	const char *s0;
    199 	int c;
    200 	int i;
    201 	int chlenbak;
    202 
    203 	_DIAGASSERT(nresult != 0);
    204 	_DIAGASSERT(ei != NULL);
    205 	_DIAGASSERT(s != NULL);
    206 	_DIAGASSERT(psenc != NULL);
    207 
    208 	s0 = *s;
    209 
    210 	if (s0 == NULL) {
    211 		_citrus_UTF8_init_state(ei, psenc);
    212 		*nresult = 0; /* state independent */
    213 		return (0);
    214 	}
    215 
    216 	chlenbak = psenc->chlen;
    217 
    218 	/* make sure we have the first byte in the buffer */
    219 	switch (psenc->chlen) {
    220 	case 0:
    221 		if (n < 1) {
    222 			goto restart;
    223 		}
    224 		psenc->ch[0] = *s0++;
    225 		psenc->chlen = 1;
    226 		n--;
    227 		break;
    228 	case 1: case 2: case 3: case 4: case 5:
    229 		break;
    230 	default:
    231 		/* illegal state */
    232 		goto ilseq;
    233 	}
    234 
    235 	c = _UTF8_count[psenc->ch[0] & 0xff];
    236 	if (c == 0)
    237 		goto ilseq;
    238 	while (psenc->chlen < c) {
    239 		if (n < 1) {
    240 			goto restart;
    241 		}
    242 		psenc->ch[psenc->chlen] = *s0++;
    243 		psenc->chlen++;
    244 		n--;
    245 	}
    246 
    247 	switch (c) {
    248 	case 1:
    249 		wchar = psenc->ch[0] & 0xff;
    250 		break;
    251 	case 2: case 3: case 4: case 5: case 6:
    252 		wchar = psenc->ch[0] & (0x7f >> c);
    253 		for (i = 1; i < c; i++) {
    254 			if ((psenc->ch[i] & 0xc0) != 0x80)
    255 				goto ilseq;
    256 			wchar <<= 6;
    257 			wchar |= (psenc->ch[i] & 0x3f);
    258 		}
    259 
    260 		_DIAGASSERT(findlen(wchar) == c);
    261 
    262 		break;
    263 	}
    264 
    265 	*s = s0;
    266 
    267 	psenc->chlen = 0;
    268 
    269 	if (pwc)
    270 		*pwc = wchar;
    271 
    272 	if (!wchar)
    273 		*nresult = 0;
    274 	else
    275 		*nresult = c - chlenbak;
    276 
    277 	return (0);
    278 
    279 ilseq:
    280 	psenc->chlen = 0;
    281 	return (EILSEQ);
    282 
    283 restart:
    284 	*nresult = (size_t)-2;
    285 	*s = s0;
    286 	return (0);
    287 }
    288 
    289 static int
    290 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei, char *s, size_t n, wchar_t wc,
    291 			  _UTF8State *psenc, size_t *nresult)
    292 {
    293 	int cnt, i;
    294 	wchar_t c;
    295 
    296 	_DIAGASSERT(ei != NULL);
    297 	_DIAGASSERT(nresult != 0);
    298 	_DIAGASSERT(s != NULL);
    299 
    300 	cnt = _UTF8_findlen(wc);
    301 	if (cnt <= 0 || cnt > 6) {
    302 		/* invalid UCS4 value */
    303 		goto ilseq;
    304 	}
    305 	if (n < cnt) {
    306 		/* bound check failure */
    307 		goto ilseq;
    308 	}
    309 
    310 	c = wc;
    311 	if (s) {
    312 		for (i = cnt - 1; i > 0; i--) {
    313 			s[i] = 0x80 | (c & 0x3f);
    314 			c >>= 6;
    315 		}
    316 		s[0] = c;
    317 		if (cnt == 1)
    318 			s[0] &= 0x7f;
    319 		else {
    320 			s[0] &= (0x7f >> cnt);
    321 			s[0] |= ((0xff00 >> cnt) & 0xff);
    322 		}
    323 	}
    324 
    325 	*nresult = (size_t)cnt;
    326 	return (0);
    327 
    328 ilseq:
    329 	*nresult = (size_t)-1;
    330 	return (EILSEQ);
    331 }
    332 
    333 
    334 static int
    335 /*ARGSUSED*/
    336 _citrus_UTF8_stdencoding_init(_UTF8EncodingInfo * __restrict ei,
    337 			      const void * __restrict var, size_t lenvar)
    338 {
    339 	_DIAGASSERT(ei != NULL);
    340 
    341 	_UTF8_init_count();
    342 	memset((void *)ei, 0, sizeof(*ei));
    343 
    344 	return (0);
    345 }
    346 
    347 static void
    348 /*ARGSUSED*/
    349 _citrus_UTF8_stdencoding_uninit(_UTF8EncodingInfo *ei)
    350 {
    351 }
    352 
    353 
    354 /* ----------------------------------------------------------------------
    355  * public interface for ctype
    356  */
    357 
    358 _CITRUS_CTYPE_DECLS(UTF8);
    359 _CITRUS_CTYPE_DEF_OPS(UTF8);
    360 
    361 #include "citrus_ctype_template.h"
    362