diff options
author | Lee Clagett <code@leeclagett.com> | 2019-11-17 04:29:18 +0000 |
---|---|---|
committer | Lee Clagett <code@leeclagett.com> | 2020-03-09 05:23:59 +0000 |
commit | 5fcc23ae0a08e6846a4d0fbfdb51627559929068 (patch) | |
tree | c2ab745028c3d5ce9c3e404392132400dcf2acf3 /contrib/epee/src | |
parent | Remove temporary std::string creation in some hex->bin calls (diff) | |
download | monero-5fcc23ae0a08e6846a4d0fbfdb51627559929068.tar.xz |
Move hex->bin conversion to monero copyright files and with less includes
Diffstat (limited to 'contrib/epee/src')
-rw-r--r-- | contrib/epee/src/hex.cpp | 41 |
1 files changed, 39 insertions, 2 deletions
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 <ostream> #include <stdexcept> +#include "storages/parserse_base_utils.h" + namespace epee { namespace @@ -84,7 +86,42 @@ namespace epee 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 auto include = [](char input) { |