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