2 /* $NetBSD: citrus_ues.c,v 1.1 2006/11/13 15:16:31 tnozaki Exp $ */
5 * Copyright (c)2006 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>
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_bcs.h"
44 #include "citrus_module.h"
45 #include "citrus_stdenc.h"
46 #include "citrus_ues.h"
59 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
60 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
62 #define _FUNCNAME(m) _citrus_UES_##m
63 #define _ENCODING_INFO _UESEncodingInfo
64 #define _ENCODING_STATE _UESState
65 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
66 #define _ENCODING_IS_STATE_DEPENDENT 0
67 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
71 _citrus_UES_init_state(_UESEncodingInfo * __restrict ei __unused,
72 _UESState * __restrict psenc)
80 _citrus_UES_pack_state(_UESEncodingInfo * __restrict ei __unused,
81 void *__restrict pspriv, const _UESState * __restrict psenc)
84 memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
89 _citrus_UES_unpack_state(_UESEncodingInfo * __restrict ei __unused,
90 _UESState * __restrict psenc, const void * __restrict pspriv)
93 memcpy((void *)psenc, pspriv, sizeof(*psenc));
100 if (ch >= '0' && ch <= '9')
102 else if (ch >= 'A' && ch <= 'F')
103 return ((ch - 'A') + 10);
104 else if (ch >= 'a' && ch <= 'f')
105 return ((ch - 'a') + 10);
115 #define BMP_MAX UINT32_C(0xFFFF)
116 #define UCS2_MAX UINT32_C(0x10FFFF)
117 #define UCS4_MAX UINT32_C(0x7FFFFFFF)
119 static const char *xdig = "0123456789abcdef";
122 to_str(char *s, wchar_t wc, int bit)
139 *p++ = xdig[(wc >> (bit -= 4)) & 0xF];
145 is_hi_surrogate(wchar_t wc)
148 return (wc >= 0xD800 && wc <= 0xDBFF);
152 is_lo_surrogate(wchar_t wc)
155 return (wc >= 0xDC00 && wc <= 0xDFFF);
158 static __inline wchar_t
159 surrogate_to_ucs(wchar_t hi, wchar_t lo)
164 return ((hi << 10 | lo) + 0x10000);
168 ucs_to_surrogate(wchar_t wc, wchar_t * __restrict hi, wchar_t * __restrict lo)
172 *hi = (wc >> 10) + 0xD800;
173 *lo = (wc & 0x3FF) + 0xDC00;
180 return ((uint32_t)wc <= 0x9F && wc != 0x24 && wc != 0x40 &&
185 _citrus_UES_mbrtowc_priv(_UESEncodingInfo * __restrict ei,
186 wchar_t * __restrict pwc, const char ** __restrict s, size_t n,
187 _UESState * __restrict psenc, size_t * __restrict nresult)
190 int ch, head, num, tail;
194 _citrus_UES_init_state(ei, psenc);
206 if (psenc->chlen == head) {
209 psenc->ch[psenc->chlen++] = *s0++;
211 ch = (unsigned char)psenc->ch[head++];
213 if (psenc->chlen == head) {
216 psenc->ch[psenc->chlen++] = *s0++;
218 switch (psenc->ch[head]) {
223 if (ei->mode & MODE_C99) {
233 for (; head < tail; ++head) {
234 if (psenc->chlen == head) {
238 *nresult = (size_t)-2;
241 psenc->ch[psenc->chlen++] = *s0++;
243 num = to_int((int)(unsigned char)psenc->ch[head]);
248 wc = (wc << 4) | num;
255 if (hi != (wchar_t)0)
257 if ((ei->mode & MODE_C99) == 0) {
258 if (is_hi_surrogate(wc) != 0) {
262 if ((uint32_t)wc <= 0x7F /* XXX */ ||
263 is_lo_surrogate(wc) != 0)
269 if (is_basic(wc) == 0 && (uint32_t)wc <= UCS4_MAX &&
270 is_hi_surrogate(wc) == 0 && is_lo_surrogate(wc) == 0)
272 *nresult = (size_t)-1;
275 if (is_lo_surrogate(wc) == 0)
277 wc = surrogate_to_ucs(hi, wc);
280 ch = (unsigned char)psenc->ch[0];
283 memmove(&psenc->ch[0], &psenc->ch[1], head);
289 *nresult = (size_t)((wc == 0) ? 0 : (s0 - *s));
296 _citrus_UES_wcrtomb_priv(_UESEncodingInfo * __restrict ei,
297 char * __restrict s, size_t n, wchar_t wc,
298 _UESState * __restrict psenc, size_t * __restrict nresult)
302 if (psenc->chlen != 0)
305 if ((ei->mode & MODE_C99) ? is_basic(wc) : (uint32_t)wc <= 0x7F) {
308 psenc->ch[psenc->chlen++] = (char)wc;
309 } else if ((uint32_t)wc <= BMP_MAX) {
312 psenc->chlen = to_str(&psenc->ch[0], wc, UCS2_BIT);
313 } else if ((ei->mode & MODE_C99) == 0 && (uint32_t)wc <= UCS2_MAX) {
316 ucs_to_surrogate(wc, &hi, &lo);
317 psenc->chlen += to_str(&psenc->ch[0], hi, UCS2_BIT);
318 psenc->chlen += to_str(&psenc->ch[6], lo, UCS2_BIT);
319 } else if ((ei->mode & MODE_C99) && (uint32_t)wc <= UCS4_MAX) {
322 psenc->chlen = to_str(&psenc->ch[0], wc, UCS4_BIT);
324 *nresult = (size_t)-1;
327 memcpy(s, psenc->ch, psenc->chlen);
328 *nresult = psenc->chlen;
334 *nresult = (size_t)-1;
340 _citrus_UES_stdenc_wctocs(_UESEncodingInfo * __restrict ei __unused,
341 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
352 _citrus_UES_stdenc_cstowc(_UESEncodingInfo * __restrict ei __unused,
353 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
365 _citrus_UES_stdenc_get_state_desc_generic(_UESEncodingInfo * __restrict ei __unused,
366 _UESState * __restrict psenc, int * __restrict rstate)
369 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
370 _STDENC_SDGEN_INCOMPLETE_CHAR;
376 _citrus_UES_encoding_module_uninit(_UESEncodingInfo *ei __unused)
379 /* ei seems to be unused */
384 _citrus_UES_encoding_module_init(_UESEncodingInfo * __restrict ei,
385 const void * __restrict var, size_t lenvar)
390 memset((void *)ei, 0, sizeof(*ei));
392 switch (_bcs_toupper(*p)) {
394 MATCH(C99, ei->mode |= MODE_C99);
400 ei->mb_cur_max = (ei->mode & MODE_C99) ? 10 : 12;
405 /* ----------------------------------------------------------------------
406 * public interface for stdenc
409 _CITRUS_STDENC_DECLS(UES);
410 _CITRUS_STDENC_DEF_OPS(UES);
412 #include "citrus_stdenc_template.h"