mirror of
https://github.com/monero-project/monero.git
synced 2025-08-03 19:34:16 -04:00
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
9 changed files with 135 additions and 62 deletions
|
@ -1,4 +1,4 @@
|
|||
// Copyright (c) 2017-2019, The Monero Project
|
||||
// Copyright (c) 2017-2020, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
|
@ -80,9 +80,20 @@ namespace epee
|
|||
static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept;
|
||||
};
|
||||
|
||||
//! Convert hex in UTF8 encoding to binary
|
||||
struct from_hex
|
||||
{
|
||||
//! \return An std::vector of unsigned integers from the `src`
|
||||
static std::vector<uint8_t> vector(boost::string_ref src);
|
||||
static bool to_string(std::string& out, 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 <boost/utility/string_ref.hpp>
|
||||
|
||||
#include "misc_log_ex.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "serialization"
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
#include <type_traits>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
#include "misc_log_ex.h"
|
||||
#include "storages/parserse_base_utils.h"
|
||||
#include "hex.h"
|
||||
|
@ -69,34 +70,9 @@ namespace string_tools
|
|||
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 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);
|
||||
return from_hex::to_string(res, s);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
PUSH_WARNINGS
|
||||
|
@ -303,23 +279,20 @@ POP_WARNINGS
|
|||
}
|
||||
//----------------------------------------------------------------------------
|
||||
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");
|
||||
if(sizeof(s)*2 != hex_str.size())
|
||||
return false;
|
||||
epee::span<char> rspan((char*)&s, sizeof(s));
|
||||
return parse_hexstr_to_binbuff(epee::to_span(hex_str), rspan);
|
||||
static_assert(std::is_standard_layout<t_pod_type>(), "expected standard layout type");
|
||||
return from_hex::to_buffer(as_mut_byte_span(s), hex_str);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
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));
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
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));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue