1 /* $NetBSD: citrus_utf8.c,v 1.17 2008/06/14 16:01:08 tnozaki Exp $ */
4 * Copyright (c)2002 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
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Paul Borman at Krystal Technologies.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/cdefs.h>
62 #if defined(LIBC_SCCS) && !defined(lint)
63 __RCSID("$NetBSD: citrus_utf8.c,v 1.17 2008/06/14 16:01:08 tnozaki Exp $");
64 #endif /* LIBC_SCCS and not lint */
73 #include <sys/types.h>
76 #include "citrus_namespace.h"
77 #include "citrus_types.h"
78 #include "citrus_module.h"
79 #include "citrus_ctype.h"
80 #include "citrus_stdenc.h"
81 #include "citrus_utf8.h"
84 /* ----------------------------------------------------------------------
85 * private stuffs used by templates
88 static int _UTF8_count_array
[256];
89 static int const *_UTF8_count
= NULL
;
91 static const u_int32_t _UTF8_range
[] = {
93 0x00000000, 0x00000080, 0x00000800, 0x00010000,
94 0x00200000, 0x04000000, 0x80000000,
106 _UTF8EncodingInfo ei
;
108 /* for future multi-locale facility */
111 _UTF8State s_mbrtowc
;
113 _UTF8State s_mbsrtowcs
;
114 _UTF8State s_wcrtomb
;
115 _UTF8State s_wcsrtombs
;
120 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
121 #define _CEI_TO_STATE(_ei_, _func_) (_ei_)->states.s_##_func_
123 #define _FUNCNAME(m) _citrus_UTF8_##m
124 #define _ENCODING_INFO _UTF8EncodingInfo
125 #define _CTYPE_INFO _UTF8CTypeInfo
126 #define _ENCODING_STATE _UTF8State
127 #define _ENCODING_MB_CUR_MAX(_ei_) 6
128 #define _ENCODING_IS_STATE_DEPENDENT 0
129 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
133 _UTF8_init_count(void)
137 memset(_UTF8_count_array
, 0, sizeof(_UTF8_count_array
));
138 for (i
= 0; i
<= 0x7f; i
++)
139 _UTF8_count_array
[i
] = 1;
140 for (i
= 0xc0; i
<= 0xdf; i
++)
141 _UTF8_count_array
[i
] = 2;
142 for (i
= 0xe0; i
<= 0xef; i
++)
143 _UTF8_count_array
[i
] = 3;
144 for (i
= 0xf0; i
<= 0xf7; i
++)
145 _UTF8_count_array
[i
] = 4;
146 for (i
= 0xf8; i
<= 0xfb; i
++)
147 _UTF8_count_array
[i
] = 5;
148 for (i
= 0xfc; i
<= 0xfd; i
++)
149 _UTF8_count_array
[i
] = 6;
150 _UTF8_count
= _UTF8_count_array
;
155 _UTF8_findlen(wchar_t v
)
160 c
= (u_int32_t
)v
; /*XXX*/
161 for (i
= 1; i
< sizeof(_UTF8_range
) / sizeof(_UTF8_range
[0]) - 1; i
++)
162 if (c
>= _UTF8_range
[i
] && c
< _UTF8_range
[i
+ 1])
165 return -1; /*out of range*/
169 _UTF8_surrogate(wchar_t wc
)
171 return wc
>= 0xd800 && wc
<= 0xdfff;
176 _citrus_UTF8_init_state(_UTF8EncodingInfo
*ei
, _UTF8State
*s
)
183 _citrus_UTF8_pack_state(_UTF8EncodingInfo
*ei
, void *pspriv
,
186 memcpy(pspriv
, (const void *)s
, sizeof(*s
));
191 _citrus_UTF8_unpack_state(_UTF8EncodingInfo
*ei
, _UTF8State
*s
,
194 memcpy((void *)s
, pspriv
, sizeof(*s
));
198 _citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo
*ei
, wchar_t *pwc
, const char **s
,
199 size_t n
, _UTF8State
*psenc
, size_t *nresult
)
206 _DIAGASSERT(nresult
!= 0);
207 _DIAGASSERT(s
!= NULL
);
208 _DIAGASSERT(psenc
!= NULL
);
213 _citrus_UTF8_init_state(ei
, psenc
);
214 *nresult
= 0; /* state independent */
218 /* make sure we have the first byte in the buffer */
219 if (psenc
->chlen
== 0) {
222 psenc
->ch
[psenc
->chlen
++] = *s0
++;
225 c
= _UTF8_count
[psenc
->ch
[0] & 0xff];
226 if (c
< 1 || c
< psenc
->chlen
)
230 wchar
= psenc
->ch
[0] & 0xff;
232 while (psenc
->chlen
< c
) {
235 psenc
->ch
[psenc
->chlen
++] = *s0
++;
237 wchar
= psenc
->ch
[0] & (0x7f >> c
);
238 for (i
= 1; i
< c
; i
++) {
239 if ((psenc
->ch
[i
] & 0xc0) != 0x80)
242 wchar
|= (psenc
->ch
[i
] & 0x3f);
244 if (_UTF8_surrogate(wchar
) || _UTF8_findlen(wchar
) != c
)
249 *nresult
= (wchar
== 0) ? 0 : s0
- *s
;
256 *nresult
= (size_t)-1;
261 *nresult
= (size_t)-2;
266 _citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo
*ei
, char *s
, size_t n
, wchar_t wc
,
267 _UTF8State
*psenc
, size_t *nresult
)
272 _DIAGASSERT(nresult
!= 0);
273 _DIAGASSERT(s
!= NULL
);
275 if (_UTF8_surrogate(wc
)) {
279 cnt
= _UTF8_findlen(wc
);
280 if (cnt
<= 0 || cnt
> 6) {
281 /* invalid UCS4 value */
286 /* bound check failure */
293 for (i
= cnt
- 1; i
> 0; i
--) {
294 s
[i
] = 0x80 | (c
& 0x3f);
301 s
[0] &= (0x7f >> cnt
);
302 s
[0] |= ((0xff00 >> cnt
) & 0xff);
306 *nresult
= (size_t)cnt
;
310 *nresult
= (size_t)-1;
316 _citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo
* __restrict ei
,
317 _csid_t
* __restrict csid
,
318 _index_t
* __restrict idx
,
322 _DIAGASSERT(csid
!= NULL
&& idx
!= NULL
);
325 *idx
= (_citrus_index_t
)wc
;
332 _citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo
* __restrict ei
,
333 wchar_t * __restrict wc
,
334 _csid_t csid
, _index_t idx
)
337 _DIAGASSERT(wc
!= NULL
);
349 _citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo
* __restrict ei
,
350 _UTF8State
* __restrict psenc
,
351 int * __restrict rstate
)
354 if (psenc
->chlen
== 0)
355 *rstate
= _STDENC_SDGEN_INITIAL
;
357 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
;
364 _citrus_UTF8_encoding_module_init(_UTF8EncodingInfo
* __restrict ei
,
365 const void * __restrict var
, size_t lenvar
)
374 _citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo
*ei
)
379 /* ----------------------------------------------------------------------
380 * public interface for ctype
383 _CITRUS_CTYPE_DECLS(UTF8
);
384 _CITRUS_CTYPE_DEF_OPS(UTF8
);
386 #include "citrus_ctype_template.h"
388 /* ----------------------------------------------------------------------
389 * public interface for stdenc
392 _CITRUS_STDENC_DECLS(UTF8
);
393 _CITRUS_STDENC_DEF_OPS(UTF8
);
395 #include "citrus_stdenc_template.h"