xref: /freebsd/lib/libiconv_modules/BIG5/citrus_big5.c (revision 64f204f9dd361c36dc47ec62df01dfbb4cf4647a)
1ad30f8e7SGabor Kovesdan /* $FreeBSD$ */
2ad30f8e7SGabor Kovesdan /*	$NetBSD: citrus_big5.c,v 1.12 2008/06/14 16:01:07 tnozaki Exp $	*/
3ad30f8e7SGabor Kovesdan 
4ad30f8e7SGabor Kovesdan /*-
5ad30f8e7SGabor Kovesdan  * Copyright (c)2002, 2006 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) 1993
32ad30f8e7SGabor Kovesdan  *	The Regents of the University of California.  All rights reserved.
33ad30f8e7SGabor Kovesdan  *
34ad30f8e7SGabor Kovesdan  * This code is derived from software contributed to Berkeley by
35ad30f8e7SGabor Kovesdan  * Paul Borman at Krystal Technologies.
36ad30f8e7SGabor Kovesdan  *
37ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
38ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
39ad30f8e7SGabor Kovesdan  * are met:
40ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
41ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
42ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
43ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
44ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
45ad30f8e7SGabor Kovesdan  * 3. Neither the name of the University nor the names of its contributors
46ad30f8e7SGabor Kovesdan  *    may be used to endorse or promote products derived from this software
47ad30f8e7SGabor Kovesdan  *    without specific prior written permission.
48ad30f8e7SGabor Kovesdan  *
49ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
60ad30f8e7SGabor Kovesdan  */
61ad30f8e7SGabor Kovesdan 
62ad30f8e7SGabor Kovesdan #include <sys/cdefs.h>
63ad30f8e7SGabor Kovesdan #include <sys/queue.h>
64ad30f8e7SGabor Kovesdan #include <sys/types.h>
65ad30f8e7SGabor Kovesdan 
66ad30f8e7SGabor Kovesdan #include <assert.h>
67ad30f8e7SGabor Kovesdan #include <errno.h>
68ad30f8e7SGabor Kovesdan #include <limits.h>
69ad30f8e7SGabor Kovesdan #include <stddef.h>
70ad30f8e7SGabor Kovesdan #include <stdint.h>
71ad30f8e7SGabor Kovesdan #include <stdio.h>
72ad30f8e7SGabor Kovesdan #include <stdlib.h>
73ad30f8e7SGabor Kovesdan #include <string.h>
74ad30f8e7SGabor Kovesdan #include <wchar.h>
75ad30f8e7SGabor Kovesdan 
76ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
77ad30f8e7SGabor Kovesdan #include "citrus_prop.h"
78ad30f8e7SGabor Kovesdan #include "citrus_types.h"
79ad30f8e7SGabor Kovesdan #include "citrus_bcs.h"
80ad30f8e7SGabor Kovesdan #include "citrus_module.h"
81ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
82ad30f8e7SGabor Kovesdan #include "citrus_big5.h"
83ad30f8e7SGabor Kovesdan 
84ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
85ad30f8e7SGabor Kovesdan  * private stuffs used by templates
86ad30f8e7SGabor Kovesdan  */
87ad30f8e7SGabor Kovesdan 
88ad30f8e7SGabor Kovesdan typedef struct {
89ad30f8e7SGabor Kovesdan 	int	 chlen;
90ad30f8e7SGabor Kovesdan 	char	 ch[2];
91ad30f8e7SGabor Kovesdan } _BIG5State;
92ad30f8e7SGabor Kovesdan 
93ad30f8e7SGabor Kovesdan typedef struct _BIG5Exclude {
94ad30f8e7SGabor Kovesdan 	TAILQ_ENTRY(_BIG5Exclude)	 entry;
95ad30f8e7SGabor Kovesdan 	wint_t				 end;
96ad30f8e7SGabor Kovesdan 	wint_t				 start;
97ad30f8e7SGabor Kovesdan } _BIG5Exclude;
98ad30f8e7SGabor Kovesdan 
99ad30f8e7SGabor Kovesdan typedef TAILQ_HEAD(_BIG5ExcludeList, _BIG5Exclude) _BIG5ExcludeList;
100ad30f8e7SGabor Kovesdan 
101ad30f8e7SGabor Kovesdan typedef struct {
102ad30f8e7SGabor Kovesdan 	_BIG5ExcludeList	 excludes;
103ad30f8e7SGabor Kovesdan 	int			 cell[0x100];
104ad30f8e7SGabor Kovesdan } _BIG5EncodingInfo;
105ad30f8e7SGabor Kovesdan 
106ad30f8e7SGabor Kovesdan #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
107ad30f8e7SGabor Kovesdan #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
108ad30f8e7SGabor Kovesdan 
109ad30f8e7SGabor Kovesdan #define _FUNCNAME(m)			_citrus_BIG5_##m
110ad30f8e7SGabor Kovesdan #define _ENCODING_INFO			_BIG5EncodingInfo
111ad30f8e7SGabor Kovesdan #define _ENCODING_STATE			_BIG5State
112ad30f8e7SGabor Kovesdan #define _ENCODING_MB_CUR_MAX(_ei_)	2
113ad30f8e7SGabor Kovesdan #define _ENCODING_IS_STATE_DEPENDENT	0
114ad30f8e7SGabor Kovesdan #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
115ad30f8e7SGabor Kovesdan 
116ad30f8e7SGabor Kovesdan 
117ad30f8e7SGabor Kovesdan static __inline void
118ad30f8e7SGabor Kovesdan /*ARGSUSED*/
119ad30f8e7SGabor Kovesdan _citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei __unused,
120ad30f8e7SGabor Kovesdan     _BIG5State * __restrict s)
121ad30f8e7SGabor Kovesdan {
122ad30f8e7SGabor Kovesdan 
123ad30f8e7SGabor Kovesdan 	memset(s, 0, sizeof(*s));
124ad30f8e7SGabor Kovesdan }
125ad30f8e7SGabor Kovesdan 
126b61949ddSDimitry Andric #if 0
127ad30f8e7SGabor Kovesdan static __inline void
128ad30f8e7SGabor Kovesdan /*ARGSUSED*/
129ad30f8e7SGabor Kovesdan _citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei __unused,
130ad30f8e7SGabor Kovesdan     void * __restrict pspriv,
131ad30f8e7SGabor Kovesdan     const _BIG5State * __restrict s)
132ad30f8e7SGabor Kovesdan {
133ad30f8e7SGabor Kovesdan 
134ad30f8e7SGabor Kovesdan 	memcpy(pspriv, (const void *)s, sizeof(*s));
135ad30f8e7SGabor Kovesdan }
136ad30f8e7SGabor Kovesdan 
137ad30f8e7SGabor Kovesdan static __inline void
138ad30f8e7SGabor Kovesdan /*ARGSUSED*/
139ad30f8e7SGabor Kovesdan _citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei __unused,
140ad30f8e7SGabor Kovesdan     _BIG5State * __restrict s,
141ad30f8e7SGabor Kovesdan     const void * __restrict pspriv)
142ad30f8e7SGabor Kovesdan {
143ad30f8e7SGabor Kovesdan 
144ad30f8e7SGabor Kovesdan 	memcpy((void *)s, pspriv, sizeof(*s));
145ad30f8e7SGabor Kovesdan }
146b61949ddSDimitry Andric #endif
147ad30f8e7SGabor Kovesdan 
148ad30f8e7SGabor Kovesdan static __inline int
149ad30f8e7SGabor Kovesdan _citrus_BIG5_check(_BIG5EncodingInfo *ei, unsigned int c)
150ad30f8e7SGabor Kovesdan {
151ad30f8e7SGabor Kovesdan 
152ad30f8e7SGabor Kovesdan 	return ((ei->cell[c & 0xFF] & 0x1) ? 2 : 1);
153ad30f8e7SGabor Kovesdan }
154ad30f8e7SGabor Kovesdan 
155ad30f8e7SGabor Kovesdan static __inline int
156ad30f8e7SGabor Kovesdan _citrus_BIG5_check2(_BIG5EncodingInfo *ei, unsigned int c)
157ad30f8e7SGabor Kovesdan {
158ad30f8e7SGabor Kovesdan 
159ad30f8e7SGabor Kovesdan 	return ((ei->cell[c & 0xFF] & 0x2) ? 1 : 0);
160ad30f8e7SGabor Kovesdan }
161ad30f8e7SGabor Kovesdan 
162ad30f8e7SGabor Kovesdan static __inline int
163ad30f8e7SGabor Kovesdan _citrus_BIG5_check_excludes(_BIG5EncodingInfo *ei, wint_t c)
164ad30f8e7SGabor Kovesdan {
165ad30f8e7SGabor Kovesdan 	_BIG5Exclude *exclude;
166ad30f8e7SGabor Kovesdan 
167ad30f8e7SGabor Kovesdan 	TAILQ_FOREACH(exclude, &ei->excludes, entry) {
168ad30f8e7SGabor Kovesdan 		if (c >= exclude->start && c <= exclude->end)
169ad30f8e7SGabor Kovesdan 			return (EILSEQ);
170ad30f8e7SGabor Kovesdan 	}
171ad30f8e7SGabor Kovesdan 	return (0);
172ad30f8e7SGabor Kovesdan }
173ad30f8e7SGabor Kovesdan 
174ad30f8e7SGabor Kovesdan static int
175*64f204f9STijl Coosemans _citrus_BIG5_fill_rowcol(void * __restrict ctx, const char * __restrict s,
176ad30f8e7SGabor Kovesdan     uint64_t start, uint64_t end)
177ad30f8e7SGabor Kovesdan {
178ad30f8e7SGabor Kovesdan 	_BIG5EncodingInfo *ei;
179ad30f8e7SGabor Kovesdan 	uint64_t n;
180ad30f8e7SGabor Kovesdan 	int i;
181ad30f8e7SGabor Kovesdan 
182ad30f8e7SGabor Kovesdan 	if (start > 0xFF || end > 0xFF)
183ad30f8e7SGabor Kovesdan 		return (EINVAL);
184ad30f8e7SGabor Kovesdan 	ei = (_BIG5EncodingInfo *)ctx;
185ad30f8e7SGabor Kovesdan 	i = strcmp("row", s) ? 1 : 0;
186ad30f8e7SGabor Kovesdan 	i = 1 << i;
187ad30f8e7SGabor Kovesdan 	for (n = start; n <= end; ++n)
188ad30f8e7SGabor Kovesdan 		ei->cell[n & 0xFF] |= i;
189ad30f8e7SGabor Kovesdan 	return (0);
190ad30f8e7SGabor Kovesdan }
191ad30f8e7SGabor Kovesdan 
192ad30f8e7SGabor Kovesdan static int
193ad30f8e7SGabor Kovesdan /*ARGSUSED*/
194*64f204f9STijl Coosemans _citrus_BIG5_fill_excludes(void * __restrict ctx,
195ad30f8e7SGabor Kovesdan     const char * __restrict s __unused, uint64_t start, uint64_t end)
196ad30f8e7SGabor Kovesdan {
197ad30f8e7SGabor Kovesdan 	_BIG5EncodingInfo *ei;
198ad30f8e7SGabor Kovesdan 	_BIG5Exclude *exclude;
199ad30f8e7SGabor Kovesdan 
200ad30f8e7SGabor Kovesdan 	if (start > 0xFFFF || end > 0xFFFF)
201ad30f8e7SGabor Kovesdan 		return (EINVAL);
202ad30f8e7SGabor Kovesdan 	ei = (_BIG5EncodingInfo *)ctx;
203ad30f8e7SGabor Kovesdan 	exclude = TAILQ_LAST(&ei->excludes, _BIG5ExcludeList);
204ad30f8e7SGabor Kovesdan 	if (exclude != NULL && (wint_t)start <= exclude->end)
205ad30f8e7SGabor Kovesdan 		return (EINVAL);
206ad30f8e7SGabor Kovesdan 	exclude = (void *)malloc(sizeof(*exclude));
207ad30f8e7SGabor Kovesdan 	if (exclude == NULL)
208ad30f8e7SGabor Kovesdan 		return (ENOMEM);
209ad30f8e7SGabor Kovesdan 	exclude->start = (wint_t)start;
210ad30f8e7SGabor Kovesdan 	exclude->end = (wint_t)end;
211ad30f8e7SGabor Kovesdan 	TAILQ_INSERT_TAIL(&ei->excludes, exclude, entry);
212ad30f8e7SGabor Kovesdan 
213ad30f8e7SGabor Kovesdan 	return (0);
214ad30f8e7SGabor Kovesdan }
215ad30f8e7SGabor Kovesdan 
216ad30f8e7SGabor Kovesdan static const _citrus_prop_hint_t root_hints[] = {
217ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_NUM("row", &_citrus_BIG5_fill_rowcol),
218ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_NUM("col", &_citrus_BIG5_fill_rowcol),
219ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_NUM("excludes", &_citrus_BIG5_fill_excludes),
220ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_END
221ad30f8e7SGabor Kovesdan };
222ad30f8e7SGabor Kovesdan 
223ad30f8e7SGabor Kovesdan static void
224ad30f8e7SGabor Kovesdan /*ARGSUSED*/
225ad30f8e7SGabor Kovesdan _citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei)
226ad30f8e7SGabor Kovesdan {
227ad30f8e7SGabor Kovesdan 	_BIG5Exclude *exclude;
228ad30f8e7SGabor Kovesdan 
229ad30f8e7SGabor Kovesdan 	while ((exclude = TAILQ_FIRST(&ei->excludes)) != NULL) {
230ad30f8e7SGabor Kovesdan 		TAILQ_REMOVE(&ei->excludes, exclude, entry);
231ad30f8e7SGabor Kovesdan 		free(exclude);
232ad30f8e7SGabor Kovesdan 	}
233ad30f8e7SGabor Kovesdan }
234ad30f8e7SGabor Kovesdan 
235ad30f8e7SGabor Kovesdan static int
236ad30f8e7SGabor Kovesdan /*ARGSUSED*/
237ad30f8e7SGabor Kovesdan _citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei,
238ad30f8e7SGabor Kovesdan     const void * __restrict var, size_t lenvar)
239ad30f8e7SGabor Kovesdan {
240ad30f8e7SGabor Kovesdan 	const char *s;
241ad30f8e7SGabor Kovesdan 	int err;
242ad30f8e7SGabor Kovesdan 
243ad30f8e7SGabor Kovesdan 	memset((void *)ei, 0, sizeof(*ei));
244ad30f8e7SGabor Kovesdan 	TAILQ_INIT(&ei->excludes);
245ad30f8e7SGabor Kovesdan 
246ad30f8e7SGabor Kovesdan 	if (lenvar > 0 && var != NULL) {
247ad30f8e7SGabor Kovesdan 		s = _bcs_skip_ws_len((const char *)var, &lenvar);
248ad30f8e7SGabor Kovesdan 		if (lenvar > 0 && *s != '\0') {
249ad30f8e7SGabor Kovesdan 			err = _citrus_prop_parse_variable(
250ad30f8e7SGabor Kovesdan 			    root_hints, (void *)ei, s, lenvar);
251ad30f8e7SGabor Kovesdan 			if (err == 0)
252ad30f8e7SGabor Kovesdan 				return (0);
253ad30f8e7SGabor Kovesdan 
254ad30f8e7SGabor Kovesdan 			_citrus_BIG5_encoding_module_uninit(ei);
255ad30f8e7SGabor Kovesdan 			memset((void *)ei, 0, sizeof(*ei));
256ad30f8e7SGabor Kovesdan 			TAILQ_INIT(&ei->excludes);
257ad30f8e7SGabor Kovesdan 		}
258ad30f8e7SGabor Kovesdan 	}
259ad30f8e7SGabor Kovesdan 
260ad30f8e7SGabor Kovesdan 	/* fallback Big5-1984, for backward compatibility. */
261*64f204f9STijl Coosemans 	_citrus_BIG5_fill_rowcol(ei, "row", 0xA1, 0xFE);
262*64f204f9STijl Coosemans 	_citrus_BIG5_fill_rowcol(ei, "col", 0x40, 0x7E);
263*64f204f9STijl Coosemans 	_citrus_BIG5_fill_rowcol(ei, "col", 0xA1, 0xFE);
264ad30f8e7SGabor Kovesdan 
265ad30f8e7SGabor Kovesdan 	return (0);
266ad30f8e7SGabor Kovesdan }
267ad30f8e7SGabor Kovesdan 
268ad30f8e7SGabor Kovesdan static int
269ad30f8e7SGabor Kovesdan /*ARGSUSED*/
270ad30f8e7SGabor Kovesdan _citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,
271ad30f8e7SGabor Kovesdan     wchar_t * __restrict pwc,
2727900abffSPeter Wemm     const char ** __restrict s, size_t n,
273ad30f8e7SGabor Kovesdan     _BIG5State * __restrict psenc,
274ad30f8e7SGabor Kovesdan     size_t * __restrict nresult)
275ad30f8e7SGabor Kovesdan {
276ad30f8e7SGabor Kovesdan 	wchar_t wchar;
2777900abffSPeter Wemm 	const char *s0;
278ad30f8e7SGabor Kovesdan 	int c, chlenbak;
279ad30f8e7SGabor Kovesdan 
280ad30f8e7SGabor Kovesdan 	s0 = *s;
281ad30f8e7SGabor Kovesdan 
282ad30f8e7SGabor Kovesdan 	if (s0 == NULL) {
283ad30f8e7SGabor Kovesdan 		_citrus_BIG5_init_state(ei, psenc);
284ad30f8e7SGabor Kovesdan 		*nresult = 0;
285ad30f8e7SGabor Kovesdan 		return (0);
286ad30f8e7SGabor Kovesdan 	}
287ad30f8e7SGabor Kovesdan 
288ad30f8e7SGabor Kovesdan 	chlenbak = psenc->chlen;
289ad30f8e7SGabor Kovesdan 
290ad30f8e7SGabor Kovesdan 	/* make sure we have the first byte in the buffer */
291ad30f8e7SGabor Kovesdan 	switch (psenc->chlen) {
292ad30f8e7SGabor Kovesdan 	case 0:
293ad30f8e7SGabor Kovesdan 		if (n < 1)
294ad30f8e7SGabor Kovesdan 			goto restart;
295ad30f8e7SGabor Kovesdan 		psenc->ch[0] = *s0++;
296ad30f8e7SGabor Kovesdan 		psenc->chlen = 1;
297ad30f8e7SGabor Kovesdan 		n--;
298ad30f8e7SGabor Kovesdan 		break;
299ad30f8e7SGabor Kovesdan 	case 1:
300ad30f8e7SGabor Kovesdan 		break;
301ad30f8e7SGabor Kovesdan 	default:
302ad30f8e7SGabor Kovesdan 		/* illegal state */
303ad30f8e7SGabor Kovesdan 		goto ilseq;
304ad30f8e7SGabor Kovesdan 	}
305ad30f8e7SGabor Kovesdan 
306ad30f8e7SGabor Kovesdan 	c = _citrus_BIG5_check(ei, psenc->ch[0] & 0xff);
307ad30f8e7SGabor Kovesdan 	if (c == 0)
308ad30f8e7SGabor Kovesdan 		goto ilseq;
309ad30f8e7SGabor Kovesdan 	while (psenc->chlen < c) {
310ad30f8e7SGabor Kovesdan 		if (n < 1) {
311ad30f8e7SGabor Kovesdan 			goto restart;
312ad30f8e7SGabor Kovesdan 		}
313ad30f8e7SGabor Kovesdan 		psenc->ch[psenc->chlen] = *s0++;
314ad30f8e7SGabor Kovesdan 		psenc->chlen++;
315ad30f8e7SGabor Kovesdan 		n--;
316ad30f8e7SGabor Kovesdan 	}
317ad30f8e7SGabor Kovesdan 
318ad30f8e7SGabor Kovesdan 	switch (c) {
319ad30f8e7SGabor Kovesdan 	case 1:
320ad30f8e7SGabor Kovesdan 		wchar = psenc->ch[0] & 0xff;
321ad30f8e7SGabor Kovesdan 		break;
322ad30f8e7SGabor Kovesdan 	case 2:
323ad30f8e7SGabor Kovesdan 		if (!_citrus_BIG5_check2(ei, psenc->ch[1] & 0xff))
324ad30f8e7SGabor Kovesdan 			goto ilseq;
325ad30f8e7SGabor Kovesdan 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
326ad30f8e7SGabor Kovesdan 		break;
327ad30f8e7SGabor Kovesdan 	default:
328ad30f8e7SGabor Kovesdan 		/* illegal state */
329ad30f8e7SGabor Kovesdan 		goto ilseq;
330ad30f8e7SGabor Kovesdan 	}
331ad30f8e7SGabor Kovesdan 
332ad30f8e7SGabor Kovesdan 	if (_citrus_BIG5_check_excludes(ei, (wint_t)wchar) != 0)
333ad30f8e7SGabor Kovesdan 		goto ilseq;
334ad30f8e7SGabor Kovesdan 
335ad30f8e7SGabor Kovesdan 	*s = s0;
336ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
337ad30f8e7SGabor Kovesdan 	if (pwc)
338ad30f8e7SGabor Kovesdan 		*pwc = wchar;
339ad30f8e7SGabor Kovesdan 	*nresult = wchar ? c - chlenbak : 0;
340ad30f8e7SGabor Kovesdan 
341ad30f8e7SGabor Kovesdan 	return (0);
342ad30f8e7SGabor Kovesdan 
343ad30f8e7SGabor Kovesdan ilseq:
344ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
345ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
346ad30f8e7SGabor Kovesdan 	return (EILSEQ);
347ad30f8e7SGabor Kovesdan 
348ad30f8e7SGabor Kovesdan restart:
349ad30f8e7SGabor Kovesdan 	*s = s0;
350ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-2;
351ad30f8e7SGabor Kovesdan 	return (0);
352ad30f8e7SGabor Kovesdan }
353ad30f8e7SGabor Kovesdan 
354ad30f8e7SGabor Kovesdan static int
355ad30f8e7SGabor Kovesdan /*ARGSUSED*/
356ad30f8e7SGabor Kovesdan _citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,
357ad30f8e7SGabor Kovesdan     char * __restrict s,
358ad30f8e7SGabor Kovesdan     size_t n, wchar_t wc, _BIG5State * __restrict psenc __unused,
359ad30f8e7SGabor Kovesdan     size_t * __restrict nresult)
360ad30f8e7SGabor Kovesdan {
361ad30f8e7SGabor Kovesdan 	unsigned char l;
362ad30f8e7SGabor Kovesdan 	int ret;
363ad30f8e7SGabor Kovesdan 
364ad30f8e7SGabor Kovesdan 	/* check invalid sequence */
365ad30f8e7SGabor Kovesdan 	if (wc & ~0xffff ||
366ad30f8e7SGabor Kovesdan 	    _citrus_BIG5_check_excludes(ei, (wint_t)wc) != 0) {
367ad30f8e7SGabor Kovesdan 		ret = EILSEQ;
368ad30f8e7SGabor Kovesdan 		goto err;
369ad30f8e7SGabor Kovesdan 	}
370ad30f8e7SGabor Kovesdan 
371ad30f8e7SGabor Kovesdan 	if (wc & 0x8000) {
372ad30f8e7SGabor Kovesdan 		if (_citrus_BIG5_check(ei, (wc >> 8) & 0xff) != 2 ||
373ad30f8e7SGabor Kovesdan 		    !_citrus_BIG5_check2(ei, wc & 0xff)) {
374ad30f8e7SGabor Kovesdan 			ret = EILSEQ;
375ad30f8e7SGabor Kovesdan 			goto err;
376ad30f8e7SGabor Kovesdan 		}
377ad30f8e7SGabor Kovesdan 		l = 2;
378ad30f8e7SGabor Kovesdan 	} else {
379ad30f8e7SGabor Kovesdan 		if (wc & ~0xff || !_citrus_BIG5_check(ei, wc & 0xff)) {
380ad30f8e7SGabor Kovesdan 			ret = EILSEQ;
381ad30f8e7SGabor Kovesdan 			goto err;
382ad30f8e7SGabor Kovesdan 		}
383ad30f8e7SGabor Kovesdan 		l = 1;
384ad30f8e7SGabor Kovesdan 	}
385ad30f8e7SGabor Kovesdan 
386ad30f8e7SGabor Kovesdan 	if (n < l) {
387ad30f8e7SGabor Kovesdan 		/* bound check failure */
388ad30f8e7SGabor Kovesdan 		ret = E2BIG;
389ad30f8e7SGabor Kovesdan 		goto err;
390ad30f8e7SGabor Kovesdan 	}
391ad30f8e7SGabor Kovesdan 
392ad30f8e7SGabor Kovesdan 	if (l == 2) {
393ad30f8e7SGabor Kovesdan 		s[0] = (wc >> 8) & 0xff;
394ad30f8e7SGabor Kovesdan 		s[1] = wc & 0xff;
395ad30f8e7SGabor Kovesdan 	} else
396ad30f8e7SGabor Kovesdan 		s[0] = wc & 0xff;
397ad30f8e7SGabor Kovesdan 
398ad30f8e7SGabor Kovesdan 	*nresult = l;
399ad30f8e7SGabor Kovesdan 
400ad30f8e7SGabor Kovesdan 	return (0);
401ad30f8e7SGabor Kovesdan 
402ad30f8e7SGabor Kovesdan err:
403ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
404ad30f8e7SGabor Kovesdan 	return (ret);
405ad30f8e7SGabor Kovesdan }
406ad30f8e7SGabor Kovesdan 
407ad30f8e7SGabor Kovesdan static __inline int
408ad30f8e7SGabor Kovesdan /*ARGSUSED*/
409ad30f8e7SGabor Kovesdan _citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei __unused,
410ad30f8e7SGabor Kovesdan     _csid_t * __restrict csid,
411ad30f8e7SGabor Kovesdan     _index_t * __restrict idx, wchar_t wc)
412ad30f8e7SGabor Kovesdan {
413ad30f8e7SGabor Kovesdan 
414ad30f8e7SGabor Kovesdan 	*csid = (wc < 0x100) ? 0 : 1;
415ad30f8e7SGabor Kovesdan 	*idx = (_index_t)wc;
416ad30f8e7SGabor Kovesdan 
417ad30f8e7SGabor Kovesdan 	return (0);
418ad30f8e7SGabor Kovesdan }
419ad30f8e7SGabor Kovesdan 
420ad30f8e7SGabor Kovesdan static __inline int
421ad30f8e7SGabor Kovesdan /*ARGSUSED*/
422ad30f8e7SGabor Kovesdan _citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei __unused,
423ad30f8e7SGabor Kovesdan     wchar_t * __restrict wc,
424ad30f8e7SGabor Kovesdan     _csid_t csid, _index_t idx)
425ad30f8e7SGabor Kovesdan {
426ad30f8e7SGabor Kovesdan 
427ad30f8e7SGabor Kovesdan 	switch (csid) {
428ad30f8e7SGabor Kovesdan 	case 0:
429ad30f8e7SGabor Kovesdan 	case 1:
430ad30f8e7SGabor Kovesdan 		*wc = (wchar_t)idx;
431ad30f8e7SGabor Kovesdan 		break;
432ad30f8e7SGabor Kovesdan 	default:
433ad30f8e7SGabor Kovesdan 		return (EILSEQ);
434ad30f8e7SGabor Kovesdan 	}
435ad30f8e7SGabor Kovesdan 
436ad30f8e7SGabor Kovesdan 	return (0);
437ad30f8e7SGabor Kovesdan }
438ad30f8e7SGabor Kovesdan 
439ad30f8e7SGabor Kovesdan static __inline int
440ad30f8e7SGabor Kovesdan /*ARGSUSED*/
441ad30f8e7SGabor Kovesdan _citrus_BIG5_stdenc_get_state_desc_generic(_BIG5EncodingInfo * __restrict ei __unused,
442ad30f8e7SGabor Kovesdan     _BIG5State * __restrict psenc,
443ad30f8e7SGabor Kovesdan     int * __restrict rstate)
444ad30f8e7SGabor Kovesdan {
445ad30f8e7SGabor Kovesdan 
446ad30f8e7SGabor Kovesdan 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
447ad30f8e7SGabor Kovesdan 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
448ad30f8e7SGabor Kovesdan 	return (0);
449ad30f8e7SGabor Kovesdan }
450ad30f8e7SGabor Kovesdan 
451ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
452ad30f8e7SGabor Kovesdan  * public interface for stdenc
453ad30f8e7SGabor Kovesdan  */
454ad30f8e7SGabor Kovesdan 
455ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(BIG5);
456ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(BIG5);
457ad30f8e7SGabor Kovesdan 
458ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
459