OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
buffer.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "buffer.hpp"
7
8#include <array>
9#include <cstring>
10#include <iomanip>
11#include <sstream>
12
13namespace cbdc {
15 m_data.clear();
16 }
17
18 void buffer::append(const void* data, size_t len) {
19 const auto orig_size = m_data.size();
20 m_data.resize(orig_size + len);
21 std::memcpy(&m_data[orig_size], data, len);
22 }
23
24 auto buffer::size() const -> size_t {
25 return m_data.size();
26 }
27
28 auto buffer::data() -> void* {
29 return m_data.data();
30 }
31
32 auto buffer::data() const -> const void* {
33 return m_data.data();
34 }
35
36 auto buffer::data_at(size_t offset) -> void* {
37 return &m_data[offset];
38 }
39
40 auto buffer::data_at(size_t offset) const -> const void* {
41 return &m_data[offset];
42 }
43
44 auto buffer::operator==(const buffer& other) const -> bool {
45 return m_data == other.m_data;
46 }
47
48 void buffer::extend(size_t len) {
49 m_data.resize(m_data.size() + len);
50 }
51
52 auto buffer::c_ptr() const -> const unsigned char* {
53 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
54 return reinterpret_cast<const unsigned char*>(m_data.data());
55 }
56
57 auto buffer::c_str() const -> const char* {
58 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
59 return reinterpret_cast<const char*>(m_data.data());
60 }
61
62 auto buffer::to_hex() const -> std::string {
63 // TODO: refactor these into general hex conversion functions with
64 // functions in hash.cpp.
65 std::stringstream ret;
66 ret << std::hex << std::setfill('0');
67
68 for(const auto& byte : m_data) {
69 // TODO: This function is likely very slow. At some point this
70 // could be converted to lookup table that we make ourselves.
71 ret << std::setw(2) << static_cast<int>(byte);
72 }
73
74 return ret.str();
75 }
76
77 auto buffer::to_hex_prefixed(const std::string& prefix) const
78 -> std::string {
79 auto res = std::string();
80 res.append(prefix);
81 res.append(to_hex());
82 return res;
83 }
84
85 auto buffer::from_hex(const std::string& hex) -> std::optional<buffer> {
86 constexpr auto max_size = 102400;
87 if(hex.empty() || ((hex.size() % 2) != 0) || (hex.size() > max_size)) {
88 return std::nullopt;
89 }
90
91 auto ret = cbdc::buffer();
92
93 for(size_t i = 0; i < hex.size(); i += 2) {
94 unsigned int v{};
95 std::stringstream s;
96 // TODO: This function is likely very slow. At some point this
97 // could be converted to lookup table that we make ourselves.
98 s << std::hex << hex.substr(i, 2);
99 if(!(s >> v)) {
100 return std::nullopt;
101 }
102 ret.m_data.push_back(static_cast<std::byte>(v));
103 }
104
105 return ret;
106 }
107
108 auto buffer::from_hex_prefixed(const std::string& hex,
109 const std::string& prefix)
110 -> std::optional<buffer> {
111 size_t offset = 0;
112 if(hex.rfind(prefix, 0) == 0) {
113 offset = prefix.size();
114 }
115 auto hex_str = hex.substr(offset);
116 if(hex_str.size() % 2 != 0) {
117 hex_str.insert(0, "0");
118 }
119 return from_hex(hex_str);
120 }
121}
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
auto data() -> void *
Returns a raw pointer to the start of the buffer data.
Definition buffer.cpp:28
void clear()
Removes any existing content in the buffer making its size 0.
Definition buffer.cpp:14
static auto from_hex(const std::string &hex) -> std::optional< cbdc::buffer >
Creates a new buffer from the provided hex string.
Definition buffer.cpp:85
auto operator==(const buffer &other) const -> bool
Definition buffer.cpp:44
auto to_hex_prefixed(const std::string &prefix="0x") const -> std::string
Returns a hex string representation of the contents of the buffer prefixed with a prefix sequence.
Definition buffer.cpp:77
auto c_str() const -> const char *
Returns a pointer to the data, cast to a char*.
Definition buffer.cpp:57
void append(const void *data, size_t len)
Adds the given number of bytes from the given pointer to the end of the buffer.
Definition buffer.cpp:18
void extend(size_t len)
Extends the size of the buffer by the given length.
Definition buffer.cpp:48
auto c_ptr() const -> const unsigned char *
Returns a pointer to the data, cast to an unsigned char*.
Definition buffer.cpp:52
static auto from_hex_prefixed(const std::string &hex, const std::string &prefix="0x") -> std::optional< buffer >
Creates a new buffer from the provided hex string optionally prefixed with a prefix sequence.
Definition buffer.cpp:108
auto to_hex() const -> std::string
Returns a hex string representation of the contents of the buffer.
Definition buffer.cpp:62
auto size() const -> size_t
Returns the number of bytes contained in the buffer.
Definition buffer.cpp:24
auto data_at(size_t offset) -> void *
Returns a raw pointer to the start of the buffer data.
Definition buffer.cpp:36
@ buffer
A singular RLP value (byte array)