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