keylogin(1): Fix a warning and raise WARNS to 6.
[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
3 /*-
4  * Copyright (c)2002 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  * Copyright (c) 1993
31  *      The Regents of the University of California.  All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * Paul Borman at Krystal Technologies.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60
61 #include <sys/types.h>
62 #include <assert.h>
63 #include <errno.h>
64 #include <limits.h>
65 #include <locale.h>
66 #include <stddef.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <wchar.h>
71
72 #include "citrus_namespace.h"
73 #include "citrus_types.h"
74 #include "citrus_module.h"
75 #include "citrus_ctype.h"
76 #include "citrus_stdenc.h"
77 #include "citrus_euc.h"
78
79
80 /* ----------------------------------------------------------------------
81  * private stuffs used by templates
82  */
83
84 typedef struct {
85         char ch[3];
86         int chlen;
87 } _EUCState;
88
89 typedef struct {
90         unsigned        count[4];
91         wchar_t         bits[4];
92         wchar_t         mask;
93         unsigned        mb_cur_max;
94 } _EUCEncodingInfo;
95
96 typedef struct {
97         _EUCEncodingInfo        ei;
98         struct {
99                 /* for future multi-locale facility */
100                 _EUCState       s_mblen;
101                 _EUCState       s_mbrlen;
102                 _EUCState       s_mbrtowc;
103                 _EUCState       s_mbtowc;
104                 _EUCState       s_mbsrtowcs;
105                 _EUCState       s_wcrtomb;
106                 _EUCState       s_wcsrtombs;
107                 _EUCState       s_wctomb;
108         } states;
109 } _EUCCTypeInfo;
110
111 #define _SS2    0x008e
112 #define _SS3    0x008f
113
114 #define _CEI_TO_EI(_cei_)               (&(_cei_)->ei)
115 #define _CEI_TO_STATE(_cei_, _func_)    (_cei_)->states.s_##_func_
116
117 #define _FUNCNAME(m)                    _citrus_EUC_##m
118 #define _ENCODING_INFO                  _EUCEncodingInfo
119 #define _CTYPE_INFO                     _EUCCTypeInfo
120 #define _ENCODING_STATE                 _EUCState
121 #define _ENCODING_MB_CUR_MAX(_ei_)      (_ei_)->mb_cur_max
122 #define _ENCODING_IS_STATE_DEPENDENT    0
123 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)        0
124
125
126 static __inline int
127 _citrus_EUC_cs(unsigned int c)
128 {
129         c &= 0xff;
130
131         return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
132 }
133
134 static __inline int
135 _citrus_EUC_parse_variable(_EUCEncodingInfo *ei,
136                            const void *var, size_t lenvar)
137 {
138         const char *v, *e;
139         int x;
140
141         /* parse variable string */
142         if (!var)
143                 return (EFTYPE);
144
145         v = (const char *) var;
146
147         while (*v == ' ' || *v == '\t')
148                 ++v;
149
150         ei->mb_cur_max = 1;
151         for (x = 0; x < 4; ++x) {
152                 ei->count[x] = (int) strtol(v, (char **)&e, 0);
153                 if (v == e || !(v = e) || ei->count[x]<1 || ei->count[x]>4) {
154                         return (EFTYPE);
155                 }
156                 if (ei->mb_cur_max < ei->count[x])
157                         ei->mb_cur_max = ei->count[x];
158                 while (*v == ' ' || *v == '\t')
159                         ++v;
160                 ei->bits[x] = (int) strtol(v, (char **)&e, 0);
161                 if (v == e || !(v = e)) {
162                         return (EFTYPE);
163                 }
164                 while (*v == ' ' || *v == '\t')
165                         ++v;
166         }
167         ei->mask = (int)strtol(v, (char **)&e, 0);
168         if (v == e || !(v = e)) {
169                 return (EFTYPE);
170         }
171
172         return 0;
173 }
174
175
176 static __inline void
177 /*ARGSUSED*/
178 _citrus_EUC_init_state(_EUCEncodingInfo *ei, _EUCState *s)
179 {
180         memset(s, 0, sizeof(*s));
181 }
182
183 static __inline void
184 /*ARGSUSED*/
185 _citrus_EUC_pack_state(_EUCEncodingInfo *ei, void *pspriv, const _EUCState *s)
186 {
187         memcpy(pspriv, (const void *)s, sizeof(*s));
188 }
189
190 static __inline void
191 /*ARGSUSED*/
192 _citrus_EUC_unpack_state(_EUCEncodingInfo *ei, _EUCState *s,
193                          const void *pspriv)
194 {
195         memcpy((void *)s, pspriv, sizeof(*s));
196 }
197
198 static int
199 _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, const char **s,
200                          size_t n, _EUCState *psenc, size_t *nresult)
201 {
202         wchar_t wchar;
203         int c, cs, len;
204         int chlenbak;
205         const char *s0, *s1 = NULL;
206
207         _DIAGASSERT(nresult != NULL);
208         _DIAGASSERT(ei != NULL);
209         _DIAGASSERT(psenc != NULL);
210         _DIAGASSERT(s != NULL);
211
212         s0 = *s;
213
214         if (s0 == NULL) {
215                 _citrus_EUC_init_state(ei, psenc);
216                 *nresult = 0; /* state independent */
217                 return (0);
218         }
219
220         chlenbak = psenc->chlen;
221
222         /* make sure we have the first byte in the buffer */
223         switch (psenc->chlen) {
224         case 0:
225                 if (n < 1)
226                         goto restart;
227                 psenc->ch[0] = *s0++;
228                 psenc->chlen = 1;
229                 n--;
230                 break;
231         case 1:
232         case 2:
233                 break;
234         default:
235                 /* illgeal state */
236                 goto encoding_error;
237         }
238
239         c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)];
240         if (c == 0)
241                 goto encoding_error;
242         while (psenc->chlen < c) {
243                 if (n < 1)
244                         goto restart;
245                 psenc->ch[psenc->chlen] = *s0++;
246                 psenc->chlen++;
247                 n--;
248         }
249         *s = s0;
250
251         switch (cs) {
252         case 3:
253         case 2:
254                 /* skip SS2/SS3 */
255                 len = c - 1;
256                 s1 = &psenc->ch[1];
257                 break;
258         case 1:
259         case 0:
260                 len = c;
261                 s1 = &psenc->ch[0];
262                 break;
263         default:
264                 goto encoding_error;
265         }
266         wchar = 0;
267         while (len-- > 0)
268                 wchar = (wchar << 8) | (*s1++ & 0xff);
269         wchar = (wchar & ~ei->mask) | ei->bits[cs];
270
271         psenc->chlen = 0;
272         if (pwc)
273                 *pwc = wchar;
274
275         if (!wchar) {
276                 *nresult = 0;
277         } else {
278                 *nresult = (size_t)(c - chlenbak);
279         }
280
281         return 0;
282
283 encoding_error:
284         psenc->chlen = 0;
285         *nresult = (size_t)-1;
286         return (EILSEQ);
287
288 restart:
289         *nresult = (size_t)-2;
290         *s = s0;
291         return (0);
292 }
293
294 static int
295 _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc,
296                          _EUCState *psenc, size_t *nresult)
297 {
298         wchar_t m, nm;
299         int cs, i, ret;
300
301         _DIAGASSERT(ei != NULL);
302         _DIAGASSERT(nresult != NULL);
303         _DIAGASSERT(s != NULL);
304
305         m = wc & ei->mask;
306         nm = wc & ~m;
307
308         for (cs = 0;
309              cs < sizeof(ei->count)/sizeof(ei->count[0]);
310              cs++) {
311                 if (m == ei->bits[cs])
312                         break;
313         }
314         /* fallback case - not sure if it is necessary */
315         if (cs == sizeof(ei->count)/sizeof(ei->count[0]))
316                 cs = 1;
317
318         i = ei->count[cs];
319         if (n < i) {
320                 ret = E2BIG;
321                 goto err;
322         }
323         m = (cs) ? 0x80 : 0x00;
324         switch (cs) {
325         case 2:
326                 *s++ = _SS2;
327                 i--;
328                 break;
329         case 3:
330                 *s++ = _SS3;
331                 i--;
332                 break;
333         }
334
335         while (i-- > 0)
336                 *s++ = ((nm >> (i << 3)) & 0xff) | m;
337
338         *nresult = (size_t)ei->count[cs];
339         return 0;
340
341 err:
342         *nresult = (size_t)-1;
343         return ret;
344 }
345
346 static __inline int
347 /*ARGSUSED*/
348 _citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,
349                           _csid_t * __restrict csid,
350                           _index_t * __restrict idx, wchar_t wc)
351 {
352         wchar_t m, nm;
353
354         _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL);
355
356         m = wc & ei->mask;
357         nm = wc & ~m;
358
359         *csid = (_citrus_csid_t)m;
360         *idx  = (_citrus_index_t)nm;
361
362         return (0);
363 }
364
365 static __inline int
366 /*ARGSUSED*/
367 _citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,
368                           wchar_t * __restrict wc,
369                           _csid_t csid, _index_t idx)
370 {
371
372         _DIAGASSERT(ei != NULL && wc != NULL);
373
374         if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0)
375                 return (EINVAL);
376
377         *wc = (wchar_t)csid | (wchar_t)idx;
378
379         return (0);
380 }
381
382 static __inline int
383 /*ARGSUSED*/
384 _citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei,
385                                           _EUCState * __restrict psenc,
386                                           int * __restrict rstate)
387 {
388
389         if (psenc->chlen == 0)
390                 *rstate = _STDENC_SDGEN_INITIAL;
391         else
392                 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
393
394         return 0;
395 }
396
397 static int
398 /*ARGSUSED*/
399 _citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,
400                                  const void * __restrict var, size_t lenvar)
401 {
402
403         _DIAGASSERT(ei != NULL);
404
405         return (_citrus_EUC_parse_variable(ei, var, lenvar));
406 }
407
408 static void
409 /*ARGSUSED*/
410 _citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei)
411 {
412 }
413
414 /* ----------------------------------------------------------------------
415  * public interface for ctype
416  */
417
418 _CITRUS_CTYPE_DECLS(EUC);
419 _CITRUS_CTYPE_DEF_OPS(EUC);
420
421 #include "citrus_ctype_template.h"
422
423 /* ----------------------------------------------------------------------
424  * public interface for stdenc
425  */
426
427 _CITRUS_STDENC_DECLS(EUC);
428 _CITRUS_STDENC_DEF_OPS(EUC);
429
430 #include "citrus_stdenc_template.h"