1 /* Look at first character in UTF-8 string.
2 Copyright (C) 1999-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 Lesser 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #if defined IN_LIBUNISTRING
21 /* Tell unistr.h to declare u8_mbtouc_unsafe as 'extern', not
23 # include "unistring-notinline.h"
32 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
47 #if CONFIG_UNICODE_SAFETY
48 if ((s[1] ^ 0x80) < 0x40)
51 *puc = ((unsigned int) (c & 0x1f) << 6)
52 | (unsigned int) (s[1] ^ 0x80);
55 #if CONFIG_UNICODE_SAFETY
56 /* invalid multibyte character */
61 /* incomplete multibyte character */
70 #if CONFIG_UNICODE_SAFETY
71 if ((s[1] ^ 0x80) < 0x40)
73 if ((s[2] ^ 0x80) < 0x40)
75 if ((c >= 0xe1 || s[1] >= 0xa0)
76 && (c != 0xed || s[1] < 0xa0))
79 *puc = ((unsigned int) (c & 0x0f) << 12)
80 | ((unsigned int) (s[1] ^ 0x80) << 6)
81 | (unsigned int) (s[2] ^ 0x80);
84 #if CONFIG_UNICODE_SAFETY
85 /* invalid multibyte character */
89 /* invalid multibyte character */
93 /* invalid multibyte character */
98 /* incomplete multibyte character */
100 if (n == 1 || (s[1] ^ 0x80) >= 0x40)
110 #if CONFIG_UNICODE_SAFETY
111 if ((s[1] ^ 0x80) < 0x40)
113 if ((s[2] ^ 0x80) < 0x40)
115 if ((s[3] ^ 0x80) < 0x40)
117 if ((c >= 0xf1 || s[1] >= 0x90)
119 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
124 *puc = ((unsigned int) (c & 0x07) << 18)
125 | ((unsigned int) (s[1] ^ 0x80) << 12)
126 | ((unsigned int) (s[2] ^ 0x80) << 6)
127 | (unsigned int) (s[3] ^ 0x80);
130 #if CONFIG_UNICODE_SAFETY
131 /* invalid multibyte character */
135 /* invalid multibyte character */
139 /* invalid multibyte character */
143 /* invalid multibyte character */
148 /* incomplete multibyte character */
150 if (n == 1 || (s[1] ^ 0x80) >= 0x40)
152 else if (n == 2 || (s[2] ^ 0x80) >= 0x40)
163 #if CONFIG_UNICODE_SAFETY
164 if ((s[1] ^ 0x80) < 0x40)
166 if ((s[2] ^ 0x80) < 0x40)
168 if ((s[3] ^ 0x80) < 0x40)
170 if ((s[4] ^ 0x80) < 0x40)
172 if (c >= 0xf9 || s[1] >= 0x88)
175 *puc = ((unsigned int) (c & 0x03) << 24)
176 | ((unsigned int) (s[1] ^ 0x80) << 18)
177 | ((unsigned int) (s[2] ^ 0x80) << 12)
178 | ((unsigned int) (s[3] ^ 0x80) << 6)
179 | (unsigned int) (s[4] ^ 0x80);
182 #if CONFIG_UNICODE_SAFETY
183 /* invalid multibyte character */
187 /* invalid multibyte character */
191 /* invalid multibyte character */
195 /* invalid multibyte character */
198 /* invalid multibyte character */
203 /* incomplete multibyte character */
212 #if CONFIG_UNICODE_SAFETY
213 if ((s[1] ^ 0x80) < 0x40)
215 if ((s[2] ^ 0x80) < 0x40)
217 if ((s[3] ^ 0x80) < 0x40)
219 if ((s[4] ^ 0x80) < 0x40)
221 if ((s[5] ^ 0x80) < 0x40)
223 if (c >= 0xfd || s[1] >= 0x84)
226 *puc = ((unsigned int) (c & 0x01) << 30)
227 | ((unsigned int) (s[1] ^ 0x80) << 24)
228 | ((unsigned int) (s[2] ^ 0x80) << 18)
229 | ((unsigned int) (s[3] ^ 0x80) << 12)
230 | ((unsigned int) (s[4] ^ 0x80) << 6)
231 | (unsigned int) (s[5] ^ 0x80);
234 #if CONFIG_UNICODE_SAFETY
235 /* invalid multibyte character */
239 /* invalid multibyte character */
243 /* invalid multibyte character */
247 /* invalid multibyte character */
251 /* invalid multibyte character */
254 /* invalid multibyte character */
259 /* incomplete multibyte character */
266 /* invalid multibyte character */