Skip to content

Commit ee88f38

Browse files
author
Anton Shabouta
committed
Initial commit
0 parents  commit ee88f38

18 files changed

+1664
-0
lines changed

Diff for: .gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Makefile*
2+
*.la
3+
*.lo
4+
acinclude.m4
5+
aclocal.m4
6+
config.guess
7+
config.h*
8+
config.sub
9+
config.log
10+
config.nice
11+
config.status
12+
configure*
13+
install-sh
14+
libtool
15+
ltmain.sh
16+
missing
17+
mkinstalldirs
18+
.deps
19+
.libs/
20+
autom4te.cache/
21+
modules/
22+
build/
23+
run-tests.php
24+
.phpenv-version
25+
extras/
26+
php.sh
27+
ltmain.sh.backup
28+
tmp-php.ini
29+
.vscode
30+
.idea

Diff for: Buffer.cpp

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
/**
2+
* This file is part of PHPinnacle/Buffer.
3+
*
4+
* (c) PHPinnacle Team <dev@phpinnacle.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
#include "Buffer.hpp"
11+
12+
/************************* COMMON *************************/
13+
Buffer::Buffer() noexcept {
14+
}
15+
16+
Buffer::Buffer(const std::vector<unsigned char> &_buffer) noexcept:
17+
buffer(_buffer) {
18+
}
19+
20+
void Buffer::clear() noexcept {
21+
buffer.clear();
22+
}
23+
24+
void Buffer::discard(unsigned long long n) {
25+
if (buffer.size() < n)
26+
throw BufferOverflow();
27+
28+
buffer.erase(buffer.begin(), buffer.begin() + n);
29+
}
30+
31+
unsigned long long Buffer::size() const noexcept {
32+
return buffer.size();
33+
}
34+
35+
bool Buffer::empty() const noexcept {
36+
return buffer.size() == 0;
37+
}
38+
39+
void Buffer::merge(const Buffer &other) noexcept {
40+
buffer.insert(buffer.end(), other.buffer.begin(), other.buffer.end());
41+
}
42+
43+
std::string Buffer::bytes() const noexcept {
44+
std::stringstream stream;
45+
46+
unsigned long long size = buffer.size();
47+
for (unsigned long long i = 0; i < size; ++i)
48+
stream << buffer[i];
49+
50+
return stream.str();
51+
}
52+
53+
std::string Buffer::flush() noexcept {
54+
std::string out = bytes();
55+
56+
clear();
57+
58+
return out;
59+
}
60+
61+
/************************* WRITING *************************/
62+
template <class T> inline void Buffer::append(const T &val) noexcept {
63+
unsigned int size = sizeof(T);
64+
unsigned const char *array = reinterpret_cast<unsigned const char*>(&val);
65+
66+
for (unsigned int i = 0; i < size; ++i)
67+
buffer.push_back(array[size - i - 1]);
68+
}
69+
70+
void Buffer::appendBoolean(bool val) noexcept {
71+
append<bool>(val);
72+
}
73+
74+
void Buffer::appendString(const std::string &str) noexcept {
75+
for (const unsigned char &s : str) appendInt8(s);
76+
}
77+
78+
void Buffer::appendInt8(char val) noexcept {
79+
append<char>(val);
80+
}
81+
void Buffer::appendUInt8(unsigned char val) noexcept {
82+
append<unsigned char>(val);
83+
}
84+
85+
void Buffer::appendInt16(short val) noexcept {
86+
append<short>(val);
87+
}
88+
void Buffer::appendUInt16(unsigned short val) noexcept {
89+
append<unsigned short>(val);
90+
}
91+
92+
void Buffer::appendInt32(int val) noexcept {
93+
append<int>(val);
94+
}
95+
void Buffer::appendUInt32(unsigned int val) noexcept {
96+
append<unsigned int>(val);
97+
}
98+
99+
void Buffer::appendInt64(long long val) noexcept {
100+
append<long long>(val);
101+
}
102+
void Buffer::appendUInt64(unsigned long long val) noexcept {
103+
append<unsigned long long>(val);
104+
}
105+
106+
void Buffer::appendFloat(float val) noexcept {
107+
union { float fnum; unsigned long inum; } u;
108+
u.fnum = val;
109+
appendUInt32(u.inum);
110+
}
111+
void Buffer::appendDouble(double val) noexcept {
112+
union { double fnum; unsigned long long inum; } u;
113+
u.fnum = val;
114+
appendUInt64(u.inum);
115+
}
116+
117+
/************************* READING *************************/
118+
template <class T> inline T Buffer::read(unsigned long long offset) const {
119+
T result = 0;
120+
unsigned int size = sizeof(T);
121+
122+
if (offset + size > buffer.size())
123+
throw BufferOverflow();
124+
125+
char *dst = (char*) &result;
126+
char *src = (char*) &buffer[offset];
127+
128+
for (unsigned int i = 0; i < size; ++i)
129+
dst[i] = src[size - i - 1];
130+
131+
return result;
132+
}
133+
134+
bool Buffer::readBoolean(unsigned long long offset) const {
135+
return read<bool>(offset);
136+
}
137+
138+
std::string Buffer::readString(unsigned long long size, unsigned long long offset) const {
139+
if (offset + size > buffer.size())
140+
throw BufferOverflow();
141+
142+
std::string result(buffer.begin() + offset, buffer.begin() + offset + size);
143+
144+
return result;
145+
}
146+
147+
char Buffer::readInt8(unsigned long long offset) const {
148+
return read<char>(offset);
149+
}
150+
unsigned char Buffer::readUInt8(unsigned long long offset) const {
151+
return read<unsigned char>(offset);
152+
}
153+
154+
short Buffer::readInt16(unsigned long long offset) const {
155+
return read<short>(offset);
156+
}
157+
unsigned short Buffer::readUInt16(unsigned long long offset) const {
158+
return read<unsigned short>(offset);
159+
}
160+
161+
int Buffer::readInt32(unsigned long long offset) const {
162+
return read<int>(offset);
163+
}
164+
unsigned int Buffer::readUInt32(unsigned long long offset) const {
165+
return read<unsigned int>(offset);
166+
}
167+
168+
long long Buffer::readInt64(unsigned long long offset) const {
169+
return read<long long>(offset);
170+
}
171+
unsigned long long Buffer::readUInt64(unsigned long long offset) const {
172+
return read<unsigned long long>(offset);
173+
}
174+
175+
float Buffer::readFloat(unsigned long long offset) const {
176+
return read<float>(offset);
177+
}
178+
double Buffer::readDouble(unsigned long long offset) const {
179+
return read<double>(offset);
180+
}
181+
182+
/************************* CONSUMING *************************/
183+
template <class T> inline T Buffer::consume() {
184+
T result = 0;
185+
unsigned int size = sizeof(T);
186+
187+
if (size > buffer.size())
188+
throw BufferOverflow();
189+
190+
char *dst = (char*) &result;
191+
char *src = (char*) &buffer[0];
192+
193+
for (unsigned int i = 0; i < size; ++i)
194+
dst[i] = src[size - i - 1];
195+
196+
buffer.erase(buffer.begin(), buffer.begin() + size);
197+
198+
return result;
199+
}
200+
201+
bool Buffer::consumeBool() {
202+
return consume<bool>();
203+
}
204+
205+
std::string Buffer::consumeString(unsigned long long size) {
206+
if (size > buffer.size())
207+
throw BufferOverflow();
208+
209+
std::string result(buffer.begin(), buffer.begin() + size);
210+
211+
buffer.erase(buffer.begin(), buffer.begin() + size);
212+
213+
return result;
214+
}
215+
216+
char Buffer::consumeInt8() {
217+
return consume<char>();
218+
}
219+
unsigned char Buffer::consumeUInt8() {
220+
return consume<unsigned char>();
221+
}
222+
223+
short Buffer::consumeInt16() {
224+
return consume<short>();
225+
}
226+
unsigned short Buffer::consumeUInt16() {
227+
return consume<unsigned short>();
228+
}
229+
230+
int Buffer::consumeInt32() {
231+
return consume<int>();
232+
}
233+
unsigned int Buffer::consumeUInt32() {
234+
return consume<unsigned int>();
235+
}
236+
237+
long long Buffer::consumeInt64() {
238+
return consume<long long>();
239+
}
240+
unsigned long long Buffer::consumeUInt64() {
241+
return consume<unsigned long long>();
242+
}
243+
244+
float Buffer::consumeFloat() {
245+
return consume<float>();
246+
}
247+
double Buffer::consumeDouble() {
248+
return consume<double>();
249+
}
250+
251+
Buffer::~Buffer() {
252+
clear();
253+
}

Diff for: Buffer.hpp

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* This file is part of PHPinnacle/Buffer.
3+
*
4+
* (c) PHPinnacle Team <dev@phpinnacle.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
#pragma once
11+
#include <vector>
12+
#include <sstream>
13+
#include <exception>
14+
15+
struct BufferOverflow : public std::exception {};
16+
17+
class Buffer {
18+
public:
19+
Buffer() noexcept;
20+
Buffer(const std::vector<unsigned char>&) noexcept;
21+
22+
void clear() noexcept;
23+
void discard(unsigned long long n);
24+
25+
unsigned long long size() const noexcept;
26+
bool empty() const noexcept;
27+
28+
void merge(const Buffer&) noexcept;
29+
30+
std::string flush() noexcept;
31+
std::string bytes() const noexcept;
32+
33+
/************************** Writing ***************************/
34+
35+
template <class T> inline void append(const T &val) noexcept;
36+
37+
void appendString(const std::string&) noexcept;
38+
39+
void appendBoolean(bool) noexcept;
40+
41+
void appendInt8(char) noexcept;
42+
void appendUInt8(unsigned char) noexcept;
43+
44+
void appendInt16(short) noexcept;
45+
void appendUInt16(unsigned short) noexcept;
46+
47+
void appendInt32(int) noexcept;
48+
void appendUInt32(unsigned int) noexcept;
49+
50+
void appendInt64(long long) noexcept;
51+
void appendUInt64(unsigned long long) noexcept;
52+
53+
void appendFloat(float) noexcept;
54+
void appendDouble(double) noexcept;
55+
56+
/************************** Reading ***************************/
57+
58+
template <class T> inline T read(unsigned long long offset) const;
59+
60+
std::string readString(unsigned long long size, unsigned long long offset) const;
61+
62+
bool readBoolean(unsigned long long offset) const;
63+
64+
char readInt8(unsigned long long offset) const;
65+
unsigned char readUInt8(unsigned long long offset) const;
66+
67+
short readInt16(unsigned long long offset) const;
68+
unsigned short readUInt16(unsigned long long offset) const;
69+
70+
int readInt32(unsigned long long offset) const;
71+
unsigned int readUInt32(unsigned long long offset) const;
72+
73+
long long readInt64(unsigned long long offset) const;
74+
unsigned long long readUInt64(unsigned long long offset) const;
75+
76+
float readFloat(unsigned long long offset) const;
77+
double readDouble(unsigned long long offset) const;
78+
79+
/************************** Consuming ***************************/
80+
81+
template <class T> inline T consume();
82+
83+
std::string consumeString(unsigned long long size);
84+
85+
bool consumeBool();
86+
87+
char consumeInt8();
88+
unsigned char consumeUInt8();
89+
90+
short consumeInt16();
91+
unsigned short consumeUInt16();
92+
93+
int consumeInt32();
94+
unsigned int consumeUInt32();
95+
96+
long long consumeInt64();
97+
unsigned long long consumeUInt64();
98+
99+
float consumeFloat();
100+
double consumeDouble();
101+
102+
~Buffer();
103+
private:
104+
std::vector<unsigned char> buffer;
105+
};

0 commit comments

Comments
 (0)