-
Notifications
You must be signed in to change notification settings - Fork 1
/
ByteArray.cpp
329 lines (267 loc) · 5.56 KB
/
ByteArray.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
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
#include "ByteArray.hpp"
#include "err.hpp"
#include <string.h>
namespace cornus {
ByteArray::ByteArray() {}
ByteArray::ByteArray(i64 num) {
add_i64(num);
}
ByteArray::ByteArray(u64 num) {
add_u64(num);
}
ByteArray::ByteArray(const ByteArray &rhs)
{
add(rhs.data(), rhs.size());
at_ = 0;
}
ByteArray::~ByteArray()
{
Clear();
}
ByteArray& ByteArray::operator = (const ByteArray &rhs)
{
add(rhs.data(), rhs.size());
at_ = 0;
return *this;
}
bool ByteArray::operator == (const ByteArray &rhs)
{
if (size_ != rhs.size_)
return false;
return (memcmp(data_, rhs.data(), size_) == 0);
}
void ByteArray::Clear() {
delete[] data_;
data_ = nullptr;
size_ = heap_size_ = at_ = 0;
}
ByteArray* ByteArray::CloneFromHere()
{
const i64 left = size_ - at_;
if (left <= 0)
return nullptr;
ByteArray *ret = new ByteArray();
ret->add(this, From::CurrentPosition);
return ret;
}
void ByteArray::add(const ByteArray *ba, const From from)
{
if (!ba)
return; // checking is a must
char *buf;
i64 buf_len;
if (from == From::CurrentPosition)
{
buf = ba->data() + ba->at();
buf_len = ba->size() - ba->at();
} else {
buf = ba->data();
buf_len = ba->size();
}
add(buf, buf_len, ExactSize::Yes);
}
void ByteArray::add(const char *p, const isize size, const ExactSize es)
{
MakeSure(size, es);
memcpy(data_ + at_, p, size);
at_ += size;
size_ += size;
}
void ByteArray::add_i8(const i8 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_u8(const u8 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_i16(const i16 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_u16(const u16 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_i32(const i32 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_u32(const u32 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_i64(const i64 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_u64(const u64 n) {
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_f32(const float n)
{
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_f64(const double n)
{
add(reinterpret_cast<const char*>(&n), sizeof n);
}
void ByteArray::add_string(const QString &s)
{
auto ba = s.toLocal8Bit();
ci32 size = ba.size();
add(reinterpret_cast<const char*>(&size), sizeof size);
add(ba.data(), size);
}
void ByteArray::alloc(const isize n)
{
// Usage:
// ByteArray ba;
// ba.alloc(vallen);
// vallen = lgetxattr(full_path.constData(), key, ba.data(), ba.size());
MakeSure(n, ExactSize::Yes);
size_ = n;
}
void ByteArray::MakeSure(isize more, const ExactSize es)
{
if (heap_size_ >= at_ + more)
return;
heap_size_ += more;
if (es != ExactSize::Yes)
heap_size_ *= 1.3;
char *p = new char[heap_size_];
if (data_ != nullptr)
{
memcpy(p, data_, size_);
delete[] data_;
}
data_ = p;
}
void ByteArray::next(char *p, const isize sz) {
memcpy(p, data_ + at_, sz);
at_ += sz;
}
i8 ByteArray::next_i8() {
i8 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
u8 ByteArray::next_u8() {
u8 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
i16 ByteArray::next_i16() {
i16 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
u16 ByteArray::next_u16() {
u16 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
i32 ByteArray::next_i32() {
i32 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
u32 ByteArray::next_u32() {
u32 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
i64 ByteArray::next_i64() {
i64 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
u64 ByteArray::next_u64() {
u64 n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
f32 ByteArray::next_f32()
{
float n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
f64 ByteArray::next_f64()
{
double n;
next(reinterpret_cast<char*>(&n), sizeof n);
return n;
}
QString ByteArray::next_string()
{
ci32 size = next_i32();
auto s = QString::fromLocal8Bit(data_ + at_, size);
at_ += size;
return s;
}
bool ByteArray::Receive(cint fd, const CloseSocket cs)
{
i64 total_bytes;
if (::read(fd, (char*)&total_bytes, sizeof(total_bytes)) != sizeof(total_bytes))
{
mtl_trace();
return false;
}
MakeSure(total_bytes, ExactSize::Yes);
size_ = 0;
while (true)
{
i64 count = ::read(fd, data_ + size_, total_bytes - size_);
if (count == -1) {
perror("read");
break;
} else if (count == 0) {
break; // EOF
}
size_ += count;
}
at_ = 0;
if (cs == CloseSocket::Yes)
{
cint status = ::close(fd);
if (status != 0)
mtl_status(errno);
}
return size_ == total_bytes;
}
bool ByteArray::Send(int fd, const CloseSocket cs) const
{
if (fd == -1)
return false;
i64 so_far;
{ // first send buffer size:
so_far = write(fd, (char*)&size_, sizeof(size_));
if (so_far != sizeof(size_)) {
if (cs == CloseSocket::Yes)
::close(fd);
return false;
}
}
so_far = 0;
while (so_far < size_)
{
ci64 count = write(fd, data_ + so_far, size_ - so_far);
if (count == -1) {
if (errno == EAGAIN)
continue;
mtl_status(errno);
if (cs == CloseSocket::Yes)
::close(fd);
return false;
} else if (count == 0) { // EOF
break;
}
so_far += count;
}
if (cs == CloseSocket::Yes)
{
cint status = ::close(fd);
if (status != 0)
mtl_status(errno);
}
return so_far == size_;
}
void ByteArray::set_msg_id(const io::Message msg_type)
{
add(reinterpret_cast<const char*>(&msg_type), sizeof msg_type);
}
}