-
Notifications
You must be signed in to change notification settings - Fork 2
/
Buffer.cpp
257 lines (201 loc) · 6.07 KB
/
Buffer.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* This file is part of PHPinnacle/Buffer.
*
* (c) PHPinnacle Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#include "Buffer.hpp"
/************************* COMMON *************************/
Buffer::Buffer() noexcept {
}
Buffer::Buffer(const std::vector<unsigned char> &_buffer) noexcept:
buffer(_buffer) {
}
void Buffer::clear() noexcept {
buffer.clear();
}
void Buffer::write(char * str, long length) noexcept {
buffer.insert(buffer.end(), str, str + length);
}
void Buffer::discard(unsigned long long n) {
if (buffer.size() < n)
throw BufferOverflow();
buffer.erase(buffer.begin(), buffer.begin() + n);
}
unsigned long long Buffer::size() const noexcept {
return buffer.size();
}
bool Buffer::empty() const noexcept {
return buffer.empty();
}
void Buffer::merge(const Buffer &other) noexcept {
buffer.insert(buffer.end(), other.buffer.begin(), other.buffer.end());
}
std::string Buffer::bytes() const noexcept {
std::stringstream stream;
unsigned long long size = buffer.size();
for (unsigned long long i = 0; i < size; ++i)
stream << buffer[i];
return stream.str();
}
std::string Buffer::flush() noexcept {
std::string out = bytes();
clear();
return out;
}
/************************* WRITING *************************/
template <class T> inline void Buffer::append(const T &val) noexcept {
unsigned int size = sizeof(T);
unsigned const char *array = reinterpret_cast<unsigned const char*>(&val);
for (unsigned int i = 0; i < size; ++i)
buffer.push_back(array[size - i - 1]);
}
void Buffer::appendBoolean(bool val) noexcept {
append<bool>(val);
}
void Buffer::appendString(const std::string &str) noexcept {
for (const unsigned char &s : str) appendInt8(s);
}
void Buffer::appendInt8(char val) noexcept {
append<char>(val);
}
void Buffer::appendUint8(unsigned char val) noexcept {
append<unsigned char>(val);
}
void Buffer::appendInt16(short val) noexcept {
append<short>(val);
}
void Buffer::appendUint16(unsigned short val) noexcept {
append<unsigned short>(val);
}
void Buffer::appendInt32(int val) noexcept {
append<int>(val);
}
void Buffer::appendUint32(unsigned int val) noexcept {
append<unsigned int>(val);
}
void Buffer::appendInt64(long long val) noexcept {
append<long long>(val);
}
void Buffer::appendUint64(unsigned long long val) noexcept {
append<unsigned long long>(val);
}
void Buffer::appendFloat(float val) noexcept {
union { float fnum; unsigned long inum; } u;
u.fnum = val;
appendUint32(u.inum);
}
void Buffer::appendDouble(double val) noexcept {
union { double fnum; unsigned long long inum; } u;
u.fnum = val;
appendUint64(u.inum);
}
/************************* READING *************************/
template <class T> inline T Buffer::read(unsigned long long offset) const {
T result = 0;
unsigned int size = sizeof(T);
if (offset + size > buffer.size())
throw BufferOverflow();
char *dst = (char*) &result;
char *src = (char*) &buffer[offset];
for (unsigned int i = 0; i < size; ++i)
dst[i] = src[size - i - 1];
return result;
}
bool Buffer::readBoolean(unsigned long long offset) const {
return read<bool>(offset);
}
std::string Buffer::readString(unsigned long long size, unsigned long long offset) const {
if (offset + size > buffer.size())
throw BufferOverflow();
std::string result(buffer.begin() + offset, buffer.begin() + offset + size);
return result;
}
char Buffer::readInt8(unsigned long long offset) const {
return read<char>(offset);
}
unsigned char Buffer::readUint8(unsigned long long offset) const {
return read<unsigned char>(offset);
}
short Buffer::readInt16(unsigned long long offset) const {
return read<short>(offset);
}
unsigned short Buffer::readUint16(unsigned long long offset) const {
return read<unsigned short>(offset);
}
int Buffer::readInt32(unsigned long long offset) const {
return read<int>(offset);
}
unsigned int Buffer::readUint32(unsigned long long offset) const {
return read<unsigned int>(offset);
}
long long Buffer::readInt64(unsigned long long offset) const {
return read<long long>(offset);
}
unsigned long long Buffer::readUint64(unsigned long long offset) const {
return read<unsigned long long>(offset);
}
float Buffer::readFloat(unsigned long long offset) const {
return read<float>(offset);
}
double Buffer::readDouble(unsigned long long offset) const {
return read<double>(offset);
}
/************************* CONSUMING *************************/
template <class T> inline T Buffer::consume() {
T result = 0;
unsigned int size = sizeof(T);
if (size > buffer.size())
throw BufferOverflow();
char *dst = (char*) &result;
char *src = (char*) &buffer[0];
for (unsigned int i = 0; i < size; ++i)
dst[i] = src[size - i - 1];
buffer.erase(buffer.begin(), buffer.begin() + size);
return result;
}
bool Buffer::consumeBool() {
return consume<bool>();
}
std::string Buffer::consumeString(unsigned long long size) {
if (size > buffer.size())
throw BufferOverflow();
std::string result(buffer.begin(), buffer.begin() + size);
buffer.erase(buffer.begin(), buffer.begin() + size);
return result;
}
char Buffer::consumeInt8() {
return consume<char>();
}
unsigned char Buffer::consumeUint8() {
return consume<unsigned char>();
}
short Buffer::consumeInt16() {
return consume<short>();
}
unsigned short Buffer::consumeUint16() {
return consume<unsigned short>();
}
int Buffer::consumeInt32() {
return consume<int>();
}
unsigned int Buffer::consumeUint32() {
return consume<unsigned int>();
}
long long Buffer::consumeInt64() {
return consume<long long>();
}
unsigned long long Buffer::consumeUint64() {
return consume<unsigned long long>();
}
float Buffer::consumeFloat() {
return consume<float>();
}
double Buffer::consumeDouble() {
return consume<double>();
}
Buffer::~Buffer() {
clear();
}