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>
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 > 4) {
113 /* Incomplete multibyte sequence */
118 * Determine the number of octets that make up this character
119 * from the first octet, and a mask that extracts the
120 * interesting bits of the first octet. We already know
121 * the character is at least two bytes long.
123 * We also specify a lower bound for the character code to
124 * detect redundant, non-"shortest form" encodings. For
125 * example, the sequence C0 80 is _not_ a legal representation
126 * of the null character. This enforces a 1-to-1 mapping
127 * between character codes and their multibyte representations.
129 ch = (unsigned char)*s;
130 if ((ch & 0x80) == 0) {
131 /* Fast path for plain ASCII characters. */
134 return (ch != '\0' ? 1 : 0);
136 if ((ch & 0xe0) == 0xc0) {
140 } else if ((ch & 0xf0) == 0xe0) {
144 } else if ((ch & 0xf8) == 0xf0) {
150 * Malformed input; input is not UTF-8.
161 * Decode the octet sequence representing the character in chunks
162 * of 6 bits, most significant first.
165 wch = (unsigned char)*s++ & mask;
169 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
170 if ((*s & 0xc0) != 0x80) {
172 * Malformed input; bad characters in the middle
182 /* Incomplete multibyte sequence. */
188 if (wch < lbound || (wch & ~10ffffff)) {
190 * Malformed input; redundant encoding.
198 return (wch == L'\0' ? 0 : want);
202 _UTF8_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
203 size_t nms, size_t len, mbstate_t * __restrict ps)
211 us = (_UTF8State *)ps;
218 * The fast path in the loop below is not safe if an ASCII
219 * character appears as anything but the first byte of a
220 * multibyte sequence. Check now to avoid doing it in the loop.
222 if (nms > 0 && us->want > 0 && (signed char)*s > 0) {
227 if (nms > 0 && (signed char)*s > 0)
229 * Fast path for plain ASCII characters
233 else if ((nb = _UTF8_mbrtowc(&wc, s, nms, ps)) ==
235 /* Invalid sequence - mbrtowc() sets errno. */
237 else if (nb == 0 || nb == (size_t)-2)
247 * The fast path in the loop below is not safe if an ASCII
248 * character appears as anything but the first byte of a
249 * multibyte sequence. Check now to avoid doing it in the loop.
251 if (nms > 0 && len > 0 && us->want > 0 && (signed char)*s > 0) {
256 if (nms > 0 && (signed char)*s > 0) {
258 * Fast path for plain ASCII characters
263 } else if ((nb = _UTF8_mbrtowc(dst, s, nms, ps)) ==
267 } else if (nb == (size_t)-2) {
270 } else if (nb == 0) {
284 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
290 us = (_UTF8State *)ps;
298 /* Reset to initial shift state (no-op) */
302 * Determine the number of octets needed to represent this character.
303 * We always output the shortest sequence possible. Also specify the
304 * first few bits of the first octet, which contains the information
305 * about the sequence length.
307 if ((wc & ~0x7f) == 0) {
308 /* Fast path for plain ASCII characters. */
311 } else if ((wc & ~0x7ff) == 0) {
314 } else if ((wc & ~0xffff) == 0) {
317 } else if ((wc & ~0x10ffff) == 0) {
326 * Output the octets representing the character in chunks
327 * of 6 bits, least significant last. The first octet is
328 * a special case because it contains the sequence length
331 for (i = len - 1; i > 0; i--) {
332 s[i] = (wc & 0x3f) | 0x80;
335 *s = (wc & 0xff) | lead;
341 _UTF8_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
342 size_t nwc, size_t len, mbstate_t * __restrict ps)
345 char buf[MB_LEN_MAX];
350 us = (_UTF8State *)ps;
362 if (0 <= *s && *s < 0x80)
363 /* Fast path for plain ASCII characters. */
365 else if ((nb = _UTF8_wcrtomb(buf, *s, ps)) ==
367 /* Invalid character - wcrtomb() sets errno. */
370 return (nbytes + nb - 1);
377 while (len > 0 && nwc-- > 0) {
378 if (0 <= *s && *s < 0x80) {
379 /* Fast path for plain ASCII characters. */
382 } else if (len > (size_t)MB_CUR_MAX) {
383 /* Enough space to translate in-place. */
384 if ((nb = _UTF8_wcrtomb(dst, *s, ps)) == (size_t)-1) {
390 * May not be enough space; use temp. buffer.
392 if ((nb = _UTF8_wcrtomb(buf, *s, ps)) == (size_t)-1) {
397 /* MB sequence for character won't fit. */
399 (void) memcpy(dst, buf, nb);
403 return (nbytes + nb - 1);