-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro-serde.h
More file actions
375 lines (333 loc) · 10.9 KB
/
micro-serde.h
File metadata and controls
375 lines (333 loc) · 10.9 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
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
//////////////////////////////////////////////////////////////////////
// SPDX-License-Identifier: MIT
//
// micro-serde.h
// =============
//
// Header-only serialization library for C99, in ~150 lines of code.
//
// Features:
// - Header-only: just include and use.
// - Schema-based: one definition reused across all struct instances.
// - Serialized data stored in network byte order, works on LE an BE
// - Incredibly easy to use
//
// Author: Giovanni Santini
// Mail: giovanni.santini@proton.me
// License: MIT
//
//
// Documentation
// -------------
//
// micro-serde.h provides a simple way to serialize and deserialize
// plain C structs without requiring reflection, runtime type
// metadata, or generated code.
//
// The library defines a schema ("type definition") once per struct,
// describing each field’s type and offset. This schema can then be
// applied to any instance of that struct at runtime. Serialization
// and deserialization functions use these definitions to walk through
// the struct fields and convert them to or from a binary or textual
// representation.
//
// For example, you would define a schema for the struct Vec2 in the
// following way:
//
// typedef struct {
// int x;
// unsigned int y;
// } Vec2;
//
// MicroSerdeDef vec2_def[] = { // Schema definition
// MICRO_SERDE_FIELD_INT(Vec2, x), // Int value
// MICRO_SERDE_FIELD_UINT(Vec2, y), // Unsigned int value
// };
//
// You can use this definition inside other definitions, like so:
//
// typedef struct {
// Vec2 vec;
// char name[10];
// } MyType;
//
// MicroSerdeDef my_type_def[] = {
// MICRO_SERDE_FIELD_CUSTOM(MyType, vec, vec2_def),
// MICRO_SERDE_FIELD_STR(MyType, name, 10),
// };
//
// Once you have created your serialization definitions, you can
// finally serialize and deserialize with the functions
// `micro_serde_serialize` and `micro_serde_deserialize`
//
// int micro_serde_serialize(MicroSerdeDef *def,
// unsigned int field_count,
// const void* instance,
// char *out, unsigned int out_size);
//
// int micro_serde_deserialize(MicroSerdeDef *def,
// unsigned int field_count,
// void* instance,
// const char *input,
// unsigned int input_size);
//
// Args:
// - def: the definition of the type
// - field_count: the number of fields in the definition.
// Can be calculated with the macro:
//
// MICRO_SERDE_DEF_LEN(my_type_def)
//
// - instance: a pointer to the variable that will be
// serialized / deserialized
// - input / output: an user-allocated buffer to store / read
// the serialized data
// - input_size: size of [input]
//
// The functions will return a non-negative integer representing
// the number of bytes serialized, or a negative MICRO_SERDE_ error
//
// For example, using the definitions in the above example:
//
// MyType my_type_instance = (MyType) { // Initialize some values
// .vec = (Vec2) {
// .x = 69,
// .y = 420,
// },
// };
// strcpy(my_type_instance.name, "Test");
//
// // Create a buffer to store the serialized struct
// unsigned int my_type_serialized_len = 100;
// char* my_type_serialized = malloc(my_type_serialized_len);
//
// // Serialze
// micro_serde_serialize(my_type_def,
// MICRO_SERDE_DEF_LEN(my_type_def),
// &my_type_instance,
// my_type_serialized,
// my_type_serialized_len);
//
// MyType my_type_instance2 = {0};
// // Deserialize
// micro_serde_deserialize(my_type_def,
// MICRO_SERDE_DEF_LEN(my_type_def),
// &my_type_instance2,
// my_type_serialized,
// my_type_serialized_len);
//
// free(my_type_serialized);
//
// For a more sophisticated serialization library that works across
// different languages, see https://github.com/protobuf-c/protobuf-c
//
//
// Usage
// -----
//
// Do this:
//
// #define MICRO_SERDE_IMPLEMENTATION
//
// before you include this file in *one* C or C++ file to create the
// implementation.
//
// i.e. it should look like this:
//
// #include ...
// #include ...
// #include ...
// #define MICRO_SERDE_IMPLEMENTATION
// #include "micro-serde.h"
//
//
// Code
// ----
//
// The official git repository of micro-serde.h is hosted at:
//
// https://github.com/San7o/micro-serde.h
//
// This is part of a bigger collection of header-only C99 libraries
// called "micro-headers", contributions are welcome:
//
// https://github.com/San7o/micro-headers
//
#ifndef MICRO_SERDE
#define MICRO_SERDE
#define MICRO_SERDE_MAJOR 0
#define MICRO_SERDE_MINOR 1
#ifdef __cplusplus
extern "C" {
#endif
// Config: Prefix for all functions
// For function inlining, set this to `static inline` and then define
// the implementation in all the files
#ifndef MICRO_SERDE_DEF
#define MICRO_SERDE_DEF extern
#endif
//
// Macros
//
#define MICRO_SERDE_OFFSET_OF(type, field) ((size_t)&(((type *)0)->field))
#define MICRO_SERDE_DEF_LEN(micro_serde_def) \
(sizeof(micro_serde_def) / sizeof(micro_serde_def[0]))
#define MICRO_SERDE_FIELD(struct_type, field_name, field_type_enum, \
field_size, custom_def, custom_def_size ) \
{ field_type_enum, MICRO_SERDE_OFFSET_OF(struct_type, field_name), \
field_size, custom_def }
#define MICRO_SERDE_FIELD_INT(struct_type, field_name) \
{ MICRO_SERDE_INT, MICRO_SERDE_OFFSET_OF(struct_type, field_name), \
sizeof(int), 0 }
#define MICRO_SERDE_FIELD_UINT(struct_type, field_name) \
{ MICRO_SERDE_UINT, MICRO_SERDE_OFFSET_OF(struct_type, field_name), \
sizeof(unsigned int), 0 }
#define MICRO_SERDE_FIELD_STR(struct_type, field_name, max_len) \
{ MICRO_SERDE_STR, MICRO_SERDE_OFFSET_OF(struct_type, field_name), max_len, 0 }
#define MICRO_SERDE_FIELD_CUSTOM(custom_type, field_name, custom_def) \
{ MICRO_SERDE_CUSTOM, MICRO_SERDE_OFFSET_OF(custom_type, field_name), \
MICRO_SERDE_DEF_LEN(custom_def), custom_def }
// Errors
#define MICRO_SERDE_OK 0
#define MICRO_SERDE_ERROR_DEF_NULL -1
#define MICRO_SERDE_ERROR_ARG_NULL -2
#define MICRO_SERDE_ERROR_UNKNOWN_TYPE -3
#define MICRO_SERDE_ERROR_OUT_OF_SPACE -4
#define MICRO_SERDE_ERROR_MISSING_CUSTOM_DEF -5
//
// Types
//
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
typedef enum {
MICRO_SERDE_INT = 0,
MICRO_SERDE_UINT,
MICRO_SERDE_STR,
MICRO_SERDE_CUSTOM,
_MICRO_SERDE_MAX,
} MicroSerdeFieldType;
struct MicroSerdeDef;
typedef struct MicroSerdeDef MicroSerdeDef;
struct MicroSerdeDef {
MicroSerdeFieldType type;
unsigned int offset;
unsigned int size;
MicroSerdeDef *custom_def;
};
//
// Function definitions
//
MICRO_SERDE_DEF int
micro_serde_serialize(MicroSerdeDef *def,
unsigned int field_count,
const void* instance,
char *out, unsigned int out_size);
MICRO_SERDE_DEF int
micro_serde_deserialize(MicroSerdeDef *def,
unsigned int field_count,
void* instance,
const char *input,
unsigned int input_size);
//
// Implementation
//
#ifdef MICRO_SERDE_IMPLEMENTATION
MICRO_SERDE_DEF int
micro_serde_serialize(MicroSerdeDef *def,
unsigned int field_count,
const void* instance,
char *out, unsigned int out_size)
{
if (!def) return MICRO_SERDE_ERROR_DEF_NULL;
if (!instance || !out) return MICRO_SERDE_ERROR_ARG_NULL;
unsigned int offset = 0;
for (unsigned int i = 0; i < field_count; ++i)
{
switch(def[i].type)
{
case MICRO_SERDE_INT: // fall
case MICRO_SERDE_UINT:
if (offset + def[i].size >= out_size)
return MICRO_SERDE_ERROR_OUT_OF_SPACE;
uint32_t uinteger;
memcpy(&uinteger, (char*)instance + def[i].offset, sizeof(uint32_t));
uint32_t net_uinteger = htonl(uinteger);
memcpy((char*)out + offset, &net_uinteger, sizeof(uint32_t));
offset += sizeof(uint32_t);
break;
case MICRO_SERDE_STR:;
if (offset + def[i].size >= out_size)
return MICRO_SERDE_ERROR_OUT_OF_SPACE;
memcpy(out + offset, (char*)instance + def[i].offset, def[i].size);
offset += def[i].size;
break;
case MICRO_SERDE_CUSTOM:
if (!def[i].custom_def)
return MICRO_SERDE_ERROR_MISSING_CUSTOM_DEF;
int ret = micro_serde_serialize(def[i].custom_def,
def[i].size,
(char*)instance + def[i].offset,
out + offset,
out_size - offset);
if (ret < 0) return ret;
offset += ret;
break;
default:
return MICRO_SERDE_ERROR_UNKNOWN_TYPE;
}
}
return offset;
}
MICRO_SERDE_DEF int
micro_serde_deserialize(MicroSerdeDef *def,
unsigned int field_count,
void* instance,
const char *input,
unsigned int input_size)
{
if (!def) return MICRO_SERDE_ERROR_DEF_NULL;
if (!instance || !input) return MICRO_SERDE_ERROR_ARG_NULL;
unsigned int offset = 0;
for (unsigned int i = 0; i < field_count; ++i)
{
switch(def[i].type)
{
case MICRO_SERDE_INT: // fall
case MICRO_SERDE_UINT:
if (offset + def[i].size >= input_size)
return MICRO_SERDE_ERROR_OUT_OF_SPACE;
uint32_t net_uinteger;
memcpy(&net_uinteger, input + offset, sizeof(uint32_t));
uint32_t uinteger = ntohl(net_uinteger);
memcpy((char*)instance + def[i].offset, &uinteger, sizeof(uint32_t));
offset += def[i].size;
break;
case MICRO_SERDE_STR:;
if (offset + def[i].size >= input_size)
return MICRO_SERDE_ERROR_OUT_OF_SPACE;
memcpy((char*)instance + def[i].offset, input + offset, def[i].size);
offset += def[i].size;
break;
case MICRO_SERDE_CUSTOM:
if (!def[i].custom_def)
return MICRO_SERDE_ERROR_MISSING_CUSTOM_DEF;
int ret = micro_serde_deserialize(def[i].custom_def,
def[i].size,
(char*)instance + def[i].offset,
input + offset,
input_size - offset);
if (ret < 0) return ret;
offset += ret;
break;
default:
return MICRO_SERDE_ERROR_UNKNOWN_TYPE;
}
}
return offset;
}
#endif // MICRO_SERDE_IMPLEMENTATION
#ifdef __cplusplus
}
#endif
#endif // MICRO_SERDE