hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
hexutils.hpp
Go to the documentation of this file.
1 
5 #ifndef IROHA_HEXUTILS_HPP
6 #define IROHA_HEXUTILS_HPP
7 
8 #include <iterator>
9 #include <string>
10 
11 #include <boost/algorithm/hex.hpp>
12 #include <boost/optional.hpp>
13 #include "common/result.hpp"
15 
16 namespace iroha {
17 
18  template <typename Container>
19  inline auto hexstringToBytestringSize(Container const &c)
20  -> decltype(c.size()) {
21  return (c.size() + 1) / 2;
22  }
23 
24  template <typename Container>
25  inline auto bytestringToHexstringSize(Container const &c)
26  -> decltype(c.size()) {
27  return c.size() * 2;
28  }
29 
35  template <typename OutputContainer>
38  OutputContainer &destination) {
39  static_assert(sizeof(*input.data()) == sizeof(uint8_t), "type mismatch");
40  const auto beg = reinterpret_cast<const uint8_t *>(input.data());
41  const auto end = beg + input.size();
42  destination.reserve(destination.size() + bytestringToHexstringSize(input));
43  boost::algorithm::hex_lower(beg, end, std::back_inserter(destination));
44  }
45 
51  inline std::string bytestringToHexstring(std::string_view str) {
52  std::string result;
55  return result;
56  }
57 
65  hexstringToBytestringResult(std::string_view str) {
66  using namespace iroha::expected;
67  if (str.empty()) {
68  return makeError("Empty hex string.");
69  }
70  if (str.size() % 2 != 0) {
71  return makeError("Hex string contains uneven number of characters.");
72  }
73  std::string result;
74  result.reserve(hexstringToBytestringSize(str));
75  try {
76  boost::algorithm::unhex(
77  str.begin(), str.end(), std::back_inserter(result));
78  } catch (const boost::algorithm::hex_decode_error &e) {
79  return makeError(e.what());
80  }
81  return iroha::expected::makeValue(std::move(result));
82  }
83 
84  /*[[deprecated]]*/ inline boost::optional<std::string> hexstringToBytestring(
85  std::string_view str) {
86  return iroha::expected::resultToOptionalValue(
88  }
89 
95  template <typename T,
96  typename = std::enable_if_t<std::is_arithmetic<T>::value>>
97  inline std::string numToHexstring(const T val) {
98  std::stringstream ss;
99  ss << std::hex << std::setfill('0') << std::setw(sizeof(T) * 2) << val;
100  return ss.str();
101  }
102 
103 } // namespace iroha
104 
105 #endif // IROHA_HEXUTILS_HPP
std::string numToHexstring(const T val)
Definition: hexutils.hpp:97
std::string bytestringToHexstring(std::string_view str)
Definition: hexutils.hpp:51
Definition: result_fwd.hpp:27
void bytestringToHexstringAppend(shared_model::interface::types::ByteRange input, OutputContainer &destination)
Definition: hexutils.hpp:36
boost::optional< std::string > hexstringToBytestring(std::string_view str)
Definition: hexutils.hpp:84
Definition: block_query.hpp:15
ByteRange makeByteRange(const Source &str)
Definition: byte_range.hpp:19
auto bytestringToHexstringSize(Container const &c) -> decltype(c.size())
Definition: hexutils.hpp:25
auto hexstringToBytestringSize(Container const &c) -> decltype(c.size())
Definition: hexutils.hpp:19
Definition: result_fwd.hpp:10
iroha::expected::Result< std::string, const char * > hexstringToBytestringResult(std::string_view str)
Definition: hexutils.hpp:65
expected::Result< T, DbError > makeError(uint32_t code, char const *format, Args &&... args)
Definition: rocksdb_common.hpp:471
std::basic_string_view< std::byte > ByteRange
Definition: byte_range.hpp:16