-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.cpp
executable file
·92 lines (72 loc) · 1.22 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
#include "Buffer.h"
void Buffer::reset(int size )
{
buf = bufstart;
cur=0;
this->size = size;
}
int Buffer::getSize()
{
return cur;
}
int Buffer::getCurr()
{
return cur;
}
uint8_t* Buffer::getBuffer()
{
return bufstart;
}
uint16_t Buffer::getShort()
{
consume(2);
return *(uint16_t*)(buf-2);
}
uint32_t Buffer::getLong()
{
consume(4);
return *(uint32_t*)(buf-4);
}
uint64_t Buffer::getLongLong()
{
consume(8);
return *(uint64_t*)(buf-8);
}
void Buffer::consume(int n){
buf = buf+n;
cur = cur+n;
}
uint8_t Buffer::getByte(){
consume(1);
return *(buf-1);
}
char* Buffer::getRaw(){
return (char*)buf;
}
void Buffer::append(uint16_t input){
*(uint16_t*)buf = input;
consume(2);
}
void Buffer::append(uint64_t input){
*(uint64_t*)buf = input;
consume(8);
}
void Buffer::append(uint32_t input){
*(uint32_t*)buf = input;
consume(4);
}
void Buffer::append(uint8_t input){
*buf = input;
consume(1);
}
void Buffer::appendLengthValue(std::string& input){
*buf = (uint8_t)input.size();
consume(1);
memcpy(buf,input.c_str(),input.size());
consume(input.size());
}
void Buffer::getLengthValue(std::string& input){
uint8_t len = getByte();
input.assign(getRaw(),len);
consume(len);
}