commit
38e86d4a07
23 files changed
+3219
No files matched your search
+116
@@ -0,0 +1,116 @@
|
||||
#include "echoion/protocol/Util.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Echoion::Protocol::Util
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr char kEnc[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
std::array<int8_t, 256> makeDecodeTable()
|
||||
{
|
||||
std::array<int8_t, 256> t{};
|
||||
t.fill(-1);
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
t[static_cast<uint8_t>(kEnc[i])] = static_cast<int8_t>(i);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
const std::array<int8_t, 256> kDec = makeDecodeTable();
|
||||
} // namespace
|
||||
|
||||
std::string base64Encode(ByteView data)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve((data.size() + 2) / 3 * 4);
|
||||
size_t i = 0;
|
||||
for (; i + 3 <= data.size(); i += 3) {
|
||||
uint32_t n = (static_cast<uint32_t>(data[i]) << 16) |
|
||||
(static_cast<uint32_t>(data[i + 1]) << 8) |
|
||||
static_cast<uint32_t>(data[i + 2]);
|
||||
out.push_back(kEnc[(n >> 18) & 0x3f]);
|
||||
out.push_back(kEnc[(n >> 12) & 0x3f]);
|
||||
out.push_back(kEnc[(n >> 6) & 0x3f]);
|
||||
out.push_back(kEnc[n & 0x3f]);
|
||||
}
|
||||
const size_t rem = data.size() - i;
|
||||
if (rem == 1) {
|
||||
uint32_t n = static_cast<uint32_t>(data[i]) << 16;
|
||||
out.push_back(kEnc[(n >> 18) & 0x3f]);
|
||||
out.push_back(kEnc[(n >> 12) & 0x3f]);
|
||||
out.push_back('=');
|
||||
out.push_back('=');
|
||||
} else if (rem == 2) {
|
||||
uint32_t n = (static_cast<uint32_t>(data[i]) << 16) |
|
||||
(static_cast<uint32_t>(data[i + 1]) << 8);
|
||||
out.push_back(kEnc[(n >> 18) & 0x3f]);
|
||||
out.push_back(kEnc[(n >> 12) & 0x3f]);
|
||||
out.push_back(kEnc[(n >> 6) & 0x3f]);
|
||||
out.push_back('=');
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Bytes base64Decode(const std::string& text)
|
||||
{
|
||||
Bytes out;
|
||||
out.reserve(text.size() / 4 * 3);
|
||||
uint32_t buf = 0;
|
||||
int bits = 0;
|
||||
for (char c : text) {
|
||||
if (c == '=' || c == '\n' || c == '\r' || c == ' ') {
|
||||
continue;
|
||||
}
|
||||
int8_t v = kDec[static_cast<uint8_t>(c)];
|
||||
if (v < 0) {
|
||||
throw std::runtime_error("invalid base64 character");
|
||||
}
|
||||
buf = (buf << 6) | static_cast<uint32_t>(v);
|
||||
bits += 6;
|
||||
if (bits >= 8) {
|
||||
bits -= 8;
|
||||
out.push_back(static_cast<uint8_t>((buf >> bits) & 0xff));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string toHex(ByteView data)
|
||||
{
|
||||
static const char* h = "0123456789abcdef";
|
||||
std::string out;
|
||||
out.reserve(data.size() * 2);
|
||||
for (uint8_t b : data) {
|
||||
out.push_back(h[b >> 4]);
|
||||
out.push_back(h[b & 0x0f]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Bytes fromHex(const std::string& hex)
|
||||
{
|
||||
auto nib = [](char c) -> int {
|
||||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
throw std::runtime_error("invalid hex character");
|
||||
};
|
||||
if (hex.size() % 2 != 0) {
|
||||
throw std::runtime_error("odd-length hex string");
|
||||
}
|
||||
Bytes out;
|
||||
out.reserve(hex.size() / 2);
|
||||
for (size_t i = 0; i < hex.size(); i += 2) {
|
||||
out.push_back(static_cast<uint8_t>((nib(hex[i]) << 4) |
|
||||
nib(hex[i + 1])));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Echoion::Protocol::Util
|
||||
Reference in new issue
Block a user