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