-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson.cpp
More file actions
1515 lines (1402 loc) · 54.3 KB
/
Copy pathjson.cpp
File metadata and controls
1515 lines (1402 loc) · 54.3 KB
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
#include "json.h"
#include <charconv>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <utility>
#include "utf.h"
#include "vlarray.h"
namespace {
using namespace stdc;
struct EmptyValues {
static inline const JsonValue &nullValue() {
static const JsonValue null;
return null;
}
static inline const JsonArray &emptyArray() {
static const JsonArray emptyArray;
return emptyArray;
}
static inline const JsonObject &emptyObject() {
static const JsonObject emptyObject;
return emptyObject;
}
};
// ------------------------------------------------------------------------------------------
// Text output
// ------------------------------------------------------------------------------------------
void appendUtf8(std::string &out, char32_t cp) {
if (cp < 0x80) {
out += char(cp);
} else if (cp < 0x800) {
out += char(0xC0 | (cp >> 6));
out += char(0x80 | (cp & 0x3F));
} else if (cp < 0x10000) {
out += char(0xE0 | (cp >> 12));
out += char(0x80 | ((cp >> 6) & 0x3F));
out += char(0x80 | (cp & 0x3F));
} else {
out += char(0xF0 | (cp >> 18));
out += char(0x80 | ((cp >> 12) & 0x3F));
out += char(0x80 | ((cp >> 6) & 0x3F));
out += char(0x80 | (cp & 0x3F));
}
}
void quoteTo(std::string &out, std::string_view s) {
std::string sanitized;
if (!stdc::utf::is_valid_utf8(s)) {
// There is nowhere to report this from, and refusing to serialize would make toJson()
// the one accessor that can fail. Substituting U+FFFD is what stdc::utf does by
// default, and it leaves the rest of the document readable.
sanitized = stdc::utf::utf32_to_utf8(stdc::utf::utf8_to_utf32(s));
s = sanitized;
}
out += '"';
for (size_t i = 0; i < s.size();) {
// Most text needs no escaping, so it goes out one range at a time. A byte above
// 0x7F is ordinary here, which is what keeps the text UTF-8 rather than escapes.
const size_t start = i;
while (i < s.size() && s[i] != '"' && s[i] != '\\' && uint8_t(s[i]) >= 0x20) {
++i;
}
out.append(s.data() + start, i - start);
if (i == s.size()) {
break;
}
const char c = s[i++];
switch (c) {
case '"':
out += "\\\"";
continue;
case '\\':
out += "\\\\";
continue;
case '\b':
out += "\\b";
continue;
case '\f':
out += "\\f";
continue;
case '\n':
out += "\\n";
continue;
case '\r':
out += "\\r";
continue;
case '\t':
out += "\\t";
continue;
default:
break;
}
// A control character with no name of its own, and nothing else: the run stops only
// at a quote, a backslash or a byte below 0x20, and the switch took the rest.
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", unsigned(uint8_t(c)));
out += buf;
}
out += '"';
}
void formatDouble(std::string &out, double d) {
if (!std::isfinite(d)) {
// JSON cannot write these at all. Null is what the value reads back as.
out += "null";
return;
}
char buf[40];
size_t n;
#if defined(__cpp_lib_to_chars) && __cpp_lib_to_chars >= 201611L
auto res = std::to_chars(buf, buf + sizeof(buf), d);
n = size_t(res.ptr - buf);
#else
// The shortest form that reads back as the same value. Seventeen digits always suffice for
// a double, but most values need fewer and look better for it.
int written = 0;
for (int precision = 15; precision <= 17; ++precision) {
written = std::snprintf(buf, sizeof(buf), "%.*g", precision, d);
if (std::strtod(buf, nullptr) == d) {
break;
}
}
n = size_t(written);
#endif
std::string_view sv(buf, n);
out += sv;
// A double that happens to be integral has to keep looking like one, or a round trip
// through text turns it into an integer.
if (sv.find_first_of(".eE") == std::string_view::npos) {
out += ".0";
}
}
template <class T>
void appendInteger(std::string &out, T value) {
char buf[32];
const auto result = std::to_chars(buf, buf + sizeof(buf), value);
out.append(buf, result.ptr);
}
void dumpTo(std::string &out, const JsonValue &v, int indent, int depth) {
const bool pretty = indent > 0;
auto newline = [&](int d) {
if (pretty) {
out += '\n';
out.append(size_t(indent) * size_t(d), ' ');
}
};
switch (v.type()) {
case JsonValue::Null:
out += "null";
return;
case JsonValue::Bool:
out += v.toBool() ? "true" : "false";
return;
case JsonValue::Int:
appendInteger(out, v.toInt());
return;
case JsonValue::Double:
formatDouble(out, v.toDouble());
return;
case JsonValue::String:
quoteTo(out, v.toStringView());
return;
case JsonValue::Binary: {
// Binary has no JSON form. This is the shape it takes so a document holding one is
// still writable, and it does not read back as binary.
out += "{\"bytes\":[";
const auto &bytes = v.toBinary();
for (size_t i = 0; i < bytes.size(); ++i) {
if (i) {
out += ',';
}
appendInteger(out, unsigned(bytes[i]));
}
out += "],\"subtype\":null}";
return;
}
case JsonValue::Array: {
const auto &arr = v.toArray();
if (arr.empty()) {
out += "[]";
return;
}
out += '[';
for (size_t i = 0; i < arr.size(); ++i) {
if (i) {
out += ',';
}
newline(depth + 1);
dumpTo(out, arr[i], indent, depth + 1);
}
newline(depth);
out += ']';
return;
}
case JsonValue::Object: {
const auto &obj = v.toObject();
if (obj.empty()) {
out += "{}";
return;
}
out += '{';
bool first = true;
for (const auto &item : obj) {
if (!first) {
out += ',';
}
first = false;
newline(depth + 1);
quoteTo(out, item.first);
out += pretty ? ": " : ":";
dumpTo(out, item.second, indent, depth + 1);
}
newline(depth);
out += '}';
return;
}
}
}
// ------------------------------------------------------------------------------------------
// Text input
// ------------------------------------------------------------------------------------------
/// A recursive descent parser over the whole input.
///
/// \note The nesting limit is not a formality. Without it a document of nothing but opening
/// brackets overflows the stack, and a package manifest does not necessarily come from
/// someone trustworthy.
class Parser {
public:
/// Deep enough for real documents -- the deepest in the JSON test corpus nests 468 -- and
/// far enough below what the stack can take. A debug build on Windows, where the frames
/// are widest and the stack is a megabyte, faults at around 950 levels, so this keeps
/// most of a factor of two in the worst case and much more in a release build.
static constexpr int maxDepth = 512;
Parser(std::string_view text, bool comments) : _s(text), _comments(comments) {
}
bool parse(JsonValue *out) {
// A byte order mark carries no information in UTF-8, but editors on Windows write one
// anyway. RFC 8259 lets a parser skip it, so skip it.
if (_s.size() >= 3 && _s.compare(0, 3, "\xEF\xBB\xBF") == 0) {
_pos = 3;
}
skipSpace();
if (!parseValue(out, 0)) {
return false;
}
skipSpace();
if (_pos != _s.size()) {
return fail(JsonParseError::TrailingContent, "trailing content after the value");
}
return true;
}
const JsonParseError &error() const {
return _error;
}
private:
bool fail(JsonParseError::Code code, const char *what) {
if (_error) {
return false;
}
// A comment where one is not allowed reads as an unexpected token from every position
// a comment can take, leaving the caller to work out that all it has to do is ask
// again with them on. Named here rather than at each of those positions.
if (code == JsonParseError::UnexpectedToken && !_comments && _pos + 1 < _s.size() &&
_s[_pos] == '/' && (_s[_pos + 1] == '/' || _s[_pos + 1] == '*')) {
code = JsonParseError::CommentNotAllowed;
what = "a comment, which this parse was not asked to ignore";
}
_error.code = code;
_error.offset = _pos;
_error.what = what;
// Counted here rather than tracked as we go, since it only matters once.
_error.line = 1;
_error.column = 1;
for (size_t i = 0; i < _pos && i < _s.size(); ++i) {
if (_s[i] == '\n') {
++_error.line;
_error.column = 1;
} else {
++_error.column;
}
}
return false;
}
bool atEnd() const {
return _pos >= _s.size();
}
char peek() const {
return _s[_pos];
}
void skipSpace() {
for (;;) {
while (!atEnd() &&
(peek() == ' ' || peek() == '\t' || peek() == '\n' || peek() == '\r')) {
++_pos;
}
if (!_comments || _pos + 1 >= _s.size() || peek() != '/') {
return;
}
if (_s[_pos + 1] == '/') {
_pos += 2;
while (!atEnd() && peek() != '\n') {
++_pos;
}
} else if (_s[_pos + 1] == '*') {
_pos += 2;
while (_pos + 1 < _s.size() && !(peek() == '*' && _s[_pos + 1] == '/')) {
++_pos;
}
// An unterminated comment is caught by whatever expected a value next.
_pos = _pos + 1 < _s.size() ? _pos + 2 : _s.size();
} else {
return;
}
}
}
bool literal(std::string_view word) {
if (_s.compare(_pos, word.size(), word) != 0) {
return false;
}
_pos += word.size();
return true;
}
bool parseValue(JsonValue *out, int depth) {
if (depth > maxDepth) {
return fail(JsonParseError::NestedTooDeeply, "nested too deeply");
}
if (atEnd()) {
return fail(JsonParseError::UnexpectedEnd, "expected a value");
}
switch (peek()) {
case 'n':
if (!literal("null")) {
return fail(JsonParseError::UnexpectedToken, "expected a value");
}
*out = JsonValue();
return true;
case 't':
if (!literal("true")) {
return fail(JsonParseError::UnexpectedToken, "expected a value");
}
*out = JsonValue(true);
return true;
case 'f':
if (!literal("false")) {
return fail(JsonParseError::UnexpectedToken, "expected a value");
}
*out = JsonValue(false);
return true;
case '"': {
std::string s;
if (!parseString(&s)) {
return false;
}
*out = JsonValue(std::move(s));
return true;
}
case '[':
return parseArray(out, depth);
case '{':
return parseObject(out, depth);
default:
return parseNumber(out);
}
}
bool parseArray(JsonValue *out, int depth) {
++_pos; // '['
JsonArray arr;
skipSpace();
if (!atEnd() && peek() == ']') {
++_pos;
*out = JsonValue(std::move(arr));
return true;
}
for (;;) {
skipSpace();
// The slot has to exist before the value can be parsed into it, so the count is
// guessed here rather than deduced from a comma later. Two is the guess. A
// one-element array pays one unused slot, not another allocation.
if (arr.empty()) {
arr.reserve(2);
}
arr.emplace_back();
if (!parseValue(&arr.back(), depth + 1)) {
return false;
}
skipSpace();
if (atEnd()) {
return fail(JsonParseError::UnexpectedEnd, "expected ',' or ']'");
}
bool hasNext = false;
if (peek() == ',') {
++_pos;
hasNext = true;
} else if (peek() == ']') {
++_pos;
} else {
return fail(JsonParseError::UnexpectedToken, "expected ',' or ']'");
}
if (!hasNext) {
*out = JsonValue(std::move(arr));
return true;
}
}
}
bool parseObject(JsonValue *out, int depth) {
++_pos; // '{'
JsonObject obj;
skipSpace();
if (!atEnd() && peek() == '}') {
++_pos;
*out = JsonValue(std::move(obj));
return true;
}
for (;;) {
skipSpace();
if (atEnd() || peek() != '"') {
return fail(atEnd() ? JsonParseError::UnexpectedEnd
: JsonParseError::UnexpectedToken,
"expected a key");
}
std::string key;
if (!parseString(&key)) {
return false;
}
skipSpace();
if (atEnd() || peek() != ':') {
return fail(atEnd() ? JsonParseError::UnexpectedEnd
: JsonParseError::UnexpectedToken,
"expected ':'");
}
++_pos;
skipSpace();
// A repeated key keeps the last one, which is what every JSON reader does: the
// node is taken first, and the value is parsed straight into it, over whatever a
// previous occurrence of the key left there.
auto it = obj.try_emplace(std::move(key)).first;
if (!parseValue(&it->second, depth + 1)) {
return false;
}
skipSpace();
if (atEnd()) {
return fail(JsonParseError::UnexpectedEnd, "expected ',' or '}'");
}
if (peek() == ',') {
++_pos;
continue;
}
if (peek() == '}') {
++_pos;
*out = JsonValue(std::move(obj));
return true;
}
return fail(JsonParseError::UnexpectedToken, "expected ',' or '}'");
}
}
bool hex4(char32_t *out) {
if (_pos + 4 > _s.size()) {
return false;
}
char32_t v = 0;
for (int i = 0; i < 4; ++i) {
char c = _s[_pos + size_t(i)];
v <<= 4;
if (c >= '0' && c <= '9') {
v |= char32_t(c - '0');
} else if (c >= 'a' && c <= 'f') {
v |= char32_t(c - 'a' + 10);
} else if (c >= 'A' && c <= 'F') {
v |= char32_t(c - 'A' + 10);
} else {
return false;
}
}
_pos += 4;
*out = v;
return true;
}
bool parseString(std::string *out) {
++_pos; // '"'
std::string res;
bool hasNonAscii = false;
for (;;) {
// Ordinary bytes dominate real documents. Append each uninterrupted range once,
// leaving the switch below only the escapes and errors it actually has to decode.
const size_t start = _pos;
while (!atEnd() && peek() != '"' && peek() != '\\' && uint8_t(peek()) >= 0x20) {
hasNonAscii |= uint8_t(peek()) >= 0x80;
++_pos;
}
res.append(_s.data() + start, _pos - start);
if (atEnd()) {
return fail(JsonParseError::UnexpectedEnd, "unterminated string");
}
char c = peek();
if (c == '"') {
++_pos;
break;
}
if (uint8_t(c) < 0x20) {
return fail(JsonParseError::IllegalString, "control character in string");
}
++_pos;
if (atEnd()) {
return fail(JsonParseError::UnexpectedEnd, "unterminated escape");
}
char e = peek();
++_pos;
switch (e) {
case '"':
res += '"';
break;
case '\\':
res += '\\';
break;
case '/':
res += '/';
break;
case 'b':
res += '\b';
break;
case 'f':
res += '\f';
break;
case 'n':
res += '\n';
break;
case 'r':
res += '\r';
break;
case 't':
res += '\t';
break;
case 'u': {
char32_t cp;
if (!hex4(&cp)) {
return fail(JsonParseError::IllegalEscape,
"expected four hexadecimal digits");
}
if (cp >= 0xD800 && cp <= 0xDBFF) {
// A high surrogate means nothing without the low one after it.
if (_pos + 1 >= _s.size() || _s[_pos] != '\\' || _s[_pos + 1] != 'u') {
return fail(JsonParseError::IllegalEscape,
"expected a low surrogate");
}
_pos += 2;
char32_t low;
if (!hex4(&low)) {
return fail(JsonParseError::IllegalEscape,
"expected four hexadecimal digits");
}
if (low < 0xDC00 || low > 0xDFFF) {
return fail(JsonParseError::IllegalEscape,
"expected a low surrogate");
}
cp = 0x10000 + ((cp - 0xD800) << 10) + (low - 0xDC00);
} else if (cp >= 0xDC00 && cp <= 0xDFFF) {
return fail(JsonParseError::IllegalEscape, "unpaired low surrogate");
}
appendUtf8(res, cp);
break;
}
default:
return fail(JsonParseError::IllegalEscape, "unknown escape");
}
}
// ASCII was checked for controls above and is UTF-8 already. Only strings containing
// raw high bytes need the full validator. A \u escape was validated as it was
// decoded.
if (hasNonAscii && !stdc::utf::is_valid_utf8(res)) {
return fail(JsonParseError::IllegalString, "string is not valid UTF-8");
}
*out = std::move(res);
return true;
}
bool parseNumber(JsonValue *out) {
size_t start = _pos;
if (!atEnd() && peek() == '-') {
++_pos;
}
size_t digitsStart = _pos;
while (!atEnd() && peek() >= '0' && peek() <= '9') {
++_pos;
}
if (_pos == digitsStart) {
return fail(JsonParseError::UnexpectedToken, "expected a value");
}
// A leading zero is not one number, it is two written together.
if (_s[digitsStart] == '0' && _pos - digitsStart > 1) {
_pos = digitsStart;
return fail(JsonParseError::IllegalNumber, "number has a leading zero");
}
bool isDouble = false;
if (!atEnd() && peek() == '.') {
isDouble = true;
++_pos;
size_t fracStart = _pos;
while (!atEnd() && peek() >= '0' && peek() <= '9') {
++_pos;
}
if (_pos == fracStart) {
return fail(JsonParseError::IllegalNumber,
"expected a digit after the decimal point");
}
}
if (!atEnd() && (peek() == 'e' || peek() == 'E')) {
isDouble = true;
++_pos;
if (!atEnd() && (peek() == '+' || peek() == '-')) {
++_pos;
}
size_t expStart = _pos;
while (!atEnd() && peek() >= '0' && peek() <= '9') {
++_pos;
}
if (_pos == expStart) {
return fail(JsonParseError::IllegalNumber, "expected a digit in the exponent");
}
}
const char *first = _s.data() + start;
const char *last = _s.data() + _pos;
if (!isDouble) {
// The written form decides the type. An integer too large for its type becomes a
// double rather than a parse error, which is what every other reader does.
if (_s[start] == '-') {
int64_t v;
if (std::from_chars(first, last, v).ec == std::errc()) {
*out = JsonValue(v);
return true;
}
} else {
uint64_t v;
if (std::from_chars(first, last, v).ec == std::errc()) {
*out = JsonValue(v);
return true;
}
}
}
// Floating-point from_chars is not everywhere yet. Where it is available, it avoids
// copying every number merely to give strtod a terminator.
#if defined(__cpp_lib_to_chars) && __cpp_lib_to_chars >= 201611L
double parsed;
const auto result = std::from_chars(first, last, parsed, std::chars_format::general);
if (result.ec == std::errc() && result.ptr == last) {
*out = JsonValue(parsed);
return true;
}
#endif
vlarray<char, 64> text(first, last);
text.push_back('\0');
char *end = nullptr;
double d = std::strtod(text.data(), &end);
if (end != text.data() + text.size() - 1) {
return fail(JsonParseError::IllegalNumber, "malformed number");
}
*out = JsonValue(d);
return true;
}
std::string_view _s;
size_t _pos = 0;
bool _comments;
JsonParseError _error;
};
// ------------------------------------------------------------------------------------------
// CBOR
// ------------------------------------------------------------------------------------------
namespace cbor {
void putHead(std::vector<uint8_t> &out, uint8_t major, uint64_t arg) {
auto m = uint8_t(major << 5);
if (arg < 24) {
out.push_back(uint8_t(m | arg));
} else if (arg <= 0xFF) {
out.push_back(uint8_t(m | 24));
out.push_back(uint8_t(arg));
} else if (arg <= 0xFFFF) {
out.push_back(uint8_t(m | 25));
out.push_back(uint8_t(arg >> 8));
out.push_back(uint8_t(arg));
} else if (arg <= 0xFFFFFFFF) {
out.push_back(uint8_t(m | 26));
for (int shift = 24; shift >= 0; shift -= 8) {
out.push_back(uint8_t(arg >> shift));
}
} else {
out.push_back(uint8_t(m | 27));
for (int shift = 56; shift >= 0; shift -= 8) {
out.push_back(uint8_t(arg >> shift));
}
}
}
void putBytes(std::vector<uint8_t> &out, uint8_t major, std::string_view s) {
putHead(out, major, s.size());
out.insert(out.end(), s.begin(), s.end());
}
void encode(std::vector<uint8_t> &out, const JsonValue &v) {
switch (v.type()) {
case JsonValue::Null:
out.push_back(0xF6);
return;
case JsonValue::Bool:
out.push_back(v.toBool() ? 0xF5 : 0xF4);
return;
case JsonValue::Int: {
auto i = v.toInt();
if (i >= 0) {
putHead(out, 0, uint64_t(i));
} else {
// Major type 1 stores minus one minus the value, which is how the most
// negative integer encodes without needing a wider type than it has.
putHead(out, 1, uint64_t(-(i + 1)));
}
return;
}
case JsonValue::Double: {
out.push_back(0xFB);
double d = v.toDouble();
uint64_t bits;
std::memcpy(&bits, &d, sizeof(bits));
for (int shift = 56; shift >= 0; shift -= 8) {
out.push_back(uint8_t(bits >> shift));
}
return;
}
case JsonValue::String:
putBytes(out, 3, v.toStringView());
return;
case JsonValue::Binary: {
const auto &b = v.toBinary();
putHead(out, 2, b.size());
out.insert(out.end(), b.begin(), b.end());
return;
}
case JsonValue::Array: {
const auto &arr = v.toArray();
putHead(out, 4, arr.size());
for (const auto &item : arr) {
encode(out, item);
}
return;
}
case JsonValue::Object: {
const auto &obj = v.toObject();
putHead(out, 5, obj.size());
for (const auto &item : obj) {
putBytes(out, 3, item.first);
encode(out, item.second);
}
return;
}
}
}
/// \note Tags are rejected rather than handled. Nothing writes them here, and accepting a
/// shape we never produce is surface with no reader. Indefinite lengths are a
/// different matter: we never write one, but other encoders do, and a decoder that
/// cannot read them cannot read their output.
class Decoder {
public:
static constexpr int maxDepth = Parser::maxDepth;
/// The initial byte that ends an indefinite-length string, array or map.
static constexpr uint8_t breakByte = 0xFF;
explicit Decoder(stdc::array_view<uint8_t> data) : _d(data) {
}
bool decode(JsonValue *out) {
if (!decodeValue(out, 0)) {
return false;
}
if (_pos != _d.size()) {
return fail(CborDecodeError::TrailingContent, "trailing bytes after the value");
}
return true;
}
const CborDecodeError &error() const {
return _error;
}
private:
bool fail(CborDecodeError::Code code, const char *what) {
if (!_error) {
_error.code = code;
_error.offset = _pos;
_error.what = what;
}
return false;
}
bool take(uint8_t *out) {
if (_pos >= _d.size()) {
return fail(CborDecodeError::UnexpectedEnd, "input ended early");
}
*out = _d[_pos++];
return true;
}
bool takeBig(int bytes, uint64_t *out) {
if (_pos + size_t(bytes) > _d.size()) {
return fail(CborDecodeError::UnexpectedEnd, "input ended early");
}
uint64_t v = 0;
for (int i = 0; i < bytes; ++i) {
v = (v << 8) | _d[_pos++];
}
*out = v;
return true;
}
/// Reads the argument that follows an initial byte.
///
/// \param indefinite Where to report minor 31, which stands for a length that is not
/// given up front. Only the string, array and map types may carry one, so
/// passing null is how the rest reject it.
bool argument(uint8_t initial, uint64_t *out, bool *indefinite = nullptr) {
if (indefinite) {
*indefinite = false;
}
uint8_t minor = initial & 0x1F;
if (minor < 24) {
*out = minor;
return true;
}
switch (minor) {
case 24:
return takeBig(1, out);
case 25:
return takeBig(2, out);
case 26:
return takeBig(4, out);
case 27:
return takeBig(8, out);
case 31:
if (!indefinite) {
return fail(CborDecodeError::IllegalEncoding,
"this type cannot have an indefinite length");
}
*indefinite = true;
return true;
default:
return fail(CborDecodeError::IllegalEncoding, "reserved length encoding");
}
}
// Text arrives as a std::string and a byte string as a std::vector<uint8_t>, which is
// what each of them ends up stored as. Reading into the other one first would cost a
// copy of the whole string to convert.
template <class Bytes>
bool rawBytes(uint64_t count, Bytes *out) {
if (count > _d.size() - _pos) {
return fail(CborDecodeError::UnexpectedEnd, "input ended early");
}
const auto *first =
reinterpret_cast<const typename Bytes::value_type *>(_d.data() + _pos);
out->assign(first, first + size_t(count));
_pos += size_t(count);
return true;
}
/// Whether the next byte ends an indefinite-length item, consuming it if so.
bool atBreak(bool *broke) {
if (_pos >= _d.size()) {
return fail(CborDecodeError::UnexpectedEnd, "input ended before the break");
}
*broke = _d[_pos] == breakByte;
if (*broke) {
++_pos;
}
return true;
}
/// Reads the pieces of an indefinite-length string up to the break and joins them.
///
/// Each piece is a definite-length string of the same major type, and a text piece has
/// to be well formed on its own -- a split through the middle of a code point is not
/// something the concatenation would show.
template <class Bytes>
bool chunkedBytes(uint8_t major, Bytes *out) {
for (;;) {
bool broke = false;
if (!atBreak(&broke)) {
return false;
}
if (broke) {
return true;
}
uint8_t initial;
if (!take(&initial)) {
return false;
}
if (uint8_t(initial >> 5) != major) {
return fail(CborDecodeError::IllegalEncoding,
"an indefinite-length string is made of strings of its own "
"kind");
}
if ((initial & 0x1F) == 31) {
return fail(CborDecodeError::IllegalEncoding,
"a piece of an indefinite-length string has to have a length");
}
uint64_t count = 0;
if (!argument(initial, &count)) {
return false;
}
Bytes chunk;
if (!rawBytes(count, &chunk)) {
return false;
}
if (major == 3 &&
!stdc::utf::is_valid_utf8(std::string_view(
reinterpret_cast<const char *>(chunk.data()), chunk.size()))) {
return fail(CborDecodeError::IllegalString,
"text string is not valid UTF-8");
}
out->insert(out->end(), chunk.begin(), chunk.end());
}
}
bool decodeValue(JsonValue *out, int depth) {
if (depth > maxDepth) {
return fail(CborDecodeError::NestedTooDeeply, "nested too deeply");
}
uint8_t initial;
if (!take(&initial)) {
return false;
}
auto major = uint8_t(initial >> 5);
uint64_t arg = 0;
switch (major) {
case 0:
if (!argument(initial, &arg)) {
return false;
}
*out = JsonValue(arg);
return true;
case 1: {
if (!argument(initial, &arg)) {
return false;
}
if (arg > uint64_t(INT64_MAX)) {
return fail(CborDecodeError::OutOfRange,
"negative integer is out of range");
}
*out = JsonValue(-int64_t(arg) - 1);
return true;
}
case 2: {
bool indefinite = false;
if (!argument(initial, &arg, &indefinite)) {
return false;
}