xref: /netbsd-src/lib/libc/citrus/modules/citrus_utf8.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: citrus_utf8.c,v 1.15 2006/03/22 00:08:09 christos 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.15 2006/03/22 00:08:09 christos 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 <locale.h>
73 #include <wchar.h>
74 #include <sys/types.h>
75 #include <limits.h>
76 
77 #include "citrus_namespace.h"
78 #include "citrus_types.h"
79 #include "citrus_module.h"
80 #include "citrus_ctype.h"
81 #include "citrus_stdenc.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 const 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 
106 typedef struct {
107 	_UTF8EncodingInfo	ei;
108 	struct {
109 		/* for future multi-locale facility */
110 		_UTF8State	s_mblen;
111 		_UTF8State	s_mbrlen;
112 		_UTF8State	s_mbrtowc;
113 		_UTF8State	s_mbtowc;
114 		_UTF8State	s_mbsrtowcs;
115 		_UTF8State	s_wcrtomb;
116 		_UTF8State	s_wcsrtombs;
117 		_UTF8State	s_wctomb;
118 	} states;
119 } _UTF8CTypeInfo;
120 
121 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
122 #define _CEI_TO_STATE(_ei_, _func_)	(_ei_)->states.s_##_func_
123 
124 #define _FUNCNAME(m)			_citrus_UTF8_##m
125 #define _ENCODING_INFO			_UTF8EncodingInfo
126 #define _CTYPE_INFO			_UTF8CTypeInfo
127 #define _ENCODING_STATE			_UTF8State
128 #define _ENCODING_MB_CUR_MAX(_ei_)	6
129 #define _ENCODING_IS_STATE_DEPENDENT	0
130 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
131 
132 
133 static __inline void
134 _UTF8_init_count(void)
135 {
136 	int i;
137 	if (!_UTF8_count) {
138 		memset(_UTF8_count_array, 0, sizeof(_UTF8_count_array));
139 		for (i = 0; i <= 0x7f; i++)
140 			_UTF8_count_array[i] = 1;
141 		for (i = 0xc0; i <= 0xdf; i++)
142 			_UTF8_count_array[i] = 2;
143 		for (i = 0xe0; i <= 0xef; i++)
144 			_UTF8_count_array[i] = 3;
145 		for (i = 0xf0; i <= 0xf7; i++)
146 			_UTF8_count_array[i] = 4;
147 		for (i = 0xf8; i <= 0xfb; i++)
148 			_UTF8_count_array[i] = 5;
149 		for (i = 0xfc; i <= 0xfd; i++)
150 			_UTF8_count_array[i] = 6;
151 		_UTF8_count = _UTF8_count_array;
152 	}
153 }
154 
155 static int
156 _UTF8_findlen(wchar_t v)
157 {
158 	int i;
159 	u_int32_t c;
160 
161 	c = (u_int32_t)v;	/*XXX*/
162 	for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]) - 1; i++)
163 		if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1])
164 			return i;
165 
166 	return -1;	/*out of range*/
167 }
168 
169 static __inline void
170 /*ARGSUSED*/
171 _citrus_UTF8_init_state(_UTF8EncodingInfo *ei, _UTF8State *s)
172 {
173 	memset(s, 0, sizeof(*s));
174 }
175 
176 static __inline void
177 /*ARGSUSED*/
178 _citrus_UTF8_pack_state(_UTF8EncodingInfo *ei, void *pspriv,
179 			const _UTF8State *s)
180 {
181 	memcpy(pspriv, (const void *)s, sizeof(*s));
182 }
183 
184 static __inline void
185 /*ARGSUSED*/
186 _citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei, _UTF8State *s,
187 			  const void *pspriv)
188 {
189 	memcpy((void *)s, pspriv, sizeof(*s));
190 }
191 
192 static int
193 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
194 			  size_t n, _UTF8State *psenc, size_t *nresult)
195 {
196 	wchar_t wchar;
197 	const char *s0;
198 	int c;
199 	int i;
200 	int chlenbak;
201 
202 	_DIAGASSERT(nresult != 0);
203 	_DIAGASSERT(s != NULL);
204 	_DIAGASSERT(psenc != NULL);
205 
206 	s0 = *s;
207 
208 	if (s0 == NULL) {
209 		_citrus_UTF8_init_state(ei, psenc);
210 		*nresult = 0; /* state independent */
211 		return (0);
212 	}
213 
214 	chlenbak = psenc->chlen;
215 
216 	/* make sure we have the first byte in the buffer */
217 	switch (psenc->chlen) {
218 	case 0:
219 		if (n < 1) {
220 			goto restart;
221 		}
222 		psenc->ch[0] = *s0++;
223 		psenc->chlen = 1;
224 		n--;
225 		break;
226 	case 1: case 2: case 3: case 4: case 5:
227 		break;
228 	default:
229 		/* illegal state */
230 		goto ilseq;
231 	}
232 
233 	c = _UTF8_count[psenc->ch[0] & 0xff];
234 	if (c == 0)
235 		goto ilseq;
236 	while (psenc->chlen < c) {
237 		if (n < 1) {
238 			goto restart;
239 		}
240 		psenc->ch[psenc->chlen] = *s0++;
241 		psenc->chlen++;
242 		n--;
243 	}
244 
245 	switch (c) {
246 	case 1:
247 		wchar = psenc->ch[0] & 0xff;
248 		break;
249 	case 2: case 3: case 4: case 5: case 6:
250 		wchar = psenc->ch[0] & (0x7f >> c);
251 		for (i = 1; i < c; i++) {
252 			if ((psenc->ch[i] & 0xc0) != 0x80)
253 				goto ilseq;
254 			wchar <<= 6;
255 			wchar |= (psenc->ch[i] & 0x3f);
256 		}
257 
258 		_DIAGASSERT(findlen(wchar) == c);
259 
260 		break;
261 	default:
262 		/* illegal state */
263 		goto ilseq;
264 	}
265 
266 	*s = s0;
267 
268 	psenc->chlen = 0;
269 
270 	if (pwc)
271 		*pwc = wchar;
272 
273 	if (!wchar)
274 		*nresult = 0;
275 	else
276 		*nresult = c - chlenbak;
277 
278 	return (0);
279 
280 ilseq:
281 	psenc->chlen = 0;
282 	*nresult = (size_t)-1;
283 	return (EILSEQ);
284 
285 restart:
286 	*s = s0;
287 	*nresult = (size_t)-2;
288 	return (0);
289 }
290 
291 static int
292 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei, char *s, size_t n, wchar_t wc,
293 			  _UTF8State *psenc, size_t *nresult)
294 {
295 	int cnt, i, ret;
296 	wchar_t c;
297 
298 	_DIAGASSERT(nresult != 0);
299 	_DIAGASSERT(s != NULL);
300 
301 	cnt = _UTF8_findlen(wc);
302 	if (cnt <= 0 || cnt > 6) {
303 		/* invalid UCS4 value */
304 		ret = EILSEQ;
305 		goto err;
306 	}
307 	if (n < cnt) {
308 		/* bound check failure */
309 		ret = E2BIG;
310 		goto err;
311 	}
312 
313 	c = wc;
314 	if (s) {
315 		for (i = cnt - 1; i > 0; i--) {
316 			s[i] = 0x80 | (c & 0x3f);
317 			c >>= 6;
318 		}
319 		s[0] = c;
320 		if (cnt == 1)
321 			s[0] &= 0x7f;
322 		else {
323 			s[0] &= (0x7f >> cnt);
324 			s[0] |= ((0xff00 >> cnt) & 0xff);
325 		}
326 	}
327 
328 	*nresult = (size_t)cnt;
329 	return 0;
330 
331 err:
332 	*nresult = (size_t)-1;
333 	return ret;
334 }
335 
336 static __inline int
337 /*ARGSUSED*/
338 _citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei,
339 			   _csid_t * __restrict csid,
340 			   _index_t * __restrict idx,
341 			   wchar_t wc)
342 {
343 
344 	_DIAGASSERT(csid != NULL && idx != NULL);
345 
346 	*csid = 0;
347 	*idx = (_citrus_index_t)wc;
348 
349 	return (0);
350 }
351 
352 static __inline int
353 /*ARGSUSED*/
354 _citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei,
355 			   wchar_t * __restrict wc,
356 			   _csid_t csid, _index_t idx)
357 {
358 
359 	_DIAGASSERT(wc != NULL);
360 
361 	if (csid != 0)
362 		return (EILSEQ);
363 
364 	*wc = (wchar_t)idx;
365 
366 	return (0);
367 }
368 
369 static __inline int
370 /*ARGSUSED*/
371 _citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo * __restrict ei,
372 					   _UTF8State * __restrict psenc,
373 					   int * __restrict rstate)
374 {
375 
376 	if (psenc->chlen == 0)
377 		*rstate = _STDENC_SDGEN_INITIAL;
378 	else
379 		*rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
380 
381 	return 0;
382 }
383 
384 static int
385 /*ARGSUSED*/
386 _citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei,
387 				  const void * __restrict var, size_t lenvar)
388 {
389 	_UTF8_init_count();
390 
391 	return 0;
392 }
393 
394 static void
395 /*ARGSUSED*/
396 _citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei)
397 {
398 }
399 
400 
401 /* ----------------------------------------------------------------------
402  * public interface for ctype
403  */
404 
405 _CITRUS_CTYPE_DECLS(UTF8);
406 _CITRUS_CTYPE_DEF_OPS(UTF8);
407 
408 #include "citrus_ctype_template.h"
409 
410 /* ----------------------------------------------------------------------
411  * public interface for stdenc
412  */
413 
414 _CITRUS_STDENC_DECLS(UTF8);
415 _CITRUS_STDENC_DEF_OPS(UTF8);
416 
417 #include "citrus_stdenc_template.h"
418