forked from rainerschoe/ZeroCopyMessagePack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decoder.cpp
418 lines (384 loc) · 11.1 KB
/
Decoder.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2021 Rainer Schoenberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "Decoder.hpp"
#include <string.h>
namespace ZCMessagePack
{
Decoder Decoder::operator[](const char * f_mapKey) const
{
Decoder newDecoder = *this;
newDecoder.seekElementByKey(f_mapKey);
return newDecoder;
}
Decoder Decoder::accessArray(uint8_t f_index) const
{
Decoder newDecoder = *this;
newDecoder.seekElementByIndex(f_index);
return newDecoder;
}
void Decoder::seekElementByIndex(uint8_t f_index)
{
if(not m_validSeek)
{
return;
}
HeaderInfo header = decodeHeader();
if(header.headerType != HeaderInfo::Array)
{
m_validSeek = false;
return;
}
if(header.numPayloadElements <= f_index)
{
m_validSeek = false;
return;
}
m_position += header.headerSize;
for(uint16_t elementNumber = 0; elementNumber < f_index; elementNumber++)
{
seekNextElement();
}
return;
}
void Decoder::seekElementByKey(const char * f_key)
{
if(not m_validSeek)
{
return;
}
HeaderInfo header = decodeHeader();
if(header.headerType != HeaderInfo::Map)
{
m_validSeek = false;
return;
}
m_position += header.headerSize;
for(uint16_t elementNumber = 0; elementNumber < header.numPayloadElements; elementNumber++)
{
auto maybeKeyMatch = compareString(f_key);
if(not maybeKeyMatch.isValid())
{
// key could not be decoded...
m_validSeek = false;
return;
}
if(maybeKeyMatch.get())
{
// key is a match, skip it to reach payload:
seekNextElement();
return;
}
else
{
// key does not match, skip key and value:
seekNextElement();
seekNextElement();
}
}
m_validSeek = false;
return;
}
void Decoder::seekNextElement()
{
HeaderInfo header = decodeHeader();
switch(header.headerType)
{
case HeaderInfo::InvalidHeader:
m_validSeek = false;
return;
case HeaderInfo::Map:
m_position += header.headerSize;
for(uint16_t elementNumber = 0; elementNumber < header.numPayloadElements; elementNumber++)
{
uint32_t startpos = m_position;
seekNextElement();
seekNextElement();
}
return;
case HeaderInfo::Array:
m_position += header.headerSize;
for(uint16_t elementNumber = 0; elementNumber < header.numPayloadElements; elementNumber++)
{
seekNextElement();
}
return;
default:
m_position += header.headerSize + header.numPayloadElements;
}
if(m_position >= m_messageSize)
{
m_validSeek = false;
}
}
Decoder::HeaderInfo Decoder::decodeHeader() const
{
HeaderInfo newHeaderInfo;
if(m_position >= m_messageSize)
{
return newHeaderInfo;
}
uint8_t typeCode = m_messageBuffer[m_position];
newHeaderInfo.headerSize = 1;
newHeaderInfo.numPayloadElements = 0;
// compressed headers:
if((typeCode & 0x80) == 0)
{
newHeaderInfo.headerType = HeaderInfo::Uint;
return newHeaderInfo;
}
if((typeCode & 0xe0) == 0xe0)
{
newHeaderInfo.headerType = HeaderInfo::Int;
return newHeaderInfo;
}
if((typeCode & 0xe0) == 0xa0)
{
newHeaderInfo.headerType = HeaderInfo::String;
newHeaderInfo.numPayloadElements = typeCode & 0x1f;
return newHeaderInfo;
}
switch(typeCode & 0xf0)
{
case 0x90:
newHeaderInfo.headerType = HeaderInfo::Array;
newHeaderInfo.numPayloadElements = typeCode & 0x0f;
return newHeaderInfo;
case 0x80:
newHeaderInfo.headerType = HeaderInfo::Map;
newHeaderInfo.numPayloadElements = typeCode & 0x0f;
return newHeaderInfo;
}
// full headers:
switch(typeCode)
{
case 0xc0:
newHeaderInfo.headerType = HeaderInfo::Nil;
return newHeaderInfo;
case 0xc2:
newHeaderInfo.headerType = HeaderInfo::False;
return newHeaderInfo;
case 0xc3:
newHeaderInfo.headerType = HeaderInfo::True;
return newHeaderInfo;
case 0xcc:
newHeaderInfo.headerType = HeaderInfo::Uint;
newHeaderInfo.numPayloadElements = 1;
return newHeaderInfo;
case 0xcd:
newHeaderInfo.headerType = HeaderInfo::Uint;
newHeaderInfo.numPayloadElements = 2;
return newHeaderInfo;
case 0xce:
newHeaderInfo.headerType = HeaderInfo::Uint;
newHeaderInfo.numPayloadElements = 4;
return newHeaderInfo;
case 0xd0:
newHeaderInfo.headerType = HeaderInfo::Int;
newHeaderInfo.numPayloadElements = 1;
return newHeaderInfo;
case 0xd1:
newHeaderInfo.headerType = HeaderInfo::Int;
newHeaderInfo.numPayloadElements = 2;
return newHeaderInfo;
case 0xd2:
newHeaderInfo.headerType = HeaderInfo::Int;
newHeaderInfo.numPayloadElements = 4;
return newHeaderInfo;
case 0xd9:
case 0xc4:
if(m_messageSize - m_position < 2)
{
return newHeaderInfo;
}
newHeaderInfo.headerType = HeaderInfo::String;
newHeaderInfo.headerSize = 2;
newHeaderInfo.numPayloadElements = m_messageBuffer[m_position + 1];
return newHeaderInfo;
case 0xdc:
if(m_messageSize - m_position < 3)
{
return newHeaderInfo;
}
newHeaderInfo.headerType = HeaderInfo::Array;
newHeaderInfo.headerSize = 3;
newHeaderInfo.numPayloadElements = (static_cast<uint16_t>(m_messageBuffer[m_position + 1]) << 8) | m_messageBuffer[m_position + 2];
return newHeaderInfo;
case 0xde:
if(m_messageSize - m_position < 3)
{
return newHeaderInfo;
}
newHeaderInfo.headerType = HeaderInfo::Map;
newHeaderInfo.headerSize = 3;
newHeaderInfo.numPayloadElements = (static_cast<uint16_t>(m_messageBuffer[m_position + 1]) << 8) | m_messageBuffer[m_position + 2];
return newHeaderInfo;
}
return newHeaderInfo;
}
Maybe<bool> Decoder::isNil() const
{
HeaderInfo header = decodeHeader();
if(header.headerType == HeaderInfo::Nil)
{
return Maybe<bool>(true);
}
else if(header.headerType != HeaderInfo::InvalidHeader)
{
return Maybe<bool>(false);
}
else
{
// decode failure
return Maybe<bool>();
}
}
Maybe<bool> Decoder::getBool() const
{
HeaderInfo header = decodeHeader();
if(header.headerType == HeaderInfo::True)
{
return Maybe<bool>(true);
}
else if(header.headerType == HeaderInfo::False)
{
return Maybe<bool>(false);
}
else
{
// type mismatch
return Maybe<bool>();
}
}
Maybe<uint32_t> Decoder::getUint32() const
{
HeaderInfo header = decodeHeader();
if(
header.headerType != HeaderInfo::Uint
or
m_position + header.headerSize + header.numPayloadElements > m_messageSize
)
{
// type mismatch
return Maybe<uint32_t>();
}
switch(header.numPayloadElements)
{
case 0:
return Maybe<uint32_t>(m_messageBuffer[m_position] & 0x7f);
case 1:
return Maybe<uint32_t>(m_messageBuffer[m_position + 1]);
case 2:
return Maybe<uint32_t>(static_cast<uint32_t>(m_messageBuffer[m_position + 1]) << 8 | m_messageBuffer[m_position + 2]);
case 4:
return Maybe<uint32_t>(static_cast<uint32_t>(m_messageBuffer[m_position + 1]) << 24 | static_cast<uint32_t>(m_messageBuffer[m_position + 2]) << 16 | static_cast<uint32_t>(m_messageBuffer[m_position + 3]) << 8 | m_messageBuffer[m_position + 4]);
default:
// number too big
return Maybe<uint32_t>();
}
}
Maybe<uint8_t> Decoder::getUint8() const
{
auto u32val = getUint32();
if((not u32val.isValid()) or u32val.get() > 0xff)
{
return Maybe<uint8_t>();
}
else
{
return Maybe<uint8_t>(u32val.get());
}
}
Maybe<uint16_t> Decoder::getUint16() const
{
auto u32val = getUint32();
if((not u32val.isValid()) or u32val.get() > 0xffff)
{
return Maybe<uint16_t>();
}
else
{
return Maybe<uint16_t>(u32val.get());
}
}
Maybe<uint16_t> Decoder::getString(char * f_out_data, uint8_t f_maxSize) const
{
if(f_maxSize < 1)
{
return Maybe<uint16_t>();
}
auto numBytes = getBinary(reinterpret_cast<uint8_t*>(f_out_data), f_maxSize - 1);
if(numBytes.isValid())
{
f_out_data[numBytes.get()] = '\0';
}
else
{
f_out_data[0] = '\0';
}
return numBytes;
}
Maybe<bool> Decoder::compareString(const char * f_string) const
{
HeaderInfo header = decodeHeader();
if(
header.headerType != HeaderInfo::String
or
m_position + header.headerSize + header.numPayloadElements > m_messageSize
)
{
// type mismatch
return Maybe<bool>();
}
const char * message = reinterpret_cast<const char*>(&m_messageBuffer[m_position + header.headerSize]);
size_t i = 0;
for(; i < header.numPayloadElements; i++)
{
if(f_string[i] == '\0' or message[i] != f_string[i])
{
return Maybe<bool>(false);
}
}
if(f_string[i] != '\0')
{
return Maybe<bool>(false);
}
return Maybe<bool>(true);
}
Maybe<uint16_t> Decoder::getBinary(uint8_t * f_out_data, uint8_t f_maxSize) const
{
HeaderInfo header = decodeHeader();
if(
header.headerType != HeaderInfo::String
or
m_position + header.headerSize + header.numPayloadElements > m_messageSize
or
header.numPayloadElements > f_maxSize
)
{
// type mismatch
return Maybe<uint16_t>();
}
memcpy(f_out_data, &m_messageBuffer[m_position + header.headerSize], header.numPayloadElements);
return Maybe<uint16_t>(header.numPayloadElements);
}
bool Decoder::isValid()
{
auto header = decodeHeader();
if(header.headerType == HeaderInfo::InvalidHeader)
{
m_validSeek = false;
}
return m_validSeek;
}
}