]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/marshal.h
marshal: allow to specify null-terminated strings
[thirdparty/lldpd.git] / src / marshal.h
1 /*
2 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef _MARSHAL_H
18 #define _MARSHAL_H
19
20 struct marshal_info;
21 enum marshal_subinfo_kind {
22 pointer,
23 substruct,
24 string,
25 };
26 #define MARSHAL_INFO_POINTER 1
27 #define MARSHAL_INFO_SUB 2
28 struct marshal_subinfo {
29 size_t offset; /* Offset compared to parent structure */
30 enum marshal_subinfo_kind kind; /* Kind of substructure */
31 struct marshal_info *mi;
32 };
33 struct marshal_info {
34 char *name; /* Name of structure */
35 size_t size; /* Size of the structure */
36 struct marshal_subinfo pointers[]; /* Pointer to other structures */
37 };
38 /* Special case for strings */
39 extern struct marshal_info marshal_info__string;
40
41 /* Declare a new marshal_info struct named after the type we want to
42 marshal. The marshalled type has to be a structure. */
43 #define MARSHAL_BEGIN(type) struct marshal_info marshal_info_##type = \
44 { \
45 .name = #type, \
46 .size = sizeof(struct type), \
47 .pointers = {
48 #define MARSHAL_ADD(_kind, type, subtype, member) \
49 { .offset = offsetof(struct type, member), \
50 .kind = _kind, \
51 .mi = &marshal_info_##subtype },
52 #define MARSHAL_POINTER(...) MARSHAL_ADD(pointer, ##__VA_ARGS__)
53 #define MARSHAL_SUBSTRUCT(...) MARSHAL_ADD(substruct, ##__VA_ARGS__)
54 #define MARSHAL_STR(type, member) MARSHAL_ADD(pointer, type, _string, member)
55 #define MARSHAL_TQE(type, field) \
56 MARSHAL_POINTER(type, type, field.tqe_next) \
57 MARSHAL_POINTER(type, type, field.tqe_prev)
58 #define MARSHAL_TQH(type, subtype) \
59 MARSHAL_POINTER(type, subtype, tqh_first) \
60 MARSHAL_POINTER(type, subtype, tqh_last)
61 #define MARSHAL_SUBTQ(type, subtype, field) \
62 MARSHAL_POINTER(type, subtype, field.tqh_first) \
63 MARSHAL_POINTER(type, subtype, field.tqh_last)
64 #define MARSHAL_END { .mi = NULL } } }
65 /* Shortcuts */
66 #define MARSHAL(type) \
67 MARSHAL_BEGIN(type) \
68 MARSHAL_END
69 #define MARSHAL_TQ(type, subtype) \
70 MARSHAL_BEGIN(type) \
71 MARSHAL_TQH(type, subtype) \
72 MARSHAL_END
73
74 /* Serialization */
75 size_t _marshal_serialize(struct marshal_info *, void *, void **, int, void *);
76 #define marshal_serialize(type, o, output) _marshal_serialize(&marshal_info_##type, o, output, 0, NULL)
77
78 /* Unserialization */
79 size_t _marshal_unserialize(struct marshal_info *, void *, size_t, void **, void*, int);
80 #define marshal_unserialize(type, o, l, input) \
81 _marshal_unserialize(&marshal_info_##type, o, l, (void **)input, NULL, 0)
82
83 #endif