2 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 2002-2004 Tim J. Robbins
7 * Copyright (c) 2011 The FreeBSD Foundation
9 * Portions of this software were developed by David Chisnall
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 __FBSDID("$FreeBSD$");
45 extern int __mb_sb_limit;
47 static size_t _UTF8_mbrtowc(wchar_t * __restrict, const char * __restrict,
48 size_t, mbstate_t * __restrict);
49 static int _UTF8_mbsinit(const mbstate_t *);
50 static size_t _UTF8_mbsnrtowcs(wchar_t * __restrict,
51 const char ** __restrict, size_t, size_t,
52 mbstate_t * __restrict);
53 static size_t _UTF8_wcrtomb(char * __restrict, wchar_t,
54 mbstate_t * __restrict);
55 static size_t _UTF8_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
56 size_t, size_t, mbstate_t * __restrict);
65 _UTF8_init(struct xlocale_ctype *l, _RuneLocale *rl)
68 l->__mbrtowc = _UTF8_mbrtowc;
69 l->__wcrtomb = _UTF8_wcrtomb;
70 l->__mbsinit = _UTF8_mbsinit;
71 l->__mbsnrtowcs = _UTF8_mbsnrtowcs;
72 l->__wcsnrtombs = _UTF8_wcsnrtombs;
76 * UCS-4 encoding used as the internal representation, so
77 * slots 0x0080-0x00FF are occuped and must be excluded
78 * from the single byte ctype by setting the limit.
80 l->__mb_sb_limit = 128;
86 _UTF8_mbsinit(const mbstate_t *ps)
89 return (ps == NULL || ((const _UTF8State *)ps)->want == 0);
93 _UTF8_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
94 mbstate_t * __restrict ps)
97 int ch, i, mask, want;
100 us = (_UTF8State *)ps;
102 if (us->want < 0 || us->want > 6) {
114 /* Incomplete multibyte sequence */
119 * Determine the number of octets that make up this character
120 * from the first octet, and a mask that extracts the
121 * interesting bits of the first octet. We already know
122 * the character is at least two bytes long.
124 * We also specify a lower bound for the character code to
125 * detect redundant, non-"shortest form" encodings. For
126 * example, the sequence C0 80 is _not_ a legal representation
127 * of the null character. This enforces a 1-to-1 mapping
128 * between character codes and their multibyte representations.
130 ch = (unsigned char)*s;
131 if ((ch & 0x80) == 0) {
132 /* Fast path for plain ASCII characters. */
135 return (ch != '\0' ? 1 : 0);
137 if ((ch & 0xe0) == 0xc0) {
141 } else if ((ch & 0xf0) == 0xe0) {
145 } else if ((ch & 0xf8) == 0xf0) {
151 * Malformed input; input is not UTF-8.
162 * Decode the octet sequence representing the character in chunks
163 * of 6 bits, most significant first.
166 wch = (unsigned char)*s++ & mask;
170 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
171 if ((*s & 0xc0) != 0x80) {
173 * Malformed input; bad characters in the middle
183 /* Incomplete multibyte sequence. */
191 * Malformed input; redundant encoding.
196 if ((wch >= 0xd800 && wch <= 0xdfff) || wch > 0x10ffff) {
198 * Malformed input; invalid code points.
206 return (wch == L'\0' ? 0 : want);
210 _UTF8_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
211 size_t nms, size_t len, mbstate_t * __restrict ps)
219 us = (_UTF8State *)ps;
226 * The fast path in the loop below is not safe if an ASCII
227 * character appears as anything but the first byte of a
228 * multibyte sequence. Check now to avoid doing it in the loop.
230 if (nms > 0 && us->want > 0 && (signed char)*s > 0) {
235 if (nms > 0 && (signed char)*s > 0)
237 * Fast path for plain ASCII characters
241 else if ((nb = _UTF8_mbrtowc(&wc, s, nms, ps)) ==
243 /* Invalid sequence - mbrtowc() sets errno. */
245 else if (nb == 0 || nb == (size_t)-2)
255 * The fast path in the loop below is not safe if an ASCII
256 * character appears as anything but the first byte of a
257 * multibyte sequence. Check now to avoid doing it in the loop.
259 if (nms > 0 && len > 0 && us->want > 0 && (signed char)*s > 0) {
264 if (nms > 0 && (signed char)*s > 0) {
266 * Fast path for plain ASCII characters
271 } else if ((nb = _UTF8_mbrtowc(dst, s, nms, ps)) ==
275 } else if (nb == (size_t)-2) {
278 } else if (nb == 0) {
292 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
298 us = (_UTF8State *)ps;
306 /* Reset to initial shift state (no-op) */
310 * Determine the number of octets needed to represent this character.
311 * We always output the shortest sequence possible. Also specify the
312 * first few bits of the first octet, which contains the information
313 * about the sequence length.
315 if ((wc & ~0x7f) == 0) {
316 /* Fast path for plain ASCII characters. */
319 } else if ((wc & ~0x7ff) == 0) {
322 } else if ((wc & ~0xffff) == 0) {
323 if (wc >= 0xd800 && wc <= 0xdfff) {
329 } else if (wc >= 0 && wc <= 0x10ffff) {
338 * Output the octets representing the character in chunks
339 * of 6 bits, least significant last. The first octet is
340 * a special case because it contains the sequence length
343 for (i = len - 1; i > 0; i--) {
344 s[i] = (wc & 0x3f) | 0x80;
347 *s = (wc & 0xff) | lead;
353 _UTF8_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
354 size_t nwc, size_t len, mbstate_t * __restrict ps)
357 char buf[MB_LEN_MAX];
362 us = (_UTF8State *)ps;
374 if (0 <= *s && *s < 0x80)
375 /* Fast path for plain ASCII characters. */
377 else if ((nb = _UTF8_wcrtomb(buf, *s, ps)) ==
379 /* Invalid character - wcrtomb() sets errno. */
382 return (nbytes + nb - 1);
389 while (len > 0 && nwc-- > 0) {
390 if (0 <= *s && *s < 0x80) {
391 /* Fast path for plain ASCII characters. */
394 } else if (len > (size_t)MB_CUR_MAX) {
395 /* Enough space to translate in-place. */
396 if ((nb = _UTF8_wcrtomb(dst, *s, ps)) == (size_t)-1) {
402 * May not be enough space; use temp. buffer.
404 if ((nb = _UTF8_wcrtomb(buf, *s, ps)) == (size_t)-1) {
409 /* MB sequence for character won't fit. */
411 memcpy(dst, buf, nb);
415 return (nbytes + nb - 1);