1ad30f8e7SGabor Kovesdan /*	$NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $	*/
2ad30f8e7SGabor Kovesdan 
3ad30f8e7SGabor Kovesdan /*-
45e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
55e53a4f9SPedro F. Giffuni  *
6ad30f8e7SGabor Kovesdan  * Copyright (c)2002 Citrus Project,
7ad30f8e7SGabor Kovesdan  * All rights reserved.
8ad30f8e7SGabor Kovesdan  *
9ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan  * are met:
12ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan  *
18ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan  */
30ad30f8e7SGabor Kovesdan 
31ad30f8e7SGabor Kovesdan /*-
32ad30f8e7SGabor Kovesdan  * Copyright (c)1999 Citrus Project,
33ad30f8e7SGabor Kovesdan  * All rights reserved.
34ad30f8e7SGabor Kovesdan  *
35ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
36ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
37ad30f8e7SGabor Kovesdan  * are met:
38ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
39ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
40ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
41ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
42ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
43ad30f8e7SGabor Kovesdan  *
44ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
55ad30f8e7SGabor Kovesdan  *
56ad30f8e7SGabor Kovesdan  *	$Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
57ad30f8e7SGabor Kovesdan  */
58ad30f8e7SGabor Kovesdan 
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
_citrus_EUCTW_cs(unsigned int c)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
_citrus_EUCTW_count(int cs)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:
1229ca40936STijl Coosemans 		return (1 << 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*/
_citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei __unused,_EUCTWState * __restrict s)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 
139b61949ddSDimitry Andric #if 0
140ad30f8e7SGabor Kovesdan static __inline void
141ad30f8e7SGabor Kovesdan /*ARGSUSED*/
142ad30f8e7SGabor Kovesdan _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei __unused,
143ad30f8e7SGabor Kovesdan     void * __restrict pspriv, const _EUCTWState * __restrict s)
144ad30f8e7SGabor Kovesdan {
145ad30f8e7SGabor Kovesdan 
146ad30f8e7SGabor Kovesdan 	memcpy(pspriv, (const void *)s, sizeof(*s));
147ad30f8e7SGabor Kovesdan }
148ad30f8e7SGabor Kovesdan 
149ad30f8e7SGabor Kovesdan static __inline void
150ad30f8e7SGabor Kovesdan /*ARGSUSED*/
151ad30f8e7SGabor Kovesdan _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei __unused,
152ad30f8e7SGabor Kovesdan     _EUCTWState * __restrict s, const void * __restrict pspriv)
153ad30f8e7SGabor Kovesdan {
154ad30f8e7SGabor Kovesdan 
155ad30f8e7SGabor Kovesdan 	memcpy((void *)s, pspriv, sizeof(*s));
156ad30f8e7SGabor Kovesdan }
157b61949ddSDimitry Andric #endif
158ad30f8e7SGabor Kovesdan 
159ad30f8e7SGabor Kovesdan static int
160ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,const void * __restrict var __unused,size_t lenvar __unused)161ad30f8e7SGabor Kovesdan _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,
162ad30f8e7SGabor Kovesdan     const void * __restrict var __unused, size_t lenvar __unused)
163ad30f8e7SGabor Kovesdan {
164ad30f8e7SGabor Kovesdan 
165ad30f8e7SGabor Kovesdan 	memset((void *)ei, 0, sizeof(*ei));
166ad30f8e7SGabor Kovesdan 
167ad30f8e7SGabor Kovesdan 	return (0);
168ad30f8e7SGabor Kovesdan }
169ad30f8e7SGabor Kovesdan 
170ad30f8e7SGabor Kovesdan static void
171ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo * ei __unused)172ad30f8e7SGabor Kovesdan _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei __unused)
173ad30f8e7SGabor Kovesdan {
174ad30f8e7SGabor Kovesdan 
175ad30f8e7SGabor Kovesdan }
176ad30f8e7SGabor Kovesdan 
177ad30f8e7SGabor Kovesdan static int
_citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,wchar_t * __restrict pwc,char ** __restrict s,size_t n,_EUCTWState * __restrict psenc,size_t * __restrict nresult)178ad30f8e7SGabor Kovesdan _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,
1791243a98eSTijl Coosemans     wchar_t * __restrict pwc, char ** __restrict s,
180ad30f8e7SGabor Kovesdan     size_t n, _EUCTWState * __restrict psenc, size_t * __restrict nresult)
181ad30f8e7SGabor Kovesdan {
1821243a98eSTijl Coosemans 	char *s0;
183ad30f8e7SGabor Kovesdan 	wchar_t wchar;
184ad30f8e7SGabor Kovesdan 	int c, chlenbak, cs;
185ad30f8e7SGabor Kovesdan 
186ad30f8e7SGabor Kovesdan 	s0 = *s;
187ad30f8e7SGabor Kovesdan 
188ad30f8e7SGabor Kovesdan 	if (s0 == NULL) {
189ad30f8e7SGabor Kovesdan 		_citrus_EUCTW_init_state(ei, psenc);
190ad30f8e7SGabor Kovesdan 		*nresult = 0; /* state independent */
191ad30f8e7SGabor Kovesdan 		return (0);
192ad30f8e7SGabor Kovesdan 	}
193ad30f8e7SGabor Kovesdan 
194ad30f8e7SGabor Kovesdan 	chlenbak = psenc->chlen;
195ad30f8e7SGabor Kovesdan 
196ad30f8e7SGabor Kovesdan 	/* make sure we have the first byte in the buffer */
197ad30f8e7SGabor Kovesdan 	switch (psenc->chlen) {
198ad30f8e7SGabor Kovesdan 	case 0:
199ad30f8e7SGabor Kovesdan 		if (n < 1)
200ad30f8e7SGabor Kovesdan 			goto restart;
201ad30f8e7SGabor Kovesdan 		psenc->ch[0] = *s0++;
202ad30f8e7SGabor Kovesdan 		psenc->chlen = 1;
203ad30f8e7SGabor Kovesdan 		n--;
204ad30f8e7SGabor Kovesdan 		break;
205ad30f8e7SGabor Kovesdan 	case 1:
206ad30f8e7SGabor Kovesdan 	case 2:
207ad30f8e7SGabor Kovesdan 		break;
208ad30f8e7SGabor Kovesdan 	default:
209ad30f8e7SGabor Kovesdan 		/* illgeal state */
210ad30f8e7SGabor Kovesdan 		goto ilseq;
211ad30f8e7SGabor Kovesdan 	}
212ad30f8e7SGabor Kovesdan 
213ad30f8e7SGabor Kovesdan 	c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
214ad30f8e7SGabor Kovesdan 	if (c == 0)
215ad30f8e7SGabor Kovesdan 		goto ilseq;
216ad30f8e7SGabor Kovesdan 	while (psenc->chlen < c) {
217ad30f8e7SGabor Kovesdan 		if (n < 1)
218ad30f8e7SGabor Kovesdan 			goto ilseq;
219ad30f8e7SGabor Kovesdan 		psenc->ch[psenc->chlen] = *s0++;
220ad30f8e7SGabor Kovesdan 		psenc->chlen++;
221ad30f8e7SGabor Kovesdan 		n--;
222ad30f8e7SGabor Kovesdan 	}
223ad30f8e7SGabor Kovesdan 
224ad30f8e7SGabor Kovesdan 	wchar = 0;
225ad30f8e7SGabor Kovesdan 	switch (cs) {
226ad30f8e7SGabor Kovesdan 	case 0:
227ad30f8e7SGabor Kovesdan 		if (psenc->ch[0] & 0x80)
228ad30f8e7SGabor Kovesdan 			goto ilseq;
229ad30f8e7SGabor Kovesdan 		wchar = psenc->ch[0] & 0xff;
230ad30f8e7SGabor Kovesdan 		break;
231ad30f8e7SGabor Kovesdan 	case 1:
232ad30f8e7SGabor Kovesdan 		if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
233ad30f8e7SGabor Kovesdan 			goto ilseq;
234ad30f8e7SGabor Kovesdan 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
235ad30f8e7SGabor Kovesdan 		wchar |= 'G' << 24;
236ad30f8e7SGabor Kovesdan 		break;
237ad30f8e7SGabor Kovesdan 	case 2:
238ad30f8e7SGabor Kovesdan 		if ((unsigned char)psenc->ch[1] < 0xa1 ||
239ad30f8e7SGabor Kovesdan 		    0xa7 < (unsigned char)psenc->ch[1])
240ad30f8e7SGabor Kovesdan 			goto ilseq;
241ad30f8e7SGabor Kovesdan 		if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
242ad30f8e7SGabor Kovesdan 			goto ilseq;
243ad30f8e7SGabor Kovesdan 		wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
244ad30f8e7SGabor Kovesdan 		wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
245ad30f8e7SGabor Kovesdan 		break;
246ad30f8e7SGabor Kovesdan 	default:
247ad30f8e7SGabor Kovesdan 		goto ilseq;
248ad30f8e7SGabor Kovesdan 	}
249ad30f8e7SGabor Kovesdan 
250ad30f8e7SGabor Kovesdan 	*s = s0;
251ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
252ad30f8e7SGabor Kovesdan 
253ad30f8e7SGabor Kovesdan 	if (pwc)
254ad30f8e7SGabor Kovesdan 		*pwc = wchar;
255ad30f8e7SGabor Kovesdan 	*nresult = wchar ? c - chlenbak : 0;
256ad30f8e7SGabor Kovesdan 	return (0);
257ad30f8e7SGabor Kovesdan 
258ad30f8e7SGabor Kovesdan ilseq:
259ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
260ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
261ad30f8e7SGabor Kovesdan 	return (EILSEQ);
262ad30f8e7SGabor Kovesdan 
263ad30f8e7SGabor Kovesdan restart:
264ad30f8e7SGabor Kovesdan 	*s = s0;
265ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
266ad30f8e7SGabor Kovesdan 	return (0);
267ad30f8e7SGabor Kovesdan }
268ad30f8e7SGabor Kovesdan 
269ad30f8e7SGabor Kovesdan static int
_citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei __unused,char * __restrict s,size_t n,wchar_t wc,_EUCTWState * __restrict psenc __unused,size_t * __restrict nresult)270ad30f8e7SGabor Kovesdan _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei __unused,
271ad30f8e7SGabor Kovesdan     char * __restrict s, size_t n, wchar_t wc,
272ad30f8e7SGabor Kovesdan     _EUCTWState * __restrict psenc __unused, size_t * __restrict nresult)
273ad30f8e7SGabor Kovesdan {
274ad30f8e7SGabor Kovesdan 	wchar_t cs, v;
275ad30f8e7SGabor Kovesdan 	int clen, i, ret;
276ad30f8e7SGabor Kovesdan 	size_t len;
277ad30f8e7SGabor Kovesdan 
278ad30f8e7SGabor Kovesdan 	cs = wc & 0x7f000080;
279ad30f8e7SGabor Kovesdan 	clen = 1;
280ad30f8e7SGabor Kovesdan 	if (wc & 0x00007f00)
281ad30f8e7SGabor Kovesdan 		clen = 2;
282ad30f8e7SGabor Kovesdan 	if ((wc & 0x007f0000) && !(wc & 0x00800000))
283ad30f8e7SGabor Kovesdan 		clen = 3;
284ad30f8e7SGabor Kovesdan 
285ad30f8e7SGabor Kovesdan 	if (clen == 1 && cs == 0x00000000) {
286ad30f8e7SGabor Kovesdan 		/* ASCII */
287ad30f8e7SGabor Kovesdan 		len = 1;
288ad30f8e7SGabor Kovesdan 		if (n < len) {
289ad30f8e7SGabor Kovesdan 			ret = E2BIG;
290ad30f8e7SGabor Kovesdan 			goto err;
291ad30f8e7SGabor Kovesdan 		}
292ad30f8e7SGabor Kovesdan 		v = wc & 0x0000007f;
293ad30f8e7SGabor Kovesdan 	} else if (clen == 2 && cs == ('G' << 24)) {
294ad30f8e7SGabor Kovesdan 		/* CNS-11643-1 */
295ad30f8e7SGabor Kovesdan 		len = 2;
296ad30f8e7SGabor Kovesdan 		if (n < len) {
297ad30f8e7SGabor Kovesdan 			ret = E2BIG;
298ad30f8e7SGabor Kovesdan 			goto err;
299ad30f8e7SGabor Kovesdan 		}
300ad30f8e7SGabor Kovesdan 		v = wc & 0x00007f7f;
301ad30f8e7SGabor Kovesdan 		v |= 0x00008080;
302ad30f8e7SGabor Kovesdan 	} else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
303ad30f8e7SGabor Kovesdan 		/* CNS-11643-[2-7] */
304ad30f8e7SGabor Kovesdan 		len = 4;
305ad30f8e7SGabor Kovesdan 		if (n < len) {
306ad30f8e7SGabor Kovesdan 			ret = E2BIG;
307ad30f8e7SGabor Kovesdan 			goto err;
308ad30f8e7SGabor Kovesdan 		}
309ad30f8e7SGabor Kovesdan 		*s++ = _SS2;
310ad30f8e7SGabor Kovesdan 		*s++ = (cs >> 24) - 'H' + 0xa2;
311ad30f8e7SGabor Kovesdan 		v = wc & 0x00007f7f;
312ad30f8e7SGabor Kovesdan 		v |= 0x00008080;
313ad30f8e7SGabor Kovesdan 	} else {
314ad30f8e7SGabor Kovesdan 		ret = EILSEQ;
315ad30f8e7SGabor Kovesdan 		goto err;
316ad30f8e7SGabor Kovesdan 	}
317ad30f8e7SGabor Kovesdan 
318ad30f8e7SGabor Kovesdan 	i = clen;
319ad30f8e7SGabor Kovesdan 	while (i-- > 0)
320ad30f8e7SGabor Kovesdan 		*s++ = (v >> (i << 3)) & 0xff;
321ad30f8e7SGabor Kovesdan 
322ad30f8e7SGabor Kovesdan 	*nresult = len;
323ad30f8e7SGabor Kovesdan 	return (0);
324ad30f8e7SGabor Kovesdan 
325ad30f8e7SGabor Kovesdan err:
326ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
327ad30f8e7SGabor Kovesdan 	return (ret);
328ad30f8e7SGabor Kovesdan }
329ad30f8e7SGabor Kovesdan 
330ad30f8e7SGabor Kovesdan static __inline int
331ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei __unused,_csid_t * __restrict csid,_index_t * __restrict idx,wchar_t wc)332ad30f8e7SGabor Kovesdan _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei __unused,
333ad30f8e7SGabor Kovesdan     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
334ad30f8e7SGabor Kovesdan {
335ad30f8e7SGabor Kovesdan 
336ad30f8e7SGabor Kovesdan 	*csid = (_csid_t)(wc >> 24) & 0xFF;
337ad30f8e7SGabor Kovesdan 	*idx  = (_index_t)(wc & 0x7F7F);
338ad30f8e7SGabor Kovesdan 
339ad30f8e7SGabor Kovesdan 	return (0);
340ad30f8e7SGabor Kovesdan }
341ad30f8e7SGabor Kovesdan 
342ad30f8e7SGabor Kovesdan static __inline int
343ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei __unused,wchar_t * __restrict wc,_csid_t csid,_index_t idx)344ad30f8e7SGabor Kovesdan _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei __unused,
345ad30f8e7SGabor Kovesdan     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
346ad30f8e7SGabor Kovesdan {
347ad30f8e7SGabor Kovesdan 
348ad30f8e7SGabor Kovesdan 	if (csid == 0) {
349ad30f8e7SGabor Kovesdan 		if ((idx & ~0x7F) != 0)
350ad30f8e7SGabor Kovesdan 			return (EINVAL);
351ad30f8e7SGabor Kovesdan 		*wc = (wchar_t)idx;
352ad30f8e7SGabor Kovesdan 	} else {
353ad30f8e7SGabor Kovesdan 		if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0)
354ad30f8e7SGabor Kovesdan 			return (EINVAL);
355ad30f8e7SGabor Kovesdan 		*wc = (wchar_t)idx | ((wchar_t)csid << 24);
356ad30f8e7SGabor Kovesdan 	}
357ad30f8e7SGabor Kovesdan 
358ad30f8e7SGabor Kovesdan 	return (0);
359ad30f8e7SGabor Kovesdan }
360ad30f8e7SGabor Kovesdan 
361ad30f8e7SGabor Kovesdan static __inline int
362ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei __unused,_EUCTWState * __restrict psenc,int * __restrict rstate)363ad30f8e7SGabor Kovesdan _citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei __unused,
364ad30f8e7SGabor Kovesdan     _EUCTWState * __restrict psenc, int * __restrict rstate)
365ad30f8e7SGabor Kovesdan {
366ad30f8e7SGabor Kovesdan 
367ad30f8e7SGabor Kovesdan 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
368ad30f8e7SGabor Kovesdan 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
369ad30f8e7SGabor Kovesdan 	return (0);
370ad30f8e7SGabor Kovesdan }
371ad30f8e7SGabor Kovesdan 
372ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
373ad30f8e7SGabor Kovesdan  * public interface for stdenc
374ad30f8e7SGabor Kovesdan  */
375ad30f8e7SGabor Kovesdan 
376ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(EUCTW);
377ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(EUCTW);
378ad30f8e7SGabor Kovesdan 
379ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
380