-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.patch
962 lines (908 loc) · 33.9 KB
/
debug.patch
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
diff --git a/msgpack.nvgt b/msgpack.nvgt
index 9189160..c0f6e18 100644
--- a/msgpack.nvgt
+++ b/msgpack.nvgt
@@ -278,6 +278,7 @@ class mp_value {
private any@ content;
mp_value(int type, int format, any@ content) {
+ dbgout("explicit value constructor");
this.init(type, format, content);
}
@@ -288,6 +289,7 @@ class mp_value {
this.itype = type;
this.iformat = format;
@this.content = @content;
+ dbgout("created value " + debug_enum(MPI_TYPE, type) + "/" + debug_enum(MPI_FORMAT, format));
}
// you can't get nil
@@ -670,6 +672,7 @@ class mp_value {
// upcast to type of same sign and check if positive or negative, then decide format based on ranges
// once format is decided, squish value to conform to it
private void store_positive_int(uint64 v) {
+ dbgout("value: storing positive int " + v);
any a;
int format = 0;
if (v <= 0x7f) {
@@ -696,6 +699,7 @@ class mp_value {
}
private void store_negative_int(int64 v) {
+ dbgout("value: storing negative int " + v);
any a;
int format = 0;
if (v >= -32) {
@@ -724,14 +728,17 @@ class mp_value {
// the rest of the logic can take place in constructors, as it wouldn't otherwise be duplicated
mp_value() {
// empty constructor, perhaps useful way to store nil?
+ dbgout("empty value constructor");
this.init(MPT_NIL, MPF_NIL, null);
}
mp_value(bool v) {
+ dbgout("bool value constructor");
this.init(MPT_BOOLEAN, (v ? MPF_TRUE : MPF_FALSE), any(v));
}
mp_value(float v) {
+ dbgout("float value constructor");
this.init(MPT_FLOAT, MPF_FLOAT32, any(v));
}
@@ -740,14 +747,17 @@ class mp_value {
// if so, we're saving 4 bytes
float vf = v;
if (v == vf) {
+ dbgout("double value constructor as float");
this.init(MPT_FLOAT, MPF_FLOAT32, any(vf));
} else {
+ dbgout("double value constructor");
this.init(MPT_FLOAT, MPF_FLOAT64, any(v));
}
}
// store strings as either text or bin, depending on the value of is_bin
mp_value(const string &in v, bool is_bin = false) {
+ dbgout((is_bin ? "bin" : "string") + " value constructor, string is length " + v.length());
if (v.length() > 0xFFFFFFFF) throw(MP_LARGE_VALUE_EXCEPTION);
int format = 0;
@@ -769,6 +779,7 @@ class mp_value {
mp_value(mp_value@[] & v) {
// ref not handle so can't be null
+ dbgout("array value constructor, size " + v.length());
if (v.length() > 0xFFFFFFFF) throw(MP_LARGE_VALUE_EXCEPTION);
int format = 0;
@@ -782,6 +793,7 @@ class mp_value {
mp_value(mp_map& v) {
// ditto with ref
// don't have to check size because map return type is uint, so even if it were too big it can't tell us
+ dbgout("map value constructor, size " + v.get_size());
int format = 0;
uint len = v.get_size();
if (len <= 0x0F) format = MPF_FIXMAP;
@@ -791,6 +803,7 @@ class mp_value {
}
mp_value(mp_ext& v) {
+ dbgout("ext value constructor with ext type " + v.type + " and length " + v.data.length());
if (v.data.length() >= 0xFFFFFFFF) throw(MP_LARGE_VALUE_EXCEPTION);
int format = 0;
@@ -808,37 +821,45 @@ class mp_value {
// now for the constructors for all the integer types that pass off to the ranging functions
mp_value(uint8 v) {
+ dbgout("uint8 value constructor");
store_positive_int(v);
}
mp_value(uint16 v) {
+ dbgout("uint16 value constructor");
store_positive_int(v);
}
mp_value(uint v) {
+ dbgout("uint value constructor");
store_positive_int(v);
}
mp_value(uint64 v) {
+ dbgout("uint64 value constructor");
store_positive_int(v);
}
mp_value(int8 v) {
+ dbgout("int8 value constructor");
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
mp_value(int16 v) {
+ dbgout("int16 value constructor");
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
mp_value(int v) {
+ dbgout("int value constructor");
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
mp_value(int64 v) {
+ dbgout("int64 value constructor");
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
@@ -1029,21 +1050,25 @@ class mp_timestamp {
datastream ds(data, "", STREAM_BYTE_ORDER_NETWORK);
if (data.length() == 4) {
// timestamp 32, seconds since epoch as an unsigned int
+ dbgout("loading 4-byte timestamp");
uint sec = ds.read_uint();
this.secs = sec; // unsigned to signed conversion but signed type is bigger so we're safe
this.nsecs = 0;
} else if (data.length() == 8) {
// seconds and nanoseconds in 32-bit unsigned ints, except slight bitfields because nanoseconds is only 30 bits
+ dbgout("loading 8-byte timestamp");
uint64 d = ds.read_uint64();
this.nsecs = d >> 34;
this.secs = d & 0x00000003ffffffff;
} else if (data.length() == 12) {
// nanoseconds in 32-bit unsigned int, seconds in 64-bit signed int
+ dbgout("loading 12-byte timestamp");
uint nsec = ds.read_uint();
int64 sec = ds.read_int64();
this.nsecs = nsec;
this.secs = sec;
} else {
+ dbgout("timestamp is not one of the expected lengths");
ds.close();
throw("msgpack invalid timestamp format");
}
@@ -1073,14 +1098,17 @@ class mp_timestamp {
uint64 data64 = (int64(this.nsecs) << 34) | this.secs;
if (data64 & 0xffffffff00000000 == 0) {
// timestamp 32
+ dbgout("serializing timestamp in 4 bytes");
uint data32 = data64 & 0xffffffff;
ds.write_uint(data32);
} else {
// timestamp 64
+ dbgout("serializing timestamp in 8 bytes");
ds.write_uint64(data64);
}
} else {
// timestamp 96
+ dbgout("serializing timestamp in 12 bytes");
ds.write_uint(this.nsecs);
ds.write_int64(this.secs);
}
@@ -1609,6 +1637,7 @@ class mp_decoder {
stream.rseek(0); // the docs are in fact wrong, this started at the end too!
this.state = MPDS_READY;
this.fixed_length = fixed_length;
+ dbgout("Decoder created, initial data is " + initial.length() + " bytes" + (fixed_length ? " and will not grow" : ""));
}
// this constructor is to be called for sub-decoders, passing some values so stream is synchronized
@@ -1629,6 +1658,7 @@ class mp_decoder {
temp.reserve(items_to_read);
@cur_array = temp;
}
+ dbgout("subdecoder created in " + (is_map ? "map" : "array") + " mode, " + items_to_read + " items to read");
}
~mp_decoder() {
@@ -1651,6 +1681,7 @@ class mp_decoder {
// if you use one decoder to handle many separate streams, it's useful to call this between each instance, otherwise the internal stream will buffer all data fed previously!
void reset(const string &in initial = "", bool fixed_length = false) {
if (sub_decoder !is null) {
+ dbgout("has a sub-decoder, resetting");
sub_decoder.reset(); // recursively reset, we can knock off an entire chain from the top this way
@sub_decoder = null;
}
@@ -1664,12 +1695,14 @@ class mp_decoder {
this.state = MPDS_READY;
this.prev_state = MPDS_MORE_DATA;
this.fixed_length = fixed_length;
+ dbgout("Decoder reset, initial data is " + initial.length() + " bytes" + (fixed_length ? " and will not grow" : ""));
} else {
@this.stream = null;
if (this.is_map) {
@this.cur_map = null;
@this.cur_key = null;
} else @this.cur_array = null;
+ dbgout("sub-decoder reset");
}
}
@@ -1683,10 +1716,12 @@ class mp_decoder {
stream.write(data);
stream.rseek(stream.rpos); // hack to make available report the true number of bytes available after pushing
bool s = this.check_more_data_resolved();
+ dbgout(data.length() + " bytes pushed, " + (s ? "ready to continue" : "still need more"));
return s;
}
bool check_more_data_resolved() {
+ dbgout("checking for data block resolution with state " + debug_enum(4, this.state) + ", stream reports " + stream.available + " available bytes");
if (state == MPDS_MORE_DATA) { // you've given it more bytes, is it enough to continue?
bool useprevstate = false;
if (sub_decoder !is null)
@@ -1745,6 +1780,7 @@ class mp_decoder {
protected void set_state(mp_decoder_state state) {
prev_state = this.state;
this.state = state;
+ dbgout("Decoder transitioned to state " + debug_enum(4, state));
}
protected void set_currents(mp_format format, mp_type type, mp_count_type count_type, mp_count count) {
@@ -1752,6 +1788,7 @@ class mp_decoder {
cur_type = type;
cur_count_type = count_type;
cur_count = count;
+ if (format != MPF_UNDETERMINED) dbgout("Currents set: " + debug_enum(MPI_FORMAT, format) + "/" + debug_enum(MPI_TYPE, type) + "/" + debug_enum(MPI_COUNT_TYPE, count_type) + "/" + debug_enum(MPI_COUNT, count));
}
protected void reset_currents() {
@@ -1760,12 +1797,14 @@ class mp_decoder {
cur_ext_type = 0;
cur_skip_content = false;
@cur_value = null;
+ dbgout("currents reset");
}
// only works once, getting the value causes the state to reset to receive the next one
mp_value@ get_value() {
if (state != MPDS_READ) return null; // there isn't a value for you to get
+ dbgout("yield value");
mp_value@ ret = cur_value;
reset_currents();
if (fixed_length && !nested && stream.available == 0) set_state(MPDS_END_DATA); // last value in the stream
@@ -1779,6 +1818,7 @@ class mp_decoder {
// if there was not yet a value to discard, state is unchanged.
bool discard_value() {
if (state == MPDS_READ) {
+ dbgout("discarded value");
reset_currents();
if (fixed_length && !nested && stream.available == 0) set_state(MPDS_END_DATA); // last value in the stream
else set_state(MPDS_READY); // prepare to read the next one
@@ -1792,7 +1832,9 @@ class mp_decoder {
if (state == MPDS_END_DATA) return state;
else if (state != MPDS_READY) return MPDS_INVALID_OPERATION; // invalid operation order!
+ dbgout("reading format...");
if (stream.available < 1) {
+ dbgout("tried to read format, stream reports " + stream.available + " bytes available");
set_state(!nested ? MPDS_END_DATA : MPDS_MORE_DATA); // a subdecoder can never have the state MPDS_END_DATA as the existence of a subdecoder depends on the parent needing to read complex contents.
// If we had hit the end, this would have happened on the parent, in which case it means we've consumed all data in the stream.
return state;
@@ -1802,14 +1844,17 @@ class mp_decoder {
mp_type type = mp_type(mp_infomap[byte][MPI_TYPE]);
mp_count_type count_type = mp_count_type(mp_infomap[byte][MPI_COUNT_TYPE]);
mp_count count = mp_count(mp_infomap[byte][MPI_COUNT]);
+ dbgout("Read start byte " + format_uint(byte, "H") + ": " + debug_enum(MPI_FORMAT, format) + "/" + debug_enum(MPI_TYPE, type) + "/" + debug_enum(MPI_COUNT_TYPE, count_type) + "/" + debug_enum(MPI_COUNT, count));
if (format == MP_INVALID) {
// this byte should never appear in this position, jam and abort!
+ dbgout("read invalid byte");
set_state(MPDS_INVALID);
outformat = MP_INVALID;
return state;
}
if (count == MPC_EMPTY) {
// only boolean and nil fit here
+ dbgout("loading standalone value");
switch (type) {
case MPT_NIL: {
@cur_value = mp_value(type, format, null);
@@ -1824,6 +1869,7 @@ class mp_decoder {
break;
default:
+ dbgout("default in standalone switch");
set_state(MPDS_INVALID);
outformat = MP_INVALID;
return state;
@@ -1832,6 +1878,7 @@ class mp_decoder {
set_state(MPDS_READ);
} else if (count == MPC_SUBFIELD) {
// first, check for fixints, which are also stored in just this byte
+ dbgout("Loading subfield value");
switch (format) {
case MPF_POS_FIXINT: {
uint8 val = byte & 0x7f;
@@ -1853,6 +1900,7 @@ class mp_decoder {
case MPF_FIXSTR: {
set_currents(format, type, count_type, count);
cur_count_argument = byte & 0x1F;
+ dbgout("current count argument set to " + cur_count_argument + " from subfield");
set_state(MPDS_CONTENT);
}
break;
@@ -1860,6 +1908,7 @@ class mp_decoder {
case MPF_FIXARRAY: {
set_currents(format, type, count_type, count);
cur_count_argument = byte & 0x0F;
+ dbgout("current count argument set to " + cur_count_argument + " from subfield");
set_state(MPDS_CONTENT);
}
break;
@@ -1867,11 +1916,13 @@ class mp_decoder {
case MPF_FIXMAP: {
set_currents(format, type, count_type, count);
cur_count_argument = byte & 0x0F;
+ dbgout("current count argument set to " + cur_count_argument + " from subfield");
set_state(MPDS_CONTENT);
}
break;
default:
+ dbgout("default in subfield switch");
set_state(MPDS_INVALID);
outformat = MP_INVALID;
return state;
@@ -1885,6 +1936,7 @@ class mp_decoder {
// so we set argument and change to a different state
// we exclude values here because we actually use the count state to read numbers rather than the content state
cur_count_argument = count & 0x7f; // lop off the high bit to load the constant argument
+ dbgout("current count argument set to " + cur_count_argument + " from start byte constant");
set_state(type == MPT_EXT ? MPDS_EXT_TYPE : MPDS_CONTENT);
} else set_state(MPDS_COUNT);
}
@@ -1896,11 +1948,14 @@ class mp_decoder {
if (state != MPDS_COUNT) return MPDS_INVALID_OPERATION;
uint bytes_to_read = cur_count & 0x7f; // knock off the constant bit
+ dbgout("reading count in " + bytes_to_read + " bytes");
if (stream.available < bytes_to_read) {
+ dbgout("need " + bytes_to_read + " bytes to read count, stream reports " + stream.available + " bytes available");
set_state(MPDS_MORE_DATA);
return state;
}
if (cur_count_type == MPCT_VALUE) { // ints and floats, have to special case for each
+ dbgout("count is value");
any vala; // to store the result
switch (cur_format) {
case MPF_FLOAT32: {
@@ -1964,6 +2019,7 @@ class mp_decoder {
break;
default: {
+ dbgout("default in value switch");
set_state(MPDS_INVALID);
outcount = 0;
return state;
@@ -1988,11 +2044,13 @@ class mp_decoder {
break;
default:
+ dbgout("default in count width switch");
set_state(MPDS_INVALID);
outcount = 0;
return state;
}
cur_count_argument = count_argument;
+ dbgout("loaded current count argument " + cur_count_argument);
outcount = cur_count_argument;
set_state((cur_type == MPT_EXT ? MPDS_EXT_TYPE : MPDS_CONTENT));
}
@@ -2002,11 +2060,14 @@ class mp_decoder {
mp_decoder_state read_ext_type(int8 &out out_ext_type = void) {
if (state != MPDS_EXT_TYPE) return MPDS_INVALID_OPERATION;
+ dbgout("reading ext type");
if (stream.available < 1) {
+ dbgout("need 1 byte to read count, stream reports 0 bytes available");
set_state(MPDS_MORE_DATA);
return state;
}
int8 ext_type = stream.read_int8();
+ dbgout("ext type is " + ext_type);
cur_ext_type = ext_type;
out_ext_type = cur_ext_type;
set_state(MPDS_CONTENT);
@@ -2016,21 +2077,26 @@ class mp_decoder {
mp_decoder_state read_content(bool skip = false) {
if (state != MPDS_CONTENT) return MPDS_INVALID_OPERATION;
+ dbgout("reading content");
if (cur_count_type == MPCT_BYTES) {
// number of bytes expected is in cur_count_argument, we read that many bytes into a string
// the formats known to use MPCT_BYTES are str, bin, and ext, all of which use a string as the base data
+ dbgout("content is n bytes");
if (stream.available < cur_count_argument) {
+ dbgout("need " + cur_count_argument + " bytes to read content, stream reports " + stream.available + " bytes available");
set_state(MPDS_MORE_DATA);
return state;
}
if (skip || cur_skip_content) {
stream.rseek_relative(cur_count_argument);
+ dbgout("skipped " + cur_count_argument + " bytes");
set_state(MPDS_READY);
reset_currents();
return state;
} else {
// gotcha, if count is 0 read(0) will read as much as possible! We actually want it to be the empty string.
const string data = (cur_count_argument > 0 ? stream.read(cur_count_argument) : "");
+ dbgout("read string of length " + data.length());
if (cur_type == MPT_EXT)
@cur_value = mp_value(cur_type, cur_format, any(mp_ext(cur_ext_type, data)));
else
@@ -2040,7 +2106,9 @@ class mp_decoder {
}
} else if (cur_count_type == MPCT_ITEMS) {
// arrays are the only ones that use this type
+ dbgout("content is n items");
if (recursion_level >= max_recursion) {
+ dbgout("Recursion level of " + max_recursion + " exceeded");
set_state(MPDS_INVALID);
throw(MP_RECURSION_LIMIT_EXCEPTION);
return MPDS_INVALID;
@@ -2052,6 +2120,7 @@ class mp_decoder {
mp_decoder_state ts = sub_decoder.subdecoder_iterate_read();
if (ts == MPDS_READ) {
// retrieve that value now from the sub-decoder and destroy it
+ dbgout("subdecoder completed, reading array");
mp_value@[]@ data = sub_decoder.get_array();
@cur_value = mp_value(cur_type, cur_format, any(data));
@sub_decoder = null;
@@ -2059,17 +2128,21 @@ class mp_decoder {
// the other states it could return would be MPDS_INVALID and MPDS_MORE_DATA, both of which need to bubble up.
// if mpds_MORE_DATA happens, pushing more data will clear it and allow this to work again
set_state(ts);
+ dbgout("end subdecoder section");
} catch {
// only potential exceptions here are max recursion and invalid key
// set state to invalid and rethrow, so that the entire stack of decoders becomes invalid.
const string exc = get_exception_info();
+ dbgout("Subdecoder threw " + exc);
set_state(MPDS_INVALID);
throw(exc);
return MPDS_INVALID;
}
} else if (cur_count_type == MPCT_ITEMS2X) {
// maps are the only ones that use this type
+ dbgout("content is n pairs");
if (recursion_level >= max_recursion) {
+ dbgout("Recursion level of " + max_recursion + " exceeded");
set_state(MPDS_INVALID);
throw(MP_RECURSION_LIMIT_EXCEPTION);
return MPDS_INVALID;
@@ -2081,6 +2154,7 @@ class mp_decoder {
mp_decoder_state ts = sub_decoder.subdecoder_iterate_read();
if (ts == MPDS_READ) {
// retrieve that value now from the sub-decoder and destroy it
+ dbgout("subdecoder completed, reading map");
mp_map@ data = sub_decoder.get_map();
@cur_value = mp_value(cur_type, cur_format, any(data));
@sub_decoder = null;
@@ -2088,14 +2162,17 @@ class mp_decoder {
// the other states it could return would be MPDS_INVALID and MPDS_MORE_DATA, both of which need to bubble up.
// if mpds_MORE_DATA happens, pushing more data will clear it and allow this to work again
set_state(ts);
+ dbgout("end subdecoder section");
} catch {
// only potential exceptions here are max recursion and invalid key
// set state to invalid and rethrow, so that the entire stack of decoders becomes invalid.
const string exc = get_exception_info();
+ dbgout("Subdecoder threw " + exc);
set_state(MPDS_INVALID);
throw(exc);
}
} else {
+ dbgout("invalid precondition for content");
set_state(MPDS_INVALID); // what state have you gotten me in, anyway?
}
return state;
@@ -2106,6 +2183,7 @@ class mp_decoder {
// because of this, you'll have to call get_value yourself if it returns MPDS_READ
mp_decoder_state try_read_value(bool skip = false) {
// the states shall progress in the order notated in the enumeration, except some may be skipped depending on the data itself
+ dbgout("try_read_value");
mp_decoder_state ts = state;
if (ts == MPDS_READY) ts = this.read_format();
if (ts == MPDS_COUNT) ts = this.read_count();
@@ -2114,6 +2192,7 @@ class mp_decoder {
// if all succeeded, that chain should give us MPDS_READ
// else we ran into something like MPDS_MORE_DATA, or skip gave us MPDS_READY again
// in either case, just return as is
+ dbgout("end try_read_value with state " + debug_enum(4, ts));
return ts;
}
@@ -2151,6 +2230,7 @@ class mp_decoder {
mp_decoder_state subdecoder_iterate_read() {
if (nested) {
if (is_map) {
+ dbgout("subdecoder_iterate_read for map");
while (items_to_read > 0) {
mp_decoder_state ts = try_read_value();
if (ts == MPDS_READ) {
@@ -2169,6 +2249,7 @@ class mp_decoder {
break;
default: {
+ dbgout("non-key type " + debug_enum(MPI_FORMAT, val.format));
set_state(MPDS_INVALID);
throw(MP_INVALID_KEY_TYPE_EXCEPTION);
return MPDS_INVALID;
@@ -2192,6 +2273,7 @@ class mp_decoder {
break;
default: {
+ dbgout("non-key type " + debug_enum(MPI_FORMAT, val.format));
set_state(MPDS_INVALID);
throw(MP_INVALID_KEY_TYPE_EXCEPTION);
return MPDS_INVALID;
@@ -2205,10 +2287,12 @@ class mp_decoder {
@cur_key = null;
is_map_value = false;
items_to_read -= 1;
+ dbgout("read map value, " + items_to_read + " pairs remaining");
} // else
} // MPDS_READ
else if (ts == MPDS_READY) {
// skip?
+ dbgout("got ready state, assumed skip");
if (!is_map_value) is_map_value = true;
else {
items_to_read -= 1;
@@ -2219,23 +2303,28 @@ class mp_decoder {
} // while
// if we have no items to read the above loop won't execute even once, and state will still be MPDS_READY from us entering!
if (state == MPDS_READY && items_to_read == 0) {
+ dbgout("read all items");
set_state(MPDS_READ);
} // MPDS_READY and 0 items to read
} // is_map
else {
+ dbgout("subdecoder_iterate_read for array");
while (items_to_read > 0) {
mp_decoder_state ts = try_read_value();
if (ts == MPDS_READ) {
cur_array.insert_last(this.get_value());
items_to_read -= 1;
+ dbgout("inserted array value, have " + items_to_read + " items left");
} // MPDS_READ
else if (ts == MPDS_READY) {
// skip?
+ dbgout("got ready state, assumed skip");
items_to_read -= 1;
} else break; // some other state happened
} // while
// if we have no items to read the above loop won't execute even once, and state will still be MPDS_READY from us entering!
if (state == MPDS_READY && items_to_read == 0) {
+ dbgout("read all items");
set_state(MPDS_READ);
} // MPDS_READY and 0 items to read
} // else not map
@@ -2257,6 +2346,7 @@ class mp_encoder {
this.strict_map_keys = strict_map_keys;
@this.stream = datastream("", "", STREAM_BYTE_ORDER_NETWORK);
this.stream.sync_rw_cursors = false;
+ dbgout("encoder created");
}
~mp_encoder() {
@@ -2270,6 +2360,7 @@ class mp_encoder {
// unlike the decoder, we will not use this function to handle writing the various kinds of integers and floats
protected void encode_count(int count_width, uint count_value) {
assert(count_width < MPC_SUBFIELD, "Constant/subfield count was passed to encoder::encode_count");
+ dbgout("Writing count " + count_value + " of type " + debug_enum(MPI_COUNT, count_width));
switch (count_width) {
case MPC_8BIT:
stream.write_uint8(count_value);
@@ -2284,6 +2375,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_count");
throw("msgpack invalid count size");
break;
}
@@ -2299,6 +2391,7 @@ class mp_encoder {
// MPT_BOOLEAN and MPT_NIL
protected void encode_literal(mp_value@ v) {
assert(v.type == MPT_BOOLEAN || v.type == MPT_NIL, "non-literal value was passed to encoder::encode_literal");
+ dbgout("writing literal " + debug_enum(MPI_FORMAT, v.format));
switch (v.format) {
case MPF_NIL:
case MPF_FALSE:
@@ -2307,6 +2400,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_literal");
throw("msgpack invalid format for literal");
break;
}
@@ -2315,6 +2409,7 @@ class mp_encoder {
// MPT_FLOAT
protected void encode_float(mp_value@ v) {
assert(v.type == MPT_FLOAT, "non-float value was passed to encoder::encode_float");
+ dbgout("writing float value as " + debug_enum(MPI_FORMAT, v.format));
switch (v.format) {
case MPF_FLOAT32:
stream.write_uint8(v.format);
@@ -2327,6 +2422,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_float");
throw("msgpack invalid format for float");
break;
}
@@ -2335,6 +2431,7 @@ class mp_encoder {
// MPT_INT
protected void encode_int(mp_value@ v) {
assert(v.type == MPT_INT, "non-int value was passed to encoder::encode_int");
+ dbgout("writing int format " + debug_enum(MPI_FORMAT, v.format));
switch (v.format) {
case MPF_POS_FIXINT:
stream.write_uint8(v.get_uint8());
@@ -2385,6 +2482,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_int");
throw("msgpack invalid format for integer");
break;
}
@@ -2394,6 +2492,7 @@ class mp_encoder {
protected void encode_string(mp_value@ v) {
assert(v.type == MPT_STRING || v.type == MPT_BIN, "non-string type passed to encoder::encode_string");
const string data = v.get_string();
+ dbgout("writing string of length " + data.length() + " as " + debug_enum(MPI_FORMAT, v.format));
switch (v.format) {
case MPF_FIXSTR: {
uint8 to_write = MPF_FIXSTR | uint8(data.length()); // subfield
@@ -2414,6 +2513,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of swich in encode_string");
throw("msgpack invalid string/bin format passed to write_string");
return;
}
@@ -2424,6 +2524,7 @@ class mp_encoder {
// MPT_EXT
protected void encode_ext(mp_value@ v) {
assert(v.type == MPT_EXT, "non-ext type passed to encoder::encode_ext");
+ dbgout("writing ext format " + debug_enum(MPI_FORMAT, v.format));
mp_ext@ e = v.get_ext();
const string data = e.data;
switch (v.format) {
@@ -2445,6 +2546,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_ext");
throw("msgpack invalid format for ext");
return;
}
@@ -2460,6 +2562,7 @@ class mp_encoder {
throw(MP_RECURSION_LIMIT_EXCEPTION);
mp_value@[]@ a = v.get_array();
const uint len = a.length();
+ dbgout("writing array format " + debug_enum(MPI_FORMAT, v.format) + " (" + len + " items");
switch (v.format) {
case MPF_FIXARRAY: {
uint8 to_write = MPF_FIXARRAY | uint8(len);
@@ -2475,6 +2578,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_array");
throw("msgpack invalid format for array");
return;
}
@@ -2491,6 +2595,7 @@ class mp_encoder {
mp_map@ m = v.get_map();
mp_value@[][] @ p = m.get_pairs(sort_map_keys);
const uint len = p.length();
+ dbgout("writing map format " + debug_enum(MPI_FORMAT, v.format) + " (" + len + " items");
switch (v.format) {
case MPF_FIXMAP: {
uint8 to_write = MPF_FIXMAP | uint8(len);
@@ -2506,6 +2611,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_map");
throw("msgpack invalid format for map");
return;
}
@@ -2556,6 +2662,7 @@ class mp_encoder {
break;
default:
+ dbgout("default of switch in encode_value");
throw("msgpack invalid value type");
break;
}
@@ -2780,16 +2887,20 @@ class mp_encoder {
// dumps, dump a value to a string and return it immediately
const string mp_dumps(mp_value@ v, bool sort_map_keys = false, bool strict_map_keys = true, uint max_recursion = 100) {
+ dbgout("dumps");
mp_encoder enc(strict_map_keys, max_recursion);
enc.write_value(v, sort_map_keys);
+ dbgout("end dumps");
return enc.flush();
}
// dumps_chain, dump a chain (array) of values to a string and return it immediately. Array is not serialized as array.
const string mp_dumps_chain(mp_value@[]& v, bool sort_map_keys = false, bool strict_map_keys = true, uint max_recursion = 100) {
+ dbgout("dumps_chain with " + v.length() + " values");
mp_encoder enc(strict_map_keys, max_recursion);
for (uint i = 0; i < v.length(); i++)
enc.write_value(v[i], sort_map_keys);
+ dbgout("end dumps_chain");
return enc.flush();
}
@@ -2797,20 +2908,25 @@ const string mp_dumps_chain(mp_value@[]& v, bool sort_map_keys = false, bool str
// if the string contains a chain of values, only the first one will be returned! Use loads_chain to handle them all.
// if any error occurs and a value is not able to be retrieved on the first read call, null will be returned
mp_value@ mp_loads(const string &in data, bool strict_map_keys = true, uint max_recursion = 100) {
+ dbgout("loads with " + data.length() + " bytes of data");
mp_decoder dec(data, true, strict_map_keys, max_recursion); // fixed-length mode
try {
mp_decoder_state ts = dec.try_read_value();
if (ts == MPDS_READ) {
+ dbgout("end loads");
return dec.get_value();
}
} catch {
+ dbgout("decoder threw " + get_exception_info() + ", loads terminating");
return null;
}
+ dbgout("Improper state in loads");
return null; // state was not MPDS_READ and no exception occurred
}
// load a chain of values until either no more data is available or an error condition occurs
mp_value@[]@ mp_loads_chain(const string &in data, bool strict_map_keys = true, uint max_recursion = 100) {
+ dbgout("loads_chain with " + data.length() + " bytes of data");
mp_value@[] ret;
mp_decoder dec(data, true, strict_map_keys, max_recursion); // fixed-length mode
while (dec.get_state() == MPDS_READY) {
@@ -2818,12 +2934,183 @@ mp_value@[]@ mp_loads_chain(const string &in data, bool strict_map_keys = true,
mp_decoder_state ts = dec.try_read_value();
if (ts == MPDS_READ) {
ret.insert_last(dec.get_value());
+ dbgout("loads_chain got value, now have " + ret.length() + " to return");
} else {
+ dbgout("loads_chain got non-read state");
break;
}
} catch {
+ dbgout("loads_chain got exception " + get_exception_info());
break;
}
}
+ dbgout("end loads_chain with " + ret.length() + " values");
return ret;
+}
+/// BEGIN DEBUG ///
+const bool DEBUG = true;
+const string DEBUG_FILE = "msgpack.log";
+file DEBUG_FILE_OBJECT;
+void dbgout(const string &in msg) {
+ if (DEBUG) {
+ DEBUG_FILE_OBJECT.open(DEBUG_FILE, "a");
+ DEBUG_FILE_OBJECT.write(msg + "\n");
+ DEBUG_FILE_OBJECT.close();
+ }
+}
+
+const string[][] enumout = prepare_enumout(); // stores string representation of enums
+const string[][] prepare_enumout() {
+ string[][] ret;
+ string[] types = {"MPT_INT", "MPT_NIL", "MPT_BOOLEAN", "MPT_FLOAT", "MPT_STRING", "MPT_BIN", "MPT_ARRAY", "MPT_MAP", "MPT_EXT"};
+ string[] count_types = {"MPCT_EMPTY", "MPCT_VALUE", "MPCT_BYTES", "MPCT_ITEMS", "MPCT_ITEMS2X"};
+ string[] counts(256);
+ counts[MPC_EMPTY] = "MPC_EMPTY";
+ counts[MPC_8BIT] = "MPC_8BIT";
+ counts[MPC_16BIT] = "MPC_16BIT";
+ counts[MPC_32BIT] = "MPC_32BIT";
+ counts[MPC_SUBFIELD] = "MPC_SUBFIELD";
+ counts[MPC_1BYTE] = "MPC_1BYTE";
+ counts[MPC_2BYTES] = "MPC_2BYTES";
+ counts[MPC_4BYTES] = "MPC_4BYTES";
+ counts[MPC_8BYTES] = "MPC_8BYTES";
+ counts[MPC_16BYTES] = "MPC_16BYTES";
+ string[] formats(256);
+ formats[MPF_POS_FIXINT] = "MPF_POS_FIXINT";
+ formats[MPF_FIXMAP] = "MPF_FIXMAP";
+ formats[MPF_FIXARRAY] = "MPF_FIXARRAY";
+ formats[MPF_FIXSTR] = "MPF_FIXSTR";
+ formats[MPF_NIL] = "MPF_NIL";
+ formats[MP_INVALID] = "MP_INVALID";
+ formats[MPF_FALSE] = "MPF_FALSE";
+ formats[MPF_TRUE] = "MPF_TRUE";
+ formats[MPF_BIN8] = "MPF_BIN8";
+ formats[MPF_BIN16] = "MPF_BIN16";
+ formats[MPF_BIN32] = "MPF_BIN32";
+ formats[MPF_EXT8] = "MPF_EXT8";
+ formats[MPF_EXT16] = "MPF_EXT16";
+ formats[MPF_EXT32] = "MPF_EXT32";
+ formats[MPF_FLOAT32] = "MPF_FLOAT32";
+ formats[MPF_FLOAT64] = "MPF_FLOAT64";
+ formats[MPF_UINT8] = "MPF_UINT8";
+ formats[MPF_UINT16] = "MPF_UINT16";
+ formats[MPF_UINT32] = "MPF_UINT32";
+ formats[MPF_UINT64] = "MPF_UINT64";
+ formats[MPF_INT8] = "MPF_INT8";
+ formats[MPF_INT16] = "MPF_INT16";
+ formats[MPF_INT32] = "MPF_INT32";
+ formats[MPF_INT64] = "MPF_INT64";
+ formats[MPF_FIXEXT1] = "MPF_FIXEXT1";
+ formats[MPF_FIXEXT2] = "MPF_FIXEXT2";
+ formats[MPF_FIXEXT4] = "MPF_FIXEXT4";
+ formats[MPF_FIXEXT8] = "MPF_FIXEXT8";
+ formats[MPF_FIXEXT16] = "MPF_FIXEXT16";
+ formats[MPF_STR8] = "MPF_STR8";
+ formats[MPF_STR16] = "MPF_STR16";
+ formats[MPF_STR32] = "MPF_STR32";
+ formats[MPF_ARRAY16] = "MPF_ARRAY16";
+ formats[MPF_ARRAY32] = "MPF_ARRAY32";
+ formats[MPF_MAP16] = "MPF_MAP16";
+ formats[MPF_MAP32] = "MPF_MAP32";
+ formats[MPF_NEG_FIXINT] = "MPF_NEG_FIXINT";
+ ret.resize(4);
+ ret[MPI_FORMAT] = formats;
+ ret[MPI_TYPE] = types;
+ ret[MPI_COUNT_TYPE] = count_types;
+ ret[MPI_COUNT] = counts;
+ return ret;
+}
+
+string debug_enum(uint infotype, int value) {
+ if (infotype == 4) {
+ switch (value) {
+ case MPDS_END_DATA:
+ return "MPDS_END_DATA";
+ break;
+
+ case MPDS_MORE_DATA:
+ return "MPDS_MORE_DATA";
+ break;
+
+ case MPDS_INVALID:
+ return "MPDS_INVALID";
+ break;
+
+ case MPDS_READY:
+ return "MPDS_READY";
+ break;
+
+ case MPDS_COUNT:
+ return "MPDS_COUNT";
+ break;
+
+ case MPDS_EXT_TYPE:
+ return "MPDS_EXT_TYPE";
+ break;
+
+ case MPDS_CONTENT:
+ return "MPDS_CONTENT";
+ break;
+
+ case MPDS_READ:
+ return "MPDS_READ";
+ break;
+
+ default:
+ return "unknown state";
+ break;
+ }
+
+ return "unknown state";
+ } else {
+ if (value == -1) return "undetermined";
+ else return enumout[infotype][value];
+ }
+}
+
+string print_value(mp_value@ v) {
+ string ret = debug_enum(MPI_TYPE, v.type) + "/" + debug_enum(MPI_FORMAT, v.format) + ": ";
+ switch (v.type) {
+ case MPT_INT:
+ case MPT_FLOAT:
+ case MPT_NIL:
+ case MPT_BOOLEAN:
+ ret += string(v);
+ return ret;
+ break;
+
+ case MPT_STRING:
+ case MPT_BIN:
+ ret += (v.type == MPT_BIN ? "b" : "") + "\"" + string(v) + "\"";
+ return ret;
+ break;
+
+ default:
+ break;
+ }
+ if (v.type == MPT_EXT) {
+ mp_ext@ e = v.get_ext();
+ ret += e.type + ": b\"" + e.data + "\"";
+ return ret;
+ } else if (v.type == MPT_ARRAY) {
+ mp_value@[]@ a = v.get_array();
+ ret += "[";
+ for (uint i = 0; i < a.length(); i++) {
+ ret += print_value(a[i]);
+ if (i < (a.length() - 1)) ret += ", ";
+ }
+ ret += "]";
+ return ret;
+ } else if (v.type == MPT_MAP) {
+ mp_map@ m = v.get_map();
+ mp_value@[][] pairs = m.get_pairs(true);
+ ret += "{";
+ for (uint i = 0; i < pairs.length(); i++) {
+ ret += print_value(pairs[i][0]) + ": " + print_value(pairs[i][1]);
+ if (i < (pairs.length() - 1)) ret += ", ";
+ }
+ ret += "}";
+ return ret;
+ }
+ return "unknown data!";
}
\ No newline at end of file