2 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
3 * Copyright (c) 2002-2004 Tim J. Robbins
6 * Copyright (c) 2011 The FreeBSD Foundation
8 * Portions of this software were developed by David Chisnall
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 __FBSDID("$FreeBSD$");
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 */
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) {
148 } else if ((ch & 0xfc) == 0xf8) {
152 } else if ((ch & 0xfe) == 0xfc) {
158 * Malformed input; input is not UTF-8.
169 * Decode the octet sequence representing the character in chunks
170 * of 6 bits, most significant first.
173 wch = (unsigned char)*s++ & mask;
176 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
177 if ((*s & 0xc0) != 0x80) {
179 * Malformed input; bad characters in the middle
189 /* Incomplete multibyte sequence. */
197 * Malformed input; redundant encoding.
202 if (wch >= 0xd800 && wch <= 0xdfff) {
204 * Malformed input; invalid code points.
212 return (wch == L'\0' ? 0 : want);
216 _UTF8_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
217 size_t nms, size_t len, mbstate_t * __restrict ps)
225 us = (_UTF8State *)ps;
232 * The fast path in the loop below is not safe if an ASCII
233 * character appears as anything but the first byte of a
234 * multibyte sequence. Check now to avoid doing it in the loop.
236 if (nms > 0 && us->want > 0 && (signed char)*s > 0) {
241 if (nms > 0 && (signed char)*s > 0)
243 * Fast path for plain ASCII characters
247 else if ((nb = _UTF8_mbrtowc(&wc, s, nms, ps)) ==
249 /* Invalid sequence - mbrtowc() sets errno. */
251 else if (nb == 0 || nb == (size_t)-2)
261 * The fast path in the loop below is not safe if an ASCII
262 * character appears as anything but the first byte of a
263 * multibyte sequence. Check now to avoid doing it in the loop.
265 if (nms > 0 && len > 0 && us->want > 0 && (signed char)*s > 0) {
270 if (nms > 0 && (signed char)*s > 0) {
272 * Fast path for plain ASCII characters
277 } else if ((nb = _UTF8_mbrtowc(dst, s, nms, ps)) ==
281 } else if (nb == (size_t)-2) {
284 } else if (nb == 0) {
298 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
304 us = (_UTF8State *)ps;
312 /* Reset to initial shift state (no-op) */
316 * Determine the number of octets needed to represent this character.
317 * We always output the shortest sequence possible. Also specify the
318 * first few bits of the first octet, which contains the information
319 * about the sequence length.
321 if ((wc & ~0x7f) == 0) {
322 /* Fast path for plain ASCII characters. */
325 } else if ((wc & ~0x7ff) == 0) {
328 } else if ((wc & ~0xffff) == 0) {
331 } else if ((wc & ~0x1fffff) == 0) {
334 } else if ((wc & ~0x3ffffff) == 0) {
337 } else if ((wc & ~0x7fffffff) == 0) {
346 * Output the octets representing the character in chunks
347 * of 6 bits, least significant last. The first octet is
348 * a special case because it contains the sequence length
351 for (i = len - 1; i > 0; i--) {
352 s[i] = (wc & 0x3f) | 0x80;
355 *s = (wc & 0xff) | lead;
361 _UTF8_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
362 size_t nwc, size_t len, mbstate_t * __restrict ps)
365 char buf[MB_LEN_MAX];
370 us = (_UTF8State *)ps;
382 if (0 <= *s && *s < 0x80)
383 /* Fast path for plain ASCII characters. */
385 else if ((nb = _UTF8_wcrtomb(buf, *s, ps)) ==
387 /* Invalid character - wcrtomb() sets errno. */
390 return (nbytes + nb - 1);
397 while (len > 0 && nwc-- > 0) {
398 if (0 <= *s && *s < 0x80) {
399 /* Fast path for plain ASCII characters. */
402 } else if (len > (size_t)MB_CUR_MAX) {
403 /* Enough space to translate in-place. */
404 if ((nb = _UTF8_wcrtomb(dst, *s, ps)) == (size_t)-1) {
410 * May not be enough space; use temp. buffer.
412 if ((nb = _UTF8_wcrtomb(buf, *s, ps)) == (size_t)-1) {
417 /* MB sequence for character won't fit. */
419 memcpy(dst, buf, nb);
423 return (nbytes + nb - 1);