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