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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static size_t _UTF8_mbrtowc(wchar_t *_RESTRICT_KYWD
,
39 const char *_RESTRICT_KYWD
,
40 size_t, mbstate_t *_RESTRICT_KYWD
);
41 static int _UTF8_mbsinit(const mbstate_t *);
42 static size_t _UTF8_mbsnrtowcs(wchar_t *_RESTRICT_KYWD
,
43 const char **_RESTRICT_KYWD
, size_t, size_t,
44 mbstate_t *_RESTRICT_KYWD
);
45 static size_t _UTF8_wcrtomb(char *_RESTRICT_KYWD
, wchar_t,
46 mbstate_t *_RESTRICT_KYWD
);
47 static size_t _UTF8_wcsnrtombs(char *_RESTRICT_KYWD
,
48 const wchar_t **_RESTRICT_KYWD
,
49 size_t, size_t, mbstate_t *_RESTRICT_KYWD
);
58 _UTF8_init(struct lc_ctype
*lct
)
60 lct
->lc_mbrtowc
= _UTF8_mbrtowc
;
61 lct
->lc_wcrtomb
= _UTF8_wcrtomb
;
62 lct
->lc_mbsinit
= _UTF8_mbsinit
;
63 lct
->lc_mbsnrtowcs
= _UTF8_mbsnrtowcs
;
64 lct
->lc_wcsnrtombs
= _UTF8_wcsnrtombs
;
66 lct
->lc_max_mblen
= 4;
70 _UTF8_mbsinit(const mbstate_t *ps
)
73 return (ps
== NULL
|| ((const _UTF8State
*)ps
)->want
== 0);
77 _UTF8_mbrtowc(wchar_t *_RESTRICT_KYWD pwc
, const char *_RESTRICT_KYWD s
,
78 size_t n
, mbstate_t *_RESTRICT_KYWD ps
)
81 int ch
, i
, mask
, want
;
84 us
= (_UTF8State
*)ps
;
86 if (us
->want
< 0 || us
->want
> 6) {
98 /* Incomplete multibyte sequence */
103 * Determine the number of octets that make up this character
104 * from the first octet, and a mask that extracts the
105 * interesting bits of the first octet. We already know
106 * the character is at least two bytes long.
108 * We also specify a lower bound for the character code to
109 * detect redundant, non-"shortest form" encodings. For
110 * example, the sequence C0 80 is _not_ a legal representation
111 * of the null character. This enforces a 1-to-1 mapping
112 * between character codes and their multibyte representations.
114 ch
= (unsigned char)*s
;
115 if ((ch
& 0x80) == 0) {
116 /* Fast path for plain ASCII characters. */
119 return (ch
!= '\0' ? 1 : 0);
121 if ((ch
& 0xe0) == 0xc0) {
125 } else if ((ch
& 0xf0) == 0xe0) {
129 } else if ((ch
& 0xf8) == 0xf0) {
134 /* These would be illegal in the UTF-8 space */
136 } else if ((ch
& 0xfc) == 0xf8) {
140 } else if ((ch
& 0xfe) == 0xfc) {
147 * Malformed input; input is not UTF-8.
158 * Decode the octet sequence representing the character in chunks
159 * of 6 bits, most significant first.
162 wch
= (unsigned char)*s
++ & mask
;
166 for (i
= (us
->want
== 0) ? 1 : 0; i
< MIN(want
, n
); i
++) {
167 if ((*s
& 0xc0) != 0x80) {
169 * Malformed input; bad characters in the middle
179 /* Incomplete multibyte sequence. */
187 * Malformed input; redundant encoding.
195 return (wch
== L
'\0' ? 0 : want
);
199 _UTF8_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst
, const char **_RESTRICT_KYWD src
,
200 size_t nms
, size_t len
, mbstate_t *_RESTRICT_KYWD ps
)
208 us
= (_UTF8State
*)ps
;
215 * The fast path in the loop below is not safe if an ASCII
216 * character appears as anything but the first byte of a
217 * multibyte sequence. Check now to avoid doing it in the loop.
219 if (nms
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
224 if (nms
> 0 && (signed char)*s
> 0)
226 * Fast path for plain ASCII characters
230 else if ((nb
= _UTF8_mbrtowc(&wc
, s
, nms
, ps
)) ==
232 /* Invalid sequence - mbrtowc() sets errno. */
234 else if (nb
== 0 || nb
== (size_t)-2)
244 * The fast path in the loop below is not safe if an ASCII
245 * character appears as anything but the first byte of a
246 * multibyte sequence. Check now to avoid doing it in the loop.
248 if (nms
> 0 && len
> 0 && us
->want
> 0 && (signed char)*s
> 0) {
253 if (nms
> 0 && (signed char)*s
> 0) {
255 * Fast path for plain ASCII characters
260 } else if ((nb
= _UTF8_mbrtowc(dst
, s
, nms
, ps
)) ==
264 } else if (nb
== (size_t)-2) {
267 } else if (nb
== 0) {
281 _UTF8_wcrtomb(char *_RESTRICT_KYWD s
, wchar_t wc
, mbstate_t *_RESTRICT_KYWD ps
)
287 us
= (_UTF8State
*)ps
;
295 /* Reset to initial shift state (no-op) */
299 * Determine the number of octets needed to represent this character.
300 * We always output the shortest sequence possible. Also specify the
301 * first few bits of the first octet, which contains the information
302 * about the sequence length.
304 if ((wc
& ~0x7f) == 0) {
305 /* Fast path for plain ASCII characters. */
308 } else if ((wc
& ~0x7ff) == 0) {
311 } else if ((wc
& ~0xffff) == 0) {
314 } else if ((wc
& ~0x1fffff) == 0) {
318 /* Again, 5 and 6 byte encodings are simply not permitted */
319 } else if ((wc
& ~0x3ffffff) == 0) {
322 } else if ((wc
& ~0x7fffffff) == 0) {
332 * Output the octets representing the character in chunks
333 * of 6 bits, least significant last. The first octet is
334 * a special case because it contains the sequence length
337 for (i
= len
- 1; i
> 0; i
--) {
338 s
[i
] = (wc
& 0x3f) | 0x80;
341 *s
= (wc
& 0xff) | lead
;
347 _UTF8_wcsnrtombs(char *_RESTRICT_KYWD dst
, const wchar_t **_RESTRICT_KYWD src
,
348 size_t nwc
, size_t len
, mbstate_t *_RESTRICT_KYWD ps
)
351 char buf
[MB_LEN_MAX
];
356 us
= (_UTF8State
*)ps
;
368 if (0 <= *s
&& *s
< 0x80)
369 /* Fast path for plain ASCII characters. */
371 else if ((nb
= _UTF8_wcrtomb(buf
, *s
, ps
)) ==
373 /* Invalid character - wcrtomb() sets errno. */
376 return (nbytes
+ nb
- 1);
383 while (len
> 0 && nwc
-- > 0) {
384 if (0 <= *s
&& *s
< 0x80) {
385 /* Fast path for plain ASCII characters. */
388 } else if (len
> (size_t)MB_CUR_MAX
) {
389 /* Enough space to translate in-place. */
390 if ((nb
= _UTF8_wcrtomb(dst
, *s
, ps
)) == (size_t)-1) {
396 * May not be enough space; use temp. buffer.
398 if ((nb
= _UTF8_wcrtomb(buf
, *s
, ps
)) == (size_t)-1) {
403 /* MB sequence for character won't fit. */
405 (void) memcpy(dst
, buf
, nb
);
409 return (nbytes
+ nb
- 1);