xref: /freebsd/lib/libiconv_modules/BIG5/citrus_big5.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
19ca40936STijl Coosemans /*	$NetBSD: citrus_big5.c,v 1.13 2011/05/23 14:53:46 joerg Exp $	*/
2ad30f8e7SGabor Kovesdan 
3ad30f8e7SGabor Kovesdan /*-
4ad30f8e7SGabor Kovesdan  * Copyright (c)2002, 2006 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/cdefs.h>
64ad30f8e7SGabor Kovesdan #include <sys/queue.h>
65ad30f8e7SGabor Kovesdan #include <sys/types.h>
66ad30f8e7SGabor Kovesdan 
67ad30f8e7SGabor Kovesdan #include <assert.h>
68ad30f8e7SGabor Kovesdan #include <errno.h>
69ad30f8e7SGabor Kovesdan #include <limits.h>
70ad30f8e7SGabor Kovesdan #include <stddef.h>
71ad30f8e7SGabor Kovesdan #include <stdint.h>
72ad30f8e7SGabor Kovesdan #include <stdio.h>
73ad30f8e7SGabor Kovesdan #include <stdlib.h>
74ad30f8e7SGabor Kovesdan #include <string.h>
75ad30f8e7SGabor Kovesdan #include <wchar.h>
76ad30f8e7SGabor Kovesdan 
77ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
78ad30f8e7SGabor Kovesdan #include "citrus_prop.h"
79ad30f8e7SGabor Kovesdan #include "citrus_types.h"
80ad30f8e7SGabor Kovesdan #include "citrus_bcs.h"
81ad30f8e7SGabor Kovesdan #include "citrus_module.h"
82ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
83ad30f8e7SGabor Kovesdan #include "citrus_big5.h"
84ad30f8e7SGabor Kovesdan 
85ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
86ad30f8e7SGabor Kovesdan  * private stuffs used by templates
87ad30f8e7SGabor Kovesdan  */
88ad30f8e7SGabor Kovesdan 
89ad30f8e7SGabor Kovesdan typedef struct {
90ad30f8e7SGabor Kovesdan 	int	 chlen;
91ad30f8e7SGabor Kovesdan 	char	 ch[2];
92ad30f8e7SGabor Kovesdan } _BIG5State;
93ad30f8e7SGabor Kovesdan 
94ad30f8e7SGabor Kovesdan typedef struct _BIG5Exclude {
95ad30f8e7SGabor Kovesdan 	TAILQ_ENTRY(_BIG5Exclude)	 entry;
96ad30f8e7SGabor Kovesdan 	wint_t				 start;
979ca40936STijl Coosemans 	wint_t				 end;
98ad30f8e7SGabor Kovesdan } _BIG5Exclude;
99ad30f8e7SGabor Kovesdan 
100ad30f8e7SGabor Kovesdan typedef TAILQ_HEAD(_BIG5ExcludeList, _BIG5Exclude) _BIG5ExcludeList;
101ad30f8e7SGabor Kovesdan 
102ad30f8e7SGabor Kovesdan typedef struct {
103ad30f8e7SGabor Kovesdan 	_BIG5ExcludeList	 excludes;
104ad30f8e7SGabor Kovesdan 	int			 cell[0x100];
105ad30f8e7SGabor Kovesdan } _BIG5EncodingInfo;
106ad30f8e7SGabor Kovesdan 
107ad30f8e7SGabor Kovesdan #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
108ad30f8e7SGabor Kovesdan #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
109ad30f8e7SGabor Kovesdan 
110ad30f8e7SGabor Kovesdan #define _FUNCNAME(m)			_citrus_BIG5_##m
111ad30f8e7SGabor Kovesdan #define _ENCODING_INFO			_BIG5EncodingInfo
112ad30f8e7SGabor Kovesdan #define _ENCODING_STATE			_BIG5State
113ad30f8e7SGabor Kovesdan #define _ENCODING_MB_CUR_MAX(_ei_)	2
114ad30f8e7SGabor Kovesdan #define _ENCODING_IS_STATE_DEPENDENT	0
115ad30f8e7SGabor Kovesdan #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
116ad30f8e7SGabor Kovesdan 
117ad30f8e7SGabor Kovesdan 
118ad30f8e7SGabor Kovesdan static __inline void
119ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei __unused,_BIG5State * __restrict s)120ad30f8e7SGabor Kovesdan _citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei __unused,
121ad30f8e7SGabor Kovesdan     _BIG5State * __restrict s)
122ad30f8e7SGabor Kovesdan {
123ad30f8e7SGabor Kovesdan 
124ad30f8e7SGabor Kovesdan 	memset(s, 0, sizeof(*s));
125ad30f8e7SGabor Kovesdan }
126ad30f8e7SGabor Kovesdan 
127b61949ddSDimitry Andric #if 0
128ad30f8e7SGabor Kovesdan static __inline void
129ad30f8e7SGabor Kovesdan /*ARGSUSED*/
130ad30f8e7SGabor Kovesdan _citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei __unused,
131ad30f8e7SGabor Kovesdan     void * __restrict pspriv,
132ad30f8e7SGabor Kovesdan     const _BIG5State * __restrict s)
133ad30f8e7SGabor Kovesdan {
134ad30f8e7SGabor Kovesdan 
135ad30f8e7SGabor Kovesdan 	memcpy(pspriv, (const void *)s, sizeof(*s));
136ad30f8e7SGabor Kovesdan }
137ad30f8e7SGabor Kovesdan 
138ad30f8e7SGabor Kovesdan static __inline void
139ad30f8e7SGabor Kovesdan /*ARGSUSED*/
140ad30f8e7SGabor Kovesdan _citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei __unused,
141ad30f8e7SGabor Kovesdan     _BIG5State * __restrict s,
142ad30f8e7SGabor Kovesdan     const void * __restrict pspriv)
143ad30f8e7SGabor Kovesdan {
144ad30f8e7SGabor Kovesdan 
145ad30f8e7SGabor Kovesdan 	memcpy((void *)s, pspriv, sizeof(*s));
146ad30f8e7SGabor Kovesdan }
147b61949ddSDimitry Andric #endif
148ad30f8e7SGabor Kovesdan 
149ad30f8e7SGabor Kovesdan static __inline int
_citrus_BIG5_check(_BIG5EncodingInfo * ei,unsigned int c)150ad30f8e7SGabor Kovesdan _citrus_BIG5_check(_BIG5EncodingInfo *ei, unsigned int c)
151ad30f8e7SGabor Kovesdan {
152ad30f8e7SGabor Kovesdan 
153ad30f8e7SGabor Kovesdan 	return ((ei->cell[c & 0xFF] & 0x1) ? 2 : 1);
154ad30f8e7SGabor Kovesdan }
155ad30f8e7SGabor Kovesdan 
156ad30f8e7SGabor Kovesdan static __inline int
_citrus_BIG5_check2(_BIG5EncodingInfo * ei,unsigned int c)157ad30f8e7SGabor Kovesdan _citrus_BIG5_check2(_BIG5EncodingInfo *ei, unsigned int c)
158ad30f8e7SGabor Kovesdan {
159ad30f8e7SGabor Kovesdan 
160ad30f8e7SGabor Kovesdan 	return ((ei->cell[c & 0xFF] & 0x2) ? 1 : 0);
161ad30f8e7SGabor Kovesdan }
162ad30f8e7SGabor Kovesdan 
163ad30f8e7SGabor Kovesdan static __inline int
_citrus_BIG5_check_excludes(_BIG5EncodingInfo * ei,wint_t c)164ad30f8e7SGabor Kovesdan _citrus_BIG5_check_excludes(_BIG5EncodingInfo *ei, wint_t c)
165ad30f8e7SGabor Kovesdan {
166ad30f8e7SGabor Kovesdan 	_BIG5Exclude *exclude;
167ad30f8e7SGabor Kovesdan 
168ad30f8e7SGabor Kovesdan 	TAILQ_FOREACH(exclude, &ei->excludes, entry) {
169ad30f8e7SGabor Kovesdan 		if (c >= exclude->start && c <= exclude->end)
170ad30f8e7SGabor Kovesdan 			return (EILSEQ);
171ad30f8e7SGabor Kovesdan 	}
172ad30f8e7SGabor Kovesdan 	return (0);
173ad30f8e7SGabor Kovesdan }
174ad30f8e7SGabor Kovesdan 
175ad30f8e7SGabor Kovesdan static int
_citrus_BIG5_fill_rowcol(void * __restrict ctx,const char * __restrict s,uint64_t start,uint64_t end)17664f204f9STijl Coosemans _citrus_BIG5_fill_rowcol(void * __restrict ctx, const char * __restrict s,
177ad30f8e7SGabor Kovesdan     uint64_t start, uint64_t end)
178ad30f8e7SGabor Kovesdan {
179ad30f8e7SGabor Kovesdan 	_BIG5EncodingInfo *ei;
180ad30f8e7SGabor Kovesdan 	uint64_t n;
181ad30f8e7SGabor Kovesdan 	int i;
182ad30f8e7SGabor Kovesdan 
183ad30f8e7SGabor Kovesdan 	if (start > 0xFF || end > 0xFF)
184ad30f8e7SGabor Kovesdan 		return (EINVAL);
185ad30f8e7SGabor Kovesdan 	ei = (_BIG5EncodingInfo *)ctx;
186ad30f8e7SGabor Kovesdan 	i = strcmp("row", s) ? 1 : 0;
187ad30f8e7SGabor Kovesdan 	i = 1 << i;
188ad30f8e7SGabor Kovesdan 	for (n = start; n <= end; ++n)
189ad30f8e7SGabor Kovesdan 		ei->cell[n & 0xFF] |= i;
190ad30f8e7SGabor Kovesdan 	return (0);
191ad30f8e7SGabor Kovesdan }
192ad30f8e7SGabor Kovesdan 
193ad30f8e7SGabor Kovesdan static int
194ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_fill_excludes(void * __restrict ctx,const char * __restrict s __unused,uint64_t start,uint64_t end)19564f204f9STijl Coosemans _citrus_BIG5_fill_excludes(void * __restrict ctx,
196ad30f8e7SGabor Kovesdan     const char * __restrict s __unused, uint64_t start, uint64_t end)
197ad30f8e7SGabor Kovesdan {
198ad30f8e7SGabor Kovesdan 	_BIG5EncodingInfo *ei;
199ad30f8e7SGabor Kovesdan 	_BIG5Exclude *exclude;
200ad30f8e7SGabor Kovesdan 
201ad30f8e7SGabor Kovesdan 	if (start > 0xFFFF || end > 0xFFFF)
202ad30f8e7SGabor Kovesdan 		return (EINVAL);
203ad30f8e7SGabor Kovesdan 	ei = (_BIG5EncodingInfo *)ctx;
204ad30f8e7SGabor Kovesdan 	exclude = TAILQ_LAST(&ei->excludes, _BIG5ExcludeList);
205ad30f8e7SGabor Kovesdan 	if (exclude != NULL && (wint_t)start <= exclude->end)
206ad30f8e7SGabor Kovesdan 		return (EINVAL);
207ad30f8e7SGabor Kovesdan 	exclude = (void *)malloc(sizeof(*exclude));
208ad30f8e7SGabor Kovesdan 	if (exclude == NULL)
209ad30f8e7SGabor Kovesdan 		return (ENOMEM);
210ad30f8e7SGabor Kovesdan 	exclude->start = (wint_t)start;
211ad30f8e7SGabor Kovesdan 	exclude->end = (wint_t)end;
212ad30f8e7SGabor Kovesdan 	TAILQ_INSERT_TAIL(&ei->excludes, exclude, entry);
213ad30f8e7SGabor Kovesdan 
214ad30f8e7SGabor Kovesdan 	return (0);
215ad30f8e7SGabor Kovesdan }
216ad30f8e7SGabor Kovesdan 
217ad30f8e7SGabor Kovesdan static const _citrus_prop_hint_t root_hints[] = {
218ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_NUM("row", &_citrus_BIG5_fill_rowcol),
219ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_NUM("col", &_citrus_BIG5_fill_rowcol),
220ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_NUM("excludes", &_citrus_BIG5_fill_excludes),
221ad30f8e7SGabor Kovesdan     _CITRUS_PROP_HINT_END
222ad30f8e7SGabor Kovesdan };
223ad30f8e7SGabor Kovesdan 
224ad30f8e7SGabor Kovesdan static void
225ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo * ei)226ad30f8e7SGabor Kovesdan _citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei)
227ad30f8e7SGabor Kovesdan {
228ad30f8e7SGabor Kovesdan 	_BIG5Exclude *exclude;
229ad30f8e7SGabor Kovesdan 
230ad30f8e7SGabor Kovesdan 	while ((exclude = TAILQ_FIRST(&ei->excludes)) != NULL) {
231ad30f8e7SGabor Kovesdan 		TAILQ_REMOVE(&ei->excludes, exclude, entry);
232ad30f8e7SGabor Kovesdan 		free(exclude);
233ad30f8e7SGabor Kovesdan 	}
234ad30f8e7SGabor Kovesdan }
235ad30f8e7SGabor Kovesdan 
236ad30f8e7SGabor Kovesdan static int
237ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei,const void * __restrict var,size_t lenvar)238ad30f8e7SGabor Kovesdan _citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei,
239ad30f8e7SGabor Kovesdan     const void * __restrict var, size_t lenvar)
240ad30f8e7SGabor Kovesdan {
241ad30f8e7SGabor Kovesdan 	const char *s;
242ad30f8e7SGabor Kovesdan 	int err;
243ad30f8e7SGabor Kovesdan 
244ad30f8e7SGabor Kovesdan 	memset((void *)ei, 0, sizeof(*ei));
245ad30f8e7SGabor Kovesdan 	TAILQ_INIT(&ei->excludes);
246ad30f8e7SGabor Kovesdan 
247ad30f8e7SGabor Kovesdan 	if (lenvar > 0 && var != NULL) {
248ad30f8e7SGabor Kovesdan 		s = _bcs_skip_ws_len((const char *)var, &lenvar);
249ad30f8e7SGabor Kovesdan 		if (lenvar > 0 && *s != '\0') {
250ad30f8e7SGabor Kovesdan 			err = _citrus_prop_parse_variable(
251ad30f8e7SGabor Kovesdan 			    root_hints, (void *)ei, s, lenvar);
252ad30f8e7SGabor Kovesdan 			if (err == 0)
253ad30f8e7SGabor Kovesdan 				return (0);
254ad30f8e7SGabor Kovesdan 
255ad30f8e7SGabor Kovesdan 			_citrus_BIG5_encoding_module_uninit(ei);
256ad30f8e7SGabor Kovesdan 			memset((void *)ei, 0, sizeof(*ei));
257ad30f8e7SGabor Kovesdan 			TAILQ_INIT(&ei->excludes);
258ad30f8e7SGabor Kovesdan 		}
259ad30f8e7SGabor Kovesdan 	}
260ad30f8e7SGabor Kovesdan 
261ad30f8e7SGabor Kovesdan 	/* fallback Big5-1984, for backward compatibility. */
26264f204f9STijl Coosemans 	_citrus_BIG5_fill_rowcol(ei, "row", 0xA1, 0xFE);
26364f204f9STijl Coosemans 	_citrus_BIG5_fill_rowcol(ei, "col", 0x40, 0x7E);
26464f204f9STijl Coosemans 	_citrus_BIG5_fill_rowcol(ei, "col", 0xA1, 0xFE);
265ad30f8e7SGabor Kovesdan 
266ad30f8e7SGabor Kovesdan 	return (0);
267ad30f8e7SGabor Kovesdan }
268ad30f8e7SGabor Kovesdan 
269ad30f8e7SGabor Kovesdan static int
270ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,wchar_t * __restrict pwc,char ** __restrict s,size_t n,_BIG5State * __restrict psenc,size_t * __restrict nresult)271ad30f8e7SGabor Kovesdan _citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,
272ad30f8e7SGabor Kovesdan     wchar_t * __restrict pwc,
2731243a98eSTijl Coosemans     char ** __restrict s, size_t n,
274ad30f8e7SGabor Kovesdan     _BIG5State * __restrict psenc,
275ad30f8e7SGabor Kovesdan     size_t * __restrict nresult)
276ad30f8e7SGabor Kovesdan {
277ad30f8e7SGabor Kovesdan 	wchar_t wchar;
2781243a98eSTijl Coosemans 	char *s0;
279ad30f8e7SGabor Kovesdan 	int c, chlenbak;
280ad30f8e7SGabor Kovesdan 
281ad30f8e7SGabor Kovesdan 	s0 = *s;
282ad30f8e7SGabor Kovesdan 
283ad30f8e7SGabor Kovesdan 	if (s0 == NULL) {
284ad30f8e7SGabor Kovesdan 		_citrus_BIG5_init_state(ei, psenc);
285ad30f8e7SGabor Kovesdan 		*nresult = 0;
286ad30f8e7SGabor Kovesdan 		return (0);
287ad30f8e7SGabor Kovesdan 	}
288ad30f8e7SGabor Kovesdan 
289ad30f8e7SGabor Kovesdan 	chlenbak = psenc->chlen;
290ad30f8e7SGabor Kovesdan 
291ad30f8e7SGabor Kovesdan 	/* make sure we have the first byte in the buffer */
292ad30f8e7SGabor Kovesdan 	switch (psenc->chlen) {
293ad30f8e7SGabor Kovesdan 	case 0:
294ad30f8e7SGabor Kovesdan 		if (n < 1)
295ad30f8e7SGabor Kovesdan 			goto restart;
296ad30f8e7SGabor Kovesdan 		psenc->ch[0] = *s0++;
297ad30f8e7SGabor Kovesdan 		psenc->chlen = 1;
298ad30f8e7SGabor Kovesdan 		n--;
299ad30f8e7SGabor Kovesdan 		break;
300ad30f8e7SGabor Kovesdan 	case 1:
301ad30f8e7SGabor Kovesdan 		break;
302ad30f8e7SGabor Kovesdan 	default:
303ad30f8e7SGabor Kovesdan 		/* illegal state */
304ad30f8e7SGabor Kovesdan 		goto ilseq;
305ad30f8e7SGabor Kovesdan 	}
306ad30f8e7SGabor Kovesdan 
307ad30f8e7SGabor Kovesdan 	c = _citrus_BIG5_check(ei, psenc->ch[0] & 0xff);
308ad30f8e7SGabor Kovesdan 	if (c == 0)
309ad30f8e7SGabor Kovesdan 		goto ilseq;
310ad30f8e7SGabor Kovesdan 	while (psenc->chlen < c) {
311ad30f8e7SGabor Kovesdan 		if (n < 1) {
312ad30f8e7SGabor Kovesdan 			goto restart;
313ad30f8e7SGabor Kovesdan 		}
314ad30f8e7SGabor Kovesdan 		psenc->ch[psenc->chlen] = *s0++;
315ad30f8e7SGabor Kovesdan 		psenc->chlen++;
316ad30f8e7SGabor Kovesdan 		n--;
317ad30f8e7SGabor Kovesdan 	}
318ad30f8e7SGabor Kovesdan 
319ad30f8e7SGabor Kovesdan 	switch (c) {
320ad30f8e7SGabor Kovesdan 	case 1:
321ad30f8e7SGabor Kovesdan 		wchar = psenc->ch[0] & 0xff;
322ad30f8e7SGabor Kovesdan 		break;
323ad30f8e7SGabor Kovesdan 	case 2:
324ad30f8e7SGabor Kovesdan 		if (!_citrus_BIG5_check2(ei, psenc->ch[1] & 0xff))
325ad30f8e7SGabor Kovesdan 			goto ilseq;
326ad30f8e7SGabor Kovesdan 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
327ad30f8e7SGabor Kovesdan 		break;
328ad30f8e7SGabor Kovesdan 	default:
329ad30f8e7SGabor Kovesdan 		/* illegal state */
330ad30f8e7SGabor Kovesdan 		goto ilseq;
331ad30f8e7SGabor Kovesdan 	}
332ad30f8e7SGabor Kovesdan 
333ad30f8e7SGabor Kovesdan 	if (_citrus_BIG5_check_excludes(ei, (wint_t)wchar) != 0)
334ad30f8e7SGabor Kovesdan 		goto ilseq;
335ad30f8e7SGabor Kovesdan 
336ad30f8e7SGabor Kovesdan 	*s = s0;
337ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
338ad30f8e7SGabor Kovesdan 	if (pwc)
339ad30f8e7SGabor Kovesdan 		*pwc = wchar;
340ad30f8e7SGabor Kovesdan 	*nresult = wchar ? c - chlenbak : 0;
341ad30f8e7SGabor Kovesdan 
342ad30f8e7SGabor Kovesdan 	return (0);
343ad30f8e7SGabor Kovesdan 
344ad30f8e7SGabor Kovesdan ilseq:
345ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
346ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
347ad30f8e7SGabor Kovesdan 	return (EILSEQ);
348ad30f8e7SGabor Kovesdan 
349ad30f8e7SGabor Kovesdan restart:
350ad30f8e7SGabor Kovesdan 	*s = s0;
351ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-2;
352ad30f8e7SGabor Kovesdan 	return (0);
353ad30f8e7SGabor Kovesdan }
354ad30f8e7SGabor Kovesdan 
355ad30f8e7SGabor Kovesdan static int
356ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,char * __restrict s,size_t n,wchar_t wc,_BIG5State * __restrict psenc __unused,size_t * __restrict nresult)357ad30f8e7SGabor Kovesdan _citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,
358ad30f8e7SGabor Kovesdan     char * __restrict s,
359ad30f8e7SGabor Kovesdan     size_t n, wchar_t wc, _BIG5State * __restrict psenc __unused,
360ad30f8e7SGabor Kovesdan     size_t * __restrict nresult)
361ad30f8e7SGabor Kovesdan {
3629ca40936STijl Coosemans 	size_t l;
363ad30f8e7SGabor Kovesdan 	int ret;
364ad30f8e7SGabor Kovesdan 
365ad30f8e7SGabor Kovesdan 	/* check invalid sequence */
366ad30f8e7SGabor Kovesdan 	if (wc & ~0xffff ||
367ad30f8e7SGabor Kovesdan 	    _citrus_BIG5_check_excludes(ei, (wint_t)wc) != 0) {
368ad30f8e7SGabor Kovesdan 		ret = EILSEQ;
369ad30f8e7SGabor Kovesdan 		goto err;
370ad30f8e7SGabor Kovesdan 	}
371ad30f8e7SGabor Kovesdan 
372ad30f8e7SGabor Kovesdan 	if (wc & 0x8000) {
373ad30f8e7SGabor Kovesdan 		if (_citrus_BIG5_check(ei, (wc >> 8) & 0xff) != 2 ||
374ad30f8e7SGabor Kovesdan 		    !_citrus_BIG5_check2(ei, wc & 0xff)) {
375ad30f8e7SGabor Kovesdan 			ret = EILSEQ;
376ad30f8e7SGabor Kovesdan 			goto err;
377ad30f8e7SGabor Kovesdan 		}
378ad30f8e7SGabor Kovesdan 		l = 2;
379ad30f8e7SGabor Kovesdan 	} else {
380ad30f8e7SGabor Kovesdan 		if (wc & ~0xff || !_citrus_BIG5_check(ei, wc & 0xff)) {
381ad30f8e7SGabor Kovesdan 			ret = EILSEQ;
382ad30f8e7SGabor Kovesdan 			goto err;
383ad30f8e7SGabor Kovesdan 		}
384ad30f8e7SGabor Kovesdan 		l = 1;
385ad30f8e7SGabor Kovesdan 	}
386ad30f8e7SGabor Kovesdan 
387ad30f8e7SGabor Kovesdan 	if (n < l) {
388ad30f8e7SGabor Kovesdan 		/* bound check failure */
389ad30f8e7SGabor Kovesdan 		ret = E2BIG;
390ad30f8e7SGabor Kovesdan 		goto err;
391ad30f8e7SGabor Kovesdan 	}
392ad30f8e7SGabor Kovesdan 
393ad30f8e7SGabor Kovesdan 	if (l == 2) {
394ad30f8e7SGabor Kovesdan 		s[0] = (wc >> 8) & 0xff;
395ad30f8e7SGabor Kovesdan 		s[1] = wc & 0xff;
396ad30f8e7SGabor Kovesdan 	} else
397ad30f8e7SGabor Kovesdan 		s[0] = wc & 0xff;
398ad30f8e7SGabor Kovesdan 
399ad30f8e7SGabor Kovesdan 	*nresult = l;
400ad30f8e7SGabor Kovesdan 
401ad30f8e7SGabor Kovesdan 	return (0);
402ad30f8e7SGabor Kovesdan 
403ad30f8e7SGabor Kovesdan err:
404ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
405ad30f8e7SGabor Kovesdan 	return (ret);
406ad30f8e7SGabor Kovesdan }
407ad30f8e7SGabor Kovesdan 
408ad30f8e7SGabor Kovesdan static __inline int
409ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei __unused,_csid_t * __restrict csid,_index_t * __restrict idx,wchar_t wc)410ad30f8e7SGabor Kovesdan _citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei __unused,
411ad30f8e7SGabor Kovesdan     _csid_t * __restrict csid,
412ad30f8e7SGabor Kovesdan     _index_t * __restrict idx, wchar_t wc)
413ad30f8e7SGabor Kovesdan {
414ad30f8e7SGabor Kovesdan 
415ad30f8e7SGabor Kovesdan 	*csid = (wc < 0x100) ? 0 : 1;
416ad30f8e7SGabor Kovesdan 	*idx = (_index_t)wc;
417ad30f8e7SGabor Kovesdan 
418ad30f8e7SGabor Kovesdan 	return (0);
419ad30f8e7SGabor Kovesdan }
420ad30f8e7SGabor Kovesdan 
421ad30f8e7SGabor Kovesdan static __inline int
422ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei __unused,wchar_t * __restrict wc,_csid_t csid,_index_t idx)423ad30f8e7SGabor Kovesdan _citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei __unused,
424ad30f8e7SGabor Kovesdan     wchar_t * __restrict wc,
425ad30f8e7SGabor Kovesdan     _csid_t csid, _index_t idx)
426ad30f8e7SGabor Kovesdan {
427ad30f8e7SGabor Kovesdan 
428ad30f8e7SGabor Kovesdan 	switch (csid) {
429ad30f8e7SGabor Kovesdan 	case 0:
430ad30f8e7SGabor Kovesdan 	case 1:
431ad30f8e7SGabor Kovesdan 		*wc = (wchar_t)idx;
432ad30f8e7SGabor Kovesdan 		break;
433ad30f8e7SGabor Kovesdan 	default:
434ad30f8e7SGabor Kovesdan 		return (EILSEQ);
435ad30f8e7SGabor Kovesdan 	}
436ad30f8e7SGabor Kovesdan 
437ad30f8e7SGabor Kovesdan 	return (0);
438ad30f8e7SGabor Kovesdan }
439ad30f8e7SGabor Kovesdan 
440ad30f8e7SGabor Kovesdan static __inline int
441ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_BIG5_stdenc_get_state_desc_generic(_BIG5EncodingInfo * __restrict ei __unused,_BIG5State * __restrict psenc,int * __restrict rstate)442ad30f8e7SGabor Kovesdan _citrus_BIG5_stdenc_get_state_desc_generic(_BIG5EncodingInfo * __restrict ei __unused,
443ad30f8e7SGabor Kovesdan     _BIG5State * __restrict psenc,
444ad30f8e7SGabor Kovesdan     int * __restrict rstate)
445ad30f8e7SGabor Kovesdan {
446ad30f8e7SGabor Kovesdan 
447ad30f8e7SGabor Kovesdan 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
448ad30f8e7SGabor Kovesdan 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
449ad30f8e7SGabor Kovesdan 	return (0);
450ad30f8e7SGabor Kovesdan }
451ad30f8e7SGabor Kovesdan 
452ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
453ad30f8e7SGabor Kovesdan  * public interface for stdenc
454ad30f8e7SGabor Kovesdan  */
455ad30f8e7SGabor Kovesdan 
456ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(BIG5);
457ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(BIG5);
458ad30f8e7SGabor Kovesdan 
459ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
460