-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialize.cpp
46 lines (38 loc) · 870 Bytes
/
serialize.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
/*
* CPP Seializer - Fast and simple data serialization for C++.
*
* https://swapped.ch/cpp-serializer
*
* The code is distributed under terms of the BSD license.
* Copyright (c) 2019 Alex Pankratov. All rights reserved.
*/
#include "serialize.h"
void store_size(buffer & buf, size_t n)
{
/*
* variable-size lsb in 7bit chunks
*
* 000zzzzzzzyyyyyyyxxxxxxx -> 1xxxxxxx 1yyyyyyy 0zzzzzzz
*/
do
buf.push_back( (uint8_t)(((n > 0x7f) ? 0x80 : 0x00) | (n & 0x7f)) );
while (n >>= 7);
}
bool parse_size(parser & par, size_t & val)
{
size_t off;
uint8_t byte;
for (val=0, off=0; ; off += 7)
{
if (! par.has(1))
return false;
byte = *par.ptr++;
val |= (byte & 0x7f) << off;
if (! (byte & 0x80))
break;
}
/* catch 32bit overflows */
if ( (off > 28) || (off == 28) && (byte > 0x0F) )
return false;
return true;
}