forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bser.cpp
740 lines (631 loc) · 16.1 KB
/
bser.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/* Copyright 2013-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include "thirdparty/jansson/jansson_private.h"
/*
* This defines a binary serialization of the JSON data objects in this
* library. It is designed for use with watchman and is not intended to serve
* as a general binary JSON interchange format. In particular, all integers
* are signed integers and are stored in host byte order to minimize
* transformation overhead.
*/
/* Return the smallest size int that can store the value */
#define INT_SIZE(x) \
(((x) == ((int8_t)x)) \
? 1 \
: ((x) == ((int16_t)x)) ? 2 : ((x) == ((int32_t)x)) ? 4 : 8)
#define BSER_ARRAY 0x00
#define BSER_OBJECT 0x01
#define BSER_BYTESTRING 0x02
#define BSER_INT8 0x03
#define BSER_INT16 0x04
#define BSER_INT32 0x05
#define BSER_INT64 0x06
#define BSER_REAL 0x07
#define BSER_TRUE 0x08
#define BSER_FALSE 0x09
#define BSER_NULL 0x0a
#define BSER_TEMPLATE 0x0b
#define BSER_SKIP 0x0c
#define BSER_UTF8STRING 0x0d
static const char bser_true = BSER_TRUE;
static const char bser_false = BSER_FALSE;
static const char bser_null = BSER_NULL;
static const char bser_bytestring_hdr = BSER_BYTESTRING;
static const char bser_array_hdr = BSER_ARRAY;
static const char bser_object_hdr = BSER_OBJECT;
static const char bser_template_hdr = BSER_TEMPLATE;
static const char bser_utf8string_hdr = BSER_UTF8STRING;
static const char bser_skip = BSER_SKIP;
static bool is_bser_version_supported(const bser_ctx_t* ctx) {
return ctx->bser_version == 1 || ctx->bser_version == 2;
}
static int bser_real(const bser_ctx_t* ctx, double val, void* data) {
char sz = BSER_REAL;
if (!is_bser_version_supported(ctx)) {
return -1;
}
if (ctx->dump(&sz, sizeof(sz), data)) {
return -1;
}
return ctx->dump((char*)&val, sizeof(val), data);
}
bool bunser_generic_string(
const char* buf,
json_int_t avail,
json_int_t* needed,
const char** start,
json_int_t* len) {
json_int_t ineed;
if (!bunser_int(buf + 1, avail - 1, &ineed, len)) {
*needed = ineed;
return false;
}
buf += ineed + 1;
avail -= ineed + 1;
*needed = ineed + 1 + *len;
if (*len > avail) {
return false;
}
*start = buf;
return true;
}
// Attempt to unserialize an integer value.
// Returns bool if successful, and populates *val with the value.
// Otherwise populates *needed with the size required to successfully
// decode the integer value
bool bunser_int(
const char* buf,
json_int_t avail,
json_int_t* needed,
json_int_t* val) {
int8_t i8;
int16_t i16;
int32_t i32;
int64_t i64;
switch (buf[0]) {
case BSER_INT8:
*needed = 2;
break;
case BSER_INT16:
*needed = 3;
break;
case BSER_INT32:
*needed = 5;
break;
case BSER_INT64:
*needed = 9;
break;
default:
*needed = -1;
return false;
}
if (avail < *needed) {
return false;
}
switch (buf[0]) {
case BSER_INT8:
memcpy(&i8, buf + 1, sizeof(i8));
*val = i8;
return true;
case BSER_INT16:
memcpy(&i16, buf + 1, sizeof(i16));
*val = i16;
return true;
case BSER_INT32:
memcpy(&i32, buf + 1, sizeof(i32));
*val = i32;
return true;
case BSER_INT64:
memcpy(&i64, buf + 1, sizeof(i64));
*val = i64;
return true;
default:
return false;
}
}
static int bser_int(const bser_ctx_t* ctx, json_int_t val, void* data) {
int8_t i8;
int16_t i16;
int32_t i32;
int64_t i64;
char sz;
int size = INT_SIZE(val);
char* iptr;
if (!is_bser_version_supported(ctx)) {
return -1;
}
switch (size) {
case 1:
sz = BSER_INT8;
i8 = (int8_t)val;
iptr = (char*)&i8;
break;
case 2:
sz = BSER_INT16;
i16 = (int16_t)val;
iptr = (char*)&i16;
break;
case 4:
sz = BSER_INT32;
i32 = (int32_t)val;
iptr = (char*)&i32;
break;
case 8:
sz = BSER_INT64;
i64 = (int64_t)val;
iptr = (char*)&i64;
break;
default:
return -1;
}
if (ctx->dump(&sz, sizeof(sz), data)) {
return -1;
}
return ctx->dump(iptr, size, data);
}
static int bser_generic_string(
const bser_ctx_t* ctx,
w_string_piece str,
void* data,
const char hdr) {
if (!is_bser_version_supported(ctx)) {
return -1;
}
if (ctx->dump(&hdr, sizeof(hdr), data)) {
return -1;
}
if (bser_int(ctx, str.size(), data)) {
return -1;
}
if (ctx->dump(str.data(), str.size(), data)) {
return -1;
}
return 0;
}
static int
bser_bytestring(const bser_ctx_t* ctx, w_string_piece str, void* data) {
return bser_generic_string(ctx, str, data, bser_bytestring_hdr);
}
static int
bser_utf8string(const bser_ctx_t* ctx, w_string_piece str, void* data) {
if ((ctx->bser_capabilities & BSER_CAP_DISABLE_UNICODE) ||
ctx->bser_version == 1) {
return bser_bytestring(ctx, str, data);
}
return bser_generic_string(ctx, str, data, bser_utf8string_hdr);
}
static int
bser_mixedstring(const bser_ctx_t* ctx, w_string_piece str, void* data) {
if (ctx->bser_version != 1 &&
!(BSER_CAP_DISABLE_UNICODE_FOR_ERRORS & ctx->bser_capabilities) &&
!(BSER_CAP_DISABLE_UNICODE & ctx->bser_capabilities)) {
auto utf8_clean = str.asUTF8Clean();
return bser_utf8string(ctx, utf8_clean, data);
} else {
return bser_bytestring(ctx, str, data);
}
}
static int bser_array(const bser_ctx_t* ctx, const json_t* array, void* data);
static int bser_template(
const bser_ctx_t* ctx,
const json_t* array,
const json_t* templ,
void* data) {
size_t n = json_array_size(array);
size_t i, pn;
if (!is_bser_version_supported(ctx)) {
return -1;
}
if (ctx->dump(&bser_template_hdr, sizeof(bser_template_hdr), data)) {
return -1;
}
// The template goes next
if (bser_array(ctx, templ, data)) {
return -1;
}
// Now the array of arrays of object values.
// How many objects
if (bser_int(ctx, n, data)) {
return -1;
}
pn = json_array_size(templ);
// For each object
for (i = 0; i < n; i++) {
auto obj = json_array_get(array, i);
size_t pi;
// For each factored key
for (pi = 0; pi < pn; pi++) {
const char* key = json_string_value(json_array_get(templ, pi));
// Look up the object property
auto val = json_object_get(obj, key);
if (!val) {
// property not set on this one; emit a skip
if (ctx->dump(&bser_skip, sizeof(bser_skip), data)) {
return -1;
}
continue;
}
// Emit value
if (w_bser_dump(ctx, val, data)) {
return -1;
}
}
}
return 0;
}
static int bser_array(const bser_ctx_t* ctx, const json_t* array, void* data) {
size_t n = json_array_size(array);
size_t i;
if (!is_bser_version_supported(ctx)) {
return -1;
}
auto templ = json_array_get_template(array);
if (templ) {
return bser_template(ctx, array, templ, data);
}
if (ctx->dump(&bser_array_hdr, sizeof(bser_array_hdr), data)) {
return -1;
}
if (bser_int(ctx, n, data)) {
return -1;
}
for (i = 0; i < n; i++) {
auto val = json_array_get(array, i);
if (w_bser_dump(ctx, val, data)) {
return -1;
}
}
return 0;
}
static int bser_object(const bser_ctx_t* ctx, const json_ref& obj, void* data) {
size_t n;
if (!is_bser_version_supported(ctx)) {
return -1;
}
if (ctx->dump(&bser_object_hdr, sizeof(bser_object_hdr), data)) {
return -1;
}
n = json_object_size(obj);
if (bser_int(ctx, n, data)) {
return -1;
}
for (auto& it : obj.object()) {
auto& key = it.first;
auto& val = it.second;
if (bser_bytestring(ctx, key.c_str(), data)) {
return -1;
}
if (w_bser_dump(ctx, val, data)) {
return -1;
}
}
return 0;
}
int w_bser_dump(const bser_ctx_t* ctx, const json_ref& json, void* data) {
int type = json_typeof(json);
if (!is_bser_version_supported(ctx)) {
return -1;
}
switch (type) {
case JSON_NULL:
return ctx->dump(&bser_null, sizeof(bser_null), data);
case JSON_TRUE:
return ctx->dump(&bser_true, sizeof(bser_true), data);
case JSON_FALSE:
return ctx->dump(&bser_false, sizeof(bser_false), data);
case JSON_REAL:
return bser_real(ctx, json_real_value(json), data);
case JSON_INTEGER:
return bser_int(ctx, json.asInt(), data);
case JSON_STRING: {
auto& wstr = json_to_w_string(json);
switch (wstr.type()) {
case W_STRING_BYTE:
return bser_bytestring(ctx, wstr, data);
case W_STRING_UNICODE:
return bser_utf8string(ctx, wstr, data);
case W_STRING_MIXED:
return bser_mixedstring(ctx, wstr, data);
default:
w_assert(false, "unknown string type 0x%02x", wstr.type());
return -1;
}
}
case JSON_ARRAY:
return bser_array(ctx, json, data);
case JSON_OBJECT:
return bser_object(ctx, json, data);
default:
return -1;
}
}
static int measure(const char*, size_t size, void* ptr) {
auto tot = (json_int_t*)ptr;
*tot += size;
return 0;
}
int w_bser_write_pdu(
const uint32_t bser_version,
const uint32_t bser_capabilities,
json_dump_callback_t dump,
const json_ref& json,
void* data) {
json_int_t m_size = 0;
bser_ctx_t ctx{bser_version, bser_capabilities, measure};
if (!is_bser_version_supported(&ctx)) {
return -1;
}
if (w_bser_dump(&ctx, json, &m_size)) {
return -1;
}
// To actually write the contents
ctx.dump = dump;
if (bser_version == 2) {
if (dump(BSER_V2_MAGIC, 2, data)) {
return -1;
}
} else {
if (dump(BSER_MAGIC, 2, data)) {
return -1;
}
}
if (bser_version == 2) {
if (dump(
(const char*)&bser_capabilities, sizeof(bser_capabilities), data)) {
return -1;
}
}
if (bser_int(&ctx, m_size, data)) {
return -1;
}
if (w_bser_dump(&ctx, json, data)) {
return -1;
}
return 0;
}
static json_ref bunser_array(
const char* buf,
const char* end,
json_int_t* used,
json_error_t* jerr) {
json_int_t needed;
json_int_t total = 0;
json_int_t i, nelems;
buf++;
total++;
if (!bunser_int(buf, end - buf, &needed, &nelems)) {
if (needed == -1) {
snprintf(
jerr->text,
sizeof(jerr->text),
"invalid integer encoding 0x%02x for array length. buf=%p\n",
(int)buf[0],
buf);
return nullptr;
}
*used = needed + total;
snprintf(
jerr->text,
sizeof(jerr->text),
"invalid array length encoding 0x%02x (needed %d but have %d)",
(int)buf[0],
(int)needed,
(int)(end - buf));
return nullptr;
}
total += needed;
buf += needed;
auto arrval = json_array();
for (i = 0; i < nelems; i++) {
needed = 0;
auto item = bunser(buf, end, &needed, jerr);
total += needed;
buf += needed;
if (!item) {
*used = total;
return nullptr;
}
if (json_array_append_new(arrval, std::move(item))) {
*used = total;
snprintf(jerr->text, sizeof(jerr->text), "failed to append array item");
return nullptr;
}
}
*used = total;
return arrval;
}
static json_ref bunser_template(
const char* buf,
const char* end,
json_int_t* used,
json_error_t* jerr) {
json_int_t needed = 0;
json_int_t total = 0;
json_int_t i, nelems;
json_int_t ip, np;
buf++;
total++;
if (*buf != BSER_ARRAY) {
snprintf(
jerr->text,
sizeof(jerr->text),
"Expected array encoding, but found 0x%02x",
*buf);
*used = total;
return nullptr;
}
// Load in the property names template
auto templ = bunser_array(buf, end, &needed, jerr);
if (!templ) {
*used = needed + total;
return nullptr;
}
total += needed;
buf += needed;
// And the number of objects
needed = 0;
if (!bunser_int(buf, end - buf, &needed, &nelems)) {
*used = needed + total;
snprintf(
jerr->text,
sizeof(jerr->text),
"invalid object number encoding (needed %d but have %d)",
(int)needed,
(int)(end - buf));
return nullptr;
}
total += needed;
buf += needed;
np = json_array_size(templ);
// Now load up the array with object values
auto arrval = json_array_of_size((size_t)nelems);
for (i = 0; i < nelems; i++) {
auto item = json_object_of_size((size_t)np);
for (ip = 0; ip < np; ip++) {
if (*buf == BSER_SKIP) {
buf++;
total++;
continue;
}
needed = 0;
auto val = bunser(buf, end, &needed, jerr);
if (!val) {
*used = needed + total;
return nullptr;
}
buf += needed;
total += needed;
json_object_set_new_nocheck(
item,
json_string_value(json_array_get(templ, (size_t)ip)),
std::move(val));
}
json_array_append_new(arrval, std::move(item));
}
*used = total;
return arrval;
}
static json_ref bunser_object(
const char* buf,
const char* end,
json_int_t* used,
json_error_t* jerr) {
json_int_t needed;
json_int_t total = 0;
json_int_t i, nelems;
char keybuf[128];
total = 1;
buf++;
if (!bunser_int(buf, end - buf, &needed, &nelems)) {
*used = needed + total;
snprintf(
jerr->text,
sizeof(jerr->text),
"invalid object property count encoding");
return nullptr;
}
total += needed;
buf += needed;
auto objval = json_object();
for (i = 0; i < nelems; i++) {
const char* start;
json_int_t slen;
// Read key
if (!bunser_generic_string(buf, end - buf, &needed, &start, &slen)) {
*used = total + needed;
snprintf(
jerr->text, sizeof(jerr->text), "invalid bytestring for object key");
return nullptr;
}
total += needed;
buf += needed;
// Saves us allocating a string when the library is going to
// do that anyway
if ((uint16_t)slen > sizeof(keybuf) - 1) {
snprintf(jerr->text, sizeof(jerr->text), "object key is too long");
return nullptr;
}
memcpy(keybuf, start, (size_t)slen);
keybuf[slen] = '\0';
// Read value
auto item = bunser(buf, end, &needed, jerr);
total += needed;
buf += needed;
if (!item) {
*used = total;
return nullptr;
}
if (json_object_set_new_nocheck(objval, keybuf, std::move(item))) {
*used = total;
snprintf(jerr->text, sizeof(jerr->text), "failed to add object property");
return nullptr;
}
}
*used = total;
return objval;
}
json_ref bunser(
const char* buf,
const char* end,
json_int_t* needed,
json_error_t* jerr) {
json_int_t ival;
switch (buf[0]) {
case BSER_INT8:
case BSER_INT16:
case BSER_INT32:
case BSER_INT64:
if (!bunser_int(buf, end - buf, needed, &ival)) {
snprintf(jerr->text, sizeof(jerr->text), "invalid integer encoding");
return nullptr;
}
return json_integer(ival);
case BSER_BYTESTRING:
case BSER_UTF8STRING: {
const char* start;
json_int_t len;
if (!bunser_generic_string(buf, end - buf, needed, &start, &len)) {
snprintf(jerr->text, sizeof(jerr->text), "invalid bytestring encoding");
return nullptr;
}
return typed_string_to_json(
start,
len,
buf[0] == BSER_BYTESTRING ? W_STRING_BYTE : W_STRING_UNICODE);
}
case BSER_REAL: {
double dval;
*needed = sizeof(double) + 1;
memcpy(&dval, buf + 1, sizeof(dval));
return json_real(dval);
}
case BSER_TRUE:
*needed = 1;
return json_true();
case BSER_FALSE:
*needed = 1;
return json_false();
case BSER_NULL:
*needed = 1;
return json_null();
case BSER_ARRAY:
return bunser_array(buf, end, needed, jerr);
case BSER_TEMPLATE:
return bunser_template(buf, end, needed, jerr);
case BSER_OBJECT:
return bunser_object(buf, end, needed, jerr);
default:
snprintf(
jerr->text,
sizeof(jerr->text),
"invalid bser encoding type %02x",
(int)buf[0]);
return nullptr;
}
#ifndef _WIN32 // It knows this is unreachable
return nullptr;
#endif
}
/* vim:ts=2:sw=2:et:
*/