-
Notifications
You must be signed in to change notification settings - Fork 2
/
lsa_deserialise.c
206 lines (173 loc) · 3.98 KB
/
lsa_deserialise.c
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
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iv_list.h>
#include <limits.h>
#include <string.h>
#include "lsa_deserialise.h"
#include "lsa_type.h"
struct src {
uint8_t *src;
size_t srclen;
size_t off;
};
#define SRC_GET_PTR(_src, _buflen) \
({ \
size_t off = (_src)->off; \
if ((_src)->srclen - off < _buflen) \
goto short_read; \
(_src)->off += _buflen; \
(_src)->src + off; \
})
#define SRC_READ(_src, _buf, _buflen) \
{ \
if ((_src)->srclen - (_src)->off < _buflen) \
goto short_read; \
memcpy(_buf, (_src)->src + (_src)->off, _buflen); \
(_src)->off += _buflen; \
}
#define SRC_READ_UINT64_T(src) \
({ \
uint8_t val; \
int cnt; \
uint64_t v; \
\
SRC_READ(src, &val, 1); \
if (val == 0x80) \
goto error; \
\
cnt = 0; \
v = val & 0x7f; \
while (val & 0x80) { \
if (++cnt == 10) \
goto error; \
SRC_READ(src, &val, 1); \
v = (v << 7) | (val & 0x7f); \
} \
\
v; \
})
#define SRC_READ_INT(src) \
({ \
uint64_t v; \
\
v = SRC_READ_UINT64_T(src); \
if (v > INT_MAX) \
goto error; \
\
(int)v; \
})
#define SRC_READ_SIZE_T(src) \
({ \
uint64_t v; \
\
v = SRC_READ_UINT64_T(src); \
if (v > SIZE_MAX) \
goto error; \
\
(size_t)v; \
})
static int lsa_deserialise_attr_set(struct lsa *lsa, struct lsa_attr_set *dst,
struct src *src, int maxdepth)
{
while (src->off < src->srclen) {
int type;
int flags;
size_t keylen;
uint8_t *key;
size_t datalen;
uint8_t *data;
int sign;
type = SRC_READ_INT(src);
flags = SRC_READ_INT(src);
if (flags & LSA_ATTR_FLAG_HAS_KEY) {
keylen = SRC_READ_SIZE_T(src);
key = SRC_GET_PTR(src, keylen);
} else {
keylen = 0;
key = NULL;
}
datalen = SRC_READ_SIZE_T(src);
data = SRC_GET_PTR(src, datalen);
sign = !!(flags & LSA_ATTR_FLAG_SIGNED);
if (flags & LSA_ATTR_FLAG_DATA_IS_TLV) {
struct lsa_attr_set *set;
struct src srcdata;
if (maxdepth == 0)
return -1;
set = lsa_attr_set_add_attr_set(lsa, dst, type, sign,
key, keylen);
if (set == NULL)
return -1;
srcdata.src = data;
srcdata.srclen = datalen;
srcdata.off = 0;
if (lsa_deserialise_attr_set(lsa, set, &srcdata,
maxdepth - 1) < 0) {
return -1;
}
} else {
if (lsa_attr_set_add_attr(lsa, dst, type, sign,
key, keylen,
data, datalen) < 0) {
return -1;
}
}
}
return 0;
short_read:
error:
return -1;
}
ssize_t lsa_deserialise(struct lsa **lsap, uint8_t *buf, size_t buflen)
{
struct lsa *lsa = NULL;
struct src src;
size_t len;
uint8_t id[NODE_ID_LEN];
src.src = buf;
src.srclen = buflen;
src.off = 0;
len = SRC_READ_SIZE_T(&src);
if (len == 0) {
*lsap = NULL;
return src.off;
}
if (len > SSIZE_MAX - src.off)
return -1;
len += src.off;
if (len > buflen)
return 0;
src.srclen = len;
SRC_READ(&src, id, NODE_ID_LEN);
lsa = lsa_alloc(id);
if (lsa == NULL)
return -1;
if (lsa_deserialise_attr_set(lsa, &lsa->root, &src, 8) < 0)
goto error;
*lsap = lsa;
return src.off;
short_read:
return 0;
error:
if (lsa != NULL)
lsa_put(lsa);
return -1;
}