1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/*	$NetBSD: citrus_utf8.c,v 1.17 2008/06/14 16:01:08 tnozaki Exp $	*/
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c)2002 Citrus Project,
6219019Sgabor * All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor * 1. Redistributions of source code must retain the above copyright
12219019Sgabor *    notice, this list of conditions and the following disclaimer.
13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14219019Sgabor *    notice, this list of conditions and the following disclaimer in the
15219019Sgabor *    documentation and/or other materials provided with the distribution.
16219019Sgabor *
17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219019Sgabor * SUCH DAMAGE.
28219019Sgabor */
29219019Sgabor
30219019Sgabor/*-
31219019Sgabor * Copyright (c) 1993
32219019Sgabor *	The Regents of the University of California.  All rights reserved.
33219019Sgabor *
34219019Sgabor * This code is derived from software contributed to Berkeley by
35219019Sgabor * Paul Borman at Krystal Technologies.
36219019Sgabor *
37219019Sgabor * Redistribution and use in source and binary forms, with or without
38219019Sgabor * modification, are permitted provided that the following conditions
39219019Sgabor * are met:
40219019Sgabor * 1. Redistributions of source code must retain the above copyright
41219019Sgabor *    notice, this list of conditions and the following disclaimer.
42219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
43219019Sgabor *    notice, this list of conditions and the following disclaimer in the
44219019Sgabor *    documentation and/or other materials provided with the distribution.
45219019Sgabor * 3. Neither the name of the University nor the names of its contributors
46219019Sgabor *    may be used to endorse or promote products derived from this software
47219019Sgabor *    without specific prior written permission.
48219019Sgabor *
49219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59219019Sgabor * SUCH DAMAGE.
60219019Sgabor */
61219019Sgabor
62219019Sgabor#include <sys/cdefs.h>
63219019Sgabor#include <sys/types.h>
64219019Sgabor
65219019Sgabor#include <assert.h>
66219019Sgabor#include <errno.h>
67219019Sgabor#include <limits.h>
68219019Sgabor#include <stdbool.h>
69219019Sgabor#include <stddef.h>
70219019Sgabor#include <stdio.h>
71219019Sgabor#include <stdlib.h>
72219019Sgabor#include <string.h>
73219019Sgabor#include <wchar.h>
74219019Sgabor
75219019Sgabor#include "citrus_namespace.h"
76219019Sgabor#include "citrus_types.h"
77219019Sgabor#include "citrus_module.h"
78219019Sgabor#include "citrus_stdenc.h"
79219019Sgabor#include "citrus_utf8.h"
80219019Sgabor
81219019Sgabor
82219019Sgabor/* ----------------------------------------------------------------------
83219019Sgabor * private stuffs used by templates
84219019Sgabor */
85219019Sgabor
86219019Sgaborstatic uint8_t _UTF8_count_array[256] = {
87219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 00 - 0F */
88219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 10 - 1F */
89219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 20 - 2F */
90219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 30 - 3F */
91219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 40 - 4F */
92219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 50 - 5F */
93219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 60 - 6F */
94219019Sgabor	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 70 - 7F */
95219019Sgabor	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 80 - 8F */
96219019Sgabor	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 90 - 9F */
97219019Sgabor	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* A0 - AF */
98219019Sgabor	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* B0 - BF */
99219019Sgabor	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* C0 - CF */
100219019Sgabor	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* D0 - DF */
101219019Sgabor	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* E0 - EF */
102219019Sgabor	4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0	/* F0 - FF */
103219019Sgabor};
104219019Sgabor
105219019Sgaborstatic uint8_t const *_UTF8_count = _UTF8_count_array;
106219019Sgabor
107219019Sgaborstatic const uint32_t _UTF8_range[] = {
108219019Sgabor	0,	/*dummy*/
109219019Sgabor	0x00000000, 0x00000080, 0x00000800, 0x00010000,
110219019Sgabor	0x00200000, 0x04000000, 0x80000000,
111219019Sgabor};
112219019Sgabor
113219019Sgabortypedef struct {
114219019Sgabor	int	 chlen;
115219019Sgabor	char	 ch[6];
116219019Sgabor} _UTF8State;
117219019Sgabor
118219019Sgabortypedef void *_UTF8EncodingInfo;
119219019Sgabor
120219019Sgabor#define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
121219019Sgabor#define _CEI_TO_STATE(_ei_, _func_)	(_ei_)->states.s_##_func_
122219019Sgabor
123219019Sgabor#define _FUNCNAME(m)			_citrus_UTF8_##m
124219019Sgabor#define _ENCODING_INFO			_UTF8EncodingInfo
125219019Sgabor#define _ENCODING_STATE			_UTF8State
126219019Sgabor#define _ENCODING_MB_CUR_MAX(_ei_)	6
127219019Sgabor#define _ENCODING_IS_STATE_DEPENDENT	0
128219019Sgabor#define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
129219019Sgabor
130219019Sgaborstatic size_t
131219019Sgabor_UTF8_findlen(wchar_t v)
132219019Sgabor{
133219019Sgabor	size_t i;
134219019Sgabor	uint32_t c;
135219019Sgabor
136219019Sgabor	c = (uint32_t)v;	/*XXX*/
137219019Sgabor	for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]) - 1; i++)
138219019Sgabor		if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1])
139219019Sgabor			return (i);
140219019Sgabor
141219019Sgabor	return (-1);	/*out of range*/
142219019Sgabor}
143219019Sgabor
144219019Sgaborstatic __inline bool
145219019Sgabor_UTF8_surrogate(wchar_t wc)
146219019Sgabor{
147219019Sgabor
148219019Sgabor	return (wc >= 0xd800 && wc <= 0xdfff);
149219019Sgabor}
150219019Sgabor
151219019Sgaborstatic __inline void
152219019Sgabor/*ARGSUSED*/
153219019Sgabor_citrus_UTF8_init_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s)
154219019Sgabor{
155219019Sgabor
156219019Sgabor	s->chlen = 0;
157219019Sgabor}
158219019Sgabor
159219019Sgaborstatic __inline void
160219019Sgabor/*ARGSUSED*/
161219019Sgabor_citrus_UTF8_pack_state(_UTF8EncodingInfo *ei __unused, void *pspriv,
162219019Sgabor    const _UTF8State *s)
163219019Sgabor{
164219019Sgabor
165219019Sgabor	memcpy(pspriv, (const void *)s, sizeof(*s));
166219019Sgabor}
167219019Sgabor
168219019Sgaborstatic __inline void
169219019Sgabor/*ARGSUSED*/
170219019Sgabor_citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s,
171219019Sgabor    const void *pspriv)
172219019Sgabor{
173219019Sgabor
174219019Sgabor	memcpy((void *)s, pspriv, sizeof(*s));
175219019Sgabor}
176219019Sgabor
177219019Sgaborstatic int
178252583Speter_citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
179219019Sgabor    size_t n, _UTF8State *psenc, size_t *nresult)
180219019Sgabor{
181252583Speter	const char *s0;
182219019Sgabor	wchar_t wchar;
183219019Sgabor	int i;
184219019Sgabor	uint8_t c;
185219019Sgabor
186219019Sgabor	s0 = *s;
187219019Sgabor
188219019Sgabor	if (s0 == NULL) {
189219019Sgabor		_citrus_UTF8_init_state(ei, psenc);
190219019Sgabor		*nresult = 0; /* state independent */
191219019Sgabor		return (0);
192219019Sgabor	}
193219019Sgabor
194219019Sgabor	/* make sure we have the first byte in the buffer */
195219019Sgabor	if (psenc->chlen == 0) {
196219019Sgabor		if (n-- < 1)
197219019Sgabor			goto restart;
198219019Sgabor		psenc->ch[psenc->chlen++] = *s0++;
199219019Sgabor	}
200219019Sgabor
201219019Sgabor	c = _UTF8_count[psenc->ch[0] & 0xff];
202219019Sgabor	if (c < 1 || c < psenc->chlen)
203219019Sgabor		goto ilseq;
204219019Sgabor
205219019Sgabor	if (c == 1)
206219019Sgabor		wchar = psenc->ch[0] & 0xff;
207219019Sgabor	else {
208219019Sgabor		while (psenc->chlen < c) {
209219019Sgabor			if (n-- < 1)
210219019Sgabor				goto restart;
211219019Sgabor			psenc->ch[psenc->chlen++] = *s0++;
212219019Sgabor		}
213219019Sgabor		wchar = psenc->ch[0] & (0x7f >> c);
214219019Sgabor		for (i = 1; i < c; i++) {
215219019Sgabor			if ((psenc->ch[i] & 0xc0) != 0x80)
216219019Sgabor				goto ilseq;
217219019Sgabor			wchar <<= 6;
218219019Sgabor			wchar |= (psenc->ch[i] & 0x3f);
219219019Sgabor		}
220219019Sgabor		if (_UTF8_surrogate(wchar) || _UTF8_findlen(wchar) != c)
221219019Sgabor			goto ilseq;
222219019Sgabor	}
223219019Sgabor	if (pwc != NULL)
224219019Sgabor		*pwc = wchar;
225219019Sgabor	*nresult = (wchar == 0) ? 0 : s0 - *s;
226219019Sgabor	*s = s0;
227219019Sgabor	psenc->chlen = 0;
228219019Sgabor
229219019Sgabor	return (0);
230219019Sgabor
231219019Sgaborilseq:
232219019Sgabor	*nresult = (size_t)-1;
233219019Sgabor	return (EILSEQ);
234219019Sgabor
235219019Sgaborrestart:
236219019Sgabor	*s = s0;
237219019Sgabor	*nresult = (size_t)-2;
238219019Sgabor	return (0);
239219019Sgabor}
240219019Sgabor
241219019Sgaborstatic int
242219019Sgabor_citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei __unused, char *s, size_t n,
243219019Sgabor    wchar_t wc, _UTF8State *psenc __unused, size_t *nresult)
244219019Sgabor{
245219019Sgabor	wchar_t c;
246219019Sgabor	size_t cnt;
247219019Sgabor	int i, ret;
248219019Sgabor
249219019Sgabor	if (_UTF8_surrogate(wc)) {
250219019Sgabor		ret = EILSEQ;
251219019Sgabor		goto err;
252219019Sgabor	}
253219019Sgabor	cnt = _UTF8_findlen(wc);
254219019Sgabor	if (cnt <= 0 || cnt > 6) {
255219019Sgabor		/* invalid UCS4 value */
256219019Sgabor		ret = EILSEQ;
257219019Sgabor		goto err;
258219019Sgabor	}
259219019Sgabor	if (n < cnt) {
260219019Sgabor		/* bound check failure */
261219019Sgabor		ret = E2BIG;
262219019Sgabor		goto err;
263219019Sgabor	}
264219019Sgabor
265219019Sgabor	c = wc;
266219019Sgabor	if (s) {
267219019Sgabor		for (i = cnt - 1; i > 0; i--) {
268219019Sgabor			s[i] = 0x80 | (c & 0x3f);
269219019Sgabor			c >>= 6;
270219019Sgabor		}
271219019Sgabor		s[0] = c;
272219019Sgabor		if (cnt == 1)
273219019Sgabor			s[0] &= 0x7f;
274219019Sgabor		else {
275219019Sgabor			s[0] &= (0x7f >> cnt);
276219019Sgabor			s[0] |= ((0xff00 >> cnt) & 0xff);
277219019Sgabor		}
278219019Sgabor	}
279219019Sgabor
280219019Sgabor	*nresult = (size_t)cnt;
281219019Sgabor	return (0);
282219019Sgabor
283219019Sgaborerr:
284219019Sgabor	*nresult = (size_t)-1;
285219019Sgabor	return (ret);
286219019Sgabor}
287219019Sgabor
288219019Sgaborstatic __inline int
289219019Sgabor/*ARGSUSED*/
290219019Sgabor_citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei __unused,
291219019Sgabor    _csid_t * __restrict csid, _index_t * __restrict idx,
292219019Sgabor    wchar_t wc)
293219019Sgabor{
294219019Sgabor
295219019Sgabor	*csid = 0;
296219019Sgabor	*idx = (_citrus_index_t)wc;
297219019Sgabor
298219019Sgabor	return (0);
299219019Sgabor}
300219019Sgabor
301219019Sgaborstatic __inline int
302219019Sgabor/*ARGSUSED*/
303219019Sgabor_citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei __unused,
304219019Sgabor    wchar_t * __restrict wc, _csid_t csid, _index_t idx)
305219019Sgabor{
306219019Sgabor
307219019Sgabor	if (csid != 0)
308219019Sgabor		return (EILSEQ);
309219019Sgabor
310219019Sgabor	*wc = (wchar_t)idx;
311219019Sgabor
312219019Sgabor	return (0);
313219019Sgabor}
314219019Sgabor
315219019Sgaborstatic __inline int
316219019Sgabor/*ARGSUSED*/
317219019Sgabor_citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo * __restrict ei __unused,
318219019Sgabor    _UTF8State * __restrict psenc, int * __restrict rstate)
319219019Sgabor{
320219019Sgabor
321219019Sgabor	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
322219019Sgabor	    _STDENC_SDGEN_INCOMPLETE_CHAR;
323219019Sgabor	return (0);
324219019Sgabor}
325219019Sgabor
326219019Sgaborstatic int
327219019Sgabor/*ARGSUSED*/
328219019Sgabor_citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei __unused,
329219019Sgabor    const void * __restrict var __unused, size_t lenvar __unused)
330219019Sgabor{
331219019Sgabor
332219019Sgabor	return (0);
333219019Sgabor}
334219019Sgabor
335219019Sgaborstatic void
336219019Sgabor/*ARGSUSED*/
337219019Sgabor_citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei __unused)
338219019Sgabor{
339219019Sgabor
340219019Sgabor}
341219019Sgabor
342219019Sgabor/* ----------------------------------------------------------------------
343219019Sgabor * public interface for stdenc
344219019Sgabor */
345219019Sgabor
346219019Sgabor_CITRUS_STDENC_DECLS(UTF8);
347219019Sgabor_CITRUS_STDENC_DEF_OPS(UTF8);
348219019Sgabor
349219019Sgabor#include "citrus_stdenc_template.h"
350