-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialize.cpp
207 lines (155 loc) · 6.43 KB
/
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
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
/*
* Serialize.cpp
* File implementing functions for serializing data types into buffers for serial transfer
* Author: Alex St. Clair
* September 2019
*
* The functions are passed the buffer pointer, size of the buffer, and a pointer to
* a variable tracking the current index in the buffer. Note that the functions will
* increment the index variable automatically and only if bytes on the buffer are
* actually used.
*
* The maximum buffer size supported is 65531 (ie. UINT16_MAX - 4)
*/
#include "Serialize.h"
#define NULL nullptr
// Default to big endian
Endianness_t endianness = SERIALIZE_BIG_ENDIAN;
// --- Safely add data to the buffer, return success ---
bool BufferAddUInt8(uint8_t data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
// check for NULL pointers
if (NULL == buffer || NULL == curr_index) return false;
// ensure we don't overrun the buffer
if (*curr_index + sizeof(uint8_t) > buffer_size) return false;
buffer[(*curr_index)++] = data;
return true;
}
bool BufferAddUInt16(uint16_t data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
// check for NULL pointers
if (NULL == buffer || NULL == curr_index) return false;
// ensure we don't overrun the buffer
if (*curr_index + sizeof(uint16_t) > buffer_size) return false;
if (SERIALIZE_BIG_ENDIAN == endianness) {
buffer[(*curr_index)++] = (data >> 8) & 0xFF;
buffer[(*curr_index)++] = data & 0xFF;
} else {
buffer[(*curr_index)++] = data & 0xFF;
buffer[(*curr_index)++] = data >> 8;
}
return true;
}
bool BufferAddUInt32(uint32_t data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
// check for NULL pointers
if (NULL == buffer || NULL == curr_index) return false;
// ensure we don't overrun the buffer
if (*curr_index + sizeof(uint32_t) > buffer_size) return false;
if (SERIALIZE_BIG_ENDIAN == endianness) {
buffer[(*curr_index)++] = (data >> 24) & 0xFF;
buffer[(*curr_index)++] = (data >> 16) & 0xFF;
buffer[(*curr_index)++] = (data >> 8) & 0xFF;
buffer[(*curr_index)++] = data & 0xFF;
} else {
buffer[(*curr_index)++] = data & 0xFF;
buffer[(*curr_index)++] = (data >> 8) & 0xFF;
buffer[(*curr_index)++] = (data >> 16) & 0xFF;
buffer[(*curr_index)++] = (data >> 24) & 0xFF;
}
return true;
}
bool BufferAddInt8(int8_t data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
return BufferAddUInt8((uint8_t) data, buffer, buffer_size, curr_index);
}
bool BufferAddInt16(int16_t data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
return BufferAddUInt16((int16_t) data, buffer, buffer_size, curr_index);
}
bool BufferAddInt32(int32_t data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
return BufferAddUInt32((int32_t) data, buffer, buffer_size, curr_index);
}
bool BufferAddFloat(float data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
uint32_t * temp = (uint32_t *) &data;
return BufferAddUInt32(*temp, buffer, buffer_size, curr_index);
}
// --- Safely get data from a buffer, return success ---
bool BufferGetUInt8(uint8_t * data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
// check for NULL pointers
if (NULL == data || NULL == buffer || NULL == curr_index) return false;
// ensure we don't overrun the buffer
if (*curr_index + sizeof(uint8_t) > buffer_size) return false;
*data = buffer[(*curr_index)++];
return true;
}
bool BufferGetUInt16(uint16_t * data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
// check for NULL pointers
if (NULL == data || NULL == buffer || NULL == curr_index) return false;
// ensure we don't overrun the buffer
if (*curr_index + sizeof(uint16_t) > buffer_size) return false;
*data = 0;
if (SERIALIZE_BIG_ENDIAN == endianness) {
*data |= ((uint16_t) buffer[(*curr_index)++]) << 8;
*data |= ((uint16_t) buffer[(*curr_index)++]);
} else {
*data |= ((uint16_t) buffer[(*curr_index)++]);
*data |= ((uint16_t) buffer[(*curr_index)++]) << 8;
}
return true;
}
bool BufferGetUInt32(uint32_t * data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
// check for NULL pointers
if (NULL == data || NULL == buffer || NULL == curr_index) return false;
// ensure we don't overrun the buffer
if (*curr_index + sizeof(uint32_t) > buffer_size) return false;
*data = 0;
if (SERIALIZE_BIG_ENDIAN == endianness) {
*data |= ((uint32_t) buffer[(*curr_index)++]) << 24;
*data |= ((uint32_t) buffer[(*curr_index)++]) << 16;
*data |= ((uint32_t) buffer[(*curr_index)++]) << 8;
*data |= ((uint32_t) buffer[(*curr_index)++]);
} else {
*data |= ((uint32_t) buffer[(*curr_index)++]);
*data |= ((uint32_t) buffer[(*curr_index)++]) << 8;
*data |= ((uint32_t) buffer[(*curr_index)++]) << 16;
*data |= ((uint32_t) buffer[(*curr_index)++]) << 24;
}
return true;
}
bool BufferGetInt8(int8_t * data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
uint8_t placeholder = 0;
if (!BufferGetUInt8(&placeholder, buffer, buffer_size, curr_index)) return false;
*data = (int8_t) placeholder;
return true;
}
bool BufferGetInt16(int16_t * data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
uint16_t placeholder = 0;
if (!BufferGetUInt16(&placeholder, buffer, buffer_size, curr_index)) return false;
*data = (int16_t) placeholder;
return true;
}
bool BufferGetInt32(int32_t * data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
uint32_t placeholder = 0;
if (!BufferGetUInt32(&placeholder, buffer, buffer_size, curr_index)) return false;
*data = (int32_t) placeholder;
return true;
}
bool BufferGetFloat(float * data, uint8_t * buffer, uint16_t buffer_size, uint16_t * curr_index)
{
uint32_t placeholder = 0;
float * temp;
if (!BufferGetUInt32(&placeholder, buffer, buffer_size, curr_index)) return false;
// cast the integer into a temporary float pointer variable
temp = (float *) &placeholder;
*data = *temp;
return true;
}