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"
14f348b6d1SVeronia Bahaa #include "qemu/unicode.h"
15cb2744eaSMarkus Armbruster
is_valid_codepoint(int codepoint)16*e59f39d4SMarkus Armbruster static bool is_valid_codepoint(int codepoint)
17*e59f39d4SMarkus Armbruster {
18*e59f39d4SMarkus Armbruster if (codepoint > 0x10FFFFu) {
19*e59f39d4SMarkus Armbruster return false; /* beyond Unicode range */
20*e59f39d4SMarkus Armbruster }
21*e59f39d4SMarkus Armbruster if ((codepoint >= 0xFDD0 && codepoint <= 0xFDEF)
22*e59f39d4SMarkus Armbruster || (codepoint & 0xFFFE) == 0xFFFE) {
23*e59f39d4SMarkus Armbruster return false; /* noncharacter */
24*e59f39d4SMarkus Armbruster }
25*e59f39d4SMarkus Armbruster if (codepoint >= 0xD800 && codepoint <= 0xDFFF) {
26*e59f39d4SMarkus Armbruster return false; /* surrogate code point */
27*e59f39d4SMarkus Armbruster }
28*e59f39d4SMarkus Armbruster return true;
29*e59f39d4SMarkus Armbruster }
30*e59f39d4SMarkus Armbruster
31cb2744eaSMarkus Armbruster /**
32cb2744eaSMarkus Armbruster * mod_utf8_codepoint:
33cb2744eaSMarkus Armbruster * @s: string encoded in modified UTF-8
34cb2744eaSMarkus Armbruster * @n: maximum number of bytes to read from @s, if less than 6
35cb2744eaSMarkus Armbruster * @end: set to end of sequence on return
36cb2744eaSMarkus Armbruster *
37cb2744eaSMarkus Armbruster * Convert the modified UTF-8 sequence at the start of @s. Modified
38cb2744eaSMarkus Armbruster * UTF-8 is exactly like UTF-8, except U+0000 is encoded as
39cb2744eaSMarkus Armbruster * "\xC0\x80".
40cb2744eaSMarkus Armbruster *
41cb2744eaSMarkus Armbruster * If @n is zero or @s points to a zero byte, the sequence is invalid,
42cb2744eaSMarkus Armbruster * and @end is set to @s.
43cb2744eaSMarkus Armbruster *
44cb2744eaSMarkus Armbruster * If @s points to an impossible byte (0xFE or 0xFF) or a continuation
45cb2744eaSMarkus Armbruster * byte, the sequence is invalid, and @end is set to @s + 1
46cb2744eaSMarkus Armbruster *
47cb2744eaSMarkus Armbruster * Else, the first byte determines how many continuation bytes are
48cb2744eaSMarkus Armbruster * expected. If there are fewer, the sequence is invalid, and @end is
49cb2744eaSMarkus Armbruster * set to @s + 1 + actual number of continuation bytes. Else, the
50cb2744eaSMarkus Armbruster * sequence is well-formed, and @end is set to @s + 1 + expected
51cb2744eaSMarkus Armbruster * number of continuation bytes.
52cb2744eaSMarkus Armbruster *
53cb2744eaSMarkus Armbruster * A well-formed sequence is valid unless it encodes a codepoint
54cb2744eaSMarkus Armbruster * outside the Unicode range U+0000..U+10FFFF, one of Unicode's 66
55cb2744eaSMarkus Armbruster * noncharacters, a surrogate codepoint, or is overlong. Except the
56cb2744eaSMarkus Armbruster * overlong sequence "\xC0\x80" is valid.
57cb2744eaSMarkus Armbruster *
58cb2744eaSMarkus Armbruster * Conversion succeeds if and only if the sequence is valid.
59cb2744eaSMarkus Armbruster *
60cb2744eaSMarkus Armbruster * Returns: the Unicode codepoint on success, -1 on failure.
61cb2744eaSMarkus Armbruster */
mod_utf8_codepoint(const char * s,size_t n,char ** end)62cb2744eaSMarkus Armbruster int mod_utf8_codepoint(const char *s, size_t n, char **end)
63cb2744eaSMarkus Armbruster {
64cb2744eaSMarkus Armbruster static int min_cp[5] = { 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
65cb2744eaSMarkus Armbruster const unsigned char *p;
66cb2744eaSMarkus Armbruster unsigned byte, mask, len, i;
67cb2744eaSMarkus Armbruster int cp;
68cb2744eaSMarkus Armbruster
69cb2744eaSMarkus Armbruster if (n == 0 || *s == 0) {
70cb2744eaSMarkus Armbruster /* empty sequence */
71cb2744eaSMarkus Armbruster *end = (char *)s;
72cb2744eaSMarkus Armbruster return -1;
73cb2744eaSMarkus Armbruster }
74cb2744eaSMarkus Armbruster
75cb2744eaSMarkus Armbruster p = (const unsigned char *)s;
76cb2744eaSMarkus Armbruster byte = *p++;
77cb2744eaSMarkus Armbruster if (byte < 0x80) {
78cb2744eaSMarkus Armbruster cp = byte; /* one byte sequence */
79cb2744eaSMarkus Armbruster } else if (byte >= 0xFE) {
80cb2744eaSMarkus Armbruster cp = -1; /* impossible bytes 0xFE, 0xFF */
81cb2744eaSMarkus Armbruster } else if ((byte & 0x40) == 0) {
82cb2744eaSMarkus Armbruster cp = -1; /* unexpected continuation byte */
83cb2744eaSMarkus Armbruster } else {
84cb2744eaSMarkus Armbruster /* multi-byte sequence */
85cb2744eaSMarkus Armbruster len = 0;
86cb2744eaSMarkus Armbruster for (mask = 0x80; byte & mask; mask >>= 1) {
87cb2744eaSMarkus Armbruster len++;
88cb2744eaSMarkus Armbruster }
89cb2744eaSMarkus Armbruster assert(len > 1 && len < 7);
90cb2744eaSMarkus Armbruster cp = byte & (mask - 1);
91cb2744eaSMarkus Armbruster for (i = 1; i < len; i++) {
92cb2744eaSMarkus Armbruster byte = i < n ? *p : 0;
93cb2744eaSMarkus Armbruster if ((byte & 0xC0) != 0x80) {
94cb2744eaSMarkus Armbruster cp = -1; /* continuation byte missing */
95cb2744eaSMarkus Armbruster goto out;
96cb2744eaSMarkus Armbruster }
97cb2744eaSMarkus Armbruster p++;
98cb2744eaSMarkus Armbruster cp <<= 6;
99cb2744eaSMarkus Armbruster cp |= byte & 0x3F;
100cb2744eaSMarkus Armbruster }
101*e59f39d4SMarkus Armbruster if (!is_valid_codepoint(cp)) {
102*e59f39d4SMarkus Armbruster cp = -1;
103cb2744eaSMarkus Armbruster } else if (cp < min_cp[len - 2] && !(cp == 0 && len == 2)) {
104cb2744eaSMarkus Armbruster cp = -1; /* overlong, not \xC0\x80 */
105cb2744eaSMarkus Armbruster }
106cb2744eaSMarkus Armbruster }
107cb2744eaSMarkus Armbruster
108cb2744eaSMarkus Armbruster out:
109cb2744eaSMarkus Armbruster *end = (char *)p;
110cb2744eaSMarkus Armbruster return cp;
111cb2744eaSMarkus Armbruster }
112*e59f39d4SMarkus Armbruster
113*e59f39d4SMarkus Armbruster /**
114*e59f39d4SMarkus Armbruster * mod_utf8_encode:
115*e59f39d4SMarkus Armbruster * @buf: Destination buffer
116*e59f39d4SMarkus Armbruster * @bufsz: size of @buf, at least 5.
117*e59f39d4SMarkus Armbruster * @codepoint: Unicode codepoint to encode
118*e59f39d4SMarkus Armbruster *
119*e59f39d4SMarkus Armbruster * Convert Unicode codepoint @codepoint to modified UTF-8.
120*e59f39d4SMarkus Armbruster *
121*e59f39d4SMarkus Armbruster * Returns: the length of the UTF-8 sequence on success, -1 when
122*e59f39d4SMarkus Armbruster * @codepoint is invalid.
123*e59f39d4SMarkus Armbruster */
mod_utf8_encode(char buf[],size_t bufsz,int codepoint)124*e59f39d4SMarkus Armbruster ssize_t mod_utf8_encode(char buf[], size_t bufsz, int codepoint)
125*e59f39d4SMarkus Armbruster {
126*e59f39d4SMarkus Armbruster assert(bufsz >= 5);
127*e59f39d4SMarkus Armbruster
128*e59f39d4SMarkus Armbruster if (!is_valid_codepoint(codepoint)) {
129*e59f39d4SMarkus Armbruster return -1;
130*e59f39d4SMarkus Armbruster }
131*e59f39d4SMarkus Armbruster
132*e59f39d4SMarkus Armbruster if (codepoint > 0 && codepoint <= 0x7F) {
133*e59f39d4SMarkus Armbruster buf[0] = codepoint & 0x7F;
134*e59f39d4SMarkus Armbruster buf[1] = 0;
135*e59f39d4SMarkus Armbruster return 1;
136*e59f39d4SMarkus Armbruster }
137*e59f39d4SMarkus Armbruster if (codepoint <= 0x7FF) {
138*e59f39d4SMarkus Armbruster buf[0] = 0xC0 | ((codepoint >> 6) & 0x1F);
139*e59f39d4SMarkus Armbruster buf[1] = 0x80 | (codepoint & 0x3F);
140*e59f39d4SMarkus Armbruster buf[2] = 0;
141*e59f39d4SMarkus Armbruster return 2;
142*e59f39d4SMarkus Armbruster }
143*e59f39d4SMarkus Armbruster if (codepoint <= 0xFFFF) {
144*e59f39d4SMarkus Armbruster buf[0] = 0xE0 | ((codepoint >> 12) & 0x0F);
145*e59f39d4SMarkus Armbruster buf[1] = 0x80 | ((codepoint >> 6) & 0x3F);
146*e59f39d4SMarkus Armbruster buf[2] = 0x80 | (codepoint & 0x3F);
147*e59f39d4SMarkus Armbruster buf[3] = 0;
148*e59f39d4SMarkus Armbruster return 3;
149*e59f39d4SMarkus Armbruster }
150*e59f39d4SMarkus Armbruster buf[0] = 0xF0 | ((codepoint >> 18) & 0x07);
151*e59f39d4SMarkus Armbruster buf[1] = 0x80 | ((codepoint >> 12) & 0x3F);
152*e59f39d4SMarkus Armbruster buf[2] = 0x80 | ((codepoint >> 6) & 0x3F);
153*e59f39d4SMarkus Armbruster buf[3] = 0x80 | (codepoint & 0x3F);
154*e59f39d4SMarkus Armbruster buf[4] = 0;
155*e59f39d4SMarkus Armbruster return 4;
156*e59f39d4SMarkus Armbruster }
157