libc: Some warning fixes.
[dragonfly.git] / lib / libc / citrus / modules / citrus_utf1632.c
blobac5916eaee6a275fa95053b81e2fff0c5efdbeb5
1 /* $NetBSD: citrus_utf1632.c,v 1.8 2008/03/20 11:47:45 tnozaki Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_utf1632.c,v 1.2 2008/04/10 10:21:02 hasso Exp $ */
4 /*-
5 * Copyright (c)2003 Citrus Project,
6 * All rights reserved.
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.
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.
30 #include <sys/types.h>
31 #include <sys/endian.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <locale.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <wchar.h>
42 #include "citrus_namespace.h"
43 #include "citrus_types.h"
44 #include "citrus_module.h"
45 #include "citrus_stdenc.h"
46 #include "citrus_bcs.h"
48 #include "citrus_utf1632.h"
51 /* ----------------------------------------------------------------------
52 * private stuffs used by templates
55 typedef struct {
56 u_int8_t ch[4];
57 int chlen;
58 int current_endian;
59 } _UTF1632State;
61 typedef struct {
62 int preffered_endian;
63 unsigned int cur_max;
64 #define _ENDIAN_UNKNOWN 0
65 #define _ENDIAN_BIG 1
66 #define _ENDIAN_LITTLE 2
67 u_int32_t mode;
68 #define _MODE_UTF32 0x00000001U
69 #define _MODE_FORCE_ENDIAN 0x00000002U
70 } _UTF1632EncodingInfo;
72 #define _FUNCNAME(m) _citrus_UTF1632_##m
73 #define _ENCODING_INFO _UTF1632EncodingInfo
74 #define _ENCODING_STATE _UTF1632State
75 #define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max)
76 #define _ENCODING_IS_STATE_DEPENDENT 0
77 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
80 static __inline void
81 /*ARGSUSED*/
82 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei, _UTF1632State *s)
84 memset(s, 0, sizeof(*s));
87 static int
88 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc,
89 const char **s, size_t n, _UTF1632State *psenc,
90 size_t *nresult)
92 int chlenbak, endian, needlen;
93 wchar_t wc;
94 size_t result;
95 const char *s0;
97 _DIAGASSERT(nresult != 0);
98 _DIAGASSERT(ei != NULL);
99 _DIAGASSERT(s != NULL);
100 _DIAGASSERT(psenc != NULL);
102 s0 = *s;
104 if (s0 == NULL) {
105 _citrus_UTF1632_init_state(ei, psenc);
106 *nresult = 0; /* state independent */
107 return (0);
110 result = 0;
111 chlenbak = psenc->chlen;
113 refetch:
114 if ((ei->mode & _MODE_UTF32) != 0 || chlenbak>=2)
115 needlen = 4;
116 else
117 needlen = 2;
119 while (chlenbak < needlen) {
120 if (n==0)
121 goto restart;
122 psenc->ch[chlenbak++] = *s0++;
123 n--;
124 result++;
127 /* judge endian marker */
128 if ((ei->mode & _MODE_UTF32) == 0) {
129 /* UTF16 */
130 if (psenc->ch[0]==0xFE && psenc->ch[1]==0xFF) {
131 psenc->current_endian = _ENDIAN_BIG;
132 chlenbak = 0;
133 goto refetch;
134 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE) {
135 psenc->current_endian = _ENDIAN_LITTLE;
136 chlenbak = 0;
137 goto refetch;
139 } else {
140 /* UTF32 */
141 if (psenc->ch[0]==0x00 && psenc->ch[1]==0x00 &&
142 psenc->ch[2]==0xFE && psenc->ch[3]==0xFF) {
143 psenc->current_endian = _ENDIAN_BIG;
144 chlenbak = 0;
145 goto refetch;
146 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE &&
147 psenc->ch[2]==0x00 && psenc->ch[3]==0x00) {
148 psenc->current_endian = _ENDIAN_LITTLE;
149 chlenbak = 0;
150 goto refetch;
153 if ((ei->mode & _MODE_FORCE_ENDIAN) != 0 ||
154 psenc->current_endian == _ENDIAN_UNKNOWN)
155 endian = ei->preffered_endian;
156 else
157 endian = psenc->current_endian;
159 /* get wc */
160 if ((ei->mode & _MODE_UTF32) == 0) {
161 /* UTF16 */
162 if (needlen==2) {
163 switch (endian) {
164 case _ENDIAN_LITTLE:
165 wc = (psenc->ch[0] |
166 ((wchar_t)psenc->ch[1] << 8));
167 break;
168 case _ENDIAN_BIG:
169 wc = (psenc->ch[1] |
170 ((wchar_t)psenc->ch[0] << 8));
171 break;
172 default:
173 goto ilseq;
175 if (wc >= 0xD800 && wc <= 0xDBFF) {
176 /* surrogate high */
177 needlen=4;
178 goto refetch;
180 } else {
181 /* surrogate low */
182 wc -= 0xD800; /* wc : surrogate high (see above) */
183 wc <<= 10;
184 switch (endian) {
185 case _ENDIAN_LITTLE:
186 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
187 goto ilseq;
188 wc |= psenc->ch[2];
189 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
190 break;
191 case _ENDIAN_BIG:
192 if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF)
193 goto ilseq;
194 wc |= psenc->ch[3];
195 wc |= (wchar_t)(psenc->ch[2] & 3) << 8;
196 break;
197 default:
198 goto ilseq;
200 wc += 0x10000;
202 } else {
203 /* UTF32 */
204 switch (endian) {
205 case _ENDIAN_LITTLE:
206 wc = (psenc->ch[0] |
207 ((wchar_t)psenc->ch[1] << 8) |
208 ((wchar_t)psenc->ch[2] << 16) |
209 ((wchar_t)psenc->ch[3] << 24));
210 break;
211 case _ENDIAN_BIG:
212 wc = (psenc->ch[3] |
213 ((wchar_t)psenc->ch[2] << 8) |
214 ((wchar_t)psenc->ch[1] << 16) |
215 ((wchar_t)psenc->ch[0] << 24));
216 break;
217 default:
218 goto ilseq;
220 if (wc >= 0xD800 && wc <= 0xDFFF)
221 goto ilseq;
225 *pwc = wc;
226 psenc->chlen = 0;
227 *nresult = result;
228 *s = s0;
230 return (0);
232 ilseq:
233 *nresult = (size_t)-1;
234 psenc->chlen = 0;
235 return (EILSEQ);
237 restart:
238 *nresult = (size_t)-2;
239 psenc->chlen = chlenbak;
240 *s = s0;
241 return (0);
244 static int
245 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
246 wchar_t wc, _UTF1632State *psenc,
247 size_t *nresult)
249 wchar_t wc2;
250 static const char _bom[4] = {
251 #if BYTE_ORDER == BIG_ENDIAN
252 0x00, 0x00, 0xFE, 0xFF,
253 #else
254 0xFF, 0xFE, 0x00, 0x00,
255 #endif
257 const char *bom = &_bom[0];
258 size_t cnt;
260 _DIAGASSERT(ei != NULL);
261 _DIAGASSERT(nresult != 0);
262 _DIAGASSERT(s != NULL);
264 cnt = (size_t)0;
265 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
266 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
267 if (ei->mode & _MODE_UTF32) {
268 cnt = 4;
269 } else {
270 cnt = 2;
271 #if BYTE_ORDER == BIG_ENDIAN
272 bom += 2;
273 #endif
275 if (n < cnt)
276 goto e2big;
277 memcpy(s, bom, cnt);
278 s += cnt, n -= cnt;
280 psenc->current_endian = ei->preffered_endian;
283 wc2 = 0;
284 if ((ei->mode & _MODE_UTF32)==0) {
285 /* UTF16 */
286 if (wc>0xFFFF) {
287 /* surrogate */
288 if (wc>0x10FFFF)
289 goto ilseq;
290 if (n < 4)
291 goto e2big;
292 cnt += 4;
293 wc -= 0x10000;
294 wc2 = (wc & 0x3FF) | 0xDC00;
295 wc = (wc>>10) | 0xD800;
296 } else {
297 if (n < 2)
298 goto e2big;
299 cnt += 2;
302 surrogate:
303 switch (psenc->current_endian) {
304 case _ENDIAN_BIG:
305 s[1] = wc;
306 s[0] = (wc >>= 8);
307 break;
308 case _ENDIAN_LITTLE:
309 s[0] = wc;
310 s[1] = (wc >>= 8);
311 break;
313 if (wc2!=0) {
314 wc = wc2;
315 wc2 = 0;
316 s += 2;
317 goto surrogate;
319 } else {
320 /* UTF32 */
321 if (wc >= 0xD800 && wc <= 0xDFFF)
322 goto ilseq;
323 if (n < 4)
324 goto e2big;
325 cnt += 4;
326 switch (psenc->current_endian) {
327 case _ENDIAN_BIG:
328 s[3] = wc;
329 s[2] = (wc >>= 8);
330 s[1] = (wc >>= 8);
331 s[0] = (wc >>= 8);
332 break;
333 case _ENDIAN_LITTLE:
334 s[0] = wc;
335 s[1] = (wc >>= 8);
336 s[2] = (wc >>= 8);
337 s[3] = (wc >>= 8);
338 break;
341 *nresult = cnt;
343 return 0;
345 ilseq:
346 *nresult = (size_t)-1;
347 return EILSEQ;
348 e2big:
349 *nresult = (size_t)-1;
350 return E2BIG;
353 static void
354 parse_variable(_UTF1632EncodingInfo * __restrict ei,
355 const void * __restrict var, size_t lenvar)
357 #define MATCH(x, act) \
358 do { \
359 if (lenvar >= (sizeof(#x)-1) && \
360 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
361 act; \
362 lenvar -= sizeof(#x)-1; \
363 p += sizeof(#x)-1; \
365 } while (/*CONSTCOND*/0)
366 const char *p;
367 p = var;
368 while (lenvar>0) {
369 switch (*p) {
370 case 'B':
371 case 'b':
372 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
373 break;
374 case 'L':
375 case 'l':
376 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
377 break;
378 case 'F':
379 case 'f':
380 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
381 break;
382 case 'U':
383 case 'u':
384 MATCH(utf32, ei->mode |= _MODE_UTF32);
385 break;
387 p++;
388 lenvar--;
392 static int
393 /*ARGSUSED*/
394 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
395 const void * __restrict var,
396 size_t lenvar)
398 _DIAGASSERT(ei != NULL);
400 memset((void *)ei, 0, sizeof(*ei));
402 parse_variable(ei, var, lenvar);
404 if ((ei->mode&_MODE_UTF32)==0)
405 ei->cur_max = 6; /* endian + surrogate */
406 else
407 ei->cur_max = 8; /* endian + normal */
409 if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
410 #if BYTE_ORDER == BIG_ENDIAN
411 ei->preffered_endian = _ENDIAN_BIG;
412 #else
413 ei->preffered_endian = _ENDIAN_LITTLE;
414 #endif
417 return (0);
420 static void
421 /*ARGSUSED*/
422 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei)
426 static __inline int
427 /*ARGSUSED*/
428 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei,
429 _csid_t * __restrict csid,
430 _index_t * __restrict idx,
431 _wc_t wc)
434 _DIAGASSERT(csid != NULL && idx != NULL);
436 *csid = 0;
437 *idx = (_index_t)wc;
439 return (0);
442 static __inline int
443 /*ARGSUSED*/
444 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei,
445 _wc_t * __restrict wc,
446 _csid_t csid, _index_t idx)
449 _DIAGASSERT(wc != NULL);
451 if (csid != 0)
452 return (EILSEQ);
454 *wc = (_wc_t)idx;
456 return (0);
459 static __inline int
460 /*ARGSUSED*/
461 _citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo * __restrict ei,
462 _UTF1632State * __restrict psenc,
463 int * __restrict rstate)
466 if (psenc->chlen == 0)
467 *rstate = _STDENC_SDGEN_INITIAL;
468 else
469 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
471 return 0;
474 /* ----------------------------------------------------------------------
475 * public interface for stdenc
478 _CITRUS_STDENC_DECLS(UTF1632);
479 _CITRUS_STDENC_DEF_OPS(UTF1632);
481 #include "citrus_stdenc_template.h"