keylogin(1): Fix a warning and raise WARNS to 6.
[dragonfly.git] / lib / libc / citrus / modules / citrus_euctw.c
1 /* $NetBSD: citrus_euctw.c,v 1.10 2005/10/29 18:02:04 tshiozak 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)1999 Citrus Project,
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  *      $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
55  */
56
57 #include <sys/types.h>
58 #include <assert.h>
59 #include <errno.h>
60 #include <limits.h>
61 #include <locale.h>
62 #include <stddef.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <wchar.h>
67
68 #include "citrus_namespace.h"
69 #include "citrus_types.h"
70 #include "citrus_module.h"
71 #include "citrus_ctype.h"
72 #include "citrus_stdenc.h"
73 #include "citrus_euctw.h"
74
75
76 /* ----------------------------------------------------------------------
77  * private stuffs used by templates
78  */
79
80 typedef struct {
81         char ch[4];
82         int chlen;
83 } _EUCTWState;
84
85 typedef struct {
86         int dummy;
87 } _EUCTWEncodingInfo;
88 typedef struct {
89         _EUCTWEncodingInfo      ei;
90         struct {
91                 /* for future multi-locale facility */
92                 _EUCTWState     s_mblen;
93                 _EUCTWState     s_mbrlen;
94                 _EUCTWState     s_mbrtowc;
95                 _EUCTWState     s_mbtowc;
96                 _EUCTWState     s_mbsrtowcs;
97                 _EUCTWState     s_wcrtomb;
98                 _EUCTWState     s_wcsrtombs;
99                 _EUCTWState     s_wctomb;
100         } states;
101 } _EUCTWCTypeInfo;
102
103 #define _SS2    0x008e
104 #define _SS3    0x008f
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_EUCTW_##m
110 #define _ENCODING_INFO                  _EUCTWEncodingInfo
111 #define _CTYPE_INFO                     _EUCTWCTypeInfo
112 #define _ENCODING_STATE                 _EUCTWState
113 #define _ENCODING_MB_CUR_MAX(_ei_)      4
114 #define _ENCODING_IS_STATE_DEPENDENT    0
115 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)        0
116
117 static __inline int
118 _citrus_EUCTW_cs(u_int c)
119 {
120         c &= 0xff;
121
122         return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
123 }
124
125 static __inline int
126 _citrus_EUCTW_count(int cs)
127 {
128         switch (cs) {
129         case 0:
130                 return 1;
131         case 1:
132                 return 2;
133         case 2:
134                 return 4;
135         case 3:
136                 abort();
137                 /*NOTREACHED*/
138         }
139         return 0;
140 }
141
142 static __inline void
143 /*ARGSUSED*/
144 _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei,
145                          _EUCTWState * __restrict s)
146 {
147         memset(s, 0, sizeof(*s));
148 }
149
150 static __inline void
151 /*ARGSUSED*/
152 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei,
153                          void * __restrict pspriv,
154                          const _EUCTWState * __restrict s)
155 {
156         memcpy(pspriv, (const void *)s, sizeof(*s));
157 }
158
159 static __inline void
160 /*ARGSUSED*/
161 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei,
162                            _EUCTWState * __restrict s,
163                            const void * __restrict pspriv)
164 {
165         memcpy((void *)s, pspriv, sizeof(*s));
166 }
167
168 static int
169 /*ARGSUSED*/
170 _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,
171                                    const void * __restrict var, size_t lenvar)
172 {
173
174         _DIAGASSERT(ei != NULL);
175
176         memset((void *)ei, 0, sizeof(*ei));
177
178         return 0;
179 }
180
181 static void
182 /*ARGSUSED*/
183 _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei)
184 {
185 }
186
187 static int
188 _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,
189                            wchar_t * __restrict pwc,
190                            const char ** __restrict s,
191                            size_t n, _EUCTWState * __restrict psenc,
192                            size_t * __restrict nresult)
193 {
194         wchar_t wchar;
195         int c, cs;
196         int chlenbak;
197         const char *s0;
198
199         _DIAGASSERT(nresult != NULL);
200         _DIAGASSERT(ei != NULL);
201         _DIAGASSERT(psenc != NULL);
202         _DIAGASSERT(s != NULL);
203
204         s0 = *s;
205
206         if (s0 == NULL) {
207                 _citrus_EUCTW_init_state(ei, psenc);
208                 *nresult = 0; /* state independent */
209                 return (0);
210         }
211
212         chlenbak = psenc->chlen;
213
214         /* make sure we have the first byte in the buffer */
215         switch (psenc->chlen) {
216         case 0:
217                 if (n < 1)
218                         goto restart;
219                 psenc->ch[0] = *s0++;
220                 psenc->chlen = 1;
221                 n--;
222                 break;
223         case 1:
224         case 2:
225                 break;
226         default:
227                 /* illgeal state */
228                 goto ilseq;
229         }
230
231         c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
232         if (c == 0)
233                 goto ilseq;
234         while (psenc->chlen < c) {
235                 if (n < 1)
236                         goto ilseq;
237                 psenc->ch[psenc->chlen] = *s0++;
238                 psenc->chlen++;
239                 n--;
240         }
241
242         wchar = 0;
243         switch (cs) {
244         case 0:
245                 if (psenc->ch[0] & 0x80)
246                         goto ilseq;
247                 wchar = psenc->ch[0] & 0xff;
248                 break;
249         case 1:
250                 if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
251                         goto ilseq;
252                 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
253                 wchar |= 'G' << 24;
254                 break;
255         case 2:
256                 if ((u_char)psenc->ch[1] < 0xa1 || 0xa7 < (u_char)psenc->ch[1])
257                         goto ilseq;
258                 if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
259                         goto ilseq;
260                 wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
261                 wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
262                 break;
263         default:
264                 goto ilseq;
265         }
266
267         *s = s0;
268         psenc->chlen = 0;
269
270         if (pwc)
271                 *pwc = wchar;
272
273         if (!wchar)
274                 *nresult = 0;
275         else
276                 *nresult = c - chlenbak;
277
278         return (0);
279
280 ilseq:
281         psenc->chlen = 0;
282         *nresult = (size_t)-1;
283         return (EILSEQ);
284
285 restart:
286         *s = s0;
287         *nresult = (size_t)-1;
288         return (0);
289 }
290
291 static int
292 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei,
293                            char * __restrict s, size_t n, wchar_t wc,
294                            _EUCTWState * __restrict psenc,
295                            size_t * __restrict nresult)
296 {
297         wchar_t cs = wc & 0x7f000080;
298         wchar_t v;
299         int i, len, clen, ret;
300
301         _DIAGASSERT(ei != NULL);
302         _DIAGASSERT(nresult != NULL);
303         _DIAGASSERT(s != NULL);
304
305         clen = 1;
306         if (wc & 0x00007f00)
307                 clen = 2;
308         if ((wc & 0x007f0000) && !(wc & 0x00800000))
309                 clen = 3;
310
311         if (clen == 1 && cs == 0x00000000) {
312                 /* ASCII */
313                 len = 1;
314                 if (n < len) {
315                         ret = E2BIG;
316                         goto err;
317                 }
318                 v = wc & 0x0000007f;
319         } else if (clen == 2 && cs == ('G' << 24)) {
320                 /* CNS-11643-1 */
321                 len = 2;
322                 if (n < len) {
323                         ret = E2BIG;
324                         goto err;
325                 }
326                 v = wc & 0x00007f7f;
327                 v |= 0x00008080;
328         } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
329                 /* CNS-11643-[2-7] */
330                 len = 4;
331                 if (n < len) {
332                         ret = E2BIG;
333                         goto err;
334                 }
335                 *s++ = _SS2;
336                 *s++ = (cs >> 24) - 'H' + 0xa2;
337                 v = wc & 0x00007f7f;
338                 v |= 0x00008080;
339         } else {
340                 ret = EILSEQ;
341                 goto err;
342         }
343
344         i = clen;
345         while (i-- > 0)
346                 *s++ = (v >> (i << 3)) & 0xff;
347
348         *nresult = len;
349         return 0;
350
351 err:
352         *nresult = (size_t)-1;
353         return ret;
354 }
355
356 static __inline int
357 /*ARGSUSED*/
358 _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei,
359                             _csid_t * __restrict csid,
360                             _index_t * __restrict idx, wchar_t wc)
361 {
362
363         _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL);
364
365         *csid = (_csid_t)(wc >> 24) & 0xFF;
366         *idx  = (_index_t)(wc & 0x7F7F);
367
368         return (0);
369 }
370
371 static __inline int
372 /*ARGSUSED*/
373 _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei,
374                             wchar_t * __restrict wc,
375                             _csid_t csid, _index_t idx)
376 {
377
378         _DIAGASSERT(ei != NULL && wc != NULL);
379
380         if (csid==0) {
381                 if ((idx & ~0x7F) != 0)
382                         return (EINVAL);
383                 *wc = (wchar_t)idx;
384         } else {
385                 if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0)
386                         return (EINVAL);
387                 *wc = (wchar_t)idx | ((wchar_t)csid<<24);
388         }
389
390         return (0);
391 }
392
393 static __inline int
394 /*ARGSUSED*/
395 _citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei,
396                                             _EUCTWState * __restrict psenc,
397                                             int * __restrict rstate)
398 {
399
400         if (psenc->chlen == 0)
401                 *rstate = _STDENC_SDGEN_INITIAL;
402         else
403                 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
404
405         return 0;
406 }
407
408 /* ----------------------------------------------------------------------
409  * public interface for ctype
410  */
411
412 _CITRUS_CTYPE_DECLS(EUCTW);
413 _CITRUS_CTYPE_DEF_OPS(EUCTW);
414
415 #include "citrus_ctype_template.h"
416
417 /* ----------------------------------------------------------------------
418  * public interface for stdenc
419  */
420
421 _CITRUS_STDENC_DECLS(EUCTW);
422 _CITRUS_STDENC_DEF_OPS(EUCTW);
423
424 #include "citrus_stdenc_template.h"