Electroneum
Loading...
Searching...
No Matches
hex.h
Go to the documentation of this file.
1// Copyright (c) 2017-Present, Electroneum
2// Copyright (c) 2017-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#pragma once
31
32#include <array>
33#include <cstdint>
34#include <iosfwd>
35#include <string>
36#include <boost/utility/string_ref.hpp>
37
38#include "wipeable_string.h"
39#include "span.h"
40
41namespace epee
42{
43 struct to_hex
44 {
46 static std::string string(const span<const std::uint8_t> src);
49 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))); }
50
52 template<std::size_t N>
53 static std::array<char, N * 2> array(const std::array<std::uint8_t, N>& src) noexcept
54 {
55 std::array<char, N * 2> out{{}};
56 static_assert(N <= 128, "keep the stack size down");
57 buffer_unchecked(out.data(), {src.data(), src.size()});
58 return out;
59 }
60
62 static void buffer(std::ostream& out, const span<const std::uint8_t> src);
63
65 static void formatted(std::ostream& out, const span<const std::uint8_t> src);
66
67 private:
68 template<typename T> T static convert(const span<const std::uint8_t> src);
69
71 static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept;
72 };
73
74 struct from_hex
75 {
77 static std::vector<uint8_t> vector(boost::string_ref src);
78 };
79}
Non-owning sequence of data. Does not deep copy.
Definition span.h:57
unsigned char uint8_t
Definition stdint.h:124
static std::vector< uint8_t > vector(boost::string_ref src)
Definition hex.cpp:88
static epee::wipeable_string wipeable_string(const span< const std::uint8_t > src)
Definition hex.cpp:69
static std::array< char, N *2 > array(const std::array< std::uint8_t, N > &src) noexcept
Definition hex.h:53
static std::string string(const span< const std::uint8_t > src)
Definition hex.cpp:68
static void formatted(std::ostream &out, const span< const std::uint8_t > src)
Append < + src + > as hex to out.
Definition hex.cpp:76
static void buffer(std::ostream &out, const span< const std::uint8_t > src)
Append src as hex to out.
Definition hex.cpp:71
static epee::wipeable_string wipeable_string(const T &pod)
Definition hex.h:49
#define T(x)