xref: /netbsd/lib/libc/citrus/modules/citrus_utf8.c (revision bf9ec67e)
1 /*	$NetBSD: citrus_utf8.c,v 1.6 2002/03/28 10:53:49 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.6 2002/03/28 10:53:49 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 _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
121 #define _CEI_TO_STATE(_ei_, _func_)	(_ei_)->states.s_##_func_
122 
123 #define _FUNCNAME(m)			_citrus_UTF8_##m
124 #define _ENCODING_INFO			_UTF8EncodingInfo
125 #define _CTYPE_INFO			_UTF8CTypeInfo
126 #define _ENCODING_STATE			_UTF8State
127 #define _ENCODING_MB_CUR_MAX(_ei_)	6
128 #define _ENCODING_IS_STATE_DEPENDENT	0
129 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
130 
131 
132 static __inline void
133 _UTF8_init_count(void)
134 {
135 	int i;
136 	if (!_UTF8_count) {
137 		memset(_UTF8_count_array, 0, sizeof(_UTF8_count_array));
138 		for (i = 0; i <= 0x7f; i++)
139 			_UTF8_count_array[i] = 1;
140 		for (i = 0xc0; i <= 0xdf; i++)
141 			_UTF8_count_array[i] = 2;
142 		for (i = 0xe0; i <= 0xef; i++)
143 			_UTF8_count_array[i] = 3;
144 		for (i = 0xf0; i <= 0xf7; i++)
145 			_UTF8_count_array[i] = 4;
146 		for (i = 0xf8; i <= 0xfb; i++)
147 			_UTF8_count_array[i] = 5;
148 		for (i = 0xfc; i <= 0xfd; i++)
149 			_UTF8_count_array[i] = 6;
150 		_UTF8_count = _UTF8_count_array;
151 	}
152 }
153 
154 static int
155 _UTF8_findlen(wchar_t v)
156 {
157 	int i;
158 	u_int32_t c;
159 
160 	c = (u_int32_t)v;	/*XXX*/
161 	for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]); i++)
162 		if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1])
163 			return i;
164 
165 	return -1;	/*out of range*/
166 }
167 
168 static __inline void
169 /*ARGSUSED*/
170 _citrus_UTF8_init_state(_UTF8EncodingInfo *ei, _UTF8State *s)
171 {
172 	memset(s, 0, sizeof(*s));
173 }
174 
175 static __inline void
176 /*ARGSUSED*/
177 _citrus_UTF8_pack_state(_UTF8EncodingInfo *ei, void *pspriv,
178 			const _UTF8State *s)
179 {
180 	memcpy(pspriv, (const void *)s, sizeof(*s));
181 }
182 
183 static __inline void
184 /*ARGSUSED*/
185 _citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei, _UTF8State *s,
186 			  const void *pspriv)
187 {
188 	memcpy((void *)s, pspriv, sizeof(*s));
189 }
190 
191 static int
192 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
193 			  size_t n, _UTF8State *psenc, size_t *nresult)
194 {
195 	wchar_t wchar;
196 	const char *s0;
197 	int c;
198 	int i;
199 	int chlenbak;
200 
201 	_DIAGASSERT(nresult != 0);
202 	_DIAGASSERT(ei != NULL);
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 	}
262 
263 	*s = s0;
264 
265 	psenc->chlen = 0;
266 
267 	if (pwc)
268 		*pwc = wchar;
269 
270 	if (!wchar)
271 		*nresult = 0;
272 	else
273 		*nresult = c - chlenbak;
274 
275 	return (0);
276 
277 ilseq:
278 	psenc->chlen = 0;
279 	return (EILSEQ);
280 
281 restart:
282 	*nresult = (size_t)-2;
283 	*s = s0;
284 	return (0);
285 }
286 
287 static int
288 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei, char *s, size_t n, wchar_t wc,
289 			  _UTF8State *psenc, size_t *nresult)
290 {
291 	int cnt, i;
292 	wchar_t c;
293 
294 	_DIAGASSERT(ei != NULL);
295 	_DIAGASSERT(nresult != 0);
296 	_DIAGASSERT(s != NULL);
297 
298 	cnt = _UTF8_findlen(wc);
299 	if (cnt <= 0 || cnt > 6) {
300 		/* invalid UCS4 value */
301 		goto ilseq;
302 	}
303 	if (n < cnt) {
304 		/* bound check failure */
305 		goto ilseq;
306 	}
307 
308 	c = wc;
309 	if (s) {
310 		for (i = cnt - 1; i > 0; i--) {
311 			s[i] = 0x80 | (c & 0x3f);
312 			c >>= 6;
313 		}
314 		s[0] = c;
315 		if (cnt == 1)
316 			s[0] &= 0x7f;
317 		else {
318 			s[0] &= (0x7f >> cnt);
319 			s[0] |= ((0xff00 >> cnt) & 0xff);
320 		}
321 	}
322 
323 	*nresult = (size_t)cnt;
324 	return (0);
325 
326 ilseq:
327 	*nresult = (size_t)-1;
328 	return (EILSEQ);
329 }
330 
331 
332 static int
333 /*ARGSUSED*/
334 _citrus_UTF8_stdencoding_init(_UTF8EncodingInfo * __restrict ei,
335 			      const void * __restrict var, size_t lenvar)
336 {
337 	_DIAGASSERT(ei != NULL);
338 
339 	_UTF8_init_count();
340 	memset((void *)ei, 0, sizeof(*ei));
341 
342 	return (0);
343 }
344 
345 static void
346 /*ARGSUSED*/
347 _citrus_UTF8_stdencoding_uninit(_UTF8EncodingInfo *ei)
348 {
349 }
350 
351 
352 /* ----------------------------------------------------------------------
353  * public interface for ctype
354  */
355 
356 _CITRUS_CTYPE_DECLS(UTF8);
357 _CITRUS_CTYPE_DEF_OPS(UTF8);
358 
359 #include "citrus_ctype_template.h"
360