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