]> git.ozlabs.org Git - ccan/blob - ccan/charset/charset.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / charset / charset.c
1 /*
2   Copyright (C) 2011 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 #include <assert.h>
26
27
28 bool utf8_validate(const char *str, size_t length)
29 {
30         const char *s = str;
31         const char *e = str + length;
32         int len;
33         
34         for (; s < e; s += len) {
35                 len = utf8_validate_char(s, e);
36                 if (len == 0)
37                         return false;
38         }
39         assert(s == e);
40         
41         return true;
42 }
43
44 /*
45  * This function implements the syntax given in RFC3629, which is
46  * the same as that given in The Unicode Standard, Version 6.0.
47  *
48  * It has the following properties:
49  *
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).
57  */
58 int utf8_validate_char(const char *s, const char *e)
59 {
60         unsigned char c = *s++;
61         
62         if (c <= 0x7F) {        /* 00..7F */
63                 return 1;
64         } else if (c <= 0xC1) { /* 80..C1 */
65                 /* Disallow overlong 2-byte sequence. */
66                 return 0;
67         } else if (c <= 0xDF) { /* C2..DF */
68                 /* Make sure the character isn't clipped. */
69                 if (e - s < 1)
70                         return 0;
71                 
72                 /* Make sure subsequent byte is in the range 0x80..0xBF. */
73                 if (((unsigned char)*s++ & 0xC0) != 0x80)
74                         return 0;
75                 
76                 return 2;
77         } else if (c <= 0xEF) { /* E0..EF */
78                 /* Make sure the character isn't clipped. */
79                 if (e - s < 2)
80                         return 0;
81                 
82                 /* Disallow overlong 3-byte sequence. */
83                 if (c == 0xE0 && (unsigned char)*s < 0xA0)
84                         return 0;
85                 
86                 /* Disallow U+D800..U+DFFF. */
87                 if (c == 0xED && (unsigned char)*s > 0x9F)
88                         return 0;
89                 
90                 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
91                 if (((unsigned char)*s++ & 0xC0) != 0x80)
92                         return 0;
93                 if (((unsigned char)*s++ & 0xC0) != 0x80)
94                         return 0;
95                 
96                 return 3;
97         } else if (c <= 0xF4) { /* F0..F4 */
98                 /* Make sure the character isn't clipped. */
99                 if (e - s < 3)
100                         return 0;
101                 
102                 /* Disallow overlong 4-byte sequence. */
103                 if (c == 0xF0 && (unsigned char)*s < 0x90)
104                         return 0;
105                 
106                 /* Disallow codepoints beyond U+10FFFF. */
107                 if (c == 0xF4 && (unsigned char)*s > 0x8F)
108                         return 0;
109                 
110                 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
111                 if (((unsigned char)*s++ & 0xC0) != 0x80)
112                         return 0;
113                 if (((unsigned char)*s++ & 0xC0) != 0x80)
114                         return 0;
115                 if (((unsigned char)*s++ & 0xC0) != 0x80)
116                         return 0;
117                 
118                 return 4;
119         } else {                /* F5..FF */
120                 return 0;
121         }
122 }
123
124 int utf8_read_char(const char *s, uchar_t *out)
125 {
126         const unsigned char *c = (const unsigned char*) s;
127
128         if (c[0] <= 0x7F) {
129                 /* 00..7F */
130                 *out = c[0];
131                 return 1;
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);
136                 return 2;
137         } else if (c[0] <= 0xEF) {
138                 /* E0..EF */
139                 *out = ((uchar_t)c[0] &  0xF) << 12 |
140                        ((uchar_t)c[1] & 0x3F) << 6  |
141                        ((uchar_t)c[2] & 0x3F);
142                 return 3;
143         } else {
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);
149                 return 4;
150         }
151 }
152
153 int utf8_write_char(uchar_t unicode, char *out)
154 {
155         unsigned char *o = (unsigned char*) out;
156
157         if (unicode <= 0x7F) {
158                 /* U+0000..U+007F */
159                 *o++ = unicode;
160                 return 1;
161         } else if (unicode <= 0x7FF) {
162                 /* U+0080..U+07FF */
163                 *o++ = 0xC0 | unicode >> 6;
164                 *o++ = 0x80 | (unicode & 0x3F);
165                 return 2;
166         } else if (unicode <= 0xFFFF) {
167                 /* U+0800..U+FFFF */
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);
174                 return 3;
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);
181                 return 4;
182         } else {
183                 /* U+110000... */
184                 unicode = REPLACEMENT_CHARACTER;
185                 goto three_byte_character;
186         }
187 }
188
189 uchar_t from_surrogate_pair(unsigned int uc, unsigned int lc)
190 {
191         if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF)
192                 return 0x10000 + ((((uchar_t)uc & 0x3FF) << 10) | (lc & 0x3FF));
193         else
194                 return REPLACEMENT_CHARACTER;
195 }
196
197 bool to_surrogate_pair(uchar_t unicode, unsigned int *uc, unsigned int *lc)
198 {
199         if (unicode >= 0x10000 && unicode <= 0x10FFFF) {
200                 uchar_t n = unicode - 0x10000;
201                 *uc = ((n >> 10) & 0x3FF) | 0xD800;
202                 *lc = (n & 0x3FF) | 0xDC00;
203                 return true;
204         } else {
205                 *uc = *lc = REPLACEMENT_CHARACTER;
206                 return false;
207         }
208 }