-
Notifications
You must be signed in to change notification settings - Fork 5
/
serialization.h
71 lines (66 loc) · 2.14 KB
/
serialization.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
//
// serialization.h
// MPWide
//
// These functions are endian-neutral, and assume big-endian byte order on networks.
// They are inline and with proper compiler flags will be be very efficient.
//
// Created by Joris Borgdorff on 02-07-13.
// Copyright (c) 2013 Derek Groen. All rights reserved.
//
#ifndef __MPWide__serialization__
#define __MPWide__serialization__
#include <cstddef> // size_t
#include <cassert> // assert
#include <cstring> // memset
inline void
serialize_size_t(unsigned char *net_number, const size_t native_number)
{
if (sizeof(size_t) >= 8)
{
net_number[0] = (native_number >> 56) & 0xff;
net_number[1] = (native_number >> 48) & 0xff;
net_number[2] = (native_number >> 40) & 0xff;
net_number[3] = (native_number >> 32) & 0xff;
}
else
{
assert(sizeof(size_t) >= 4);
// First bytes are not defined -- 0
memset(net_number, 0, 4);
}
net_number[4] = (native_number >> 24) & 0xff;
net_number[5] = (native_number >> 16) & 0xff;
net_number[6] = (native_number >> 8) & 0xff;
net_number[7] = native_number & 0xff;
}
inline size_t
deserialize_size_t(const unsigned char *net_number)
{
if (sizeof(size_t) >= 8)
{
return ((size_t)net_number[0] << 56)
| ((size_t)net_number[1] << 48)
| ((size_t)net_number[2] << 40)
| ((size_t)net_number[3] << 32)
| ((size_t)net_number[4] << 24)
| ((size_t)net_number[5] << 16)
| ((size_t)net_number[6] << 8)
| (size_t)net_number[7];
}
else
{
// may cause problems if the sent size is larger than size_t max - but then the
// memory won't be large enough to hold the data anyway.
assert(net_number[0] == 0);
assert(net_number[1] == 0);
assert(net_number[2] == 0);
assert(net_number[3] == 0);
assert(sizeof(size_t) >= 4);
return ((size_t)net_number[4] << 24)
| ((size_t)net_number[5] << 16)
| ((size_t)net_number[6] << 8)
| (size_t)net_number[7];
}
}
#endif /* defined(__MPWide__serialization__) */