1 /* Look at first character in UTF-8 string.
2 Copyright (C) 1999-2000, 2002, 2006-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
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/>. */
24 u8_strmbtouc (ucs4_t *puc, const uint8_t *s)
26 /* Keep in sync with unistr.h and utf8-ucs4.c. */
32 return (c != 0 ? 1 : 0);
38 #if CONFIG_UNICODE_SAFETY
39 if ((s[1] ^ 0x80) < 0x40)
44 *puc = ((unsigned int) (c & 0x1f) << 6)
45 | (unsigned int) (s[1] ^ 0x80);
51 #if CONFIG_UNICODE_SAFETY
52 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
53 && (c >= 0xe1 || s[1] >= 0xa0)
54 && (c != 0xed || s[1] < 0xa0))
56 if (s[1] != 0 && s[2] != 0)
59 *puc = ((unsigned int) (c & 0x0f) << 12)
60 | ((unsigned int) (s[1] ^ 0x80) << 6)
61 | (unsigned int) (s[2] ^ 0x80);
67 #if CONFIG_UNICODE_SAFETY
68 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
69 && (s[3] ^ 0x80) < 0x40
70 && (c >= 0xf1 || s[1] >= 0x90)
72 && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
76 if (s[1] != 0 && s[2] != 0 && s[3] != 0)
79 *puc = ((unsigned int) (c & 0x07) << 18)
80 | ((unsigned int) (s[1] ^ 0x80) << 12)
81 | ((unsigned int) (s[2] ^ 0x80) << 6)
82 | (unsigned int) (s[3] ^ 0x80);
89 #if CONFIG_UNICODE_SAFETY
90 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
91 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
92 && (c >= 0xf9 || s[1] >= 0x88))
94 if (s[1] != 0 && s[2] != 0 && s[3] != 0 && s[4] != 0)
97 *puc = ((unsigned int) (c & 0x03) << 24)
98 | ((unsigned int) (s[1] ^ 0x80) << 18)
99 | ((unsigned int) (s[2] ^ 0x80) << 12)
100 | ((unsigned int) (s[3] ^ 0x80) << 6)
101 | (unsigned int) (s[4] ^ 0x80);
107 #if CONFIG_UNICODE_SAFETY
108 if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
109 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
110 && (s[5] ^ 0x80) < 0x40
111 && (c >= 0xfd || s[1] >= 0x84))
113 if (s[1] != 0 && s[2] != 0 && s[3] != 0 && s[4] != 0 && s[5] != 0)
116 *puc = ((unsigned int) (c & 0x01) << 30)
117 | ((unsigned int) (s[1] ^ 0x80) << 24)
118 | ((unsigned int) (s[2] ^ 0x80) << 18)
119 | ((unsigned int) (s[3] ^ 0x80) << 12)
120 | ((unsigned int) (s[4] ^ 0x80) << 6)
121 | (unsigned int) (s[5] ^ 0x80);
127 /* invalid or incomplete multibyte character */