1 /* Look at first character in UTF-8 string.
2 Copyright (C) 1999-2002, 2006-2007, 2009 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 /* invalid multibyte character */
59 /* incomplete multibyte character */
68 #if CONFIG_UNICODE_SAFETY
69 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
70 && (c >= 0xe1 || s[1] >= 0xa0)
71 && (c != 0xed || s[1] < 0xa0))
74 *puc = ((unsigned int) (c & 0x0f) << 12)
75 | ((unsigned int) (s[1] ^ 0x80) << 6)
76 | (unsigned int) (s[2] ^ 0x80);
79 /* invalid multibyte character */
83 /* incomplete multibyte character */
92 #if CONFIG_UNICODE_SAFETY
93 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
94 && (s[3] ^ 0x80) < 0x40
95 && (c >= 0xf1 || s[1] >= 0x90)
97 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
102 *puc = ((unsigned int) (c & 0x07) << 18)
103 | ((unsigned int) (s[1] ^ 0x80) << 12)
104 | ((unsigned int) (s[2] ^ 0x80) << 6)
105 | (unsigned int) (s[3] ^ 0x80);
108 /* invalid multibyte character */
112 /* incomplete multibyte character */
122 #if CONFIG_UNICODE_SAFETY
123 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
124 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
125 && (c >= 0xf9 || s[1] >= 0x88))
128 *puc = ((unsigned int) (c & 0x03) << 24)
129 | ((unsigned int) (s[1] ^ 0x80) << 18)
130 | ((unsigned int) (s[2] ^ 0x80) << 12)
131 | ((unsigned int) (s[3] ^ 0x80) << 6)
132 | (unsigned int) (s[4] ^ 0x80);
135 /* invalid multibyte character */
139 /* incomplete multibyte character */
148 #if CONFIG_UNICODE_SAFETY
149 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
150 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
151 && (s[5] ^ 0x80) < 0x40
152 && (c >= 0xfd || s[1] >= 0x84))
155 *puc = ((unsigned int) (c & 0x01) << 30)
156 | ((unsigned int) (s[1] ^ 0x80) << 24)
157 | ((unsigned int) (s[2] ^ 0x80) << 18)
158 | ((unsigned int) (s[3] ^ 0x80) << 12)
159 | ((unsigned int) (s[4] ^ 0x80) << 6)
160 | (unsigned int) (s[5] ^ 0x80);
163 /* invalid multibyte character */
167 /* incomplete multibyte character */
174 /* invalid multibyte character */