-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopenVCBBlueprint.cpp
83 lines (66 loc) · 2.76 KB
/
openVCBBlueprint.cpp
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
#include "openVCB.h"
namespace openVCB {
static const int B64index[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0,
0, 0, 0, 63, 0, 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 };
std::vector<unsigned char> b64decode(std::string data) {
const size_t len = data.length();
int pad = len > 0 && (len % 4 || data[len - 1] == '=');
const size_t size = ((len + 3) / 4 - pad) * 4;
std::vector<unsigned char> result(size / 4 * 3 + pad);
for (size_t i = 0, j = 0; i < size; i += 4) {
int n = B64index[data[i]] << 18 | B64index[data[i + 1]] << 12 | B64index[data[i + 2]] << 6 | B64index[data[i + 3]];
result[j++] = n >> 16;
result[j++] = n >> 8 & 0xFF;
result[j++] = n & 0xFF;
}
if (pad) {
int n = B64index[data[size]] << 18 | B64index[data[size + 1]] << 12;
result[result.size() - 1] = n >> 16;
if (len > size + 2 && data[size + 2] != '=') {
n |= B64index[data[size + 2]] << 6;
result.push_back(n >> 8 & 0xFF);
}
}
return result;
}
bool isBase64(std::string text) {
const size_t len = text.length();
//must be multiple of 4
if (len % 4 != 0) {
return false;
}
//valid characters only
for (size_t i = 0; i < len; i ++) {
char ch = text[i];
if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '/' || ch == '+' || (i >= len - 3 && ch == '='))) {
return false;
}
}
return true;
}
std::string removeWhitespace(std::string str) {
str.erase(remove_if(str.begin(), str.end(), std::isspace), str.end());
return str;
}
bool Project::readFromBlueprint(std::string clipboardData) {
clipboardData = removeWhitespace(clipboardData);
if (!isBase64(clipboardData)) {
return false;
}
std::vector<unsigned char> logicData = b64decode(clipboardData);
//check minimum size: zstd magic number [4] + vcb header [32]
if (logicData.size() <= 36) {
return false;
}
//check zstd magic number
if ((*(unsigned int*)&logicData[0]) != 0xFD2FB528) {
return false;
}
return Project::processLogicData(logicData, 32);
}
}