-
Notifications
You must be signed in to change notification settings - Fork 10
/
ByteBuffer.h
executable file
·179 lines (145 loc) · 5.03 KB
/
ByteBuffer.h
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
/**
ByteBuffer
ByteBuffer.h
Copyright 2011 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
// If defined, utility functions within the class are enabled
#define BB_UTILITY
#include <cstdlib>
#include <cstring>
#include <vector>
#ifdef BB_UTILITY
#include <iostream>
#include <stdio.h>
#endif
using namespace std;
typedef unsigned char byte;
class ByteBuffer {
private:
unsigned int rpos, wpos;
vector<byte> buf;
#ifdef BB_UTILITY
string name;
#endif
template <typename T> T read() {
T data = read<T>(rpos);
rpos += sizeof(T);
return data;
}
template <typename T> T read(unsigned int index) const {
if(index + sizeof(T) <= buf.size())
return *((T*)&buf[index]);
return 0;
}
template <typename T> void append(T data) {
unsigned int s = sizeof(data);
if (size() < (wpos + s))
buf.resize(wpos + s);
memcpy(&buf[wpos], (byte*)&data, s);
wpos += s;
}
template <typename T> void insert(T data, unsigned int index) {
if((index + sizeof(data)) > size())
return;
memcpy(&buf[index], (byte*)&data, sizeof(data));
wpos = index+sizeof(data);
}
public:
ByteBuffer(unsigned int size = 4096);
ByteBuffer(byte* arr, unsigned int size);
~ByteBuffer();
unsigned int bytesRemaining(); // 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
ByteBuffer* clone(); // Return a new instance of a bytebuffer with the exact same contents and the same state (rpos, wpos)
//ByteBuffer compact(); // TODO?
bool equals(ByteBuffer* other); // Compare if the contents are equivalent
void resize(unsigned int newSize);
unsigned int size(); // Size of internal vector
// Basic Searching (Linear)
template <typename T> int find(T key, unsigned int start=0) {
int ret = -1;
unsigned int len = buf.size();
for(unsigned int 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(byte key, byte rep, unsigned int start = 0, bool firstOccuranceOnly=false);
// Read
byte peek(); // Relative peek. Reads and returns the next byte in the buffer from the current position but does not increment the read position
byte get(); // Relative get method. Reads the byte at the buffers current position then increments the position
byte get(unsigned int index); // Absolute get method. Read byte at index
void getBytes(byte* buf, unsigned int len); // Absolute read into array buf of length len
char getChar(); // Relative
char getChar(unsigned int index); // Absolute
double getDouble();
double getDouble(unsigned int index);
float getFloat();
float getFloat(unsigned int index);
int getInt();
int getInt(unsigned int index);
long getLong();
long getLong(unsigned int index);
short getShort();
short getShort(unsigned int index);
// Write
void put(ByteBuffer* src); // Relative write of the entire contents of another ByteBuffer (src)
void put(byte b); // Relative write
void put(byte b, unsigned int index); // Absolute write at index
void putBytes(byte* b, unsigned int len); // Relative write
void putBytes(byte* b, unsigned int len, unsigned int index); // Absolute write starting at index
void putChar(char value); // Relative
void putChar(char value, unsigned int index); // Absolute
void putDouble(double value);
void putDouble(double value, unsigned int index);
void putFloat(float value);
void putFloat(float value, unsigned int index);
void putInt(int value);
void putInt(int value, unsigned int index);
void putLong(long value);
void putLong(long value, unsigned int index);
void putShort(short value);
void putShort(short value, unsigned int index);
// Buffer Position Accessors & Mutators
void setReadPos(unsigned int r) {
rpos = r;
}
int getReadPos() {
return rpos;
}
void setWritePos(unsigned int w) {
wpos = w;
}
int getWritePos() {
return wpos;
}
// Utility Functions
#ifdef BB_UTILITY
void setName(string n);
string getName();
void printAscii();
void printHex();
void printPosition();
#endif
};
#endif