lokinet/llarp/rpc/json_binary_proxy.cpp
dr7ana f574cd798f Clang format include sorting + CMake
- includes are now sorted in consistent, logical order; first step in an attempt to fix the tomfoolery (no relation to Tom) brought in by include-what-you-use
- shuffled around some cmake linking to simplify dependency graph
- superfluous files removed
2024-01-31 07:54:12 -08:00

65 lines
1.8 KiB
C++

#include "json_binary_proxy.hpp"
#include <oxenc/base64.h>
#include <oxenc/hex.h>
namespace llarp::rpc
{
void
load_binary_parameter_impl(
std::string_view bytes, size_t raw_size, bool allow_raw, uint8_t* val_data)
{
if (allow_raw && bytes.size() == raw_size)
{
std::memcpy(val_data, bytes.data(), bytes.size());
return;
}
else if (bytes.size() == raw_size * 2)
{
if (oxenc::is_hex(bytes))
{
oxenc::from_hex(bytes.begin(), bytes.end(), val_data);
return;
}
}
else
{
const size_t b64_padded = (raw_size + 2) / 3 * 4;
const size_t b64_padding = raw_size % 3 == 1 ? 2 : raw_size % 3 == 2 ? 1 : 0;
const size_t b64_unpadded = b64_padded - b64_padding;
const std::string_view b64_padding_string = b64_padding == 2 ? "=="sv
: b64_padding == 1 ? "="sv
: ""sv;
if (bytes.size() == b64_unpadded
|| (b64_padding > 0 && bytes.size() == b64_padded
&& bytes.substr(b64_unpadded) == b64_padding_string))
{
if (oxenc::is_base64(bytes))
{
oxenc::from_base64(bytes.begin(), bytes.end(), val_data);
return;
}
}
}
throw std::runtime_error{"Invalid binary value: unexpected size and/or encoding"};
}
nlohmann::json&
json_binary_proxy::operator=(std::string_view binary_data)
{
switch (format)
{
case fmt::bt:
return e = binary_data;
case fmt::hex:
return e = oxenc::to_hex(binary_data);
case fmt::base64:
return e = oxenc::to_base64(binary_data);
}
throw std::runtime_error{"Internal error: invalid binary encoding"};
}
} // namespace llarp::rpc