2 /* $NetBSD: citrus_utf1632.c,v 1.9 2008/06/14 16:01:08 tnozaki 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/cdefs.h>
31 #include <sys/endian.h>
32 #include <sys/types.h>
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_module.h"
46 #include "citrus_stdenc.h"
47 #include "citrus_bcs.h"
49 #include "citrus_utf1632.h"
52 /* ----------------------------------------------------------------------
53 * private stuffs used by templates
62 #define _ENDIAN_UNKNOWN 0
64 #define _ENDIAN_LITTLE 2
65 #if BYTE_ORDER == BIG_ENDIAN
66 #define _ENDIAN_INTERNAL _ENDIAN_BIG
67 #define _ENDIAN_SWAPPED _ENDIAN_LITTLE
69 #define _ENDIAN_INTERNAL _ENDIAN_LITTLE
70 #define _ENDIAN_SWAPPED _ENDIAN_BIG
72 #define _MODE_UTF32 0x00000001U
73 #define _MODE_FORCE_ENDIAN 0x00000002U
79 } _UTF1632EncodingInfo;
81 #define _FUNCNAME(m) _citrus_UTF1632_##m
82 #define _ENCODING_INFO _UTF1632EncodingInfo
83 #define _ENCODING_STATE _UTF1632State
84 #define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max)
85 #define _ENCODING_IS_STATE_DEPENDENT 0
86 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
91 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei __unused,
95 memset(s, 0, sizeof(*s));
99 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc,
100 const char **s, size_t n, _UTF1632State *psenc, size_t *nresult)
105 int chlenbak, endian, needlen;
110 _citrus_UTF1632_init_state(ei, psenc);
111 *nresult = 0; /* state independent */
116 chlenbak = psenc->chlen;
119 needlen = ((ei->mode & _MODE_UTF32) != 0 || chlenbak >= 2) ? 4 : 2;
121 while (chlenbak < needlen) {
124 psenc->ch[chlenbak++] = *s0++;
129 /* judge endian marker */
130 if ((ei->mode & _MODE_UTF32) == 0) {
132 if (psenc->ch[0] == 0xFE && psenc->ch[1] == 0xFF) {
133 psenc->current_endian = _ENDIAN_BIG;
136 } else if (psenc->ch[0] == 0xFF && psenc->ch[1] == 0xFE) {
137 psenc->current_endian = _ENDIAN_LITTLE;
143 if (psenc->ch[0] == 0x00 && psenc->ch[1] == 0x00 &&
144 psenc->ch[2] == 0xFE && psenc->ch[3] == 0xFF) {
145 psenc->current_endian = _ENDIAN_BIG;
148 } else if (psenc->ch[0] == 0xFF && psenc->ch[1] == 0xFE &&
149 psenc->ch[2] == 0x00 && psenc->ch[3] == 0x00) {
150 psenc->current_endian = _ENDIAN_LITTLE;
155 endian = ((ei->mode & _MODE_FORCE_ENDIAN) != 0 ||
156 psenc->current_endian == _ENDIAN_UNKNOWN) ? ei->preffered_endian :
157 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[3] < 0xDC || psenc->ch[3] > 0xDF)
189 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
192 if (psenc->ch[2]<0xDC || psenc->ch[2]>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, size_t *nresult)
249 static const char _bom[4] = {
250 0x00, 0x00, 0xFE, 0xFF,
252 const char *bom = &_bom[0];
256 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
257 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
258 if (ei->mode & _MODE_UTF32)
269 psenc->current_endian = ei->preffered_endian;
273 if ((ei->mode & _MODE_UTF32)==0) {
283 wc2 = (wc & 0x3FF) | 0xDC00;
284 wc = (wc>>10) | 0xD800;
292 switch (psenc->current_endian) {
310 if (wc >= 0xD800 && wc <= 0xDFFF)
315 switch (psenc->current_endian) {
335 *nresult = (size_t)-1;
338 *nresult = (size_t)-1;
343 parse_variable(_UTF1632EncodingInfo * __restrict ei,
344 const void * __restrict var, size_t lenvar)
353 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
357 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
361 MATCH(internal, ei->preffered_endian = _ENDIAN_INTERNAL);
365 MATCH(swapped, ei->preffered_endian = _ENDIAN_SWAPPED);
369 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
373 MATCH(utf32, ei->mode |= _MODE_UTF32);
383 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
384 const void * __restrict var, size_t lenvar)
387 memset((void *)ei, 0, sizeof(*ei));
389 parse_variable(ei, var, lenvar);
391 ei->cur_max = ((ei->mode&_MODE_UTF32) == 0) ? 6 : 8;
392 /* 6: endian + surrogate */
393 /* 8: endian + normal */
395 if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
396 ei->preffered_endian = _ENDIAN_BIG;
404 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei __unused)
411 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei __unused,
412 _csid_t * __restrict csid, _index_t * __restrict idx, _wc_t wc)
423 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei __unused,
424 _wc_t * __restrict wc, _csid_t csid, _index_t idx)
437 _citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo * __restrict ei __unused,
438 _UTF1632State * __restrict psenc, int * __restrict rstate)
441 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
442 _STDENC_SDGEN_INCOMPLETE_CHAR;
446 /* ----------------------------------------------------------------------
447 * public interface for stdenc
450 _CITRUS_STDENC_DECLS(UTF1632);
451 _CITRUS_STDENC_DEF_OPS(UTF1632);
453 #include "citrus_stdenc_template.h"