-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathBase64.cpp
More file actions
185 lines (163 loc) · 5.08 KB
/
Copy pathBase64.cpp
File metadata and controls
185 lines (163 loc) · 5.08 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "Base64.h"
#include <vector>
#include "BuiltinLoader.h"
#include "Helpers.h"
using namespace v8;
namespace tns {
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 == ' ';
}
// The string's code units as bytes. Fails when any unit is above U+00FF,
// which neither op can represent.
bool GetLatin1Bytes(Isolate* isolate, Local<Value> value,
std::vector<uint8_t>* out) {
if (!value->IsString()) {
return false;
}
Local<v8::String> str = value.As<v8::String>();
if (!str->ContainsOnlyOneByte()) {
return false;
}
const int length = str->Length();
out->resize(static_cast<size_t>(length));
if (length > 0) {
str->WriteOneByteV2(isolate, 0, static_cast<uint32_t>(length), out->data());
}
return true;
}
// btoa: base64-encode the input's code units.
void BtoaCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
std::vector<uint8_t> input;
if (!GetLatin1Bytes(isolate, info[0], &input)) {
info.GetReturnValue().SetNull();
return;
}
std::vector<uint8_t> out;
out.reserve((input.size() + 2) / 3 * 4);
size_t i = 0;
for (; i + 3 <= input.size(); i += 3) {
const uint32_t group = (static_cast<uint32_t>(input[i]) << 16) |
(static_cast<uint32_t>(input[i + 1]) << 8) |
input[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 = input.size() - i;
if (remaining == 1) {
const uint32_t group = static_cast<uint32_t>(input[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>(input[i]) << 16) |
(static_cast<uint32_t>(input[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('=');
}
if (out.empty()) {
info.GetReturnValue().Set(v8::String::Empty(isolate));
return;
}
Local<v8::String> result;
if (v8::String::NewFromOneByte(isolate, out.data(), NewStringType::kNormal,
static_cast<int>(out.size()))
.ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
// atob: forgiving-base64 decode
// (https://infra.spec.whatwg.org/#forgiving-base64-decode).
void AtobCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
std::vector<uint8_t> raw;
if (!GetLatin1Bytes(isolate, info[0], &raw)) {
info.GetReturnValue().SetNull();
return;
}
std::vector<uint8_t> data;
data.reserve(raw.size());
for (uint8_t c : raw) {
if (!IsAsciiWhitespace(c)) {
data.push_back(c);
}
}
if (data.size() % 4 == 0) {
size_t strip = 0;
while (strip < 2 && !data.empty() && data.back() == '=') {
data.pop_back();
strip++;
}
}
if (data.size() % 4 == 1) {
info.GetReturnValue().SetNull();
return;
}
std::vector<uint8_t> out;
out.reserve(data.size() / 4 * 3 + 2);
uint32_t accumulator = 0;
uint32_t bits = 0;
for (uint8_t c : data) {
const uint8_t value = SixBits(c);
if (value == kInvalid) {
info.GetReturnValue().SetNull();
return;
}
accumulator = (accumulator << 6) | value;
bits += 6;
if (bits >= 8) {
bits -= 8;
out.push_back(static_cast<uint8_t>((accumulator >> bits) & 0xFF));
}
}
if (out.empty()) {
info.GetReturnValue().Set(v8::String::Empty(isolate));
return;
}
Local<v8::String> result;
if (v8::String::NewFromOneByte(isolate, out.data(), NewStringType::kNormal,
static_cast<int>(out.size()))
.ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
MaybeLocal<Object> CreateBinding(Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<Object> binding = Object::New(isolate);
tns::SetMethodNoSideEffect(context, binding, "btoa", BtoaCallback);
tns::SetMethodNoSideEffect(context, binding, "atob", AtobCallback);
return binding;
}
} // namespace
MaybeLocal<Object> Base64::GetExports(Local<Context> context) {
return BuiltinLoader::GetExports(context, BuiltinId::kBase64, CreateBinding);
}
} // namespace tns