Debian Codegraph @

src/utf8.c

raw
1 /*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2014 Ran Benita <[email protected]>
4 * SPDX-License-Identifier: MIT
5 *
6 * Author: Rob Bradford <[email protected]>
7 */
8
9 #include "config.h"
10
11 #include "utils.h"
12 #include "utf8.h"
13
14 /* Conformant encoding form conversion from UTF-32 to UTF-8.
15 *
16 * See https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G28875
17 * for further details.
18 */
19 int
20 utf32_to_utf8(uint32_t unichar, char *buffer)
21 {
22 int count, shift, length;
23 uint8_t head;
24
25 /* NOLINTBEGIN(bugprone-branch-clone) */
26 if (unichar <= 0x007f) {
27 buffer[0] = (char) unichar;
28 buffer[1] = '\0';
29 return 2;
30 }
31 else if (unichar <= 0x07FF) {
32 length = 2;
33 head = 0xc0;
34 }
35 /* Handle surrogates */
36 else if (is_surrogate(unichar)) {
37 goto ill_formed_code_unit_subsequence;
38 }
39 else if (unichar <= 0xffff) {
40 length = 3;
41 head = 0xe0;
42 }
43 else if (unichar <= 0x10ffff) {
44 length = 4;
45 head = 0xf0;
46 }
47 else {
48 goto ill_formed_code_unit_subsequence;
49 }
50 /* NOLINTEND(bugprone-branch-clone) */
51
52 for (count = length - 1, shift = 0; count > 0; count--, shift += 6)
53 buffer[count] = (char)(0x80 | ((unichar >> shift) & 0x3f));
54
55 buffer[0] = (char)(head | ((unichar >> shift) & 0x3f));
56 buffer[length] = '\0';
57
58 return length + 1;
59
60 ill_formed_code_unit_subsequence:
61 buffer[0] = '\0';
62 return 0;
63 }
64
65 bool
66 is_valid_utf8(const char *ss, size_t len)
67 {
68 size_t i = 0;
69 size_t tail_bytes = 0;
70 const uint8_t *s = (const uint8_t *) ss;
71
72 /* This beauty is from:
73 * The Unicode Standard Version 6.2 - Core Specification, Table 3.7
74 * https://www.unicode.org/versions/Unicode6.2.0/ch03.pdf#G7404
75 * We can optimize if needed. */
76 while (i < len)
77 {
78 /* NOLINTBEGIN(bugprone-branch-clone) */
79 if (s[i] <= 0x7F) {
80 tail_bytes = 0;
81 }
82 else if (s[i] >= 0xC2 && s[i] <= 0xDF) {
83 tail_bytes = 1;
84 }
85 else if (s[i] == 0xE0) {
86 i++;
87 if (i >= len || !(s[i] >= 0xA0 && s[i] <= 0xBF))
88 return false;
89 tail_bytes = 1;
90 }
91 else if (s[i] >= 0xE1 && s[i] <= 0xEC) {
92 tail_bytes = 2;
93 }
94 else if (s[i] == 0xED) {
95 i++;
96 if (i >= len || !(s[i] >= 0x80 && s[i] <= 0x9F))
97 return false;
98 tail_bytes = 1;
99 }
100 else if (s[i] >= 0xEE && s[i] <= 0xEF) {
101 tail_bytes = 2;
102 }
103 else if (s[i] == 0xF0) {
104 i++;
105 if (i >= len || !(s[i] >= 0x90 && s[i] <= 0xBF))
106 return false;
107 tail_bytes = 2;
108 }
109 else if (s[i] >= 0xF1 && s[i] <= 0xF3) {
110 tail_bytes = 3;
111 }
112 else if (s[i] == 0xF4) {
113 i++;
114 if (i >= len || !(s[i] >= 0x80 && s[i] <= 0x8F))
115 return false;
116 tail_bytes = 2;
117 }
118 else {
119 return false;
120 }
121 /* NOLINTEND(bugprone-branch-clone) */
122
123 i++;
124
125 while (i < len && tail_bytes > 0 && s[i] >= 0x80 && s[i] <= 0xBF) {
126 i++;
127 tail_bytes--;
128 }
129
130 if (tail_bytes != 0)
131 return false;
132 }
133
134 return true;
135 }