2 Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com)
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 bool utf8_validate(const char *str, size_t length)
31 const char *e = str + length;
34 for (; s < e; s += len) {
35 len = utf8_validate_char(s, e);
45 * This function implements the syntax given in RFC3629, which is
46 * the same as that given in The Unicode Standard, Version 6.0.
48 * It has the following properties:
50 * * All codepoints U+0000..U+10FFFF may be encoded,
51 * except for U+D800..U+DFFF, which are reserved
52 * for UTF-16 surrogate pair encoding.
53 * * UTF-8 byte sequences longer than 4 bytes are not permitted,
54 * as they exceed the range of Unicode.
55 * * The sixty-six Unicode "non-characters" are permitted
56 * (namely, U+FDD0..U+FDEF, U+xxFFFE, and U+xxFFFF).
58 int utf8_validate_char(const char *s, const char *e)
60 unsigned char c = *s++;
62 if (c <= 0x7F) { /* 00..7F */
64 } else if (c <= 0xC1) { /* 80..C1 */
65 /* Disallow overlong 2-byte sequence. */
67 } else if (c <= 0xDF) { /* C2..DF */
68 /* Make sure the character isn't clipped. */
72 /* Make sure subsequent byte is in the range 0x80..0xBF. */
73 if (((unsigned char)*s++ & 0xC0) != 0x80)
77 } else if (c <= 0xEF) { /* E0..EF */
78 /* Make sure the character isn't clipped. */
82 /* Disallow overlong 3-byte sequence. */
83 if (c == 0xE0 && (unsigned char)*s < 0xA0)
86 /* Disallow U+D800..U+DFFF. */
87 if (c == 0xED && (unsigned char)*s > 0x9F)
90 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
91 if (((unsigned char)*s++ & 0xC0) != 0x80)
93 if (((unsigned char)*s++ & 0xC0) != 0x80)
97 } else if (c <= 0xF4) { /* F0..F4 */
98 /* Make sure the character isn't clipped. */
102 /* Disallow overlong 4-byte sequence. */
103 if (c == 0xF0 && (unsigned char)*s < 0x90)
106 /* Disallow codepoints beyond U+10FFFF. */
107 if (c == 0xF4 && (unsigned char)*s > 0x8F)
110 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
111 if (((unsigned char)*s++ & 0xC0) != 0x80)
113 if (((unsigned char)*s++ & 0xC0) != 0x80)
115 if (((unsigned char)*s++ & 0xC0) != 0x80)
119 } else { /* F5..FF */
124 int utf8_read_char(const char *s, uchar_t *out)
126 const unsigned char *c = (const unsigned char*) s;
132 } else if (c[0] <= 0xDF) {
133 /* C2..DF (unless input is invalid) */
134 *out = ((uchar_t)c[0] & 0x1F) << 6 |
135 ((uchar_t)c[1] & 0x3F);
137 } else if (c[0] <= 0xEF) {
139 *out = ((uchar_t)c[0] & 0xF) << 12 |
140 ((uchar_t)c[1] & 0x3F) << 6 |
141 ((uchar_t)c[2] & 0x3F);
144 /* F0..F4 (unless input is invalid) */
145 *out = ((uchar_t)c[0] & 0x7) << 18 |
146 ((uchar_t)c[1] & 0x3F) << 12 |
147 ((uchar_t)c[2] & 0x3F) << 6 |
148 ((uchar_t)c[3] & 0x3F);
153 int utf8_write_char(uchar_t unicode, char *out)
155 unsigned char *o = (unsigned char*) out;
157 if (unicode <= 0x7F) {
161 } else if (unicode <= 0x7FF) {
163 *o++ = 0xC0 | unicode >> 6;
164 *o++ = 0x80 | (unicode & 0x3F);
166 } else if (unicode <= 0xFFFF) {
168 if (unicode >= 0xD800 && unicode <= 0xDFFF)
169 unicode = REPLACEMENT_CHARACTER;
170 three_byte_character:
171 *o++ = 0xE0 | unicode >> 12;
172 *o++ = 0x80 | (unicode >> 6 & 0x3F);
173 *o++ = 0x80 | (unicode & 0x3F);
175 } else if (unicode <= 0x10FFFF) {
176 /* U+10000..U+10FFFF */
177 *o++ = 0xF0 | unicode >> 18;
178 *o++ = 0x80 | (unicode >> 12 & 0x3F);
179 *o++ = 0x80 | (unicode >> 6 & 0x3F);
180 *o++ = 0x80 | (unicode & 0x3F);
184 unicode = REPLACEMENT_CHARACTER;
185 goto three_byte_character;
189 uchar_t from_surrogate_pair(unsigned int uc, unsigned int lc)
191 if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF)
192 return 0x10000 + ((((uchar_t)uc & 0x3FF) << 10) | (lc & 0x3FF));
194 return REPLACEMENT_CHARACTER;
197 bool to_surrogate_pair(uchar_t unicode, unsigned int *uc, unsigned int *lc)
199 if (unicode >= 0x10000 && unicode <= 0x10FFFF) {
200 uchar_t n = unicode - 0x10000;
201 *uc = ((n >> 10) & 0x3FF) | 0xD800;
202 *lc = (n & 0x3FF) | 0xDC00;
205 *uc = *lc = REPLACEMENT_CHARACTER;