1 /* Conversion UTF-8 to UCS-4.
2 Copyright (C) 2001-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/>. */
23 #if defined IN_LIBUNISTRING || HAVE_INLINE
26 u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n)
36 if ((s[1] ^ 0x80) < 0x40)
38 *puc = ((unsigned int) (c & 0x1f) << 6)
39 | (unsigned int) (s[1] ^ 0x80);
42 /* invalid multibyte character */
46 /* incomplete multibyte character */
55 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
56 && (c >= 0xe1 || s[1] >= 0xa0)
57 && (c != 0xed || s[1] < 0xa0))
59 *puc = ((unsigned int) (c & 0x0f) << 12)
60 | ((unsigned int) (s[1] ^ 0x80) << 6)
61 | (unsigned int) (s[2] ^ 0x80);
64 /* invalid multibyte character */
68 /* incomplete multibyte character */
77 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
78 && (s[3] ^ 0x80) < 0x40
79 && (c >= 0xf1 || s[1] >= 0x90)
81 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
85 *puc = ((unsigned int) (c & 0x07) << 18)
86 | ((unsigned int) (s[1] ^ 0x80) << 12)
87 | ((unsigned int) (s[2] ^ 0x80) << 6)
88 | (unsigned int) (s[3] ^ 0x80);
91 /* invalid multibyte character */
95 /* incomplete multibyte character */
105 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
106 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
107 && (c >= 0xf9 || s[1] >= 0x88))
109 *puc = ((unsigned int) (c & 0x03) << 24)
110 | ((unsigned int) (s[1] ^ 0x80) << 18)
111 | ((unsigned int) (s[2] ^ 0x80) << 12)
112 | ((unsigned int) (s[3] ^ 0x80) << 6)
113 | (unsigned int) (s[4] ^ 0x80);
116 /* invalid multibyte character */
120 /* incomplete multibyte character */
129 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
130 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
131 && (s[5] ^ 0x80) < 0x40
132 && (c >= 0xfd || s[1] >= 0x84))
134 *puc = ((unsigned int) (c & 0x01) << 30)
135 | ((unsigned int) (s[1] ^ 0x80) << 24)
136 | ((unsigned int) (s[2] ^ 0x80) << 18)
137 | ((unsigned int) (s[3] ^ 0x80) << 12)
138 | ((unsigned int) (s[4] ^ 0x80) << 6)
139 | (unsigned int) (s[5] ^ 0x80);
142 /* invalid multibyte character */
146 /* incomplete multibyte character */
153 /* invalid multibyte character */