xref: /freebsd/lib/libiconv_modules/BIG5/citrus_big5.c (revision 7079fc4e0236ff334c2fb0f4367159be6df39332)
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 static __inline void
127 /*ARGSUSED*/
128 _citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei __unused,
129     void * __restrict pspriv,
130     const _BIG5State * __restrict s)
131 {
132 
133 	memcpy(pspriv, (const void *)s, sizeof(*s));
134 }
135 
136 static __inline void
137 /*ARGSUSED*/
138 _citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei __unused,
139     _BIG5State * __restrict s,
140     const void * __restrict pspriv)
141 {
142 
143 	memcpy((void *)s, pspriv, sizeof(*s));
144 }
145 
146 static __inline int
147 _citrus_BIG5_check(_BIG5EncodingInfo *ei, unsigned int c)
148 {
149 
150 	return ((ei->cell[c & 0xFF] & 0x1) ? 2 : 1);
151 }
152 
153 static __inline int
154 _citrus_BIG5_check2(_BIG5EncodingInfo *ei, unsigned int c)
155 {
156 
157 	return ((ei->cell[c & 0xFF] & 0x2) ? 1 : 0);
158 }
159 
160 static __inline int
161 _citrus_BIG5_check_excludes(_BIG5EncodingInfo *ei, wint_t c)
162 {
163 	_BIG5Exclude *exclude;
164 
165 	TAILQ_FOREACH(exclude, &ei->excludes, entry) {
166 		if (c >= exclude->start && c <= exclude->end)
167 			return (EILSEQ);
168 	}
169 	return (0);
170 }
171 
172 static int
173 _citrus_BIG5_fill_rowcol(void ** __restrict ctx, const char * __restrict s,
174     uint64_t start, uint64_t end)
175 {
176 	_BIG5EncodingInfo *ei;
177 	uint64_t n;
178 	int i;
179 
180 	if (start > 0xFF || end > 0xFF)
181 		return (EINVAL);
182 	ei = (_BIG5EncodingInfo *)ctx;
183 	i = strcmp("row", s) ? 1 : 0;
184 	i = 1 << i;
185 	for (n = start; n <= end; ++n)
186 		ei->cell[n & 0xFF] |= i;
187 	return (0);
188 }
189 
190 static int
191 /*ARGSUSED*/
192 _citrus_BIG5_fill_excludes(void ** __restrict ctx,
193     const char * __restrict s __unused, uint64_t start, uint64_t end)
194 {
195 	_BIG5EncodingInfo *ei;
196 	_BIG5Exclude *exclude;
197 
198 	if (start > 0xFFFF || end > 0xFFFF)
199 		return (EINVAL);
200 	ei = (_BIG5EncodingInfo *)ctx;
201 	exclude = TAILQ_LAST(&ei->excludes, _BIG5ExcludeList);
202 	if (exclude != NULL && (wint_t)start <= exclude->end)
203 		return (EINVAL);
204 	exclude = (void *)malloc(sizeof(*exclude));
205 	if (exclude == NULL)
206 		return (ENOMEM);
207 	exclude->start = (wint_t)start;
208 	exclude->end = (wint_t)end;
209 	TAILQ_INSERT_TAIL(&ei->excludes, exclude, entry);
210 
211 	return (0);
212 }
213 
214 static const _citrus_prop_hint_t root_hints[] = {
215     _CITRUS_PROP_HINT_NUM("row", &_citrus_BIG5_fill_rowcol),
216     _CITRUS_PROP_HINT_NUM("col", &_citrus_BIG5_fill_rowcol),
217     _CITRUS_PROP_HINT_NUM("excludes", &_citrus_BIG5_fill_excludes),
218     _CITRUS_PROP_HINT_END
219 };
220 
221 static void
222 /*ARGSUSED*/
223 _citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei)
224 {
225 	_BIG5Exclude *exclude;
226 
227 	while ((exclude = TAILQ_FIRST(&ei->excludes)) != NULL) {
228 		TAILQ_REMOVE(&ei->excludes, exclude, entry);
229 		free(exclude);
230 	}
231 }
232 
233 static int
234 /*ARGSUSED*/
235 _citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei,
236     const void * __restrict var, size_t lenvar)
237 {
238 	void *ctx = (void *)ei;
239 	const char *s;
240 	int err;
241 
242 	memset((void *)ei, 0, sizeof(*ei));
243 	TAILQ_INIT(&ei->excludes);
244 
245 	if (lenvar > 0 && var != NULL) {
246 		s = _bcs_skip_ws_len((const char *)var, &lenvar);
247 		if (lenvar > 0 && *s != '\0') {
248 			err = _citrus_prop_parse_variable(
249 			    root_hints, (void *)ei, s, lenvar);
250 			if (err == 0)
251 				return (0);
252 
253 			_citrus_BIG5_encoding_module_uninit(ei);
254 			memset((void *)ei, 0, sizeof(*ei));
255 			TAILQ_INIT(&ei->excludes);
256 		}
257 	}
258 
259 	/* fallback Big5-1984, for backward compatibility. */
260 	_citrus_BIG5_fill_rowcol((void **)&ctx, "row", 0xA1, 0xFE);
261 	_citrus_BIG5_fill_rowcol((void **)&ctx, "col", 0x40, 0x7E);
262 	_citrus_BIG5_fill_rowcol((void **)&ctx, "col", 0xA1, 0xFE);
263 
264 	return (0);
265 }
266 
267 static int
268 /*ARGSUSED*/
269 _citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,
270     wchar_t * __restrict pwc,
271     const char ** __restrict s, size_t n,
272     _BIG5State * __restrict psenc,
273     size_t * __restrict nresult)
274 {
275 	wchar_t wchar;
276 	const char *s0;
277 	int c, chlenbak;
278 
279 	s0 = *s;
280 
281 	if (s0 == NULL) {
282 		_citrus_BIG5_init_state(ei, psenc);
283 		*nresult = 0;
284 		return (0);
285 	}
286 
287 	chlenbak = psenc->chlen;
288 
289 	/* make sure we have the first byte in the buffer */
290 	switch (psenc->chlen) {
291 	case 0:
292 		if (n < 1)
293 			goto restart;
294 		psenc->ch[0] = *s0++;
295 		psenc->chlen = 1;
296 		n--;
297 		break;
298 	case 1:
299 		break;
300 	default:
301 		/* illegal state */
302 		goto ilseq;
303 	}
304 
305 	c = _citrus_BIG5_check(ei, psenc->ch[0] & 0xff);
306 	if (c == 0)
307 		goto ilseq;
308 	while (psenc->chlen < c) {
309 		if (n < 1) {
310 			goto restart;
311 		}
312 		psenc->ch[psenc->chlen] = *s0++;
313 		psenc->chlen++;
314 		n--;
315 	}
316 
317 	switch (c) {
318 	case 1:
319 		wchar = psenc->ch[0] & 0xff;
320 		break;
321 	case 2:
322 		if (!_citrus_BIG5_check2(ei, psenc->ch[1] & 0xff))
323 			goto ilseq;
324 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
325 		break;
326 	default:
327 		/* illegal state */
328 		goto ilseq;
329 	}
330 
331 	if (_citrus_BIG5_check_excludes(ei, (wint_t)wchar) != 0)
332 		goto ilseq;
333 
334 	*s = s0;
335 	psenc->chlen = 0;
336 	if (pwc)
337 		*pwc = wchar;
338 	*nresult = wchar ? c - chlenbak : 0;
339 
340 	return (0);
341 
342 ilseq:
343 	psenc->chlen = 0;
344 	*nresult = (size_t)-1;
345 	return (EILSEQ);
346 
347 restart:
348 	*s = s0;
349 	*nresult = (size_t)-2;
350 	return (0);
351 }
352 
353 static int
354 /*ARGSUSED*/
355 _citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,
356     char * __restrict s,
357     size_t n, wchar_t wc, _BIG5State * __restrict psenc __unused,
358     size_t * __restrict nresult)
359 {
360 	unsigned char l;
361 	int ret;
362 
363 	/* check invalid sequence */
364 	if (wc & ~0xffff ||
365 	    _citrus_BIG5_check_excludes(ei, (wint_t)wc) != 0) {
366 		ret = EILSEQ;
367 		goto err;
368 	}
369 
370 	if (wc & 0x8000) {
371 		if (_citrus_BIG5_check(ei, (wc >> 8) & 0xff) != 2 ||
372 		    !_citrus_BIG5_check2(ei, wc & 0xff)) {
373 			ret = EILSEQ;
374 			goto err;
375 		}
376 		l = 2;
377 	} else {
378 		if (wc & ~0xff || !_citrus_BIG5_check(ei, wc & 0xff)) {
379 			ret = EILSEQ;
380 			goto err;
381 		}
382 		l = 1;
383 	}
384 
385 	if (n < l) {
386 		/* bound check failure */
387 		ret = E2BIG;
388 		goto err;
389 	}
390 
391 	if (l == 2) {
392 		s[0] = (wc >> 8) & 0xff;
393 		s[1] = wc & 0xff;
394 	} else
395 		s[0] = wc & 0xff;
396 
397 	*nresult = l;
398 
399 	return (0);
400 
401 err:
402 	*nresult = (size_t)-1;
403 	return (ret);
404 }
405 
406 static __inline int
407 /*ARGSUSED*/
408 _citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei __unused,
409     _csid_t * __restrict csid,
410     _index_t * __restrict idx, wchar_t wc)
411 {
412 
413 	*csid = (wc < 0x100) ? 0 : 1;
414 	*idx = (_index_t)wc;
415 
416 	return (0);
417 }
418 
419 static __inline int
420 /*ARGSUSED*/
421 _citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei __unused,
422     wchar_t * __restrict wc,
423     _csid_t csid, _index_t idx)
424 {
425 
426 	switch (csid) {
427 	case 0:
428 	case 1:
429 		*wc = (wchar_t)idx;
430 		break;
431 	default:
432 		return (EILSEQ);
433 	}
434 
435 	return (0);
436 }
437 
438 static __inline int
439 /*ARGSUSED*/
440 _citrus_BIG5_stdenc_get_state_desc_generic(_BIG5EncodingInfo * __restrict ei __unused,
441     _BIG5State * __restrict psenc,
442     int * __restrict rstate)
443 {
444 
445 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
446 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
447 	return (0);
448 }
449 
450 /* ----------------------------------------------------------------------
451  * public interface for stdenc
452  */
453 
454 _CITRUS_STDENC_DECLS(BIG5);
455 _CITRUS_STDENC_DEF_OPS(BIG5);
456 
457 #include "citrus_stdenc_template.h"
458