]>
git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/utf2.c
2 * Copyright (c) 2002-2004 Tim J. Robbins
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 // MWW: Generated by applying utf2.c.patch to utf8.c in the FreeBSD patch sets.
29 #include <sys/param.h>
30 __FBSDID("$FreeBSD: src/lib/libc/locale/utf8.c,v 1.16 2007/10/15 09:51:30 ache Exp $");
32 #include "xlocale_private.h"
42 #define UTF2_MB_CUR_MAX 3
44 static size_t _UTF2_mbrtowc(wchar_t * __restrict
, const char * __restrict
,
45 size_t, mbstate_t * __restrict
, locale_t
);
46 static int _UTF2_mbsinit(const mbstate_t *, locale_t
);
47 static size_t _UTF2_mbsnrtowcs(wchar_t * __restrict
,
48 const char ** __restrict
, size_t, size_t,
49 mbstate_t * __restrict
, locale_t
);
50 static size_t _UTF2_wcrtomb(char * __restrict
, wchar_t,
51 mbstate_t * __restrict
, locale_t
);
52 static size_t _UTF2_wcsnrtombs(char * __restrict
, const wchar_t ** __restrict
,
53 size_t, size_t, mbstate_t * __restrict
, locale_t
);
62 _UTF2_init(struct __xlocale_st_runelocale
*xrl
)
65 xrl
->__mbrtowc
= _UTF2_mbrtowc
;
66 xrl
->__wcrtomb
= _UTF2_wcrtomb
;
67 xrl
->__mbsinit
= _UTF2_mbsinit
;
68 xrl
->__mbsnrtowcs
= _UTF2_mbsnrtowcs
;
69 xrl
->__wcsnrtombs
= _UTF2_wcsnrtombs
;
70 xrl
->__mb_cur_max
= UTF2_MB_CUR_MAX
;
72 * UCS-4 encoding used as the internal representation, so
73 * slots 0x0080-0x00FF are occuped and must be excluded
74 * from the single byte ctype by setting the limit.
76 xrl
->__mb_sb_limit
= 128;
82 _UTF2_mbsinit(const mbstate_t *ps
, locale_t loc
)
85 return (ps
== NULL
|| ((const _UTF2State
*)ps
)->want
== 0);
89 _UTF2_mbrtowc(wchar_t * __restrict pwc
, const char * __restrict s
, size_t n
,
90 mbstate_t * __restrict ps
, locale_t loc
)
93 int ch
, i
, mask
, want
;
96 us
= (_UTF2State
*)ps
;
98 if (us
->want
< 0 || us
->want
> 6) {
110 /* Incomplete multibyte sequence */
113 if (us
->want
== 0 && ((ch
= (unsigned char)*s
) & ~0x7f) == 0) {
114 /* Fast path for plain ASCII characters. */
117 return (ch
!= '\0' ? 1 : 0);
122 * Determine the number of octets that make up this character
123 * from the first octet, and a mask that extracts the
124 * interesting bits of the first octet. We already know
125 * the character is at least two bytes long.
127 * We also specify a lower bound for the character code to
128 * detect redundant, non-"shortest form" encodings. For
129 * example, the sequence C0 80 is _not_ a legal representation
130 * of the null character. This enforces a 1-to-1 mapping
131 * between character codes and their multibyte representations.
133 ch
= (unsigned char)*s
;
134 if ((ch
& 0x80) == 0) {
138 } else if ((ch
& 0xe0) == 0xc0) {
142 } else if ((ch
& 0xf0) == 0xe0) {
148 * Malformed input; input is not UTF2.
159 * Decode the octet sequence representing the character in chunks
160 * of 6 bits, most significant first.
163 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 _UTF2_mbsnrtowcs(wchar_t * __restrict dst
, const char ** __restrict src
,
200 size_t nms
, size_t len
, mbstate_t * __restrict ps
, locale_t loc
)
208 us
= (_UTF2State
*)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
= _UTF2_mbrtowc(&wc
, s
, nms
, ps
, loc
)) ==
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
= _UTF2_mbrtowc(dst
, s
, nms
, ps
, loc
)) ==
264 } else if (nb
== (size_t)-2) {
267 } else if (nb
== 0) {
281 _UTF2_wcrtomb(char * __restrict s
, wchar_t wc
, mbstate_t * __restrict ps
, locale_t loc
)
287 us
= (_UTF2State
*)ps
;
295 /* Reset to initial shift state (no-op) */
298 if ((wc
& ~0x7f) == 0) {
299 /* Fast path for plain ASCII characters. */
305 * Determine the number of octets needed to represent this character.
306 * We always output the shortest sequence possible. Also specify the
307 * first few bits of the first octet, which contains the information
308 * about the sequence length.
310 if ((wc
& ~0x7f) == 0) {
313 } else if ((wc
& ~0x7ff) == 0) {
316 } else if ((wc
& ~0xffff) == 0) {
325 * Output the octets representing the character in chunks
326 * of 6 bits, least significant last. The first octet is
327 * a special case because it contains the sequence length
330 for (i
= len
- 1; i
> 0; i
--) {
331 s
[i
] = (wc
& 0x3f) | 0x80;
334 *s
= (wc
& 0xff) | lead
;
340 _UTF2_wcsnrtombs(char * __restrict dst
, const wchar_t ** __restrict src
,
341 size_t nwc
, size_t len
, mbstate_t * __restrict ps
, locale_t loc
)
344 char buf
[MB_LEN_MAX
];
349 us
= (_UTF2State
*)ps
;
361 if (0 <= *s
&& *s
< 0x80)
362 /* Fast path for plain ASCII characters. */
364 else if ((nb
= _UTF2_wcrtomb(buf
, *s
, ps
, loc
)) ==
366 /* Invalid character - wcrtomb() sets errno. */
369 return (nbytes
+ nb
- 1);
376 while (len
> 0 && nwc
-- > 0) {
377 if (0 <= *s
&& *s
< 0x80) {
378 /* Fast path for plain ASCII characters. */
381 } else if (len
> (size_t)UTF2_MB_CUR_MAX
) {
382 /* Enough space to translate in-place. */
383 if ((nb
= _UTF2_wcrtomb(dst
, *s
, ps
, loc
)) == (size_t)-1) {
389 * May not be enough space; use temp. buffer.
391 if ((nb
= _UTF2_wcrtomb(buf
, *s
, ps
, loc
)) == (size_t)-1) {
396 /* MB sequence for character won't fit. */
398 memcpy(dst
, buf
, nb
);
402 return (nbytes
+ nb
- 1);