/src/monero/monero/contrib/epee/include/hex.h
Line | Count | Source |
1 | | // Copyright (c) 2017-2024, 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 | | #pragma once |
30 | | |
31 | | #include <array> |
32 | | #include <cstdint> |
33 | | #include <iosfwd> |
34 | | #include <string> |
35 | | #include <boost/utility/string_ref.hpp> |
36 | | |
37 | | #include "wipeable_string.h" |
38 | | #include "span.h" |
39 | | |
40 | | namespace epee |
41 | | { |
42 | | struct to_hex |
43 | | { |
44 | | //! \return A std::string containing hex of `src`. |
45 | | static std::string string(const span<const std::uint8_t> src); |
46 | | //! \return A epee::wipeable_string containing hex of `src`. |
47 | | static epee::wipeable_string wipeable_string(const span<const std::uint8_t> src); |
48 | | template<typename T> static epee::wipeable_string wipeable_string(const T &pod) { return wipeable_string(span<const uint8_t>((const uint8_t*)&pod, sizeof(pod))); } |
49 | | |
50 | | //! \return An array containing hex of `src`. |
51 | | template<std::size_t N> |
52 | | static std::array<char, N * 2> array(const std::array<std::uint8_t, N>& src) noexcept |
53 | 0 | { |
54 | 0 | std::array<char, N * 2> out; |
55 | 0 | static_assert(N <= 128, "keep the stack size down"); |
56 | 0 | buffer_unchecked(out.data(), {src.data(), src.size()}); |
57 | 0 | return out; |
58 | 0 | } |
59 | | |
60 | | //! \return An array containing hex of `src`. |
61 | | template<typename T> |
62 | | static std::array<char, sizeof(T) * 2> array(const T& src) noexcept |
63 | 8.28k | { |
64 | 8.28k | std::array<char, sizeof(T) * 2> out; |
65 | 8.28k | static_assert(sizeof(T) <= 128, "keep the stack size down"); |
66 | 8.28k | buffer_unchecked(out.data(), as_byte_span(src)); |
67 | 8.28k | return out; |
68 | 8.28k | } std::__1::array<char, (sizeof (crypto::hash))*(2)> epee::to_hex::array<crypto::hash>(crypto::hash const&) Line | Count | Source | 63 | 8.28k | { | 64 | 8.28k | std::array<char, sizeof(T) * 2> out; | 65 | 8.28k | static_assert(sizeof(T) <= 128, "keep the stack size down"); | 66 | 8.28k | buffer_unchecked(out.data(), as_byte_span(src)); | 67 | 8.28k | return out; | 68 | 8.28k | } |
Unexecuted instantiation: std::__1::array<char, (sizeof (crypto::signature))*(2)> epee::to_hex::array<crypto::signature>(crypto::signature const&) Unexecuted instantiation: std::__1::array<char, (sizeof (crypto::key_image))*(2)> epee::to_hex::array<crypto::key_image>(crypto::key_image const&) Unexecuted instantiation: std::__1::array<char, (sizeof (crypto::public_key))*(2)> epee::to_hex::array<crypto::public_key>(crypto::public_key const&) Unexecuted instantiation: std::__1::array<char, (sizeof (crypto::view_tag))*(2)> epee::to_hex::array<crypto::view_tag>(crypto::view_tag const&) Unexecuted instantiation: std::__1::array<char, (sizeof (rct::key))*(2)> epee::to_hex::array<rct::key>(rct::key const&) |
69 | | |
70 | | //! Write `src` as hex to `out`. `out` must be exactly 2x in size. |
71 | | static bool buffer(span<char> out, const span<const std::uint8_t> src) noexcept; |
72 | | |
73 | | //! Append `src` as hex to `out`. |
74 | | static void buffer(std::ostream& out, const span<const std::uint8_t> src); |
75 | | |
76 | | //! Append `< + src + >` as hex to `out`. |
77 | | static void formatted(std::ostream& out, const span<const std::uint8_t> src); |
78 | | |
79 | | private: |
80 | | template<typename T> T static convert(const span<const std::uint8_t> src); |
81 | | |
82 | | //! Write `src` bytes as hex to `out`. `out` must be twice the length |
83 | | static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept; |
84 | | }; |
85 | | |
86 | | //! Convert hex in UTF8 encoding to binary |
87 | | struct from_hex |
88 | | { |
89 | | static bool to_string(std::string& out, boost::string_ref src); |
90 | | |
91 | | static bool to_buffer(span<std::uint8_t> out, boost::string_ref src) noexcept; |
92 | | |
93 | | private: |
94 | | static bool to_buffer_unchecked(std::uint8_t* out, boost::string_ref src) noexcept; |
95 | | }; |
96 | | |
97 | | //! Convert hex in current C locale encoding to binary |
98 | | struct from_hex_locale |
99 | | { |
100 | | static std::vector<uint8_t> to_vector(boost::string_ref src); |
101 | | }; |
102 | | } |