1ad30f8e7SGabor Kovesdan /* $FreeBSD$ */
2ad30f8e7SGabor Kovesdan /*	$NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $	*/
3ad30f8e7SGabor Kovesdan 
4ad30f8e7SGabor Kovesdan /*-
5ad30f8e7SGabor Kovesdan  * Copyright (c)2002 Citrus Project,
6ad30f8e7SGabor Kovesdan  * All rights reserved.
7ad30f8e7SGabor Kovesdan  *
8ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
9ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
10ad30f8e7SGabor Kovesdan  * are met:
11ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
12ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
13ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
14ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
15ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
16ad30f8e7SGabor Kovesdan  *
17ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
28ad30f8e7SGabor Kovesdan  */
29ad30f8e7SGabor Kovesdan 
30ad30f8e7SGabor Kovesdan /*-
31ad30f8e7SGabor Kovesdan  * Copyright (c)1999 Citrus Project,
32ad30f8e7SGabor Kovesdan  * All rights reserved.
33ad30f8e7SGabor Kovesdan  *
34ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
35ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
36ad30f8e7SGabor Kovesdan  * are met:
37ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
38ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
39ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
40ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
41ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
42ad30f8e7SGabor Kovesdan  *
43ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
54ad30f8e7SGabor Kovesdan  *
55ad30f8e7SGabor Kovesdan  *	$Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
56ad30f8e7SGabor Kovesdan  */
57ad30f8e7SGabor Kovesdan 
58ad30f8e7SGabor Kovesdan #include <sys/cdefs.h>
59ad30f8e7SGabor Kovesdan #include <sys/types.h>
60ad30f8e7SGabor Kovesdan 
61ad30f8e7SGabor Kovesdan #include <assert.h>
62ad30f8e7SGabor Kovesdan #include <errno.h>
63ad30f8e7SGabor Kovesdan #include <limits.h>
64ad30f8e7SGabor Kovesdan #include <stddef.h>
65ad30f8e7SGabor Kovesdan #include <stdio.h>
66ad30f8e7SGabor Kovesdan #include <stdlib.h>
67ad30f8e7SGabor Kovesdan #include <string.h>
68ad30f8e7SGabor Kovesdan #include <wchar.h>
69ad30f8e7SGabor Kovesdan 
70ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
71ad30f8e7SGabor Kovesdan #include "citrus_types.h"
72ad30f8e7SGabor Kovesdan #include "citrus_module.h"
73ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
74ad30f8e7SGabor Kovesdan #include "citrus_euctw.h"
75ad30f8e7SGabor Kovesdan 
76ad30f8e7SGabor Kovesdan 
77ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
78ad30f8e7SGabor Kovesdan  * private stuffs used by templates
79ad30f8e7SGabor Kovesdan  */
80ad30f8e7SGabor Kovesdan 
81ad30f8e7SGabor Kovesdan typedef struct {
82ad30f8e7SGabor Kovesdan 	int	 chlen;
83ad30f8e7SGabor Kovesdan 	char	 ch[4];
84ad30f8e7SGabor Kovesdan } _EUCTWState;
85ad30f8e7SGabor Kovesdan 
86ad30f8e7SGabor Kovesdan typedef struct {
87ad30f8e7SGabor Kovesdan 	int	 dummy;
88ad30f8e7SGabor Kovesdan } _EUCTWEncodingInfo;
89ad30f8e7SGabor Kovesdan 
90ad30f8e7SGabor Kovesdan #define	_SS2	0x008e
91ad30f8e7SGabor Kovesdan #define	_SS3	0x008f
92ad30f8e7SGabor Kovesdan 
93ad30f8e7SGabor Kovesdan #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
94ad30f8e7SGabor Kovesdan #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
95ad30f8e7SGabor Kovesdan 
96ad30f8e7SGabor Kovesdan #define _FUNCNAME(m)			_citrus_EUCTW_##m
97ad30f8e7SGabor Kovesdan #define _ENCODING_INFO			_EUCTWEncodingInfo
98ad30f8e7SGabor Kovesdan #define _ENCODING_STATE			_EUCTWState
99ad30f8e7SGabor Kovesdan #define _ENCODING_MB_CUR_MAX(_ei_)	4
100ad30f8e7SGabor Kovesdan #define _ENCODING_IS_STATE_DEPENDENT	0
101ad30f8e7SGabor Kovesdan #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
102ad30f8e7SGabor Kovesdan 
103ad30f8e7SGabor Kovesdan static __inline int
104ad30f8e7SGabor Kovesdan _citrus_EUCTW_cs(unsigned int c)
105ad30f8e7SGabor Kovesdan {
106ad30f8e7SGabor Kovesdan 
107ad30f8e7SGabor Kovesdan 	c &= 0xff;
108ad30f8e7SGabor Kovesdan 
109ad30f8e7SGabor Kovesdan 	return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
110ad30f8e7SGabor Kovesdan }
111ad30f8e7SGabor Kovesdan 
112ad30f8e7SGabor Kovesdan static __inline int
113ad30f8e7SGabor Kovesdan _citrus_EUCTW_count(int cs)
114ad30f8e7SGabor Kovesdan {
115ad30f8e7SGabor Kovesdan 
116ad30f8e7SGabor Kovesdan 	switch (cs) {
117ad30f8e7SGabor Kovesdan 	case 0:
118ad30f8e7SGabor Kovesdan 		/*FALLTHROUGH*/
119ad30f8e7SGabor Kovesdan 	case 1:
120ad30f8e7SGabor Kovesdan 		/*FALLTHROUGH*/
121ad30f8e7SGabor Kovesdan 	case 2:
122ad30f8e7SGabor Kovesdan 		return (2^cs);
123ad30f8e7SGabor Kovesdan 	case 3:
124ad30f8e7SGabor Kovesdan 		abort();
125ad30f8e7SGabor Kovesdan 		/*NOTREACHED*/
126ad30f8e7SGabor Kovesdan 	}
127ad30f8e7SGabor Kovesdan 	return (0);
128ad30f8e7SGabor Kovesdan }
129ad30f8e7SGabor Kovesdan 
130ad30f8e7SGabor Kovesdan static __inline void
131ad30f8e7SGabor Kovesdan /*ARGSUSED*/
132ad30f8e7SGabor Kovesdan _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei __unused,
133ad30f8e7SGabor Kovesdan     _EUCTWState * __restrict s)
134ad30f8e7SGabor Kovesdan {
135ad30f8e7SGabor Kovesdan 
136ad30f8e7SGabor Kovesdan 	memset(s, 0, sizeof(*s));
137ad30f8e7SGabor Kovesdan }
138ad30f8e7SGabor Kovesdan 
139ad30f8e7SGabor Kovesdan static __inline void
140ad30f8e7SGabor Kovesdan /*ARGSUSED*/
141ad30f8e7SGabor Kovesdan _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei __unused,
142ad30f8e7SGabor Kovesdan     void * __restrict pspriv, const _EUCTWState * __restrict s)
143ad30f8e7SGabor Kovesdan {
144ad30f8e7SGabor Kovesdan 
145ad30f8e7SGabor Kovesdan 	memcpy(pspriv, (const void *)s, sizeof(*s));
146ad30f8e7SGabor Kovesdan }
147ad30f8e7SGabor Kovesdan 
148ad30f8e7SGabor Kovesdan static __inline void
149ad30f8e7SGabor Kovesdan /*ARGSUSED*/
150ad30f8e7SGabor Kovesdan _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei __unused,
151ad30f8e7SGabor Kovesdan     _EUCTWState * __restrict s, const void * __restrict pspriv)
152ad30f8e7SGabor Kovesdan {
153ad30f8e7SGabor Kovesdan 
154ad30f8e7SGabor Kovesdan 	memcpy((void *)s, pspriv, sizeof(*s));
155ad30f8e7SGabor Kovesdan }
156ad30f8e7SGabor Kovesdan 
157ad30f8e7SGabor Kovesdan static int
158ad30f8e7SGabor Kovesdan /*ARGSUSED*/
159ad30f8e7SGabor Kovesdan _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,
160ad30f8e7SGabor Kovesdan     const void * __restrict var __unused, size_t lenvar __unused)
161ad30f8e7SGabor Kovesdan {
162ad30f8e7SGabor Kovesdan 
163ad30f8e7SGabor Kovesdan 	memset((void *)ei, 0, sizeof(*ei));
164ad30f8e7SGabor Kovesdan 
165ad30f8e7SGabor Kovesdan 	return (0);
166ad30f8e7SGabor Kovesdan }
167ad30f8e7SGabor Kovesdan 
168ad30f8e7SGabor Kovesdan static void
169ad30f8e7SGabor Kovesdan /*ARGSUSED*/
170ad30f8e7SGabor Kovesdan _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei __unused)
171ad30f8e7SGabor Kovesdan {
172ad30f8e7SGabor Kovesdan 
173ad30f8e7SGabor Kovesdan }
174ad30f8e7SGabor Kovesdan 
175ad30f8e7SGabor Kovesdan static int
176ad30f8e7SGabor Kovesdan _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,
177ad30f8e7SGabor Kovesdan     wchar_t * __restrict pwc, char ** __restrict s,
178ad30f8e7SGabor Kovesdan     size_t n, _EUCTWState * __restrict psenc, size_t * __restrict nresult)
179ad30f8e7SGabor Kovesdan {
180ad30f8e7SGabor Kovesdan 	char *s0;
181ad30f8e7SGabor Kovesdan 	wchar_t wchar;
182ad30f8e7SGabor Kovesdan 	int c, chlenbak, cs;
183ad30f8e7SGabor Kovesdan 
184ad30f8e7SGabor Kovesdan 	s0 = *s;
185ad30f8e7SGabor Kovesdan 
186ad30f8e7SGabor Kovesdan 	if (s0 == NULL) {
187ad30f8e7SGabor Kovesdan 		_citrus_EUCTW_init_state(ei, psenc);
188ad30f8e7SGabor Kovesdan 		*nresult = 0; /* state independent */
189ad30f8e7SGabor Kovesdan 		return (0);
190ad30f8e7SGabor Kovesdan 	}
191ad30f8e7SGabor Kovesdan 
192ad30f8e7SGabor Kovesdan 	chlenbak = psenc->chlen;
193ad30f8e7SGabor Kovesdan 
194ad30f8e7SGabor Kovesdan 	/* make sure we have the first byte in the buffer */
195ad30f8e7SGabor Kovesdan 	switch (psenc->chlen) {
196ad30f8e7SGabor Kovesdan 	case 0:
197ad30f8e7SGabor Kovesdan 		if (n < 1)
198ad30f8e7SGabor Kovesdan 			goto restart;
199ad30f8e7SGabor Kovesdan 		psenc->ch[0] = *s0++;
200ad30f8e7SGabor Kovesdan 		psenc->chlen = 1;
201ad30f8e7SGabor Kovesdan 		n--;
202ad30f8e7SGabor Kovesdan 		break;
203ad30f8e7SGabor Kovesdan 	case 1:
204ad30f8e7SGabor Kovesdan 	case 2:
205ad30f8e7SGabor Kovesdan 		break;
206ad30f8e7SGabor Kovesdan 	default:
207ad30f8e7SGabor Kovesdan 		/* illgeal state */
208ad30f8e7SGabor Kovesdan 		goto ilseq;
209ad30f8e7SGabor Kovesdan 	}
210ad30f8e7SGabor Kovesdan 
211ad30f8e7SGabor Kovesdan 	c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
212ad30f8e7SGabor Kovesdan 	if (c == 0)
213ad30f8e7SGabor Kovesdan 		goto ilseq;
214ad30f8e7SGabor Kovesdan 	while (psenc->chlen < c) {
215ad30f8e7SGabor Kovesdan 		if (n < 1)
216ad30f8e7SGabor Kovesdan 			goto ilseq;
217ad30f8e7SGabor Kovesdan 		psenc->ch[psenc->chlen] = *s0++;
218ad30f8e7SGabor Kovesdan 		psenc->chlen++;
219ad30f8e7SGabor Kovesdan 		n--;
220ad30f8e7SGabor Kovesdan 	}
221ad30f8e7SGabor Kovesdan 
222ad30f8e7SGabor Kovesdan 	wchar = 0;
223ad30f8e7SGabor Kovesdan 	switch (cs) {
224ad30f8e7SGabor Kovesdan 	case 0:
225ad30f8e7SGabor Kovesdan 		if (psenc->ch[0] & 0x80)
226ad30f8e7SGabor Kovesdan 			goto ilseq;
227ad30f8e7SGabor Kovesdan 		wchar = psenc->ch[0] & 0xff;
228ad30f8e7SGabor Kovesdan 		break;
229ad30f8e7SGabor Kovesdan 	case 1:
230ad30f8e7SGabor Kovesdan 		if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
231ad30f8e7SGabor Kovesdan 			goto ilseq;
232ad30f8e7SGabor Kovesdan 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
233ad30f8e7SGabor Kovesdan 		wchar |= 'G' << 24;
234ad30f8e7SGabor Kovesdan 		break;
235ad30f8e7SGabor Kovesdan 	case 2:
236ad30f8e7SGabor Kovesdan 		if ((unsigned char)psenc->ch[1] < 0xa1 ||
237ad30f8e7SGabor Kovesdan 		    0xa7 < (unsigned char)psenc->ch[1])
238ad30f8e7SGabor Kovesdan 			goto ilseq;
239ad30f8e7SGabor Kovesdan 		if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
240ad30f8e7SGabor Kovesdan 			goto ilseq;
241ad30f8e7SGabor Kovesdan 		wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
242ad30f8e7SGabor Kovesdan 		wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
243ad30f8e7SGabor Kovesdan 		break;
244ad30f8e7SGabor Kovesdan 	default:
245ad30f8e7SGabor Kovesdan 		goto ilseq;
246ad30f8e7SGabor Kovesdan 	}
247ad30f8e7SGabor Kovesdan 
248ad30f8e7SGabor Kovesdan 	*s = s0;
249ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
250ad30f8e7SGabor Kovesdan 
251ad30f8e7SGabor Kovesdan 	if (pwc)
252ad30f8e7SGabor Kovesdan 		*pwc = wchar;
253ad30f8e7SGabor Kovesdan 	*nresult = wchar ? c - chlenbak : 0;
254ad30f8e7SGabor Kovesdan 	return (0);
255ad30f8e7SGabor Kovesdan 
256ad30f8e7SGabor Kovesdan ilseq:
257ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
258ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
259ad30f8e7SGabor Kovesdan 	return (EILSEQ);
260ad30f8e7SGabor Kovesdan 
261ad30f8e7SGabor Kovesdan restart:
262ad30f8e7SGabor Kovesdan 	*s = s0;
263ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
264ad30f8e7SGabor Kovesdan 	return (0);
265ad30f8e7SGabor Kovesdan }
266ad30f8e7SGabor Kovesdan 
267ad30f8e7SGabor Kovesdan static int
268ad30f8e7SGabor Kovesdan _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei __unused,
269ad30f8e7SGabor Kovesdan     char * __restrict s, size_t n, wchar_t wc,
270ad30f8e7SGabor Kovesdan     _EUCTWState * __restrict psenc __unused, size_t * __restrict nresult)
271ad30f8e7SGabor Kovesdan {
272ad30f8e7SGabor Kovesdan 	wchar_t cs, v;
273ad30f8e7SGabor Kovesdan 	int clen, i, ret;
274ad30f8e7SGabor Kovesdan 	size_t len;
275ad30f8e7SGabor Kovesdan 
276ad30f8e7SGabor Kovesdan 	cs = wc & 0x7f000080;
277ad30f8e7SGabor Kovesdan 	clen = 1;
278ad30f8e7SGabor Kovesdan 	if (wc & 0x00007f00)
279ad30f8e7SGabor Kovesdan 		clen = 2;
280ad30f8e7SGabor Kovesdan 	if ((wc & 0x007f0000) && !(wc & 0x00800000))
281ad30f8e7SGabor Kovesdan 		clen = 3;
282ad30f8e7SGabor Kovesdan 
283ad30f8e7SGabor Kovesdan 	if (clen == 1 && cs == 0x00000000) {
284ad30f8e7SGabor Kovesdan 		/* ASCII */
285ad30f8e7SGabor Kovesdan 		len = 1;
286ad30f8e7SGabor Kovesdan 		if (n < len) {
287ad30f8e7SGabor Kovesdan 			ret = E2BIG;
288ad30f8e7SGabor Kovesdan 			goto err;
289ad30f8e7SGabor Kovesdan 		}
290ad30f8e7SGabor Kovesdan 		v = wc & 0x0000007f;
291ad30f8e7SGabor Kovesdan 	} else if (clen == 2 && cs == ('G' << 24)) {
292ad30f8e7SGabor Kovesdan 		/* CNS-11643-1 */
293ad30f8e7SGabor Kovesdan 		len = 2;
294ad30f8e7SGabor Kovesdan 		if (n < len) {
295ad30f8e7SGabor Kovesdan 			ret = E2BIG;
296ad30f8e7SGabor Kovesdan 			goto err;
297ad30f8e7SGabor Kovesdan 		}
298ad30f8e7SGabor Kovesdan 		v = wc & 0x00007f7f;
299ad30f8e7SGabor Kovesdan 		v |= 0x00008080;
300ad30f8e7SGabor Kovesdan 	} else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
301ad30f8e7SGabor Kovesdan 		/* CNS-11643-[2-7] */
302ad30f8e7SGabor Kovesdan 		len = 4;
303ad30f8e7SGabor Kovesdan 		if (n < len) {
304ad30f8e7SGabor Kovesdan 			ret = E2BIG;
305ad30f8e7SGabor Kovesdan 			goto err;
306ad30f8e7SGabor Kovesdan 		}
307ad30f8e7SGabor Kovesdan 		*s++ = _SS2;
308ad30f8e7SGabor Kovesdan 		*s++ = (cs >> 24) - 'H' + 0xa2;
309ad30f8e7SGabor Kovesdan 		v = wc & 0x00007f7f;
310ad30f8e7SGabor Kovesdan 		v |= 0x00008080;
311ad30f8e7SGabor Kovesdan 	} else {
312ad30f8e7SGabor Kovesdan 		ret = EILSEQ;
313ad30f8e7SGabor Kovesdan 		goto err;
314ad30f8e7SGabor Kovesdan 	}
315ad30f8e7SGabor Kovesdan 
316ad30f8e7SGabor Kovesdan 	i = clen;
317ad30f8e7SGabor Kovesdan 	while (i-- > 0)
318ad30f8e7SGabor Kovesdan 		*s++ = (v >> (i << 3)) & 0xff;
319ad30f8e7SGabor Kovesdan 
320ad30f8e7SGabor Kovesdan 	*nresult = len;
321ad30f8e7SGabor Kovesdan 	return (0);
322ad30f8e7SGabor Kovesdan 
323ad30f8e7SGabor Kovesdan err:
324ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
325ad30f8e7SGabor Kovesdan 	return (ret);
326ad30f8e7SGabor Kovesdan }
327ad30f8e7SGabor Kovesdan 
328ad30f8e7SGabor Kovesdan static __inline int
329ad30f8e7SGabor Kovesdan /*ARGSUSED*/
330ad30f8e7SGabor Kovesdan _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei __unused,
331ad30f8e7SGabor Kovesdan     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
332ad30f8e7SGabor Kovesdan {
333ad30f8e7SGabor Kovesdan 
334ad30f8e7SGabor Kovesdan 	*csid = (_csid_t)(wc >> 24) & 0xFF;
335ad30f8e7SGabor Kovesdan 	*idx  = (_index_t)(wc & 0x7F7F);
336ad30f8e7SGabor Kovesdan 
337ad30f8e7SGabor Kovesdan 	return (0);
338ad30f8e7SGabor Kovesdan }
339ad30f8e7SGabor Kovesdan 
340ad30f8e7SGabor Kovesdan static __inline int
341ad30f8e7SGabor Kovesdan /*ARGSUSED*/
342ad30f8e7SGabor Kovesdan _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei __unused,
343ad30f8e7SGabor Kovesdan     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
344ad30f8e7SGabor Kovesdan {
345ad30f8e7SGabor Kovesdan 
346ad30f8e7SGabor Kovesdan 	if (csid == 0) {
347ad30f8e7SGabor Kovesdan 		if ((idx & ~0x7F) != 0)
348ad30f8e7SGabor Kovesdan 			return (EINVAL);
349ad30f8e7SGabor Kovesdan 		*wc = (wchar_t)idx;
350ad30f8e7SGabor Kovesdan 	} else {
351ad30f8e7SGabor Kovesdan 		if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0)
352ad30f8e7SGabor Kovesdan 			return (EINVAL);
353ad30f8e7SGabor Kovesdan 		*wc = (wchar_t)idx | ((wchar_t)csid << 24);
354ad30f8e7SGabor Kovesdan 	}
355ad30f8e7SGabor Kovesdan 
356ad30f8e7SGabor Kovesdan 	return (0);
357ad30f8e7SGabor Kovesdan }
358ad30f8e7SGabor Kovesdan 
359ad30f8e7SGabor Kovesdan static __inline int
360ad30f8e7SGabor Kovesdan /*ARGSUSED*/
361ad30f8e7SGabor Kovesdan _citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei __unused,
362ad30f8e7SGabor Kovesdan     _EUCTWState * __restrict psenc, int * __restrict rstate)
363ad30f8e7SGabor Kovesdan {
364ad30f8e7SGabor Kovesdan 
365ad30f8e7SGabor Kovesdan 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
366ad30f8e7SGabor Kovesdan 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
367ad30f8e7SGabor Kovesdan 	return (0);
368ad30f8e7SGabor Kovesdan }
369ad30f8e7SGabor Kovesdan 
370ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
371ad30f8e7SGabor Kovesdan  * public interface for stdenc
372ad30f8e7SGabor Kovesdan  */
373ad30f8e7SGabor Kovesdan 
374ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(EUCTW);
375ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(EUCTW);
376ad30f8e7SGabor Kovesdan 
377ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
378