]>
git.proxmox.com Git - mirror_qemu.git/blob - util/unicode.c
4 * Copyright (C) 2013 Red Hat, Inc.
7 * Markus Armbruster <armbru@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qemu/unicode.h"
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
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
26 * If @n is zero or @s points to a zero byte, the sequence is invalid,
27 * and @end is set to @s.
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
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.
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.
43 * Conversion succeeds if and only if the sequence is valid.
45 * Returns: the Unicode codepoint on success, -1 on failure.
47 int mod_utf8_codepoint(const char *s
, size_t n
, char **end
)
49 static int min_cp
[5] = { 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
50 const unsigned char *p
;
51 unsigned byte
, mask
, len
, i
;
54 if (n
== 0 || *s
== 0) {
60 p
= (const unsigned char *)s
;
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 */
69 /* multi-byte sequence */
71 for (mask
= 0x80; byte
& mask
; mask
>>= 1) {
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 */
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 */