citrus_utf8.c revision 1.2 1 /* $NetBSD: citrus_utf8.c,v 1.2 2002/03/18 08:56:32 yamt 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. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66 #if defined(LIBC_SCCS) && !defined(lint)
67 __RCSID("$NetBSD: citrus_utf8.c,v 1.2 2002/03/18 08:56:32 yamt Exp $");
68 #endif /* LIBC_SCCS and not lint */
69
70 #include <assert.h>
71 #include <errno.h>
72 #include <string.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <stddef.h>
76 #include <locale.h>
77 #include <wchar.h>
78 #include <sys/types.h>
79 #include <limits.h>
80 #include "citrus_module.h"
81 #include "citrus_ctype.h"
82 #include "citrus_utf8.h"
83
84
85 /* ----------------------------------------------------------------------
86 * private stuffs used by templates
87 */
88
89 static int _UTF8_count_array[256];
90 static int const *_UTF8_count = NULL;
91
92 static u_int32_t _UTF8_range[] = {
93 0, /*dummy*/
94 0x00000000, 0x00000080, 0x00000800, 0x00010000,
95 0x00200000, 0x04000000, 0x80000000,
96 };
97
98 typedef struct {
99 char ch[6];
100 int chlen;
101 } _UTF8State;
102
103 typedef struct {
104 } _UTF8EncodingInfo;
105 typedef struct {
106 _UTF8EncodingInfo ei;
107 struct {
108 /* for future multi-locale facility */
109 _UTF8State s_mblen;
110 _UTF8State s_mbrlen;
111 _UTF8State s_mbrtowc;
112 _UTF8State s_mbtowc;
113 _UTF8State s_mbsrtowcs;
114 _UTF8State s_wcrtomb;
115 _UTF8State s_wcsrtombs;
116 _UTF8State s_wcstombs;
117 _UTF8State s_wctomb;
118 } states;
119 } _UTF8CTypeInfo;
120
121 #define _TO_EI(_cl_) ((_UTF8EncodingInfo *)(_cl_))
122 #define _TO_CEI(_cl_) ((_UTF8CTypeInfo *)(_cl_))
123 #define _TO_STATE(_ps_) ((_UTF8State *)(_ps_))
124 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
125 #define _CEI_TO_STATE(_ei_, _func_) (_ei_)->states.s_##_func_
126
127 #define _FUNCNAME(m) _citrus_UTF8_##m
128 #define _ENCODING_INFO _UTF8EncodingInfo
129 #define _CTYPE_INFO _UTF8CTypeInfo
130 #define _ENCODING_STATE _UTF8State
131 #define _ENCODING_MB_CUR_MAX(_cl_) 6
132 #define _ENCODING_IS_STATE_DEPENDENT 0
133
134
135 static __inline void
136 _UTF8_init_count(void)
137 {
138 int i;
139 if (!_UTF8_count) {
140 memset(_UTF8_count_array, 0, sizeof(_UTF8_count_array));
141 for (i = 0; i <= 0x7f; i++)
142 _UTF8_count_array[i] = 1;
143 for (i = 0xc0; i <= 0xdf; i++)
144 _UTF8_count_array[i] = 2;
145 for (i = 0xe0; i <= 0xef; i++)
146 _UTF8_count_array[i] = 3;
147 for (i = 0xf0; i <= 0xf7; i++)
148 _UTF8_count_array[i] = 4;
149 for (i = 0xf8; i <= 0xfb; i++)
150 _UTF8_count_array[i] = 5;
151 for (i = 0xfc; i <= 0xfd; i++)
152 _UTF8_count_array[i] = 6;
153 _UTF8_count = _UTF8_count_array;
154 }
155 }
156
157 static int
158 _UTF8_findlen(wchar_t v)
159 {
160 int i;
161 u_int32_t c;
162
163 c = (u_int32_t)v; /*XXX*/
164 for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]); i++)
165 if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1])
166 return i;
167
168 return -1; /*out of range*/
169 }
170
171 static __inline void
172 /*ARGSUSED*/
173 _citrus_UTF8_init_state(_UTF8EncodingInfo *ei, _UTF8State *s)
174 {
175 memset(s, 0, sizeof(*s));
176 }
177
178 static __inline void
179 /*ARGSUSED*/
180 _citrus_UTF8_pack_state(_UTF8EncodingInfo *ei, void *pspriv,
181 const _UTF8State *s)
182 {
183 memcpy(pspriv, (const void *)s, sizeof(*s));
184 }
185
186 static __inline void
187 /*ARGSUSED*/
188 _citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei, _UTF8State *s,
189 const void *pspriv)
190 {
191 memcpy((void *)s, pspriv, sizeof(*s));
192 }
193
194 static int
195 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s,
196 size_t n, _UTF8State *psenc, size_t *nresult)
197 {
198 wchar_t wchar;
199 const char *s0;
200 int c;
201 int i;
202 int chlenbak;
203
204 _DIAGASSERT(nresult != 0);
205 _DIAGASSERT(ei != NULL);
206 _DIAGASSERT(s != NULL);
207 _DIAGASSERT(psenc != NULL);
208
209 s0 = *s;
210
211 if (s0 == NULL) {
212 _citrus_UTF8_init_state(ei, psenc);
213 *nresult = 0; /* state independent */
214 return (0);
215 }
216
217 chlenbak = psenc->chlen;
218
219 /* make sure we have the first byte in the buffer */
220 switch (psenc->chlen) {
221 case 0:
222 if (n < 1) {
223 goto restart;
224 }
225 psenc->ch[0] = *s0++;
226 psenc->chlen = 1;
227 n--;
228 break;
229 case 1: case 2: case 3: case 4: case 5:
230 break;
231 default:
232 /* illegal state */
233 goto ilseq;
234 }
235
236 c = _UTF8_count[psenc->ch[0] & 0xff];
237 if (c == 0)
238 goto ilseq;
239 while (psenc->chlen < c) {
240 if (n < 1) {
241 goto restart;
242 }
243 psenc->ch[psenc->chlen] = *s0++;
244 psenc->chlen++;
245 n--;
246 }
247
248 switch (c) {
249 case 1:
250 wchar = psenc->ch[0] & 0xff;
251 break;
252 case 2: case 3: case 4: case 5: case 6:
253 wchar = psenc->ch[0] & (0x7f >> c);
254 for (i = 1; i < c; i++) {
255 if ((psenc->ch[i] & 0xc0) != 0x80)
256 goto ilseq;
257 wchar <<= 6;
258 wchar |= (psenc->ch[i] & 0x3f);
259 }
260
261 _DIAGASSERT(findlen(wchar) == c);
262
263 break;
264 }
265
266 *s = s0;
267
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 return (EILSEQ);
283
284 restart:
285 *nresult = (size_t)-2;
286 *s = s0;
287 return (0);
288 }
289
290 static int
291 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei, char *s, size_t n, wchar_t wc,
292 _UTF8State *psenc, size_t *nresult)
293 {
294 int cnt, i;
295 wchar_t c;
296
297 _DIAGASSERT(ei != NULL);
298 _DIAGASSERT(nresult != 0);
299 _DIAGASSERT(s != NULL);
300
301 cnt = _UTF8_findlen(wc);
302 if (cnt <= 0 || cnt > 6) {
303 /* invalid UCS4 value */
304 goto ilseq;
305 }
306 if (n < cnt) {
307 /* bound check failure */
308 goto ilseq;
309 }
310
311 c = wc;
312 if (s) {
313 for (i = cnt - 1; i > 0; i--) {
314 s[i] = 0x80 | (c & 0x3f);
315 c >>= 6;
316 }
317 s[0] = c;
318 if (cnt == 1)
319 s[0] &= 0x7f;
320 else {
321 s[0] &= (0x7f >> cnt);
322 s[0] |= ((0xff00 >> cnt) & 0xff);
323 }
324 }
325
326 *nresult = (size_t)cnt;
327 return (0);
328
329 ilseq:
330 *nresult = (size_t)-1;
331 return (EILSEQ);
332 }
333
334
335 static int
336 /*ARGSUSED*/
337 _citrus_UTF8_stdencoding_init(_UTF8EncodingInfo * __restrict ei,
338 const void * __restrict var, size_t lenvar)
339 {
340 _DIAGASSERT(ei != NULL);
341
342 _UTF8_init_count();
343 memset((void *)ei, 0, sizeof(*ei));
344
345 return (0);
346 }
347
348 static void
349 /*ARGSUSED*/
350 _citrus_UTF8_stdencoding_uninit(_UTF8EncodingInfo *ei)
351 {
352 }
353
354
355 /* ----------------------------------------------------------------------
356 * public interface for ctype
357 */
358
359 _CITRUS_CTYPE_DECLS(UTF8);
360 _CITRUS_CTYPE_DEF_OPS(UTF8);
361
362 #include "citrus_ctype_template.h"
363