xref: /freebsd/lib/libiconv_modules/UTF8/citrus_utf8.c (revision 4ec234c813eed05c166859bba82c882e40826eb9)
1 /* $FreeBSD$ */
2 /*	$NetBSD: citrus_utf8.c,v 1.17 2008/06/14 16:01:08 tnozaki Exp $	*/
3 
4 /*-
5  * Copyright (c)2002 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*-
31  * Copyright (c) 1993
32  *	The Regents of the University of California.  All rights reserved.
33  *
34  * This code is derived from software contributed to Berkeley by
35  * Paul Borman at Krystal Technologies.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  */
61 
62 #include <sys/cdefs.h>
63 #include <sys/types.h>
64 
65 #include <assert.h>
66 #include <errno.h>
67 #include <limits.h>
68 #include <stdbool.h>
69 #include <stddef.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <wchar.h>
74 
75 #include "citrus_namespace.h"
76 #include "citrus_types.h"
77 #include "citrus_module.h"
78 #include "citrus_stdenc.h"
79 #include "citrus_utf8.h"
80 
81 
82 /* ----------------------------------------------------------------------
83  * private stuffs used by templates
84  */
85 
86 static uint8_t _UTF8_count_array[256] = {
87 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 00 - 0F */
88 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 10 - 1F */
89 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 20 - 2F */
90 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 30 - 3F */
91 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 40 - 4F */
92 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 50 - 5F */
93 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 60 - 6F */
94 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 70 - 7F */
95 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 80 - 8F */
96 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 90 - 9F */
97 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* A0 - AF */
98 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* B0 - BF */
99 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* C0 - CF */
100 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* D0 - DF */
101 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* E0 - EF */
102 	4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0	/* F0 - FF */
103 };
104 
105 static uint8_t const *_UTF8_count = _UTF8_count_array;
106 
107 static const uint32_t _UTF8_range[] = {
108 	0,	/*dummy*/
109 	0x00000000, 0x00000080, 0x00000800, 0x00010000,
110 	0x00200000, 0x04000000, 0x80000000,
111 };
112 
113 typedef struct {
114 	int	 chlen;
115 	char	 ch[6];
116 } _UTF8State;
117 
118 typedef void *_UTF8EncodingInfo;
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 _ENCODING_STATE			_UTF8State
126 #define _ENCODING_MB_CUR_MAX(_ei_)	6
127 #define _ENCODING_IS_STATE_DEPENDENT	0
128 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
129 
130 static size_t
131 _UTF8_findlen(wchar_t v)
132 {
133 	size_t i;
134 	uint32_t c;
135 
136 	c = (uint32_t)v;	/*XXX*/
137 	for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]) - 1; i++)
138 		if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1])
139 			return (i);
140 
141 	return (-1);	/*out of range*/
142 }
143 
144 static __inline bool
145 _UTF8_surrogate(wchar_t wc)
146 {
147 
148 	return (wc >= 0xd800 && wc <= 0xdfff);
149 }
150 
151 static __inline void
152 /*ARGSUSED*/
153 _citrus_UTF8_init_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s)
154 {
155 
156 	s->chlen = 0;
157 }
158 
159 #if 0
160 static __inline void
161 /*ARGSUSED*/
162 _citrus_UTF8_pack_state(_UTF8EncodingInfo *ei __unused, void *pspriv,
163     const _UTF8State *s)
164 {
165 
166 	memcpy(pspriv, (const void *)s, sizeof(*s));
167 }
168 
169 static __inline void
170 /*ARGSUSED*/
171 _citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s,
172     const void *pspriv)
173 {
174 
175 	memcpy((void *)s, pspriv, sizeof(*s));
176 }
177 #endif
178 
179 static int
180 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
181     size_t n, _UTF8State *psenc, size_t *nresult)
182 {
183 	const char *s0;
184 	wchar_t wchar;
185 	int i;
186 	uint8_t c;
187 
188 	s0 = *s;
189 
190 	if (s0 == NULL) {
191 		_citrus_UTF8_init_state(ei, psenc);
192 		*nresult = 0; /* state independent */
193 		return (0);
194 	}
195 
196 	/* make sure we have the first byte in the buffer */
197 	if (psenc->chlen == 0) {
198 		if (n-- < 1)
199 			goto restart;
200 		psenc->ch[psenc->chlen++] = *s0++;
201 	}
202 
203 	c = _UTF8_count[psenc->ch[0] & 0xff];
204 	if (c < 1 || c < psenc->chlen)
205 		goto ilseq;
206 
207 	if (c == 1)
208 		wchar = psenc->ch[0] & 0xff;
209 	else {
210 		while (psenc->chlen < c) {
211 			if (n-- < 1)
212 				goto restart;
213 			psenc->ch[psenc->chlen++] = *s0++;
214 		}
215 		wchar = psenc->ch[0] & (0x7f >> c);
216 		for (i = 1; i < c; i++) {
217 			if ((psenc->ch[i] & 0xc0) != 0x80)
218 				goto ilseq;
219 			wchar <<= 6;
220 			wchar |= (psenc->ch[i] & 0x3f);
221 		}
222 		if (_UTF8_surrogate(wchar) || _UTF8_findlen(wchar) != c)
223 			goto ilseq;
224 	}
225 	if (pwc != NULL)
226 		*pwc = wchar;
227 	*nresult = (wchar == 0) ? 0 : s0 - *s;
228 	*s = s0;
229 	psenc->chlen = 0;
230 
231 	return (0);
232 
233 ilseq:
234 	*nresult = (size_t)-1;
235 	return (EILSEQ);
236 
237 restart:
238 	*s = s0;
239 	*nresult = (size_t)-2;
240 	return (0);
241 }
242 
243 static int
244 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei __unused, char *s, size_t n,
245     wchar_t wc, _UTF8State *psenc __unused, size_t *nresult)
246 {
247 	wchar_t c;
248 	size_t cnt;
249 	int i, ret;
250 
251 	if (_UTF8_surrogate(wc)) {
252 		ret = EILSEQ;
253 		goto err;
254 	}
255 	cnt = _UTF8_findlen(wc);
256 	if (cnt <= 0 || cnt > 6) {
257 		/* invalid UCS4 value */
258 		ret = EILSEQ;
259 		goto err;
260 	}
261 	if (n < cnt) {
262 		/* bound check failure */
263 		ret = E2BIG;
264 		goto err;
265 	}
266 
267 	c = wc;
268 	if (s) {
269 		for (i = cnt - 1; i > 0; i--) {
270 			s[i] = 0x80 | (c & 0x3f);
271 			c >>= 6;
272 		}
273 		s[0] = c;
274 		if (cnt == 1)
275 			s[0] &= 0x7f;
276 		else {
277 			s[0] &= (0x7f >> cnt);
278 			s[0] |= ((0xff00 >> cnt) & 0xff);
279 		}
280 	}
281 
282 	*nresult = (size_t)cnt;
283 	return (0);
284 
285 err:
286 	*nresult = (size_t)-1;
287 	return (ret);
288 }
289 
290 static __inline int
291 /*ARGSUSED*/
292 _citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei __unused,
293     _csid_t * __restrict csid, _index_t * __restrict idx,
294     wchar_t wc)
295 {
296 
297 	*csid = 0;
298 	*idx = (_citrus_index_t)wc;
299 
300 	return (0);
301 }
302 
303 static __inline int
304 /*ARGSUSED*/
305 _citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei __unused,
306     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
307 {
308 
309 	if (csid != 0)
310 		return (EILSEQ);
311 
312 	*wc = (wchar_t)idx;
313 
314 	return (0);
315 }
316 
317 static __inline int
318 /*ARGSUSED*/
319 _citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo * __restrict ei __unused,
320     _UTF8State * __restrict psenc, int * __restrict rstate)
321 {
322 
323 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
324 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
325 	return (0);
326 }
327 
328 static int
329 /*ARGSUSED*/
330 _citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei __unused,
331     const void * __restrict var __unused, size_t lenvar __unused)
332 {
333 
334 	return (0);
335 }
336 
337 static void
338 /*ARGSUSED*/
339 _citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei __unused)
340 {
341 
342 }
343 
344 /* ----------------------------------------------------------------------
345  * public interface for stdenc
346  */
347 
348 _CITRUS_STDENC_DECLS(UTF8);
349 _CITRUS_STDENC_DEF_OPS(UTF8);
350 
351 #include "citrus_stdenc_template.h"
352