Kagome
Polkadot Runtime Engine in C++17
hexutil.cpp
Go to the documentation of this file.
1 
6 #include "common/hexutil.hpp"
7 
8 #include <boost/algorithm/hex.hpp>
9 #include <gsl/span>
10 
13  switch (e) {
14  case UnhexError::NON_HEX_INPUT:
15  return "Input contains non-hex characters";
16  case UnhexError::NOT_ENOUGH_INPUT:
17  return "Input contains odd number of characters";
18  case UnhexError::VALUE_OUT_OF_RANGE:
19  return "Decoded value is out of range of requested type";
20  case UnhexError::MISSING_0X_PREFIX:
21  return "Missing expected 0x prefix";
22  case UnhexError::UNKNOWN:
23  return "Unknown error";
24  }
25  return "Unknown error (error id not listed)";
26 }
27 
28 namespace kagome::common {
29 
30  std::string int_to_hex(uint64_t n, size_t fixed_width) noexcept {
31  std::stringstream result;
32  result.width(fixed_width);
33  result.fill('0');
34  result << std::hex << std::uppercase << n;
35  auto str = result.str();
36  if (str.length() % 2 != 0) {
37  str.push_back('\0');
38  for (int64_t i = str.length() - 2; i >= 0; --i) {
39  str[i + 1] = str[i];
40  }
41  str[0] = '0';
42  }
43  return str;
44  }
45 
46  std::string hex_upper(const gsl::span<const uint8_t> bytes) noexcept {
47  std::string res(bytes.size() * 2, '\x00');
48  boost::algorithm::hex(bytes.begin(), bytes.end(), res.begin());
49  return res;
50  }
51 
52  std::string hex_lower(const gsl::span<const uint8_t> bytes) noexcept {
53  std::string res(bytes.size() * 2, '\x00');
54  boost::algorithm::hex_lower(bytes.begin(), bytes.end(), res.begin());
55  return res;
56  }
57 
58  std::string hex_lower_0x(gsl::span<const uint8_t> bytes) noexcept {
59  constexpr char prefix[] = {'0', 'x'};
60  constexpr size_t prefix_len = sizeof(prefix);
61 
62  std::string res(bytes.size() * 2 + prefix_len, '\x00');
63  res.replace(0, prefix_len, prefix, prefix_len);
64 
66  bytes.begin(), bytes.end(), res.begin() + prefix_len);
67  return res;
68  }
69 
70  outcome::result<std::vector<uint8_t>> unhex(std::string_view hex) {
71  std::vector<uint8_t> blob;
72  blob.reserve((hex.size() + 1) / 2);
73 
74  try {
75  boost::algorithm::unhex(hex.begin(), hex.end(), std::back_inserter(blob));
76  return blob;
77 
78  } catch (const boost::algorithm::not_enough_input &e) {
80 
81  } catch (const boost::algorithm::non_hex_input &e) {
83 
84  } catch (const std::exception &e) {
85  return UnhexError::UNKNOWN;
86  }
87  }
88 
89  outcome::result<std::vector<uint8_t>> unhexWith0x(
90  std::string_view hex_with_prefix) {
91  const static std::string leading_chrs = "0x";
92 
93  if (hex_with_prefix.substr(0, leading_chrs.size()) != leading_chrs) {
95  }
96 
97  auto without_prefix = hex_with_prefix.substr(leading_chrs.size());
98 
99  return common::unhex(without_prefix);
100  }
101 } // namespace kagome::common
std::string hex_lower(const gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation.
Definition: hexutil.cpp:52
outcome::result< std::vector< uint8_t > > unhexWith0x(std::string_view hex_with_prefix)
Unhex hex-string with 0x in the begining.
Definition: hexutil.cpp:89
std::string hex_upper(const gsl::span< const uint8_t > bytes) noexcept
Converts bytes to uppercase hex representation.
Definition: hexutil.cpp:46
std::string int_to_hex(uint64_t n, size_t fixed_width) noexcept
Converts an integer to an uppercase hex representation.
Definition: hexutil.cpp:30
OUTCOME_CPP_DEFINE_CATEGORY(kagome::common, UnhexError, e)
Definition: hexutil.cpp:11
std::string hex_lower_0x(gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation with prefix 0x.
Definition: hexutil.cpp:58
UnhexError
error codes for exceptions that may occur during unhexing
Definition: hexutil.hpp:20
outcome::result< std::vector< uint8_t > > unhex(std::string_view hex)
Converts hex representation to bytes.
Definition: hexutil.cpp:70