Home | History | Annotate | Line # | Download | only in modules
      1 /*	$NetBSD: citrus_utf8.c,v 1.18 2013/05/28 16:57:56 joerg 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_utf8.c,v 1.18 2013/05/28 16:57:56 joerg 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_utf8.h"
     82 
     83 
     84 /* ----------------------------------------------------------------------
     85  * private stuffs used by templates
     86  */
     87 
     88 static int _UTF8_count_array[256];
     89 static int const *_UTF8_count = NULL;
     90 
     91 static const u_int32_t _UTF8_range[] = {
     92 	0,	/*dummy*/
     93 	0x00000000, 0x00000080, 0x00000800, 0x00010000,
     94 	0x00200000, 0x04000000, 0x80000000,
     95 };
     96 
     97 typedef struct {
     98 	char ch[6];
     99 	int chlen;
    100 } _UTF8State;
    101 
    102 typedef struct {
    103 } _UTF8EncodingInfo;
    104 
    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_mbsnrtowcs;
    115 		_UTF8State	s_wcrtomb;
    116 		_UTF8State	s_wcsrtombs;
    117 		_UTF8State	s_wcsnrtombs;
    118 		_UTF8State	s_wctomb;
    119 	} states;
    120 } _UTF8CTypeInfo;
    121 
    122 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
    123 #define _CEI_TO_STATE(_ei_, _func_)	(_ei_)->states.s_##_func_
    124 
    125 #define _FUNCNAME(m)			_citrus_UTF8_##m
    126 #define _ENCODING_INFO			_UTF8EncodingInfo
    127 #define _CTYPE_INFO			_UTF8CTypeInfo
    128 #define _ENCODING_STATE			_UTF8State
    129 #define _ENCODING_MB_CUR_MAX(_ei_)	6
    130 #define _ENCODING_IS_STATE_DEPENDENT	0
    131 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	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]) - 1; 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 int
    171 _UTF8_surrogate(wchar_t wc)
    172 {
    173 	return wc >= 0xd800 && wc <= 0xdfff;
    174 }
    175 
    176 static __inline void
    177 /*ARGSUSED*/
    178 _citrus_UTF8_init_state(_UTF8EncodingInfo *ei, _UTF8State *s)
    179 {
    180 	s->chlen = 0;
    181 }
    182 
    183 static __inline void
    184 /*ARGSUSED*/
    185 _citrus_UTF8_pack_state(_UTF8EncodingInfo *ei, void *pspriv,
    186 			const _UTF8State *s)
    187 {
    188 	memcpy(pspriv, (const void *)s, sizeof(*s));
    189 }
    190 
    191 static __inline void
    192 /*ARGSUSED*/
    193 _citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei, _UTF8State *s,
    194 			  const void *pspriv)
    195 {
    196 	memcpy((void *)s, pspriv, sizeof(*s));
    197 }
    198 
    199 static int
    200 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
    201 			  size_t n, _UTF8State *psenc, size_t *nresult)
    202 {
    203 	wchar_t wchar;
    204 	const char *s0;
    205 	int c;
    206 	int i;
    207 
    208 	_DIAGASSERT(nresult != 0);
    209 	_DIAGASSERT(s != NULL);
    210 	_DIAGASSERT(psenc != NULL);
    211 
    212 	s0 = *s;
    213 
    214 	if (s0 == NULL) {
    215 		_citrus_UTF8_init_state(ei, psenc);
    216 		*nresult = 0; /* state independent */
    217 		return 0;
    218 	}
    219 
    220 	/* make sure we have the first byte in the buffer */
    221 	if (psenc->chlen == 0) {
    222 		if (n-- < 1)
    223 			goto restart;
    224 		psenc->ch[psenc->chlen++] = *s0++;
    225 	}
    226 
    227 	c = _UTF8_count[psenc->ch[0] & 0xff];
    228 	if (c < 1 || c < psenc->chlen)
    229 		goto ilseq;
    230 
    231 	if (c == 1)
    232 		wchar = psenc->ch[0] & 0xff;
    233 	else {
    234 		while (psenc->chlen < c) {
    235 			if (n-- < 1)
    236 				goto restart;
    237 			psenc->ch[psenc->chlen++] = *s0++;
    238 		}
    239 		wchar = psenc->ch[0] & (0x7f >> c);
    240 		for (i = 1; i < c; i++) {
    241 			if ((psenc->ch[i] & 0xc0) != 0x80)
    242 				goto ilseq;
    243 			wchar <<= 6;
    244 			wchar |= (psenc->ch[i] & 0x3f);
    245 		}
    246 		if (_UTF8_surrogate(wchar) || _UTF8_findlen(wchar) != c)
    247 			goto ilseq;
    248 	}
    249 	if (pwc != NULL)
    250 		*pwc = wchar;
    251 	*nresult = (wchar == 0) ? 0 : s0 - *s;
    252 	*s = s0;
    253 	psenc->chlen = 0;
    254 
    255 	return 0;
    256 
    257 ilseq:
    258 	*nresult = (size_t)-1;
    259 	return EILSEQ;
    260 
    261 restart:
    262 	*s = s0;
    263 	*nresult = (size_t)-2;
    264 	return 0;
    265 }
    266 
    267 static int
    268 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei, char *s, size_t n, wchar_t wc,
    269 			  _UTF8State *psenc, size_t *nresult)
    270 {
    271 	int cnt, i, ret;
    272 	wchar_t c;
    273 
    274 	_DIAGASSERT(nresult != 0);
    275 	_DIAGASSERT(s != NULL);
    276 
    277 	if (_UTF8_surrogate(wc)) {
    278 		ret = EILSEQ;
    279 		goto err;
    280 	}
    281 	cnt = _UTF8_findlen(wc);
    282 	if (cnt <= 0 || cnt > 6) {
    283 		/* invalid UCS4 value */
    284 		ret = EILSEQ;
    285 		goto err;
    286 	}
    287 	if (n < cnt) {
    288 		/* bound check failure */
    289 		ret = E2BIG;
    290 		goto err;
    291 	}
    292 
    293 	c = wc;
    294 	if (s) {
    295 		for (i = cnt - 1; i > 0; i--) {
    296 			s[i] = 0x80 | (c & 0x3f);
    297 			c >>= 6;
    298 		}
    299 		s[0] = c;
    300 		if (cnt == 1)
    301 			s[0] &= 0x7f;
    302 		else {
    303 			s[0] &= (0x7f >> cnt);
    304 			s[0] |= ((0xff00 >> cnt) & 0xff);
    305 		}
    306 	}
    307 
    308 	*nresult = (size_t)cnt;
    309 	return 0;
    310 
    311 err:
    312 	*nresult = (size_t)-1;
    313 	return ret;
    314 }
    315 
    316 static __inline int
    317 /*ARGSUSED*/
    318 _citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei,
    319 			   _csid_t * __restrict csid,
    320 			   _index_t * __restrict idx,
    321 			   wchar_t wc)
    322 {
    323 
    324 	_DIAGASSERT(csid != NULL && idx != NULL);
    325 
    326 	*csid = 0;
    327 	*idx = (_citrus_index_t)wc;
    328 
    329 	return (0);
    330 }
    331 
    332 static __inline int
    333 /*ARGSUSED*/
    334 _citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei,
    335 			   wchar_t * __restrict wc,
    336 			   _csid_t csid, _index_t idx)
    337 {
    338 
    339 	_DIAGASSERT(wc != NULL);
    340 
    341 	if (csid != 0)
    342 		return (EILSEQ);
    343 
    344 	*wc = (wchar_t)idx;
    345 
    346 	return (0);
    347 }
    348 
    349 static __inline int
    350 /*ARGSUSED*/
    351 _citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo * __restrict ei,
    352 					   _UTF8State * __restrict psenc,
    353 					   int * __restrict rstate)
    354 {
    355 
    356 	if (psenc->chlen == 0)
    357 		*rstate = _STDENC_SDGEN_INITIAL;
    358 	else
    359 		*rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
    360 
    361 	return 0;
    362 }
    363 
    364 static int
    365 /*ARGSUSED*/
    366 _citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei,
    367 				  const void * __restrict var, size_t lenvar)
    368 {
    369 	_UTF8_init_count();
    370 
    371 	return 0;
    372 }
    373 
    374 static void
    375 /*ARGSUSED*/
    376 _citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei)
    377 {
    378 }
    379 
    380 
    381 /* ----------------------------------------------------------------------
    382  * public interface for ctype
    383  */
    384 
    385 _CITRUS_CTYPE_DECLS(UTF8);
    386 _CITRUS_CTYPE_DEF_OPS(UTF8);
    387 
    388 #include "citrus_ctype_template.h"
    389 
    390 /* ----------------------------------------------------------------------
    391  * public interface for stdenc
    392  */
    393 
    394 _CITRUS_STDENC_DECLS(UTF8);
    395 _CITRUS_STDENC_DEF_OPS(UTF8);
    396 
    397 #include "citrus_stdenc_template.h"
    398