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 Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n)
43 #if CONFIG_UNICODE_SAFETY
44 if ((s[1] ^ 0x80) < 0x40)
47 *puc = ((unsigned int) (c & 0x1f) << 6)
48 | (unsigned int) (s[1] ^ 0x80);
51 /* invalid multibyte character */
55 /* incomplete multibyte character */
64 #if CONFIG_UNICODE_SAFETY
65 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
66 && (c >= 0xe1 || s[1] >= 0xa0)
67 && (c != 0xed || s[1] < 0xa0))
70 *puc = ((unsigned int) (c & 0x0f) << 12)
71 | ((unsigned int) (s[1] ^ 0x80) << 6)
72 | (unsigned int) (s[2] ^ 0x80);
75 /* invalid multibyte character */
79 /* incomplete multibyte character */
88 #if CONFIG_UNICODE_SAFETY
89 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
90 && (s[3] ^ 0x80) < 0x40
91 && (c >= 0xf1 || s[1] >= 0x90)
93 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
98 *puc = ((unsigned int) (c & 0x07) << 18)
99 | ((unsigned int) (s[1] ^ 0x80) << 12)
100 | ((unsigned int) (s[2] ^ 0x80) << 6)
101 | (unsigned int) (s[3] ^ 0x80);
104 /* invalid multibyte character */
108 /* incomplete multibyte character */
118 #if CONFIG_UNICODE_SAFETY
119 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
120 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
121 && (c >= 0xf9 || s[1] >= 0x88))
124 *puc = ((unsigned int) (c & 0x03) << 24)
125 | ((unsigned int) (s[1] ^ 0x80) << 18)
126 | ((unsigned int) (s[2] ^ 0x80) << 12)
127 | ((unsigned int) (s[3] ^ 0x80) << 6)
128 | (unsigned int) (s[4] ^ 0x80);
131 /* invalid multibyte character */
135 /* incomplete multibyte character */
144 #if CONFIG_UNICODE_SAFETY
145 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
146 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
147 && (s[5] ^ 0x80) < 0x40
148 && (c >= 0xfd || s[1] >= 0x84))
151 *puc = ((unsigned int) (c & 0x01) << 30)
152 | ((unsigned int) (s[1] ^ 0x80) << 24)
153 | ((unsigned int) (s[2] ^ 0x80) << 18)
154 | ((unsigned int) (s[3] ^ 0x80) << 12)
155 | ((unsigned int) (s[4] ^ 0x80) << 6)
156 | (unsigned int) (s[5] ^ 0x80);
159 /* invalid multibyte character */
163 /* incomplete multibyte character */
170 /* invalid multibyte character */