asar coverage - build #272


src/asar/
File: src/asar/unicode.cpp
Date: 2025-03-03 19:47:10
Lines:
46/93
49.5%
Functions:
4/7
57.1%
Branches:
52/127
40.9%

Line Branch Exec Source
1 #include "unicode.h"
2
3 351027 size_t utf8_val(int* codepoint, const char* inp) {
4 351027 unsigned char c = *inp++;
5 int val;
6
2/2
✓ Branch 0 taken 350623 times.
✓ Branch 1 taken 404 times.
351027 if (c < 0x80) {
7 // plain ascii
8 350623 *codepoint = c;
9 350623 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 404 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 404 times.
✗ Branch 3 not taken.
404 else if (c > 0xC1 && c < 0xF5) {
15 // 1, 2 or 3 continuation bytes
16
4/4
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 110 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 158 times.
404 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 187 times.
404 val = (c & ((1 << (6 - cont_byte_count)) - 1));
19
2/2
✓ Branch 0 taken 758 times.
✓ Branch 1 taken 402 times.
1160 for (int i = 0; i < cont_byte_count; i++) {
20 758 unsigned char next = *inp++;
21
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 756 times.
758 if ((next & 0xC0) != 0x80) {
22 2 *codepoint = -1;
23 2 return 0u;
24 }
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
756 val = (val << 6) | (next & 0x3F);
26 }
27 402 if (// too many cont.bytes
28
4/5
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 216 times.
✓ Branch 3 taken 186 times.
✗ Branch 4 not taken.
402 (*inp & 0xC0) == 0x80 ||
29
30 // invalid codepoints
31
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 292 times.
402 val > 0x10FFFF ||
32
33 // check overlong encodings
34
3/4
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 134 times.
✓ Branch 3 taken 268 times.
402 (cont_byte_count == 3 && val < 0x1000) ||
35
3/4
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 158 times.
✓ Branch 3 taken 244 times.
402 (cont_byte_count == 2 && val < 0x800) ||
36
3/4
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 290 times.
402 (cont_byte_count == 1 && val < 0x80) ||
37
38 // UTF16 surrogates
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 (val >= 0xD800 && val <= 0xDFFF)
40 ) {
41 *codepoint = -1;
42 return 0u;
43 };
44 402 *codepoint = val;
45 402 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 329 bool is_valid_utf8(const char* inp) {
79
3/3
✓ Branch 0 taken 98884 times.
✓ Branch 1 taken 100680 times.
✓ Branch 2 taken 184 times.
199748 while (*inp != '\0') {
80 100537 int codepoint;
81
1/2
✓ Branch 0 taken 100420 times.
✗ Branch 1 not taken.
199421 inp += utf8_val(&codepoint, inp);
82
83
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 199419 times.
199421 if (codepoint == -1) return false;
84 }
85
86 327 return true;
87 }
88
89 size_t utf16_val(int* codepoint, const wchar_t* inp)
90 {
91 wchar_t first_word = *inp;
92
93 if (first_word <= 0xD800 || first_word >= 0xDFFF)
94 {
95 // Single word
96 *codepoint = first_word;
97 return 1u;
98 }
99 else if (first_word >= 0xD800 && first_word <= 0xDBFF)
100 {
101 // Start of a surrogate pair
102 wchar_t second_word = *(inp + 1);
103
104 if (second_word >= 0xDC00 && second_word <= 0xDFFF)
105 {
106 *codepoint = 0x10000
107 + ((int)(first_word - 0xD800) << 10u)
108 + ((int)(second_word - 0xDC00));
109 return 2u;
110 }
111 }
112
113 // Everything not covered above is considered invalid.
114 *codepoint = -1;
115 return 0u;
116 }
117
118 148029 bool codepoint_to_utf16(std::wstring* out, unsigned int codepoint)
119 {
120
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 148017 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
148029 if (codepoint >= 0x10000 && codepoint <= 0x10FFFF)
121 {
122 12 wchar_t high = (wchar_t)(((codepoint - 0x10000) >> 10) + 0xD800);
123 12 wchar_t low = (wchar_t)(((codepoint - 0x10000) & 0b1111111111) + 0xDC00);
124
125
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;
126 12 return true;
127 }
128
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 148017 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
148017 else if (codepoint <= 0xD800 || codepoint >= 0xDFFF)
129 {
130
1/4
✓ Branch 0 taken 148017 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
148017 *out = std::wstring() + (wchar_t)codepoint;
131 148017 return true;
132 }
133
134 // Everything not covered above should be considered invalid.
135 return false;
136 }
137
138
139 bool utf16_to_utf8(string* result, const wchar_t* u16_str)
140 {
141 *result = "";
142
143 int codepoint;
144 do
145 {
146 u16_str += utf16_val(&codepoint, u16_str);
147
148 string next;
149 if (codepoint == -1 || !codepoint_to_utf8(&next, codepoint)) return false;
150
151 *result += next;
152 } while (codepoint != 0);
153
154 return true;
155 }
156
157 3270 bool utf8_to_utf16(std::wstring* result, const char* u8_str)
158 {
159
1/3
✓ Branch 0 taken 3270 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
3270 *result = L"";
160
161 int codepoint;
162 do
163 {
164
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
148029 u8_str += utf8_val(&codepoint, u8_str);
165
166 148029 std::wstring next;
167
4/8
✓ Branch 0 taken 148029 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148029 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 148029 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 148029 times.
148029 if (codepoint == -1 || !codepoint_to_utf16(&next, codepoint)) return false;
168
169
1/3
✓ Branch 0 taken 148029 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
148029 *result += next;
170
3/4
✓ Branch 0 taken 148029 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144759 times.
✓ Branch 3 taken 3270 times.
296058 } while (codepoint != 0);
171
172 3270 return true;
173 }
174