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