2 * UCW Library -- Unicode Characters
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>
8 * This software may be freely distributed and used according to the terms
9 * of the GNU Lesser General Public License.
12 #ifndef _UCW_UNICODE_H
13 #define _UCW_UNICODE_H
15 #include <ucw/unaligned.h>
17 #ifdef CONFIG_UCW_CLEAN_ABI
18 #define utf8_strlen ucw_utf8_strlen
19 #define utf8_strnlen ucw_utf8_strnlen
22 /* Macros for handling UTF-8 */
24 #define UNI_REPLACEMENT 0xfffc /** Unicode value used as a default replacement of invalid characters. **/
27 * Encode a value from the range `[0, 0xFFFF]`
28 * (basic multilingual plane); up to 3 bytes needed (RFC2279).
30 static inline byte *utf8_put(byte *p, uint u)
36 *p++ = 0xc0 | (u >> 6);
37 *p++ = 0x80 | (u & 0x3f);
42 *p++ = 0xe0 | (u >> 12);
43 *p++ = 0x80 | ((u >> 6) & 0x3f);
44 *p++ = 0x80 | (u & 0x3f);
50 * Encode a value from the range `[0, 0x7FFFFFFF]`;
51 * (superset of Unicode 4.0) up to 6 bytes needed (RFC2279).
53 static inline byte *utf8_32_put(byte *p, uint u)
59 *p++ = 0xc0 | (u >> 6);
64 *p++ = 0xe0 | (u >> 12);
69 *p++ = 0xf0 | (u >> 18);
74 *p++ = 0xf8 | (u >> 24);
77 else if (u < (1U<<31))
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);
91 #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f)
92 #define UTF8_CHECK_AVAIL(n) if (unlikely(avail < n)) goto bad
93 #define UTF8_CHECK_RANGE(r) if (unlikely(u < r)) goto bad
96 * Decode a value from the range `[0, 0xFFFF]` (basic multilingual plane)
97 * or return @repl if the encoding has been corrupted.
99 static inline byte *utf8_get_repl(const byte *p, uint *uu, uint repl)
104 else if (unlikely(u < 0xc0))
106 /* Incorrect byte sequence */
114 UTF8_CHECK_RANGE(0x80);
116 else if (likely(u < 0xf0))
121 UTF8_CHECK_RANGE(0x800);
130 * Decode a value from the range `[0, 0x7FFFFFFF]`
131 * or return @repl if the encoding has been corrupted.
133 static inline byte *utf8_32_get_repl(const byte *p, uint *uu, uint repl)
139 else if (unlikely(u < 0xc0))
174 if (unlikely(u < limit))
183 /* Incorrect byte sequence */
189 * Decode a value from the range `[0, 0x7FFFFFFF]`
190 * or return @repl if the encoding has been corrupted.
191 * This function never reads behind @stop (including).
192 * At least one byte must be available (@stop > @p).
194 static inline byte *utf8_32_get_repl_safe(const byte *p, const byte *stop, uint *uu, uint repl)
199 else if (unlikely(u < 0xc0))
202 size_t avail = stop - p;
241 if (unlikely(u < limit))
252 /* Incorrect byte sequence */
258 * Decode a value from the range `[0, 0xFFFF]` (basic multilingual plane)
259 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
261 static inline byte *utf8_get(const byte *p, uint *uu)
263 return utf8_get_repl(p, uu, UNI_REPLACEMENT);
267 * Decode a value from the range `[0, 0x7FFFFFFF]`
268 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
270 static inline byte *utf8_32_get(const byte *p, uint *uu)
272 return utf8_32_get_repl(p, uu, UNI_REPLACEMENT);
275 #define UTF8_SKIP(p) do { \
278 while (c & 0x40 && *p >= 0x80 && *p < 0xc0) \
282 #define UTF8_SKIP_BWD(p) while ((*--(p) & 0xc0) == 0x80)
285 * Return the number of bytes needed to encode a given value from the range `[0, 0x7FFFFFFF]` to UTF-8.
287 static inline uint utf8_space(uint u)
303 * Compute the length of a single UTF-8 character from its first byte. The encoding must be valid.
305 static inline uint utf8_encoding_len(uint c)
309 ASSERT(c >= 0xc0 && c < 0xfe);
321 /** Maximum number of bytes an UTF-8 character can have. **/
322 #define UTF8_MAX_LEN 6
325 * Encode an UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
326 * up to 4 bytes needed.
328 static inline void *utf16_le_put(void *p, uint u)
330 if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
335 else if ((u -= 0x10000) < 0x100000)
337 put_u16_le(p, 0xd800 | (u >> 10));
338 put_u16_le(p + 2, 0xdc00 | (u & 0x3ff));
346 * Encode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
347 * up to 4 bytes needed.
349 static inline void *utf16_be_put(void *p, uint u)
351 if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
356 else if ((u -= 0x10000) < 0x100000)
358 put_u16_be(p, 0xd800 | (u >> 10));
359 put_u16_be(p + 2, 0xdc00 | (u & 0x3ff));
367 * Decode a UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
368 * or return @repl if the encoding has been corrupted.
370 static inline void *utf16_le_get_repl(const void *p, uint *uu, uint repl)
372 uint u = get_u16_le(p), x, y;
375 if (x < 0x400 && (y = get_u16_le(p + 2) - 0xdc00) < 0x400)
377 u = 0x10000 + (x << 10) + y;
383 return (void *)(p + 2);
387 * Decode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
388 * or return @repl if the encoding has been corrupted.
390 static inline void *utf16_be_get_repl(const void *p, uint *uu, uint repl)
392 uint u = get_u16_be(p), x, y;
395 if (x < 0x400 && (y = get_u16_be(p + 2) - 0xdc00) < 0x400)
397 u = 0x10000 + (x << 10) + y;
403 return (void *)(p + 2);
407 * Decode a UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
408 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
410 static inline void *utf16_le_get(const void *p, uint *uu)
412 return utf16_le_get_repl(p, uu, UNI_REPLACEMENT);
416 * Decode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
417 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
419 static inline void *utf16_be_get(const void *p, uint *uu)
421 return utf16_be_get_repl(p, uu, UNI_REPLACEMENT);
425 * Basic sanity check on Unicode characters. Return `UNI_REPLACEMENT` if the input
426 * character is a surrogate, ASCII or Latin-1 control character different from the tab,
427 * or if it lies outside the basic plane. In all other cases, it acts as an identity.
429 static inline uint unicode_sanitize_char(uint u)
431 if (u >= 0x10000 || // We don't accept anything outside the basic plane
432 u >= 0xd800 && u < 0xf900 || // neither we do surrogates and private use characters
433 u >= 0x80 && u < 0xa0 || // nor latin-1 control chars
434 u < 0x20 && u != '\t')
435 return UNI_REPLACEMENT;
442 * Count the number of Unicode characters in a zero-terminated UTF-8 string.
443 * Returned value for corrupted encoding is undefined, but is never greater than strlen().
445 size_t utf8_strlen(const byte *str);
448 * Same as @utf8_strlen(), but returns at most @n characters.
450 size_t utf8_strnlen(const byte *str, size_t n);