Merge pull request #6351
81c5943
Remove temporary std::string creation in some hex->bin calls (vtnerd)5fcc23a
Move hex->bin conversion to monero copyright files and with less includes (vtnerd)3387f0e
Reduce template bloat in hex->bin for ZMQ json (vtnerd)
This commit is contained in:
commit
cfc0f4a7fa
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2017-2019, The Monero Project
|
// Copyright (c) 2017-2020, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
|
@ -80,9 +80,20 @@ namespace epee
|
||||||
static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept;
|
static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//! Convert hex in UTF8 encoding to binary
|
||||||
struct from_hex
|
struct from_hex
|
||||||
{
|
{
|
||||||
//! \return An std::vector of unsigned integers from the `src`
|
static bool to_string(std::string& out, boost::string_ref src);
|
||||||
static std::vector<uint8_t> vector(boost::string_ref src);
|
|
||||||
|
static bool to_buffer(span<std::uint8_t> out, boost::string_ref src) noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static bool to_buffer_unchecked(std::uint8_t* out, boost::string_ref src) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Convert hex in current C locale encoding to binary
|
||||||
|
struct from_hex_locale
|
||||||
|
{
|
||||||
|
static std::vector<uint8_t> to_vector(boost::string_ref src);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <boost/utility/string_ref.hpp>
|
#include <boost/utility/string_ref.hpp>
|
||||||
|
|
||||||
|
#include "misc_log_ex.h"
|
||||||
|
|
||||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||||
#define MONERO_DEFAULT_LOG_CATEGORY "serialization"
|
#define MONERO_DEFAULT_LOG_CATEGORY "serialization"
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
#include <boost/algorithm/string/predicate.hpp>
|
#include <boost/algorithm/string/predicate.hpp>
|
||||||
|
#include <boost/utility/string_ref.hpp>
|
||||||
#include "misc_log_ex.h"
|
#include "misc_log_ex.h"
|
||||||
#include "storages/parserse_base_utils.h"
|
#include "storages/parserse_base_utils.h"
|
||||||
#include "hex.h"
|
#include "hex.h"
|
||||||
|
@ -69,34 +70,9 @@ namespace string_tools
|
||||||
return to_hex::string(to_byte_span(to_span(src)));
|
return to_hex::string(to_byte_span(to_span(src)));
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
inline bool parse_hexstr_to_binbuff(const epee::span<const char> s, epee::span<char>& res)
|
inline bool parse_hexstr_to_binbuff(const boost::string_ref s, std::string& res)
|
||||||
{
|
{
|
||||||
if (s.size() != res.size() * 2)
|
return from_hex::to_string(res, s);
|
||||||
return false;
|
|
||||||
|
|
||||||
unsigned char *dst = (unsigned char *)&res[0];
|
|
||||||
const unsigned char *src = (const unsigned char *)s.data();
|
|
||||||
for(size_t i = 0; i < s.size(); i += 2)
|
|
||||||
{
|
|
||||||
int tmp = *src++;
|
|
||||||
tmp = epee::misc_utils::parse::isx[tmp];
|
|
||||||
if (tmp == 0xff) return false;
|
|
||||||
int t2 = *src++;
|
|
||||||
t2 = epee::misc_utils::parse::isx[t2];
|
|
||||||
if (t2 == 0xff) return false;
|
|
||||||
*dst++ = (tmp << 4) | t2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//----------------------------------------------------------------------------
|
|
||||||
inline bool parse_hexstr_to_binbuff(const std::string& s, std::string& res)
|
|
||||||
{
|
|
||||||
if (s.size() & 1)
|
|
||||||
return false;
|
|
||||||
res.resize(s.size() / 2);
|
|
||||||
epee::span<char> rspan((char*)&res[0], res.size());
|
|
||||||
return parse_hexstr_to_binbuff(epee::to_span(s), rspan);
|
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
PUSH_WARNINGS
|
PUSH_WARNINGS
|
||||||
|
@ -303,23 +279,20 @@ POP_WARNINGS
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
template<class t_pod_type>
|
template<class t_pod_type>
|
||||||
bool hex_to_pod(const std::string& hex_str, t_pod_type& s)
|
bool hex_to_pod(const boost::string_ref hex_str, t_pod_type& s)
|
||||||
{
|
{
|
||||||
static_assert(std::is_pod<t_pod_type>::value, "expected pod type");
|
static_assert(std::is_standard_layout<t_pod_type>(), "expected standard layout type");
|
||||||
if(sizeof(s)*2 != hex_str.size())
|
return from_hex::to_buffer(as_mut_byte_span(s), hex_str);
|
||||||
return false;
|
|
||||||
epee::span<char> rspan((char*)&s, sizeof(s));
|
|
||||||
return parse_hexstr_to_binbuff(epee::to_span(hex_str), rspan);
|
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
template<class t_pod_type>
|
template<class t_pod_type>
|
||||||
bool hex_to_pod(const std::string& hex_str, tools::scrubbed<t_pod_type>& s)
|
bool hex_to_pod(const boost::string_ref hex_str, tools::scrubbed<t_pod_type>& s)
|
||||||
{
|
{
|
||||||
return hex_to_pod(hex_str, unwrap(s));
|
return hex_to_pod(hex_str, unwrap(s));
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
template<class t_pod_type>
|
template<class t_pod_type>
|
||||||
bool hex_to_pod(const std::string& hex_str, epee::mlocked<t_pod_type>& s)
|
bool hex_to_pod(const boost::string_ref hex_str, epee::mlocked<t_pod_type>& s)
|
||||||
{
|
{
|
||||||
return hex_to_pod(hex_str, unwrap(s));
|
return hex_to_pod(hex_str, unwrap(s));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2017-2019, The Monero Project
|
// Copyright (c) 2017-2020, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
|
@ -33,6 +33,8 @@
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "storages/parserse_base_utils.h"
|
||||||
|
|
||||||
namespace epee
|
namespace epee
|
||||||
{
|
{
|
||||||
namespace
|
namespace
|
||||||
|
@ -84,7 +86,42 @@ namespace epee
|
||||||
return write_hex(out, src);
|
return write_hex(out, src);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> from_hex::vector(boost::string_ref src)
|
|
||||||
|
bool from_hex::to_string(std::string& out, const boost::string_ref src)
|
||||||
|
{
|
||||||
|
out.resize(src.size() / 2);
|
||||||
|
return to_buffer_unchecked(reinterpret_cast<std::uint8_t*>(&out[0]), src);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool from_hex::to_buffer(span<std::uint8_t> out, const boost::string_ref src) noexcept
|
||||||
|
{
|
||||||
|
if (src.size() / 2 != out.size())
|
||||||
|
return false;
|
||||||
|
return to_buffer_unchecked(out.data(), src);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool from_hex::to_buffer_unchecked(std::uint8_t* dst, const boost::string_ref s) noexcept
|
||||||
|
{
|
||||||
|
if (s.size() % 2 != 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const unsigned char *src = (const unsigned char *)s.data();
|
||||||
|
for(size_t i = 0; i < s.size(); i += 2)
|
||||||
|
{
|
||||||
|
int tmp = *src++;
|
||||||
|
tmp = epee::misc_utils::parse::isx[tmp];
|
||||||
|
if (tmp == 0xff) return false;
|
||||||
|
int t2 = *src++;
|
||||||
|
t2 = epee::misc_utils::parse::isx[t2];
|
||||||
|
if (t2 == 0xff) return false;
|
||||||
|
*dst++ = (tmp << 4) | t2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<uint8_t> from_hex_locale::to_vector(const boost::string_ref src)
|
||||||
{
|
{
|
||||||
// should we include a specific character
|
// should we include a specific character
|
||||||
auto include = [](char input) {
|
auto include = [](char input) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2014-2019, The Monero Project
|
// Copyright (c) 2014-2020, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
|
@ -51,7 +51,7 @@ namespace cryptonote
|
||||||
const std::vector<std::string> ssl_allowed_fingerprints = command_line::get_arg(vm, arg.rpc_ssl_allowed_fingerprints);
|
const std::vector<std::string> ssl_allowed_fingerprints = command_line::get_arg(vm, arg.rpc_ssl_allowed_fingerprints);
|
||||||
|
|
||||||
std::vector<std::vector<uint8_t>> allowed_fingerprints{ ssl_allowed_fingerprints.size() };
|
std::vector<std::vector<uint8_t>> allowed_fingerprints{ ssl_allowed_fingerprints.size() };
|
||||||
std::transform(ssl_allowed_fingerprints.begin(), ssl_allowed_fingerprints.end(), allowed_fingerprints.begin(), epee::from_hex::vector);
|
std::transform(ssl_allowed_fingerprints.begin(), ssl_allowed_fingerprints.end(), allowed_fingerprints.begin(), epee::from_hex_locale::to_vector);
|
||||||
for (const auto &fpr: allowed_fingerprints)
|
for (const auto &fpr: allowed_fingerprints)
|
||||||
{
|
{
|
||||||
if (fpr.size() != SSL_FINGERPRINT_SIZE)
|
if (fpr.size() != SSL_FINGERPRINT_SIZE)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2016-2019, The Monero Project
|
// Copyright (c) 2016-2020, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
|
@ -32,7 +32,11 @@
|
||||||
#include <boost/variant/apply_visitor.hpp>
|
#include <boost/variant/apply_visitor.hpp>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include "string_tools.h"
|
|
||||||
|
// drop macro from windows.h
|
||||||
|
#ifdef GetObject
|
||||||
|
#undef GetObject
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace cryptonote
|
namespace cryptonote
|
||||||
{
|
{
|
||||||
|
@ -109,6 +113,19 @@ namespace
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void read_hex(const rapidjson::Value& val, epee::span<std::uint8_t> dest)
|
||||||
|
{
|
||||||
|
if (!val.IsString())
|
||||||
|
{
|
||||||
|
throw WRONG_TYPE("string");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!epee::from_hex::to_buffer(dest, {val.GetString(), val.Size()}))
|
||||||
|
{
|
||||||
|
throw BAD_INPUT();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const rapidjson::Value& src)
|
void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const rapidjson::Value& src)
|
||||||
{
|
{
|
||||||
src.Accept(dest);
|
src.Accept(dest);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2016-2019, The Monero Project
|
// Copyright (c) 2016-2020, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
|
@ -30,7 +30,6 @@
|
||||||
|
|
||||||
#include <boost/utility/string_ref.hpp>
|
#include <boost/utility/string_ref.hpp>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "string_tools.h" // out of order because windows.h GetObject macro conflicts with GenericValue<..>::GetObject()
|
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
#include <rapidjson/stringbuffer.h>
|
#include <rapidjson/stringbuffer.h>
|
||||||
#include <rapidjson/writer.h>
|
#include <rapidjson/writer.h>
|
||||||
|
@ -39,6 +38,8 @@
|
||||||
#include "rpc/message_data_structs.h"
|
#include "rpc/message_data_structs.h"
|
||||||
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
|
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
|
||||||
#include "common/sfinae_helpers.h"
|
#include "common/sfinae_helpers.h"
|
||||||
|
#include "hex.h"
|
||||||
|
#include "span.h"
|
||||||
|
|
||||||
#define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
|
#define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
|
||||||
do \
|
do \
|
||||||
|
@ -118,6 +119,8 @@ inline constexpr bool is_to_hex()
|
||||||
return std::is_pod<Type>() && !std::is_integral<Type>();
|
return std::is_pod<Type>() && !std::is_integral<Type>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void read_hex(const rapidjson::Value& val, epee::span<std::uint8_t> dest);
|
||||||
|
|
||||||
// POD to json key
|
// POD to json key
|
||||||
template <class Type>
|
template <class Type>
|
||||||
inline typename std::enable_if<is_to_hex<Type>()>::type toJsonKey(rapidjson::Writer<rapidjson::StringBuffer>& dest, const Type& pod)
|
inline typename std::enable_if<is_to_hex<Type>()>::type toJsonKey(rapidjson::Writer<rapidjson::StringBuffer>& dest, const Type& pod)
|
||||||
|
@ -137,18 +140,8 @@ inline typename std::enable_if<is_to_hex<Type>()>::type toJsonValue(rapidjson::W
|
||||||
template <class Type>
|
template <class Type>
|
||||||
inline typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t)
|
inline typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t)
|
||||||
{
|
{
|
||||||
if (!val.IsString())
|
static_assert(std::is_standard_layout<Type>(), "expected standard layout type");
|
||||||
{
|
json::read_hex(val, epee::as_mut_byte_span(t));
|
||||||
throw WRONG_TYPE("string");
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: handle failure to convert hex string to POD type
|
|
||||||
bool success = epee::string_tools::hex_to_pod(val.GetString(), t);
|
|
||||||
|
|
||||||
if (!success)
|
|
||||||
{
|
|
||||||
throw BAD_INPUT();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const rapidjson::Value& src);
|
void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const rapidjson::Value& src);
|
||||||
|
|
|
@ -355,7 +355,7 @@ std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variabl
|
||||||
else if (!daemon_ssl_ca_file.empty() || !daemon_ssl_allowed_fingerprints.empty())
|
else if (!daemon_ssl_ca_file.empty() || !daemon_ssl_allowed_fingerprints.empty())
|
||||||
{
|
{
|
||||||
std::vector<std::vector<uint8_t>> ssl_allowed_fingerprints{ daemon_ssl_allowed_fingerprints.size() };
|
std::vector<std::vector<uint8_t>> ssl_allowed_fingerprints{ daemon_ssl_allowed_fingerprints.size() };
|
||||||
std::transform(daemon_ssl_allowed_fingerprints.begin(), daemon_ssl_allowed_fingerprints.end(), ssl_allowed_fingerprints.begin(), epee::from_hex::vector);
|
std::transform(daemon_ssl_allowed_fingerprints.begin(), daemon_ssl_allowed_fingerprints.end(), ssl_allowed_fingerprints.begin(), epee::from_hex_locale::to_vector);
|
||||||
for (const auto &fpr: ssl_allowed_fingerprints)
|
for (const auto &fpr: ssl_allowed_fingerprints)
|
||||||
{
|
{
|
||||||
THROW_WALLET_EXCEPTION_IF(fpr.size() != SSL_FINGERPRINT_SIZE, tools::error::wallet_internal_error,
|
THROW_WALLET_EXCEPTION_IF(fpr.size() != SSL_FINGERPRINT_SIZE, tools::error::wallet_internal_error,
|
||||||
|
|
|
@ -824,14 +824,14 @@ TEST(ToHex, String)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FromHex, String)
|
TEST(HexLocale, String)
|
||||||
{
|
{
|
||||||
// the source data to encode and decode
|
// the source data to encode and decode
|
||||||
std::vector<uint8_t> source{{ 0x00, 0xFF, 0x0F, 0xF0 }};
|
std::vector<uint8_t> source{{ 0x00, 0xFF, 0x0F, 0xF0 }};
|
||||||
|
|
||||||
// encode and decode the data
|
// encode and decode the data
|
||||||
auto hex = epee::to_hex::string({ source.data(), source.size() });
|
auto hex = epee::to_hex::string({ source.data(), source.size() });
|
||||||
auto decoded = epee::from_hex::vector(hex);
|
auto decoded = epee::from_hex_locale::to_vector(hex);
|
||||||
|
|
||||||
// encoded should be twice the size and should decode to the exact same data
|
// encoded should be twice the size and should decode to the exact same data
|
||||||
EXPECT_EQ(source.size() * 2, hex.size());
|
EXPECT_EQ(source.size() * 2, hex.size());
|
||||||
|
@ -840,7 +840,7 @@ TEST(FromHex, String)
|
||||||
// we will now create a padded hex string, we want to explicitly allow
|
// we will now create a padded hex string, we want to explicitly allow
|
||||||
// decoding it this way also, ignoring spaces and colons between the numbers
|
// decoding it this way also, ignoring spaces and colons between the numbers
|
||||||
hex.assign("00:ff 0f:f0");
|
hex.assign("00:ff 0f:f0");
|
||||||
EXPECT_EQ(source, epee::from_hex::vector(hex));
|
EXPECT_EQ(source, epee::from_hex_locale::to_vector(hex));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ToHex, Array)
|
TEST(ToHex, Array)
|
||||||
|
@ -902,6 +902,46 @@ TEST(ToHex, Formatted)
|
||||||
EXPECT_EQ(expected, out.str());
|
EXPECT_EQ(expected, out.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(FromHex, ToString)
|
||||||
|
{
|
||||||
|
static constexpr const char hex[] = u8"deadbeeffY";
|
||||||
|
static constexpr const char binary[] = {
|
||||||
|
char(0xde), char(0xad), char(0xbe), char(0xef), 0x00
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string out{};
|
||||||
|
EXPECT_FALSE(epee::from_hex::to_string(out, hex));
|
||||||
|
|
||||||
|
boost::string_ref portion{hex};
|
||||||
|
portion.remove_suffix(1);
|
||||||
|
EXPECT_FALSE(epee::from_hex::to_string(out, portion));
|
||||||
|
|
||||||
|
portion.remove_suffix(1);
|
||||||
|
EXPECT_TRUE(epee::from_hex::to_string(out, portion));
|
||||||
|
EXPECT_EQ(std::string{binary}, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(FromHex, ToBuffer)
|
||||||
|
{
|
||||||
|
static constexpr const char hex[] = u8"deadbeeffY";
|
||||||
|
static constexpr const std::uint8_t binary[] = {0xde, 0xad, 0xbe, 0xef};
|
||||||
|
|
||||||
|
std::vector<std::uint8_t> out{};
|
||||||
|
out.resize(sizeof(binary));
|
||||||
|
EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), hex));
|
||||||
|
|
||||||
|
boost::string_ref portion{hex};
|
||||||
|
portion.remove_suffix(1);
|
||||||
|
EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion));
|
||||||
|
|
||||||
|
portion.remove_suffix(1);
|
||||||
|
EXPECT_FALSE(epee::from_hex::to_buffer({out.data(), out.size() - 1}, portion));
|
||||||
|
|
||||||
|
EXPECT_TRUE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion));
|
||||||
|
const std::vector<std::uint8_t> expected{std::begin(binary), std::end(binary)};
|
||||||
|
EXPECT_EQ(expected, out);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(StringTools, BuffToHex)
|
TEST(StringTools, BuffToHex)
|
||||||
{
|
{
|
||||||
const std::vector<unsigned char> all_bytes = get_all_bytes();
|
const std::vector<unsigned char> all_bytes = get_all_bytes();
|
||||||
|
|
Loading…
Reference in New Issue