]>
git.saurik.com Git - apple/libc.git/blob - locale/utf2-fbsd.c
0e00131028399214cab6fc2489364aed6dd9cfec
2 * Copyright (c) 2002-2004 Tim J. Robbins
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
28 __FBSDID("$FreeBSD: src/lib/libc/locale/utf8.c,v 1.16 2007/10/15 09:51:30 ache Exp $");
30 #include "xlocale_private.h"
40 #define UTF2_MB_CUR_MAX 3
42 static size_t _UTF2_mbrtowc(wchar_t * __restrict
, const char * __restrict
,
43 size_t, mbstate_t * __restrict
, locale_t
);
44 static int _UTF2_mbsinit(const mbstate_t *, locale_t
);
45 static size_t _UTF2_mbsnrtowcs(wchar_t * __restrict
,
46 const char ** __restrict
, size_t, size_t,
47 mbstate_t * __restrict
, locale_t
);
48 static size_t _UTF2_wcrtomb(char * __restrict
, wchar_t,
49 mbstate_t * __restrict
, locale_t
);
50 static size_t _UTF2_wcsnrtombs(char * __restrict
, const wchar_t ** __restrict
,
51 size_t, size_t, mbstate_t * __restrict
, locale_t
);
59 __private_extern__
int
60 _UTF2_init(struct __xlocale_st_runelocale
*xrl
)
63 xrl
->__mbrtowc
= _UTF2_mbrtowc
;
64 xrl
->__wcrtomb
= _UTF2_wcrtomb
;
65 xrl
->__mbsinit
= _UTF2_mbsinit
;
66 xrl
->__mbsnrtowcs
= _UTF2_mbsnrtowcs
;
67 xrl
->__wcsnrtombs
= _UTF2_wcsnrtombs
;
68 xrl
->__mb_cur_max
= UTF2_MB_CUR_MAX
;
70 * UCS-4 encoding used as the internal representation, so
71 * slots 0x0080-0x00FF are occuped and must be excluded
72 * from the single byte ctype by setting the limit.
74 xrl
->__mb_sb_limit
= 128;
80 _UTF2_mbsinit(const mbstate_t *ps
, locale_t loc
)
83 return (ps
== NULL
|| ((const _UTF2State
*)ps
)->want
== 0);
87 _UTF2_mbrtowc(wchar_t * __restrict pwc
, const char * __restrict s
, size_t n
,
88 mbstate_t * __restrict ps
, locale_t loc
)
91 int ch
, i
, mask
, want
;
94 us
= (_UTF2State
*)ps
;
96 if (us
->want
< 0 || us
->want
> 6) {
108 /* Incomplete multibyte sequence */
111 if (us
->want
== 0 && ((ch
= (unsigned char)*s
) & ~0x7f) == 0) {
112 /* Fast path for plain ASCII characters. */
115 return (ch
!= '\0' ? 1 : 0);
120 * Determine the number of octets that make up this character
121 * from the first octet, and a mask that extracts the
122 * interesting bits of the first octet. We already know
123 * the character is at least two bytes long.
125 * We also specify a lower bound for the character code to
126 * detect redundant, non-"shortest form" encodings. For
127 * example, the sequence C0 80 is _not_ a legal representation
128 * of the null character. This enforces a 1-to-1 mapping
129 * between character codes and their multibyte representations.
131 ch
= (unsigned char)*s
;
132 if ((ch
& 0x80) == 0) {
136 } else if ((ch
& 0xe0) == 0xc0) {
140 } else if ((ch
& 0xf0) == 0xe0) {
146 * Malformed input; input is not UTF2.
157 * Decode the octet sequence representing the character in chunks
158 * of 6 bits, most significant first.
161 wch
= (unsigned char)*s
++ & mask
;
164 for (i
= (us
->want
== 0) ? 1 : 0; i
< MIN(want
, n
); i
++) {
165 if ((*s
& 0xc0) != 0x80) {
167 * Malformed input; bad characters in the middle
177 /* Incomplete multibyte sequence. */
185 * Malformed input; redundant encoding.
193 return (wch
== L
'\0' ? 0 : want
);
197 _UTF2_mbsnrtowcs(wchar_t * __restrict dst
, const char ** __restrict src
,
198 size_t nms
, size_t len
, mbstate_t * __restrict ps
, locale_t loc
)
206 us
= (_UTF2State
*)ps
;
213 * The fast path in the loop below is not safe if an ASCII
214 * character appears as anything but the first byte of a
215 * multibyte sequence. Check now to avoid doing it in the loop.
217 if (nms
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
222 if (nms
> 0 && (signed char)*s
> 0)
224 * Fast path for plain ASCII characters
228 else if ((nb
= _UTF2_mbrtowc(&wc
, s
, nms
, ps
, loc
)) ==
230 /* Invalid sequence - mbrtowc() sets errno. */
232 else if (nb
== 0 || nb
== (size_t)-2)
242 * The fast path in the loop below is not safe if an ASCII
243 * character appears as anything but the first byte of a
244 * multibyte sequence. Check now to avoid doing it in the loop.
246 if (nms
> 0 && len
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
251 if (nms
> 0 && (signed char)*s
> 0) {
253 * Fast path for plain ASCII characters
258 } else if ((nb
= _UTF2_mbrtowc(dst
, s
, nms
, ps
, loc
)) ==
262 } else if (nb
== (size_t)-2) {
265 } else if (nb
== 0) {
279 _UTF2_wcrtomb(char * __restrict s
, wchar_t wc
, mbstate_t * __restrict ps
, locale_t loc
)
285 us
= (_UTF2State
*)ps
;
293 /* Reset to initial shift state (no-op) */
296 if ((wc
& ~0x7f) == 0) {
297 /* Fast path for plain ASCII characters. */
303 * Determine the number of octets needed to represent this character.
304 * We always output the shortest sequence possible. Also specify the
305 * first few bits of the first octet, which contains the information
306 * about the sequence length.
308 if ((wc
& ~0x7f) == 0) {
311 } else if ((wc
& ~0x7ff) == 0) {
314 } else if ((wc
& ~0xffff) == 0) {
323 * Output the octets representing the character in chunks
324 * of 6 bits, least significant last. The first octet is
325 * a special case because it contains the sequence length
328 for (i
= len
- 1; i
> 0; i
--) {
329 s
[i
] = (wc
& 0x3f) | 0x80;
332 *s
= (wc
& 0xff) | lead
;
338 _UTF2_wcsnrtombs(char * __restrict dst
, const wchar_t ** __restrict src
,
339 size_t nwc
, size_t len
, mbstate_t * __restrict ps
, locale_t loc
)
342 char buf
[MB_LEN_MAX
];
347 us
= (_UTF2State
*)ps
;
359 if (0 <= *s
&& *s
< 0x80)
360 /* Fast path for plain ASCII characters. */
362 else if ((nb
= _UTF2_wcrtomb(buf
, *s
, ps
, loc
)) ==
364 /* Invalid character - wcrtomb() sets errno. */
367 return (nbytes
+ nb
- 1);
374 while (len
> 0 && nwc
-- > 0) {
375 if (0 <= *s
&& *s
< 0x80) {
376 /* Fast path for plain ASCII characters. */
379 } else if (len
> (size_t)UTF2_MB_CUR_MAX
) {
380 /* Enough space to translate in-place. */
381 if ((nb
= _UTF2_wcrtomb(dst
, *s
, ps
, loc
)) == (size_t)-1) {
387 * May not be enough space; use temp. buffer.
389 if ((nb
= _UTF2_wcrtomb(buf
, *s
, ps
, loc
)) == (size_t)-1) {
394 /* MB sequence for character won't fit. */
396 memcpy(dst
, buf
, nb
);
400 return (nbytes
+ nb
- 1);