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
32 #include <sys/param.h>
33 __FBSDID("$FreeBSD$");
43 extern int __mb_sb_limit;
45 static size_t _UTF8_mbrtowc(wchar_t * __restrict, const char * __restrict,
46 size_t, mbstate_t * __restrict);
47 static int _UTF8_mbsinit(const mbstate_t *);
48 static size_t _UTF8_mbsnrtowcs(wchar_t * __restrict,
49 const char ** __restrict, size_t, size_t,
50 mbstate_t * __restrict);
51 static size_t _UTF8_wcrtomb(char * __restrict, wchar_t,
52 mbstate_t * __restrict);
53 static size_t _UTF8_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
54 size_t, size_t, mbstate_t * __restrict);
63 _UTF8_init(struct xlocale_ctype *l, _RuneLocale *rl)
66 l->__mbrtowc = _UTF8_mbrtowc;
67 l->__wcrtomb = _UTF8_wcrtomb;
68 l->__mbsinit = _UTF8_mbsinit;
69 l->__mbsnrtowcs = _UTF8_mbsnrtowcs;
70 l->__wcsnrtombs = _UTF8_wcsnrtombs;
74 * UCS-4 encoding used as the internal representation, so
75 * slots 0x0080-0x00FF are occuped and must be excluded
76 * from the single byte ctype by setting the limit.
78 l->__mb_sb_limit = 128;
84 _UTF8_mbsinit(const mbstate_t *ps)
87 return (ps == NULL || ((const _UTF8State *)ps)->want == 0);
91 _UTF8_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
92 mbstate_t * __restrict ps)
95 int ch, i, mask, want;
98 us = (_UTF8State *)ps;
100 if (us->want < 0 || us->want > 6) {
112 /* Incomplete multibyte sequence */
115 if (us->want == 0 && ((ch = (unsigned char)*s) & ~0x7f) == 0) {
116 /* Fast path for plain ASCII characters. */
119 return (ch != '\0' ? 1 : 0);
124 * Determine the number of octets that make up this character
125 * from the first octet, and a mask that extracts the
126 * interesting bits of the first octet. We already know
127 * the character is at least two bytes long.
129 * We also specify a lower bound for the character code to
130 * detect redundant, non-"shortest form" encodings. For
131 * example, the sequence C0 80 is _not_ a legal representation
132 * of the null character. This enforces a 1-to-1 mapping
133 * between character codes and their multibyte representations.
135 ch = (unsigned char)*s;
136 if ((ch & 0x80) == 0) {
140 } else if ((ch & 0xe0) == 0xc0) {
144 } else if ((ch & 0xf0) == 0xe0) {
148 } else if ((ch & 0xf8) == 0xf0) {
152 } else if ((ch & 0xfc) == 0xf8) {
156 } else if ((ch & 0xfe) == 0xfc) {
162 * Malformed input; input is not UTF-8.
173 * Decode the octet sequence representing the character in chunks
174 * of 6 bits, most significant first.
177 wch = (unsigned char)*s++ & mask;
180 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
181 if ((*s & 0xc0) != 0x80) {
183 * Malformed input; bad characters in the middle
193 /* Incomplete multibyte sequence. */
201 * Malformed input; redundant encoding.
209 return (wch == L'\0' ? 0 : want);
213 _UTF8_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
214 size_t nms, size_t len, mbstate_t * __restrict ps)
222 us = (_UTF8State *)ps;
229 * The fast path in the loop below is not safe if an ASCII
230 * character appears as anything but the first byte of a
231 * multibyte sequence. Check now to avoid doing it in the loop.
233 if (nms > 0 && us->want > 0 && (signed char)*s > 0) {
238 if (nms > 0 && (signed char)*s > 0)
240 * Fast path for plain ASCII characters
244 else if ((nb = _UTF8_mbrtowc(&wc, s, nms, ps)) ==
246 /* Invalid sequence - mbrtowc() sets errno. */
248 else if (nb == 0 || nb == (size_t)-2)
258 * The fast path in the loop below is not safe if an ASCII
259 * character appears as anything but the first byte of a
260 * multibyte sequence. Check now to avoid doing it in the loop.
262 if (nms > 0 && len > 0 && us->want > 0 && (signed char)*s > 0) {
267 if (nms > 0 && (signed char)*s > 0) {
269 * Fast path for plain ASCII characters
274 } else if ((nb = _UTF8_mbrtowc(dst, s, nms, ps)) ==
278 } else if (nb == (size_t)-2) {
281 } else if (nb == 0) {
295 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
301 us = (_UTF8State *)ps;
309 /* Reset to initial shift state (no-op) */
312 if ((wc & ~0x7f) == 0) {
313 /* Fast path for plain ASCII characters. */
319 * Determine the number of octets needed to represent this character.
320 * We always output the shortest sequence possible. Also specify the
321 * first few bits of the first octet, which contains the information
322 * about the sequence length.
324 if ((wc & ~0x7f) == 0) {
327 } else if ((wc & ~0x7ff) == 0) {
330 } else if ((wc & ~0xffff) == 0) {
333 } else if ((wc & ~0x1fffff) == 0) {
336 } else if ((wc & ~0x3ffffff) == 0) {
339 } else if ((wc & ~0x7fffffff) == 0) {
348 * Output the octets representing the character in chunks
349 * of 6 bits, least significant last. The first octet is
350 * a special case because it contains the sequence length
353 for (i = len - 1; i > 0; i--) {
354 s[i] = (wc & 0x3f) | 0x80;
357 *s = (wc & 0xff) | lead;
363 _UTF8_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
364 size_t nwc, size_t len, mbstate_t * __restrict ps)
367 char buf[MB_LEN_MAX];
372 us = (_UTF8State *)ps;
384 if (0 <= *s && *s < 0x80)
385 /* Fast path for plain ASCII characters. */
387 else if ((nb = _UTF8_wcrtomb(buf, *s, ps)) ==
389 /* Invalid character - wcrtomb() sets errno. */
392 return (nbytes + nb - 1);
399 while (len > 0 && nwc-- > 0) {
400 if (0 <= *s && *s < 0x80) {
401 /* Fast path for plain ASCII characters. */
404 } else if (len > (size_t)MB_CUR_MAX) {
405 /* Enough space to translate in-place. */
406 if ((nb = _UTF8_wcrtomb(dst, *s, ps)) == (size_t)-1) {
412 * May not be enough space; use temp. buffer.
414 if ((nb = _UTF8_wcrtomb(buf, *s, ps)) == (size_t)-1) {
419 /* MB sequence for character won't fit. */
421 memcpy(dst, buf, nb);
425 return (nbytes + nb - 1);