forked from ssbc/tinySSB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbipf.cpp
561 lines (522 loc) · 14.6 KB
/
bipf.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// bipf.cpp
// tinySSB for ESP32
// 2022-2023 <[email protected]>
#include <Arduino.h>
#include <string.h>
#include "bipf.h"
#include "util.h"
#define TAG_SIZE 3
#define TAG_MASK 7
// -------------------------------------------------------------------------
int bipf_equal(struct bipf_s *a, struct bipf_s *b)
{
if (a == NULL || b == NULL || a->typ != b->typ)
return 0;
switch(a->typ) {
case BIPF_STRING:
case BIPF_BYTES:
return a->cnt == b->cnt && !memcmp(a->u.buf, b->u.buf, a->cnt);
case BIPF_INT:
case BIPF_BOOLNONE:
return a->u.i == b->u.i;
case BIPF_DOUBLE:
return a->u.d == b->u.d;
case BIPF_LIST:
if (a->cnt != b->cnt)
return 0;
for (int i = 0; i < a->cnt; i++)
if (!bipf_equal(a->u.list[i], b->u.list[i]))
return 0;
return 1;
case BIPF_DICT:
if (a->cnt != b->cnt)
return 0;
for (int i = 0; i < a->cnt; i++)
if (!bipf_equal(a->u.dict[2*i], b->u.dict[2*i]) ||
!bipf_equal(a->u.dict[2*i + 1], b->u.dict[2*i + 1]))
return 0;
return 1;
default:
break;
}
return 0;
}
struct bipf_s* bipf_mkBool(int flag)
{
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_BOOLNONE;
bptr->u.i = flag ? 1 : 0;
return bptr;
}
struct bipf_s* bipf_mkBytes(unsigned char *buf, int len)
{
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_BYTES;
bptr->cnt = len;
bptr->u.buf = (unsigned char*) malloc(len);
memcpy(bptr->u.buf, buf, len);
return bptr;
}
struct bipf_s* bipf_mkInt(int val)
{
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_INT;
bptr->u.i = val;
return bptr;
}
struct bipf_s* bipf_mkNone()
{
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_BOOLNONE;
bptr->u.i = -1;
return bptr;
}
struct bipf_s* bipf_mkList()
{
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_LIST;
return bptr;
}
struct bipf_s* bipf_mkDict()
{
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_DICT;
return bptr;
}
struct bipf_s* bipf_mkString(char *cp)
{
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_STRING;
bptr->cnt = strlen(cp);
bptr->u.str = (char*) malloc(bptr->cnt);
memcpy(bptr->u.str, cp, bptr->cnt);
return bptr;
}
void bipf_list_append(struct bipf_s *lptr, struct bipf_s *e)
{
lptr->cnt++;
// Serial.printf("realloc list\n\r");
lptr->u.list = (struct bipf_s**) realloc(lptr->u.list, lptr->cnt * sizeof(struct bipf_s*));
lptr->u.list[lptr->cnt - 1] = e;
}
struct bipf_s* bipf_list_getref(struct bipf_s *lptr, int i)
{
return lptr->u.list[i];
}
void bipf_dict_set(struct bipf_s *dptr, struct bipf_s *k, struct bipf_s *v)
{
int i;
for (i = 0; i < dptr->cnt; i++)
if (bipf_equal(k, dptr->u.dict[2*i]))
break;
if (i < dptr->cnt) {
bipf_free(dptr->u.dict[2*i]);
bipf_free(dptr->u.dict[2*i + 1]);
} else {
dptr->cnt++;
// Serial.printf("realloc dict new cnt=%d\n\r", dptr->cnt);
dptr->u.dict = (struct bipf_s**) realloc(dptr->u.dict, dptr->cnt * 2 * sizeof(struct bipf_s*));
}
// Serial.printf("dict set k=%p v=%p\n\r", k, v);
dptr->u.dict[2*i] = k;
dptr->u.dict[2*i + 1] = v;
}
struct bipf_s* bipf_dict_getref(struct bipf_s *dptr, struct bipf_s *k)
{
for (int i = 0; i < dptr->cnt; i++)
if (bipf_equal(k, dptr->u.dict[2*i])) {
// Serial.printf("dict_getref %d %s=", i, to_hex((unsigned char*) (k->u.str), k->cnt));
// Serial.printf("%s\r\n", to_hex((unsigned char*) (dptr->u.dict[2*i]->u.str), dptr->u.dict[2*i]->cnt));
return dptr->u.dict[2*i + 1];
}
return NULL;
}
void bipf_dict_delete(struct bipf_s *dptr, struct bipf_s *k)
{
// Serial.printf("BIPF dict delete %d\r\n", dptr->cnt);
for (int i = 0; i < dptr->cnt; i++)
if (bipf_equal(k, dptr->u.dict[2*i])) {
bipf_free(dptr->u.dict[2*i]);
bipf_free(dptr->u.dict[2*i + 1]);
// dptr->u.dict[2*i] = NULL;
// dptr->u.dict[2*i + 1] = NULL;
if (i != dptr->cnt-1)
memmove(dptr->u.dict + 2*i, dptr->u.dict + 2*i + 2,
2*(dptr->cnt - i - 1) * sizeof(struct bipf_s*));
dptr->cnt--;
return;
}
}
void bipf_free(struct bipf_s *bptr)
{
struct bipf_s **pp;
switch (bptr->typ) {
case BIPF_BYTES:
case BIPF_STRING:
free(bptr->u.buf);
break;
case BIPF_INT:
break;
case BIPF_LIST:
pp = bptr->u.list;
while (bptr->cnt-- > 0)
bipf_free(*pp++);
free(bptr->u.list);
break;
case BIPF_DICT:
pp = bptr->u.dict;
while (bptr->cnt-- > 0) {
bipf_free(*pp++);
bipf_free(*pp++);
}
free(bptr->u.dict);
break;
default:
// Serial.println("can't free BIPF for typ=" + String(bptr->typ));
break;
}
free(bptr);
}
// ------------------------------------------------------------------------------
unsigned int bipf_varint_decode(unsigned char *buf, int pos, int *lptr)
{
unsigned int val = 0;
int shift = 0;
int old = pos;
int end = pos + *lptr;
while (pos < end) {
unsigned int b = buf[pos];
val |= (b & 0x7f) << shift;
if ((b & 0x80) == 0)
break;
shift += 7;
pos += 1;
}
*lptr = pos - old + 1;
return val;
}
unsigned int bipf_varint_encoding_length(unsigned int val)
{
int cnt = 1;
while (1) {
val >>= 7;
if (val == 0)
return cnt;
cnt++;
}
}
unsigned int bipf_varint_encode(unsigned char *buf, unsigned int val)
{
unsigned char *old = buf - 1;
while (1) {
*buf = val & 0x7f;
val >>= 7;
if (val != 0)
*buf |= 0x80;
else
return buf - old;
buf++;
}
}
// ------------------------------------------------------------------------------
struct bipf_s* _bipf_dec_inner(int tag, unsigned char *buf, int pos, int *lptr)
{
// Serial.println(String(" inner ") + to_hex(buf+pos, 6) + " pos=" + String(pos) + " lim=" + String(*lptr));
if (tag == BIPF_BOOLNONE) { // i.e., length is 0
struct bipf_s *bptr = (struct bipf_s*) malloc(sizeof(struct bipf_s));
bptr->typ = BIPF_BOOLNONE;
*lptr = 0;
return bptr;
}
int t = tag & TAG_MASK;
int sz = tag >> TAG_SIZE;
int lim = pos + sz;
if (lim > *lptr)
return NULL;
// Serial.println(" inner dec: t=" + String(t) + " sz=" + String(sz) + " pos=" + String(pos));
switch(t) {
case BIPF_BOOLNONE: {
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_BOOLNONE;
bptr->u.i = sz == 0 ? -1 : (buf[pos] ? 1 : 0);
*lptr = sz;
return bptr;
}
case BIPF_BYTES: {
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_BYTES;
bptr->cnt = sz;
bptr->u.buf = (unsigned char*) malloc(sz);
memcpy(bptr->u.buf, buf + pos, sz);
*lptr = sz;
return bptr;
}
case BIPF_INT: {
// Serial.println(" int");
int old = pos;
int val = 0;
int i;
for (i = 0; i < 8*sz; i += 8) // little endian
val |= buf[pos++] << i;
int m = 1 << i+7;
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_INT;
bptr->u.i = (val & m) == 0 ? val : val - (m << 1);
// Serial.println("bipf INT " + String(bptr->u.i));
*lptr = pos - old;
return bptr;
}
case BIPF_LIST: {
int old = pos;
struct bipf_s *bptr = bipf_mkList();
while (pos < lim) {
// Serial.println("list loop pos=" + String(pos));
int szL = lim;
unsigned int tagL = bipf_varint_decode(buf, pos, &szL);
// Serial.println("bipf tagL " + String(tag) + "/" + String(tag>>3));
pos += szL;
szL = lim;
struct bipf_s *e = _bipf_dec_inner(tagL, buf, pos, &szL);
if (e == NULL)
return NULL;
bipf_list_append(bptr, e);
pos += szL;
}
*lptr = pos - old;
// Serial.println("cnt=" + String(bptr->cnt));
return bptr;
}
case BIPF_DICT: {
int old = pos;
struct bipf_s *bptr = bipf_mkDict();
while (pos < lim) {
// Serial.println("dict loop pos=" + String(pos));
int szK = lim;
unsigned int tagK = bipf_varint_decode(buf, pos, &szK);
pos += szK;
szK = lim;
struct bipf_s *k = _bipf_dec_inner(tagK, buf, pos, &szK);
if (k == NULL)
return NULL;
pos += szK;
int szV = lim;
unsigned int tagV = bipf_varint_decode(buf, pos, &szV);
pos += szV;
szV = lim;
struct bipf_s *v = _bipf_dec_inner(tagV, buf, pos, &szV);
if (v == NULL)
return NULL;
bipf_dict_set(bptr, k, v);
pos += szV;
}
*lptr = pos - old;
// Serial.println("cnt=" + String(bptr->cnt));
return bptr;
}
case BIPF_STRING: {
struct bipf_s *bptr = (struct bipf_s*) calloc(1, sizeof(struct bipf_s));
bptr->typ = BIPF_STRING;
bptr->cnt = sz;
bptr->u.buf = (unsigned char*) malloc(sz+1);
memcpy(bptr->u.str, buf + pos, sz);
bptr->u.str[sz] = '\0';
*lptr = sz;
return bptr;
}
default:
Serial.printf("bipf not implemented or wrong tag=%d, pos=%d\r\n", tag, pos);
break;
}
return NULL; // _type2decoder[t](buf, pos, tag >> _TAG_SIZE, lptr);
}
// ------------------------------------------------------------------------------
struct bipf_s* bipf_decode(unsigned char *buf, int pos, int *lptr)
{
// Serial.printf("decode %s\r\n", to_hex(buf+pos, *lptr, 0));
// read the next value from buffer at start.
// returns a tuple with the value and the consumed bytes
int sz1 = *lptr;
int tag = bipf_varint_decode(buf, pos, &sz1);
int sz2 = *lptr;
// Serial.printf("bipf tag %d/%d sz1=%d sz2=%d\r\n", tag&7, tag>>3, sz1, sz2);
struct bipf_s *val = _bipf_dec_inner(tag, buf, pos + sz1, &sz2);
*lptr = sz1 + sz2;
return val;
}
int bipf_bodyLength(bipf_s *bptr)
{
// returns the length needed for the body of the encode value
// if val == None:
// return 1
switch (bptr->typ) { // length of body
case BIPF_BOOLNONE:
return bptr->u.i >= 0 ? 1 : 0;
case BIPF_BYTES:
case BIPF_STRING:
return bptr->cnt;
case BIPF_INT: {
int val = bptr->u.i;
if (val < 0) val = -val - 1;
int sz = 1;
val >>= 8;
while (val > 0) {
sz++;
val >>= 8;
}
// Serial.println("int " + String(bptr->u.i) + " size=" + String(sz));
return sz;
}
case BIPF_LIST: {
int sz = 0;
for (int i = 0; i < bptr->cnt; i++)
sz += bipf_encodingLength(bptr->u.list[i]);
// Serial.println("list " + String(sz));
return sz;
}
case BIPF_DICT: {
int sz = 0;
for (int i = 0; i < bptr->cnt; i++) {
sz += bipf_encodingLength(bptr->u.dict[2*i]);
sz += bipf_encodingLength(bptr->u.dict[2*i+1]);
}
// Serial.println("dict " + String(sz));
return sz;
}
default:
break;
}
// Serial.println("unknown BIPF type " + String(bptr->typ));
return -1;
}
int bipf_encodingLength(bipf_s *bptr)
{
// returns the length needed to encode the value (header and body)
int sz = bipf_bodyLength(bptr);
int len = bipf_varint_encoding_length(sz << TAG_SIZE) + sz;
// Serial.println(String(bptr->typ) + " sz=" + String(sz) + ", encLen=" + String(len));
return len;
}
void bipf_encodeBody(unsigned char *buf, bipf_s *bptr)
{
switch (bptr->typ) { // length of body
case BIPF_BOOLNONE:
*buf = bptr->u.i >= 0 ? 1 : 0;
return;
case BIPF_BYTES:
case BIPF_STRING:
memcpy(buf, bptr->u.buf, bptr->cnt);
return;
case BIPF_INT: {
int val = bptr->u.i;
if (val < 0) val = -val - 1;
*buf++ = (unsigned int) val & 0xff;
val >>= 8;
while (val > 0) {
*buf++ = (unsigned int) val & 0xff;
val >>= 8;
}
return;
}
case BIPF_LIST:
for (int i = 0; i < bptr->cnt; i++)
buf += bipf_encode(buf, bptr->u.list[i]);
return;
case BIPF_DICT:
for (int i = 0; i < bptr->cnt; i++) {
buf += bipf_encode(buf, bptr->u.dict[2*i]);
buf += bipf_encode(buf, bptr->u.dict[2*i+1]);
}
return;
default:
break;
}
// Serial.println("encodeBody: unknown BIPF type " + String(bptr->typ));
}
int bipf_encode(unsigned char *buf, struct bipf_s *bptr)
{
int sz = bipf_bodyLength(bptr);
int tagInt = sz << TAG_SIZE | bptr->typ;
int tagLen = bipf_varint_encode(buf, tagInt);
if (sz > 0)
bipf_encodeBody(buf+tagLen, bptr);
return tagLen + sz;
}
unsigned char* bipf_dumps(struct bipf_s *bptr, int *len)
{
*len = bipf_encodingLength(bptr);
unsigned char *buf = (unsigned char*) malloc(*len);
bipf_encode(buf, bptr);
return buf;
}
struct bipf_s* bipf_loads(unsigned char *buf, int len)
{
return bipf_decode(buf, 0, &len);
}
struct bipf_s* str2bipf(char *s) // returns addr of a static (!) BIPF obj
{
static bipf_s e = { BIPF_STRING };
e.cnt = strlen(s);
e.u.str = s;
return &e;
}
String bipf2String(struct bipf_s *bptr, char *nl, int lev)
{
String v, indent, r = nl;
struct bipf_s **pp;
indent = "";
if (nl)
for (int i = 0; i < lev; i++)
indent += " ";
else
r = "";
switch (bptr->typ) {
case BIPF_BYTES:
return "#" + String(to_hex(bptr->u.buf, bptr->cnt));
case BIPF_STRING: {
char *c_str = (char*) malloc(bptr->cnt+1);
memcpy(c_str, bptr->u.str, bptr->cnt);
c_str[bptr->cnt] = '\0';
String s(c_str);
free(c_str);
return "\"" + s + "\"";
}
case BIPF_BOOLNONE:
if (bptr->u.i < 0)
return "none";
if (bptr->u.i == 0)
return "false";
return "true";
case BIPF_DOUBLE:
return String(bptr->u.d);
case BIPF_INT:
return String(bptr->u.i);
case BIPF_LIST:
if (bptr->cnt == 0)
return "[]";
v = "[" + r;
pp = bptr->u.list;
for (int i = 0; i < bptr->cnt; i++) {
if (i > 0)
v += "," + r;
v += indent + bipf2String(*pp++, nl, lev+1);
}
return v + r + indent + "]";
case BIPF_DICT:
if (bptr->cnt == 0)
return "{}";
v = "{" + r;
pp = bptr->u.dict;
for (int i = 0; i < bptr->cnt; i++) {
if (i > 0)
v += "," + r;
v += indent + bipf2String(*pp++, nl, lev+1) + (nl ? ": " : ":");
v += bipf2String(*pp++, nl, lev+1);
}
return v + r + indent + "}";
default:
// Serial.println("can't render typ=" + String(bptr->typ));
break;
}
return "?";
}
// eof