Repository navigation
Expand file tree
/
Copy pathbackend_json.cpp
More file actions
101 lines (88 loc) · 3.02 KB
/
Copy pathbackend_json.cpp
File metadata and controls
101 lines (88 loc) · 3.02 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
#include "bench.h"
#include <nlohmann/json.hpp>
namespace {
const char B64_ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string base64_encode(const uint8_t* data, size_t len) {
std::string out;
out.reserve(((len + 2) / 3) * 4);
for (size_t i = 0; i < len; i += 3) {
uint32_t n = uint32_t(data[i]) << 16;
if (i + 1 < len) n |= uint32_t(data[i + 1]) << 8;
if (i + 2 < len) n |= uint32_t(data[i + 2]);
out.push_back(B64_ALPHABET[(n >> 18) & 63]);
out.push_back(B64_ALPHABET[(n >> 12) & 63]);
out.push_back(i + 1 < len ? B64_ALPHABET[(n >> 6) & 63] : '=');
out.push_back(i + 2 < len ? B64_ALPHABET[n & 63] : '=');
}
return out;
}
int b64_val(char c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
}
std::string base64_decode(const std::string& s) {
std::string out;
uint32_t buf = 0;
int bits = 0;
for (char c : s) {
if (c == '=' || c == '\n' || c == '\r') continue;
int v = b64_val(c);
if (v < 0) continue;
buf = (buf << 6) | uint32_t(v);
bits += 6;
if (bits >= 8) {
bits -= 8;
out.push_back(char((buf >> bits) & 0xFF));
}
}
return out;
}
} // namespace
class JSONBackend : public Backend {
public:
const char* name() const override { return "nlohmann/json"; }
void reset() override {
m_doc = nlohmann::json::object();
}
void populate(size_t params_count, bool with_attachment, size_t attachment_bytes) override {
if (m_pool.size() != params_count)
m_pool = make_kvs(params_count);
nlohmann::json params = nlohmann::json::object();
for (const KV& kv : m_pool) {
switch (kv.type) {
case KV::Type::Double: params[kv.key] = kv.d; break;
case KV::Type::Int: params[kv.key] = kv.i; break;
case KV::Type::Bool: params[kv.key] = kv.b; break;
case KV::Type::Str: params[kv.key] = kv.s; break;
}
}
m_doc["params"] = std::move(params);
if (with_attachment) {
m_blob.assign(attachment_bytes, 0xAB);
m_doc["attachment"] = base64_encode(m_blob.data(), m_blob.size());
}
}
void serialize(std::vector<uint8_t>& out) override {
m_text = m_doc.dump();
out.assign(m_text.begin(), m_text.end());
}
void deserialize(const uint8_t* data, size_t size) override {
m_recv = nlohmann::json::parse(data, data + size);
if (m_recv.contains("attachment")) {
base64_decode(m_recv["attachment"].get<std::string>());
}
}
private:
nlohmann::json m_doc;
nlohmann::json m_recv;
std::string m_text;
std::vector<uint8_t> m_blob;
std::vector<KV> m_pool;
};
Backend* make_json_backend() {
return new JSONBackend();
}