-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer_out.cpp
124 lines (97 loc) · 2.03 KB
/
buffer_out.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
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
#include "buffer_out.h"
buffer_out::buffer_out()
{
}
buffer_out::~buffer_out()
{
}
void buffer_out::add_net_byte(const uint8_t b)
{
buffer.push_back(b);
}
void buffer_out::add_net_short(const uint16_t s)
{
buffer.push_back(s >> 8);
buffer.push_back(s);
}
void buffer_out::add_net_long(const uint32_t l)
{
buffer.push_back(l >> 24);
buffer.push_back(l >> 16);
buffer.push_back(l >> 8);
buffer.push_back(l);
}
size_t buffer_out::add_net_short(const uint16_t s, const ssize_t offset)
{
if (offset != -1) {
buffer.at(offset + 0) = s >> 8;
buffer.at(offset + 1) = s;
return offset;
}
size_t o = buffer.size();
buffer.push_back(s >> 8);
buffer.push_back(s);
return o;
}
size_t buffer_out::add_net_long(const uint32_t l, const ssize_t offset)
{
if (offset != -1) {
buffer.at(offset + 0) = l >> 24;
buffer.at(offset + 1) = l >> 16;
buffer.at(offset + 2) = l >> 8;
buffer.at(offset + 3) = l;
return offset;
}
size_t o = buffer.size();
buffer.push_back(l >> 24);
buffer.push_back(l >> 16);
buffer.push_back(l >> 8);
buffer.push_back(l);
return o;
}
void buffer_out::add_any_addr(const any_addr & a)
{
int len = a.get_len();
for(int i=0; i<len; i++)
buffer.push_back(a[i]);
}
void buffer_out::add_buffer_out(const buffer_out & o)
{
for(auto b : o.get_payload())
buffer.push_back(b);
}
void buffer_out::add_buffer_in(buffer_in & i)
{
int n = i.get_n_bytes_left();
for(int k=0; k<n; k++)
buffer.push_back(i.get_net_byte());
}
void buffer_out::add_buffer(const uint8_t *const p, const size_t l)
{
for(size_t i=0; i<l; i++)
buffer.push_back(p[i]);
}
void buffer_out::add_padding(const int m)
{
int padding = m - (buffer.size() % m);
if (padding != m) {
while(padding)
buffer.push_back(0), padding--;
}
}
const std::vector<uint8_t> & buffer_out::get_payload() const
{
return buffer;
}
bool buffer_out::compare(const buffer_in & b) const
{
return b.peek() == buffer;
}
size_t buffer_out::get_size() const
{
return buffer.size();
}
const uint8_t *buffer_out::get_content() const
{
return buffer.data();
}