1 /* Look at first character in UTF-8 string.
2 Copyright (C) 1999-2002, 2006-2007 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/>. */
26 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
41 #if CONFIG_UNICODE_SAFETY
42 if ((s[1] ^ 0x80) < 0x40)
45 *puc = ((unsigned int) (c & 0x1f) << 6)
46 | (unsigned int) (s[1] ^ 0x80);
49 /* invalid multibyte character */
53 /* incomplete multibyte character */
62 #if CONFIG_UNICODE_SAFETY
63 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
64 && (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 /* invalid multibyte character */
77 /* incomplete multibyte character */
86 #if CONFIG_UNICODE_SAFETY
87 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
88 && (s[3] ^ 0x80) < 0x40
89 && (c >= 0xf1 || s[1] >= 0x90)
91 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
96 *puc = ((unsigned int) (c & 0x07) << 18)
97 | ((unsigned int) (s[1] ^ 0x80) << 12)
98 | ((unsigned int) (s[2] ^ 0x80) << 6)
99 | (unsigned int) (s[3] ^ 0x80);
102 /* invalid multibyte character */
106 /* incomplete multibyte character */
116 #if CONFIG_UNICODE_SAFETY
117 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
118 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
119 && (c >= 0xf9 || s[1] >= 0x88))
122 *puc = ((unsigned int) (c & 0x03) << 24)
123 | ((unsigned int) (s[1] ^ 0x80) << 18)
124 | ((unsigned int) (s[2] ^ 0x80) << 12)
125 | ((unsigned int) (s[3] ^ 0x80) << 6)
126 | (unsigned int) (s[4] ^ 0x80);
129 /* invalid multibyte character */
133 /* incomplete multibyte character */
142 #if CONFIG_UNICODE_SAFETY
143 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
144 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
145 && (s[5] ^ 0x80) < 0x40
146 && (c >= 0xfd || s[1] >= 0x84))
149 *puc = ((unsigned int) (c & 0x01) << 30)
150 | ((unsigned int) (s[1] ^ 0x80) << 24)
151 | ((unsigned int) (s[2] ^ 0x80) << 18)
152 | ((unsigned int) (s[3] ^ 0x80) << 12)
153 | ((unsigned int) (s[4] ^ 0x80) << 6)
154 | (unsigned int) (s[5] ^ 0x80);
157 /* invalid multibyte character */
161 /* incomplete multibyte character */
168 /* invalid multibyte character */