3a424c450308d6ab788493bdcd2d6a480f560954
[dragonfly.git] / lib / libc / citrus / modules / citrus_euc.c
1 /* $NetBSD: citrus_euc.c,v 1.11 2006/03/19 01:25:44 christos Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_euc.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
3
4 /*-
5  * Copyright (c)2002 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/types.h>
63 #include <assert.h>
64 #include <errno.h>
65 #include <limits.h>
66 #include <locale.h>
67 #include <stddef.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <wchar.h>
72
73 #include "citrus_namespace.h"
74 #include "citrus_types.h"
75 #include "citrus_module.h"
76 #include "citrus_ctype.h"
77 #include "citrus_stdenc.h"
78 #include "citrus_euc.h"
79
80
81 /* ----------------------------------------------------------------------
82  * private stuffs used by templates
83  */
84
85 typedef struct {
86         char ch[3];
87         int chlen;
88 } _EUCState;
89
90 typedef struct {
91         unsigned        count[4];
92         wchar_t         bits[4];
93         wchar_t         mask;
94         unsigned        mb_cur_max;
95 } _EUCEncodingInfo;
96
97 typedef struct {
98         _EUCEncodingInfo        ei;
99         struct {
100                 /* for future multi-locale facility */
101                 _EUCState       s_mblen;
102                 _EUCState       s_mbrlen;
103                 _EUCState       s_mbrtowc;
104                 _EUCState       s_mbtowc;
105                 _EUCState       s_mbsrtowcs;
106                 _EUCState       s_wcrtomb;
107                 _EUCState       s_wcsrtombs;
108                 _EUCState       s_wctomb;
109         } states;
110 } _EUCCTypeInfo;
111
112 #define _SS2    0x008e
113 #define _SS3    0x008f
114
115 #define _CEI_TO_EI(_cei_)               (&(_cei_)->ei)
116 #define _CEI_TO_STATE(_cei_, _func_)    (_cei_)->states.s_##_func_
117
118 #define _FUNCNAME(m)                    _citrus_EUC_##m
119 #define _ENCODING_INFO                  _EUCEncodingInfo
120 #define _CTYPE_INFO                     _EUCCTypeInfo
121 #define _ENCODING_STATE                 _EUCState
122 #define _ENCODING_MB_CUR_MAX(_ei_)      (_ei_)->mb_cur_max
123 #define _ENCODING_IS_STATE_DEPENDENT    0
124 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)        0
125
126
127 static __inline int
128 _citrus_EUC_cs(unsigned int c)
129 {
130         c &= 0xff;
131
132         return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
133 }
134
135 static __inline int
136 _citrus_EUC_parse_variable(_EUCEncodingInfo *ei,
137                            const void *var, size_t lenvar)
138 {
139         const char *v, *e;
140         int x;
141
142         /* parse variable string */
143         if (!var)
144                 return (EFTYPE);
145
146         v = (const char *) var;
147
148         while (*v == ' ' || *v == '\t')
149                 ++v;
150
151         ei->mb_cur_max = 1;
152         for (x = 0; x < 4; ++x) {
153                 ei->count[x] = (int) strtol(v, (char **)&e, 0);
154                 if (v == e || !(v = e) || ei->count[x]<1 || ei->count[x]>4) {
155                         return (EFTYPE);
156                 }
157                 if (ei->mb_cur_max < ei->count[x])
158                         ei->mb_cur_max = ei->count[x];
159                 while (*v == ' ' || *v == '\t')
160                         ++v;
161                 ei->bits[x] = (int) strtol(v, (char **)&e, 0);
162                 if (v == e || !(v = e)) {
163                         return (EFTYPE);
164                 }
165                 while (*v == ' ' || *v == '\t')
166                         ++v;
167         }
168         ei->mask = (int)strtol(v, (char **)&e, 0);
169         if (v == e || !(v = e)) {
170                 return (EFTYPE);
171         }
172
173         return 0;
174 }
175
176
177 static __inline void
178 /*ARGSUSED*/
179 _citrus_EUC_init_state(_EUCEncodingInfo *ei, _EUCState *s)
180 {
181         memset(s, 0, sizeof(*s));
182 }
183
184 static __inline void
185 /*ARGSUSED*/
186 _citrus_EUC_pack_state(_EUCEncodingInfo *ei, void *pspriv, const _EUCState *s)
187 {
188         memcpy(pspriv, (const void *)s, sizeof(*s));
189 }
190
191 static __inline void
192 /*ARGSUSED*/
193 _citrus_EUC_unpack_state(_EUCEncodingInfo *ei, _EUCState *s,
194                          const void *pspriv)
195 {
196         memcpy((void *)s, pspriv, sizeof(*s));
197 }
198
199 static int
200 _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, const char **s,
201                          size_t n, _EUCState *psenc, size_t *nresult)
202 {
203         wchar_t wchar;
204         int c, cs, len;
205         int chlenbak;
206         const char *s0, *s1 = NULL;
207
208         _DIAGASSERT(nresult != 0);
209         _DIAGASSERT(ei != NULL);
210         _DIAGASSERT(psenc != NULL);
211         _DIAGASSERT(s != NULL);
212
213         s0 = *s;
214
215         if (s0 == NULL) {
216                 _citrus_EUC_init_state(ei, psenc);
217                 *nresult = 0; /* state independent */
218                 return (0);
219         }
220
221         chlenbak = psenc->chlen;
222
223         /* make sure we have the first byte in the buffer */
224         switch (psenc->chlen) {
225         case 0:
226                 if (n < 1)
227                         goto restart;
228                 psenc->ch[0] = *s0++;
229                 psenc->chlen = 1;
230                 n--;
231                 break;
232         case 1:
233         case 2:
234                 break;
235         default:
236                 /* illgeal state */
237                 goto encoding_error;
238         }
239
240         c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)];
241         if (c == 0)
242                 goto encoding_error;
243         while (psenc->chlen < c) {
244                 if (n < 1)
245                         goto restart;
246                 psenc->ch[psenc->chlen] = *s0++;
247                 psenc->chlen++;
248                 n--;
249         }
250         *s = s0;
251
252         switch (cs) {
253         case 3:
254         case 2:
255                 /* skip SS2/SS3 */
256                 len = c - 1;
257                 s1 = &psenc->ch[1];
258                 break;
259         case 1:
260         case 0:
261                 len = c;
262                 s1 = &psenc->ch[0];
263                 break;
264         default:
265                 goto encoding_error;
266         }
267         wchar = 0;
268         while (len-- > 0)
269                 wchar = (wchar << 8) | (*s1++ & 0xff);
270         wchar = (wchar & ~ei->mask) | ei->bits[cs];
271
272         psenc->chlen = 0;
273         if (pwc)
274                 *pwc = wchar;
275
276         if (!wchar) {
277                 *nresult = 0;
278         } else {
279                 *nresult = (size_t)(c - chlenbak);
280         }
281
282         return 0;
283
284 encoding_error:
285         psenc->chlen = 0;
286         *nresult = (size_t)-1;
287         return (EILSEQ);
288
289 restart:
290         *nresult = (size_t)-2;
291         *s = s0;
292         return (0);
293 }
294
295 static int
296 _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc,
297                          _EUCState *psenc, size_t *nresult)
298 {
299         wchar_t m, nm;
300         int cs, i, ret;
301
302         _DIAGASSERT(ei != NULL);
303         _DIAGASSERT(nresult != 0);
304         _DIAGASSERT(s != NULL);
305
306         m = wc & ei->mask;
307         nm = wc & ~m;
308
309         for (cs = 0;
310              cs < sizeof(ei->count)/sizeof(ei->count[0]);
311              cs++) {
312                 if (m == ei->bits[cs])
313                         break;
314         }
315         /* fallback case - not sure if it is necessary */
316         if (cs == sizeof(ei->count)/sizeof(ei->count[0]))
317                 cs = 1;
318
319         i = ei->count[cs];
320         if (n < i) {
321                 ret = E2BIG;
322                 goto err;
323         }
324         m = (cs) ? 0x80 : 0x00;
325         switch (cs) {
326         case 2:
327                 *s++ = _SS2;
328                 i--;
329                 break;
330         case 3:
331                 *s++ = _SS3;
332                 i--;
333                 break;
334         }
335
336         while (i-- > 0)
337                 *s++ = ((nm >> (i << 3)) & 0xff) | m;
338
339         *nresult = (size_t)ei->count[cs];
340         return 0;
341
342 err:
343         *nresult = (size_t)-1;
344         return ret;
345 }
346
347 static __inline int
348 /*ARGSUSED*/
349 _citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,
350                           _csid_t * __restrict csid,
351                           _index_t * __restrict idx, wchar_t wc)
352 {
353         wchar_t m, nm;
354
355         _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL);
356
357         m = wc & ei->mask;
358         nm = wc & ~m;
359
360         *csid = (_citrus_csid_t)m;
361         *idx  = (_citrus_index_t)nm;
362
363         return (0);
364 }
365
366 static __inline int
367 /*ARGSUSED*/
368 _citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,
369                           wchar_t * __restrict wc,
370                           _csid_t csid, _index_t idx)
371 {
372
373         _DIAGASSERT(ei != NULL && wc != NULL);
374
375         if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0)
376                 return (EINVAL);
377
378         *wc = (wchar_t)csid | (wchar_t)idx;
379
380         return (0);
381 }
382
383 static __inline int
384 /*ARGSUSED*/
385 _citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei,
386                                           _EUCState * __restrict psenc,
387                                           int * __restrict rstate)
388 {
389
390         if (psenc->chlen == 0)
391                 *rstate = _STDENC_SDGEN_INITIAL;
392         else
393                 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
394
395         return 0;
396 }
397
398 static int
399 /*ARGSUSED*/
400 _citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,
401                                  const void * __restrict var, size_t lenvar)
402 {
403
404         _DIAGASSERT(ei != NULL);
405
406         return (_citrus_EUC_parse_variable(ei, var, lenvar));
407 }
408
409 static void
410 /*ARGSUSED*/
411 _citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei)
412 {
413 }
414
415 /* ----------------------------------------------------------------------
416  * public interface for ctype
417  */
418
419 _CITRUS_CTYPE_DECLS(EUC);
420 _CITRUS_CTYPE_DEF_OPS(EUC);
421
422 #include "citrus_ctype_template.h"
423
424 /* ----------------------------------------------------------------------
425  * public interface for stdenc
426  */
427
428 _CITRUS_STDENC_DECLS(EUC);
429 _CITRUS_STDENC_DEF_OPS(EUC);
430
431 #include "citrus_stdenc_template.h"