1 /* Conversion UTF-8 to UCS-4.
2 Copyright (C) 2001-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_aux (ucs4_t *puc, const uint8_t *s, size_t n)
38 if ((s[1] ^ 0x80) < 0x40)
40 *puc = ((unsigned int) (c & 0x1f) << 6)
41 | (unsigned int) (s[1] ^ 0x80);
44 /* invalid multibyte character */
48 /* incomplete multibyte character */
57 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
58 && (c >= 0xe1 || s[1] >= 0xa0)
59 && (c != 0xed || s[1] < 0xa0))
61 *puc = ((unsigned int) (c & 0x0f) << 12)
62 | ((unsigned int) (s[1] ^ 0x80) << 6)
63 | (unsigned int) (s[2] ^ 0x80);
66 /* invalid multibyte character */
70 /* incomplete multibyte character */
79 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
80 && (s[3] ^ 0x80) < 0x40
81 && (c >= 0xf1 || s[1] >= 0x90)
83 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
87 *puc = ((unsigned int) (c & 0x07) << 18)
88 | ((unsigned int) (s[1] ^ 0x80) << 12)
89 | ((unsigned int) (s[2] ^ 0x80) << 6)
90 | (unsigned int) (s[3] ^ 0x80);
93 /* invalid multibyte character */
97 /* incomplete multibyte character */
107 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
108 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
109 && (c >= 0xf9 || s[1] >= 0x88))
111 *puc = ((unsigned int) (c & 0x03) << 24)
112 | ((unsigned int) (s[1] ^ 0x80) << 18)
113 | ((unsigned int) (s[2] ^ 0x80) << 12)
114 | ((unsigned int) (s[3] ^ 0x80) << 6)
115 | (unsigned int) (s[4] ^ 0x80);
118 /* invalid multibyte character */
122 /* incomplete multibyte character */
131 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
132 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
133 && (s[5] ^ 0x80) < 0x40
134 && (c >= 0xfd || s[1] >= 0x84))
136 *puc = ((unsigned int) (c & 0x01) << 30)
137 | ((unsigned int) (s[1] ^ 0x80) << 24)
138 | ((unsigned int) (s[2] ^ 0x80) << 18)
139 | ((unsigned int) (s[3] ^ 0x80) << 12)
140 | ((unsigned int) (s[4] ^ 0x80) << 6)
141 | (unsigned int) (s[5] ^ 0x80);
144 /* invalid multibyte character */
148 /* incomplete multibyte character */
155 /* invalid multibyte character */