2 /* $NetBSD: citrus_ues.c,v 1.3 2012/02/12 13:51:29 wiz Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2006 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>
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_bcs.h"
46 #include "citrus_module.h"
47 #include "citrus_stdenc.h"
48 #include "citrus_ues.h"
61 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
62 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
64 #define _FUNCNAME(m) _citrus_UES_##m
65 #define _ENCODING_INFO _UESEncodingInfo
66 #define _ENCODING_STATE _UESState
67 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
68 #define _ENCODING_IS_STATE_DEPENDENT 0
69 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
73 _citrus_UES_init_state(_UESEncodingInfo * __restrict ei __unused,
74 _UESState * __restrict psenc)
83 _citrus_UES_pack_state(_UESEncodingInfo * __restrict ei __unused,
84 void *__restrict pspriv, const _UESState * __restrict psenc)
87 memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
92 _citrus_UES_unpack_state(_UESEncodingInfo * __restrict ei __unused,
93 _UESState * __restrict psenc, const void * __restrict pspriv)
96 memcpy((void *)psenc, pspriv, sizeof(*psenc));
104 if (ch >= '0' && ch <= '9')
106 else if (ch >= 'A' && ch <= 'F')
107 return ((ch - 'A') + 10);
108 else if (ch >= 'a' && ch <= 'f')
109 return ((ch - 'a') + 10);
119 #define BMP_MAX UINT32_C(0xFFFF)
120 #define UCS2_MAX UINT32_C(0x10FFFF)
121 #define UCS4_MAX UINT32_C(0x7FFFFFFF)
123 static const char *xdig = "0123456789abcdef";
126 to_str(char *s, wchar_t wc, int bit)
143 *p++ = xdig[(wc >> (bit -= 4)) & 0xF];
149 is_hi_surrogate(wchar_t wc)
152 return (wc >= 0xD800 && wc <= 0xDBFF);
156 is_lo_surrogate(wchar_t wc)
159 return (wc >= 0xDC00 && wc <= 0xDFFF);
162 static __inline wchar_t
163 surrogate_to_ucs(wchar_t hi, wchar_t lo)
168 return ((hi << 10 | lo) + 0x10000);
172 ucs_to_surrogate(wchar_t wc, wchar_t * __restrict hi, wchar_t * __restrict lo)
176 *hi = (wc >> 10) + 0xD800;
177 *lo = (wc & 0x3FF) + 0xDC00;
184 return ((uint32_t)wc <= 0x9F && wc != 0x24 && wc != 0x40 &&
189 _citrus_UES_mbrtowc_priv(_UESEncodingInfo * __restrict ei,
190 wchar_t * __restrict pwc, char ** __restrict s, size_t n,
191 _UESState * __restrict psenc, size_t * __restrict nresult)
194 int ch, head, num, tail;
198 _citrus_UES_init_state(ei, psenc);
210 if (psenc->chlen == head) {
213 psenc->ch[psenc->chlen++] = *s0++;
215 ch = (unsigned char)psenc->ch[head++];
217 if (psenc->chlen == head) {
220 psenc->ch[psenc->chlen++] = *s0++;
222 switch (psenc->ch[head]) {
227 if (ei->mode & MODE_C99) {
237 for (; head < tail; ++head) {
238 if (psenc->chlen == head) {
242 *nresult = (size_t)-2;
245 psenc->ch[psenc->chlen++] = *s0++;
247 num = to_int((int)(unsigned char)psenc->ch[head]);
252 wc = (wc << 4) | num;
259 if (hi != (wchar_t)0)
261 if ((ei->mode & MODE_C99) == 0) {
262 if (is_hi_surrogate(wc) != 0) {
266 if ((uint32_t)wc <= 0x7F /* XXX */ ||
267 is_lo_surrogate(wc) != 0)
273 if (is_basic(wc) == 0 && (uint32_t)wc <= UCS4_MAX &&
274 is_hi_surrogate(wc) == 0 && is_lo_surrogate(wc) == 0)
276 *nresult = (size_t)-1;
279 if (is_lo_surrogate(wc) == 0)
281 wc = surrogate_to_ucs(hi, wc);
284 ch = (unsigned char)psenc->ch[0];
287 memmove(&psenc->ch[0], &psenc->ch[1], head);
293 *nresult = (size_t)((wc == 0) ? 0 : (s0 - *s));
300 _citrus_UES_wcrtomb_priv(_UESEncodingInfo * __restrict ei,
301 char * __restrict s, size_t n, wchar_t wc,
302 _UESState * __restrict psenc, size_t * __restrict nresult)
306 if (psenc->chlen != 0)
309 if ((ei->mode & MODE_C99) ? is_basic(wc) : (uint32_t)wc <= 0x7F) {
312 psenc->ch[psenc->chlen++] = (char)wc;
313 } else if ((uint32_t)wc <= BMP_MAX) {
316 psenc->chlen = to_str(&psenc->ch[0], wc, UCS2_BIT);
317 } else if ((ei->mode & MODE_C99) == 0 && (uint32_t)wc <= UCS2_MAX) {
320 ucs_to_surrogate(wc, &hi, &lo);
321 psenc->chlen += to_str(&psenc->ch[0], hi, UCS2_BIT);
322 psenc->chlen += to_str(&psenc->ch[6], lo, UCS2_BIT);
323 } else if ((ei->mode & MODE_C99) && (uint32_t)wc <= UCS4_MAX) {
326 psenc->chlen = to_str(&psenc->ch[0], wc, UCS4_BIT);
328 *nresult = (size_t)-1;
331 memcpy(s, psenc->ch, psenc->chlen);
332 *nresult = psenc->chlen;
338 *nresult = (size_t)-1;
344 _citrus_UES_stdenc_wctocs(_UESEncodingInfo * __restrict ei __unused,
345 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
356 _citrus_UES_stdenc_cstowc(_UESEncodingInfo * __restrict ei __unused,
357 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
369 _citrus_UES_stdenc_get_state_desc_generic(_UESEncodingInfo * __restrict ei __unused,
370 _UESState * __restrict psenc, int * __restrict rstate)
373 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
374 _STDENC_SDGEN_INCOMPLETE_CHAR;
380 _citrus_UES_encoding_module_uninit(_UESEncodingInfo *ei __unused)
383 /* ei seems to be unused */
388 _citrus_UES_encoding_module_init(_UESEncodingInfo * __restrict ei,
389 const void * __restrict var, size_t lenvar)
394 memset((void *)ei, 0, sizeof(*ei));
396 switch (_bcs_toupper(*p)) {
398 MATCH(C99, ei->mode |= MODE_C99);
404 ei->mb_cur_max = (ei->mode & MODE_C99) ? 10 : 12;
409 /* ----------------------------------------------------------------------
410 * public interface for stdenc
413 _CITRUS_STDENC_DECLS(UES);
414 _CITRUS_STDENC_DEF_OPS(UES);
416 #include "citrus_stdenc_template.h"