asar coverage - build #285


src/asar/
File: src/asar/unicode.cpp
Date: 2025-03-08 01:27:32
Lines:
51/98
52.0%
Functions:
5/8
62.5%
Branches:
55/130
42.3%

Line Branch Exec Source
1 #include "unicode.h"
2
3 273334 size_t utf8_val(int* codepoint, const char* inp) {
4 273334 unsigned char c = *inp++;
5 int val;
6
2/2
✓ Branch 0 taken 272990 times.
✓ Branch 1 taken 344 times.
273334 if (c < 0x80) {
7 // plain ascii
8 272990 *codepoint = c;
9 272990 return 1u;
10 }
11 // RPG Hacker: Byte sequences starting with 0xC0 or 0xC1 are invalid.
12 // So are byte sequences starting with anything >= 0xF5.
13 // And anything below 0xC0 indicates a follow-up byte and should never be at the start of a sequence.
14
2/4
✓ Branch 0 taken 344 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 344 times.
✗ Branch 3 not taken.
344 else if (c > 0xC1 && c < 0xF5) {
15 // 1, 2 or 3 continuation bytes
16
4/4
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 118 times.
✓ Branch 3 taken 128 times.
344 int cont_byte_count = (c >= 0xF0) ? 3 : (c >= 0xE0) ? 2 : 1;
17 // bit hack to extract the significant bits from the start byte
18
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
344 val = (c & ((1 << (6 - cont_byte_count)) - 1));
19
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 342 times.
998 for (int i = 0; i < cont_byte_count; i++) {
20 656 unsigned char next = *inp++;
21
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 654 times.
656 if ((next & 0xC0) != 0x80) {
22 2 *codepoint = -1;
23 2 return 0u;
24 }
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 291 times.
654 val = (val << 6) | (next & 0x3F);
26 }
27 342 if (// too many cont.bytes
28
4/5
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 186 times.
✓ Branch 3 taken 156 times.
✗ Branch 4 not taken.
342 (*inp & 0xC0) == 0x80 ||
29
30 // invalid codepoints
31
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 244 times.
342 val > 0x10FFFF ||
32
33 // check overlong encodings
34
3/4
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 226 times.
342 (cont_byte_count == 3 && val < 0x1000) ||
35
3/4
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 214 times.
342 (cont_byte_count == 2 && val < 0x800) ||
36
3/4
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 242 times.
342 (cont_byte_count == 1 && val < 0x80) ||
37
38 // UTF16 surrogates
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
100 (val >= 0xD800 && val <= 0xDFFF)
40 ) {
41 *codepoint = -1;
42 return 0u;
43 };
44 342 *codepoint = val;
45 342 return 1u + cont_byte_count;
46 }
47
48 // if none of the above, this couldn't possibly be a valid encoding
49 *codepoint = -1;
50 return 0u;
51 }
52
53 bool codepoint_to_utf8(string* out, unsigned int codepoint) {
54 *out = "";
55 if (codepoint < 0x80) {
56 *out += (unsigned char)codepoint;
57 }
58 else if (codepoint < 0x800) {
59 *out += (unsigned char)(0xc0 | (codepoint >> 6));
60 *out += (unsigned char)(0x80 | (codepoint & 0x3f));
61 }
62 else if (codepoint < 0x10000) {
63 *out += (unsigned char)(0xe0 | (codepoint >> 12));
64 *out += (unsigned char)(0x80 | ((codepoint >> 6) & 0x3f));
65 *out += (unsigned char)(0x80 | (codepoint & 0x3f));
66 }
67 else if (codepoint < 0x110000) {
68 *out += (unsigned char)(0xf0 | (codepoint >> 18));
69 *out += (unsigned char)(0x80 | ((codepoint >> 12) & 0x3f));
70 *out += (unsigned char)(0x80 | ((codepoint >> 6) & 0x3f));
71 *out += (unsigned char)(0x80 | (codepoint & 0x3f));
72 }
73 else return false;
74
75 return true;
76 }
77
78 331 bool is_valid_utf8(const char* inp, size_t inp_len) {
79
2/2
✓ Branch 0 taken 26316 times.
✓ Branch 1 taken 329 times.
26645 for(size_t i = 0; i < inp_len;) {
80 // optimization: if next 8 bytes are ascii, skip them
81
2/2
✓ Branch 0 taken 25101 times.
✓ Branch 1 taken 1215 times.
26316 if(i + 8 <= inp_len) {
82 12644 uint64_t buf;
83 25101 memcpy(&buf, inp+i, sizeof(buf));
84
2/2
✓ Branch 0 taken 24827 times.
✓ Branch 1 taken 274 times.
25101 if((buf & 0x8080808080808080ull) == 0) {
85 24827 i += 8; continue;
86 }
87 }
88
89 823 int codepoint;
90
1/2
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
1489 i += utf8_val(&codepoint, inp+i);
91
92
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1487 times.
1489 if (codepoint == -1) return false;
93 }
94
95 329 return true;
96 }
97
98 size_t utf16_val(int* codepoint, const wchar_t* inp)
99 {
100 wchar_t first_word = *inp;
101
102 if (first_word <= 0xD800 || first_word >= 0xDFFF)
103 {
104 // Single word
105 *codepoint = first_word;
106 return 1u;
107 }
108 else if (first_word >= 0xD800 && first_word <= 0xDBFF)
109 {
110 // Start of a surrogate pair
111 wchar_t second_word = *(inp + 1);
112
113 if (second_word >= 0xDC00 && second_word <= 0xDFFF)
114 {
115 *codepoint = 0x10000
116 + ((int)(first_word - 0xD800) << 10u)
117 + ((int)(second_word - 0xDC00));
118 return 2u;
119 }
120 }
121
122 // Everything not covered above is considered invalid.
123 *codepoint = -1;
124 return 0u;
125 }
126
127 268980 bool codepoint_to_utf16(std::wstring* out, unsigned int codepoint)
128 {
129
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 268968 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
268980 if (codepoint >= 0x10000 && codepoint <= 0x10FFFF)
130 {
131 12 wchar_t high = (wchar_t)(((codepoint - 0x10000) >> 10) + 0xD800);
132 12 wchar_t low = (wchar_t)(((codepoint - 0x10000) & 0b1111111111) + 0xDC00);
133
134
2/7
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
12 *out = std::wstring() + high + low;
135 12 return true;
136 }
137
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 268968 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
268968 else if (codepoint <= 0xD800 || codepoint >= 0xDFFF)
138 {
139
1/4
✓ Branch 0 taken 268968 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
268968 *out = std::wstring() + (wchar_t)codepoint;
140 268968 return true;
141 }
142
143 // Everything not covered above should be considered invalid.
144 return false;
145 }
146
147
148 bool utf16_to_utf8(string* result, const wchar_t* u16_str)
149 {
150 *result = "";
151
152 int codepoint;
153 do
154 {
155 u16_str += utf16_val(&codepoint, u16_str);
156
157 string next;
158 if (codepoint == -1 || !codepoint_to_utf8(&next, codepoint)) return false;
159
160 *result += next;
161 } while (codepoint != 0);
162
163 return true;
164 }
165
166 5958 bool utf8_to_utf16(std::wstring* result, const char* u8_str)
167 {
168
1/3
✓ Branch 0 taken 5958 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
5958 *result = L"";
169
170 int codepoint;
171 do
172 {
173
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
268980 u8_str += utf8_val(&codepoint, u8_str);
174
175 268980 std::wstring next;
176
4/8
✓ Branch 0 taken 268980 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 268980 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 268980 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 268980 times.
268980 if (codepoint == -1 || !codepoint_to_utf16(&next, codepoint)) return false;
177
178
1/3
✓ Branch 0 taken 268980 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
268980 *result += next;
179
3/4
✓ Branch 0 taken 268980 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 263022 times.
✓ Branch 3 taken 5958 times.
537960 } while (codepoint != 0);
180
181 5958 return true;
182 }
183