2 * Copyright (c) 2002-2004 Tim J. Robbins
5 * Copyright (c) 2011 The FreeBSD Foundation
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: head/lib/libc/locale/utf8.c 227753 2011-11-20 14:45:42Z theraven $
34 #include <sys/param.h>
44 extern int __mb_sb_limit;
46 static size_t _UTF8_mbrtowc(wchar_t * __restrict, const char * __restrict,
47 size_t, mbstate_t * __restrict);
48 static int _UTF8_mbsinit(const mbstate_t *);
49 static size_t _UTF8_mbsnrtowcs(wchar_t * __restrict,
50 const char ** __restrict, size_t, size_t,
51 mbstate_t * __restrict);
52 static size_t _UTF8_wcrtomb(char * __restrict, wchar_t,
53 mbstate_t * __restrict);
54 static size_t _UTF8_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
55 size_t, size_t, mbstate_t * __restrict);
64 _UTF8_init(struct xlocale_ctype *l, _RuneLocale *rl)
67 l->__mbrtowc = _UTF8_mbrtowc;
68 l->__wcrtomb = _UTF8_wcrtomb;
69 l->__mbsinit = _UTF8_mbsinit;
70 l->__mbsnrtowcs = _UTF8_mbsnrtowcs;
71 l->__wcsnrtombs = _UTF8_wcsnrtombs;
75 * UCS-4 encoding used as the internal representation, so
76 * slots 0x0080-0x00FF are occuped and must be excluded
77 * from the single byte ctype by setting the limit.
79 l->__mb_sb_limit = 128;
85 _UTF8_mbsinit(const mbstate_t *ps)
88 return (ps == NULL || ((const _UTF8State *)ps)->want == 0);
92 _UTF8_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
93 mbstate_t * __restrict ps)
96 int ch, i, mask, want;
99 us = (_UTF8State *)ps;
101 if (us->want < 0 || us->want > 6) {
113 /* Incomplete multibyte sequence */
116 if (us->want == 0 && ((ch = (unsigned char)*s) & ~0x7f) == 0) {
117 /* Fast path for plain ASCII characters. */
120 return (ch != '\0' ? 1 : 0);
125 * Determine the number of octets that make up this character
126 * from the first octet, and a mask that extracts the
127 * interesting bits of the first octet. We already know
128 * the character is at least two bytes long.
130 * We also specify a lower bound for the character code to
131 * detect redundant, non-"shortest form" encodings. For
132 * example, the sequence C0 80 is _not_ a legal representation
133 * of the null character. This enforces a 1-to-1 mapping
134 * between character codes and their multibyte representations.
136 ch = (unsigned char)*s;
137 if ((ch & 0x80) == 0) {
141 } else if ((ch & 0xe0) == 0xc0) {
145 } else if ((ch & 0xf0) == 0xe0) {
149 } else if ((ch & 0xf8) == 0xf0) {
153 } else if ((ch & 0xfc) == 0xf8) {
157 } else if ((ch & 0xfe) == 0xfc) {
163 * Malformed input; input is not UTF-8.
174 * Decode the octet sequence representing the character in chunks
175 * of 6 bits, most significant first.
178 wch = (unsigned char)*s++ & mask;
181 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
182 if ((*s & 0xc0) != 0x80) {
184 * Malformed input; bad characters in the middle
194 /* Incomplete multibyte sequence. */
202 * Malformed input; redundant encoding.
210 return (wch == L'\0' ? 0 : want);
214 _UTF8_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
215 size_t nms, size_t len, mbstate_t * __restrict ps)
223 us = (_UTF8State *)ps;
230 * The fast path in the loop below is not safe if an ASCII
231 * character appears as anything but the first byte of a
232 * multibyte sequence. Check now to avoid doing it in the loop.
234 if (nms > 0 && us->want > 0 && (signed char)*s > 0) {
239 if (nms > 0 && (signed char)*s > 0)
241 * Fast path for plain ASCII characters
245 else if ((nb = _UTF8_mbrtowc(&wc, s, nms, ps)) ==
247 /* Invalid sequence - mbrtowc() sets errno. */
249 else if (nb == 0 || nb == (size_t)-2)
259 * The fast path in the loop below is not safe if an ASCII
260 * character appears as anything but the first byte of a
261 * multibyte sequence. Check now to avoid doing it in the loop.
263 if (nms > 0 && len > 0 && us->want > 0 && (signed char)*s > 0) {
268 if (nms > 0 && (signed char)*s > 0) {
270 * Fast path for plain ASCII characters
275 } else if ((nb = _UTF8_mbrtowc(dst, s, nms, ps)) ==
279 } else if (nb == (size_t)-2) {
282 } else if (nb == 0) {
296 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
302 us = (_UTF8State *)ps;
310 /* Reset to initial shift state (no-op) */
313 if ((wc & ~0x7f) == 0) {
314 /* Fast path for plain ASCII characters. */
320 * Determine the number of octets needed to represent this character.
321 * We always output the shortest sequence possible. Also specify the
322 * first few bits of the first octet, which contains the information
323 * about the sequence length.
325 if ((wc & ~0x7f) == 0) {
328 } else if ((wc & ~0x7ff) == 0) {
331 } else if ((wc & ~0xffff) == 0) {
334 } else if ((wc & ~0x1fffff) == 0) {
337 } else if ((wc & ~0x3ffffff) == 0) {
340 } else if ((wc & ~0x7fffffff) == 0) {
349 * Output the octets representing the character in chunks
350 * of 6 bits, least significant last. The first octet is
351 * a special case because it contains the sequence length
354 for (i = len - 1; i > 0; i--) {
355 s[i] = (wc & 0x3f) | 0x80;
358 *s = (wc & 0xff) | lead;
364 _UTF8_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
365 size_t nwc, size_t len, mbstate_t * __restrict ps)
368 char buf[MB_LEN_MAX];
373 us = (_UTF8State *)ps;
385 if (0 <= *s && *s < 0x80)
386 /* Fast path for plain ASCII characters. */
388 else if ((nb = _UTF8_wcrtomb(buf, *s, ps)) ==
390 /* Invalid character - wcrtomb() sets errno. */
393 return (nbytes + nb - 1);
400 while (len > 0 && nwc-- > 0) {
401 if (0 <= *s && *s < 0x80) {
402 /* Fast path for plain ASCII characters. */
405 } else if (len > (size_t)MB_CUR_MAX) {
406 /* Enough space to translate in-place. */
407 if ((nb = _UTF8_wcrtomb(dst, *s, ps)) == (size_t)-1) {
413 * May not be enough space; use temp. buffer.
415 if ((nb = _UTF8_wcrtomb(buf, *s, ps)) == (size_t)-1) {
420 /* MB sequence for character won't fit. */
422 memcpy(dst, buf, nb);
426 return (nbytes + nb - 1);