]>
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.16 2007/10/15 09:51:30 ache Exp $");
30 #include "xlocale_private.h"
41 * 10952550: detect ill-formed UTF-8
42 * Unicode 6.0, section D92, mandates specific byte sequences for well-
43 * formed UTF-8. UTF-8 sequences are now limited to 4 bytes, while the
44 * FreeBSD code originally handled up to 6. Illegal surrogate code point
45 * sequences are now detected. And while "non-shortest forms" were detected,
46 * this only happened after completing the sequence. Now, all ill-formed
47 * sequences are detected at the earliest point.
49 * Table 3-7. Well-Formed UTF-8 Byte Sequences
51 * Code Points 1st 2nd 3rd 4th Byte
52 * U+0000..U+007F 00..7F
53 * U+0080..U+07FF C2..DF 80..BF
54 * U+0800..U+0FFF E0 A0..BF 80..BF
55 * U+1000..U+CFFF E1..EC 80..BF 80..BF
56 * U+D000..U+D7FF ED 80..9F 80..BF
57 * U+E000..U+FFFF EE..EF 80..BF 80..BF
58 * U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
59 * U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
60 * U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
62 * Note that while any 3rd and 4th byte can be in the range 80..BF, the
63 * second byte is often limited to a smaller range.
67 unsigned char lowerbound
;
68 unsigned char upperbound
;
70 static SecondByte sb_00_00
= {0x00, 0x00};
71 static SecondByte sb_80_8F
= {0x80, 0x8F};
72 static SecondByte sb_80_9F
= {0x80, 0x9F};
73 static SecondByte sb_80_BF
= {0x80, 0xBF};
74 static SecondByte sb_90_BF
= {0x90, 0xBF};
75 static SecondByte sb_A0_BF
= {0xA0, 0xBF};
77 #define UTF8_MB_CUR_MAX 4
79 static size_t _UTF8_mbrtowc(wchar_t * __restrict
, const char * __restrict
,
80 size_t, mbstate_t * __restrict
, locale_t
);
81 static int _UTF8_mbsinit(const mbstate_t *, locale_t
);
82 static size_t _UTF8_mbsnrtowcs(wchar_t * __restrict
,
83 const char ** __restrict
, size_t, size_t,
84 mbstate_t * __restrict
, locale_t
);
85 static size_t _UTF8_wcrtomb(char * __restrict
, wchar_t,
86 mbstate_t * __restrict
, locale_t
);
87 static size_t _UTF8_wcsnrtombs(char * __restrict
, const wchar_t ** __restrict
,
88 size_t, size_t, mbstate_t * __restrict
, locale_t
);
97 _UTF8_init(struct __xlocale_st_runelocale
*xrl
)
100 xrl
->__mbrtowc
= _UTF8_mbrtowc
;
101 xrl
->__wcrtomb
= _UTF8_wcrtomb
;
102 xrl
->__mbsinit
= _UTF8_mbsinit
;
103 xrl
->__mbsnrtowcs
= _UTF8_mbsnrtowcs
;
104 xrl
->__wcsnrtombs
= _UTF8_wcsnrtombs
;
105 xrl
->__mb_cur_max
= UTF8_MB_CUR_MAX
;
107 * UCS-4 encoding used as the internal representation, so
108 * slots 0x0080-0x00FF are occuped and must be excluded
109 * from the single byte ctype by setting the limit.
111 xrl
->__mb_sb_limit
= 128;
117 _UTF8_mbsinit(const mbstate_t *ps
, locale_t loc
)
120 return (ps
== NULL
|| ((const _UTF8State
*)ps
)->want
== 0);
124 _UTF8_mbrtowc(wchar_t * __restrict pwc
, const char * __restrict s
, size_t n
,
125 mbstate_t * __restrict ps
, locale_t loc
)
128 int ch
, i
, mask
, want
;
132 us
= (_UTF8State
*)ps
;
134 if (us
->want
< 0 || us
->want
> UTF8_MB_CUR_MAX
) {
146 /* Incomplete multibyte sequence */
149 if (us
->want
== 0 && ((ch
= (unsigned char)*s
) & ~0x7f) == 0) {
150 /* Fast path for plain ASCII characters. */
153 return (ch
!= '\0' ? 1 : 0);
158 * Determine the number of octets that make up this character
159 * from the first octet, and a mask that extracts the
160 * interesting bits of the first octet. We already know
161 * the character is at least two bytes long.
163 * We detect if the first byte is illegal, and set sb to
164 * the legal range of the second byte.
166 ch
= (unsigned char)*s
;
167 if ((ch
& 0x80) == 0) {
171 } else if ((ch
& 0xe0) == 0xc0) {
172 if (ch
< 0xc2) goto malformed
;
176 } else if ((ch
& 0xf0) == 0xe0) {
190 } else if ((ch
& 0xf8) == 0xf0) {
191 if (ch
> 0xf4) goto malformed
;
208 * Malformed input; input is not UTF-8.
219 * Decode the octet sequence representing the character in chunks
220 * of 6 bits, most significant first.
223 wch
= (unsigned char)*s
++ & mask
;
226 for (i
= (us
->want
== 0) ? 1 : 0; i
< MIN(want
, n
); i
++) {
228 if ((unsigned char)*s
< sb
.lowerbound
||
229 (unsigned char)*s
> sb
.upperbound
) goto malformed
;
231 } else if ((*s
& 0xc0) != 0x80) goto malformed
;
236 /* Incomplete multibyte sequence. */
245 return (wch
== L
'\0' ? 0 : want
);
249 _UTF8_mbsnrtowcs(wchar_t * __restrict dst
, const char ** __restrict src
,
250 size_t nms
, size_t len
, mbstate_t * __restrict ps
, locale_t loc
)
258 us
= (_UTF8State
*)ps
;
265 * The fast path in the loop below is not safe if an ASCII
266 * character appears as anything but the first byte of a
267 * multibyte sequence. Check now to avoid doing it in the loop.
269 if (nms
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
274 if (nms
> 0 && (signed char)*s
> 0)
276 * Fast path for plain ASCII characters
280 else if ((nb
= _UTF8_mbrtowc(&wc
, s
, nms
, ps
, loc
)) ==
282 /* Invalid sequence - mbrtowc() sets errno. */
284 else if (nb
== 0 || nb
== (size_t)-2)
294 * The fast path in the loop below is not safe if an ASCII
295 * character appears as anything but the first byte of a
296 * multibyte sequence. Check now to avoid doing it in the loop.
298 if (nms
> 0 && len
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
303 if (nms
> 0 && (signed char)*s
> 0) {
305 * Fast path for plain ASCII characters
310 } else if ((nb
= _UTF8_mbrtowc(dst
, s
, nms
, ps
, loc
)) ==
314 } else if (nb
== (size_t)-2) {
317 } else if (nb
== 0) {
331 _UTF8_wcrtomb(char * __restrict s
, wchar_t wc
, mbstate_t * __restrict ps
, locale_t loc
)
337 us
= (_UTF8State
*)ps
;
345 /* Reset to initial shift state (no-op) */
348 if ((wc
& ~0x7f) == 0) {
349 /* Fast path for plain ASCII characters. */
355 * Determine the number of octets needed to represent this character.
356 * We always output the shortest sequence possible. Also specify the
357 * first few bits of the first octet, which contains the information
358 * about the sequence length.
360 if ((wc
& ~0x7f) == 0) {
363 } else if ((wc
& ~0x7ff) == 0) {
366 } else if ((wc
& ~0xffff) == 0) {
367 if (wc
>= 0xd800 && wc
<= 0xdfff) goto illegal
;
370 } else if ((wc
& ~0x1fffff) == 0) {
371 if (wc
> 0x10ffff) goto illegal
;
381 * Output the octets representing the character in chunks
382 * of 6 bits, least significant last. The first octet is
383 * a special case because it contains the sequence length
386 for (i
= len
- 1; i
> 0; i
--) {
387 s
[i
] = (wc
& 0x3f) | 0x80;
390 *s
= (wc
& 0xff) | lead
;
396 _UTF8_wcsnrtombs(char * __restrict dst
, const wchar_t ** __restrict src
,
397 size_t nwc
, size_t len
, mbstate_t * __restrict ps
, locale_t loc
)
400 char buf
[MB_LEN_MAX
];
405 us
= (_UTF8State
*)ps
;
417 if (0 <= *s
&& *s
< 0x80)
418 /* Fast path for plain ASCII characters. */
420 else if ((nb
= _UTF8_wcrtomb(buf
, *s
, ps
, loc
)) ==
422 /* Invalid character - wcrtomb() sets errno. */
425 return (nbytes
+ nb
- 1);
432 while (len
> 0 && nwc
-- > 0) {
433 if (0 <= *s
&& *s
< 0x80) {
434 /* Fast path for plain ASCII characters. */
437 } else if (len
> (size_t)UTF8_MB_CUR_MAX
) {
438 /* Enough space to translate in-place. */
439 if ((nb
= _UTF8_wcrtomb(dst
, *s
, ps
, loc
)) == (size_t)-1) {
445 * May not be enough space; use temp. buffer.
447 if ((nb
= _UTF8_wcrtomb(buf
, *s
, ps
, loc
)) == (size_t)-1) {
452 /* MB sequence for character won't fit. */
454 memcpy(dst
, buf
, nb
);
458 return (nbytes
+ nb
- 1);