1 /* Look at first character in UTF-8 string, returning an error code.
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,
26 u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n)
41 if ((s[1] ^ 0x80) < 0x40)
43 *puc = ((unsigned int) (c & 0x1f) << 6)
44 | (unsigned int) (s[1] ^ 0x80);
47 /* invalid multibyte character */
51 /* incomplete multibyte character */
60 if ((s[1] ^ 0x80) < 0x40
61 && (c >= 0xe1 || s[1] >= 0xa0)
62 && (c != 0xed || s[1] < 0xa0))
66 if ((s[2] ^ 0x80) < 0x40)
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 */
82 /* invalid multibyte character */
86 /* incomplete multibyte character */
95 if ((s[1] ^ 0x80) < 0x40
96 && (c >= 0xf1 || s[1] >= 0x90)
98 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
104 if ((s[2] ^ 0x80) < 0x40)
108 if ((s[3] ^ 0x80) < 0x40)
110 *puc = ((unsigned int) (c & 0x07) << 18)
111 | ((unsigned int) (s[1] ^ 0x80) << 12)
112 | ((unsigned int) (s[2] ^ 0x80) << 6)
113 | (unsigned int) (s[3] ^ 0x80);
116 /* invalid multibyte character */
120 /* incomplete multibyte character */
125 /* invalid multibyte character */
129 /* incomplete multibyte character */
134 /* invalid multibyte character */
138 /* incomplete multibyte character */
148 if ((s[1] ^ 0x80) < 0x40
149 && (c >= 0xf9 || s[1] >= 0x88))
153 if ((s[2] ^ 0x80) < 0x40)
157 if ((s[3] ^ 0x80) < 0x40)
161 if ((s[4] ^ 0x80) < 0x40)
163 *puc = ((unsigned int) (c & 0x03) << 24)
164 | ((unsigned int) (s[1] ^ 0x80) << 18)
165 | ((unsigned int) (s[2] ^ 0x80) << 12)
166 | ((unsigned int) (s[3] ^ 0x80) << 6)
167 | (unsigned int) (s[4] ^ 0x80);
170 /* invalid multibyte character */
174 /* incomplete multibyte character */
179 /* invalid multibyte character */
183 /* incomplete multibyte character */
188 /* invalid multibyte character */
192 /* incomplete multibyte character */
197 /* invalid multibyte character */
201 /* incomplete multibyte character */
210 if ((s[1] ^ 0x80) < 0x40
211 && (c >= 0xfd || s[1] >= 0x84))
215 if ((s[2] ^ 0x80) < 0x40)
219 if ((s[3] ^ 0x80) < 0x40)
223 if ((s[4] ^ 0x80) < 0x40)
227 if ((s[5] ^ 0x80) < 0x40)
229 *puc = ((unsigned int) (c & 0x01) << 30)
230 | ((unsigned int) (s[1] ^ 0x80) << 24)
231 | ((unsigned int) (s[2] ^ 0x80) << 18)
232 | ((unsigned int) (s[3] ^ 0x80) << 12)
233 | ((unsigned int) (s[4] ^ 0x80) << 6)
234 | (unsigned int) (s[5] ^ 0x80);
237 /* invalid multibyte character */
241 /* incomplete multibyte character */
246 /* invalid multibyte character */
250 /* incomplete multibyte character */
255 /* invalid multibyte character */
259 /* incomplete multibyte character */
264 /* invalid multibyte character */
268 /* incomplete multibyte character */
273 /* invalid multibyte character */
277 /* incomplete multibyte character */
284 /* invalid multibyte character */