]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/marshal.h
marshal: handle fixed-size strings
[thirdparty/lldpd.git] / src / marshal.h
CommitLineData
db323555
VB
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
20struct marshal_info;
21enum marshal_subinfo_kind {
22 pointer,
23 substruct,
24};
25#define MARSHAL_INFO_POINTER 1
26#define MARSHAL_INFO_SUB 2
27struct marshal_subinfo {
28 size_t offset; /* Offset compared to parent structure */
ca4ed9da 29 size_t offset2; /* Ancillary offset (for related data) */
db323555
VB
30 enum marshal_subinfo_kind kind; /* Kind of substructure */
31 struct marshal_info *mi;
32};
33struct 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};
da781141
VB
38/* Special case for strings */
39extern struct marshal_info marshal_info__string;
ca4ed9da 40extern struct marshal_info marshal_info__fstring;
db323555
VB
41
42/* Declare a new marshal_info struct named after the type we want to
43 marshal. The marshalled type has to be a structure. */
5e73393b 44#define MARSHAL_BEGIN(type) struct marshal_info marshal_info_##type = \
db323555
VB
45 { \
46 .name = #type, \
47 .size = sizeof(struct type), \
48 .pointers = {
ca4ed9da
VB
49#define MARSHAL_ADD(_kind, type, subtype, member) \
50 { .offset = offsetof(struct type, member), \
51 .kind = _kind, \
db323555 52 .mi = &marshal_info_##subtype },
5e73393b
VB
53#define MARSHAL_POINTER(...) MARSHAL_ADD(pointer, ##__VA_ARGS__)
54#define MARSHAL_SUBSTRUCT(...) MARSHAL_ADD(substruct, ##__VA_ARGS__)
da781141 55#define MARSHAL_STR(type, member) MARSHAL_ADD(pointer, type, _string, member)
ca4ed9da
VB
56#define MARSHAL_FSTR(type, member, len) \
57 { .offset = offsetof(struct type, member), \
58 .offset2 = offsetof(struct type, len), \
59 .kind = pointer, \
60 .mi = &marshal_info__fstring },
5e73393b
VB
61#define MARSHAL_TQE(type, field) \
62 MARSHAL_POINTER(type, type, field.tqe_next) \
63 MARSHAL_POINTER(type, type, field.tqe_prev)
64#define MARSHAL_TQH(type, subtype) \
65 MARSHAL_POINTER(type, subtype, tqh_first) \
66 MARSHAL_POINTER(type, subtype, tqh_last)
67#define MARSHAL_SUBTQ(type, subtype, field) \
68 MARSHAL_POINTER(type, subtype, field.tqh_first) \
69 MARSHAL_POINTER(type, subtype, field.tqh_last)
70#define MARSHAL_END { .mi = NULL } } }
db323555 71/* Shortcuts */
5e73393b
VB
72#define MARSHAL(type) \
73 MARSHAL_BEGIN(type) \
74 MARSHAL_END
75#define MARSHAL_TQ(type, subtype) \
76 MARSHAL_BEGIN(type) \
77 MARSHAL_TQH(type, subtype) \
78 MARSHAL_END
db323555
VB
79
80/* Serialization */
ca4ed9da
VB
81size_t _marshal_serialize(struct marshal_info *, void *, void **, int, void *, int);
82#define marshal_serialize(type, o, output) _marshal_serialize(&marshal_info_##type, o, output, 0, NULL, 0)
db323555
VB
83
84/* Unserialization */
ca4ed9da 85size_t _marshal_unserialize(struct marshal_info *, void *, size_t, void **, void*, int, int);
db323555 86#define marshal_unserialize(type, o, l, input) \
ca4ed9da 87 _marshal_unserialize(&marshal_info_##type, o, l, (void **)input, NULL, 0, 0)
db323555
VB
88
89#endif