xref: /freebsd/lib/libiconv_modules/EUC/citrus_euc.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1ad30f8e7SGabor Kovesdan /*	$NetBSD: citrus_euc.c,v 1.14 2009/01/11 02:46:24 christos 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 /*-
30*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
31*8a16b7a1SPedro 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 <stddef.h>
69ad30f8e7SGabor Kovesdan #include <stdio.h>
70ad30f8e7SGabor Kovesdan #include <stdlib.h>
71ad30f8e7SGabor Kovesdan #include <string.h>
72ad30f8e7SGabor Kovesdan #include <wchar.h>
73ad30f8e7SGabor Kovesdan 
74ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
75ad30f8e7SGabor Kovesdan #include "citrus_bcs.h"
76ad30f8e7SGabor Kovesdan #include "citrus_types.h"
77ad30f8e7SGabor Kovesdan #include "citrus_module.h"
78ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
79ad30f8e7SGabor Kovesdan #include "citrus_euc.h"
80ad30f8e7SGabor Kovesdan 
81ad30f8e7SGabor Kovesdan 
82ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
83ad30f8e7SGabor Kovesdan  * private stuffs used by templates
84ad30f8e7SGabor Kovesdan  */
85ad30f8e7SGabor Kovesdan 
86ad30f8e7SGabor Kovesdan typedef struct {
87ad30f8e7SGabor Kovesdan 	int	 chlen;
88ad30f8e7SGabor Kovesdan 	char	 ch[3];
89ad30f8e7SGabor Kovesdan } _EUCState;
90ad30f8e7SGabor Kovesdan 
91ad30f8e7SGabor Kovesdan typedef struct {
92ad30f8e7SGabor Kovesdan 	wchar_t		 bits[4];
93ad30f8e7SGabor Kovesdan 	wchar_t		 mask;
94ad30f8e7SGabor Kovesdan 	unsigned	 count[4];
95ad30f8e7SGabor Kovesdan 	unsigned	 mb_cur_max;
96ad30f8e7SGabor Kovesdan } _EUCEncodingInfo;
97ad30f8e7SGabor Kovesdan 
98ad30f8e7SGabor Kovesdan #define	_SS2	0x008e
99ad30f8e7SGabor Kovesdan #define	_SS3	0x008f
100ad30f8e7SGabor Kovesdan 
101ad30f8e7SGabor Kovesdan #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
102ad30f8e7SGabor Kovesdan #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
103ad30f8e7SGabor Kovesdan 
104ad30f8e7SGabor Kovesdan #define _FUNCNAME(m)			_citrus_EUC_##m
105ad30f8e7SGabor Kovesdan #define _ENCODING_INFO			_EUCEncodingInfo
106ad30f8e7SGabor Kovesdan #define _ENCODING_STATE			_EUCState
107ad30f8e7SGabor Kovesdan #define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
108ad30f8e7SGabor Kovesdan #define _ENCODING_IS_STATE_DEPENDENT	0
109ad30f8e7SGabor Kovesdan #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
110ad30f8e7SGabor Kovesdan 
111ad30f8e7SGabor Kovesdan 
112ad30f8e7SGabor Kovesdan static __inline int
_citrus_EUC_cs(unsigned int c)113ad30f8e7SGabor Kovesdan _citrus_EUC_cs(unsigned int c)
114ad30f8e7SGabor Kovesdan {
115ad30f8e7SGabor Kovesdan 
116ad30f8e7SGabor Kovesdan 	c &= 0xff;
117ad30f8e7SGabor Kovesdan 
118ad30f8e7SGabor Kovesdan 	return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
119ad30f8e7SGabor Kovesdan }
120ad30f8e7SGabor Kovesdan 
121ad30f8e7SGabor Kovesdan static __inline int
_citrus_EUC_parse_variable(_EUCEncodingInfo * ei,const void * var,size_t lenvar __unused)122ad30f8e7SGabor Kovesdan _citrus_EUC_parse_variable(_EUCEncodingInfo *ei, const void *var,
123ad30f8e7SGabor Kovesdan     size_t lenvar __unused)
124ad30f8e7SGabor Kovesdan {
125ad30f8e7SGabor Kovesdan 	char *e;
126ad30f8e7SGabor Kovesdan 	const char *v;
127ad30f8e7SGabor Kovesdan 	int x;
128ad30f8e7SGabor Kovesdan 
129ad30f8e7SGabor Kovesdan 	/* parse variable string */
130ad30f8e7SGabor Kovesdan 	if (!var)
131ad30f8e7SGabor Kovesdan 		return (EFTYPE);
132ad30f8e7SGabor Kovesdan 
133ad30f8e7SGabor Kovesdan 	v = (const char *)var;
134ad30f8e7SGabor Kovesdan 
135ad30f8e7SGabor Kovesdan 	while (*v == ' ' || *v == '\t')
136ad30f8e7SGabor Kovesdan 		++v;
137ad30f8e7SGabor Kovesdan 
138ad30f8e7SGabor Kovesdan 	ei->mb_cur_max = 1;
139ad30f8e7SGabor Kovesdan 	for (x = 0; x < 4; ++x) {
140ad30f8e7SGabor Kovesdan 		ei->count[x] = (int)_bcs_strtol(v, (char **)&e, 0);
141ad30f8e7SGabor Kovesdan 		if (v == e || !(v = e) || ei->count[x] < 1 || ei->count[x] > 4) {
142ad30f8e7SGabor Kovesdan 			return (EFTYPE);
143ad30f8e7SGabor Kovesdan 		}
144ad30f8e7SGabor Kovesdan 		if (ei->mb_cur_max < ei->count[x])
145ad30f8e7SGabor Kovesdan 			ei->mb_cur_max = ei->count[x];
146ad30f8e7SGabor Kovesdan 		while (*v == ' ' || *v == '\t')
147ad30f8e7SGabor Kovesdan 			++v;
148ad30f8e7SGabor Kovesdan 		ei->bits[x] = (int)_bcs_strtol(v, (char **)&e, 0);
149ad30f8e7SGabor Kovesdan 		if (v == e || !(v = e)) {
150ad30f8e7SGabor Kovesdan 			return (EFTYPE);
151ad30f8e7SGabor Kovesdan 		}
152ad30f8e7SGabor Kovesdan 		while (*v == ' ' || *v == '\t')
153ad30f8e7SGabor Kovesdan 			++v;
154ad30f8e7SGabor Kovesdan 	}
155ad30f8e7SGabor Kovesdan 	ei->mask = (int)_bcs_strtol(v, (char **)&e, 0);
156ad30f8e7SGabor Kovesdan 	if (v == e || !(v = e)) {
157ad30f8e7SGabor Kovesdan 		return (EFTYPE);
158ad30f8e7SGabor Kovesdan 	}
159ad30f8e7SGabor Kovesdan 
160ad30f8e7SGabor Kovesdan 	return (0);
161ad30f8e7SGabor Kovesdan }
162ad30f8e7SGabor Kovesdan 
163ad30f8e7SGabor Kovesdan 
164ad30f8e7SGabor Kovesdan static __inline void
165ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUC_init_state(_EUCEncodingInfo * ei __unused,_EUCState * s)166ad30f8e7SGabor Kovesdan _citrus_EUC_init_state(_EUCEncodingInfo *ei __unused, _EUCState *s)
167ad30f8e7SGabor Kovesdan {
168ad30f8e7SGabor Kovesdan 
169ad30f8e7SGabor Kovesdan 	memset(s, 0, sizeof(*s));
170ad30f8e7SGabor Kovesdan }
171ad30f8e7SGabor Kovesdan 
172b61949ddSDimitry Andric #if 0
173ad30f8e7SGabor Kovesdan static __inline void
174ad30f8e7SGabor Kovesdan /*ARGSUSED*/
175ad30f8e7SGabor Kovesdan _citrus_EUC_pack_state(_EUCEncodingInfo *ei __unused, void *pspriv,
176ad30f8e7SGabor Kovesdan     const _EUCState *s)
177ad30f8e7SGabor Kovesdan {
178ad30f8e7SGabor Kovesdan 
179ad30f8e7SGabor Kovesdan 	memcpy(pspriv, (const void *)s, sizeof(*s));
180ad30f8e7SGabor Kovesdan }
181ad30f8e7SGabor Kovesdan 
182ad30f8e7SGabor Kovesdan static __inline void
183ad30f8e7SGabor Kovesdan /*ARGSUSED*/
184ad30f8e7SGabor Kovesdan _citrus_EUC_unpack_state(_EUCEncodingInfo *ei __unused, _EUCState *s,
185ad30f8e7SGabor Kovesdan     const void *pspriv)
186ad30f8e7SGabor Kovesdan {
187ad30f8e7SGabor Kovesdan 
188ad30f8e7SGabor Kovesdan 	memcpy((void *)s, pspriv, sizeof(*s));
189ad30f8e7SGabor Kovesdan }
190b61949ddSDimitry Andric #endif
191ad30f8e7SGabor Kovesdan 
192ad30f8e7SGabor Kovesdan static int
_citrus_EUC_mbrtowc_priv(_EUCEncodingInfo * ei,wchar_t * pwc,char ** s,size_t n,_EUCState * psenc,size_t * nresult)1931243a98eSTijl Coosemans _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, char **s,
194ad30f8e7SGabor Kovesdan     size_t n, _EUCState *psenc, size_t *nresult)
195ad30f8e7SGabor Kovesdan {
196ad30f8e7SGabor Kovesdan 	wchar_t wchar;
197ad30f8e7SGabor Kovesdan 	int c, chlenbak, cs, len;
1981243a98eSTijl Coosemans 	char *s0, *s1 = NULL;
199ad30f8e7SGabor Kovesdan 
200ad30f8e7SGabor Kovesdan 	s0 = *s;
201ad30f8e7SGabor Kovesdan 
202ad30f8e7SGabor Kovesdan 	if (s0 == NULL) {
203ad30f8e7SGabor Kovesdan 		_citrus_EUC_init_state(ei, psenc);
204ad30f8e7SGabor Kovesdan 		*nresult = 0; /* state independent */
205ad30f8e7SGabor Kovesdan 		return (0);
206ad30f8e7SGabor Kovesdan 	}
207ad30f8e7SGabor Kovesdan 
208ad30f8e7SGabor Kovesdan 	chlenbak = psenc->chlen;
209ad30f8e7SGabor Kovesdan 
210ad30f8e7SGabor Kovesdan 	/* make sure we have the first byte in the buffer */
211ad30f8e7SGabor Kovesdan 	switch (psenc->chlen) {
212ad30f8e7SGabor Kovesdan 	case 0:
213ad30f8e7SGabor Kovesdan 		if (n < 1)
214ad30f8e7SGabor Kovesdan 			goto restart;
215ad30f8e7SGabor Kovesdan 		psenc->ch[0] = *s0++;
216ad30f8e7SGabor Kovesdan 		psenc->chlen = 1;
217ad30f8e7SGabor Kovesdan 		n--;
218ad30f8e7SGabor Kovesdan 		break;
219ad30f8e7SGabor Kovesdan 	case 1:
220ad30f8e7SGabor Kovesdan 	case 2:
221ad30f8e7SGabor Kovesdan 		break;
222ad30f8e7SGabor Kovesdan 	default:
223ad30f8e7SGabor Kovesdan 		/* illgeal state */
224ad30f8e7SGabor Kovesdan 		goto encoding_error;
225ad30f8e7SGabor Kovesdan 	}
226ad30f8e7SGabor Kovesdan 
227ad30f8e7SGabor Kovesdan 	c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)];
228ad30f8e7SGabor Kovesdan 	if (c == 0)
229ad30f8e7SGabor Kovesdan 		goto encoding_error;
230ad30f8e7SGabor Kovesdan 	while (psenc->chlen < c) {
231ad30f8e7SGabor Kovesdan 		if (n < 1)
232ad30f8e7SGabor Kovesdan 			goto restart;
233ad30f8e7SGabor Kovesdan 		psenc->ch[psenc->chlen] = *s0++;
234ad30f8e7SGabor Kovesdan 		psenc->chlen++;
235ad30f8e7SGabor Kovesdan 		n--;
236ad30f8e7SGabor Kovesdan 	}
237ad30f8e7SGabor Kovesdan 	*s = s0;
238ad30f8e7SGabor Kovesdan 
239ad30f8e7SGabor Kovesdan 	switch (cs) {
240ad30f8e7SGabor Kovesdan 	case 3:
241ad30f8e7SGabor Kovesdan 	case 2:
242ad30f8e7SGabor Kovesdan 		/* skip SS2/SS3 */
243ad30f8e7SGabor Kovesdan 		len = c - 1;
244ad30f8e7SGabor Kovesdan 		s1 = &psenc->ch[1];
245ad30f8e7SGabor Kovesdan 		break;
246ad30f8e7SGabor Kovesdan 	case 1:
247ad30f8e7SGabor Kovesdan 	case 0:
248ad30f8e7SGabor Kovesdan 		len = c;
249ad30f8e7SGabor Kovesdan 		s1 = &psenc->ch[0];
250ad30f8e7SGabor Kovesdan 		break;
251ad30f8e7SGabor Kovesdan 	default:
252ad30f8e7SGabor Kovesdan 		goto encoding_error;
253ad30f8e7SGabor Kovesdan 	}
254ad30f8e7SGabor Kovesdan 	wchar = 0;
255ad30f8e7SGabor Kovesdan 	while (len-- > 0)
256ad30f8e7SGabor Kovesdan 		wchar = (wchar << 8) | (*s1++ & 0xff);
257ad30f8e7SGabor Kovesdan 	wchar = (wchar & ~ei->mask) | ei->bits[cs];
258ad30f8e7SGabor Kovesdan 
259ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
260ad30f8e7SGabor Kovesdan 	if (pwc)
261ad30f8e7SGabor Kovesdan 		*pwc = wchar;
262ad30f8e7SGabor Kovesdan 	*nresult = wchar ? (size_t)(c - chlenbak) : 0;
263ad30f8e7SGabor Kovesdan 	return (0);
264ad30f8e7SGabor Kovesdan 
265ad30f8e7SGabor Kovesdan encoding_error:
266ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
267ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
268ad30f8e7SGabor Kovesdan 	return (EILSEQ);
269ad30f8e7SGabor Kovesdan 
270ad30f8e7SGabor Kovesdan restart:
271ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-2;
272ad30f8e7SGabor Kovesdan 	*s = s0;
273ad30f8e7SGabor Kovesdan 	return (0);
274ad30f8e7SGabor Kovesdan }
275ad30f8e7SGabor Kovesdan 
276ad30f8e7SGabor Kovesdan static int
_citrus_EUC_wcrtomb_priv(_EUCEncodingInfo * ei,char * s,size_t n,wchar_t wc,_EUCState * psenc __unused,size_t * nresult)277ad30f8e7SGabor Kovesdan _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc,
278ad30f8e7SGabor Kovesdan     _EUCState *psenc __unused, size_t *nresult)
279ad30f8e7SGabor Kovesdan {
280ad30f8e7SGabor Kovesdan 	wchar_t m, nm;
281ad30f8e7SGabor Kovesdan 	unsigned int cs;
282ad30f8e7SGabor Kovesdan 	int ret;
283ad30f8e7SGabor Kovesdan 	short i;
284ad30f8e7SGabor Kovesdan 
285ad30f8e7SGabor Kovesdan 	m = wc & ei->mask;
286ad30f8e7SGabor Kovesdan 	nm = wc & ~m;
287ad30f8e7SGabor Kovesdan 
288ad30f8e7SGabor Kovesdan 	for (cs = 0; cs < sizeof(ei->count) / sizeof(ei->count[0]); cs++)
289ad30f8e7SGabor Kovesdan 		if (m == ei->bits[cs])
290ad30f8e7SGabor Kovesdan 			break;
291ad30f8e7SGabor Kovesdan 	/* fallback case - not sure if it is necessary */
292ad30f8e7SGabor Kovesdan 	if (cs == sizeof(ei->count) / sizeof(ei->count[0]))
293ad30f8e7SGabor Kovesdan 		cs = 1;
294ad30f8e7SGabor Kovesdan 
295ad30f8e7SGabor Kovesdan 	i = ei->count[cs];
296ad30f8e7SGabor Kovesdan 	if (n < (unsigned)i) {
297ad30f8e7SGabor Kovesdan 		ret = E2BIG;
298ad30f8e7SGabor Kovesdan 		goto err;
299ad30f8e7SGabor Kovesdan 	}
300ad30f8e7SGabor Kovesdan 	m = (cs) ? 0x80 : 0x00;
301ad30f8e7SGabor Kovesdan 	switch (cs) {
302ad30f8e7SGabor Kovesdan 	case 2:
303ad30f8e7SGabor Kovesdan 		*s++ = _SS2;
304ad30f8e7SGabor Kovesdan 		i--;
305ad30f8e7SGabor Kovesdan 		break;
306ad30f8e7SGabor Kovesdan 	case 3:
307ad30f8e7SGabor Kovesdan 		*s++ = _SS3;
308ad30f8e7SGabor Kovesdan 		i--;
309ad30f8e7SGabor Kovesdan 		break;
310ad30f8e7SGabor Kovesdan 	}
311ad30f8e7SGabor Kovesdan 
312ad30f8e7SGabor Kovesdan 	while (i-- > 0)
313ad30f8e7SGabor Kovesdan 		*s++ = ((nm >> (i << 3)) & 0xff) | m;
314ad30f8e7SGabor Kovesdan 
315ad30f8e7SGabor Kovesdan 	*nresult = (size_t)ei->count[cs];
316ad30f8e7SGabor Kovesdan 	return (0);
317ad30f8e7SGabor Kovesdan 
318ad30f8e7SGabor Kovesdan err:
319ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
320ad30f8e7SGabor Kovesdan 	return (ret);
321ad30f8e7SGabor Kovesdan }
322ad30f8e7SGabor Kovesdan 
323ad30f8e7SGabor Kovesdan static __inline int
324ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,_csid_t * __restrict csid,_index_t * __restrict idx,wchar_t wc)325ad30f8e7SGabor Kovesdan _citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,
326ad30f8e7SGabor Kovesdan     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
327ad30f8e7SGabor Kovesdan {
328ad30f8e7SGabor Kovesdan 	wchar_t m, nm;
329ad30f8e7SGabor Kovesdan 
330ad30f8e7SGabor Kovesdan 	m = wc & ei->mask;
331ad30f8e7SGabor Kovesdan 	nm = wc & ~m;
332ad30f8e7SGabor Kovesdan 
333ad30f8e7SGabor Kovesdan 	*csid = (_citrus_csid_t)m;
334ad30f8e7SGabor Kovesdan 	*idx  = (_citrus_index_t)nm;
335ad30f8e7SGabor Kovesdan 
336ad30f8e7SGabor Kovesdan 	return (0);
337ad30f8e7SGabor Kovesdan }
338ad30f8e7SGabor Kovesdan 
339ad30f8e7SGabor Kovesdan static __inline int
340ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,wchar_t * __restrict wc,_csid_t csid,_index_t idx)341ad30f8e7SGabor Kovesdan _citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,
342ad30f8e7SGabor Kovesdan     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
343ad30f8e7SGabor Kovesdan {
344ad30f8e7SGabor Kovesdan 
345ad30f8e7SGabor Kovesdan 	if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0)
346ad30f8e7SGabor Kovesdan 		return (EINVAL);
347ad30f8e7SGabor Kovesdan 
348ad30f8e7SGabor Kovesdan 	*wc = (wchar_t)csid | (wchar_t)idx;
349ad30f8e7SGabor Kovesdan 
350ad30f8e7SGabor Kovesdan 	return (0);
351ad30f8e7SGabor Kovesdan }
352ad30f8e7SGabor Kovesdan 
353ad30f8e7SGabor Kovesdan static __inline int
354ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei __unused,_EUCState * __restrict psenc,int * __restrict rstate)355ad30f8e7SGabor Kovesdan _citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei __unused,
356ad30f8e7SGabor Kovesdan     _EUCState * __restrict psenc, int * __restrict rstate)
357ad30f8e7SGabor Kovesdan {
358ad30f8e7SGabor Kovesdan 
359ad30f8e7SGabor Kovesdan 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
360ad30f8e7SGabor Kovesdan 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
361ad30f8e7SGabor Kovesdan 	return (0);
362ad30f8e7SGabor Kovesdan }
363ad30f8e7SGabor Kovesdan 
364ad30f8e7SGabor Kovesdan static int
365ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,const void * __restrict var,size_t lenvar)366ad30f8e7SGabor Kovesdan _citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,
367ad30f8e7SGabor Kovesdan     const void * __restrict var, size_t lenvar)
368ad30f8e7SGabor Kovesdan {
369ad30f8e7SGabor Kovesdan 
370ad30f8e7SGabor Kovesdan 	return (_citrus_EUC_parse_variable(ei, var, lenvar));
371ad30f8e7SGabor Kovesdan }
372ad30f8e7SGabor Kovesdan 
373ad30f8e7SGabor Kovesdan static void
374ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei __unused)375ad30f8e7SGabor Kovesdan _citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei __unused)
376ad30f8e7SGabor Kovesdan {
377ad30f8e7SGabor Kovesdan 
378ad30f8e7SGabor Kovesdan }
379ad30f8e7SGabor Kovesdan 
380ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
381ad30f8e7SGabor Kovesdan  * public interface for stdenc
382ad30f8e7SGabor Kovesdan  */
383ad30f8e7SGabor Kovesdan 
384ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(EUC);
385ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(EUC);
386ad30f8e7SGabor Kovesdan 
387ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
388