xref: /openbmc/qemu/util/unicode.c (revision 4a09d0bb)
1 /*
2  * Dealing with Unicode
3  *
4  * Copyright (C) 2013 Red Hat, Inc.
5  *
6  * Authors:
7  *  Markus Armbruster <armbru@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or
10  * later.  See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/unicode.h"
15 
16 /**
17  * mod_utf8_codepoint:
18  * @s: string encoded in modified UTF-8
19  * @n: maximum number of bytes to read from @s, if less than 6
20  * @end: set to end of sequence on return
21  *
22  * Convert the modified UTF-8 sequence at the start of @s.  Modified
23  * UTF-8 is exactly like UTF-8, except U+0000 is encoded as
24  * "\xC0\x80".
25  *
26  * If @n is zero or @s points to a zero byte, the sequence is invalid,
27  * and @end is set to @s.
28  *
29  * If @s points to an impossible byte (0xFE or 0xFF) or a continuation
30  * byte, the sequence is invalid, and @end is set to @s + 1
31  *
32  * Else, the first byte determines how many continuation bytes are
33  * expected.  If there are fewer, the sequence is invalid, and @end is
34  * set to @s + 1 + actual number of continuation bytes.  Else, the
35  * sequence is well-formed, and @end is set to @s + 1 + expected
36  * number of continuation bytes.
37  *
38  * A well-formed sequence is valid unless it encodes a codepoint
39  * outside the Unicode range U+0000..U+10FFFF, one of Unicode's 66
40  * noncharacters, a surrogate codepoint, or is overlong.  Except the
41  * overlong sequence "\xC0\x80" is valid.
42  *
43  * Conversion succeeds if and only if the sequence is valid.
44  *
45  * Returns: the Unicode codepoint on success, -1 on failure.
46  */
47 int mod_utf8_codepoint(const char *s, size_t n, char **end)
48 {
49     static int min_cp[5] = { 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
50     const unsigned char *p;
51     unsigned byte, mask, len, i;
52     int cp;
53 
54     if (n == 0 || *s == 0) {
55         /* empty sequence */
56         *end = (char *)s;
57         return -1;
58     }
59 
60     p = (const unsigned char *)s;
61     byte = *p++;
62     if (byte < 0x80) {
63         cp = byte;              /* one byte sequence */
64     } else if (byte >= 0xFE) {
65         cp = -1;                /* impossible bytes 0xFE, 0xFF */
66     } else if ((byte & 0x40) == 0) {
67         cp = -1;                /* unexpected continuation byte */
68     } else {
69         /* multi-byte sequence */
70         len = 0;
71         for (mask = 0x80; byte & mask; mask >>= 1) {
72             len++;
73         }
74         assert(len > 1 && len < 7);
75         cp = byte & (mask - 1);
76         for (i = 1; i < len; i++) {
77             byte = i < n ? *p : 0;
78             if ((byte & 0xC0) != 0x80) {
79                 cp = -1;        /* continuation byte missing */
80                 goto out;
81             }
82             p++;
83             cp <<= 6;
84             cp |= byte & 0x3F;
85         }
86         if (cp > 0x10FFFF) {
87             cp = -1;            /* beyond Unicode range */
88         } else if ((cp >= 0xFDD0 && cp <= 0xFDEF)
89                    || (cp & 0xFFFE) == 0xFFFE) {
90             cp = -1;            /* noncharacter */
91         } else if (cp >= 0xD800 && cp <= 0xDFFF) {
92             cp = -1;            /* surrogate code point */
93         } else if (cp < min_cp[len - 2] && !(cp == 0 && len == 2)) {
94             cp = -1;            /* overlong, not \xC0\x80 */
95         }
96     }
97 
98 out:
99     *end = (char *)p;
100     return cp;
101 }
102