-
Notifications
You must be signed in to change notification settings - Fork 1
/
vom.c
381 lines (319 loc) · 8.02 KB
/
vom.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
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
376
377
378
379
380
381
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "miniv.h"
#include <stdio.h>
static struct utype knownUserTypes[utidMax];
static int nKnownUserTypes = 0;
static int64_t decodeSign(uint64_t in) {
if ((in & 1) == 0) {
return in>>1;
}
return ~(in>>1);
}
static void eat(buf_t *in, ulong_t n) {
/*
char buf[100];
snprintf(buf, sizeof(buf), "eat %lu", n);
bufDump(buf, *in);
*/
if (n > in->len) {
// Do not allow underflow
printf("underflow!\n");
in->buf += in->len;
in->len = 0;
} else {
in->buf += n;
in->len -= n;
}
}
err_t decodeString(buf_t *in, buf_t *v) {
vomControl ctl = controlNone;
uint64_t len;
err_t err = decodeVar128(in, &len, &ctl); ck();
if (ctl != controlNone) {
// unexpected control message here
err = ERR_DECVOM; ck();
}
v->buf = in->buf;
v->len = len;
// TODO: buf needs a flag system to say, "this is a slice, don't realloc/dealloc"
v->cap = 0;
eat(in, len);
return ERR_OK;
}
err_t decodeInt(buf_t *in, int *v) {
vomControl ctl = controlNone;
uint64_t i;
err_t err = decodeVar128(in, &i, &ctl); ck();
if (ctl != controlNone) {
// unexpected control message here
err = ERR_DECVOM; ck();
}
*v = (int)decodeSign(i);
return ERR_OK;
}
err_t decodeDouble(buf_t *in, double *v) {
vomControl ctl = controlNone;
uint64_t uval;
err_t err = decodeVar128(in, &uval, &ctl); ck();
if (ctl != controlNone) {
// unexpected control message here
err = ERR_DECVOM; ck();
}
uint64_t ieee = (uval&0xff)<<56 |
(uval&0xff00)<<40 |
(uval&0xff0000)<<24 |
(uval&0xff000000)<<8 |
(uval&0xff00000000)>>8 |
(uval&0xff0000000000)>>24 |
(uval&0xff000000000000)>>40 |
(uval&0xff00000000000000)>>56;
union {
uint64_t u64;
double d;
} u;
u.u64 = ieee;
*v = u.d;
return ERR_OK;
}
err_t decodeByte(buf_t *in, unsigned char *v) {
if (in->len == 0) {
return ERR_DECVOM;
}
*v = in->buf[0];
eat(in, 1);
return ERR_OK;
}
err_t decodeVar128(buf_t *in, uint64_t *v, vomControl *ctl) {
*ctl = controlNone;
if (in->len == 0) {
return ERR_DECVOM;
}
unsigned char uc = in->buf[0];
if (uc < 128) {
*v = uc;
eat(in, 1);
return ERR_OK;
}
if ((uc & 0xf0) == 0xf0) {
ulong_t len = 0xf - (uc & 0x0f) + 1;
// 65-128 bit ints not handled yet. oops.
if (len > 8) {
return ERR_DECVOM;
}
// check that there is enough data to read (we already read data[0]
// into uc, above, thus len+1.
if (in->len < len+1) {
return ERR_DECVOM_MORE;
}
uint64_t v0 = 0;
for (ulong_t i = 0; i < len; i++) {
v0 <<= 8;
v0 |= in->buf[i+1];
}
*v = v0;
eat(in, len+1);
return ERR_OK;
}
// it is a control point
eat(in, 1);
*v = 0;
*ctl = uc;
switch (uc) {
case 0xe0:
case 0xe1:
break;
default:
// unknown control
return ERR_DECVOM;
}
return ERR_OK;
}
static bool isPrimitive(int64_t tid) {
if (tid >= 1 && tid <= 15) {
return true;
}
if (tid == 39 || tid == 40) {
return true;
}
return false;
}
static struct utype *findUt(buf_t name) {
for (int i = 0; i < nKnownUserTypes; i++) {
if (bufEqual(knownUserTypes[i].tname, name)) {
return &knownUserTypes[i];
}
}
return NULL;
}
static struct utype *lookupUtid(decoder_t *dec, int64_t utid) {
int64_t i = utid - utidBase;
if (i < 0 || i >= utidMax) {
return NULL;
}
return dec->utypes[i];
}
static err_t decodeType(decoder_t *dec) {
vomControl ctl = controlNone;
err_t err = ERR_OK;
while (dec->left.len > 0) {
uint64_t tlen;
err = decodeVar128(&dec->left, &tlen, &ctl); ck();
if (ctl != controlNone) {
if (ctl == controlEnd) {
return ERR_OK;
}
// unexpected control message here
err = ERR_DECVOM; ck();
}
int64_t curTid = -1 * dec->curTid;
if (curTid > utidMax) {
err = ERR_DECVOM; ck();
}
uint64_t before = dec->left.len;
uint64_t kind0;
err = decodeVar128(&dec->left, &kind0, &ctl); ck();
if (ctl != controlNone) {
if (ctl == controlEnd) {
return ERR_OK;
}
// unexpected control message here
err = ERR_DECVOM; ck();
}
uint64_t kindlen = before - dec->left.len;
tlen -= kindlen;
wireKind kind = (wireKind)kind0;
if (kind != kindStruct && kind != kindArray) {
printf("kind %d\n", kind);
bufDump("not impl", dec->left);
err = ERR_DECVOM; ck();
}
buf_t lenbuf = dec->left;
// eat the index number 0 off the front of it.
lenbuf.buf += 1;
lenbuf.len -= 1;
uint64_t len;
err = decodeVar128(&lenbuf, &len, &ctl); ck();
if (ctl != controlNone) {
// unexpected control message here
err = ERR_DECVOM; ck();
}
buf_t name = lenbuf;
name.len = len;
dec->utypes[curTid - utidBase] = findUt(name);
eat(&dec->left, tlen);
return ERR_OK;
}
return ERR_DECVOM_MORE;
}
err_t vomDecode(decoder_t *dec, value_t *vout, bool *done) {
vomControl ctl = controlNone;
err_t err = ERR_OK;
// First time we are called with new input.
if (dec->left.buf == NULL) {
dec->left = dec->in;
// Check the version.
if (dec->left.len != 0 && dec->left.buf[0] != vomVersion81) {
return ERR_DECVOM;
}
dec->version = vomVersion81;
dec->curTid = 0;
eat(&dec->left, 1);
}
while (dec->left.len > 0) {
if (dec->curTid == 0) {
uint64_t tid;
err = decodeVar128(&dec->left, &tid, &ctl); ck();
if (ctl != controlNone) {
if (ctl == controlEnd) {
return ERR_OK;
}
// unexpected control message here
return ERR_DECVOM;
}
dec->curTid = decodeSign(tid);
}
if (isPrimitive(dec->curTid)) {
// with the current design, we shoud only be seeing primitives
// in decoder callbacks.
err = ERR_DECVOM; ck();
}
if (dec->curTid < 0) {
err = decodeType(dec); ck();
// back to the top of the while to see if there is a value to process now
dec->curTid = 0;
continue;
}
// it is a user-defined type. lookup and decode.
struct utype *ut = lookupUtid(dec, dec->curTid);
if (ut == NULL) {
// unknown type
err = ERR_DECVOM; ck();
}
uint64_t vlen;
err = decodeVar128(&dec->left, &vlen, &ctl); ck();
if (ctl != controlNone) {
// unexpected control message here
err = ERR_DECVOM; ck();
}
// Implement the special case for Array length.
if (ut->kind == kindArray) {
if (vlen != 0) {
err = ERR_DECVOM; ck();
}
vlen = (uint64_t)ut->sz;
}
buf_t valbuf = dec->left;
valbuf.len = vlen;
eat(&dec->left, vlen);
// prepare the destination buffer: put the input that the
// decoder will point back at in front, then put a place for
// the structure itself at the end.
bufExpand(&vout->buf, vlen + (uint64_t)ut->sz);
vout->buf.len = 0;
bufAppend(&vout->buf, valbuf);
vout->ptr = &vout->buf.buf[vlen];
vout->typ = NULL;
buf_t decbuf = vout->buf;
err = ut->decoder(&decbuf, vout->ptr); ck();
*done = true;
vout->typ = ut; // only set it now that we know we were successful
return ERR_OK;
}
// out of data to process
err = ERR_DECVOM_MORE; ck();
return ERR_OK;
}
void decoderDealloc(decoder_t *dec) {
bufDealloc(&dec->in);
dec->left.buf = NULL;
dec->left.len = 0;
}
void valueDealloc(value_t *v) {
valueZero(v);
bufDealloc(&v->buf);
}
void valueZero(value_t *v) {
if (v->buf.buf != NULL) {
bufTruncate(&v->buf);
}
v->ptr = NULL;
}
err_t decoderFeed(decoder_t *dec, buf_t in) {
return bufAppend(&dec->in, in);
}
err_t vomRegister(buf_t tname, wireKind kind, ssize_t sz, err_t (*decoder)(buf_t *, void *)) {
if (nKnownUserTypes >= utidMax) {
return ERR_DECVOM;
}
struct utype ut = {
.tname = tname,
.kind = kind,
.sz = sz,
.decoder = decoder,
};
knownUserTypes[nKnownUserTypes] = ut;
nKnownUserTypes++;
return ERR_OK;
}