-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize.cpp
More file actions
157 lines (136 loc) · 3.27 KB
/
serialize.cpp
File metadata and controls
157 lines (136 loc) · 3.27 KB
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
#include <assert.h>
#include <stdint.h>
#include <string.h>
void write_uint8(char **p, const char *end, uint8_t b)
{
assert(*p < end);
**p = b;
*p += 1;
}
void write_sint8(char **p, const char *end, int8_t b)
{
assert(*p < end);
**p = b;
*p += 1;
}
void write_uint16_be(char **p, const char *end, uint16_t u)
{
write_uint8(p, end, u >> 8);
write_uint8(p, end, u & 0xff);
}
void write_uint32_be(char **p, const char *end, uint32_t u)
{
write_uint8(p, end, (u >> 24) & 0xff);
write_uint8(p, end, (u >> 16) & 0xff);
write_uint8(p, end, (u >> 8) & 0xff);
write_uint8(p, end, u & 0xff);
}
uint8_t read_uint8(const char **p, const char *end)
{
assert(*p < end);
uint8_t res = **p;
*p += 1;
return res;
}
int8_t read_sint8(const char **p, const char *end)
{
return (int8_t)read_uint8(p, end);
}
int16_t read_sint16_be(const char **p, const char *end)
{
int16_t t = 0;
// big-endian
t |= read_uint8(p, end) << 8;
t |= read_uint8(p, end);
return t;
}
uint16_t read_uint16_be(const char **p, const char *end)
{
return (uint16_t)read_sint16_be(p, end);
}
int32_t read_sint32_be(const char **p, const char *end)
{
int32_t t = 0;
// big-endian
t |= read_uint8(p, end) << 24;
t |= read_uint8(p, end) << 16;
t |= read_uint8(p, end) << 8;
t |= read_uint8(p, end);
return t;
}
uint32_t read_uint32_be(const char **p, const char *end)
{
return (uint32_t)read_sint32_be(p, end);
}
int16_t read_sint16_le(const char **p, const char *end)
{
int16_t t = 0;
// little-endian
t |= read_uint8(p, end);
t |= read_uint8(p, end) << 8;
return t;
}
uint16_t read_uint16_le(const char **p, const char *end)
{
return (uint16_t)read_sint16_le(p, end);
}
int32_t read_sint32_le(const char **p, const char *end)
{
int32_t t = 0;
// little-endian
t |= read_uint8(p, end);
t |= read_uint8(p, end) << 8;
t |= read_uint8(p, end) << 16;
t |= read_uint8(p, end) << 24;
return t;
}
uint32_t read_uint32_le(const char **p, const char *end)
{
return (uint32_t)read_sint32_le(p, end);
}
int64_t read_sint64_le (const char **p, const char *end)
{
int64_t t = 0;
// little-endian
t |= ((uint64_t)read_uint8(p, end));
t |= ((uint64_t)read_uint8(p, end)) << 8;
t |= ((uint64_t)read_uint8(p, end)) << 16;
t |= ((uint64_t)read_uint8(p, end)) << 24;
t |= ((uint64_t)read_uint8(p, end)) << 32;
t |= ((uint64_t)read_uint8(p, end)) << 40;
t |= ((uint64_t)read_uint8(p, end)) << 48;
t |= ((uint64_t)read_uint8(p, end)) << 56;
return t;
}
uint64_t read_uint64_le (const char **p, const char *end)
{
return (uint64_t)read_sint64_le(p, end);
}
void read_ascii_fixed(const char **p, const char *end, char *s, int n)
{
for (int i = 0; i < n; i++)
{
*s = read_uint8(p, end);
s += 1;
}
// important: null terminate
*s = '\0';
}
void read_chunk(const char **p, const char *end, char *buf, int len)
{
int remaining_data = end - *p;
assert(remaining_data >= len);
memcpy(buf, *p, len);
*p += len;
}
void write_ascii_fixed(char **p, const char *end, const char *s, int n)
{
for (int i = 0; i < n; i++)
{
write_uint8(p, end, *s);
if (*s)
{
s += 1;
}
}
}