-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTNBase64HelperModule.cpp
More file actions
43 lines (33 loc) · 1.19 KB
/
Copy pathRTNBase64HelperModule.cpp
File metadata and controls
43 lines (33 loc) · 1.19 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
#include <string>
#include <vector>
#include "RTNBase64HelperModule.h"
namespace facebook::react {
RTNBase64HelperModule::RTNBase64HelperModule(std::shared_ptr<CallInvoker> jsInvoker)
: NativeRTNBase64HelperCxxSpec(std::move(jsInvoker)) {}
jsi::String RTNBase64HelperModule::base64EncodeSync(jsi::Runtime &rt, jsi::String str) {
auto data = str.utf8(rt);
auto result = base64_encode(data);
return jsi::String::createFromUtf8(rt, result);
}
std::string RTNBase64HelperModule::base64_encode(const std::string &input) {
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string ret;
int val = 0, valb = -6;
for (unsigned char c: input) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
ret.push_back(base64_chars[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) {
ret.push_back('=');
if (valb > -4) ret.push_back('=');
}
return ret;
}
} // namespace facebook::react