2 /* $NetBSD: citrus_utf1632.c,v 1.9 2008/06/14 16:01:08 tnozaki Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include <sys/endian.h>
34 #include <sys/types.h>
45 #include "citrus_namespace.h"
46 #include "citrus_types.h"
47 #include "citrus_module.h"
48 #include "citrus_stdenc.h"
49 #include "citrus_bcs.h"
51 #include "citrus_utf1632.h"
54 /* ----------------------------------------------------------------------
55 * private stuffs used by templates
64 #define _ENDIAN_UNKNOWN 0
66 #define _ENDIAN_LITTLE 2
67 #if BYTE_ORDER == BIG_ENDIAN
68 #define _ENDIAN_INTERNAL _ENDIAN_BIG
69 #define _ENDIAN_SWAPPED _ENDIAN_LITTLE
71 #define _ENDIAN_INTERNAL _ENDIAN_LITTLE
72 #define _ENDIAN_SWAPPED _ENDIAN_BIG
74 #define _MODE_UTF32 0x00000001U
75 #define _MODE_FORCE_ENDIAN 0x00000002U
81 } _UTF1632EncodingInfo;
83 #define _FUNCNAME(m) _citrus_UTF1632_##m
84 #define _ENCODING_INFO _UTF1632EncodingInfo
85 #define _ENCODING_STATE _UTF1632State
86 #define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max)
87 #define _ENCODING_IS_STATE_DEPENDENT 0
88 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
93 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei __unused,
97 memset(s, 0, sizeof(*s));
101 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc,
102 char **s, size_t n, _UTF1632State *psenc, size_t *nresult)
107 int chlenbak, endian, needlen;
112 _citrus_UTF1632_init_state(ei, psenc);
113 *nresult = 0; /* state independent */
118 chlenbak = psenc->chlen;
121 needlen = ((ei->mode & _MODE_UTF32) != 0 || chlenbak >= 2) ? 4 : 2;
123 while (chlenbak < needlen) {
126 psenc->ch[chlenbak++] = *s0++;
131 /* judge endian marker */
132 if ((ei->mode & _MODE_UTF32) == 0) {
134 if (psenc->ch[0] == 0xFE && psenc->ch[1] == 0xFF) {
135 psenc->current_endian = _ENDIAN_BIG;
138 } else if (psenc->ch[0] == 0xFF && psenc->ch[1] == 0xFE) {
139 psenc->current_endian = _ENDIAN_LITTLE;
145 if (psenc->ch[0] == 0x00 && psenc->ch[1] == 0x00 &&
146 psenc->ch[2] == 0xFE && psenc->ch[3] == 0xFF) {
147 psenc->current_endian = _ENDIAN_BIG;
150 } else if (psenc->ch[0] == 0xFF && psenc->ch[1] == 0xFE &&
151 psenc->ch[2] == 0x00 && psenc->ch[3] == 0x00) {
152 psenc->current_endian = _ENDIAN_LITTLE;
157 endian = ((ei->mode & _MODE_FORCE_ENDIAN) != 0 ||
158 psenc->current_endian == _ENDIAN_UNKNOWN) ? ei->preffered_endian :
159 psenc->current_endian;
162 if ((ei->mode & _MODE_UTF32) == 0) {
168 ((wchar_t)psenc->ch[1] << 8));
172 ((wchar_t)psenc->ch[0] << 8));
177 if (wc >= 0xD800 && wc <= 0xDBFF) {
184 wc -= 0xD800; /* wc : surrogate high (see above) */
188 if (psenc->ch[3] < 0xDC || psenc->ch[3] > 0xDF)
191 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
194 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
197 wc |= (wchar_t)(psenc->ch[2] & 3) << 8;
209 ((wchar_t)psenc->ch[1] << 8) |
210 ((wchar_t)psenc->ch[2] << 16) |
211 ((wchar_t)psenc->ch[3] << 24));
215 ((wchar_t)psenc->ch[2] << 8) |
216 ((wchar_t)psenc->ch[1] << 16) |
217 ((wchar_t)psenc->ch[0] << 24));
222 if (wc >= 0xD800 && wc <= 0xDFFF)
235 *nresult = (size_t)-1;
240 *nresult = (size_t)-2;
241 psenc->chlen = chlenbak;
247 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
248 wchar_t wc, _UTF1632State *psenc, size_t *nresult)
251 static const char _bom[4] = {
252 0x00, 0x00, 0xFE, 0xFF,
254 const char *bom = &_bom[0];
258 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
259 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
260 if (ei->mode & _MODE_UTF32)
271 psenc->current_endian = ei->preffered_endian;
275 if ((ei->mode & _MODE_UTF32)==0) {
285 wc2 = (wc & 0x3FF) | 0xDC00;
286 wc = (wc>>10) | 0xD800;
294 switch (psenc->current_endian) {
312 if (wc >= 0xD800 && wc <= 0xDFFF)
317 switch (psenc->current_endian) {
337 *nresult = (size_t)-1;
340 *nresult = (size_t)-1;
345 parse_variable(_UTF1632EncodingInfo * __restrict ei,
346 const void * __restrict var, size_t lenvar)
355 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
359 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
363 MATCH(internal, ei->preffered_endian = _ENDIAN_INTERNAL);
367 MATCH(swapped, ei->preffered_endian = _ENDIAN_SWAPPED);
371 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
375 MATCH(utf32, ei->mode |= _MODE_UTF32);
385 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
386 const void * __restrict var, size_t lenvar)
389 memset((void *)ei, 0, sizeof(*ei));
391 parse_variable(ei, var, lenvar);
393 ei->cur_max = ((ei->mode&_MODE_UTF32) == 0) ? 6 : 8;
394 /* 6: endian + surrogate */
395 /* 8: endian + normal */
397 if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
398 ei->preffered_endian = _ENDIAN_BIG;
406 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei __unused)
413 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei __unused,
414 _csid_t * __restrict csid, _index_t * __restrict idx, _wc_t wc)
425 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei __unused,
426 _wc_t * __restrict wc, _csid_t csid, _index_t idx)
439 _citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo * __restrict ei __unused,
440 _UTF1632State * __restrict psenc, int * __restrict rstate)
443 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
444 _STDENC_SDGEN_INCOMPLETE_CHAR;
448 /* ----------------------------------------------------------------------
449 * public interface for stdenc
452 _CITRUS_STDENC_DECLS(UTF1632);
453 _CITRUS_STDENC_DEF_OPS(UTF1632);
455 #include "citrus_stdenc_template.h"