forked from ttalvitie/browservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
139 lines (117 loc) · 3.17 KB
/
Copy pathcommon.cpp
File metadata and controls
139 lines (117 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "common.hpp"
#include "include/wrapper/cef_closure_task.h"
#include "include/base/cef_callback.h"
namespace browservice {
namespace {
template <typename A, typename B>
void sanitizeUTF8StringImpl(const string& str, A byteHandler, B pointHandler) {
for(size_t i = 0; i < str.size(); ++i) {
int ch = (int)(uint8_t)str[i];
if(ch == 0) {
continue;
}
if(ch < 128) {
byteHandler(str.data() + i, 1);
pointHandler(ch);
continue;
}
size_t length;
if((ch & 0xE0) == 0xC0) {
length = 2;
} else if((ch & 0xF0) == 0xE0) {
length = 3;
} else if((ch & 0xF8) == 0xF0) {
length = 4;
} else {
continue;
}
if(i + length > str.size()) {
continue;
}
bool ok = true;
int point = ch & (0x7F >> length);
for(size_t j = 1; j < length; ++j) {
int ch2 = (int)(uint8_t)str[i + j];
if((ch2 & 0xC0) != 0x80) {
ok = false;
break;
}
point = (point << 6) | (ch2 & 0x3F);
}
if(!ok) {
continue;
}
if(
(length == (size_t)2 && point >= 0x80 && point <= 0x7FF) ||
(length == (size_t)3 && (
(point >= 0x800 && point <= 0xD7FF) ||
(point >= 0xE000 && point <= 0xFFFF)
)) ||
(length == (size_t)4 && point >= 0x10000 && point <= 0x10FFFF)
) {
byteHandler(str.data() + i, length);
pointHandler(point);
i += length - 1;
}
}
}
}
string sanitizeUTF8String(string str) {
string ret;
sanitizeUTF8StringImpl(
str,
[&](const char* bytes, size_t count) {
ret.insert(ret.end(), bytes, bytes + count);
},
[](int) {}
);
return ret;
}
vector<int> sanitizeUTF8StringToCodePoints(string str) {
vector<int> ret;
sanitizeUTF8StringImpl(
str,
[](const char*, size_t) {},
[&](int point) {
ret.push_back(point);
}
);
return ret;
}
mt19937 createRNG() {
static_assert(sizeof(random_device::result_type) == 4);
static_assert(mt19937::word_size == 32);
random_device dev;
array<uint32_t, mt19937::state_size> seedData;
for(uint32_t& val : seedData) {
val = dev();
}
seed_seq seedSeq(seedData.begin(), seedData.end());
return mt19937(seedSeq);
}
static atomic<bool> panicUsingCEFFatalError_(false);
void Panicker::panic_(string msg) {
stringstream output;
output << "PANIC @ " << location_;
if(!msg.empty()) {
output << ": " << msg;
}
output << "\n";
cerr << output.str();
cerr.flush();
if(panicUsingCEFFatalError_.load()) {
LOG(FATAL);
}
abort();
}
void enablePanicUsingCEFFatalError() {
panicUsingCEFFatalError_.store(true);
}
void postTask(function<void()> func) {
void (*call)(function<void()>) = [](function<void()> func) {
func();
};
CefPostTask(TID_UI, base::BindOnce(call, func));
}
atomic<bool> requireUIThreadEnabled_(false);
}