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