| 1 | // Copyright (c) 2017-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include "hex.h" |
| 30 | |
| 31 | #include <iterator> |
| 32 | #include <limits> |
| 33 | #include <ostream> |
| 34 | #include <stdexcept> |
| 35 | |
| 36 | #include "storages/parserse_base_utils.h" |
| 37 | |
| 38 | namespace epee |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | template<typename T> |
| 43 | void write_hex(T&& out, const span<const std::uint8_t> src) |
| 44 | { |
| 45 | static constexpr const char hex[] = u8"0123456789abcdef"; |
| 46 | static_assert(sizeof(hex) == 17, "bad string size"); |
| 47 | for (const std::uint8_t byte : src) |
| 48 | { |
| 49 | *out = hex[byte >> 4]; |
| 50 | ++out; |
| 51 | *out = hex[byte & 0x0F]; |
| 52 | ++out; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | template<typename T> |
| 58 | T to_hex::convert(const span<const std::uint8_t> src) |
| 59 | { |
| 60 | if (std::numeric_limits<std::size_t>::max() / 2 < src.size()) |
| 61 | throw std::range_error("hex_view::to_string exceeded maximum size"); |
| 62 | |
| 63 | T out{}; |
| 64 | out.resize(src.size() * 2); |
| 65 | to_hex::buffer_unchecked((char*)out.data(), src); // can't see the non const version in wipeable_string?? |
| 66 | return out; |
| 67 | } |
| 68 | |
| 69 | std::string to_hex::string(const span<const std::uint8_t> src) { return convert<std::string>(src); } |
| 70 | epee::wipeable_string to_hex::wipeable_string(const span<const std::uint8_t> src) { return convert<epee::wipeable_string>(src); } |
| 71 | |
| 72 | void to_hex::buffer(std::ostream& out, const span<const std::uint8_t> src) |
| 73 | { |
| 74 | write_hex(std::ostreambuf_iterator<char>{out}, src); |
| 75 | } |
| 76 | |
| 77 | void to_hex::formatted(std::ostream& out, const span<const std::uint8_t> src) |
| 78 | { |
| 79 | out.put('<'); |
| 80 | buffer(out, src); |
| 81 | out.put('>'); |
| 82 | } |
| 83 | |
| 84 | void to_hex::buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept |
| 85 | { |
| 86 | return write_hex(out, src); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | bool from_hex::to_string(std::string& out, const boost::string_ref src) |
| 91 | { |
| 92 | out.resize(src.size() / 2); |
| 93 | return to_buffer_unchecked(reinterpret_cast<std::uint8_t*>(&out[0]), src); |
| 94 | } |
| 95 | |
| 96 | bool from_hex::to_buffer(span<std::uint8_t> out, const boost::string_ref src) noexcept |
| 97 | { |
| 98 | if (src.size() / 2 != out.size()) |
| 99 | return false; |
| 100 | return to_buffer_unchecked(out.data(), src); |
| 101 | } |
| 102 | |
| 103 | bool from_hex::to_buffer_unchecked(std::uint8_t* dst, const boost::string_ref s) noexcept |
| 104 | { |
| 105 | if (s.size() % 2 != 0) |
| 106 | return false; |
| 107 | |
| 108 | const unsigned char *src = (const unsigned char *)s.data(); |
| 109 | for(size_t i = 0; i < s.size(); i += 2) |
| 110 | { |
| 111 | int tmp = *src++; |
| 112 | tmp = epee::misc_utils::parse::isx[tmp]; |
| 113 | if (tmp == 0xff) return false; |
| 114 | int t2 = *src++; |
| 115 | t2 = epee::misc_utils::parse::isx[t2]; |
| 116 | if (t2 == 0xff) return false; |
| 117 | *dst++ = (tmp << 4) | t2; |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | std::vector<uint8_t> from_hex_locale::to_vector(const boost::string_ref src) |
| 125 | { |
| 126 | // should we include a specific character |
| 127 | auto include = [](char input) { |
| 128 | // we ignore spaces and colons |
| 129 | return !std::isspace(input) && input != ':'; |
| 130 | }; |
| 131 | |
| 132 | // the number of relevant characters to decode |
| 133 | auto count = std::count_if(src.begin(), src.end(), include); |
| 134 | |
| 135 | // this must be a multiple of two, otherwise we have a truncated input |
| 136 | if (count % 2) { |
| 137 | throw std::length_error{ "Invalid hexadecimal input length" }; |
| 138 | } |
| 139 | |
| 140 | std::vector<uint8_t> result; |
| 141 | result.reserve(count / 2); |
| 142 | |
| 143 | // the data to work with (std::string is always null-terminated) |
| 144 | auto data = src.begin(); |
| 145 | |
| 146 | // convert a single hex character to an unsigned integer |
| 147 | auto char_to_int = [](const char *input) { |
| 148 | switch (std::tolower(*input)) { |
| 149 | case '0': return 0; |
| 150 | case '1': return 1; |
| 151 | case '2': return 2; |
| 152 | case '3': return 3; |
| 153 | case '4': return 4; |
| 154 | case '5': return 5; |
| 155 | case '6': return 6; |
| 156 | case '7': return 7; |
| 157 | case '8': return 8; |
| 158 | case '9': return 9; |
| 159 | case 'a': return 10; |
| 160 | case 'b': return 11; |
| 161 | case 'c': return 12; |
| 162 | case 'd': return 13; |
| 163 | case 'e': return 14; |
| 164 | case 'f': return 15; |
| 165 | default: throw std::range_error{ "Invalid hexadecimal input" }; |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | // keep going until we reach the end |
| 170 | while (data != src.end()) { |
| 171 | // skip unwanted characters |
| 172 | if (!include(*data)) { |
| 173 | ++data; |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | // convert two matching characters to int |
| 178 | auto high = char_to_int(data++); |
| 179 | auto low = char_to_int(data++); |
| 180 | |
| 181 | result.push_back(high << 4 | low); |
| 182 | } |
| 183 | |
| 184 | return result; |
| 185 | } |
| 186 | } |