1 /* $NetBSD: citrus_ues.c,v 1.3 2012/02/12 13:51:29 wiz 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.3 2012/02/12 13:51:29 wiz 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
;
72 _UESState s_wcsrtombs
;
77 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
78 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
80 #define _FUNCNAME(m) _citrus_UES_##m
81 #define _ENCODING_INFO _UESEncodingInfo
82 #define _CTYPE_INFO _UESCTypeInfo
83 #define _ENCODING_STATE _UESState
84 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
85 #define _ENCODING_IS_STATE_DEPENDENT 0
86 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
90 _citrus_UES_init_state(_UESEncodingInfo
* __restrict ei
,
91 _UESState
* __restrict psenc
)
98 _citrus_UES_pack_state(_UESEncodingInfo
* __restrict ei
,
99 void *__restrict pspriv
, const _UESState
* __restrict psenc
)
101 /* ei seem to be unused */
102 _DIAGASSERT(pspriv
!= NULL
);
103 _DIAGASSERT(psenc
!= NULL
);
105 memcpy(pspriv
, (const void *)psenc
, sizeof(*psenc
));
110 _citrus_UES_unpack_state(_UESEncodingInfo
* __restrict ei
,
111 _UESState
* __restrict psenc
, const void * __restrict pspriv
)
113 /* ei seem to be unused */
114 _DIAGASSERT(psenc
!= NULL
);
115 _DIAGASSERT(pspriv
!= NULL
);
117 memcpy((void *)psenc
, pspriv
, sizeof(*psenc
));
123 if (ch
>= '0' && ch
<= '9')
125 else if (ch
>= 'A' && ch
<= 'F')
126 return (ch
- 'A') + 10;
127 else if (ch
>= 'a' && ch
<= 'f')
128 return (ch
- 'a') + 10;
138 #define BMP_MAX UINT32_C(0xFFFF)
139 #define UCS2_MAX UINT32_C(0x10FFFF)
140 #define UCS4_MAX UINT32_C(0x7FFFFFFF)
142 static const char *xdig
= "0123456789abcdef";
145 to_str(char *s
, wchar_t wc
, int bit
)
162 *p
++ = xdig
[(wc
>> (bit
-= 4)) & 0xF];
168 is_hi_surrogate(wchar_t wc
)
170 return wc
>= 0xD800 && wc
<= 0xDBFF;
174 is_lo_surrogate(wchar_t wc
)
176 return wc
>= 0xDC00 && wc
<= 0xDFFF;
179 static __inline
wchar_t
180 surrogate_to_ucs(wchar_t hi
, wchar_t lo
)
182 _DIAGASSERT(is_hi_surrogate(hi
));
183 _DIAGASSERT(is_lo_surrogate(lo
));
187 return (hi
<< 10 | lo
) + 0x10000;
191 ucs_to_surrogate(wchar_t wc
, wchar_t * __restrict hi
, wchar_t * __restrict lo
)
193 _DIAGASSERT(hi
!= NULL
);
194 _DIAGASSERT(lo
!= NULL
);
195 _DIAGASSERT(wc
>= 0x10000);
198 *hi
= (wc
>> 10) + 0xD800;
199 *lo
= (wc
& 0x3FF) + 0xDC00;
205 return (uint32_t)wc
<= 0x9F &&
206 wc
!= 0x24 && wc
!= 0x40 && wc
!= 0x60;
210 _citrus_UES_mbrtowc_priv(_UESEncodingInfo
* __restrict ei
,
211 wchar_t * __restrict pwc
, const char ** __restrict s
, size_t n
,
212 _UESState
* __restrict psenc
, size_t * __restrict nresult
)
215 int ch
, head
, tail
, num
;
218 _DIAGASSERT(ei
!= NULL
);
219 /* pwc may be null */
220 _DIAGASSERT(s
!= NULL
);
221 _DIAGASSERT(psenc
!= NULL
);
222 _DIAGASSERT(nresult
!= NULL
);
225 _citrus_UES_init_state(ei
, psenc
);
237 if (psenc
->chlen
== head
) {
240 psenc
->ch
[psenc
->chlen
++] = *s0
++;
242 ch
= (unsigned char)psenc
->ch
[head
++];
244 if (psenc
->chlen
== head
) {
247 psenc
->ch
[psenc
->chlen
++] = *s0
++;
249 switch (psenc
->ch
[head
]) {
254 if (ei
->mode
& MODE_C99
) {
264 for (; head
< tail
; ++head
) {
265 if (psenc
->chlen
== head
) {
269 *nresult
= (size_t)-2;
272 psenc
->ch
[psenc
->chlen
++] = *s0
++;
274 num
= to_int((int)(unsigned char)psenc
->ch
[head
]);
279 wc
= (wc
<< 4) | num
;
286 if (hi
!= (wchar_t)0)
288 if ((ei
->mode
& MODE_C99
) == 0) {
289 if (is_hi_surrogate(wc
) != 0) {
293 if ((uint32_t)wc
<= 0x7F /* XXX */ ||
294 is_lo_surrogate(wc
) != 0)
300 if (is_basic(wc
) == 0 && (uint32_t)wc
<= UCS4_MAX
&&
301 is_hi_surrogate(wc
) == 0 && is_lo_surrogate(wc
) == 0)
303 *nresult
= (size_t)-1;
306 if (is_lo_surrogate(wc
) == 0)
308 wc
= surrogate_to_ucs(hi
, wc
);
311 ch
= (unsigned char)psenc
->ch
[0];
314 memmove(&psenc
->ch
[0], &psenc
->ch
[1], head
);
320 *nresult
= (size_t)((wc
== 0) ? 0 : (s0
- *s
));
327 _citrus_UES_wcrtomb_priv(_UESEncodingInfo
* __restrict ei
,
328 char * __restrict s
, size_t n
, wchar_t wc
,
329 _UESState
* __restrict psenc
, size_t * __restrict nresult
)
333 if (psenc
->chlen
!= 0)
336 if ((ei
->mode
& MODE_C99
) ? is_basic(wc
) : (uint32_t)wc
<= 0x7F) {
339 psenc
->ch
[psenc
->chlen
++] = (char)wc
;
340 } else if ((uint32_t)wc
<= BMP_MAX
) {
343 psenc
->chlen
= to_str(&psenc
->ch
[0], wc
, UCS2_BIT
);
344 } else if ((ei
->mode
& MODE_C99
) == 0 && (uint32_t)wc
<= UCS2_MAX
) {
347 ucs_to_surrogate(wc
, &hi
, &lo
);
348 psenc
->chlen
+= to_str(&psenc
->ch
[0], hi
, UCS2_BIT
);
349 psenc
->chlen
+= to_str(&psenc
->ch
[6], lo
, UCS2_BIT
);
350 } else if ((ei
->mode
& MODE_C99
) && (uint32_t)wc
<= UCS4_MAX
) {
353 psenc
->chlen
= to_str(&psenc
->ch
[0], wc
, UCS4_BIT
);
355 *nresult
= (size_t)-1;
358 memcpy(s
, psenc
->ch
, psenc
->chlen
);
359 *nresult
= psenc
->chlen
;
365 *nresult
= (size_t)-1;
371 _citrus_UES_stdenc_wctocs(_UESEncodingInfo
* __restrict ei
,
372 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
374 /* ei seem to be unused */
375 _DIAGASSERT(csid
!= NULL
);
376 _DIAGASSERT(idx
!= NULL
);
386 _citrus_UES_stdenc_cstowc(_UESEncodingInfo
* __restrict ei
,
387 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
389 /* ei seem to be unused */
390 _DIAGASSERT(wc
!= NULL
);
401 _citrus_UES_stdenc_get_state_desc_generic(_UESEncodingInfo
* __restrict ei
,
402 _UESState
* __restrict psenc
, int * __restrict rstate
)
404 _DIAGASSERT(psenc
!= NULL
);
405 _DIAGASSERT(rstate
!= NULL
);
407 if (psenc
->chlen
== 0)
408 *rstate
= _STDENC_SDGEN_INITIAL
;
410 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
; /* XXX */
417 _citrus_UES_encoding_module_uninit(_UESEncodingInfo
*ei
)
419 /* ei seems to be unused */
424 _citrus_UES_encoding_module_init(_UESEncodingInfo
* __restrict ei
,
425 const void * __restrict var
, size_t lenvar
)
429 _DIAGASSERT(ei
!= NULL
);
432 #define MATCH(x, act) \
434 if (lenvar >= (sizeof(#x)-1) && \
435 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
437 lenvar -= sizeof(#x)-1; \
440 } while (/*CONSTCOND*/0)
441 memset((void *)ei
, 0, sizeof(*ei
));
443 switch (_bcs_toupper(*p
)) {
445 MATCH(C99
, ei
->mode
|= MODE_C99
);
451 ei
->mb_cur_max
= (ei
->mode
& MODE_C99
) ? 10 : 12;
456 /* ----------------------------------------------------------------------
457 * public interface for ctype
460 _CITRUS_CTYPE_DECLS(UES
);
461 _CITRUS_CTYPE_DEF_OPS(UES
);
463 #include "citrus_ctype_template.h"
465 /* ----------------------------------------------------------------------
466 * public interface for stdenc
469 _CITRUS_STDENC_DECLS(UES
);
470 _CITRUS_STDENC_DEF_OPS(UES
);
472 #include "citrus_stdenc_template.h"