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