charset: Rewrote utf8_validate, and added four new functions:
[ccan] / ccan / charset / charset.c
1 /*
2   Copyright (C) 2010 Joseph A. Adams (joeyadams3.14159@gmail.com)
3   All rights reserved.
4
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:
11
12   The above copyright notice and this permission notice shall be included in
13   all copies or substantial portions of the Software.
14
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
21   THE SOFTWARE.
22 */
23
24 #include "charset.h"
25
26 /*
27  * This function implements the syntax given in RFC3629, which is
28  * the same as that given in The Unicode Standard, Version 6.0.
29  *
30  * It has the following properties:
31  *
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).
39  */
40 bool utf8_validate(const char *str, size_t length)
41 {
42         const unsigned char *s = (const unsigned char*)str;
43         const unsigned char *e = s + length;
44         
45         while (s < e) {
46                 unsigned char c = *s++;
47                 unsigned char c2;
48                 int len_minus_two;
49                 
50                 /* Validate the first byte and determine the sequence length. */
51                 if (c <= 0x7F)          /* 00..7F */
52                         continue;
53                 else if (c <= 0xC1)     /* 80..C1 */
54                         return false;
55                 else if (c <= 0xDF)     /* C2..DF */
56                         len_minus_two = 0;
57                 else if (c <= 0xEF)     /* E0..EF */
58                         len_minus_two = 1;
59                 else if (c <= 0xF4)     /* F0..F4 */
60                         len_minus_two = 2;
61                 else
62                         return false;
63                 
64                 /* Make sure the character isn't clipped. */
65                 if (s + len_minus_two >= e)
66                         return false;
67                 
68                 c2 = *s;
69                 
70                 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
71                 do {
72                         if ((*s++ & 0xC0) != 0x80)
73                                 return false;
74                 } while (len_minus_two--);
75                 
76                 /* Handle special cases. */
77                 switch (c) {
78                         case 0xE0:
79                                 /* Disallow overlong 3-byte sequence. */
80                                 if (c2 < 0xA0)
81                                         return false;
82                                 break;
83                         case 0xED:
84                                 /* Disallow U+D800..U+DFFF. */
85                                 if (c2 > 0x9F)
86                                         return false;
87                                 break;
88                         case 0xF0:
89                                 /* Disallow overlong 4-byte sequence. */
90                                 if (c2 < 0x90)
91                                         return false;
92                                 break;
93                         case 0xF4:
94                                 /* Disallow codepoints beyond U+10FFFF. */
95                                 if (c2 > 0x8F)
96                                         return false;
97                                 break;
98                 }
99         }
100         
101         return true;
102 }
103
104 int utf8_read_char(const char *s, uchar_t *out)
105 {
106         const unsigned char *c = (const unsigned char*) s;
107
108         if (c[0] <= 0x7F) {
109                 /* 00..7F */
110                 *out = c[0];
111                 return 1;
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);
116                 return 2;
117         } else if (c[0] <= 0xEF) {
118                 /* E0..EF */
119                 *out = ((uchar_t)c[0] &  0xF) << 12 |
120                        ((uchar_t)c[1] & 0x3F) << 6  |
121                        ((uchar_t)c[2] & 0x3F);
122                 return 3;
123         } else {
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);
129                 return 4;
130         }
131 }
132
133 int utf8_write_char(uchar_t unicode, char *out)
134 {
135         unsigned char *o = (unsigned char*) out;
136
137         if (unicode <= 0x7F) {
138                 /* U+0000..U+007F */
139                 *o++ = unicode;
140                 return 1;
141         } else if (unicode <= 0x7FF) {
142                 /* U+0080..U+07FF */
143                 *o++ = 0xC0 | unicode >> 6;
144                 *o++ = 0x80 | (unicode & 0x3F);
145                 return 2;
146         } else if (unicode <= 0xFFFF) {
147                 /* U+0800..U+FFFF */
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);
154                 return 3;
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);
161                 return 4;
162         } else {
163                 /* U+110000... */
164                 unicode = REPLACEMENT_CHARACTER;
165                 goto three_byte_character;
166         }
167 }
168
169 uchar_t from_surrogate_pair(unsigned int uc, unsigned int lc)
170 {
171         if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF)
172                 return 0x10000 + ((((uchar_t)uc & 0x3FF) << 10) | (lc & 0x3FF));
173         else
174                 return REPLACEMENT_CHARACTER;
175 }
176
177 bool to_surrogate_pair(uchar_t unicode, unsigned int *uc, unsigned int *lc)
178 {
179         if (unicode >= 0x10000 && unicode <= 0x10FFFF) {
180                 uchar_t n = unicode - 0x10000;
181                 *uc = ((n >> 10) & 0x3FF) | 0xD800;
182                 *lc = (n & 0x3FF) | 0xDC00;
183                 return true;
184         } else {
185                 *uc = *lc = REPLACEMENT_CHARACTER;
186                 return false;
187         }
188 }