forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBuffer.hpp
197 lines (152 loc) · 4.08 KB
/
DataBuffer.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef _SNARKFRONT_DATA_BUFFER_HPP_
#define _SNARKFRONT_DATA_BUFFER_HPP_
#include <array>
#include <climits>
#include <cstdint>
#include <istream>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// data buffer
//
template <typename T>
class DataBuffer
{
public:
DataBuffer() = default;
template <typename X>
DataBuffer(X& a) // not const reference for std::ostream
: m_buf(a)
{}
template <typename X, typename U>
DataBuffer(X& a, const U b) // PrintHex constructor takes two arguments
: m_buf(a, b)
{}
T& operator* () {
return m_buf;
}
const T* operator-> () const {
return std::addressof(m_buf);
}
void push8(const std::uint8_t a) {
m_buf.pushOctet(a);
}
void push32(const std::uint32_t a) {
push8(a >> 3 * CHAR_BIT);
push8((a >> 2 * CHAR_BIT) & 0xff);
push8((a >> CHAR_BIT) & 0xff);
push8(a & 0xff);
}
void push64(const std::uint64_t a) {
push32(a >> 4 * CHAR_BIT);
push32(a & 0xffffffff);
}
void pushText(const std::string& a) {
for (const auto& c : a)
push8(c);
}
void push(const std::uint8_t a) { push8(a); }
void push(const std::uint32_t a) { push32(a); }
void push(const std::uint64_t a) { push64(a); }
void push(const std::string& a) { pushText(a); }
void push(const char* a) { push(std::string(a)); }
template <typename U, std::size_t N>
void push(const std::array<U, N>& a) {
for (const auto& b : a)
push(b);
}
template <typename U>
void push(const std::vector<U>& a) {
for (const auto& b : a)
push(b);
}
void push(const DataBuffer& other) {
for (const auto& a : other->data())
push8(a);
}
private:
T m_buf;
};
// (extract) read from stream into data buffer object
template <typename T>
std::istream& operator>> (std::istream& is, DataBuffer<T>& a) {
std::uint8_t c;
while (! is.eof() && (is >> c)) {
a.push8(c);
}
return is;
}
////////////////////////////////////////////////////////////////////////////////
// clear text buffer
//
class ClearText
{
public:
ClearText() = default;
ClearText(DataBuffer<ClearText>&);
void clear();
void pushOctet(const std::uint8_t);
const std::vector<std::uint8_t>& data() const;
bool empty() const;
std::size_t size() const;
std::size_t sizeBits() const;
template <typename T>
T getWord(std::size_t& index) const {
T a = 0;
for (std::size_t i = 0; i < sizeof(T); ++i) {
a = (a << CHAR_BIT) | m_data[index++];
}
return a;
}
private:
std::vector<std::uint8_t> m_data;
};
////////////////////////////////////////////////////////////////////////////////
// data buffer stream
//
class DataBufferStream
{
public:
DataBufferStream();
DataBufferStream(const DataBufferStream& other) = default;
template <typename T>
DataBufferStream(const T& a)
: DataBufferStream{}
{
m_buf.push(a);
}
template <typename T>
void push(const T& a) {
m_buf.push(a);
}
template <typename T, typename... Args>
void push(const T& a, const Args... parameterPack) {
push(a);
push(parameterPack...);
}
bool empty() const;
const std::vector<std::uint8_t>& data() const;
DataBuffer<ClearText>& operator* ();
DataBuffer<ClearText>* operator-> ();
template <typename T>
T getWord() {
return m_buf->getWord<T>(m_index);
}
template <typename T, std::size_t N>
std::array<T, N> getArray() {
std::array<T, N> a;
for (std::size_t i = 0; i < N; ++i)
a[i] = getWord<T>();
return a;
}
private:
DataBuffer<ClearText> m_buf;
std::size_t m_index;
};
// (extract) read from stream into data buffer stream object
std::istream& operator>> (std::istream& is, DataBufferStream& a);
} // namespace snarkfront
#endif