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
82 } _UTF1632EncodingInfo;
84 #define _FUNCNAME(m) _citrus_UTF1632_##m
85 #define _ENCODING_INFO _UTF1632EncodingInfo
86 #define _ENCODING_STATE _UTF1632State
87 #define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max)
88 #define _ENCODING_MB_CUR_MIN(_ei_) ((_ei_)->cur_min)
89 #define _ENCODING_IS_STATE_DEPENDENT 0
90 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
95 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei __unused,
99 memset(s, 0, sizeof(*s));
103 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc,
104 char **s, size_t n, _UTF1632State *psenc, size_t *nresult)
109 int chlenbak, endian, needlen;
114 _citrus_UTF1632_init_state(ei, psenc);
115 *nresult = 0; /* state independent */
120 chlenbak = psenc->chlen;
123 needlen = ((ei->mode & _MODE_UTF32) != 0 || chlenbak >= 2) ? 4 : 2;
125 while (chlenbak < needlen) {
128 psenc->ch[chlenbak++] = *s0++;
133 /* judge endian marker */
134 if ((ei->mode & _MODE_UTF32) == 0) {
136 if (psenc->ch[0] == 0xFE && psenc->ch[1] == 0xFF) {
137 psenc->current_endian = _ENDIAN_BIG;
140 } else if (psenc->ch[0] == 0xFF && psenc->ch[1] == 0xFE) {
141 psenc->current_endian = _ENDIAN_LITTLE;
147 if (psenc->ch[0] == 0x00 && psenc->ch[1] == 0x00 &&
148 psenc->ch[2] == 0xFE && psenc->ch[3] == 0xFF) {
149 psenc->current_endian = _ENDIAN_BIG;
152 } else if (psenc->ch[0] == 0xFF && psenc->ch[1] == 0xFE &&
153 psenc->ch[2] == 0x00 && psenc->ch[3] == 0x00) {
154 psenc->current_endian = _ENDIAN_LITTLE;
159 endian = ((ei->mode & _MODE_FORCE_ENDIAN) != 0 ||
160 psenc->current_endian == _ENDIAN_UNKNOWN) ? ei->preffered_endian :
161 psenc->current_endian;
164 if ((ei->mode & _MODE_UTF32) == 0) {
170 ((wchar_t)psenc->ch[1] << 8));
174 ((wchar_t)psenc->ch[0] << 8));
179 if (wc >= 0xD800 && wc <= 0xDBFF) {
186 wc -= 0xD800; /* wc : surrogate high (see above) */
190 if (psenc->ch[3] < 0xDC || psenc->ch[3] > 0xDF)
193 wc |= (wchar_t)(psenc->ch[3] & 3) << 8;
196 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
199 wc |= (wchar_t)(psenc->ch[2] & 3) << 8;
211 ((wchar_t)psenc->ch[1] << 8) |
212 ((wchar_t)psenc->ch[2] << 16) |
213 ((wchar_t)psenc->ch[3] << 24));
217 ((wchar_t)psenc->ch[2] << 8) |
218 ((wchar_t)psenc->ch[1] << 16) |
219 ((wchar_t)psenc->ch[0] << 24));
224 if (wc >= 0xD800 && wc <= 0xDFFF)
237 *nresult = (size_t)-1;
242 *nresult = (size_t)-2;
243 psenc->chlen = chlenbak;
249 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
250 wchar_t wc, _UTF1632State *psenc, size_t *nresult)
253 static const char _bom[4] = {
254 0x00, 0x00, 0xFE, 0xFF,
256 const char *bom = &_bom[0];
260 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
261 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
262 if (ei->mode & _MODE_UTF32)
273 psenc->current_endian = ei->preffered_endian;
277 if ((ei->mode & _MODE_UTF32)==0) {
287 wc2 = (wc & 0x3FF) | 0xDC00;
288 wc = (wc>>10) | 0xD800;
296 switch (psenc->current_endian) {
314 if (wc >= 0xD800 && wc <= 0xDFFF)
319 switch (psenc->current_endian) {
339 *nresult = (size_t)-1;
342 *nresult = (size_t)-1;
347 parse_variable(_UTF1632EncodingInfo * __restrict ei,
348 const void * __restrict var, size_t lenvar)
357 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
361 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
365 MATCH(internal, ei->preffered_endian = _ENDIAN_INTERNAL);
369 MATCH(swapped, ei->preffered_endian = _ENDIAN_SWAPPED);
373 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
377 MATCH(utf32, ei->mode |= _MODE_UTF32);
387 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
388 const void * __restrict var, size_t lenvar)
391 memset((void *)ei, 0, sizeof(*ei));
393 parse_variable(ei, var, lenvar);
395 ei->cur_min = ((ei->mode&_MODE_UTF32) == 0) ? 2 : 4;
396 ei->cur_max = ((ei->mode&_MODE_UTF32) == 0) ? 6 : 8;
397 /* 6: endian + surrogate */
398 /* 8: endian + normal */
400 if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
401 ei->preffered_endian = _ENDIAN_BIG;
409 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei __unused)
416 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei __unused,
417 _csid_t * __restrict csid, _index_t * __restrict idx, _wc_t wc)
428 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei __unused,
429 _wc_t * __restrict wc, _csid_t csid, _index_t idx)
442 _citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo * __restrict ei __unused,
443 _UTF1632State * __restrict psenc, int * __restrict rstate)
446 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
447 _STDENC_SDGEN_INCOMPLETE_CHAR;
451 /* ----------------------------------------------------------------------
452 * public interface for stdenc
455 _CITRUS_STDENC_DECLS(UTF1632);
456 _CITRUS_STDENC_DEF_OPS(UTF1632);
458 #include "citrus_stdenc_template.h"