]>
git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/utf8.c
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.11 2004/07/27 06:29:48 tjr Exp $");
38 size_t _UTF8_mbrtowc(wchar_t * __restrict
, const char * __restrict
, size_t,
39 mbstate_t * __restrict
);
40 int _UTF8_mbsinit(const mbstate_t *);
41 size_t _UTF8_mbsnrtowcs(wchar_t * __restrict
, const char ** __restrict
,
42 size_t, size_t, mbstate_t * __restrict
);
43 size_t _UTF8_wcrtomb(char * __restrict
, wchar_t, mbstate_t * __restrict
);
44 size_t _UTF8_wcsnrtombs(char * __restrict
, const wchar_t ** __restrict
,
45 size_t, size_t, mbstate_t * __restrict
);
54 _UTF8_init(_RuneLocale
*rl
)
57 __mbrtowc
= _UTF8_mbrtowc
;
58 __wcrtomb
= _UTF8_wcrtomb
;
59 __mbsinit
= _UTF8_mbsinit
;
60 __mbsnrtowcs
= _UTF8_mbsnrtowcs
;
61 __wcsnrtombs
= _UTF8_wcsnrtombs
;
62 _CurrentRuneLocale
= rl
;
69 _UTF8_mbsinit(const mbstate_t *ps
)
72 return (ps
== NULL
|| ((const _UTF8State
*)ps
)->want
== 0);
76 _UTF8_mbrtowc(wchar_t * __restrict pwc
, const char * __restrict s
, size_t n
,
77 mbstate_t * __restrict ps
)
80 int ch
, i
, mask
, want
;
83 us
= (_UTF8State
*)ps
;
85 if (us
->want
< 0 || us
->want
> 6) {
97 /* Incomplete multibyte sequence */
100 if (us
->want
== 0 && ((ch
= (unsigned char)*s
) & ~0x7f) == 0) {
101 /* Fast path for plain ASCII characters. */
104 return (ch
!= '\0' ? 1 : 0);
109 * Determine the number of octets that make up this character
110 * from the first octet, and a mask that extracts the
111 * interesting bits of the first octet. We already know
112 * the character is at least two bytes long.
114 * We also specify a lower bound for the character code to
115 * detect redundant, non-"shortest form" encodings. For
116 * example, the sequence C0 80 is _not_ a legal representation
117 * of the null character. This enforces a 1-to-1 mapping
118 * between character codes and their multibyte representations.
120 ch
= (unsigned char)*s
;
121 if ((ch
& 0x80) == 0) {
125 } else if ((ch
& 0xe0) == 0xc0) {
129 } else if ((ch
& 0xf0) == 0xe0) {
133 } else if ((ch
& 0xf8) == 0xf0) {
137 } else if ((ch
& 0xfc) == 0xf8) {
141 } else if ((ch
& 0xfc) == 0xfc) {
147 * Malformed input; input is not UTF-8.
158 * Decode the octet sequence representing the character in chunks
159 * of 6 bits, most significant first.
162 wch
= (unsigned char)*s
++ & mask
;
165 for (i
= (us
->want
== 0) ? 1 : 0; i
< MIN(want
, n
); i
++) {
166 if ((*s
& 0xc0) != 0x80) {
168 * Malformed input; bad characters in the middle
178 /* Incomplete multibyte sequence. */
186 * Malformed input; redundant encoding.
194 return (wch
== L
'\0' ? 0 : want
);
198 _UTF8_mbsnrtowcs(wchar_t * __restrict dst
, const char ** __restrict src
,
199 size_t nms
, size_t len
, mbstate_t * __restrict ps
)
207 us
= (_UTF8State
*)ps
;
214 * The fast path in the loop below is not safe if an ASCII
215 * character appears as anything but the first byte of a
216 * multibyte sequence. Check now to avoid doing it in the loop.
218 if (nms
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
223 if (nms
> 0 && (signed char)*s
> 0)
225 * Fast path for plain ASCII characters
229 else if ((nb
= _UTF8_mbrtowc(&wc
, s
, nms
, ps
)) ==
231 /* Invalid sequence - mbrtowc() sets errno. */
233 else if (nb
== 0 || nb
== (size_t)-2)
243 * The fast path in the loop below is not safe if an ASCII
244 * character appears as anything but the first byte of a
245 * multibyte sequence. Check now to avoid doing it in the loop.
247 if (nms
> 0 && len
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
252 if (nms
> 0 && (signed char)*s
> 0) {
254 * Fast path for plain ASCII characters
259 } else if ((nb
= _UTF8_mbrtowc(dst
, s
, nms
, ps
)) ==
263 } else if (nb
== (size_t)-2) {
266 } else if (nb
== 0) {
280 _UTF8_wcrtomb(char * __restrict s
, wchar_t wc
, mbstate_t * __restrict ps
)
286 us
= (_UTF8State
*)ps
;
294 /* Reset to initial shift state (no-op) */
297 if ((wc
& ~0x7f) == 0) {
298 /* Fast path for plain ASCII characters. */
304 * Determine the number of octets needed to represent this character.
305 * We always output the shortest sequence possible. Also specify the
306 * first few bits of the first octet, which contains the information
307 * about the sequence length.
309 if ((wc
& ~0x7f) == 0) {
312 } else if ((wc
& ~0x7ff) == 0) {
315 } else if ((wc
& ~0xffff) == 0) {
318 } else if ((wc
& ~0x1fffff) == 0) {
321 } else if ((wc
& ~0x3ffffff) == 0) {
324 } else if ((wc
& ~0x7fffffff) == 0) {
333 * Output the octets representing the character in chunks
334 * of 6 bits, least significant last. The first octet is
335 * a special case because it contains the sequence length
338 for (i
= len
- 1; i
> 0; i
--) {
339 s
[i
] = (wc
& 0x3f) | 0x80;
342 *s
= (wc
& 0xff) | lead
;
348 _UTF8_wcsnrtombs(char * __restrict dst
, const wchar_t ** __restrict src
,
349 size_t nwc
, size_t len
, mbstate_t * __restrict ps
)
352 char buf
[MB_LEN_MAX
];
357 us
= (_UTF8State
*)ps
;
369 if (0 <= *s
&& *s
< 0x80)
370 /* Fast path for plain ASCII characters. */
372 else if ((nb
= _UTF8_wcrtomb(buf
, *s
, ps
)) ==
374 /* Invalid character - wcrtomb() sets errno. */
377 return (nbytes
+ nb
- 1);
384 while (len
> 0 && nwc
-- > 0) {
385 if (0 <= *s
&& *s
< 0x80) {
386 /* Fast path for plain ASCII characters. */
389 } else if (len
> (size_t)MB_CUR_MAX
) {
390 /* Enough space to translate in-place. */
391 if ((nb
= (int)_UTF8_wcrtomb(dst
, *s
, ps
)) < 0) {
397 * May not be enough space; use temp. buffer.
399 if ((nb
= (int)_UTF8_wcrtomb(buf
, *s
, ps
)) < 0) {
404 /* MB sequence for character won't fit. */
406 memcpy(dst
, buf
, nb
);
410 return (nbytes
+ nb
- 1);