1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/*	$NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $	*/
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c)2002 Citrus Project,
6219019Sgabor * All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor * 1. Redistributions of source code must retain the above copyright
12219019Sgabor *    notice, this list of conditions and the following disclaimer.
13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14219019Sgabor *    notice, this list of conditions and the following disclaimer in the
15219019Sgabor *    documentation and/or other materials provided with the distribution.
16219019Sgabor *
17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219019Sgabor * SUCH DAMAGE.
28219019Sgabor */
29219019Sgabor
30219019Sgabor/*-
31219019Sgabor * Copyright (c)1999 Citrus Project,
32219019Sgabor * All rights reserved.
33219019Sgabor *
34219019Sgabor * Redistribution and use in source and binary forms, with or without
35219019Sgabor * modification, are permitted provided that the following conditions
36219019Sgabor * are met:
37219019Sgabor * 1. Redistributions of source code must retain the above copyright
38219019Sgabor *    notice, this list of conditions and the following disclaimer.
39219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
40219019Sgabor *    notice, this list of conditions and the following disclaimer in the
41219019Sgabor *    documentation and/or other materials provided with the distribution.
42219019Sgabor *
43219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53219019Sgabor * SUCH DAMAGE.
54219019Sgabor *
55219019Sgabor *	$Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
56219019Sgabor */
57219019Sgabor
58219019Sgabor#include <sys/cdefs.h>
59219019Sgabor#include <sys/types.h>
60219019Sgabor
61219019Sgabor#include <assert.h>
62219019Sgabor#include <errno.h>
63219019Sgabor#include <limits.h>
64219019Sgabor#include <stddef.h>
65219019Sgabor#include <stdio.h>
66219019Sgabor#include <stdlib.h>
67219019Sgabor#include <string.h>
68219019Sgabor#include <wchar.h>
69219019Sgabor
70219019Sgabor#include "citrus_namespace.h"
71219019Sgabor#include "citrus_types.h"
72219019Sgabor#include "citrus_module.h"
73219019Sgabor#include "citrus_stdenc.h"
74219019Sgabor#include "citrus_euctw.h"
75219019Sgabor
76219019Sgabor
77219019Sgabor/* ----------------------------------------------------------------------
78219019Sgabor * private stuffs used by templates
79219019Sgabor */
80219019Sgabor
81219019Sgabortypedef struct {
82219019Sgabor	int	 chlen;
83219019Sgabor	char	 ch[4];
84219019Sgabor} _EUCTWState;
85219019Sgabor
86219019Sgabortypedef struct {
87219019Sgabor	int	 dummy;
88219019Sgabor} _EUCTWEncodingInfo;
89219019Sgabor
90219019Sgabor#define	_SS2	0x008e
91219019Sgabor#define	_SS3	0x008f
92219019Sgabor
93219019Sgabor#define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
94219019Sgabor#define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
95219019Sgabor
96219019Sgabor#define _FUNCNAME(m)			_citrus_EUCTW_##m
97219019Sgabor#define _ENCODING_INFO			_EUCTWEncodingInfo
98219019Sgabor#define _ENCODING_STATE			_EUCTWState
99219019Sgabor#define _ENCODING_MB_CUR_MAX(_ei_)	4
100219019Sgabor#define _ENCODING_IS_STATE_DEPENDENT	0
101219019Sgabor#define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
102219019Sgabor
103219019Sgaborstatic __inline int
104219019Sgabor_citrus_EUCTW_cs(unsigned int c)
105219019Sgabor{
106219019Sgabor
107219019Sgabor	c &= 0xff;
108219019Sgabor
109219019Sgabor	return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
110219019Sgabor}
111219019Sgabor
112219019Sgaborstatic __inline int
113219019Sgabor_citrus_EUCTW_count(int cs)
114219019Sgabor{
115219019Sgabor
116219019Sgabor	switch (cs) {
117219019Sgabor	case 0:
118219019Sgabor		/*FALLTHROUGH*/
119219019Sgabor	case 1:
120219019Sgabor		/*FALLTHROUGH*/
121219019Sgabor	case 2:
122219019Sgabor		return (2^cs);
123219019Sgabor	case 3:
124219019Sgabor		abort();
125219019Sgabor		/*NOTREACHED*/
126219019Sgabor	}
127219019Sgabor	return (0);
128219019Sgabor}
129219019Sgabor
130219019Sgaborstatic __inline void
131219019Sgabor/*ARGSUSED*/
132219019Sgabor_citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei __unused,
133219019Sgabor    _EUCTWState * __restrict s)
134219019Sgabor{
135219019Sgabor
136219019Sgabor	memset(s, 0, sizeof(*s));
137219019Sgabor}
138219019Sgabor
139219019Sgaborstatic __inline void
140219019Sgabor/*ARGSUSED*/
141219019Sgabor_citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei __unused,
142219019Sgabor    void * __restrict pspriv, const _EUCTWState * __restrict s)
143219019Sgabor{
144219019Sgabor
145219019Sgabor	memcpy(pspriv, (const void *)s, sizeof(*s));
146219019Sgabor}
147219019Sgabor
148219019Sgaborstatic __inline void
149219019Sgabor/*ARGSUSED*/
150219019Sgabor_citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei __unused,
151219019Sgabor    _EUCTWState * __restrict s, const void * __restrict pspriv)
152219019Sgabor{
153219019Sgabor
154219019Sgabor	memcpy((void *)s, pspriv, sizeof(*s));
155219019Sgabor}
156219019Sgabor
157219019Sgaborstatic int
158219019Sgabor/*ARGSUSED*/
159219019Sgabor_citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,
160219019Sgabor    const void * __restrict var __unused, size_t lenvar __unused)
161219019Sgabor{
162219019Sgabor
163219019Sgabor	memset((void *)ei, 0, sizeof(*ei));
164219019Sgabor
165219019Sgabor	return (0);
166219019Sgabor}
167219019Sgabor
168219019Sgaborstatic void
169219019Sgabor/*ARGSUSED*/
170219019Sgabor_citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei __unused)
171219019Sgabor{
172219019Sgabor
173219019Sgabor}
174219019Sgabor
175219019Sgaborstatic int
176219019Sgabor_citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,
177252583Speter    wchar_t * __restrict pwc, const char ** __restrict s,
178219019Sgabor    size_t n, _EUCTWState * __restrict psenc, size_t * __restrict nresult)
179219019Sgabor{
180252583Speter	const char *s0;
181219019Sgabor	wchar_t wchar;
182219019Sgabor	int c, chlenbak, cs;
183219019Sgabor
184219019Sgabor	s0 = *s;
185219019Sgabor
186219019Sgabor	if (s0 == NULL) {
187219019Sgabor		_citrus_EUCTW_init_state(ei, psenc);
188219019Sgabor		*nresult = 0; /* state independent */
189219019Sgabor		return (0);
190219019Sgabor	}
191219019Sgabor
192219019Sgabor	chlenbak = psenc->chlen;
193219019Sgabor
194219019Sgabor	/* make sure we have the first byte in the buffer */
195219019Sgabor	switch (psenc->chlen) {
196219019Sgabor	case 0:
197219019Sgabor		if (n < 1)
198219019Sgabor			goto restart;
199219019Sgabor		psenc->ch[0] = *s0++;
200219019Sgabor		psenc->chlen = 1;
201219019Sgabor		n--;
202219019Sgabor		break;
203219019Sgabor	case 1:
204219019Sgabor	case 2:
205219019Sgabor		break;
206219019Sgabor	default:
207219019Sgabor		/* illgeal state */
208219019Sgabor		goto ilseq;
209219019Sgabor	}
210219019Sgabor
211219019Sgabor	c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
212219019Sgabor	if (c == 0)
213219019Sgabor		goto ilseq;
214219019Sgabor	while (psenc->chlen < c) {
215219019Sgabor		if (n < 1)
216219019Sgabor			goto ilseq;
217219019Sgabor		psenc->ch[psenc->chlen] = *s0++;
218219019Sgabor		psenc->chlen++;
219219019Sgabor		n--;
220219019Sgabor	}
221219019Sgabor
222219019Sgabor	wchar = 0;
223219019Sgabor	switch (cs) {
224219019Sgabor	case 0:
225219019Sgabor		if (psenc->ch[0] & 0x80)
226219019Sgabor			goto ilseq;
227219019Sgabor		wchar = psenc->ch[0] & 0xff;
228219019Sgabor		break;
229219019Sgabor	case 1:
230219019Sgabor		if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
231219019Sgabor			goto ilseq;
232219019Sgabor		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
233219019Sgabor		wchar |= 'G' << 24;
234219019Sgabor		break;
235219019Sgabor	case 2:
236219019Sgabor		if ((unsigned char)psenc->ch[1] < 0xa1 ||
237219019Sgabor		    0xa7 < (unsigned char)psenc->ch[1])
238219019Sgabor			goto ilseq;
239219019Sgabor		if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
240219019Sgabor			goto ilseq;
241219019Sgabor		wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
242219019Sgabor		wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
243219019Sgabor		break;
244219019Sgabor	default:
245219019Sgabor		goto ilseq;
246219019Sgabor	}
247219019Sgabor
248219019Sgabor	*s = s0;
249219019Sgabor	psenc->chlen = 0;
250219019Sgabor
251219019Sgabor	if (pwc)
252219019Sgabor		*pwc = wchar;
253219019Sgabor	*nresult = wchar ? c - chlenbak : 0;
254219019Sgabor	return (0);
255219019Sgabor
256219019Sgaborilseq:
257219019Sgabor	psenc->chlen = 0;
258219019Sgabor	*nresult = (size_t)-1;
259219019Sgabor	return (EILSEQ);
260219019Sgabor
261219019Sgaborrestart:
262219019Sgabor	*s = s0;
263219019Sgabor	*nresult = (size_t)-1;
264219019Sgabor	return (0);
265219019Sgabor}
266219019Sgabor
267219019Sgaborstatic int
268219019Sgabor_citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei __unused,
269219019Sgabor    char * __restrict s, size_t n, wchar_t wc,
270219019Sgabor    _EUCTWState * __restrict psenc __unused, size_t * __restrict nresult)
271219019Sgabor{
272219019Sgabor	wchar_t cs, v;
273219019Sgabor	int clen, i, ret;
274219019Sgabor	size_t len;
275219019Sgabor
276219019Sgabor	cs = wc & 0x7f000080;
277219019Sgabor	clen = 1;
278219019Sgabor	if (wc & 0x00007f00)
279219019Sgabor		clen = 2;
280219019Sgabor	if ((wc & 0x007f0000) && !(wc & 0x00800000))
281219019Sgabor		clen = 3;
282219019Sgabor
283219019Sgabor	if (clen == 1 && cs == 0x00000000) {
284219019Sgabor		/* ASCII */
285219019Sgabor		len = 1;
286219019Sgabor		if (n < len) {
287219019Sgabor			ret = E2BIG;
288219019Sgabor			goto err;
289219019Sgabor		}
290219019Sgabor		v = wc & 0x0000007f;
291219019Sgabor	} else if (clen == 2 && cs == ('G' << 24)) {
292219019Sgabor		/* CNS-11643-1 */
293219019Sgabor		len = 2;
294219019Sgabor		if (n < len) {
295219019Sgabor			ret = E2BIG;
296219019Sgabor			goto err;
297219019Sgabor		}
298219019Sgabor		v = wc & 0x00007f7f;
299219019Sgabor		v |= 0x00008080;
300219019Sgabor	} else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
301219019Sgabor		/* CNS-11643-[2-7] */
302219019Sgabor		len = 4;
303219019Sgabor		if (n < len) {
304219019Sgabor			ret = E2BIG;
305219019Sgabor			goto err;
306219019Sgabor		}
307219019Sgabor		*s++ = _SS2;
308219019Sgabor		*s++ = (cs >> 24) - 'H' + 0xa2;
309219019Sgabor		v = wc & 0x00007f7f;
310219019Sgabor		v |= 0x00008080;
311219019Sgabor	} else {
312219019Sgabor		ret = EILSEQ;
313219019Sgabor		goto err;
314219019Sgabor	}
315219019Sgabor
316219019Sgabor	i = clen;
317219019Sgabor	while (i-- > 0)
318219019Sgabor		*s++ = (v >> (i << 3)) & 0xff;
319219019Sgabor
320219019Sgabor	*nresult = len;
321219019Sgabor	return (0);
322219019Sgabor
323219019Sgaborerr:
324219019Sgabor	*nresult = (size_t)-1;
325219019Sgabor	return (ret);
326219019Sgabor}
327219019Sgabor
328219019Sgaborstatic __inline int
329219019Sgabor/*ARGSUSED*/
330219019Sgabor_citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei __unused,
331219019Sgabor    _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
332219019Sgabor{
333219019Sgabor
334219019Sgabor	*csid = (_csid_t)(wc >> 24) & 0xFF;
335219019Sgabor	*idx  = (_index_t)(wc & 0x7F7F);
336219019Sgabor
337219019Sgabor	return (0);
338219019Sgabor}
339219019Sgabor
340219019Sgaborstatic __inline int
341219019Sgabor/*ARGSUSED*/
342219019Sgabor_citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei __unused,
343219019Sgabor    wchar_t * __restrict wc, _csid_t csid, _index_t idx)
344219019Sgabor{
345219019Sgabor
346219019Sgabor	if (csid == 0) {
347219019Sgabor		if ((idx & ~0x7F) != 0)
348219019Sgabor			return (EINVAL);
349219019Sgabor		*wc = (wchar_t)idx;
350219019Sgabor	} else {
351219019Sgabor		if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0)
352219019Sgabor			return (EINVAL);
353219019Sgabor		*wc = (wchar_t)idx | ((wchar_t)csid << 24);
354219019Sgabor	}
355219019Sgabor
356219019Sgabor	return (0);
357219019Sgabor}
358219019Sgabor
359219019Sgaborstatic __inline int
360219019Sgabor/*ARGSUSED*/
361219019Sgabor_citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei __unused,
362219019Sgabor    _EUCTWState * __restrict psenc, int * __restrict rstate)
363219019Sgabor{
364219019Sgabor
365219019Sgabor	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
366219019Sgabor	    _STDENC_SDGEN_INCOMPLETE_CHAR;
367219019Sgabor	return (0);
368219019Sgabor}
369219019Sgabor
370219019Sgabor/* ----------------------------------------------------------------------
371219019Sgabor * public interface for stdenc
372219019Sgabor */
373219019Sgabor
374219019Sgabor_CITRUS_STDENC_DECLS(EUCTW);
375219019Sgabor_CITRUS_STDENC_DEF_OPS(EUCTW);
376219019Sgabor
377219019Sgabor#include "citrus_stdenc_template.h"
378