-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathByteBuffer.h
More file actions
executable file
·190 lines (156 loc) · 5.64 KB
/
Copy pathByteBuffer.h
File metadata and controls
executable file
·190 lines (156 loc) · 5.64 KB
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
/**
ByteBuffer
ByteBuffer.h
Copyright 2011-2015 Ramsey Kant
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _BYTEBUFFER_H_
#define _BYTEBUFFER_H_
#include <cstdint>
#include <cstring>
#include <vector>
#include <memory>
#ifdef BB_UTILITY
#include <string>
#endif
// Default number of bytes to allocate in the backing buffer if no size is provided
constexpr uint32_t BB_DEFAULT_SIZE = 4096;
#ifdef BB_USE_NS
namespace bb {
#endif
class ByteBuffer {
public:
explicit ByteBuffer(uint32_t size = BB_DEFAULT_SIZE);
explicit ByteBuffer(const uint8_t* arr, uint32_t size);
virtual ~ByteBuffer() = default;
uint32_t bytesRemaining() const; // Number of bytes from the current read position till the end of the buffer
void clear(); // Clear our the vector and reset read and write positions
std::unique_ptr<ByteBuffer> clone(); // Return a new instance of a ByteBuffer with the exact same contents and the same state (rpos, wpos)
bool equals(const ByteBuffer* other) const; // Compare if the contents are equivalent
void resize(uint32_t newSize);
uint32_t size() const; // Size of internal vector
// Basic Searching (Linear)
template<typename T> int32_t find(T key, uint32_t start=0) {
int32_t ret = -1;
uint32_t len = buf.size();
for (uint32_t i = start; i < len; i++) {
T data = read<T>(i);
// Wasn't actually found, bounds of buffer were exceeded
if ((key != 0) && (data == 0))
break;
// Key was found in array
if (data == key) {
ret = i;
break;
}
}
return ret;
}
// Replacement
void replace(uint8_t key, uint8_t rep, uint32_t start = 0, bool firstOccurrenceOnly=false);
// Read
uint8_t peek() const; // Relative peek. Reads and returns the next byte in the buffer from the current position but does not increment the read position
uint8_t get(); // Relative get method. Reads the byte at the buffers current position then increments the position
uint8_t get(uint32_t index) const; // Absolute get method. Read byte at index
void getBytes(uint8_t* const out_buf, uint32_t out_len); // Absolute read into array buf of length len
char getChar(); // Relative
char getChar(uint32_t index) const; // Absolute
double getDouble();
double getDouble(uint32_t index) const;
float getFloat();
float getFloat(uint32_t index) const;
uint32_t getInt();
uint32_t getInt(uint32_t index) const;
uint64_t getLong();
uint64_t getLong(uint32_t index) const;
uint16_t getShort();
uint16_t getShort(uint32_t index) const;
// Write
void put(const ByteBuffer* src); // Relative write of the entire contents of another ByteBuffer (src)
void put(uint8_t b); // Relative write
void put(uint8_t b, uint32_t index); // Absolute write at index
void putBytes(const uint8_t* const b, uint32_t len); // Relative write
void putBytes(const uint8_t* const b, uint32_t len, uint32_t index); // Absolute write starting at index
void putChar(char value); // Relative
void putChar(char value, uint32_t index); // Absolute
void putDouble(double value);
void putDouble(double value, uint32_t index);
void putFloat(float value);
void putFloat(float value, uint32_t index);
void putInt(uint32_t value);
void putInt(uint32_t value, uint32_t index);
void putLong(uint64_t value);
void putLong(uint64_t value, uint32_t index);
void putShort(uint16_t value);
void putShort(uint16_t value, uint32_t index);
// Buffer Position Accessors & Mutators
void setReadPos(uint32_t r) {
rpos = r;
}
uint32_t getReadPos() const {
return rpos;
}
void setWritePos(uint32_t w) {
wpos = w;
}
uint32_t getWritePos() const {
return wpos;
}
// Utility Functions
#ifdef BB_UTILITY
void setName(std::string_view n);
std::string getName() const;
void printInfo() const;
void printAH() const;
void printAscii() const;
void printHex() const;
void printPosition() const;
#endif
private:
uint32_t rpos = 0;
uint32_t wpos = 0;
std::vector<uint8_t> buf;
#ifdef BB_UTILITY
std::string name = "";
#endif
template<typename T> T read() {
T data = read<T>(rpos);
rpos += sizeof(T);
return data;
}
template<typename T> T read(uint32_t index) const {
if (index + sizeof(T) <= buf.size()) {
T val;
std::memcpy(&val, &buf[index], sizeof(T));
return val;
}
return T{};
}
template<typename T> void append(T data) {
constexpr size_t s = sizeof(T);
if (size() < static_cast<size_t>(wpos) + s)
buf.resize(static_cast<size_t>(wpos) + s);
memcpy(&buf[wpos], (uint8_t*)&data, s);
wpos += s;
}
template<typename T> void insert(T data, uint32_t index) {
const size_t end = static_cast<size_t>(index) + sizeof(T);
if (end > size()) {
buf.resize(end);
}
memcpy(&buf[index], (uint8_t*)&data, sizeof(T));
wpos = index + sizeof(T);
}
};
#ifdef BB_USE_NS
}
#endif
#endif