keylogin(1): Fix a warning and raise WARNS to 6.
[dragonfly.git] / lib / libc / citrus / modules / citrus_utf8.c
1 /* $NetBSD: citrus_utf8.c,v 1.16 2007/03/06 16:13:58 tnozaki 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_utf8.h"
78
79
80 /* ----------------------------------------------------------------------
81  * private stuffs used by templates
82  */
83
84 static int _UTF8_count_array[256];
85 static int const *_UTF8_count = NULL;
86
87 static const u_int32_t _UTF8_range[] = {
88         0,      /*dummy*/
89         0x00000000, 0x00000080, 0x00000800, 0x00010000, 
90         0x00200000, 0x04000000, 0x80000000,
91 };
92
93 typedef struct {
94         char ch[6];
95         int chlen;
96 } _UTF8State;
97
98 typedef struct {
99 } _UTF8EncodingInfo;
100
101 typedef struct {
102         _UTF8EncodingInfo       ei;
103         struct {
104                 /* for future multi-locale facility */
105                 _UTF8State      s_mblen;
106                 _UTF8State      s_mbrlen;
107                 _UTF8State      s_mbrtowc;
108                 _UTF8State      s_mbtowc;
109                 _UTF8State      s_mbsrtowcs;
110                 _UTF8State      s_wcrtomb;
111                 _UTF8State      s_wcsrtombs;
112                 _UTF8State      s_wctomb;
113         } states;
114 } _UTF8CTypeInfo;
115
116 #define _CEI_TO_EI(_cei_)               (&(_cei_)->ei)
117 #define _CEI_TO_STATE(_ei_, _func_)     (_ei_)->states.s_##_func_
118
119 #define _FUNCNAME(m)                    _citrus_UTF8_##m
120 #define _ENCODING_INFO                  _UTF8EncodingInfo
121 #define _CTYPE_INFO                     _UTF8CTypeInfo
122 #define _ENCODING_STATE                 _UTF8State
123 #define _ENCODING_MB_CUR_MAX(_ei_)      6
124 #define _ENCODING_IS_STATE_DEPENDENT    0
125 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)        0
126
127
128 static __inline void
129 _UTF8_init_count(void)
130 {
131         int i;
132         if (!_UTF8_count) {
133                 memset(_UTF8_count_array, 0, sizeof(_UTF8_count_array));
134                 for (i = 0; i <= 0x7f; i++)
135                         _UTF8_count_array[i] = 1;
136                 for (i = 0xc0; i <= 0xdf; i++)
137                         _UTF8_count_array[i] = 2;
138                 for (i = 0xe0; i <= 0xef; i++)
139                         _UTF8_count_array[i] = 3;
140                 for (i = 0xf0; i <= 0xf7; i++)
141                         _UTF8_count_array[i] = 4;
142                 for (i = 0xf8; i <= 0xfb; i++)
143                         _UTF8_count_array[i] = 5;
144                 for (i = 0xfc; i <= 0xfd; i++)
145                         _UTF8_count_array[i] = 6;
146                 _UTF8_count = _UTF8_count_array;
147         }
148 }
149
150 static int
151 _UTF8_findlen(wchar_t v)
152 {
153         int i;
154         u_int32_t c;
155
156         c = (u_int32_t)v;       /*XXX*/
157         for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]) - 1; i++)
158                 if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1])
159                         return i;
160
161         return -1;      /*out of range*/
162 }
163
164 static __inline int
165 _UTF8_surrogate(wchar_t wc)
166 {
167         return wc >= 0xd800 && wc <= 0xdfff;
168 }
169
170 static __inline void
171 /*ARGSUSED*/
172 _citrus_UTF8_init_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s)
173 {
174         s->chlen = 0;
175 }
176
177 static __inline void
178 /*ARGSUSED*/
179 _citrus_UTF8_pack_state(_UTF8EncodingInfo *ei __unused, void *pspriv,
180                         const _UTF8State *s)
181 {
182         memcpy(pspriv, (const void *)s, sizeof(*s));
183 }
184
185 static __inline void
186 /*ARGSUSED*/
187 _citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s,
188                           const void *pspriv)
189 {
190         memcpy((void *)s, pspriv, sizeof(*s));
191 }
192
193 static int
194 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
195                           size_t n, _UTF8State *psenc, size_t *nresult)
196 {
197         wchar_t wchar;
198         const char *s0;
199         int c;
200         int i;
201
202         _DIAGASSERT(nresult != NULL);
203         _DIAGASSERT(s != NULL);
204         _DIAGASSERT(psenc != NULL);
205
206         s0 = *s;
207
208         if (s0 == NULL) {
209                 _citrus_UTF8_init_state(ei, psenc);
210                 *nresult = 0; /* state independent */
211                 return 0;
212         }
213
214         /* make sure we have the first byte in the buffer */
215         if (psenc->chlen == 0) {
216                 if (n-- < 1)
217                         goto restart;
218                 psenc->ch[psenc->chlen++] = *s0++;
219         }
220
221         c = _UTF8_count[psenc->ch[0] & 0xff];
222         if (c < 1 || c < psenc->chlen)
223                 goto ilseq;
224
225         if (c == 1)
226                 wchar = psenc->ch[0] & 0xff;
227         else {
228                 while (psenc->chlen < c) {
229                         if (n-- < 1)
230                                 goto restart;
231                         psenc->ch[psenc->chlen++] = *s0++;
232                 }
233                 wchar = psenc->ch[0] & (0x7f >> c);
234                 for (i = 1; i < c; i++) {
235                         if ((psenc->ch[i] & 0xc0) != 0x80)
236                                 goto ilseq;
237                         wchar <<= 6;
238                         wchar |= (psenc->ch[i] & 0x3f);
239                 }
240                 if (_UTF8_surrogate(wchar) || _UTF8_findlen(wchar) != c)
241                         goto ilseq;
242         }
243         if (pwc != NULL)
244                 *pwc = wchar;
245         *nresult = (wchar == 0) ? 0 : s0 - *s;
246         *s = s0;
247         psenc->chlen = 0;
248
249         return 0;
250
251 ilseq:
252         *nresult = (size_t)-1;
253         return EILSEQ;
254
255 restart:
256         *s = s0;
257         *nresult = (size_t)-2;
258         return 0;
259 }
260
261 static int
262 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei __unused,
263                           char *s, size_t n, wchar_t wc,
264                           _UTF8State *psenc __unused, size_t *nresult)
265 {
266         int cnt, i, ret;
267         wchar_t c;
268
269         _DIAGASSERT(nresult != NULL);
270         _DIAGASSERT(s != NULL);
271         
272         if (_UTF8_surrogate(wc)) {
273                 ret = EILSEQ;
274                 goto err;
275         }
276         cnt = _UTF8_findlen(wc);
277         if (cnt <= 0 || cnt > 6) {
278                 /* invalid UCS4 value */
279                 ret = EILSEQ;
280                 goto err;
281         }
282         if (n < cnt) {
283                 /* bound check failure */
284                 ret = E2BIG;
285                 goto err;
286         }
287
288         c = wc;
289         if (s) {
290                 for (i = cnt - 1; i > 0; i--) {
291                         s[i] = 0x80 | (c & 0x3f);
292                         c >>= 6;
293                 }
294                 s[0] = c;
295                 if (cnt == 1)
296                         s[0] &= 0x7f;
297                 else {
298                         s[0] &= (0x7f >> cnt);
299                         s[0] |= ((0xff00 >> cnt) & 0xff);
300                 }
301         }
302
303         *nresult = (size_t)cnt;
304         return 0;
305
306 err:
307         *nresult = (size_t)-1;
308         return ret;
309 }
310
311 static __inline int
312 /*ARGSUSED*/
313 _citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei __unused,
314                            _csid_t * __restrict csid,
315                            _index_t * __restrict idx,
316                            wchar_t wc)
317 {
318
319         _DIAGASSERT(csid != NULL && idx != NULL);
320
321         *csid = 0;
322         *idx = (_citrus_index_t)wc;
323
324         return (0);
325 }
326
327 static __inline int
328 /*ARGSUSED*/
329 _citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei __unused,
330                            wchar_t * __restrict wc,
331                            _csid_t csid, _index_t idx)
332 {
333
334         _DIAGASSERT(wc != NULL);
335
336         if (csid != 0)
337                 return (EILSEQ);
338
339         *wc = (wchar_t)idx;
340
341         return (0);
342 }
343
344 static __inline int
345 /*ARGSUSED*/
346 _citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo * __restrict ei __unused,
347                                            _UTF8State * __restrict psenc,
348                                            int * __restrict rstate)
349 {
350
351         if (psenc->chlen == 0)
352                 *rstate = _STDENC_SDGEN_INITIAL;
353         else
354                 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
355
356         return 0;
357 }
358
359 static int
360 /*ARGSUSED*/
361 _citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei __unused,
362                                   const void * __restrict var __unused,
363                                   size_t lenvar __unused)
364 {
365         _UTF8_init_count();
366
367         return 0;
368 }
369
370 static void
371 /*ARGSUSED*/
372 _citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei __unused)
373 {
374 }
375
376
377 /* ----------------------------------------------------------------------
378  * public interface for ctype
379  */
380
381 _CITRUS_CTYPE_DECLS(UTF8);
382 _CITRUS_CTYPE_DEF_OPS(UTF8);
383
384 #include "citrus_ctype_template.h"
385
386 /* ----------------------------------------------------------------------
387  * public interface for stdenc
388  */
389
390 _CITRUS_STDENC_DECLS(UTF8);
391 _CITRUS_STDENC_DEF_OPS(UTF8);
392
393 #include "citrus_stdenc_template.h"