Menu

[r804]: / Trunk / Sources / ByteBuffer.cpp  Maximize  Restore  History

Download this file

454 lines (383 with data), 10.2 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
ByteBuffer
ByteBuffer.cpp
Copyright 2011 - 2013 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.
*/
#include "CvGameCoreDLL.h"
#include "ByteBuffer.h"
/**
* ByteBuffer constructor
* Reserves specified size in internal vector
*
* @param size Size (in bytes) of space to preallocate internally. Default is set in DEFAULT_SIZE
*/
ByteBuffer::ByteBuffer(uint32_t size) {
buf.reserve(size);
clear();
#ifdef BB_UTILITY
name = "";
#endif
}
/**
* ByteBuffer constructor
* Consume an entire uint8_t array of length len in the ByteBuffer
*
* @param arr uint8_t array of data (should be of length len)
* @param size Size of space to allocate
*/
ByteBuffer::ByteBuffer(uint8_t* arr, uint32_t size) {
// If the provided array is NULL, allocate a blank buffer of the provided size
if (arr == NULL) {
buf.reserve(size);
clear();
}
else { // Consume the provided array
buf.reserve(size);
clear();
putBytes(arr, size);
}
#ifdef BB_UTILITY
name = "";
#endif
}
/**
* ByteBuffer Deconstructor
*
*/
ByteBuffer::~ByteBuffer() {
}
/**
* Bytes Remaining
* Returns the number of bytes from the current read position till the end of the buffer
*
* @return Number of bytes from rpos to the end (size())
*/
uint32_t ByteBuffer::bytesRemaining() {
return size() - rpos;
}
/**
* Clear
* Clears out all data from the internal vector (original preallocated size remains), resets the positions to 0
*/
void ByteBuffer::clear() {
rpos = 0;
wpos = 0;
buf.clear();
}
/**
* Clone
* Allocate an exact copy of the ByteBuffer on the heap and return a pointer
*
* @return A pointer to the newly cloned ByteBuffer. NULL if no more memory available
*/
ByteBuffer* ByteBuffer::clone() {
ByteBuffer* ret = new ByteBuffer(buf.size());
// Copy data
for (uint32_t i = 0; i < buf.size(); i++) {
ret->put((uint8_t)get(i));
}
// Reset positions
ret->setReadPos(0);
ret->setWritePos(0);
return ret;
}
/**
* Equals, test for data equivilancy
* Compare this ByteBuffer to another by looking at each byte in the internal buffers and making sure they are the same
*
* @param other A pointer to a ByteBuffer to compare to this one
* @return True if the internal buffers match. False if otherwise
*/
bool ByteBuffer::equals(ByteBuffer* other) {
// If sizes aren't equal, they can't be equal
if (size() != other->size())
return false;
// Compare byte by byte
uint32_t len = size();
for (uint32_t i = 0; i < len; i++) {
if ((uint8_t)get(i) != (uint8_t)other->get(i))
return false;
}
return true;
}
/**
* Resize
* Reallocates memory for the internal buffer of size newSize. Read and write positions will also be reset
*
* @param newSize The amount of memory to allocate
*/
void ByteBuffer::resize(uint32_t newSize) {
buf.resize(newSize);
rpos = 0;
wpos = 0;
}
/**
* Size
* Returns the size of the internal buffer...not necessarily the length of bytes used as data!
*
* @return size of the internal buffer
*/
uint32_t ByteBuffer::size() {
return buf.size();
}
// Replacement
/**
* Replace
* Replace occurance of a particular uint8_t, key, with the uint8_t rep
*
* @param key uint8_t to find for replacement
* @param rep uint8_t to replace the found key with
* @param start Index to start from. By default, start is 0
* @param firstOccuranceOnly If true, only replace the first occurance of the key. If false, replace all occurances. False by default
*/
void ByteBuffer::replace(uint8_t key, uint8_t rep, uint32_t start, bool firstOccuranceOnly) {
uint32_t len = buf.size();
for (uint32_t i = start; i < len; i++) {
uint8_t data = read<uint8_t>(i);
// Wasn't actually found, bounds of buffer were exceeded
if ((key != 0) && (data == 0))
break;
// Key was found in array, perform replacement
if (data == key) {
buf[i] = rep;
if (firstOccuranceOnly)
return;
}
}
}
// Read Functions
uint8_t ByteBuffer::peek() {
return read<uint8_t>(rpos);
}
uint8_t ByteBuffer::get() {
return read<uint8_t>();
}
uint8_t ByteBuffer::get(uint32_t index) {
return read<uint8_t>(index);
}
void ByteBuffer::getBytes(uint8_t* buf, uint32_t len) {
for (uint32_t i = 0; i < len; i++) {
buf[i] = read<uint8_t>();
}
}
char ByteBuffer::getChar() {
return read<char>();
}
char ByteBuffer::getChar(uint32_t index) {
return read<char>(index);
}
double ByteBuffer::getDouble() {
return read<double>();
}
double ByteBuffer::getDouble(uint32_t index) {
return read<double>(index);
}
float ByteBuffer::getFloat() {
return read<float>();
}
float ByteBuffer::getFloat(uint32_t index) {
return read<float>(index);
}
uint32_t ByteBuffer::getInt() {
return read<uint32_t>();
}
uint32_t ByteBuffer::getInt(uint32_t index) {
return read<uint32_t>(index);
}
uint64_t ByteBuffer::getLong() {
return read<uint64_t>();
}
uint64_t ByteBuffer::getLong(uint32_t index) {
return read<uint64_t>(index);
}
uint16_t ByteBuffer::getShort() {
return read<uint16_t>();
}
uint16_t ByteBuffer::getShort(uint32_t index) {
return read<uint16_t>(index);
}
// Write Functions
void ByteBuffer::put(ByteBuffer* src) {
uint32_t len = src->size();
for (uint32_t i = 0; i < len; i++)
append<uint8_t>(src->get(i));
}
void ByteBuffer::put(uint8_t b) {
append<uint8_t>(b);
}
void ByteBuffer::put(uint8_t b, uint32_t index) {
insert<uint8_t>(b, index);
}
void ByteBuffer::putBytes(uint8_t* b, uint32_t len) {
// Insert the data one byte at a time into the internal buffer at position i+starting index
for (uint32_t i = 0; i < len; i++)
append<uint8_t>(b[i]);
}
void ByteBuffer::putBytes(uint8_t* b, uint32_t len, uint32_t index) {
wpos = index;
// Insert the data one byte at a time into the internal buffer at position i+starting index
for (uint32_t i = 0; i < len; i++)
append<uint8_t>(b[i]);
}
void ByteBuffer::putChar(char value) {
append<char>(value);
}
void ByteBuffer::putChar(char value, uint32_t index) {
insert<char>(value, index);
}
void ByteBuffer::putDouble(double value) {
append<double>(value);
}
void ByteBuffer::putDouble(double value, uint32_t index) {
insert<double>(value, index);
}
void ByteBuffer::putFloat(float value) {
append<float>(value);
}
void ByteBuffer::putFloat(float value, uint32_t index) {
insert<float>(value, index);
}
void ByteBuffer::putInt(uint32_t value) {
append<uint32_t>(value);
}
void ByteBuffer::putInt(uint32_t value, uint32_t index) {
insert<uint32_t>(value, index);
}
void ByteBuffer::putLong(uint64_t value) {
append<uint64_t>(value);
}
void ByteBuffer::putLong(uint64_t value, uint32_t index) {
insert<uint64_t>(value, index);
}
void ByteBuffer::putShort(uint16_t value) {
append<uint16_t>(value);
}
void ByteBuffer::putShort(uint16_t value, uint32_t index) {
insert<uint16_t>(value, index);
}
void ByteBuffer::putString(const wchar *szName)
{
int iLen = szName == NULL ? 0 : std::wcslen(szName);
putInt(iLen);
for (int i = 0; i < iLen; i++)
{
putChar((char)szName[i]);
}
}
void ByteBuffer::putString(const char *szName)
{
int iLen = szName == NULL ? 0 : std::strlen(szName);
putInt(iLen);
for (int i = 0; i < iLen; i++)
{
putChar(szName[i]);
}
}
void ByteBuffer::putString(const std::string& szName)
{
putString(szName.c_str());
}
void ByteBuffer::putString(const std::wstring& szName)
{
putString(szName.c_str());
}
void ByteBuffer::getString(char *szName)
{
int iLen = getInt();
if (iLen != 0)
{
for (int i = 0; i < iLen; i++)
{
char c = getChar();
*szName = c;
szName++;
}
}
}
void ByteBuffer::getString(wchar *szName)
{
int iLen = getInt();
if (iLen != 0)
{
for (int i = 0; i < iLen; i++)
{
wchar c = getChar();
*szName = c;
szName++;
}
}
}
void ByteBuffer::getString(std::string& szName)
{
szName.clear();
int iLen = getInt();
for (int i = 0; i < iLen; i++)
{
char c = getChar();
szName += c;
}
}
void ByteBuffer::getString(std::wstring& szName)
{
szName.clear();
int iLen = getInt();
for (int i = 0; i < iLen; i++)
{
wchar c = getChar();
szName += c;
}
}
// Utility Functions
#ifdef BB_UTILITY
void ByteBuffer::setName(std::string n) {
name = n;
}
std::string ByteBuffer::getName() {
return name;
}
void ByteBuffer::printInfo() {
uint32_t length = buf.size();
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Info Print" << std::endl;
}
void ByteBuffer::printAH() {
uint32_t length = buf.size();
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII & Hex Print" << std::endl;
for (uint32_t i = 0; i < length; i++) {
printf("0x%02x ", buf[i]);
}
printf("\n");
for (uint32_t i = 0; i < length; i++) {
printf("%c ", buf[i]);
}
printf("\n");
}
void ByteBuffer::printAscii() {
uint32_t length = buf.size();
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII Print" << std::endl;
for (uint32_t i = 0; i < length; i++) {
printf("%c ", buf[i]);
}
printf("\n");
}
void ByteBuffer::printHex() {
uint32_t length = buf.size();
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Hex Print" << std::endl;
for (uint32_t i = 0; i < length; i++) {
printf("0x%02x ", buf[i]);
}
printf("\n");
}
void ByteBuffer::printPosition() {
uint32_t length = buf.size();
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << " Read Pos: " << rpos << ". Write Pos: " << wpos << std::endl;
}
#endif