1 /* Conversion UTF-8 to UCS-4.
2 Copyright (C) 2001-2002, 2006-2007, 2009-2013 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2001.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #if defined IN_LIBUNISTRING || HAVE_INLINE
26 u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n)
36 #if CONFIG_UNICODE_SAFETY
37 if ((s[1] ^ 0x80) < 0x40)
40 *puc = ((unsigned int) (c & 0x1f) << 6)
41 | (unsigned int) (s[1] ^ 0x80);
44 #if CONFIG_UNICODE_SAFETY
45 /* invalid multibyte character */
50 /* incomplete multibyte character */
59 #if CONFIG_UNICODE_SAFETY
60 if ((s[1] ^ 0x80) < 0x40)
62 if ((s[2] ^ 0x80) < 0x40)
64 if ((c >= 0xe1 || s[1] >= 0xa0)
65 && (c != 0xed || s[1] < 0xa0))
68 *puc = ((unsigned int) (c & 0x0f) << 12)
69 | ((unsigned int) (s[1] ^ 0x80) << 6)
70 | (unsigned int) (s[2] ^ 0x80);
73 #if CONFIG_UNICODE_SAFETY
74 /* invalid multibyte character */
78 /* invalid multibyte character */
82 /* invalid multibyte character */
87 /* incomplete multibyte character */
89 if (n == 1 || (s[1] ^ 0x80) >= 0x40)
99 #if CONFIG_UNICODE_SAFETY
100 if ((s[1] ^ 0x80) < 0x40)
102 if ((s[2] ^ 0x80) < 0x40)
104 if ((s[3] ^ 0x80) < 0x40)
106 if ((c >= 0xf1 || s[1] >= 0x90)
108 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
113 *puc = ((unsigned int) (c & 0x07) << 18)
114 | ((unsigned int) (s[1] ^ 0x80) << 12)
115 | ((unsigned int) (s[2] ^ 0x80) << 6)
116 | (unsigned int) (s[3] ^ 0x80);
119 #if CONFIG_UNICODE_SAFETY
120 /* invalid multibyte character */
124 /* invalid multibyte character */
128 /* invalid multibyte character */
132 /* invalid multibyte character */
137 /* incomplete multibyte character */
139 if (n == 1 || (s[1] ^ 0x80) >= 0x40)
141 else if (n == 2 || (s[2] ^ 0x80) >= 0x40)
152 #if CONFIG_UNICODE_SAFETY
153 if ((s[1] ^ 0x80) < 0x40)
155 if ((s[2] ^ 0x80) < 0x40)
157 if ((s[3] ^ 0x80) < 0x40)
159 if ((s[4] ^ 0x80) < 0x40)
161 if (c >= 0xf9 || s[1] >= 0x88)
164 *puc = ((unsigned int) (c & 0x03) << 24)
165 | ((unsigned int) (s[1] ^ 0x80) << 18)
166 | ((unsigned int) (s[2] ^ 0x80) << 12)
167 | ((unsigned int) (s[3] ^ 0x80) << 6)
168 | (unsigned int) (s[4] ^ 0x80);
171 #if CONFIG_UNICODE_SAFETY
172 /* invalid multibyte character */
176 /* invalid multibyte character */
180 /* invalid multibyte character */
184 /* invalid multibyte character */
187 /* invalid multibyte character */
192 /* incomplete multibyte character */
201 #if CONFIG_UNICODE_SAFETY
202 if ((s[1] ^ 0x80) < 0x40)
204 if ((s[2] ^ 0x80) < 0x40)
206 if ((s[3] ^ 0x80) < 0x40)
208 if ((s[4] ^ 0x80) < 0x40)
210 if ((s[5] ^ 0x80) < 0x40)
212 if (c >= 0xfd || s[1] >= 0x84)
215 *puc = ((unsigned int) (c & 0x01) << 30)
216 | ((unsigned int) (s[1] ^ 0x80) << 24)
217 | ((unsigned int) (s[2] ^ 0x80) << 18)
218 | ((unsigned int) (s[3] ^ 0x80) << 12)
219 | ((unsigned int) (s[4] ^ 0x80) << 6)
220 | (unsigned int) (s[5] ^ 0x80);
223 #if CONFIG_UNICODE_SAFETY
224 /* invalid multibyte character */
228 /* invalid multibyte character */
232 /* invalid multibyte character */
236 /* invalid multibyte character */
240 /* invalid multibyte character */
243 /* invalid multibyte character */
248 /* incomplete multibyte character */
255 /* invalid multibyte character */