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