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 $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
30 #include <sys/types.h>
31 #include <sys/endian.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
64 #define _ENDIAN_UNKNOWN 0
66 #define _ENDIAN_LITTLE 2
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
82 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei, _UTF1632State *s)
84 memset(s, 0, sizeof(*s));
88 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc,
89 const char **s, size_t n, _UTF1632State *psenc,
92 int chlenbak, endian, needlen;
97 _DIAGASSERT(nresult != 0);
98 _DIAGASSERT(ei != NULL);
99 _DIAGASSERT(s != NULL);
100 _DIAGASSERT(psenc != NULL);
105 _citrus_UTF1632_init_state(ei, psenc);
106 *nresult = 0; /* state independent */
111 chlenbak = psenc->chlen;
114 if ((ei->mode & _MODE_UTF32) != 0 || chlenbak>=2)
119 while (chlenbak < needlen) {
122 psenc->ch[chlenbak++] = *s0++;
127 /* judge endian marker */
128 if ((ei->mode & _MODE_UTF32) == 0) {
130 if (psenc->ch[0]==0xFE && psenc->ch[1]==0xFF) {
131 psenc->current_endian = _ENDIAN_BIG;
134 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE) {
135 psenc->current_endian = _ENDIAN_LITTLE;
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;
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;
153 if ((ei->mode & _MODE_FORCE_ENDIAN) != 0 ||
154 psenc->current_endian == _ENDIAN_UNKNOWN)
155 endian = ei->preffered_endian;
157 endian = psenc->current_endian;
160 if ((ei->mode & _MODE_UTF32) == 0) {
166 ((wchar_t)psenc->ch[1] << 8));
170 ((wchar_t)psenc->ch[0] << 8));
175 if (wc >= 0xD800 && wc <= 0xDBFF) {
182 wc -= 0xD800; /* wc : surrogate high (see above) */
186 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
189 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
192 if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF)
195 wc |= (wchar_t)(psenc->ch[2] & 3) << 8;
207 ((wchar_t)psenc->ch[1] << 8) |
208 ((wchar_t)psenc->ch[2] << 16) |
209 ((wchar_t)psenc->ch[3] << 24));
213 ((wchar_t)psenc->ch[2] << 8) |
214 ((wchar_t)psenc->ch[1] << 16) |
215 ((wchar_t)psenc->ch[0] << 24));
220 if (wc >= 0xD800 && wc <= 0xDFFF)
233 *nresult = (size_t)-1;
238 *nresult = (size_t)-2;
239 psenc->chlen = chlenbak;
245 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
246 wchar_t wc, _UTF1632State *psenc,
250 static const char _bom[4] = {
251 #if BYTE_ORDER == BIG_ENDIAN
252 0x00, 0x00, 0xFE, 0xFF,
254 0xFF, 0xFE, 0x00, 0x00,
257 const char *bom = &_bom[0];
260 _DIAGASSERT(ei != NULL);
261 _DIAGASSERT(nresult != 0);
262 _DIAGASSERT(s != NULL);
265 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
266 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
267 if (ei->mode & _MODE_UTF32) {
271 #if BYTE_ORDER == BIG_ENDIAN
280 psenc->current_endian = ei->preffered_endian;
284 if ((ei->mode & _MODE_UTF32)==0) {
294 wc2 = (wc & 0x3FF) | 0xDC00;
295 wc = (wc>>10) | 0xD800;
303 switch (psenc->current_endian) {
321 if (wc >= 0xD800 && wc <= 0xDFFF)
326 switch (psenc->current_endian) {
346 *nresult = (size_t)-1;
349 *nresult = (size_t)-1;
354 parse_variable(_UTF1632EncodingInfo * __restrict ei,
355 const void * __restrict var, size_t lenvar)
357 #define MATCH(x, act) \
359 if (lenvar >= (sizeof(#x)-1) && \
360 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
362 lenvar -= sizeof(#x)-1; \
365 } while (/*CONSTCOND*/0)
372 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
376 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
380 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
384 MATCH(utf32, ei->mode |= _MODE_UTF32);
394 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
395 const void * __restrict var,
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 */
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;
413 ei->preffered_endian = _ENDIAN_LITTLE;
422 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei)
428 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei,
429 _csid_t * __restrict csid,
430 _index_t * __restrict idx,
434 _DIAGASSERT(csid != NULL && idx != NULL);
444 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei,
445 _wc_t * __restrict wc,
446 _csid_t csid, _index_t idx)
449 _DIAGASSERT(wc != NULL);
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;
469 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
474 /* ----------------------------------------------------------------------
475 * public interface for stdenc
478 _CITRUS_STDENC_DECLS(UTF1632);
479 _CITRUS_STDENC_DEF_OPS(UTF1632);
481 #include "citrus_stdenc_template.h"