Repository navigation
Expand file tree
/
Copy pathBase64.cpp
More file actions
109 lines (97 loc) · 2.94 KB
/
Copy pathBase64.cpp
File metadata and controls
109 lines (97 loc) · 2.94 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
#include "ns/Base64.h"
namespace nativescript::common::base64 {
namespace {
constexpr char kAlphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// 6-bit value per ASCII byte; 0xFF marks everything outside the alphabet.
constexpr uint8_t kInvalid = 0xFF;
uint8_t SixBits(uint8_t c) {
if (c >= 'A' && c <= 'Z') {
return static_cast<uint8_t>(c - 'A');
}
if (c >= 'a' && c <= 'z') {
return static_cast<uint8_t>(c - 'a' + 26);
}
if (c >= '0' && c <= '9') {
return static_cast<uint8_t>(c - '0' + 52);
}
if (c == '+') {
return 62;
}
if (c == '/') {
return 63;
}
return kInvalid;
}
bool IsAsciiWhitespace(uint8_t c) {
return c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == ' ';
}
} // namespace
std::string Encode(const uint8_t* data, size_t length) {
std::string out;
out.reserve((length + 2) / 3 * 4);
size_t i = 0;
for (; i + 3 <= length; i += 3) {
const uint32_t group = (static_cast<uint32_t>(data[i]) << 16) |
(static_cast<uint32_t>(data[i + 1]) << 8) |
data[i + 2];
out.push_back(kAlphabet[(group >> 18) & 0x3F]);
out.push_back(kAlphabet[(group >> 12) & 0x3F]);
out.push_back(kAlphabet[(group >> 6) & 0x3F]);
out.push_back(kAlphabet[group & 0x3F]);
}
const size_t remaining = length - i;
if (remaining == 1) {
const uint32_t group = static_cast<uint32_t>(data[i]) << 16;
out.push_back(kAlphabet[(group >> 18) & 0x3F]);
out.push_back(kAlphabet[(group >> 12) & 0x3F]);
out.push_back('=');
out.push_back('=');
} else if (remaining == 2) {
const uint32_t group = (static_cast<uint32_t>(data[i]) << 16) |
(static_cast<uint32_t>(data[i + 1]) << 8);
out.push_back(kAlphabet[(group >> 18) & 0x3F]);
out.push_back(kAlphabet[(group >> 12) & 0x3F]);
out.push_back(kAlphabet[(group >> 6) & 0x3F]);
out.push_back('=');
}
return out;
}
std::optional<std::vector<uint8_t>> ForgivingDecode(const uint8_t* data,
size_t length) {
std::vector<uint8_t> input;
input.reserve(length);
for (size_t i = 0; i < length; i++) {
if (!IsAsciiWhitespace(data[i])) {
input.push_back(data[i]);
}
}
if (input.size() % 4 == 0) {
size_t strip = 0;
while (strip < 2 && !input.empty() && input.back() == '=') {
input.pop_back();
strip++;
}
}
if (input.size() % 4 == 1) {
return std::nullopt;
}
std::vector<uint8_t> out;
out.reserve(input.size() / 4 * 3 + 2);
uint32_t accumulator = 0;
uint32_t bits = 0;
for (uint8_t c : input) {
const uint8_t value = SixBits(c);
if (value == kInvalid) {
return std::nullopt;
}
accumulator = (accumulator << 6) | value;
bits += 6;
if (bits >= 8) {
bits -= 8;
out.push_back(static_cast<uint8_t>((accumulator >> bits) & 0xFF));
}
}
return out;
}
} // namespace nativescript::common::base64