forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgpack.hh
692 lines (653 loc) · 19.4 KB
/
msgpack.hh
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
#ifndef GSTORE_MSGPACK_HH
#define GSTORE_MSGPACK_HH
#include "json.hh"
#include "small_vector.hh"
#include "straccum.hh"
#include <vector>
namespace msgpack {
using lcdf::Json;
using lcdf::Str;
using lcdf::String;
using lcdf::StringAccum;
namespace format {
enum {
ffixuint = 0x00, nfixuint = 0x80,
ffixmap = 0x80, nfixmap = 0x10,
ffixarray = 0x90, nfixarray = 0x10,
ffixstr = 0xA0, nfixstr = 0x20,
fnull = 0xC0,
ffalse = 0xC2, ftrue = 0xC3,
fbin8 = 0xC4, fbin16 = 0xC5, fbin32 = 0xC6,
fext8 = 0xC7, fext16 = 0xC8, fext32 = 0xC9,
ffloat32 = 0xCA, ffloat64 = 0xCB,
fuint8 = 0xCC, fuint16 = 0xCD, fuint32 = 0xCE, fuint64 = 0xCF,
fint8 = 0xD0, fint16 = 0xD1, fint32 = 0xD2, fint64 = 0xD3,
ffixext1 = 0xD4, ffixext2 = 0xD5, ffixext4 = 0xD6,
ffixext8 = 0xD7, ffixext16 = 0xD8,
fstr8 = 0xD9, fstr16 = 0xDA, fstr32 = 0xDB,
farray16 = 0xDC, farray32 = 0xDD,
fmap16 = 0xDE, fmap32 = 0xDF,
ffixnegint = 0xE0, nfixnegint = 0x20,
nfixint = nfixuint + nfixnegint
};
inline bool in_range(uint8_t x, unsigned low, unsigned n) {
return (unsigned) x - low < n;
}
inline bool in_wrapped_range(uint8_t x, unsigned low, unsigned n) {
return (unsigned) (int8_t) x - low < n;
}
inline bool is_fixint(uint8_t x) {
return in_wrapped_range(x, -nfixnegint, nfixint);
}
inline bool is_null_or_bool(uint8_t x) {
return in_range(x, fnull, 4);
}
inline bool is_bool(uint8_t x) {
return in_range(x, ffalse, 2);
}
inline bool is_fixstr(uint8_t x) {
return in_range(x, ffixstr, nfixstr);
}
inline bool is_fixarray(uint8_t x) {
return in_range(x, ffixarray, nfixarray);
}
inline bool is_fixmap(uint8_t x) {
return in_range(x, ffixmap, nfixmap);
}
inline char* write_null(char* s) {
*s++ = fnull;
return s;
}
inline char* write_bool(char* s, bool x) {
*s++ = ffalse + x;
return s;
}
template <size_t s> struct sized_writer {};
template <> struct sized_writer<4> {
static inline char* write_unsigned(char* s, uint32_t x) {
if (x < nfixuint)
*s++ = x;
else if (x < 256) {
*s++ = fuint8;
*s++ = x;
} else if (x < 65536) {
*s++ = fuint16;
s = write_in_net_order<uint16_t>(s, (uint16_t) x);
} else {
*s++ = fuint32;
s = write_in_net_order<uint32_t>(s, x);
}
return s;
}
static inline char* write_signed(char* s, int32_t x) {
if ((uint32_t) x + nfixnegint < nfixint)
*s++ = x;
else if ((uint32_t) x + 128 < 256) {
*s++ = fint8;
*s++ = x;
} else if ((uint32_t) x + 32768 < 65536) {
*s++ = fint16;
s = write_in_net_order<int16_t>(s, (int16_t) x);
} else {
*s++ = fint32;
s = write_in_net_order<int32_t>(s, x);
}
return s;
}
};
template <> struct sized_writer<8> {
static inline char* write_unsigned(char* s, uint64_t x) {
if (x < 4294967296ULL)
return sized_writer<4>::write_unsigned(s, (uint32_t) x);
else {
*s++ = fuint64;
return write_in_net_order<uint64_t>(s, x);
}
}
static inline char* write_signed(char* s, int64_t x) {
if (x + 2147483648ULL < 4294967296ULL)
return sized_writer<4>::write_signed(s, (int32_t) x);
else {
*s++ = fint64;
return write_in_net_order<int64_t>(s, x);
}
}
};
inline char* write_int(char* s, int x) {
return sized_writer<sizeof(x)>::write_signed(s, x);
}
inline char* write_int(char* s, unsigned x) {
return sized_writer<sizeof(x)>::write_unsigned(s, x);
}
inline char* write_int(char* s, long x) {
return sized_writer<sizeof(x)>::write_signed(s, x);
}
inline char* write_int(char* s, unsigned long x) {
return sized_writer<sizeof(x)>::write_unsigned(s, x);
}
#if HAVE_LONG_LONG && SIZEOF_LONG_LONG <= 8
inline char* write_int(char* s, long long x) {
return sized_writer<sizeof(x)>::write_signed(s, x);
}
inline char* write_int(char* s, unsigned long long x) {
return sized_writer<sizeof(x)>::write_unsigned(s, x);
}
#endif
inline char* write_wide_int64(char* s, uint64_t x) {
*s++ = fuint64;
return write_in_net_order<uint64_t>(s, x);
}
inline char* write_wide_int64(char* s, int64_t x) {
*s++ = fint64;
return write_in_net_order<int64_t>(s, x);
}
inline char* write_float(char* s, float x) {
*s++ = ffloat32;
return write_in_net_order<float>(s, x);
}
inline char* write_double(char* s, double x) {
*s++ = ffloat64;
return write_in_net_order<double>(s, x);
}
inline char* write_string(char* s, const char *data, int len) {
if (len < nfixstr)
*s++ = 0xA0 + len;
else if (len < 256) {
*s++ = fstr8;
*s++ = len;
} else if (len < 65536) {
*s++ = fstr16;
s = write_in_net_order<uint16_t>(s, (uint16_t) len);
} else {
*s++ = fstr32;
s = write_in_net_order<uint32_t>(s, len);
}
memcpy(s, data, len);
return s + len;
}
inline char* write_string(char* s, Str x) {
return write_string(s, x.data(), x.length());
}
template <typename T>
inline char* write_string(char* s, const lcdf::String_base<T>& x) {
return write_string(s, x.data(), x.length());
}
inline char* write_array_header(char* s, uint32_t size) {
if (size < nfixarray) {
*s++ = ffixarray + size;
return s;
} else if (size < 65536) {
*s++ = farray16;
return write_in_net_order<uint16_t>(s, (uint16_t) size);
} else {
*s++ = farray32;
return write_in_net_order<uint32_t>(s, (uint32_t) size);
}
}
inline char* write_map_header(char* s, uint32_t size) {
if (size < nfixmap) {
*s++ = ffixmap + size;
return s;
} else if (size < 65536) {
*s++ = fmap16;
return write_in_net_order<uint16_t>(s, (uint16_t) size);
} else {
*s++ = fmap32;
return write_in_net_order<uint32_t>(s, (uint32_t) size);
}
}
} // namespace format
struct array_t {
uint32_t size;
array_t(uint32_t s)
: size(s) {
}
};
inline array_t array(uint32_t size) {
return array_t(size);
}
struct object_t {
uint32_t size;
object_t(uint32_t s)
: size(s) {
}
};
inline object_t object(uint32_t size) {
return object_t(size);
}
template <typename T>
class unparser {
public:
inline unparser(T& base)
: base_(base) {
}
template <typename X>
inline unparser(T& base, const X& x)
: base_(base) {
*this << x;
}
inline void clear() {
base_.clear();
}
inline unparser<T>& null() {
base_.append((char) format::fnull);
return *this;
}
inline unparser<T>& operator<<(const Json::null_t&) {
return null();
}
inline unparser<T>& operator<<(int x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_int(s, x));
return *this;
}
inline unparser<T>& operator<<(unsigned x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_int(s, x));
return *this;
}
inline unparser<T>& operator<<(long x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_int(s, x));
return *this;
}
inline unparser<T>& operator<<(unsigned long x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_int(s, x));
return *this;
}
#if HAVE_LONG_LONG && SIZEOF_LONG_LONG <= 8
inline unparser<T>& operator<<(long long x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_int(s, x));
return *this;
}
inline unparser<T>& operator<<(unsigned long long x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_int(s, x));
return *this;
}
#endif
inline unparser<T>& write_wide(int64_t x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_wide_int64(s, x));
return *this;
}
inline unparser<T>& write_wide(uint64_t x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_wide_int64(s, x));
return *this;
}
inline unparser<T>& operator<<(float x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_float(s, x));
return *this;
}
inline unparser<T>& operator<<(double x) {
char* s = base_.reserve(sizeof(x) + 1);
base_.set_end(format::write_double(s, x));
return *this;
}
inline unparser<T>& operator<<(Str x) {
char* s = base_.reserve(5 + x.length());
base_.set_end(format::write_string(s, x.data(), x.length()));
return *this;
}
template <typename X>
inline unparser<T>& operator<<(const lcdf::String_base<X>& x) {
char* s = base_.reserve(5 + x.length());
base_.set_end(format::write_string(s, x.data(), x.length()));
return *this;
}
inline unparser<T>& operator<<(array_t x) {
char* s = base_.reserve(5);
base_.set_end(format::write_array_header(s, x.size));
return *this;
}
inline unparser<T>& write_array_header(uint32_t size) {
char* s = base_.reserve(5);
base_.set_end(format::write_array_header(s, size));
return *this;
}
inline unparser<T>& operator<<(object_t x) {
char* s = base_.reserve(5);
base_.set_end(format::write_map_header(s, x.size));
return *this;
}
unparser<T>& operator<<(const Json& j);
template <typename X>
inline unparser<T>& write(const X& x) {
return *this << x;
}
private:
T& base_;
};
class streaming_parser {
public:
inline streaming_parser();
inline void reset();
inline bool empty() const;
inline bool done() const;
inline bool success() const;
inline bool error() const;
inline size_t consume(const char* first, size_t length,
const String& str = String());
inline const char* consume(const char* first, const char* last,
const String& str = String());
const uint8_t* consume(const uint8_t* first, const uint8_t* last,
const String& str = String());
inline Json& result();
inline const Json& result() const;
private:
enum {
st_final = -2, st_error = -1, st_normal = 0, st_partial = 1,
st_string = 2
};
struct selem {
Json* jp;
int size;
};
int state_;
small_vector<selem, 2> stack_;
String str_;
Json json_;
Json jokey_;
};
class parser {
public:
explicit inline parser(const char* s)
: s_(reinterpret_cast<const unsigned char*>(s)), str_() {
}
explicit inline parser(const unsigned char* s)
: s_(s), str_() {
}
explicit inline parser(const String& str)
: s_(reinterpret_cast<const uint8_t*>(str.begin())), str_(str) {
}
inline const char* position() const {
return reinterpret_cast<const char*>(s_);
}
inline bool try_read_null() {
if (*s_ == format::fnull) {
++s_;
return true;
} else
return false;
}
inline int read_tiny_int() {
assert(format::is_fixint(*s_));
return (int8_t) *s_++;
}
inline parser& read_tiny_int(int& x) {
x = read_tiny_int();
return *this;
}
template <typename T>
inline parser& read_int(T& x) {
if (format::is_fixint(*s_)) {
x = (int8_t) *s_;
++s_;
} else {
assert((uint32_t) *s_ - format::fuint8 < 8);
hard_read_int(x);
}
return *this;
}
inline parser& operator>>(int& x) {
return read_int(x);
}
inline parser& operator>>(long& x) {
return read_int(x);
}
inline parser& operator>>(long long& x) {
return read_int(x);
}
inline parser& operator>>(unsigned& x) {
return read_int(x);
}
inline parser& operator>>(unsigned long& x) {
return read_int(x);
}
inline parser& operator>>(unsigned long long& x) {
return read_int(x);
}
inline parser& operator>>(bool& x) {
assert(format::is_bool(*s_));
x = *s_ - format::ffalse;
++s_;
return *this;
}
inline parser& operator>>(double& x) {
assert(*s_ == format::ffloat64);
x = read_in_net_order<double>(s_ + 1);
s_ += 9;
return *this;
}
parser& operator>>(Str& x);
parser& operator>>(String& x);
inline parser& read_array_header(unsigned& size) {
if (format::is_fixarray(*s_)) {
size = *s_ - format::ffixarray;
s_ += 1;
} else if (*s_ == format::farray16) {
size = read_in_net_order<uint16_t>(s_ + 1);
s_ += 3;
} else {
assert(*s_ == format::farray32);
size = read_in_net_order<uint32_t>(s_ + 1);
s_ += 5;
}
return *this;
}
template <typename T> parser& operator>>(::std::vector<T>& x);
inline parser& operator>>(Json& j);
inline parser& skip_primitives(unsigned n) {
for (; n != 0; --n) {
if (format::is_fixint(*s_) || format::is_null_or_bool(*s_))
s_ += 1;
else if (format::is_fixstr(*s_))
s_ += 1 + (*s_ - format::ffixstr);
else if (*s_ == format::fuint8 || *s_ == format::fint8)
s_ += 2;
else if (*s_ == format::fuint16 || *s_ == format::fint16)
s_ += 3;
else if (*s_ == format::fuint32 || *s_ == format::fint32
|| *s_ == format::ffloat32)
s_ += 5;
else if (*s_ == format::fstr8 || *s_ == format::fbin8)
s_ += 2 + s_[1];
else if (*s_ == format::fstr16 || *s_ == format::fbin16)
s_ += 3 + read_in_net_order<uint16_t>(s_ + 1);
else if (*s_ == format::fstr32 || *s_ == format::fbin32)
s_ += 5 + read_in_net_order<uint32_t>(s_ + 1);
}
return *this;
}
inline parser& skip_primitive() {
return skip_primitives(1);
}
inline parser& skip_array_size() {
if (format::is_fixarray(*s_))
s_ += 1;
else if (*s_ == format::farray16)
s_ += 3;
else {
assert(*s_ == format::farray32);
s_ += 5;
}
return *this;
}
private:
const uint8_t* s_;
String str_;
template <typename T> void hard_read_int(T& x);
};
template <typename T>
void parser::hard_read_int(T& x) {
switch (*s_) {
case format::fuint8:
x = s_[1];
s_ += 2;
break;
case format::fuint16:
x = read_in_net_order<uint16_t>(s_ + 1);
s_ += 3;
break;
case format::fuint32:
x = read_in_net_order<uint32_t>(s_ + 1);
s_ += 5;
break;
case format::fuint64:
x = read_in_net_order<uint64_t>(s_ + 1);
s_ += 9;
break;
case format::fint8:
x = (int8_t) s_[1];
s_ += 2;
break;
case format::fint16:
x = read_in_net_order<int16_t>(s_ + 1);
s_ += 3;
break;
case format::fint32:
x = read_in_net_order<int32_t>(s_ + 1);
s_ += 5;
break;
case format::fint64:
x = read_in_net_order<int64_t>(s_ + 1);
s_ += 9;
break;
}
}
template <typename T>
unparser<T>& unparser<T>::operator<<(const Json& j) {
if (j.is_null())
base_.append(char(format::fnull));
else if (j.is_b())
base_.append(char(format::ffalse + j.as_b()));
else if (j.is_u()) {
char* x = base_.reserve(9);
base_.set_end(format::write_int(x, j.as_u()));
} else if (j.is_i()) {
char* x = base_.reserve(9);
base_.set_end(format::write_int(x, j.as_i()));
} else if (j.is_d()) {
char* x = base_.reserve(9);
base_.set_end(format::write_double(x, j.as_d()));
} else if (j.is_s()) {
char* x = base_.reserve(j.as_s().length() + 5);
base_.set_end(format::write_string(x, j.as_s()));
} else if (j.is_a()) {
char* x = base_.reserve(5);
base_.set_end(format::write_array_header(x, j.size()));
for (auto it = j.cabegin(); it != j.caend(); ++it)
*this << *it;
} else if (j.is_o()) {
char* x = base_.reserve(5);
base_.set_end(format::write_map_header(x, j.size()));
for (auto it = j.cobegin(); it != j.coend(); ++it) {
char* x = base_.reserve(it.key().length() + 5);
base_.set_end(format::write_string(x, it.key()));
*this << it.value();
}
} else
base_.append(char(format::fnull));
return *this;
}
inline String unparse(const Json& j) {
StringAccum sa;
unparser<StringAccum>(sa, j);
return sa.take_string();
}
template <typename T, typename X>
inline T& unparse(T& s, const X& x) {
unparser<T>(s) << x;
return s;
}
template <typename T, typename X>
inline T& unparse_wide(T& s, const X& x) {
unparser<T>(s).write_wide(x);
return s;
}
template <typename T>
parser& parser::operator>>(::std::vector<T>& x) {
uint32_t sz;
if ((uint32_t) *s_ - format::ffixarray < format::nfixarray) {
sz = *s_ - format::ffixarray;
++s_;
} else if (*s_ == format::farray16) {
sz = read_in_net_order<uint16_t>(s_ + 1);
s_ += 3;
} else {
assert(*s_ == format::farray32);
sz = read_in_net_order<uint32_t>(s_ + 1);
s_ += 5;
}
for (; sz != 0; --sz) {
x.push_back(T());
parse(x.back());
}
return *this;
}
inline streaming_parser::streaming_parser()
: state_(st_normal) {
}
inline void streaming_parser::reset() {
state_ = st_normal;
stack_.clear();
}
inline bool streaming_parser::empty() const {
return state_ == st_normal && stack_.empty();
}
inline bool streaming_parser::done() const {
return state_ < 0;
}
inline bool streaming_parser::success() const {
return state_ == st_final;
}
inline bool streaming_parser::error() const {
return state_ == st_error;
}
inline const char* streaming_parser::consume(const char* first,
const char* last,
const String& str) {
return reinterpret_cast<const char*>
(consume(reinterpret_cast<const uint8_t*>(first),
reinterpret_cast<const uint8_t*>(last), str));
}
inline size_t streaming_parser::consume(const char* first, size_t length,
const String& str) {
const uint8_t* ufirst = reinterpret_cast<const uint8_t*>(first);
return consume(ufirst, ufirst + length, str) - ufirst;
}
inline Json& streaming_parser::result() {
return json_;
}
inline const Json& streaming_parser::result() const {
return json_;
}
inline parser& parser::operator>>(Json& j) {
using std::swap;
streaming_parser sp;
while (!sp.done())
s_ = sp.consume(s_, s_ + 4096, str_);
if (sp.success())
swap(j, sp.result());
return *this;
}
inline Json parse(const char* first, const char* last) {
streaming_parser sp;
first = sp.consume(first, last, String());
if (sp.success())
return sp.result();
return Json();
}
inline Json parse(const String& str) {
streaming_parser sp;
sp.consume(str.begin(), str.end(), str);
if (sp.success())
return sp.result();
return Json();
}
} // namespace msgpack
#endif