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