-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathhexutil.hpp
128 lines (107 loc) · 3.29 KB
/
hexutil.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <boost/algorithm/hex.hpp>
#include <string_view>
#include <vector>
#include "outcome/outcome.hpp"
namespace kagome::common {
class BufferView;
/**
* @brief error codes for exceptions that may occur during unhexing
*/
enum class UnhexError : uint8_t {
NOT_ENOUGH_INPUT = 1,
NON_HEX_INPUT,
VALUE_OUT_OF_RANGE,
MISSING_0X_PREFIX,
UNKNOWN
};
} // namespace kagome::common
OUTCOME_HPP_DECLARE_ERROR(kagome::common, UnhexError);
namespace kagome::common {
/**
* @brief Converts bytes to hex representation
* @param array bytes
* @param len length of bytes
* @return hexstring
*/
std::string hex_lower(BufferView bytes);
/**
* @brief Converts bytes to hex representation with prefix 0x
* @param array bytes
* @return hexstring
*/
std::string hex_lower_0x(BufferView bytes);
template <std::output_iterator<uint8_t> Iter>
outcome::result<void> unhex_to(std::string_view hex, Iter out) {
try {
boost::algorithm::unhex(hex.begin(), hex.end(), out);
return outcome::success();
} catch (const boost::algorithm::not_enough_input &e) {
return UnhexError::NOT_ENOUGH_INPUT;
} catch (const boost::algorithm::non_hex_input &e) {
return UnhexError::NON_HEX_INPUT;
} catch (const std::exception &e) {
return UnhexError::UNKNOWN;
}
}
template <std::output_iterator<uint8_t> Iter>
outcome::result<void> unhexWith0x(std::string_view hex_with_prefix,
Iter out) {
constexpr std::string_view prefix = "0x";
if (!hex_with_prefix.starts_with(prefix)) {
return UnhexError::MISSING_0X_PREFIX;
}
return common::unhex_to(hex_with_prefix.substr(prefix.size()), out);
}
/**
* @brief Converts hex representation to bytes
* @param array individual chars
* @param len length of chars
* @return result containing array of bytes if input string is hex encoded and
* has even length
*
* @note reads both uppercase and lowercase hexstrings
*
* @see
* https://www.boost.org/doc/libs/1_51_0/libs/algorithm/doc/html/the_boost_algorithm_library/Misc/hex.html
*/
outcome::result<std::vector<uint8_t>> unhex(std::string_view hex);
/**
* @brief Unhex hex-string with 0x in the beginning
* @param hex hex string with 0x in the beginning
* @return unhexed buffer
*/
outcome::result<std::vector<uint8_t>> unhexWith0x(std::string_view hex);
/**
* @brief unhex hex-string with 0x or without it in the beginning
* @tparam T unsigned integer value type to decode
* @param value source hex string
* @return unhexed value
*/
template <class T>
requires std::is_unsigned_v<T>
outcome::result<T> unhexNumber(std::string_view value) {
std::vector<uint8_t> bytes;
OUTCOME_TRY(bts, common::unhexWith0x(value));
bytes = std::move(bts);
if (bytes.size() > sizeof(T)) {
return UnhexError::VALUE_OUT_OF_RANGE;
}
T result{0u};
for (auto byte : bytes) {
// check if `multiply by 10` will cause overflow
if constexpr (sizeof(T) > 1) {
result <<= 8u;
} else {
result = 0;
}
result += byte;
}
return result;
}
} // namespace kagome::common