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 (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 && (s[2] ^ 0x80) < 0x40
61 && (c >= 0xe1 || s[1] >= 0xa0)
62 && (c != 0xed || s[1] < 0xa0))
64 *puc = ((unsigned int) (c & 0x0f) << 12)
65 | ((unsigned int) (s[1] ^ 0x80) << 6)
66 | (unsigned int) (s[2] ^ 0x80);
69 /* invalid multibyte character */
73 /* incomplete multibyte character */
82 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
83 && (s[3] ^ 0x80) < 0x40
84 && (c >= 0xf1 || s[1] >= 0x90)
86 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
90 *puc = ((unsigned int) (c & 0x07) << 18)
91 | ((unsigned int) (s[1] ^ 0x80) << 12)
92 | ((unsigned int) (s[2] ^ 0x80) << 6)
93 | (unsigned int) (s[3] ^ 0x80);
96 /* invalid multibyte character */
100 /* incomplete multibyte character */
110 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
111 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
112 && (c >= 0xf9 || s[1] >= 0x88))
114 *puc = ((unsigned int) (c & 0x03) << 24)
115 | ((unsigned int) (s[1] ^ 0x80) << 18)
116 | ((unsigned int) (s[2] ^ 0x80) << 12)
117 | ((unsigned int) (s[3] ^ 0x80) << 6)
118 | (unsigned int) (s[4] ^ 0x80);
121 /* invalid multibyte character */
125 /* incomplete multibyte character */
134 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
135 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
136 && (s[5] ^ 0x80) < 0x40
137 && (c >= 0xfd || s[1] >= 0x84))
139 *puc = ((unsigned int) (c & 0x01) << 30)
140 | ((unsigned int) (s[1] ^ 0x80) << 24)
141 | ((unsigned int) (s[2] ^ 0x80) << 18)
142 | ((unsigned int) (s[3] ^ 0x80) << 12)
143 | ((unsigned int) (s[4] ^ 0x80) << 6)
144 | (unsigned int) (s[5] ^ 0x80);
147 /* invalid multibyte character */
151 /* incomplete multibyte character */
158 /* invalid multibyte character */