-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSerializers.h
240 lines (190 loc) · 6.88 KB
/
Serializers.h
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
#ifndef SERIALIZERS_H
#define SERIALIZERS_H
extern "C" {
#include "Zend/zend_string.h"
#include "Zend/zend_exceptions.h"
}
#include "classes/DataDecodeException.h"
#include <algorithm>
#include <iterator>
#include <type_traits>
enum class ByteOrder {
BigEndian,
LittleEndian,
#ifdef WORDS_BIGENDIAN
Native = BigEndian
#else
Native = LittleEndian
#endif
};
template<typename TValue>
union Flipper {
char bytes[sizeof(TValue)];
TValue value;
};
template<typename TValue>
static inline bool readByte(unsigned char* buffer, size_t used, size_t& offset, TValue& result) {
const auto SIZE = sizeof(TValue);
if (used - offset < SIZE) {
zend_throw_exception_ex(data_decode_exception_ce, 0, "Need at least %zu bytes, but only have %zu bytes", SIZE, used - offset);
return false;
}
result = *(reinterpret_cast<TValue*>(&buffer[offset]));
offset += SIZE;
return true;
}
template<typename TValue, ByteOrder byteOrder>
static inline bool readFixedSizeType(unsigned char* bytes, size_t used, size_t& offset, TValue& result) {
const auto SIZE = sizeof(TValue);
if (used - offset < SIZE) {
zend_throw_exception_ex(data_decode_exception_ce, 0, "Need at least %zu bytes, but only have %zu bytes", SIZE, used - offset);
return false;
}
Flipper<TValue> flipper;
memcpy(flipper.bytes, &bytes[offset], sizeof(flipper.bytes));
if (byteOrder != ByteOrder::Native) {
std::reverse(std::begin(flipper.bytes), std::end(flipper.bytes));
}
result = flipper.value;
offset += SIZE;
return true;
}
template<typename TValue, ByteOrder byteOrder>
static inline bool readInt24(unsigned char* bytes, size_t used, size_t& offset, TValue& result) {
const size_t SIZE = 3;
if (used - offset < SIZE) {
zend_throw_exception_ex(data_decode_exception_ce, 0, "Need at least %zu bytes, but only have %zu bytes", SIZE, used - offset);
return false;
}
result = 0;
if (byteOrder == ByteOrder::LittleEndian) {
result |= bytes[offset];
result |= bytes[offset + 1] << 8;
result |= bytes[offset + 2] << 16;
}
else {
result |= bytes[offset + 2];
result |= bytes[offset + 1] << 8;
result |= bytes[offset] << 16;
}
const size_t SIGNED_SHIFT = std::is_signed<TValue>::value ? (sizeof(TValue) - SIZE) * CHAR_BIT : 0;
if (SIGNED_SHIFT > 0) {
result = (result << SIGNED_SHIFT) >> SIGNED_SHIFT;
}
offset += SIZE;
return true;
}
struct VarIntConstants {
static const unsigned char BITS_PER_BYTE = 7u;
template<size_t TYPE_BITS>
static const unsigned char MAX_BYTES = TYPE_BITS / BITS_PER_BYTE + ((TYPE_BITS % BITS_PER_BYTE) > 0);
static const unsigned char VALUE_MASK = static_cast<unsigned char>(~(1u << BITS_PER_BYTE));
static const unsigned char MSB_MASK = static_cast<unsigned char>(1u << BITS_PER_BYTE);
};
template<typename TValue>
static inline bool readUnsignedVarInt(unsigned char* bytes, size_t used, size_t& offset, TValue& result) {
const auto TYPE_BITS = sizeof(TValue) * CHAR_BIT;
result = 0;
for (unsigned int shift = 0; shift < TYPE_BITS; shift += VarIntConstants::BITS_PER_BYTE) {
if (offset >= used) {
zend_throw_exception(data_decode_exception_ce, "No bytes left in buffer", 0);
return false;
}
auto byte = bytes[offset++];
result |= (byte & VarIntConstants::VALUE_MASK) << shift;
if ((byte & VarIntConstants::MSB_MASK) == 0) {
return true;
}
}
zend_throw_exception_ex(data_decode_exception_ce, 0, "VarInt did not terminate after %u bytes!", VarIntConstants::MAX_BYTES<TYPE_BITS>);
return false;
}
template<typename TUnsignedValue, typename TSignedValue>
static inline bool readSignedVarInt(unsigned char* bytes, size_t used, size_t& offset, TSignedValue& result) {
TUnsignedValue unsignedResult;
if (!readUnsignedVarInt<TUnsignedValue>(bytes, used, offset, unsignedResult)) {
return false;
}
TUnsignedValue mask = 0;
if (unsignedResult & 1) {
//we don't know the type of TUnsignedValue here so we can't just use ~0
mask = ~mask;
}
result = static_cast<TSignedValue>((unsignedResult >> 1) ^ mask);
return true;
}
static inline void extendBuffer(zend_string*& buffer, size_t offset, size_t usedBytes) {
size_t requiredSize = offset + usedBytes;
if (ZSTR_LEN(buffer) < requiredSize) {
size_t doubleSize = ZSTR_LEN(buffer) * 2;
buffer = zend_string_realloc(buffer, doubleSize > requiredSize ? doubleSize : requiredSize, 0);
ZSTR_VAL(buffer)[ZSTR_LEN(buffer)] = '\0'; //make sure null terminator is always set, to stop sprintf reading out-of-bounds
}
else {
buffer = zend_string_separate(buffer, 0);
}
}
template<typename TValue>
static void writeByte(zend_string*& buffer, size_t& offset, TValue value) {
extendBuffer(buffer, offset, sizeof(TValue));
ZSTR_VAL(buffer)[offset] = *reinterpret_cast<char*>(&value);
offset += sizeof(TValue);
}
template<typename TValue, ByteOrder byteOrder>
static void writeFixedSizeType(zend_string*& buffer, size_t& offset, TValue value) {
extendBuffer(buffer, offset, sizeof(TValue));
Flipper<TValue> flipper;
flipper.value = value;
if (byteOrder != ByteOrder::Native) {
std::reverse(std::begin(flipper.bytes), std::end(flipper.bytes));
}
memcpy(&ZSTR_VAL(buffer)[offset], flipper.bytes, sizeof(flipper.bytes));
offset += sizeof(TValue);
}
template<typename TValue, ByteOrder byteOrder>
static void writeInt24(zend_string*& buffer, size_t& offset, TValue value) {
const size_t SIZE = 3;
extendBuffer(buffer, offset, SIZE);
if (byteOrder == ByteOrder::LittleEndian) {
ZSTR_VAL(buffer)[offset] = value & 0xff;
ZSTR_VAL(buffer)[offset + 1] = (value >> 8) & 0xff;
ZSTR_VAL(buffer)[offset + 2] = (value >> 16) & 0xff;
}
else {
ZSTR_VAL(buffer)[offset] = (value >> 16) & 0xff;
ZSTR_VAL(buffer)[offset + 1] = (value >> 8) & 0xff;
ZSTR_VAL(buffer)[offset + 2] = value & 0xff;
}
offset += SIZE;
}
template<typename TValue>
static inline void writeUnsignedVarInt(zend_string*& buffer, size_t& offset, TValue value) {
const auto TYPE_BITS = sizeof(TValue) * CHAR_BIT;
char result[VarIntConstants::MAX_BYTES<TYPE_BITS>];
TValue remaining = value;
for (auto i = 0; i < VarIntConstants::MAX_BYTES<TYPE_BITS>; i++) {
unsigned char nextByte = remaining & VarIntConstants::VALUE_MASK;
TValue nextRemaining = remaining >> VarIntConstants::BITS_PER_BYTE;
if (nextRemaining == 0) {
result[i] = nextByte;
auto usedBytes = i + 1;
extendBuffer(buffer, offset, usedBytes);
memcpy(&ZSTR_VAL(buffer)[offset], &result[0], usedBytes);
offset += usedBytes;
return;
}
result[i] = nextByte | VarIntConstants::MSB_MASK;
remaining = nextRemaining;
}
zend_value_error("Value too large to be encoded as a VarInt");
}
template<typename TUnsignedType, typename TSignedType>
static inline void writeSignedVarInt(zend_string*& buffer, size_t& offset, TSignedType value) {
TUnsignedType mask = 0;
if (value < 0) {
//we don't know the type of TUnsignedType here, can't use ~0 directly (the compiler will optimise this anyway)
mask = ~mask;
}
writeUnsignedVarInt<TUnsignedType>(buffer, offset, (static_cast<TUnsignedType>(value) << 1) ^ mask);
}
#endif