2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
5 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
6 * Copyright (c) 2002-2004 Tim J. Robbins
9 * Copyright (c) 2011 The FreeBSD Foundation
10 * All rights reserved.
11 * Portions of this software were developed by David Chisnall
12 * under sponsorship from the FreeBSD Foundation.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 __FBSDID("$FreeBSD$");
47 extern int __mb_sb_limit;
49 static size_t _UTF8_mbrtowc(wchar_t * __restrict, const char * __restrict,
50 size_t, mbstate_t * __restrict);
51 static int _UTF8_mbsinit(const mbstate_t *);
52 static size_t _UTF8_mbsnrtowcs(wchar_t * __restrict,
53 const char ** __restrict, size_t, size_t,
54 mbstate_t * __restrict);
55 static size_t _UTF8_wcrtomb(char * __restrict, wchar_t,
56 mbstate_t * __restrict);
57 static size_t _UTF8_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
58 size_t, size_t, mbstate_t * __restrict);
67 _UTF8_init(struct xlocale_ctype *l, _RuneLocale *rl)
70 l->__mbrtowc = _UTF8_mbrtowc;
71 l->__wcrtomb = _UTF8_wcrtomb;
72 l->__mbsinit = _UTF8_mbsinit;
73 l->__mbsnrtowcs = _UTF8_mbsnrtowcs;
74 l->__wcsnrtombs = _UTF8_wcsnrtombs;
78 * UCS-4 encoding used as the internal representation, so
79 * slots 0x0080-0x00FF are occuped and must be excluded
80 * from the single byte ctype by setting the limit.
82 l->__mb_sb_limit = 128;
88 _UTF8_mbsinit(const mbstate_t *ps)
91 return (ps == NULL || ((const _UTF8State *)ps)->want == 0);
95 _UTF8_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
96 mbstate_t * __restrict ps)
99 int ch, i, mask, want;
102 us = (_UTF8State *)ps;
104 if (us->want < 0 || us->want > 6) {
116 /* Incomplete multibyte sequence */
121 * Determine the number of octets that make up this character
122 * from the first octet, and a mask that extracts the
123 * interesting bits of the first octet. We already know
124 * the character is at least two bytes long.
126 * We also specify a lower bound for the character code to
127 * detect redundant, non-"shortest form" encodings. For
128 * example, the sequence C0 80 is _not_ a legal representation
129 * of the null character. This enforces a 1-to-1 mapping
130 * between character codes and their multibyte representations.
132 ch = (unsigned char)*s;
133 if ((ch & 0x80) == 0) {
134 /* Fast path for plain ASCII characters. */
137 return (ch != '\0' ? 1 : 0);
139 if ((ch & 0xe0) == 0xc0) {
143 } else if ((ch & 0xf0) == 0xe0) {
147 } else if ((ch & 0xf8) == 0xf0) {
153 * Malformed input; input is not UTF-8.
164 * Decode the octet sequence representing the character in chunks
165 * of 6 bits, most significant first.
168 wch = (unsigned char)*s++ & mask;
172 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
173 if ((*s & 0xc0) != 0x80) {
175 * Malformed input; bad characters in the middle
185 /* Incomplete multibyte sequence. */
193 * Malformed input; redundant encoding.
198 if ((wch >= 0xd800 && wch <= 0xdfff) || wch > 0x10ffff) {
200 * Malformed input; invalid code points.
208 return (wch == L'\0' ? 0 : want);
212 _UTF8_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
213 size_t nms, size_t len, mbstate_t * __restrict ps)
221 us = (_UTF8State *)ps;
228 * The fast path in the loop below is not safe if an ASCII
229 * character appears as anything but the first byte of a
230 * multibyte sequence. Check now to avoid doing it in the loop.
232 if (nms > 0 && us->want > 0 && (signed char)*s > 0) {
237 if (nms > 0 && (signed char)*s > 0)
239 * Fast path for plain ASCII characters
243 else if ((nb = _UTF8_mbrtowc(&wc, s, nms, ps)) ==
245 /* Invalid sequence - mbrtowc() sets errno. */
247 else if (nb == 0 || nb == (size_t)-2)
257 * The fast path in the loop below is not safe if an ASCII
258 * character appears as anything but the first byte of a
259 * multibyte sequence. Check now to avoid doing it in the loop.
261 if (nms > 0 && len > 0 && us->want > 0 && (signed char)*s > 0) {
266 if (nms > 0 && (signed char)*s > 0) {
268 * Fast path for plain ASCII characters
273 } else if ((nb = _UTF8_mbrtowc(dst, s, nms, ps)) ==
277 } else if (nb == (size_t)-2) {
280 } else if (nb == 0) {
294 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
300 us = (_UTF8State *)ps;
308 /* Reset to initial shift state (no-op) */
312 * Determine the number of octets needed to represent this character.
313 * We always output the shortest sequence possible. Also specify the
314 * first few bits of the first octet, which contains the information
315 * about the sequence length.
317 if ((wc & ~0x7f) == 0) {
318 /* Fast path for plain ASCII characters. */
321 } else if ((wc & ~0x7ff) == 0) {
324 } else if ((wc & ~0xffff) == 0) {
325 if (wc >= 0xd800 && wc <= 0xdfff) {
331 } else if (wc >= 0 && wc <= 0x10ffff) {
340 * Output the octets representing the character in chunks
341 * of 6 bits, least significant last. The first octet is
342 * a special case because it contains the sequence length
345 for (i = len - 1; i > 0; i--) {
346 s[i] = (wc & 0x3f) | 0x80;
349 *s = (wc & 0xff) | lead;
355 _UTF8_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
356 size_t nwc, size_t len, mbstate_t * __restrict ps)
359 char buf[MB_LEN_MAX];
364 us = (_UTF8State *)ps;
376 if (0 <= *s && *s < 0x80)
377 /* Fast path for plain ASCII characters. */
379 else if ((nb = _UTF8_wcrtomb(buf, *s, ps)) ==
381 /* Invalid character - wcrtomb() sets errno. */
384 return (nbytes + nb - 1);
391 while (len > 0 && nwc-- > 0) {
392 if (0 <= *s && *s < 0x80) {
393 /* Fast path for plain ASCII characters. */
396 } else if (len > (size_t)MB_CUR_MAX) {
397 /* Enough space to translate in-place. */
398 if ((nb = _UTF8_wcrtomb(dst, *s, ps)) == (size_t)-1) {
404 * May not be enough space; use temp. buffer.
406 if ((nb = _UTF8_wcrtomb(buf, *s, ps)) == (size_t)-1) {
411 /* MB sequence for character won't fit. */
413 memcpy(dst, buf, nb);
417 return (nbytes + nb - 1);