]> mj.ucw.cz Git - libucw.git/blob - ucw/unicode.h
Revert "Unicode: Added reading of 32bit UTF-8 unicode values with protection against...
[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 /** Maximum number of bytes an UTF-8 character can have. **/
252 #define UTF8_MAX_LEN 6
253
254 /**
255  * Encode an UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
256  * up to 4 bytes needed.
257  **/
258 static inline void *utf16_le_put(void *p, uint u)
259 {
260   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
261     {
262       put_u16_le(p, u);
263       return p + 2;
264     }
265   else if ((u -= 0x10000) < 0x100000)
266     {
267       put_u16_le(p, 0xd800 | (u >> 10));
268       put_u16_le(p + 2, 0xdc00 | (u & 0x3ff));
269       return p + 4;
270     }
271   else
272     ASSERT(0);
273 }
274
275 /**
276  * Encode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
277  * up to 4 bytes needed.
278  **/
279 static inline void *utf16_be_put(void *p, uint u)
280 {
281   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
282     {
283       put_u16_be(p, u);
284       return p + 2;
285     }
286   else if ((u -= 0x10000) < 0x100000)
287     {
288       put_u16_be(p, 0xd800 | (u >> 10));
289       put_u16_be(p + 2, 0xdc00 | (u & 0x3ff));
290       return p + 4;
291     }
292   else
293     ASSERT(0);
294 }
295
296 /**
297  * Decode a UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
298  * or return @repl if the encoding has been corrupted.
299  **/
300 static inline void *utf16_le_get_repl(const void *p, uint *uu, uint repl)
301 {
302   uint u = get_u16_le(p), x, y;
303   x = u - 0xd800;
304   if (x < 0x800)
305     if (x < 0x400 && (y = get_u16_le(p + 2) - 0xdc00) < 0x400)
306       {
307         u = 0x10000 + (x << 10) + y;
308         p += 2;
309       }
310     else
311       u = repl;
312   *uu = u;
313   return (void *)(p + 2);
314 }
315
316 /**
317  * Decode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
318  * or return @repl if the encoding has been corrupted.
319  **/
320 static inline void *utf16_be_get_repl(const void *p, uint *uu, uint repl)
321 {
322   uint u = get_u16_be(p), x, y;
323   x = u - 0xd800;
324   if (x < 0x800)
325     if (x < 0x400 && (y = get_u16_be(p + 2) - 0xdc00) < 0x400)
326       {
327         u = 0x10000 + (x << 10) + y;
328         p += 2;
329       }
330     else
331       u = repl;
332   *uu = u;
333   return (void *)(p + 2);
334 }
335
336 /**
337  * Decode a UTF-16LE  character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
338  * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
339  **/
340 static inline void *utf16_le_get(const void *p, uint *uu)
341 {
342   return utf16_le_get_repl(p, uu, UNI_REPLACEMENT);
343 }
344
345 /**
346  * Decode a UTF-16BE  character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
347  * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
348  **/
349 static inline void *utf16_be_get(const void *p, uint *uu)
350 {
351   return utf16_be_get_repl(p, uu, UNI_REPLACEMENT);
352 }
353
354 /**
355  * Basic sanity check on Unicode characters. Return `UNI_REPLACEMENT` if the input
356  * character is a surrogate, ASCII or Latin-1 control character different from the tab,
357  * or if it lies outside the basic plane. In all other cases, it acts as an identity.
358  **/
359 static inline uint unicode_sanitize_char(uint u)
360 {
361   if (u >= 0x10000 ||                   // We don't accept anything outside the basic plane
362       u >= 0xd800 && u < 0xf900 ||      // neither we do surrogates and private use characters
363       u >= 0x80 && u < 0xa0 ||          // nor latin-1 control chars
364       u < 0x20 && u != '\t')
365     return UNI_REPLACEMENT;
366   return u;
367 }
368
369 /* unicode-utf8.c */
370
371 /**
372  * Count the number of Unicode characters in a zero-terminated UTF-8 string.
373  * Returned value for corrupted encoding is undefined, but is never greater than strlen().
374  **/
375 size_t utf8_strlen(const byte *str);
376
377 /**
378  * Same as @utf8_strlen(), but returns at most @n characters.
379  **/
380 size_t utf8_strnlen(const byte *str, size_t n);
381
382 #endif