-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathByteBuf.hpp
66 lines (57 loc) · 1.38 KB
/
ByteBuf.hpp
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
#pragma once
extern "C" {
#include "pkcs7_padding.h"
}
class ByteBuf {
private:
uint8_t *mem = nullptr;
uint32_t data_size = 0;
uint32_t pos = 0;
ByteBuf() = default;
public:
explicit ByteBuf(uint32_t size) {
this->data_size = size;
this->mem = new uint8_t[size];
}
ByteBuf(const uint8_t *data, uint32_t size) {
this->data_size = size;
this->mem = new uint8_t[size];
memcpy(this->mem, data, size);
}
~ByteBuf() {
if (this->mem != nullptr) {
delete[] this->mem;
this->mem = nullptr;
this->data_size = 0;
}
}
uint8_t * data() {
return this->mem;
}
uint32_t read_uint32() {
if (this->mem == nullptr) {
return -1;
}
if (pos + 4 > data_size) {
return -1;
}
uint32_t result = *(uint32_t *)&mem[pos];
pos = 4;
return result;
}
[[nodiscard]] uint32_t size() const {
return data_size;
}
uint32_t remove_padding() {
auto padding_size = pkcs7_padding_data_length(mem, data_size, 16);
if (padding_size == 0) {
return data_size;
}
data_size = padding_size;
auto dst = new uint8_t[data_size];
std::memcpy(dst, mem, data_size);
delete[] mem;
mem = dst;
return data_size;
}
};