1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/rpc/json_binary_proxy.cpp
dan 13b01c86a6 Updated RpcServer Initialization and Logic
-- Moved all RPCServer initialization logic to rpcserver constructor
    -- Fixed config logic, fxn binding to rpc address, fxn adding rpc cats
    -- router hive failed CI/CD resulting from outdated reference to rpcBindAddr
    -- ipc socket as default hidden from windows (for now)
refactored config endpoint
    - added rpc call script (contrib/omq-rpc.py)
    - added new fxns to .ini config stuff
    - added delete .ini file functionality to config endpoint
    - added edge case control for config endpoint

add commented out line in clang-form for header reorg later
2023-01-24 06:50:46 -08:00

63 lines
1.8 KiB
C++

#include "json_binary_proxy.hpp"
#include <oxenc/hex.h>
#include <oxenc/base64.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