1 /* $NetBSD: citrus_ues.c,v 1.4 2013/05/28 16:57:56 joerg Exp $ */
4 * Copyright (c)2006 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_ues.c,v 1.4 2013/05/28 16:57:56 joerg Exp $");
32 #endif /* LIBC_SCCS and not lint */
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_bcs.h"
46 #include "citrus_module.h"
47 #include "citrus_ctype.h"
48 #include "citrus_stdenc.h"
49 #include "citrus_ues.h"
65 /* for future multi-locale facility */
70 _UESState s_mbsrtowcs
;
71 _UESState s_mbsnrtowcs
;
73 _UESState s_wcsrtombs
;
74 _UESState s_wcsnrtombs
;
79 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
80 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
82 #define _FUNCNAME(m) _citrus_UES_##m
83 #define _ENCODING_INFO _UESEncodingInfo
84 #define _CTYPE_INFO _UESCTypeInfo
85 #define _ENCODING_STATE _UESState
86 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
87 #define _ENCODING_IS_STATE_DEPENDENT 0
88 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
92 _citrus_UES_init_state(_UESEncodingInfo
* __restrict ei
,
93 _UESState
* __restrict psenc
)
100 _citrus_UES_pack_state(_UESEncodingInfo
* __restrict ei
,
101 void *__restrict pspriv
, const _UESState
* __restrict psenc
)
103 /* ei seem to be unused */
104 _DIAGASSERT(pspriv
!= NULL
);
105 _DIAGASSERT(psenc
!= NULL
);
107 memcpy(pspriv
, (const void *)psenc
, sizeof(*psenc
));
112 _citrus_UES_unpack_state(_UESEncodingInfo
* __restrict ei
,
113 _UESState
* __restrict psenc
, const void * __restrict pspriv
)
115 /* ei seem to be unused */
116 _DIAGASSERT(psenc
!= NULL
);
117 _DIAGASSERT(pspriv
!= NULL
);
119 memcpy((void *)psenc
, pspriv
, sizeof(*psenc
));
125 if (ch
>= '0' && ch
<= '9')
127 else if (ch
>= 'A' && ch
<= 'F')
128 return (ch
- 'A') + 10;
129 else if (ch
>= 'a' && ch
<= 'f')
130 return (ch
- 'a') + 10;
140 #define BMP_MAX UINT32_C(0xFFFF)
141 #define UCS2_MAX UINT32_C(0x10FFFF)
142 #define UCS4_MAX UINT32_C(0x7FFFFFFF)
144 static const char *xdig
= "0123456789abcdef";
147 to_str(char *s
, wchar_t wc
, int bit
)
164 *p
++ = xdig
[(wc
>> (bit
-= 4)) & 0xF];
170 is_hi_surrogate(wchar_t wc
)
172 return wc
>= 0xD800 && wc
<= 0xDBFF;
176 is_lo_surrogate(wchar_t wc
)
178 return wc
>= 0xDC00 && wc
<= 0xDFFF;
181 static __inline
wchar_t
182 surrogate_to_ucs(wchar_t hi
, wchar_t lo
)
184 _DIAGASSERT(is_hi_surrogate(hi
));
185 _DIAGASSERT(is_lo_surrogate(lo
));
189 return (hi
<< 10 | lo
) + 0x10000;
193 ucs_to_surrogate(wchar_t wc
, wchar_t * __restrict hi
, wchar_t * __restrict lo
)
195 _DIAGASSERT(hi
!= NULL
);
196 _DIAGASSERT(lo
!= NULL
);
197 _DIAGASSERT(wc
>= 0x10000);
200 *hi
= (wc
>> 10) + 0xD800;
201 *lo
= (wc
& 0x3FF) + 0xDC00;
207 return (uint32_t)wc
<= 0x9F &&
208 wc
!= 0x24 && wc
!= 0x40 && wc
!= 0x60;
212 _citrus_UES_mbrtowc_priv(_UESEncodingInfo
* __restrict ei
,
213 wchar_t * __restrict pwc
, const char ** __restrict s
, size_t n
,
214 _UESState
* __restrict psenc
, size_t * __restrict nresult
)
217 int ch
, head
, tail
, num
;
220 _DIAGASSERT(ei
!= NULL
);
221 /* pwc may be null */
222 _DIAGASSERT(s
!= NULL
);
223 _DIAGASSERT(psenc
!= NULL
);
224 _DIAGASSERT(nresult
!= NULL
);
227 _citrus_UES_init_state(ei
, psenc
);
239 if (psenc
->chlen
== head
) {
242 psenc
->ch
[psenc
->chlen
++] = *s0
++;
244 ch
= (unsigned char)psenc
->ch
[head
++];
246 if (psenc
->chlen
== head
) {
249 psenc
->ch
[psenc
->chlen
++] = *s0
++;
251 switch (psenc
->ch
[head
]) {
256 if (ei
->mode
& MODE_C99
) {
266 for (; head
< tail
; ++head
) {
267 if (psenc
->chlen
== head
) {
271 *nresult
= (size_t)-2;
274 psenc
->ch
[psenc
->chlen
++] = *s0
++;
276 num
= to_int((int)(unsigned char)psenc
->ch
[head
]);
281 wc
= (wc
<< 4) | num
;
288 if (hi
!= (wchar_t)0)
290 if ((ei
->mode
& MODE_C99
) == 0) {
291 if (is_hi_surrogate(wc
) != 0) {
295 if ((uint32_t)wc
<= 0x7F /* XXX */ ||
296 is_lo_surrogate(wc
) != 0)
302 if (is_basic(wc
) == 0 && (uint32_t)wc
<= UCS4_MAX
&&
303 is_hi_surrogate(wc
) == 0 && is_lo_surrogate(wc
) == 0)
305 *nresult
= (size_t)-1;
308 if (is_lo_surrogate(wc
) == 0)
310 wc
= surrogate_to_ucs(hi
, wc
);
313 ch
= (unsigned char)psenc
->ch
[0];
316 memmove(&psenc
->ch
[0], &psenc
->ch
[1], head
);
322 *nresult
= (size_t)((wc
== 0) ? 0 : (s0
- *s
));
329 _citrus_UES_wcrtomb_priv(_UESEncodingInfo
* __restrict ei
,
330 char * __restrict s
, size_t n
, wchar_t wc
,
331 _UESState
* __restrict psenc
, size_t * __restrict nresult
)
335 if (psenc
->chlen
!= 0)
338 if ((ei
->mode
& MODE_C99
) ? is_basic(wc
) : (uint32_t)wc
<= 0x7F) {
341 psenc
->ch
[psenc
->chlen
++] = (char)wc
;
342 } else if ((uint32_t)wc
<= BMP_MAX
) {
345 psenc
->chlen
= to_str(&psenc
->ch
[0], wc
, UCS2_BIT
);
346 } else if ((ei
->mode
& MODE_C99
) == 0 && (uint32_t)wc
<= UCS2_MAX
) {
349 ucs_to_surrogate(wc
, &hi
, &lo
);
350 psenc
->chlen
+= to_str(&psenc
->ch
[0], hi
, UCS2_BIT
);
351 psenc
->chlen
+= to_str(&psenc
->ch
[6], lo
, UCS2_BIT
);
352 } else if ((ei
->mode
& MODE_C99
) && (uint32_t)wc
<= UCS4_MAX
) {
355 psenc
->chlen
= to_str(&psenc
->ch
[0], wc
, UCS4_BIT
);
357 *nresult
= (size_t)-1;
360 memcpy(s
, psenc
->ch
, psenc
->chlen
);
361 *nresult
= psenc
->chlen
;
367 *nresult
= (size_t)-1;
373 _citrus_UES_stdenc_wctocs(_UESEncodingInfo
* __restrict ei
,
374 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
376 /* ei seem to be unused */
377 _DIAGASSERT(csid
!= NULL
);
378 _DIAGASSERT(idx
!= NULL
);
388 _citrus_UES_stdenc_cstowc(_UESEncodingInfo
* __restrict ei
,
389 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
391 /* ei seem to be unused */
392 _DIAGASSERT(wc
!= NULL
);
403 _citrus_UES_stdenc_get_state_desc_generic(_UESEncodingInfo
* __restrict ei
,
404 _UESState
* __restrict psenc
, int * __restrict rstate
)
406 _DIAGASSERT(psenc
!= NULL
);
407 _DIAGASSERT(rstate
!= NULL
);
409 if (psenc
->chlen
== 0)
410 *rstate
= _STDENC_SDGEN_INITIAL
;
412 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
; /* XXX */
419 _citrus_UES_encoding_module_uninit(_UESEncodingInfo
*ei
)
421 /* ei seems to be unused */
426 _citrus_UES_encoding_module_init(_UESEncodingInfo
* __restrict ei
,
427 const void * __restrict var
, size_t lenvar
)
431 _DIAGASSERT(ei
!= NULL
);
434 #define MATCH(x, act) \
436 if (lenvar >= (sizeof(#x)-1) && \
437 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
439 lenvar -= sizeof(#x)-1; \
442 } while (/*CONSTCOND*/0)
443 memset((void *)ei
, 0, sizeof(*ei
));
445 switch (_bcs_toupper(*p
)) {
447 MATCH(C99
, ei
->mode
|= MODE_C99
);
453 ei
->mb_cur_max
= (ei
->mode
& MODE_C99
) ? 10 : 12;
458 /* ----------------------------------------------------------------------
459 * public interface for ctype
462 _CITRUS_CTYPE_DECLS(UES
);
463 _CITRUS_CTYPE_DEF_OPS(UES
);
465 #include "citrus_ctype_template.h"
467 /* ----------------------------------------------------------------------
468 * public interface for stdenc
471 _CITRUS_STDENC_DECLS(UES
);
472 _CITRUS_STDENC_DEF_OPS(UES
);
474 #include "citrus_stdenc_template.h"