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