1 /* $NetBSD: citrus_utf1632.c,v 1.8 2008/03/20 11:47:45 tnozaki Exp $ */
4 * Copyright (c)2003 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
30 #include <sys/endian.h>
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_module.h"
44 #include "citrus_stdenc.h"
45 #include "citrus_bcs.h"
47 #include "citrus_utf1632.h"
50 /* ----------------------------------------------------------------------
51 * private stuffs used by templates
63 #define _ENDIAN_UNKNOWN 0
65 #define _ENDIAN_LITTLE 2
67 #define _MODE_UTF32 0x00000001U
68 #define _MODE_FORCE_ENDIAN 0x00000002U
69 } _UTF1632EncodingInfo;
71 #define _FUNCNAME(m) _citrus_UTF1632_##m
72 #define _ENCODING_INFO _UTF1632EncodingInfo
73 #define _ENCODING_STATE _UTF1632State
74 #define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max)
75 #define _ENCODING_IS_STATE_DEPENDENT 0
76 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
81 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei, _UTF1632State *s)
83 memset(s, 0, sizeof(*s));
87 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc,
88 const char **s, size_t n, _UTF1632State *psenc,
91 int chlenbak, endian, needlen;
96 _DIAGASSERT(nresult != NULL);
97 _DIAGASSERT(ei != NULL);
98 _DIAGASSERT(s != NULL);
99 _DIAGASSERT(psenc != NULL);
104 _citrus_UTF1632_init_state(ei, psenc);
105 *nresult = 0; /* state independent */
110 chlenbak = psenc->chlen;
113 if ((ei->mode & _MODE_UTF32) != 0 || chlenbak>=2)
118 while (chlenbak < needlen) {
121 psenc->ch[chlenbak++] = *s0++;
126 /* judge endian marker */
127 if ((ei->mode & _MODE_UTF32) == 0) {
129 if (psenc->ch[0]==0xFE && psenc->ch[1]==0xFF) {
130 psenc->current_endian = _ENDIAN_BIG;
133 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE) {
134 psenc->current_endian = _ENDIAN_LITTLE;
140 if (psenc->ch[0]==0x00 && psenc->ch[1]==0x00 &&
141 psenc->ch[2]==0xFE && psenc->ch[3]==0xFF) {
142 psenc->current_endian = _ENDIAN_BIG;
145 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE &&
146 psenc->ch[2]==0x00 && psenc->ch[3]==0x00) {
147 psenc->current_endian = _ENDIAN_LITTLE;
152 if ((ei->mode & _MODE_FORCE_ENDIAN) != 0 ||
153 psenc->current_endian == _ENDIAN_UNKNOWN)
154 endian = ei->preffered_endian;
156 endian = psenc->current_endian;
159 if ((ei->mode & _MODE_UTF32) == 0) {
165 ((wchar_t)psenc->ch[1] << 8));
169 ((wchar_t)psenc->ch[0] << 8));
174 if (wc >= 0xD800 && wc <= 0xDBFF) {
181 wc -= 0xD800; /* wc : surrogate high (see above) */
185 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
188 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
191 if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF)
194 wc |= (wchar_t)(psenc->ch[2] & 3) << 8;
206 ((wchar_t)psenc->ch[1] << 8) |
207 ((wchar_t)psenc->ch[2] << 16) |
208 ((wchar_t)psenc->ch[3] << 24));
212 ((wchar_t)psenc->ch[2] << 8) |
213 ((wchar_t)psenc->ch[1] << 16) |
214 ((wchar_t)psenc->ch[0] << 24));
219 if (wc >= 0xD800 && wc <= 0xDFFF)
232 *nresult = (size_t)-1;
237 *nresult = (size_t)-2;
238 psenc->chlen = chlenbak;
244 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
245 wchar_t wc, _UTF1632State *psenc,
249 static const char _bom[4] = {
250 #if BYTE_ORDER == BIG_ENDIAN
251 0x00, 0x00, 0xFE, 0xFF,
253 0xFF, 0xFE, 0x00, 0x00,
256 const char *bom = &_bom[0];
259 _DIAGASSERT(ei != NULL);
260 _DIAGASSERT(nresult != NULL);
261 _DIAGASSERT(s != NULL);
264 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
265 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
266 if (ei->mode & _MODE_UTF32) {
270 #if BYTE_ORDER == BIG_ENDIAN
279 psenc->current_endian = ei->preffered_endian;
283 if ((ei->mode & _MODE_UTF32)==0) {
293 wc2 = (wc & 0x3FF) | 0xDC00;
294 wc = (wc>>10) | 0xD800;
302 switch (psenc->current_endian) {
320 if (wc >= 0xD800 && wc <= 0xDFFF)
325 switch (psenc->current_endian) {
345 *nresult = (size_t)-1;
348 *nresult = (size_t)-1;
353 parse_variable(_UTF1632EncodingInfo * __restrict ei,
354 const void * __restrict var, size_t lenvar)
356 #define MATCH(x, act) \
358 if (lenvar >= (sizeof(#x)-1) && \
359 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
361 lenvar -= sizeof(#x)-1; \
364 } while (/*CONSTCOND*/0)
371 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
375 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
379 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
383 MATCH(utf32, ei->mode |= _MODE_UTF32);
393 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
394 const void * __restrict var,
397 _DIAGASSERT(ei != NULL);
399 memset((void *)ei, 0, sizeof(*ei));
401 parse_variable(ei, var, lenvar);
403 if ((ei->mode&_MODE_UTF32)==0)
404 ei->cur_max = 6; /* endian + surrogate */
406 ei->cur_max = 8; /* endian + normal */
408 if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
409 #if BYTE_ORDER == BIG_ENDIAN
410 ei->preffered_endian = _ENDIAN_BIG;
412 ei->preffered_endian = _ENDIAN_LITTLE;
421 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei)
427 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei,
428 _csid_t * __restrict csid,
429 _index_t * __restrict idx,
433 _DIAGASSERT(csid != NULL && idx != NULL);
443 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei,
444 _wc_t * __restrict wc,
445 _csid_t csid, _index_t idx)
448 _DIAGASSERT(wc != NULL);
460 _citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo * __restrict ei,
461 _UTF1632State * __restrict psenc,
462 int * __restrict rstate)
465 if (psenc->chlen == 0)
466 *rstate = _STDENC_SDGEN_INITIAL;
468 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
473 /* ----------------------------------------------------------------------
474 * public interface for stdenc
477 _CITRUS_STDENC_DECLS(UTF1632);
478 _CITRUS_STDENC_DEF_OPS(UTF1632);
480 #include "citrus_stdenc_template.h"