asar coverage - build #81


src/asar/
File: src/asar/unicode.cpp
Date: 2024-01-19 05:09:49
Lines:
72/81
88.9%
Functions:
7/7
100.0%
Branches:
48/72
66.7%

Line Branch Exec Source
1 #include "unicode.h"
2
3 510351 size_t utf8_val(int* codepoint, const char* inp) {
4 510351 unsigned char c = *inp++;
5 int val;
6
2/2
✓ Branch 0 taken 510004 times.
✓ Branch 1 taken 347 times.
510351 if (c < 0x80) {
7 // plain ascii
8 510004 *codepoint = c;
9 510004 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
1/2
✓ Branch 0 taken 347 times.
✗ Branch 1 not taken.
347 else if (c > 0xC1 && c < 0xF5) {
15 // 1, 2 or 3 continuation bytes
16
4/4
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 101 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 118 times.
347 int cont_byte_count = (c >= 0xF0) ? 3 : (c >= 0xE0) ? 2 : 1;
17 // bit hack to extract the significant bits from the start byte
18 347 val = (c & ((1 << (6 - cont_byte_count)) - 1));
19
2/2
✓ Branch 0 taken 665 times.
✓ Branch 1 taken 345 times.
1010 for (int i = 0; i < cont_byte_count; i++) {
20 665 unsigned char next = *inp++;
21
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 663 times.
665 if ((next & 0xC0) != 0x80) {
22 2 *codepoint = -1;
23 2 return 0u;
24 }
25 663 val = (val << 6) | (next & 0x3F);
26 }
27 345 if (// too many cont.bytes
28
2/4
✓ Branch 0 taken 345 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 345 times.
✗ Branch 3 not taken.
345 (*inp & 0xC0) == 0x80 ||
29
30 // invalid codepoints
31 345 val > 0x10FFFF ||
32
33 // check overlong encodings
34
1/2
✓ Branch 0 taken 345 times.
✗ Branch 1 not taken.
345 (cont_byte_count == 3 && val < 0x1000) ||
35
1/2
✓ Branch 0 taken 345 times.
✗ Branch 1 not taken.
345 (cont_byte_count == 2 && val < 0x800) ||
36
1/2
✓ Branch 0 taken 345 times.
✗ Branch 1 not taken.
345 (cont_byte_count == 1 && val < 0x80) ||
37
38 // UTF16 surrogates
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 345 times.
345 (val >= 0xD800 && val <= 0xDFFF)
40 ) {
41 *codepoint = -1;
42 return 0u;
43 };
44 345 *codepoint = val;
45 345 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 30104 bool codepoint_to_utf8(string* out, unsigned int codepoint) {
54 *out = "";
55
2/2
✓ Branch 0 taken 29512 times.
✓ Branch 1 taken 592 times.
30104 if (codepoint < 0x80) {
56 29512 *out += (unsigned char)codepoint;
57 }
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
592 else if (codepoint < 0x800) {
59 *out += (unsigned char)(0xc0 | (codepoint >> 6));
60 *out += (unsigned char)(0x80 | (codepoint & 0x3f));
61 }
62
2/2
✓ Branch 0 taken 354 times.
✓ Branch 1 taken 238 times.
592 else if (codepoint < 0x10000) {
63 354 *out += (unsigned char)(0xe0 | (codepoint >> 12));
64 354 *out += (unsigned char)(0x80 | ((codepoint >> 6) & 0x3f));
65 354 *out += (unsigned char)(0x80 | (codepoint & 0x3f));
66 }
67
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 else if (codepoint < 0x110000) {
68 238 *out += (unsigned char)(0xf0 | (codepoint >> 18));
69 238 *out += (unsigned char)(0x80 | ((codepoint >> 12) & 0x3f));
70 238 *out += (unsigned char)(0x80 | ((codepoint >> 6) & 0x3f));
71 238 *out += (unsigned char)(0x80 | (codepoint & 0x3f));
72 }
73 else return false;
74
75 return true;
76 }
77
78 746 bool is_valid_utf8(const char* inp) {
79
2/2
✓ Branch 0 taken 216768 times.
✓ Branch 1 taken 744 times.
217512 while (*inp != '\0') {
80 int codepoint;
81 216768 inp += utf8_val(&codepoint, inp);
82
83
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 216766 times.
216768 if (codepoint == -1) return false;
84 }
85
86 return true;
87 }
88
89 30104 size_t utf16_val(int* codepoint, const wchar_t* inp)
90 {
91 30104 wchar_t first_word = *inp;
92
93
2/2
✓ Branch 0 taken 29866 times.
✓ Branch 1 taken 238 times.
30104 if (first_word <= 0xD800 || first_word >= 0xDFFF)
94 {
95 // Single word
96 29866 *codepoint = first_word;
97 29866 return 1u;
98 }
99
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 else if (first_word >= 0xD800 && first_word <= 0xDBFF)
100 {
101 // Start of a surrogate pair
102 238 wchar_t second_word = *(inp + 1);
103
104
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 if (second_word >= 0xDC00 && second_word <= 0xDFFF)
105 {
106 238 *codepoint = 0x10000
107 238 + ((int)(first_word - 0xD800) << 10u)
108 238 + ((int)(second_word - 0xDC00));
109 238 return 2u;
110 }
111 }
112
113 // Everything not covered above is considered invalid.
114 *codepoint = -1;
115 return 0u;
116 }
117
118 290803 bool codepoint_to_utf16(std::wstring* out, unsigned int codepoint)
119 {
120
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 290788 times.
290803 if (codepoint >= 0x10000 && codepoint <= 0x10FFFF)
121 {
122 15 wchar_t high = (wchar_t)(((codepoint - 0x10000) >> 10) + 0xD800);
123
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
15 wchar_t low = (wchar_t)(((codepoint - 0x10000) & 0b1111111111) + 0xDC00);
124
125
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 *out = std::wstring() + high + low;
126 15 return true;
127 }
128
1/2
✓ Branch 0 taken 290788 times.
✗ Branch 1 not taken.
290788 else if (codepoint <= 0xD800 || codepoint >= 0xDFFF)
129 {
130
1/2
✓ Branch 0 taken 290788 times.
✗ Branch 1 not taken.
290788 *out = std::wstring() + (wchar_t)codepoint;
131 290788 return true;
132 }
133
134 // Everything not covered above should be considered invalid.
135 return false;
136 }
137
138
139 1180 bool utf16_to_utf8(string* result, const wchar_t* u16_str)
140 {
141 *result = "";
142
143 int codepoint;
144 do
145 {
146 30104 u16_str += utf16_val(&codepoint, u16_str);
147
148 string next;
149
2/4
✓ Branch 0 taken 30104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30104 times.
30104 if (codepoint == -1 || !codepoint_to_utf8(&next, codepoint)) return false;
150
151 30104 *result += next;
152
2/2
✓ Branch 0 taken 28924 times.
✓ Branch 1 taken 1180 times.
30104 } while (codepoint != 0);
153
154 return true;
155 }
156
157 6526 bool utf8_to_utf16(std::wstring* result, const char* u8_str)
158 {
159 6526 *result = L"";
160
161 int codepoint;
162 do
163 {
164
1/2
✓ Branch 0 taken 290803 times.
✗ Branch 1 not taken.
290803 u8_str += utf8_val(&codepoint, u8_str);
165
166 std::wstring next;
167
3/6
✓ Branch 0 taken 290803 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 290803 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 290803 times.
290803 if (codepoint == -1 || !codepoint_to_utf16(&next, codepoint)) return false;
168
169
1/2
✓ Branch 0 taken 290803 times.
✗ Branch 1 not taken.
290803 *result += next;
170
2/2
✓ Branch 0 taken 284277 times.
✓ Branch 1 taken 6526 times.
290803 } while (codepoint != 0);
171
172 return true;
173 }
174