From 81c5943453fa7039912edadf51e68901a013b6a5 Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Sun, 17 Nov 2019 04:26:32 +0000 Subject: Remove temporary std::string creation in some hex->bin calls --- contrib/epee/include/string_tools.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'contrib') diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index 319c0121b..f9581a4e4 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -42,6 +42,7 @@ #include #include #include +#include #include "misc_log_ex.h" #include "storages/parserse_base_utils.h" #include "hex.h" @@ -69,7 +70,7 @@ namespace string_tools return to_hex::string(to_byte_span(to_span(src))); } //---------------------------------------------------------------------------- - inline bool parse_hexstr_to_binbuff(const epee::span s, epee::span& res) + inline bool parse_hexstr_to_binbuff(const boost::string_ref s, epee::span res) { if (s.size() != res.size() * 2) return false; @@ -90,7 +91,7 @@ namespace string_tools return true; } //---------------------------------------------------------------------------- - inline bool parse_hexstr_to_binbuff(const std::string& s, std::string& res) + inline bool parse_hexstr_to_binbuff(const boost::string_ref s, std::string& res) { if (s.size() & 1) return false; @@ -303,7 +304,7 @@ POP_WARNINGS } //---------------------------------------------------------------------------- template - 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::value, "expected pod type"); if(sizeof(s)*2 != hex_str.size()) @@ -313,13 +314,13 @@ POP_WARNINGS } //---------------------------------------------------------------------------- template - bool hex_to_pod(const std::string& hex_str, tools::scrubbed& s) + bool hex_to_pod(const boost::string_ref hex_str, tools::scrubbed& s) { return hex_to_pod(hex_str, unwrap(s)); } //---------------------------------------------------------------------------- template - bool hex_to_pod(const std::string& hex_str, epee::mlocked& s) + bool hex_to_pod(const boost::string_ref hex_str, epee::mlocked& s) { return hex_to_pod(hex_str, unwrap(s)); } -- cgit v1.2.3 From 5fcc23ae0a08e6846a4d0fbfdb51627559929068 Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Sun, 17 Nov 2019 04:29:18 +0000 Subject: Move hex->bin conversion to monero copyright files and with less includes --- contrib/epee/include/hex.h | 17 +++++++-- .../epee/include/storages/parserse_base_utils.h | 2 ++ contrib/epee/include/string_tools.h | 34 ++---------------- contrib/epee/src/hex.cpp | 41 ++++++++++++++++++++-- 4 files changed, 58 insertions(+), 36 deletions(-) (limited to 'contrib') diff --git a/contrib/epee/include/hex.h b/contrib/epee/include/hex.h index 4e8f90786..8443cb92f 100644 --- a/contrib/epee/include/hex.h +++ b/contrib/epee/include/hex.h @@ -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 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 vector(boost::string_ref src); + static bool to_string(std::string& out, boost::string_ref src); + + static bool to_buffer(span 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 to_vector(boost::string_ref src); }; } diff --git a/contrib/epee/include/storages/parserse_base_utils.h b/contrib/epee/include/storages/parserse_base_utils.h index 8a498130c..2256f6b83 100644 --- a/contrib/epee/include/storages/parserse_base_utils.h +++ b/contrib/epee/include/storages/parserse_base_utils.h @@ -31,6 +31,8 @@ #include #include +#include "misc_log_ex.h" + #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "serialization" diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index f9581a4e4..2d9317d60 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -70,34 +70,9 @@ namespace string_tools return to_hex::string(to_byte_span(to_span(src))); } //---------------------------------------------------------------------------- - inline bool parse_hexstr_to_binbuff(const boost::string_ref s, epee::span 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 boost::string_ref s, std::string& res) { - if (s.size() & 1) - return false; - res.resize(s.size() / 2); - epee::span 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 @@ -306,11 +281,8 @@ POP_WARNINGS template bool hex_to_pod(const boost::string_ref hex_str, t_pod_type& s) { - static_assert(std::is_pod::value, "expected pod type"); - if(sizeof(s)*2 != hex_str.size()) - return false; - epee::span rspan((char*)&s, sizeof(s)); - return parse_hexstr_to_binbuff(epee::to_span(hex_str), rspan); + static_assert(std::is_standard_layout(), "expected standard layout type"); + return from_hex::to_buffer(as_mut_byte_span(s), hex_str); } //---------------------------------------------------------------------------- template diff --git a/contrib/epee/src/hex.cpp b/contrib/epee/src/hex.cpp index 558983f7e..b25ce5421 100644 --- a/contrib/epee/src/hex.cpp +++ b/contrib/epee/src/hex.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2019, The Monero Project +// Copyright (c) 2017-2020, The Monero Project // // All rights reserved. // @@ -33,6 +33,8 @@ #include #include +#include "storages/parserse_base_utils.h" + namespace epee { namespace @@ -84,7 +86,42 @@ namespace epee return write_hex(out, src); } - std::vector 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(&out[0]), src); + } + + bool from_hex::to_buffer(span 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 from_hex_locale::to_vector(const boost::string_ref src) { // should we include a specific character auto include = [](char input) { -- cgit v1.2.3