1 /* Look at first character in UTF-8 string.
2 Copyright (C) 1999-2002, 2006 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_safe (ucs4_t *puc, const uint8_t *s, size_t n)
43 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 ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
63 && (c >= 0xe1 || s[1] >= 0xa0)
64 && (c != 0xed || s[1] < 0xa0))
66 *puc = ((unsigned int) (c & 0x0f) << 12)
67 | ((unsigned int) (s[1] ^ 0x80) << 6)
68 | (unsigned int) (s[2] ^ 0x80);
71 /* invalid multibyte character */
75 /* incomplete multibyte character */
84 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
85 && (s[3] ^ 0x80) < 0x40
86 && (c >= 0xf1 || s[1] >= 0x90)
88 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
92 *puc = ((unsigned int) (c & 0x07) << 18)
93 | ((unsigned int) (s[1] ^ 0x80) << 12)
94 | ((unsigned int) (s[2] ^ 0x80) << 6)
95 | (unsigned int) (s[3] ^ 0x80);
98 /* invalid multibyte character */
102 /* incomplete multibyte character */
112 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
113 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
114 && (c >= 0xf9 || s[1] >= 0x88))
116 *puc = ((unsigned int) (c & 0x03) << 24)
117 | ((unsigned int) (s[1] ^ 0x80) << 18)
118 | ((unsigned int) (s[2] ^ 0x80) << 12)
119 | ((unsigned int) (s[3] ^ 0x80) << 6)
120 | (unsigned int) (s[4] ^ 0x80);
123 /* invalid multibyte character */
127 /* incomplete multibyte character */
136 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
137 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
138 && (s[5] ^ 0x80) < 0x40
139 && (c >= 0xfd || s[1] >= 0x84))
141 *puc = ((unsigned int) (c & 0x01) << 30)
142 | ((unsigned int) (s[1] ^ 0x80) << 24)
143 | ((unsigned int) (s[2] ^ 0x80) << 18)
144 | ((unsigned int) (s[3] ^ 0x80) << 12)
145 | ((unsigned int) (s[4] ^ 0x80) << 6)
146 | (unsigned int) (s[5] ^ 0x80);
149 /* invalid multibyte character */
153 /* incomplete multibyte character */
160 /* invalid multibyte character */