Kagome
Polkadot Runtime Engine in C++17
mp_utils.cpp
Go to the documentation of this file.
1 
6 #include "common/mp_utils.hpp"
7 
8 #include <gsl/gsl_util>
9 
11 
12 namespace kagome::common {
13 
14  namespace {
15  template <size_t size, typename uint>
16  inline std::array<uint8_t, size> uint_to_le_bytes(uint &&i) {
17  std::array<uint8_t, size> res{};
18  res.fill(0);
19  export_bits(i, res.begin(), 8, false);
20  return res;
21  }
22 
23  template <size_t size, typename uint>
24  inline std::array<uint8_t, size> uint_to_be_bytes(uint &&i) {
25  std::array<uint8_t, size> res{};
26  res.fill(0);
27  export_bits(i, res.rbegin(), 8, false);
28  return res;
29  }
30 
31  template <size_t size, typename uint>
32  inline uint le_bytes_to_uint(gsl::span<const uint8_t, size> bytes) {
33  if (bytes.empty()) {
34  return uint(0);
35  }
36  uint result;
37  import_bits(result, bytes.begin(), bytes.end(), 8, false);
38  return result;
39  }
40 
41  template <size_t size, typename uint>
42  inline uint be_bytes_to_uint(gsl::span<const uint8_t, size> bytes) {
43  if (bytes.empty()) {
44  return uint(0);
45  }
46  uint result;
47  import_bits(result, bytes.rbegin(), bytes.rend(), 8, false);
48  return result;
49  }
50  } // namespace
51 
52  std::array<uint8_t, 8> uint64_to_le_bytes(uint64_t number) {
53  std::array<uint8_t, 8> result{};
54  *reinterpret_cast<uint64_t *>(result.data()) = htole64(number);
55  return result;
56  }
57 
58  uint64_t le_bytes_to_uint64(gsl::span<const uint8_t, 8> bytes) {
59  uint64_t number;
60  memcpy(&number, bytes.data(), 8);
61  return le64toh(number);
62  }
63 
64  std::array<uint8_t, 8> uint64_to_be_bytes(uint64_t number) {
65  std::array<uint8_t, 8> result{};
66  *reinterpret_cast<uint64_t *>(result.data()) = htobe64(number);
67  return result;
68  }
69 
70  uint64_t be_bytes_to_uint64(gsl::span<const uint8_t, 8> bytes) {
71  uint64_t number;
72  memcpy(&number, bytes.data(), 8);
73  return be64toh(number);
74  }
75 
76  std::array<uint8_t, 16> uint128_to_le_bytes(
78  return uint_to_le_bytes<16>(i);
79  }
80 
82  gsl::span<const uint8_t, 16> bytes) {
83  return le_bytes_to_uint<16, boost::multiprecision::uint128_t>(bytes);
84  }
85 
86  std::array<uint8_t, 16> uint128_to_be_bytes(
88  return uint_to_be_bytes<16>(i);
89  }
90 
92  gsl::span<const uint8_t, 16> bytes) {
93  return be_bytes_to_uint<16, boost::multiprecision::uint128_t>(bytes);
94  }
95 
96  std::array<uint8_t, 32> uint256_to_le_bytes(
98  return uint_to_le_bytes<32>(i);
99  }
100 
102  gsl::span<const uint8_t, 32> bytes) {
103  return le_bytes_to_uint<32, boost::multiprecision::uint256_t>(bytes);
104  }
105 
106  std::array<uint8_t, 32> uint256_to_be_bytes(
108  return uint_to_be_bytes<32>(i);
109  }
110 
112  gsl::span<const uint8_t, 32> bytes) {
113  return be_bytes_to_uint<32, boost::multiprecision::uint256_t>(bytes);
114  }
115 
116 } // namespace kagome::common
boost::multiprecision::uint256_t uint256_t
Definition: mp_utils.hpp:15
uint64_t le_bytes_to_uint64(gsl::span< const uint8_t, 8 > bytes)
Definition: mp_utils.cpp:58
boost::multiprecision::uint256_t be_bytes_to_uint256(gsl::span< const uint8_t, 32 > bytes)
Definition: mp_utils.cpp:111
boost::multiprecision::uint128_t be_bytes_to_uint128(gsl::span< const uint8_t, 16 > bytes)
Definition: mp_utils.cpp:91
boost::multiprecision::uint128_t le_bytes_to_uint128(gsl::span< const uint8_t, 16 > bytes)
Definition: mp_utils.cpp:81
uint64_t be_bytes_to_uint64(gsl::span< const uint8_t, 8 > bytes)
Definition: mp_utils.cpp:70
boost::multiprecision::uint256_t le_bytes_to_uint256(gsl::span< const uint8_t, 32 > bytes)
Definition: mp_utils.cpp:101
std::array< uint8_t, 8 > uint64_to_be_bytes(uint64_t number)
Definition: mp_utils.cpp:64
std::array< uint8_t, 32 > uint256_to_be_bytes(const boost::multiprecision::uint256_t &i)
Definition: mp_utils.cpp:106
std::array< uint8_t, 16 > uint128_to_le_bytes(const boost::multiprecision::uint128_t &i)
Definition: mp_utils.cpp:76
std::array< uint8_t, 8 > uint64_to_le_bytes(uint64_t number)
Definition: mp_utils.cpp:52
boost::multiprecision::uint128_t uint128_t
Definition: mp_utils.hpp:14
std::array< uint8_t, 16 > uint128_to_be_bytes(const boost::multiprecision::uint128_t &i)
Definition: mp_utils.cpp:86
std::array< uint8_t, 32 > uint256_to_le_bytes(const boost::multiprecision::uint256_t &i)
Definition: mp_utils.cpp:96