Debian Codegraph @

contrib/epee/include/hex.h

raw symbols SCIP
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 #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 {
54 std::array<char, N * 2> out;
55 static_assert(N <= 128, "keep the stack size down");
56 buffer_unchecked(out.data(), {src.data(), src.size()});
57 return out;
58 }
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 {
64 std::array<char, sizeof(T) * 2> out;
65 static_assert(sizeof(T) <= 128, "keep the stack size down");
66 buffer_unchecked(out.data(), as_byte_span(src));
67 return out;
68 }
69
70 //! Append `src` as hex to `out`.
71 static void buffer(std::ostream& out, const span<const std::uint8_t> src);
72
73 //! Append `< + src + >` as hex to `out`.
74 static void formatted(std::ostream& out, const span<const std::uint8_t> src);
75
76 private:
77 template<typename T> T static convert(const span<const std::uint8_t> src);
78
79 //! Write `src` bytes as hex to `out`. `out` must be twice the length
80 static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept;
81 };
82
83 //! Convert hex in UTF8 encoding to binary
84 struct from_hex
85 {
86 static bool to_string(std::string& out, boost::string_ref src);
87
88 static bool to_buffer(span<std::uint8_t> out, boost::string_ref src) noexcept;
89
90 private:
91 static bool to_buffer_unchecked(std::uint8_t* out, boost::string_ref src) noexcept;
92 };
93
94 //! Convert hex in current C locale encoding to binary
95 struct from_hex_locale
96 {
97 static std::vector<uint8_t> to_vector(boost::string_ref src);
98 };
99 }