Monero
hex.h
Go to the documentation of this file.
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  {
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 
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 
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 
71  static void buffer(std::ostream& out, const span<const std::uint8_t> src);
72 
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 
80  static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept;
81  };
82 
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 
96  {
97  static std::vector<uint8_t> to_vector(boost::string_ref src);
98  };
99 }
const uint32_t T[512]
Definition: groestl_tables.h:36
span< const std::uint8_t > as_byte_span(const T &src) noexcept
Definition: span.h:161
static std::array< char, sizeof(T) *2 > array(const T &src) noexcept
Definition: hex.h:62
::std::string string
Definition: gtest-port.h:1097
static std::array< char, N *2 > array(const std::array< std::uint8_t, N > &src) noexcept
Definition: hex.h:52
unsigned char uint8_t
Definition: stdint.h:124
static epee::wipeable_string wipeable_string(const span< const std::uint8_t > src)
Definition: hex.cpp:70
Convert hex in UTF8 encoding to binary.
Definition: hex.h:84
static bool to_buffer_unchecked(std::uint8_t *out, boost::string_ref src) noexcept
Definition: hex.cpp:103
constexpr std::size_t size() const noexcept
Definition: span.h:109
static void buffer_unchecked(char *out, const span< const std::uint8_t > src) noexcept
Write src bytes as hex to out. out must be twice the length.
Definition: hex.cpp:84
static bool to_buffer(span< std::uint8_t > out, boost::string_ref src) noexcept
Definition: hex.cpp:96
static T convert(const span< const std::uint8_t > src)
Definition: hex.cpp:58
static epee::wipeable_string wipeable_string(const T &pod)
Definition: hex.h:48
static bool to_string(std::string &out, boost::string_ref src)
Definition: hex.cpp:90
static std::vector< uint8_t > to_vector(boost::string_ref src)
Definition: hex.cpp:124
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
static void buffer(std::ostream &out, const span< const std::uint8_t > src)
Append src as hex to out.
Definition: hex.cpp:72
static std::string string(const span< const std::uint8_t > src)
Definition: hex.cpp:69
Definition: wipeable_string.h:40
Definition: hex.h:42
Convert hex in current C locale encoding to binary.
Definition: hex.h:95
static void formatted(std::ostream &out, const span< const std::uint8_t > src)
Append < + src + > as hex to out.
Definition: hex.cpp:77
constexpr pointer data() const noexcept
Definition: span.h:108