2 Copyright (C) 2010 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
27 * This function implements the syntax given in RFC3629, which is
28 * the same as that given in The Unicode Standard, Version 6.0.
30 * It has the following properties:
32 * * All codepoints U+0000..U+10FFFF may be encoded,
33 * except for U+D800..U+DFFF, which are reserved
34 * for UTF-16 surrogate pair encoding.
35 * * UTF-8 byte sequences longer than 4 bytes are not permitted,
36 * as they exceed the range of Unicode.
37 * * The sixty-six Unicode "non-characters" are permitted
38 * (namely, U+FDD0..U+FDEF, U+xxFFFE, and U+xxFFFF).
40 bool utf8_validate(const char *str, size_t length)
42 const unsigned char *s = (const unsigned char*)str;
43 const unsigned char *e = s + length;
46 unsigned char c = *s++;
50 /* Validate the first byte and determine the sequence length. */
51 if (c <= 0x7F) /* 00..7F */
53 else if (c <= 0xC1) /* 80..C1 */
55 else if (c <= 0xDF) /* C2..DF */
57 else if (c <= 0xEF) /* E0..EF */
59 else if (c <= 0xF4) /* F0..F4 */
64 /* Make sure the character isn't clipped. */
65 if (s + len_minus_two >= e)
70 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
72 if ((*s++ & 0xC0) != 0x80)
74 } while (len_minus_two--);
76 /* Handle special cases. */
79 /* Disallow overlong 3-byte sequence. */
84 /* Disallow U+D800..U+DFFF. */
89 /* Disallow overlong 4-byte sequence. */
94 /* Disallow codepoints beyond U+10FFFF. */
104 int utf8_read_char(const char *s, uchar_t *out)
106 const unsigned char *c = (const unsigned char*) s;
112 } else if (c[0] <= 0xDF) {
113 /* C2..DF (unless input is invalid) */
114 *out = ((uchar_t)c[0] & 0x1F) << 6 |
115 ((uchar_t)c[1] & 0x3F);
117 } else if (c[0] <= 0xEF) {
119 *out = ((uchar_t)c[0] & 0xF) << 12 |
120 ((uchar_t)c[1] & 0x3F) << 6 |
121 ((uchar_t)c[2] & 0x3F);
124 /* F0..F4 (unless input is invalid) */
125 *out = ((uchar_t)c[0] & 0x7) << 18 |
126 ((uchar_t)c[1] & 0x3F) << 12 |
127 ((uchar_t)c[2] & 0x3F) << 6 |
128 ((uchar_t)c[3] & 0x3F);
133 int utf8_write_char(uchar_t unicode, char *out)
135 unsigned char *o = (unsigned char*) out;
137 if (unicode <= 0x7F) {
141 } else if (unicode <= 0x7FF) {
143 *o++ = 0xC0 | unicode >> 6;
144 *o++ = 0x80 | (unicode & 0x3F);
146 } else if (unicode <= 0xFFFF) {
148 if (unicode >= 0xD800 && unicode <= 0xDFFF)
149 unicode = REPLACEMENT_CHARACTER;
150 three_byte_character:
151 *o++ = 0xE0 | unicode >> 12;
152 *o++ = 0x80 | (unicode >> 6 & 0x3F);
153 *o++ = 0x80 | (unicode & 0x3F);
155 } else if (unicode <= 0x10FFFF) {
156 /* U+10000..U+10FFFF */
157 *o++ = 0xF0 | unicode >> 18;
158 *o++ = 0x80 | (unicode >> 12 & 0x3F);
159 *o++ = 0x80 | (unicode >> 6 & 0x3F);
160 *o++ = 0x80 | (unicode & 0x3F);
164 unicode = REPLACEMENT_CHARACTER;
165 goto three_byte_character;
169 uchar_t from_surrogate_pair(unsigned int uc, unsigned int lc)
171 if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF)
172 return 0x10000 + ((((uchar_t)uc & 0x3FF) << 10) | (lc & 0x3FF));
174 return REPLACEMENT_CHARACTER;
177 bool to_surrogate_pair(uchar_t unicode, unsigned int *uc, unsigned int *lc)
179 if (unicode >= 0x10000 && unicode <= 0x10FFFF) {
180 uchar_t n = unicode - 0x10000;
181 *uc = ((n >> 10) & 0x3FF) | 0xD800;
182 *lc = (n & 0x3FF) | 0xDC00;
185 *uc = *lc = REPLACEMENT_CHARACTER;