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)
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
, i
, 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;
370 _citrus_UES_stdenc_wctocs(_UESEncodingInfo
* __restrict ei
,
371 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
373 /* ei seem to be unused */
374 _DIAGASSERT(csid
!= NULL
);
375 _DIAGASSERT(idx
!= NULL
);
385 _citrus_UES_stdenc_cstowc(_UESEncodingInfo
* __restrict ei
,
386 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
388 /* ei seem to be unused */
389 _DIAGASSERT(wc
!= NULL
);
400 _citrus_UES_stdenc_get_state_desc_generic(_UESEncodingInfo
* __restrict ei
,
401 _UESState
* __restrict psenc
, int * __restrict rstate
)
403 _DIAGASSERT(psenc
!= NULL
);
404 _DIAGASSERT(rstate
!= NULL
);
406 if (psenc
->chlen
== 0)
407 *rstate
= _STDENC_SDGEN_INITIAL
;
409 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
; /* XXX */
416 _citrus_UES_encoding_module_uninit(_UESEncodingInfo
*ei
)
418 /* ei seems to be unused */
423 _citrus_UES_encoding_module_init(_UESEncodingInfo
* __restrict ei
,
424 const void * __restrict var
, size_t lenvar
)
428 _DIAGASSERT(ei
!= NULL
);
431 #define MATCH(x, act) \
433 if (lenvar >= (sizeof(#x)-1) && \
434 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
436 lenvar -= sizeof(#x)-1; \
439 } while (/*CONSTCOND*/0)
440 memset((void *)ei
, 0, sizeof(*ei
));
442 switch (_bcs_toupper(*p
)) {
444 MATCH(C99
, ei
->mode
|= MODE_C99
);
450 ei
->mb_cur_max
= (ei
->mode
& MODE_C99
) ? 10 : 12;
455 /* ----------------------------------------------------------------------
456 * public interface for ctype
459 _CITRUS_CTYPE_DECLS(UES
);
460 _CITRUS_CTYPE_DEF_OPS(UES
);
462 #include "citrus_ctype_template.h"
464 /* ----------------------------------------------------------------------
465 * public interface for stdenc
468 _CITRUS_STDENC_DECLS(UES
);
469 _CITRUS_STDENC_DEF_OPS(UES
);
471 #include "citrus_stdenc_template.h"