]> mj.ucw.cz Git - libucw.git/blob - ucw/unicode.h
Unicode: Reject denormalized UTF-8 sequences
[libucw.git] / ucw / unicode.h
1 /*
2  *      UCW Library -- Unicode Characters
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
7  *
8  *      This software may be freely distributed and used according to the terms
9  *      of the GNU Lesser General Public License.
10  */
11
12 #ifndef _UCW_UNICODE_H
13 #define _UCW_UNICODE_H
14
15 #include <ucw/unaligned.h>
16
17 #ifdef CONFIG_UCW_CLEAN_ABI
18 #define utf8_strlen ucw_utf8_strlen
19 #define utf8_strnlen ucw_utf8_strnlen
20 #endif
21
22 /* Macros for handling UTF-8 */
23
24 #define UNI_REPLACEMENT 0xfffc  /** Unicode value used as a default replacement of invalid characters. **/
25
26 /**
27  * Encode a value from the range `[0, 0xFFFF]`
28  * (basic multilingual plane); up to 3 bytes needed (RFC2279).
29  **/
30 static inline byte *utf8_put(byte *p, uint u)
31 {
32   if (u < 0x80)
33     *p++ = u;
34   else if (u < 0x800)
35     {
36       *p++ = 0xc0 | (u >> 6);
37       *p++ = 0x80 | (u & 0x3f);
38     }
39   else
40     {
41       ASSERT(u < 0x10000);
42       *p++ = 0xe0 | (u >> 12);
43       *p++ = 0x80 | ((u >> 6) & 0x3f);
44       *p++ = 0x80 | (u & 0x3f);
45     }
46   return p;
47 }
48
49 /**
50  * Encode a value from the range `[0, 0x7FFFFFFF]`;
51  * (superset of Unicode 4.0) up to 6 bytes needed (RFC2279).
52  **/
53 static inline byte *utf8_32_put(byte *p, uint u)
54 {
55   if (u < 0x80)
56     *p++ = u;
57   else if (u < 0x800)
58     {
59       *p++ = 0xc0 | (u >> 6);
60       goto put1;
61     }
62   else if (u < (1<<16))
63     {
64       *p++ = 0xe0 | (u >> 12);
65       goto put2;
66     }
67   else if (u < (1<<21))
68     {
69       *p++ = 0xf0 | (u >> 18);
70       goto put3;
71     }
72   else if (u < (1<<26))
73     {
74       *p++ = 0xf8 | (u >> 24);
75       goto put4;
76     }
77   else if (u < (1U<<31))
78     {
79       *p++ = 0xfc | (u >> 30);
80       *p++ = 0x80 | ((u >> 24) & 0x3f);
81 put4: *p++ = 0x80 | ((u >> 18) & 0x3f);
82 put3: *p++ = 0x80 | ((u >> 12) & 0x3f);
83 put2: *p++ = 0x80 | ((u >> 6) & 0x3f);
84 put1: *p++ = 0x80 | (u & 0x3f);
85     }
86   else
87     ASSERT(0);
88   return p;
89 }
90
91 #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f)
92 #define UTF8_CHECK_RANGE(r) if (unlikely(u < r)) goto bad
93
94 /**
95  * Decode a value from the range `[0, 0xFFFF]` (basic multilingual plane)
96  * or return @repl if the encoding has been corrupted.
97  **/
98 static inline byte *utf8_get_repl(const byte *p, uint *uu, uint repl)
99 {
100   uint u = *p++;
101   if (u < 0x80)
102     ;
103   else if (unlikely(u < 0xc0))
104     {
105       /* Incorrect byte sequence */
106     bad:
107       u = repl;
108     }
109   else if (u < 0xe0)
110     {
111       u &= 0x1f;
112       UTF8_GET_NEXT;
113       UTF8_CHECK_RANGE(0x80);
114     }
115   else if (likely(u < 0xf0))
116     {
117       u &= 0x0f;
118       UTF8_GET_NEXT;
119       UTF8_GET_NEXT;
120       UTF8_CHECK_RANGE(0x800);
121     }
122   else
123     goto bad;
124   *uu = u;
125   return (byte *)p;
126 }
127
128 /**
129  * Decode a value from the range `[0, 0x7FFFFFFF]`
130  * or return @repl if the encoding has been corrupted.
131  **/
132 static inline byte *utf8_32_get_repl(const byte *p, uint *uu, uint repl)
133 {
134   uint u = *p++;
135   uint limit;
136   if (u < 0x80)
137     ;
138   else if (unlikely(u < 0xc0))
139     goto bad;
140   else if (u < 0xe0)
141     {
142       u &= 0x1f;
143       limit = 0x80;
144       goto get1;
145     }
146   else if (u < 0xf0)
147     {
148       u &= 0x0f;
149       limit = 0x800;
150       goto get2;
151     }
152   else if (u < 0xf8)
153     {
154       u &= 0x07;
155       limit = 1 << 16;
156       goto get3;
157     }
158   else if (u < 0xfc)
159     {
160       u &= 0x03;
161       limit = 1 << 21;
162       goto get4;
163     }
164   else if (u < 0xfe)
165     {
166       u &= 0x01;
167       limit = 1 << 26;
168       UTF8_GET_NEXT;
169 get4: UTF8_GET_NEXT;
170 get3: UTF8_GET_NEXT;
171 get2: UTF8_GET_NEXT;
172 get1: UTF8_GET_NEXT;
173       if (unlikely(u < limit))
174         goto bad;
175     }
176   else
177     goto bad;
178   *uu = u;
179   return (byte *)p;
180
181 bad:
182   /* Incorrect byte sequence */
183   *uu = repl;
184   return (byte *)p;
185 }
186
187 /**
188  * Decode a value from the range `[0, 0xFFFF]` (basic multilingual plane)
189  * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
190  **/
191 static inline byte *utf8_get(const byte *p, uint *uu)
192 {
193   return utf8_get_repl(p, uu, UNI_REPLACEMENT);
194 }
195
196 /**
197  * Decode a value from the range `[0, 0x7FFFFFFF]`
198  * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
199  **/
200 static inline byte *utf8_32_get(const byte *p, uint *uu)
201 {
202   return utf8_32_get_repl(p, uu, UNI_REPLACEMENT);
203 }
204
205 #define UTF8_SKIP(p) do {                               \
206     uint c = *p++;                                      \
207     if (c >= 0xc0)                                      \
208       while (c & 0x40 && *p >= 0x80 && *p < 0xc0)       \
209         p++, c <<= 1;                                   \
210   } while (0)
211
212 #define UTF8_SKIP_BWD(p) while ((*--(p) & 0xc0) == 0x80)
213
214 /**
215  * Return the number of bytes needed to encode a given value from the range `[0, 0x7FFFFFFF]` to UTF-8.
216  **/
217 static inline uint utf8_space(uint u)
218 {
219   if (u < 0x80)
220     return 1;
221   if (u < 0x800)
222     return 2;
223   if (u < (1<<16))
224     return 3;
225   if (u < (1<<21))
226     return 4;
227   if (u < (1<<26))
228     return 5;
229   return 6;
230 }
231
232 /**
233  * Compute the length of a single UTF-8 character from its first byte. The encoding must be valid.
234  **/
235 static inline uint utf8_encoding_len(uint c)
236 {
237   if (c < 0x80)
238     return 1;
239   ASSERT(c >= 0xc0 && c < 0xfe);
240   if (c < 0xe0)
241     return 2;
242   if (c < 0xf0)
243     return 3;
244   if (c < 0xf8)
245     return 4;
246   if (c < 0xfc)
247     return 5;
248   return 6;
249 }
250
251 /**
252  * Encode an UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
253  * up to 4 bytes needed.
254  **/
255 static inline void *utf16_le_put(void *p, uint u)
256 {
257   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
258     {
259       put_u16_le(p, u);
260       return p + 2;
261     }
262   else if ((u -= 0x10000) < 0x100000)
263     {
264       put_u16_le(p, 0xd800 | (u >> 10));
265       put_u16_le(p + 2, 0xdc00 | (u & 0x3ff));
266       return p + 4;
267     }
268   else
269     ASSERT(0);
270 }
271
272 /**
273  * Encode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
274  * up to 4 bytes needed.
275  **/
276 static inline void *utf16_be_put(void *p, uint u)
277 {
278   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
279     {
280       put_u16_be(p, u);
281       return p + 2;
282     }
283   else if ((u -= 0x10000) < 0x100000)
284     {
285       put_u16_be(p, 0xd800 | (u >> 10));
286       put_u16_be(p + 2, 0xdc00 | (u & 0x3ff));
287       return p + 4;
288     }
289   else
290     ASSERT(0);
291 }
292
293 /**
294  * Decode a UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
295  * or return @repl if the encoding has been corrupted.
296  **/
297 static inline void *utf16_le_get_repl(const void *p, uint *uu, uint repl)
298 {
299   uint u = get_u16_le(p), x, y;
300   x = u - 0xd800;
301   if (x < 0x800)
302     if (x < 0x400 && (y = get_u16_le(p + 2) - 0xdc00) < 0x400)
303       {
304         u = 0x10000 + (x << 10) + y;
305         p += 2;
306       }
307     else
308       u = repl;
309   *uu = u;
310   return (void *)(p + 2);
311 }
312
313 /**
314  * Decode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
315  * or return @repl if the encoding has been corrupted.
316  **/
317 static inline void *utf16_be_get_repl(const void *p, uint *uu, uint repl)
318 {
319   uint u = get_u16_be(p), x, y;
320   x = u - 0xd800;
321   if (x < 0x800)
322     if (x < 0x400 && (y = get_u16_be(p + 2) - 0xdc00) < 0x400)
323       {
324         u = 0x10000 + (x << 10) + y;
325         p += 2;
326       }
327     else
328       u = repl;
329   *uu = u;
330   return (void *)(p + 2);
331 }
332
333 /**
334  * Decode a UTF-16LE  character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
335  * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
336  **/
337 static inline void *utf16_le_get(const void *p, uint *uu)
338 {
339   return utf16_le_get_repl(p, uu, UNI_REPLACEMENT);
340 }
341
342 /**
343  * Decode a UTF-16BE  character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
344  * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
345  **/
346 static inline void *utf16_be_get(const void *p, uint *uu)
347 {
348   return utf16_be_get_repl(p, uu, UNI_REPLACEMENT);
349 }
350
351 /**
352  * Basic sanity check on Unicode characters. Return `UNI_REPLACEMENT` if the input
353  * character is a surrogate, ASCII or Latin-1 control character different from the tab,
354  * or if it lies outside the basic plane. In all other cases, it acts as an identity.
355  **/
356 static inline uint unicode_sanitize_char(uint u)
357 {
358   if (u >= 0x10000 ||                   // We don't accept anything outside the basic plane
359       u >= 0xd800 && u < 0xf900 ||      // neither we do surrogates and private use characters
360       u >= 0x80 && u < 0xa0 ||          // nor latin-1 control chars
361       u < 0x20 && u != '\t')
362     return UNI_REPLACEMENT;
363   return u;
364 }
365
366 /* unicode-utf8.c */
367
368 /**
369  * Count the number of Unicode characters in a zero-terminated UTF-8 string.
370  * Returned value for corrupted encoding is undefined, but is never greater than strlen().
371  **/
372 size_t utf8_strlen(const byte *str);
373
374 /**
375  * Same as @utf8_strlen(), but returns at most @n characters.
376  **/
377 size_t utf8_strnlen(const byte *str, size_t n);
378
379 #endif