1 /* $NetBSD: src/lib/libc/citrus/modules/citrus_utf1632.c,v 1.3 2003/06/27 12:55:13 yamt Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_utf1632.c,v 1.1 2005/03/11 23:33:53 joerg 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));
173 if (wc >= 0xD800 && wc <= 0xDBFF) {
180 wc -= 0xD800; /* wc : surrogate high (see above) */
184 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
187 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
190 if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF)
193 wc |= (wchar_t)(psenc->ch[2] & 3) << 8;
203 ((wchar_t)psenc->ch[1] << 8) |
204 ((wchar_t)psenc->ch[2] << 16) |
205 ((wchar_t)psenc->ch[3] << 24));
209 ((wchar_t)psenc->ch[2] << 8) |
210 ((wchar_t)psenc->ch[1] << 16) |
211 ((wchar_t)psenc->ch[0] << 24));
225 *nresult = (size_t)-1;
230 *nresult = (size_t)-2;
231 psenc->chlen = chlenbak;
237 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
238 wchar_t wc, _UTF1632State *psenc,
244 _DIAGASSERT(ei != NULL);
245 _DIAGASSERT(nresult != 0);
246 _DIAGASSERT(s != NULL);
249 if ((ei->mode & _MODE_UTF32)==0) {
262 wc2 = (wc & 0x3FF) | 0xDC00;
263 wc = (wc>>10) | 0xD800;
264 *nresult = (size_t)4;
270 *nresult = (size_t)2;
274 switch (ei->preffered_endian) {
296 switch (ei->preffered_endian) {
310 *nresult = (size_t)4;
316 *nresult = (size_t)-1;
321 parse_variable(_UTF1632EncodingInfo * __restrict ei,
322 const void * __restrict var, size_t lenvar)
324 #define MATCH(x, act) \
326 if (lenvar >= (sizeof(#x)-1) && \
327 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
329 lenvar -= sizeof(#x)-1; \
332 } while (/*CONSTCOND*/0)
339 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
343 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
347 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
351 MATCH(utf32, ei->mode |= _MODE_UTF32);
361 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
362 const void * __restrict var,
365 _DIAGASSERT(ei != NULL);
367 memset((void *)ei, 0, sizeof(*ei));
369 parse_variable(ei, var, lenvar);
371 if ((ei->mode&_MODE_UTF32)==0)
372 ei->cur_max = 6; /* endian + surrogate */
374 ei->cur_max = 8; /* endian + normal */
376 if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
377 #if BYTE_ORDER == BIG_ENDIAN
378 ei->preffered_endian = _ENDIAN_BIG;
380 ei->preffered_endian = _ENDIAN_LITTLE;
389 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei)
395 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei,
396 _csid_t * __restrict csid,
397 _index_t * __restrict idx,
401 _DIAGASSERT(csid != NULL && idx != NULL);
411 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei,
412 _wc_t * __restrict wc,
413 _csid_t csid, _index_t idx)
416 _DIAGASSERT(wc != NULL);
427 /* ----------------------------------------------------------------------
428 * public interface for stdenc
431 _CITRUS_STDENC_DECLS(UTF1632);
432 _CITRUS_STDENC_DEF_OPS(UTF1632);
434 #include "citrus_stdenc_template.h"