-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgpack.zig
More file actions
1369 lines (1225 loc) · 47 KB
/
msgpack.zig
File metadata and controls
1369 lines (1225 loc) · 47 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
const std = @import("std");
fn truncateToAny(comptime Target: type, n: anytype) Target {
const T = @typeInfo(@TypeOf(n)).int;
const target_sign = @typeInfo(Target).int.signedness;
if (T.signedness == target_sign) return @truncate(n);
const U = std.meta.Int(target_sign, T.bits);
const u: U = @bitCast(n);
return @truncate(u);
}
fn packU8(comptime T: type, op_num: comptime_int, n: anytype) u8 {
const n_bits = 8 - @bitSizeOf(T);
const out: u8 = @as(u8, op_num << n_bits) | truncateToAny(u8, n);
return out;
}
// Endieness
fn packBE(comptime T: type, buf: []u8, v: T) void {
comptime {
std.debug.assert(@typeInfo(T) == .int);
}
const nb = @bitSizeOf(T) / 8;
const UType = @Type(std.builtin.Type{ .int = .{
.signedness = .unsigned,
.bits = @bitSizeOf(T),
} });
const uv: UType = @bitCast(v);
inline for (0..nb) |i| {
const shift: comptime_int = (nb - 1 - i) * 8;
buf[i] = @truncate(uv >> shift);
}
}
test "pack BE" {
var buf: [9]u8 = [_]u8{0} ** 9;
packBE(u16, &buf, 0x1234);
try std.testing.expectEqual(buf[0], 0x12);
try std.testing.expectEqual(buf[1], 0x34);
packBE(u32, &buf, 0xabcd5678);
try std.testing.expectEqual(buf[0], 0xab);
try std.testing.expectEqual(buf[1], 0xcd);
try std.testing.expectEqual(buf[2], 0x56);
try std.testing.expectEqual(buf[3], 0x78);
packBE(u64, &buf, 0x1234abcd9abcdef0);
try std.testing.expectEqual(buf[0], 0x12);
try std.testing.expectEqual(buf[1], 0x34);
try std.testing.expectEqual(buf[2], 0xab);
try std.testing.expectEqual(buf[3], 0xcd);
try std.testing.expectEqual(buf[4], 0x9a);
try std.testing.expectEqual(buf[5], 0xbc);
try std.testing.expectEqual(buf[6], 0xde);
try std.testing.expectEqual(buf[7], 0xf0);
packBE(i16, &buf, -0x1234);
try std.testing.expectEqual(buf[0], 0xed);
try std.testing.expectEqual(buf[1], 0xcc);
}
// https://github.com/msgpack/msgpack/blob/master/spec.md#nil-format
pub const m_nil: u8 = 0xc0;
// https://github.com/msgpack/msgpack/blob/master/spec.md#bool-format-family
pub const m_false: u8 = 0xc2;
pub const m_true: u8 = 0xc3;
// https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family
const IntFamily = enum {
pos_fixint,
neg_fixint,
int8,
int16,
int32,
int64,
uint8,
uint16,
uint32,
uint64,
pub fn determine(comptime T: type, v: T) IntFamily {
comptime {
std.debug.assert(@typeInfo(T) == .int);
}
if (v >= 0) {
if (v <= std.math.maxInt(u7)) {
return .pos_fixint;
} else if (v <= std.math.maxInt(u8)) {
return .uint8;
} else if (v <= std.math.maxInt(u16)) {
return .uint16;
} else if (v <= std.math.maxInt(u32)) {
return .uint32;
} else {
return .uint64;
}
} else {
if (v >= std.math.minInt(i6)) {
// 5 bits but the prefix are all 1
return .neg_fixint;
} else if (v >= std.math.minInt(i8)) {
return .int8;
} else if (v >= std.math.minInt(i16)) {
return .int16;
} else if (v >= std.math.minInt(i32)) {
return .int32;
} else {
return .int64;
}
}
}
};
pub fn packInt(comptime T: type, v: T, buf: []u8) []u8 {
const fam = IntFamily.determine(T, v);
switch (fam) {
.pos_fixint => {
buf[0] = packU8(u1, 0, v);
return buf[0..1];
},
.neg_fixint => {
buf[0] = packU8(u3, 0b111, v);
return buf[0..1];
},
.uint8 => {
buf[0] = 0xcc;
buf[1] = truncateToAny(u8, v);
return buf[0..2];
},
.uint16 => {
buf[0] = 0xcd;
packBE(u16, buf[1..], @intCast(v));
return buf[0..3];
},
.uint32 => {
buf[0] = 0xce;
packBE(u32, buf[1..], @intCast(v));
return buf[0..5];
},
.uint64 => {
buf[0] = 0xcf;
packBE(u64, buf[1..], @intCast(v));
return buf[0..9];
},
.int8 => {
buf[0] = 0xd0;
buf[1] = truncateToAny(u8, v);
return buf[0..2];
},
.int16 => {
buf[0] = 0xd1;
packBE(i16, buf[1..], @intCast(v));
return buf[0..3];
},
.int32 => {
buf[0] = 0xd2;
packBE(i32, buf[1..], @intCast(v));
return buf[0..5];
},
.int64 => {
buf[0] = 0xd3;
packBE(i64, buf[1..], @intCast(v));
return buf[0..9];
},
}
}
test "test int packing" {
{
var buf: [9]u8 = @splat(42);
const out = packInt(u32, 5, &buf);
try std.testing.expectEqualSlices(u8, &[_]u8{5}, out);
try std.testing.expectEqualSlices(u8, &([_]u8{5} ++ .{42} ** 8), &buf);
}
{
var buf: [9]u8 = @splat(42);
const out = packInt(i8, -5, &buf);
try std.testing.expectEqualSlices(u8, &[_]u8{0xfb}, out);
try std.testing.expectEqualSlices(u8, &([_]u8{0xfb} ++ .{42} ** 8), &buf);
}
{
var buf: [9]u8 = @splat(42);
const out = packInt(i10, 128, &buf);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcc, 0x80 }, out);
try std.testing.expectEqualSlices(u8, &([_]u8{ 0xcc, 0x80 } ++ .{42} ** 7), &buf);
}
{
var buf: [9]u8 = @splat(42);
const out = packInt(u32, 0xbaadf00d, &buf);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xce, 0xba, 0xad, 0xf0, 0x0d }, out);
try std.testing.expectEqualSlices(u8, &([_]u8{ 0xce, 0xba, 0xad, 0xf0, 0x0d } ++ .{42} ** 4), &buf);
}
{
var buf: [9]u8 = @splat(42);
const out = packInt(i64, 0x420baadf00d, &buf);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcf, 0, 0, 0x04, 0x20, 0xba, 0xad, 0xf0, 0x0d }, out);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcf, 0, 0, 0x04, 0x20, 0xba, 0xad, 0xf0, 0x0d }, &buf);
}
{
var buf: [9]u8 = @splat(42);
const out = packInt(i64, -555, &buf);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xd1, 0xfd, 0xd5 }, out);
}
}
// https://github.com/msgpack/msgpack/blob/master/spec.md#float-format-family
pub const FloatFamily = enum {
float32,
float64,
pub fn determine(comptime T: type, v: T) FloatFamily {
comptime {
std.debug.assert(@typeInfo(T) == .float);
}
const f_info = @typeInfo(T).float;
if (f_info.bits <= 32) {
return .float32;
} else {
const v32: f32 = @floatCast(v);
const v32_in_64: f64 = @floatCast(v32);
if (v32_in_64 == v) {
return .float32;
}
return .float64;
}
}
};
pub fn packFloat(comptime T: type, v: T, buf: []u8) []u8 {
const fam = FloatFamily.determine(T, v);
switch (fam) {
.float32 => {
buf[0] = 0xca;
const vf: f32 = @floatCast(v);
const vu: u32 = @bitCast(vf);
packBE(u32, buf[1..], vu);
return buf[0..5];
},
.float64 => {
buf[0] = 0xcb;
const vf: f64 = @floatCast(v);
const vu: u64 = @bitCast(vf);
packBE(u64, buf[1..], vu);
return buf[0..9];
},
}
}
// https://github.com/msgpack/msgpack/blob/master/spec.md#str-format-family
pub const PackError = error{TooManyElements};
const StrFamily = enum {
fixstr,
str8,
str16,
str32,
pub fn determine(l: usize) PackError!StrFamily {
if (l <= std.math.maxInt(u5)) {
return .fixstr;
} else if (l <= std.math.maxInt(u8)) {
return .str8;
} else if (l <= std.math.maxInt(u16)) {
return .str16;
} else if (l <= std.math.maxInt(u32)) {
return .str32;
} else {
return PackError.TooManyElements;
}
}
};
pub fn packStrHeader(len: usize, buf: []u8) PackError![]u8 {
const fam = try StrFamily.determine(len);
switch (fam) {
.fixstr => {
buf[0] = packU8(u3, 0b101, len);
return buf[0..1];
},
.str8 => {
buf[0] = 0xd9;
buf[1] = truncateToAny(u8, len);
return buf[0..2];
},
.str16 => {
buf[0] = 0xda;
packBE(u16, buf[1..], @intCast(len));
return buf[0..3];
},
.str32 => {
buf[0] = 0xdb;
packBE(u32, buf[1..], @intCast(len));
return buf[0..5];
},
}
}
// https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family
// TODO: Not implemented yet
// https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family
const ArrayFamily = enum {
fixarray,
array16,
array32,
pub fn determine(l: usize) PackError!ArrayFamily {
if (l <= std.math.maxInt(u4)) {
return .fixarray;
} else if (l <= std.math.maxInt(u16)) {
return .array16;
} else if (l <= std.math.maxInt(u32)) {
return .array32;
} else {
return PackError.TooManyElements;
}
}
};
pub fn packArrayHeader(len: usize, buf: []u8) PackError![]u8 {
const fam = try ArrayFamily.determine(len);
switch (fam) {
.fixarray => {
buf[0] = packU8(u4, 0b1001, len);
return buf[0..1];
},
.array16 => {
buf[0] = 0xdc;
packBE(u16, buf[1..], @intCast(len));
return buf[0..3];
},
.array32 => {
buf[0] = 0xdd;
packBE(u32, buf[1..], @intCast(len));
return buf[0..5];
},
}
}
// https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family
const MapFamily = enum {
fixmap,
map16,
map32,
pub fn determine(l: usize) PackError!MapFamily {
if (l <= std.math.maxInt(u4)) {
return .fixmap;
} else if (l <= std.math.maxInt(u16)) {
return .map16;
} else if (l <= std.math.maxInt(u32)) {
return .map32;
} else {
return PackError.TooManyElements;
}
}
};
pub fn packMapHeader(len: usize, buf: []u8) PackError![]u8 {
const fam = try MapFamily.determine(len);
switch (fam) {
.fixmap => {
buf[0] = packU8(u4, 0b1000, len);
return buf[0..1];
},
.map16 => {
buf[0] = 0xde;
packBE(u16, buf[1..], @intCast(len));
return buf[0..3];
},
.map32 => {
buf[0] = 0xdf;
packBE(u32, buf[1..], @intCast(len));
return buf[0..5];
},
}
}
test "test map packing" {
{
var buf: [1]u8 = undefined;
const out = try packMapHeader(0, &buf);
try std.testing.expectEqualSlices(u8, &[_]u8{0x80}, out);
try std.testing.expectEqualSlices(u8, &[_]u8{0x80}, &buf);
}
}
// https://github.com/msgpack/msgpack/blob/master/spec.md#ext-format-family
// TODO: Not implemented yet
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
// NOTE: Will not implement this for now
// IO based serialization
pub const Packer = struct {
const Self = @This();
writer: *std.io.Writer,
pub fn addNil(self: *Self) !void {
_ = try self.writer.writeByte(m_nil);
}
pub fn addBool(self: *Self, v: bool) !void {
_ = try self.writer.writeByte(if (v) m_true else m_false);
}
pub fn addInt(self: *Self, comptime T: type, v: T) !void {
var buf: [9]u8 = undefined;
const bytes = packInt(T, v, buf[0..]);
try self.writer.writeAll(bytes);
}
pub fn addFloat(self: *Self, comptime T: type, v: T) !void {
var buf: [9]u8 = undefined;
const bytes = packFloat(T, v, buf[0..]);
try self.writer.writeAll(bytes);
}
pub fn addStr(self: *Self, str: []const u8) !void {
var buf: [5]u8 = undefined;
const header_bytes = try packStrHeader(str.len, buf[0..]);
try self.writer.writeAll(header_bytes);
try self.writer.writeAll(str);
}
pub fn beginArray(self: *Self, len: usize) !void {
var buf: [5]u8 = undefined;
const header_bytes = try packArrayHeader(len, buf[0..]);
try self.writer.writeAll(header_bytes);
}
pub fn beginMap(self: *Self, len: usize) !void {
var buf: [5]u8 = undefined;
const header_bytes = try packMapHeader(len, buf[0..]);
try self.writer.writeAll(header_bytes);
}
};
test "test writer" {
var buf: [1024]u8 = std.mem.zeroes([1024]u8);
{
var writer = std.io.Writer.fixed(&buf);
var pack = Packer{ .writer = &writer };
try pack.beginMap(4);
const k0: *const [3:0]u8 = "abc";
try pack.addStr(k0);
try pack.addBool(false);
const k1: *const [3]u8 = "def";
try pack.addStr(k1);
try pack.addInt(u32, 55);
const k2: [20]u8 = @splat('%');
try pack.addStr(&k2);
try pack.addInt(u64, 0xbaadf00d);
const k3 = "this is an 向量";
try pack.addStr(k3);
try pack.beginArray(3);
try pack.addFloat(f64, -0.9);
try pack.addFloat(f64, 0.51);
try pack.addFloat(f64, 1.0 / 3.0);
try writer.flush();
const ref = "\x84\xa3abc\xc2\xa3def7\xb4%%%%%%%%%%%%%%%%%%%%\xce\xba\xad\xf0\r\xb1this is an \xe5\x90\x91\xe9\x87\x8f\x93\xcb\xbf\xec\xcc\xcc\xcc\xcc\xcc\xcd\xcb?\xe0Q\xeb\x85\x1e\xb8R\xcb?\xd5UUUUUU";
const gen = buf[0..writer.end];
try std.testing.expectEqualSlices(u8, ref, gen);
}
{
var writer = std.io.Writer.fixed(&buf);
var pack = Packer{ .writer = &writer };
try pack.beginArray(100);
for (0..100) |i| try pack.addInt(usize, i);
try writer.flush();
const ref = "\xdc\x00d\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc";
const gen = buf[0..writer.end];
try std.testing.expectEqualSlices(u8, ref, gen);
}
}
// Unpacking
pub fn MsgpackHandler(CtxT: type) type {
return struct {
const T = CtxT;
nil: ?fn (ud: *CtxT) anyerror!void = null,
bool: ?fn (ud: *CtxT, v: bool) anyerror!void = null,
uint: ?fn (ud: *CtxT, v: u64) anyerror!void = null,
int: ?fn (ud: *CtxT, v: i64) anyerror!void = null,
float: ?fn (ud: *CtxT, v: f64) anyerror!void = null,
str: ?fn (ud: *CtxT, str: []const u8) anyerror!void = null,
mapBegin: ?fn (ud: *CtxT, len: usize) anyerror!void = null,
mapField: ?fn (ud: *CtxT, key: []const u8) anyerror!void = null,
mapEnd: ?fn (ud: *CtxT) anyerror!void = null,
arrayBegin: ?fn (ud: *CtxT, len: usize) anyerror!void = null,
arrayEnd: ?fn (ud: *CtxT) anyerror!void = null,
};
}
const up_log = std.log.scoped(.msgpack_unpacker);
fn errNoImpl() error{NotImplemented} {
std.debug.dumpCurrentStackTrace(null);
up_log.err("Not implemented", .{});
return error.NotImplemented;
}
const MsgpackError = error{
InvalidHeader,
MapKeyIsNotStr,
};
pub fn Unpacker(CtxT: type, comptime handler: MsgpackHandler(CtxT)) type {
return struct {
const Self = @This();
ctx: CtxT,
reader: *std.io.Reader,
alloc: std.mem.Allocator,
fn isShortStr(header: u8) bool {
return header == 0xd9 or (header >= 0xa0 and header <= 0xbf);
}
fn unpackStr(self: *Self, header: u8, out_buf: []u8) !struct { []u8, u64 } {
var read: usize = 0;
var str_len: usize = 0;
switch (header) {
// fixstr
0xa0...0xbf => {
str_len = @intCast(header & 0x1f);
},
// str 8
0xd9 => {
var buf: u8 = 0;
try self.reader.readSliceAll(@ptrCast(&buf));
read += 1;
str_len = @intCast(buf);
},
// str 16
0xda => {
var buf: [2]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
str_len = (@as(u64, @intCast(buf[0])) << 8) | @as(u64, @intCast(buf[1]));
},
// str 32
0xdb => {
var buf: [4]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
str_len = (@as(u64, @intCast(buf[0])) << 24) | (@as(u64, @intCast(buf[1])) << 16) | (@as(u64, @intCast(buf[2])) << 8) | @as(u64, @intCast(buf[3]));
},
else => return error.InvalidHeader,
}
if (str_len > out_buf.len) {
return error.OutOfMemory;
}
try self.reader.readSliceAll(out_buf[0..str_len]);
read += str_len;
return .{ out_buf[0..str_len], read };
}
fn unpackStrAlloc(self: *Self, header: u8) !struct { []u8, u64 } {
var read: usize = 0;
var str_len: usize = 0;
switch (header) {
// fixstr
0xa0...0xbf => {
str_len = @intCast(header & 0x1f);
},
// str 8
0xd9 => {
var buf: u8 = 0;
try self.reader.readSliceAll(@ptrCast(&buf));
read += 1;
str_len = @intCast(buf);
},
// str 16
0xda => {
var buf: [2]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
str_len = (@as(u64, @intCast(buf[0])) << 8) | @as(u64, @intCast(buf[1]));
},
// str 32
0xdb => {
var buf: [4]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
str_len = (@as(u64, @intCast(buf[0])) << 24) | (@as(u64, @intCast(buf[1])) << 16) | (@as(u64, @intCast(buf[2])) << 8) | @as(u64, @intCast(buf[3]));
},
else => return error.InvalidHeader,
}
const str = try self.alloc.alloc(u8, str_len);
try self.reader.readSliceAll(str);
read += str_len;
return .{ str, read };
}
fn walkMap(self: *Self, header: u8) !u64 {
var read: u64 = 0;
var map_len: usize = 0;
switch (header) {
// fixmap
0x80...0x8f => map_len = @intCast(header & 0x0f),
// map 16
0xde => {
var buf: [2]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
map_len = (@as(u64, @intCast(buf[0])) << 8) | @as(u64, @intCast(buf[1]));
},
// map 32
0xdf => {
var buf: [4]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
map_len = (@as(u64, @intCast(buf[0])) << 24) | (@as(u64, @intCast(buf[1])) << 16) | (@as(u64, @intCast(buf[2])) << 8) | @as(u64, @intCast(buf[3]));
},
else => unreachable,
}
try handler.mapBegin.?(&self.ctx, map_len);
for (0..map_len) |idx| {
// Read key
var key_header: u8 = 0;
try self.reader.readSliceAll(@ptrCast(&key_header));
read += 1;
if (Self.isShortStr(key_header)) {
var buf: [256]u8 = undefined;
const key, const key_n = self.unpackStr(key_header, &buf) catch |err| {
if (err == MsgpackError.InvalidHeader) {
std.log.err("Map entry #{}, key header {x} is not str.", .{ idx, key_header });
return MsgpackError.MapKeyIsNotStr;
}
return err;
};
read += key_n;
try handler.mapField.?(&self.ctx, key);
} else {
const key, const key_n = self.unpackStrAlloc(key_header) catch |err| {
if (err == MsgpackError.InvalidHeader) {
std.log.err("Map entry #{}, key header {x} is not str.", .{ idx, key_header });
return MsgpackError.MapKeyIsNotStr;
}
return err;
};
defer self.alloc.free(key);
read += key_n;
try handler.mapField.?(&self.ctx, key);
}
// Process value
read += try self.next(1);
}
if (handler.mapEnd) |f| {
try f(&self.ctx);
}
return read;
}
fn walkArr(self: *Self, header: u8) !u64 {
var read: u64 = 0;
var arr_len: usize = 0;
switch (header) {
// fixarray
0x90...0x9f => arr_len = @intCast(header & 0x0f),
// array 16
0xdc => {
var buf: [2]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
arr_len = (@as(u64, @intCast(buf[0])) << 8) | @as(u64, @intCast(buf[1]));
},
// array 32
0xdd => {
var buf: [4]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
arr_len = (@as(u64, @intCast(buf[0])) << 24) | (@as(u64, @intCast(buf[1])) << 16) | (@as(u64, @intCast(buf[2])) << 8) | @as(u64, @intCast(buf[3]));
},
else => unreachable,
}
try (handler.arrayBegin orelse return errNoImpl())(&self.ctx, arr_len);
for (0..arr_len) |_| {
read += try self.next(1);
}
if (handler.arrayEnd) |f| {
try f(&self.ctx);
}
return read;
}
// fn unpackMapContent(self: *Self, n: usize) !u64 {
// for (0..n) |_| {
// var str_header: u8 = 0;
// }
// }
pub fn next(self: *Self, limit_n: usize) anyerror!u64 {
var read: u64 = 0;
for (0..limit_n) |_| {
var header: u8 = 0;
try self.reader.readSliceAll(@ptrCast(&header));
read += 1;
try switch (header) {
// positive fixint
0x00...0x7f => if (handler.uint) |h_uint| {
_ = try h_uint(&self.ctx, @intCast(header));
} else return errNoImpl(),
// fixmap, map 16, map 32
0x80...0x8f, 0xde, 0xdf => {
if (handler.mapBegin == null or handler.mapField == null) return errNoImpl();
read += try self.walkMap(header);
},
// fixarray, arr 16, arr 32
0x90...0x9f, 0xdc, 0xdd => read += try self.walkArr(header),
// fixstr, str 8
0xa0...0xbf, 0xd9 => if (handler.str) |h_str| {
var buf: [256]u8 = undefined;
const str, const n = try self.unpackStr(header, &buf);
_ = try h_str(&self.ctx, str);
read += n;
} else return errNoImpl(),
// str 16, str 32
0xda, 0xdb => if (handler.str) |h_str| {
const str, const n = try self.unpackStrAlloc(header);
defer self.alloc.free(str);
_ = try h_str(&self.ctx, str);
read += n;
} else return errNoImpl(),
// nil
0xc0 => (handler.nil orelse return errNoImpl())(&self.ctx),
// (never used)
0xc1 => return error.InvalidHeader,
// false
0xc2 => (handler.bool orelse return errNoImpl())(&self.ctx, false),
// true
0xc3 => (handler.bool orelse return errNoImpl())(&self.ctx, true),
// bin 8, bin 16, bin 32
0xc4, 0xc5, 0xc6 => return errNoImpl(),
// ext 8, ext 16, ext 32
0xc7, 0xc8, 0xc9 => return errNoImpl(),
// float 32
0xca => if (handler.float) |h_float| {
var buf: [4]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
const u = (@as(u32, @intCast(buf[0])) << 24) | (@as(u32, @intCast(buf[1])) << 16) | (@as(u32, @intCast(buf[2])) << 8) | @as(u32, @intCast(buf[3]));
try h_float(&self.ctx, @floatCast(@as(f32, @bitCast(u))));
} else return errNoImpl(),
// float 64
0xcb => if (handler.float) |h_float| {
var buf: [8]u8 = undefined;
try self.reader.readSliceAll(&buf);
read += buf.len;
var u: u64 = 0;
inline for (0..8) |i| {
u |= (@as(u64, @intCast(buf[i])) << (56 - i * 8));
}
try h_float(&self.ctx, @floatCast(@as(f64, @bitCast(u))));
} else return errNoImpl(),
// uint 8, 16, 32, 64
inline 0xcc...0xcf => |h| if (handler.uint) |h_uint| {
const nb = 1 << (h - 0xcc);
var buf: [8]u8 = undefined;
read += try self.reader.readSliceShort(buf[0..nb]);
const UType = @Type(std.builtin.Type{ .int = .{ .bits = nb * 8, .signedness = .unsigned } });
var u: UType = 0;
inline for (0..nb) |i| {
u |= (@as(UType, @intCast(buf[i])) << ((nb - 1 - i) * 8));
}
try h_uint(&self.ctx, @intCast(u));
} else return errNoImpl(),
// int 8, 16, 32, 64
inline 0xd0...0xd3 => |h| if (handler.int) |h_int| {
const nb = 1 << (h - 0xd0);
var buf: [8]u8 = undefined;
read += try self.reader.readSliceShort(buf[0..nb]);
const UType = @Type(std.builtin.Type{ .int = .{ .bits = nb * 8, .signedness = .unsigned } });
var u: UType = 0;
inline for (0..nb) |i| {
u |= (@as(UType, @intCast(buf[i])) << ((nb - 1 - i) * 8));
}
try h_int(&self.ctx, @bitCast(@as(u64, @intCast(u))));
} else return errNoImpl(),
// fixext 1, 2, 4, 8, 16
0xd4...0xd8 => return errNoImpl(),
// negative fixint
0xe0...0xff => {},
};
}
return read;
}
pub fn init(reader: std.io.Reader, ctx: CtxT, alloc: std.mem.Allocator) Self {
return .{
.reader = reader,
.ctx = ctx,
.alloc = alloc,
};
}
};
}
test "test unpacker" {
const ref = "\x84\xa3abc\xc2\xa3def7\xb4%%%%%%%%%%%%%%%%%%%%\xce\xba\xad\xf0\r\xb1this is an \xe5\x90\x91\xe9\x87\x8f\x93\xcb\xbf\xec\xcc\xcc\xcc\xcc\xcc\xcd\xcb?\xe0Q\xeb\x85\x1e\xb8R\xcb?\xd5UUUUUU";
var reader = std.io.Reader.fixed(ref);
var gpa = std.heap.DebugAllocator(.{}).init;
const alloc = gpa.allocator();
const FmtCtx = struct {
const Self = @This();
writer: *std.io.Writer,
pub fn f_bool(s: *Self, v: bool) !void {
if (v) {
_ = try s.writer.writeAll("true, ");
} else {
_ = try s.writer.writeAll("false, ");
}
}
pub fn fmtInt(s: *Self, v: i64) !void {
try s.writer.print("{}, ", .{v});
}
pub fn fmtUint(s: *Self, v: u64) !void {
try s.writer.print("{}, ", .{v});
}
pub fn fmtFloat(s: *Self, v: f64) !void {
try s.writer.print("{}, ", .{v});
}
pub fn mapBegin(s: *Self, _: usize) !void {
try s.writer.writeByte('{');
}
pub fn mapField(s: *Self, key: []const u8) !void {
try s.writer.print("\"{s}\" = ", .{key});
}
pub fn mapEnd(s: *Self) !void {
try s.writer.writeAll("}, ");
}
pub fn arrayBegin(s: *Self, _: usize) !void {
try s.writer.writeByte('{');
}
pub fn arrayEnd(s: *Self) !void {
try s.writer.writeAll("}, ");
}
};
var out_buf: [1024]u8 = std.mem.zeroes([1024]u8);
var writer = std.io.Writer.fixed(&out_buf);
var unpacker: Unpacker(
FmtCtx,
.{
.bool = FmtCtx.f_bool,
.int = FmtCtx.fmtInt,
.uint = FmtCtx.fmtUint,
.float = FmtCtx.fmtFloat,
.mapBegin = FmtCtx.mapBegin,
.mapField = FmtCtx.mapField,
.arrayBegin = FmtCtx.arrayBegin,
.arrayEnd = FmtCtx.arrayEnd,
.mapEnd = FmtCtx.mapEnd,
},
) = .{ .reader = &reader, .ctx = .{ .writer = &writer }, .alloc = alloc };
const u = try unpacker.next(1);
try std.testing.expectEqual(ref.len, u);
const should_unpack =
"{\"abc\" = false, \"def\" = 55, \"%%%%%%%%%%%%%%%%%%%%\" = 3131961357, \"this is an 向量\" = {-0.9, 0.51, 0.3333333333333333, }, }, ";
try writer.flush();
try std.testing.expectEqualStrings(should_unpack, out_buf[0..writer.end]);
try std.testing.expect(gpa.deinit() == .ok);
}
// Streaming Scanner (tokenizer), inspired by std.json.Scanner
// Provides a forward-only token stream over MessagePack without building a DOM.
pub const Scanner = struct {
const Self = @This();
pub const TokenTag = enum {
end,
nil,
boolean,
uint,
int,
float,
string,
array_begin,
array_end,
map_begin,
map_key,
map_end,
};
pub const Token = union(TokenTag) {
end: void,
nil: void,
boolean: bool,
uint: u64,
int: i64,
float: f64,
string: []const u8,
array_begin: usize,
array_end: void,
map_begin: usize,
map_key: []const u8,
map_end: void,
};
const FrameKind = enum { array, map };
const Frame = struct {
kind: FrameKind,
remaining: usize, // for map: remaining key-value pairs
expect_key: bool, // only meaningful for map
};
reader: *std.io.Reader,
alloc: std.mem.Allocator,
// Small-string scratch buffer; returned slices are valid until next call
scratch: [256]u8 = undefined,
// If the last returned string was heap-allocated, keep it here for release()
last_alloc: ?[]u8 = null,
// Simple manual stack to track nested containers
frames: [64]Frame = undefined,
depth: usize = 0,
// If a container has just become complete, we synthesize a closing token
// on the next call without reading more bytes
pending_close: enum { none, array_end, map_end } = .none,
// Internal helpers
fn clearLastAlloc(self: *Self) void {
if (self.last_alloc) |buf| {
self.alloc.free(buf);
self.last_alloc = null;
}
}
fn pushArray(self: *Self, len: usize) void {
std.debug.assert(self.depth < self.frames.len);
self.frames[self.depth] = .{ .kind = .array, .remaining = len, .expect_key = false };
self.depth += 1;
}
fn pushMap(self: *Self, len: usize) void {
std.debug.assert(self.depth < self.frames.len);
self.frames[self.depth] = .{ .kind = .map, .remaining = len, .expect_key = true };
self.depth += 1;
}
fn parentNoteValueSeen(self: *Self) void {
if (self.depth == 0) return;
const top = &self.frames[self.depth - 1];
switch (top.kind) {
.array => {
std.debug.assert(top.remaining > 0);
top.remaining -= 1;