-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
1363 lines (1259 loc) · 25.6 KB
/
read.go
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
package msgp
import (
"io"
"math"
"sync"
"time"
"github.com/angenal/gf/internal/fwd"
)
// where we keep old *Readers
var readerPool = sync.Pool{New: func() interface{} { return &Reader{} }}
// Type is a MessagePack wire type,
// including this package's built-in
// extension types.
type Type byte
// MessagePack Types
//
// The zero value of Type
// is InvalidType.
const (
InvalidType Type = iota
// MessagePack built-in types
StrType
BinType
MapType
ArrayType
Float64Type
Float32Type
BoolType
IntType
UintType
NilType
ExtensionType
// pseudo-types provided
// by extensions
Complex64Type
Complex128Type
TimeType
_maxtype
)
// String implements fmt.Stringer
func (t Type) String() string {
switch t {
case StrType:
return "str"
case BinType:
return "bin"
case MapType:
return "map"
case ArrayType:
return "array"
case Float64Type:
return "float64"
case Float32Type:
return "float32"
case BoolType:
return "bool"
case UintType:
return "uint"
case IntType:
return "int"
case ExtensionType:
return "ext"
case NilType:
return "nil"
default:
return "<invalid>"
}
}
func freeR(m *Reader) {
readerPool.Put(m)
}
// Unmarshaler is the interface fulfilled
// by objects that know how to unmarshal
// themselves from MessagePack.
// UnmarshalMsg unmarshals the object
// from binary, returing any leftover
// bytes and any errors encountered.
type Unmarshaler interface {
UnmarshalMsg([]byte) ([]byte, error)
}
// Decodable is the interface fulfilled
// by objects that know how to read
// themselves from a *Reader.
type Decodable interface {
DecodeMsg(*Reader) error
}
// Decode decodes 'd' from 'r'.
func Decode(r io.Reader, d Decodable) error {
rd := NewReader(r)
err := d.DecodeMsg(rd)
freeR(rd)
return err
}
// NewReader returns a *Reader that
// reads from the provided reader. The
// reader will be buffered.
func NewReader(r io.Reader) *Reader {
p := readerPool.Get().(*Reader)
if p.R == nil {
p.R = fwd.NewReader(r)
} else {
p.R.Reset(r)
}
return p
}
// NewReaderSize returns a *Reader with a buffer of the given size.
// (This is vastly preferable to passing the decoder a reader that is already buffered.)
func NewReaderSize(r io.Reader, sz int) *Reader {
return &Reader{R: fwd.NewReaderSize(r, sz)}
}
// NewReaderBuf returns a *Reader with a provided buffer.
func NewReaderBuf(r io.Reader, buf []byte) *Reader {
return &Reader{R: fwd.NewReaderBuf(r, buf)}
}
// Reader wraps an io.Reader and provides
// methods to read MessagePack-encoded values
// from it. Readers are buffered.
type Reader struct {
// R is the buffered reader
// that the Reader uses
// to decode MessagePack.
// The Reader itself
// is stateless; all the
// buffering is done
// within R.
R *fwd.Reader
scratch []byte
}
// Read implements `io.Reader`
func (m *Reader) Read(p []byte) (int, error) {
return m.R.Read(p)
}
// CopyNext reads the next object from m without decoding it and writes it to w.
// It avoids unnecessary copies internally.
func (m *Reader) CopyNext(w io.Writer) (int64, error) {
sz, o, err := getNextSize(m.R)
if err != nil {
return 0, err
}
var n int64
// Opportunistic optimization: if we can fit the whole thing in the m.R
// buffer, then just get a pointer to that, and pass it to w.Write,
// avoiding an allocation.
if int(sz) <= m.R.BufferSize() {
var nn int
var buf []byte
buf, err = m.R.Next(int(sz))
if err != nil {
if err == io.ErrUnexpectedEOF {
err = ErrShortBytes
}
return 0, err
}
nn, err = w.Write(buf)
n += int64(nn)
} else {
// Fall back to io.CopyN.
// May avoid allocating if w is a ReaderFrom (e.g. bytes.Buffer)
n, err = io.CopyN(w, m.R, int64(sz))
if err == io.ErrUnexpectedEOF {
err = ErrShortBytes
}
}
if err != nil {
return n, err
} else if n < int64(sz) {
return n, io.ErrShortWrite
}
// for maps and slices, read elements
for x := uintptr(0); x < o; x++ {
var n2 int64
n2, err = m.CopyNext(w)
if err != nil {
return n, err
}
n += n2
}
return n, nil
}
// ReadFull implements `io.ReadFull`
func (m *Reader) ReadFull(p []byte) (int, error) {
return m.R.ReadFull(p)
}
// Reset resets the underlying reader.
func (m *Reader) Reset(r io.Reader) { m.R.Reset(r) }
// Buffered returns the number of bytes currently in the read buffer.
func (m *Reader) Buffered() int { return m.R.Buffered() }
// BufferSize returns the capacity of the read buffer.
func (m *Reader) BufferSize() int { return m.R.BufferSize() }
// NextType returns the next object type to be decoded.
func (m *Reader) NextType() (Type, error) {
p, err := m.R.Peek(1)
if err != nil {
return InvalidType, err
}
t := getType(p[0])
if t == InvalidType {
return t, InvalidPrefixError(p[0])
}
if t == ExtensionType {
v, err := m.peekExtensionType()
if err != nil {
return InvalidType, err
}
switch v {
case Complex64Extension:
return Complex64Type, nil
case Complex128Extension:
return Complex128Type, nil
case TimeExtension:
return TimeType, nil
}
}
return t, nil
}
// IsNil returns whether or not
// the next byte is a null messagepack byte
func (m *Reader) IsNil() bool {
p, err := m.R.Peek(1)
return err == nil && p[0] == mnil
}
// getNextSize returns the size of the next object on the wire.
// returns (obj size, obj elements, error)
// only maps and arrays have non-zero obj elements
// for maps and arrays, obj size does not include elements
//
// use uintptr b/c it's guaranteed to be large enough
// to hold whatever we can fit in memory.
func getNextSize(r *fwd.Reader) (uintptr, uintptr, error) {
b, err := r.Peek(1)
if err != nil {
return 0, 0, err
}
lead := b[0]
spec := &sizes[lead]
size, mode := spec.size, spec.extra
if size == 0 {
return 0, 0, InvalidPrefixError(lead)
}
if mode >= 0 {
return uintptr(size), uintptr(mode), nil
}
b, err = r.Peek(int(size))
if err != nil {
return 0, 0, err
}
switch mode {
case extra8:
return uintptr(size) + uintptr(b[1]), 0, nil
case extra16:
return uintptr(size) + uintptr(big.Uint16(b[1:])), 0, nil
case extra32:
return uintptr(size) + uintptr(big.Uint32(b[1:])), 0, nil
case map16v:
return uintptr(size), 2 * uintptr(big.Uint16(b[1:])), nil
case map32v:
return uintptr(size), 2 * uintptr(big.Uint32(b[1:])), nil
case array16v:
return uintptr(size), uintptr(big.Uint16(b[1:])), nil
case array32v:
return uintptr(size), uintptr(big.Uint32(b[1:])), nil
default:
return 0, 0, fatal
}
}
// Skip skips over the next object, regardless of
// its type. If it is an array or map, the whole array
// or map will be skipped.
func (m *Reader) Skip() error {
var (
v uintptr // bytes
o uintptr // objects
err error
p []byte
)
// we can use the faster
// method if we have enough
// buffered data
if m.R.Buffered() >= 5 {
p, err = m.R.Peek(5)
if err != nil {
return err
}
v, o, err = getSize(p)
if err != nil {
return err
}
} else {
v, o, err = getNextSize(m.R)
if err != nil {
return err
}
}
// 'v' is always non-zero
// if err == nil
_, err = m.R.Skip(int(v))
if err != nil {
return err
}
// for maps and slices, skip elements
for x := uintptr(0); x < o; x++ {
err = m.Skip()
if err != nil {
return err
}
}
return nil
}
// ReadMapHeader reads the next object
// as a map header and returns the size
// of the map and the number of bytes written.
// It will return a TypeError{} if the next
// object is not a map.
func (m *Reader) ReadMapHeader() (sz uint32, err error) {
var p []byte
var lead byte
p, err = m.R.Peek(1)
if err != nil {
return
}
lead = p[0]
if isfixmap(lead) {
sz = uint32(rfixmap(lead))
_, err = m.R.Skip(1)
return
}
switch lead {
case mmap16:
p, err = m.R.Next(3)
if err != nil {
return
}
sz = uint32(big.Uint16(p[1:]))
return
case mmap32:
p, err = m.R.Next(5)
if err != nil {
return
}
sz = big.Uint32(p[1:])
return
default:
err = badPrefix(MapType, lead)
return
}
}
// ReadMapKey reads either a 'str' or 'bin' field from
// the reader and returns the value as a []byte. It uses
// scratch for storage if it is large enough.
func (m *Reader) ReadMapKey(scratch []byte) ([]byte, error) {
out, err := m.ReadStringAsBytes(scratch)
if err != nil {
if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
return m.ReadBytes(scratch)
}
return nil, err
}
return out, nil
}
// MapKeyPtr returns a []byte pointing to the contents
// of a valid map key. The key cannot be empty, and it
// must be shorter than the total buffer size of the
// *Reader. Additionally, the returned slice is only
// valid until the next *Reader method call. Users
// should exercise extreme care when using this
// method; writing into the returned slice may
// corrupt future reads.
func (m *Reader) ReadMapKeyPtr() ([]byte, error) {
p, err := m.R.Peek(1)
if err != nil {
return nil, err
}
lead := p[0]
var read int
if isfixstr(lead) {
read = int(rfixstr(lead))
m.R.Skip(1)
goto fill
}
switch lead {
case mstr8, mbin8:
p, err = m.R.Next(2)
if err != nil {
return nil, err
}
read = int(p[1])
case mstr16, mbin16:
p, err = m.R.Next(3)
if err != nil {
return nil, err
}
read = int(big.Uint16(p[1:]))
case mstr32, mbin32:
p, err = m.R.Next(5)
if err != nil {
return nil, err
}
read = int(big.Uint32(p[1:]))
default:
return nil, badPrefix(StrType, lead)
}
fill:
if read == 0 {
return nil, ErrShortBytes
}
return m.R.Next(read)
}
// ReadArrayHeader reads the next object as an
// array header and returns the size of the array
// and the number of bytes read.
func (m *Reader) ReadArrayHeader() (sz uint32, err error) {
var lead byte
var p []byte
p, err = m.R.Peek(1)
if err != nil {
return
}
lead = p[0]
if isfixarray(lead) {
sz = uint32(rfixarray(lead))
_, err = m.R.Skip(1)
return
}
switch lead {
case marray16:
p, err = m.R.Next(3)
if err != nil {
return
}
sz = uint32(big.Uint16(p[1:]))
return
case marray32:
p, err = m.R.Next(5)
if err != nil {
return
}
sz = big.Uint32(p[1:])
return
default:
err = badPrefix(ArrayType, lead)
return
}
}
// ReadNil reads a 'nil' MessagePack byte from the reader
func (m *Reader) ReadNil() error {
p, err := m.R.Peek(1)
if err != nil {
return err
}
if p[0] != mnil {
return badPrefix(NilType, p[0])
}
_, err = m.R.Skip(1)
return err
}
// ReadFloat64 reads a float64 from the reader.
// (If the value on the wire is encoded as a float32,
// it will be up-cast to a float64.)
func (m *Reader) ReadFloat64() (f float64, err error) {
var p []byte
p, err = m.R.Peek(9)
if err != nil {
// we'll allow a coversion from float32 to float64,
// since we don't lose any precision
if err == io.EOF && len(p) > 0 && p[0] == mfloat32 {
ef, err := m.ReadFloat32()
return float64(ef), err
}
return
}
if p[0] != mfloat64 {
// see above
if p[0] == mfloat32 {
ef, err := m.ReadFloat32()
return float64(ef), err
}
err = badPrefix(Float64Type, p[0])
return
}
f = math.Float64frombits(getMuint64(p))
_, err = m.R.Skip(9)
return
}
// ReadFloat32 reads a float32 from the reader
func (m *Reader) ReadFloat32() (f float32, err error) {
var p []byte
p, err = m.R.Peek(5)
if err != nil {
return
}
if p[0] != mfloat32 {
err = badPrefix(Float32Type, p[0])
return
}
f = math.Float32frombits(getMuint32(p))
_, err = m.R.Skip(5)
return
}
// ReadBool reads a bool from the reader
func (m *Reader) ReadBool() (b bool, err error) {
var p []byte
p, err = m.R.Peek(1)
if err != nil {
return
}
switch p[0] {
case mtrue:
b = true
case mfalse:
default:
err = badPrefix(BoolType, p[0])
return
}
_, err = m.R.Skip(1)
return
}
// ReadInt64 reads an int64 from the reader
func (m *Reader) ReadInt64() (i int64, err error) {
var p []byte
var lead byte
p, err = m.R.Peek(1)
if err != nil {
return
}
lead = p[0]
if isfixint(lead) {
i = int64(rfixint(lead))
_, err = m.R.Skip(1)
return
} else if isnfixint(lead) {
i = int64(rnfixint(lead))
_, err = m.R.Skip(1)
return
}
switch lead {
case mint8:
p, err = m.R.Next(2)
if err != nil {
return
}
i = int64(getMint8(p))
return
case muint8:
p, err = m.R.Next(2)
if err != nil {
return
}
i = int64(getMuint8(p))
return
case mint16:
p, err = m.R.Next(3)
if err != nil {
return
}
i = int64(getMint16(p))
return
case muint16:
p, err = m.R.Next(3)
if err != nil {
return
}
i = int64(getMuint16(p))
return
case mint32:
p, err = m.R.Next(5)
if err != nil {
return
}
i = int64(getMint32(p))
return
case muint32:
p, err = m.R.Next(5)
if err != nil {
return
}
i = int64(getMuint32(p))
return
case mint64:
p, err = m.R.Next(9)
if err != nil {
return
}
i = getMint64(p)
return
case muint64:
p, err = m.R.Next(9)
if err != nil {
return
}
u := getMuint64(p)
if u > math.MaxInt64 {
err = UintOverflow{Value: u, FailedBitsize: 64}
return
}
i = int64(u)
return
default:
err = badPrefix(IntType, lead)
return
}
}
// ReadInt32 reads an int32 from the reader
func (m *Reader) ReadInt32() (i int32, err error) {
var in int64
in, err = m.ReadInt64()
if in > math.MaxInt32 || in < math.MinInt32 {
err = IntOverflow{Value: in, FailedBitsize: 32}
return
}
i = int32(in)
return
}
// ReadInt16 reads an int16 from the reader
func (m *Reader) ReadInt16() (i int16, err error) {
var in int64
in, err = m.ReadInt64()
if in > math.MaxInt16 || in < math.MinInt16 {
err = IntOverflow{Value: in, FailedBitsize: 16}
return
}
i = int16(in)
return
}
// ReadInt8 reads an int8 from the reader
func (m *Reader) ReadInt8() (i int8, err error) {
var in int64
in, err = m.ReadInt64()
if in > math.MaxInt8 || in < math.MinInt8 {
err = IntOverflow{Value: in, FailedBitsize: 8}
return
}
i = int8(in)
return
}
// ReadInt reads an int from the reader
func (m *Reader) ReadInt() (i int, err error) {
if smallint {
var in int32
in, err = m.ReadInt32()
i = int(in)
return
}
var in int64
in, err = m.ReadInt64()
i = int(in)
return
}
// ReadUint64 reads a uint64 from the reader
func (m *Reader) ReadUint64() (u uint64, err error) {
var p []byte
var lead byte
p, err = m.R.Peek(1)
if err != nil {
return
}
lead = p[0]
if isfixint(lead) {
u = uint64(rfixint(lead))
_, err = m.R.Skip(1)
return
}
switch lead {
case mint8:
p, err = m.R.Next(2)
if err != nil {
return
}
v := int64(getMint8(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint8:
p, err = m.R.Next(2)
if err != nil {
return
}
u = uint64(getMuint8(p))
return
case mint16:
p, err = m.R.Next(3)
if err != nil {
return
}
v := int64(getMint16(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint16:
p, err = m.R.Next(3)
if err != nil {
return
}
u = uint64(getMuint16(p))
return
case mint32:
p, err = m.R.Next(5)
if err != nil {
return
}
v := int64(getMint32(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint32:
p, err = m.R.Next(5)
if err != nil {
return
}
u = uint64(getMuint32(p))
return
case mint64:
p, err = m.R.Next(9)
if err != nil {
return
}
v := int64(getMint64(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint64:
p, err = m.R.Next(9)
if err != nil {
return
}
u = getMuint64(p)
return
default:
if isnfixint(lead) {
err = UintBelowZero{Value: int64(rnfixint(lead))}
} else {
err = badPrefix(UintType, lead)
}
return
}
}
// ReadUint32 reads a uint32 from the reader
func (m *Reader) ReadUint32() (u uint32, err error) {
var in uint64
in, err = m.ReadUint64()
if in > math.MaxUint32 {
err = UintOverflow{Value: in, FailedBitsize: 32}
return
}
u = uint32(in)
return
}
// ReadUint16 reads a uint16 from the reader
func (m *Reader) ReadUint16() (u uint16, err error) {
var in uint64
in, err = m.ReadUint64()
if in > math.MaxUint16 {
err = UintOverflow{Value: in, FailedBitsize: 16}
return
}
u = uint16(in)
return
}
// ReadUint8 reads a uint8 from the reader
func (m *Reader) ReadUint8() (u uint8, err error) {
var in uint64
in, err = m.ReadUint64()
if in > math.MaxUint8 {
err = UintOverflow{Value: in, FailedBitsize: 8}
return
}
u = uint8(in)
return
}
// ReadUint reads a uint from the reader
func (m *Reader) ReadUint() (u uint, err error) {
if smallint {
var un uint32
un, err = m.ReadUint32()
u = uint(un)
return
}
var un uint64
un, err = m.ReadUint64()
u = uint(un)
return
}
// ReadByte is analogous to ReadUint8.
//
// NOTE: this is *not* an implementation
// of io.ByteReader.
func (m *Reader) ReadByte() (b byte, err error) {
var in uint64
in, err = m.ReadUint64()
if in > math.MaxUint8 {
err = UintOverflow{Value: in, FailedBitsize: 8}
return
}
b = byte(in)
return
}
// ReadBytes reads a MessagePack 'bin' object
// from the reader and returns its value. It may
// use 'scratch' for storage if it is non-nil.
func (m *Reader) ReadBytes(scratch []byte) (b []byte, err error) {
var p []byte
var lead byte
p, err = m.R.Peek(2)
if err != nil {
return
}
lead = p[0]
var read int64
switch lead {
case mbin8:
read = int64(p[1])
m.R.Skip(2)
case mbin16:
p, err = m.R.Next(3)
if err != nil {
return
}
read = int64(big.Uint16(p[1:]))
case mbin32:
p, err = m.R.Next(5)
if err != nil {
return
}
read = int64(big.Uint32(p[1:]))
default:
err = badPrefix(BinType, lead)
return
}
if int64(cap(scratch)) < read {
b = make([]byte, read)
} else {
b = scratch[0:read]
}
_, err = m.R.ReadFull(b)
return
}
// ReadBytesHeader reads the size header
// of a MessagePack 'bin' object. The user
// is responsible for dealing with the next
// 'sz' bytes from the reader in an application-specific
// way.
func (m *Reader) ReadBytesHeader() (sz uint32, err error) {
var p []byte
p, err = m.R.Peek(1)
if err != nil {
return
}
switch p[0] {
case mbin8:
p, err = m.R.Next(2)
if err != nil {
return
}
sz = uint32(p[1])
return
case mbin16:
p, err = m.R.Next(3)
if err != nil {
return
}
sz = uint32(big.Uint16(p[1:]))
return
case mbin32:
p, err = m.R.Next(5)
if err != nil {
return
}
sz = uint32(big.Uint32(p[1:]))
return
default:
err = badPrefix(BinType, p[0])
return
}
}
// ReadExactBytes reads a MessagePack 'bin'-encoded
// object off of the wire into the provided slice. An
// ArrayError will be returned if the object is not
// exactly the length of the input slice.
func (m *Reader) ReadExactBytes(into []byte) error {
p, err := m.R.Peek(2)
if err != nil {
return err
}
lead := p[0]
var read int64 // bytes to read
var skip int // prefix size to skip
switch lead {
case mbin8:
read = int64(p[1])
skip = 2
case mbin16:
p, err = m.R.Peek(3)
if err != nil {
return err
}
read = int64(big.Uint16(p[1:]))
skip = 3
case mbin32:
p, err = m.R.Peek(5)
if err != nil {
return err
}
read = int64(big.Uint32(p[1:]))
skip = 5
default:
return badPrefix(BinType, lead)
}
if read != int64(len(into)) {
return ArrayError{Wanted: uint32(len(into)), Got: uint32(read)}
}
m.R.Skip(skip)
_, err = m.R.ReadFull(into)