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) {
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;
168 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
169 if ((*s & 0xc0) != 0x80) {
171 * Malformed input; bad characters in the middle
181 /* Incomplete multibyte sequence. */
189 * Malformed input; redundant encoding.
194 if ((wch >= 0xd800 && wch <= 0xdfff) || wch > 0x10ffff) {
196 * Malformed input; invalid code points.
204 return (wch == L'\0' ? 0 : want);
208 _UTF8_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
209 size_t nms, size_t len, mbstate_t * __restrict ps)
217 us = (_UTF8State *)ps;
224 * The fast path in the loop below is not safe if an ASCII
225 * character appears as anything but the first byte of a
226 * multibyte sequence. Check now to avoid doing it in the loop.
228 if (nms > 0 && us->want > 0 && (signed char)*s > 0) {
233 if (nms > 0 && (signed char)*s > 0)
235 * Fast path for plain ASCII characters
239 else if ((nb = _UTF8_mbrtowc(&wc, s, nms, ps)) ==
241 /* Invalid sequence - mbrtowc() sets errno. */
243 else if (nb == 0 || nb == (size_t)-2)
253 * The fast path in the loop below is not safe if an ASCII
254 * character appears as anything but the first byte of a
255 * multibyte sequence. Check now to avoid doing it in the loop.
257 if (nms > 0 && len > 0 && us->want > 0 && (signed char)*s > 0) {
262 if (nms > 0 && (signed char)*s > 0) {
264 * Fast path for plain ASCII characters
269 } else if ((nb = _UTF8_mbrtowc(dst, s, nms, ps)) ==
273 } else if (nb == (size_t)-2) {
276 } else if (nb == 0) {
290 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
296 us = (_UTF8State *)ps;
304 /* Reset to initial shift state (no-op) */
308 * Determine the number of octets needed to represent this character.
309 * We always output the shortest sequence possible. Also specify the
310 * first few bits of the first octet, which contains the information
311 * about the sequence length.
313 if ((wc & ~0x7f) == 0) {
314 /* Fast path for plain ASCII characters. */
317 } else if ((wc & ~0x7ff) == 0) {
320 } else if ((wc & ~0xffff) == 0) {
321 if (wc >= 0xd800 && wc <= 0xdfff) {
327 } else if (wc >= 0 && wc <= 0x10ffff) {
336 * Output the octets representing the character in chunks
337 * of 6 bits, least significant last. The first octet is
338 * a special case because it contains the sequence length
341 for (i = len - 1; i > 0; i--) {
342 s[i] = (wc & 0x3f) | 0x80;
345 *s = (wc & 0xff) | lead;
351 _UTF8_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
352 size_t nwc, size_t len, mbstate_t * __restrict ps)
355 char buf[MB_LEN_MAX];
360 us = (_UTF8State *)ps;
372 if (0 <= *s && *s < 0x80)
373 /* Fast path for plain ASCII characters. */
375 else if ((nb = _UTF8_wcrtomb(buf, *s, ps)) ==
377 /* Invalid character - wcrtomb() sets errno. */
380 return (nbytes + nb - 1);
387 while (len > 0 && nwc-- > 0) {
388 if (0 <= *s && *s < 0x80) {
389 /* Fast path for plain ASCII characters. */
392 } else if (len > (size_t)MB_CUR_MAX) {
393 /* Enough space to translate in-place. */
394 if ((nb = _UTF8_wcrtomb(dst, *s, ps)) == (size_t)-1) {
400 * May not be enough space; use temp. buffer.
402 if ((nb = _UTF8_wcrtomb(buf, *s, ps)) == (size_t)-1) {
407 /* MB sequence for character won't fit. */
409 memcpy(dst, buf, nb);
413 return (nbytes + nb - 1);