1
2
3
4 package codec
5
6 import (
7 "encoding"
8 "errors"
9 "io"
10 "math"
11 "reflect"
12 "strconv"
13 "time"
14 )
15
16 const msgBadDesc = "unrecognized descriptor byte"
17
18 const (
19 decDefMaxDepth = 1024
20 decDefChanCap = 64
21 decScratchByteArrayLen = (8 + 2 + 2 + 1) * 8
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 decFailNonEmptyIntf = false
37
38
39
40
41
42
43
44 decUseTransient = true
45 )
46
47 var (
48 errNeedMapOrArrayDecodeToStruct = errors.New("only encoded map or array can decode into struct")
49 errCannotDecodeIntoNil = errors.New("cannot decode into nil")
50
51 errExpandSliceCannotChange = errors.New("expand slice: cannot change")
52
53 errDecoderNotInitialized = errors.New("Decoder not initialized")
54
55 errDecUnreadByteNothingToRead = errors.New("cannot unread - nothing has been read")
56 errDecUnreadByteLastByteNotRead = errors.New("cannot unread - last byte has not been read")
57 errDecUnreadByteUnknown = errors.New("cannot unread - reason unknown")
58 errMaxDepthExceeded = errors.New("maximum decoding depth exceeded")
59 )
60
61
62
63 type decByteState uint8
64
65 const (
66 decByteStateNone decByteState = iota
67 decByteStateZerocopy
68 decByteStateReuseBuf
69
70 )
71
72 type decNotDecodeableReason uint8
73
74 const (
75 decNotDecodeableReasonUnknown decNotDecodeableReason = iota
76 decNotDecodeableReasonBadKind
77 decNotDecodeableReasonNonAddrValue
78 decNotDecodeableReasonNilReference
79 )
80
81 type decDriver interface {
82
83 CheckBreak() bool
84
85
86
87
88
89 TryNil() bool
90
91
92
93
94
95
96 ContainerType() (vt valueType)
97
98
99
100
101
102
103
104
105
106
107
108
109 DecodeNaked()
110
111 DecodeInt64() (i int64)
112 DecodeUint64() (ui uint64)
113
114 DecodeFloat64() (f float64)
115 DecodeBool() (b bool)
116
117
118
119
120
121
122
123 DecodeStringAsBytes() (v []byte)
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 DecodeBytes(in []byte) (out []byte)
141
142
143
144 DecodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext)
145
146
147 DecodeTime() (t time.Time)
148
149
150
151
152 ReadArrayStart() int
153
154
155
156
157 ReadMapStart() int
158
159 reset()
160
161
162
163
164
165
166
167
168
169
170
171
172 nextValueBytes(start []byte) []byte
173
174
175 descBd() string
176
177 decoder() *Decoder
178
179 driverStateManager
180 decNegintPosintFloatNumber
181 }
182
183 type decDriverContainerTracker interface {
184 ReadArrayElem()
185 ReadMapElemKey()
186 ReadMapElemValue()
187 ReadArrayEnd()
188 ReadMapEnd()
189 }
190
191 type decNegintPosintFloatNumber interface {
192 decInteger() (ui uint64, neg, ok bool)
193 decFloat() (f float64, ok bool)
194 }
195
196 type decDriverNoopNumberHelper struct{}
197
198 func (x decDriverNoopNumberHelper) decInteger() (ui uint64, neg, ok bool) {
199 panic("decInteger unsupported")
200 }
201 func (x decDriverNoopNumberHelper) decFloat() (f float64, ok bool) { panic("decFloat unsupported") }
202
203 type decDriverNoopContainerReader struct{}
204
205
206
207 func (x decDriverNoopContainerReader) ReadArrayEnd() {}
208 func (x decDriverNoopContainerReader) ReadMapEnd() {}
209 func (x decDriverNoopContainerReader) CheckBreak() (v bool) { return }
210
211
212 type DecodeOptions struct {
213
214
215
216 MapType reflect.Type
217
218
219
220 SliceType reflect.Type
221
222
223
224
225
226
227
228
229 MaxInitLen int
230
231
232
233
234 ReaderBufferSize int
235
236
237
238 MaxDepth int16
239
240
241
242 ErrorIfNoField bool
243
244
245
246
247 ErrorIfNoArrayExpand bool
248
249
250 SignedInteger bool
251
252
253
254
255
256
257
258
259
260
261
262
263
264 MapValueReset bool
265
266
267
268
269 SliceElementReset bool
270
271
272
273
274
275
276
277
278
279
280
281
282 InterfaceReset bool
283
284
285
286
287
288
289
290
291
292
293
294 InternString bool
295
296
297
298
299
300
301
302
303
304 PreferArrayOverSlice bool
305
306
307
308
309
310
311
312
313
314 DeleteOnNilMapValue bool
315
316
317
318 RawToString bool
319
320
321
322
323
324
325
326
327
328
329
330
331 ZeroCopy bool
332
333
334
335
336
337 PreferPointerForStructOrArray bool
338
339
340
341
342
343 ValidateUnicode bool
344 }
345
346
347
348 func (d *Decoder) rawExt(f *codecFnInfo, rv reflect.Value) {
349 d.d.DecodeExt(rv2i(rv), f.ti.rt, 0, nil)
350 }
351
352 func (d *Decoder) ext(f *codecFnInfo, rv reflect.Value) {
353 d.d.DecodeExt(rv2i(rv), f.ti.rt, f.xfTag, f.xfFn)
354 }
355
356 func (d *Decoder) selferUnmarshal(f *codecFnInfo, rv reflect.Value) {
357 rv2i(rv).(Selfer).CodecDecodeSelf(d)
358 }
359
360 func (d *Decoder) binaryUnmarshal(f *codecFnInfo, rv reflect.Value) {
361 bm := rv2i(rv).(encoding.BinaryUnmarshaler)
362 xbs := d.d.DecodeBytes(nil)
363 fnerr := bm.UnmarshalBinary(xbs)
364 d.onerror(fnerr)
365 }
366
367 func (d *Decoder) textUnmarshal(f *codecFnInfo, rv reflect.Value) {
368 tm := rv2i(rv).(encoding.TextUnmarshaler)
369 fnerr := tm.UnmarshalText(d.d.DecodeStringAsBytes())
370 d.onerror(fnerr)
371 }
372
373 func (d *Decoder) jsonUnmarshal(f *codecFnInfo, rv reflect.Value) {
374 d.jsonUnmarshalV(rv2i(rv).(jsonUnmarshaler))
375 }
376
377 func (d *Decoder) jsonUnmarshalV(tm jsonUnmarshaler) {
378
379 var bs0 = []byte{}
380 if !d.bytes {
381 bs0 = d.blist.get(256)
382 }
383 bs := d.d.nextValueBytes(bs0)
384 fnerr := tm.UnmarshalJSON(bs)
385 if !d.bytes {
386 d.blist.put(bs)
387 if !byteSliceSameData(bs0, bs) {
388 d.blist.put(bs0)
389 }
390 }
391 d.onerror(fnerr)
392 }
393
394 func (d *Decoder) kErr(f *codecFnInfo, rv reflect.Value) {
395 d.errorf("no decoding function defined for kind %v", rv.Kind())
396 }
397
398 func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
399 rvSetBytes(rv, d.rawBytes())
400 }
401
402 func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
403 rvSetString(rv, d.stringZC(d.d.DecodeStringAsBytes()))
404 }
405
406 func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
407 rvSetBool(rv, d.d.DecodeBool())
408 }
409
410 func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
411 rvSetTime(rv, d.d.DecodeTime())
412 }
413
414 func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
415 rvSetFloat32(rv, d.decodeFloat32())
416 }
417
418 func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
419 rvSetFloat64(rv, d.d.DecodeFloat64())
420 }
421
422 func (d *Decoder) kComplex64(f *codecFnInfo, rv reflect.Value) {
423 rvSetComplex64(rv, complex(d.decodeFloat32(), 0))
424 }
425
426 func (d *Decoder) kComplex128(f *codecFnInfo, rv reflect.Value) {
427 rvSetComplex128(rv, complex(d.d.DecodeFloat64(), 0))
428 }
429
430 func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
431 rvSetInt(rv, int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize)))
432 }
433
434 func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
435 rvSetInt8(rv, int8(chkOvf.IntV(d.d.DecodeInt64(), 8)))
436 }
437
438 func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
439 rvSetInt16(rv, int16(chkOvf.IntV(d.d.DecodeInt64(), 16)))
440 }
441
442 func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
443 rvSetInt32(rv, int32(chkOvf.IntV(d.d.DecodeInt64(), 32)))
444 }
445
446 func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
447 rvSetInt64(rv, d.d.DecodeInt64())
448 }
449
450 func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
451 rvSetUint(rv, uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
452 }
453
454 func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
455 rvSetUintptr(rv, uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
456 }
457
458 func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
459 rvSetUint8(rv, uint8(chkOvf.UintV(d.d.DecodeUint64(), 8)))
460 }
461
462 func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
463 rvSetUint16(rv, uint16(chkOvf.UintV(d.d.DecodeUint64(), 16)))
464 }
465
466 func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
467 rvSetUint32(rv, uint32(chkOvf.UintV(d.d.DecodeUint64(), 32)))
468 }
469
470 func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
471 rvSetUint64(rv, d.d.DecodeUint64())
472 }
473
474 func (d *Decoder) kInterfaceNaked(f *codecFnInfo) (rvn reflect.Value) {
475
476
477
478 n := d.naked()
479 d.d.DecodeNaked()
480
481
482
483
484
485
486
487
488 if decFailNonEmptyIntf && f.ti.numMeth > 0 {
489 d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth)
490 }
491 switch n.v {
492 case valueTypeMap:
493 mtid := d.mtid
494 if mtid == 0 {
495 if d.jsms {
496 mtid = mapStrIntfTypId
497 } else {
498 mtid = mapIntfIntfTypId
499 }
500 }
501 if mtid == mapStrIntfTypId {
502 var v2 map[string]interface{}
503 d.decode(&v2)
504 rvn = rv4iptr(&v2).Elem()
505 } else if mtid == mapIntfIntfTypId {
506 var v2 map[interface{}]interface{}
507 d.decode(&v2)
508 rvn = rv4iptr(&v2).Elem()
509 } else if d.mtr {
510 rvn = reflect.New(d.h.MapType)
511 d.decode(rv2i(rvn))
512 rvn = rvn.Elem()
513 } else {
514 rvn = rvZeroAddrK(d.h.MapType, reflect.Map)
515 d.decodeValue(rvn, nil)
516 }
517 case valueTypeArray:
518 if d.stid == 0 || d.stid == intfSliceTypId {
519 var v2 []interface{}
520 d.decode(&v2)
521 rvn = rv4iptr(&v2).Elem()
522 } else if d.str {
523 rvn = reflect.New(d.h.SliceType)
524 d.decode(rv2i(rvn))
525 rvn = rvn.Elem()
526 } else {
527 rvn = rvZeroAddrK(d.h.SliceType, reflect.Slice)
528 d.decodeValue(rvn, nil)
529 }
530 if reflectArrayOfSupported && d.h.PreferArrayOverSlice {
531 rvn = rvGetArray4Slice(rvn)
532 }
533 case valueTypeExt:
534 tag, bytes := n.u, n.l
535 bfn := d.h.getExtForTag(tag)
536 var re = RawExt{Tag: tag}
537 if bytes == nil {
538
539
540 if bfn == nil {
541 d.decode(&re.Value)
542 rvn = rv4iptr(&re).Elem()
543 } else {
544 if bfn.ext == SelfExt {
545 rvn = rvZeroAddrK(bfn.rt, bfn.rt.Kind())
546 d.decodeValue(rvn, d.h.fnNoExt(bfn.rt))
547 } else {
548 rvn = reflect.New(bfn.rt)
549 d.interfaceExtConvertAndDecode(rv2i(rvn), bfn.ext)
550 rvn = rvn.Elem()
551 }
552 }
553 } else {
554
555 if bfn == nil {
556 re.setData(bytes, false)
557 rvn = rv4iptr(&re).Elem()
558 } else {
559 rvn = reflect.New(bfn.rt)
560 if bfn.ext == SelfExt {
561 d.sideDecode(rv2i(rvn), bfn.rt, bytes)
562 } else {
563 bfn.ext.ReadExt(rv2i(rvn), bytes)
564 }
565 rvn = rvn.Elem()
566 }
567 }
568
569 if d.h.PreferPointerForStructOrArray && rvn.CanAddr() {
570 if rk := rvn.Kind(); rk == reflect.Array || rk == reflect.Struct {
571 rvn = rvn.Addr()
572 }
573 }
574 case valueTypeNil:
575
576
577 case valueTypeInt:
578 rvn = n.ri()
579 case valueTypeUint:
580 rvn = n.ru()
581 case valueTypeFloat:
582 rvn = n.rf()
583 case valueTypeBool:
584 rvn = n.rb()
585 case valueTypeString, valueTypeSymbol:
586 rvn = n.rs()
587 case valueTypeBytes:
588 rvn = n.rl()
589 case valueTypeTime:
590 rvn = n.rt()
591 default:
592 halt.errorf("kInterfaceNaked: unexpected valueType: %d", n.v)
593 }
594 return
595 }
596
597 func (d *Decoder) kInterface(f *codecFnInfo, rv reflect.Value) {
598
599
600
601
602
603
604
605
606
607 isnilrv := rvIsNil(rv)
608
609 var rvn reflect.Value
610
611 if d.h.InterfaceReset {
612
613 rvn = d.h.intf2impl(f.ti.rtid)
614 if !rvn.IsValid() {
615 rvn = d.kInterfaceNaked(f)
616 if rvn.IsValid() {
617 rvSetIntf(rv, rvn)
618 } else if !isnilrv {
619 decSetNonNilRV2Zero4Intf(rv)
620 }
621 return
622 }
623 } else if isnilrv {
624
625 rvn = d.h.intf2impl(f.ti.rtid)
626 if !rvn.IsValid() {
627 rvn = d.kInterfaceNaked(f)
628 if rvn.IsValid() {
629 rvSetIntf(rv, rvn)
630 }
631 return
632 }
633 } else {
634
635 rvn = rv.Elem()
636 }
637
638
639
640 canDecode, _ := isDecodeable(rvn)
641
642
643
644
645
646 if !canDecode {
647 rvn2 := d.oneShotAddrRV(rvn.Type(), rvn.Kind())
648 rvSetDirect(rvn2, rvn)
649 rvn = rvn2
650 }
651
652 d.decodeValue(rvn, nil)
653 rvSetIntf(rv, rvn)
654 }
655
656 func decStructFieldKeyNotString(dd decDriver, keyType valueType, b *[decScratchByteArrayLen]byte) (rvkencname []byte) {
657 if keyType == valueTypeInt {
658 rvkencname = strconv.AppendInt(b[:0], dd.DecodeInt64(), 10)
659 } else if keyType == valueTypeUint {
660 rvkencname = strconv.AppendUint(b[:0], dd.DecodeUint64(), 10)
661 } else if keyType == valueTypeFloat {
662 rvkencname = strconv.AppendFloat(b[:0], dd.DecodeFloat64(), 'f', -1, 64)
663 } else {
664 halt.errorf("invalid struct key type: %v", keyType)
665 }
666 return
667 }
668
669 func (d *Decoder) kStructField(si *structFieldInfo, rv reflect.Value) {
670 if d.d.TryNil() {
671 if rv = si.path.field(rv); rv.IsValid() {
672 decSetNonNilRV2Zero(rv)
673 }
674 return
675 }
676 d.decodeValueNoCheckNil(si.path.fieldAlloc(rv), nil)
677 }
678
679 func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) {
680 ctyp := d.d.ContainerType()
681 ti := f.ti
682 var mf MissingFielder
683 if ti.flagMissingFielder {
684 mf = rv2i(rv).(MissingFielder)
685 } else if ti.flagMissingFielderPtr {
686 mf = rv2i(rvAddr(rv, ti.ptr)).(MissingFielder)
687 }
688 if ctyp == valueTypeMap {
689 containerLen := d.mapStart(d.d.ReadMapStart())
690 if containerLen == 0 {
691 d.mapEnd()
692 return
693 }
694 hasLen := containerLen >= 0
695 var name2 []byte
696 if mf != nil {
697 var namearr2 [16]byte
698 name2 = namearr2[:0]
699 }
700 var rvkencname []byte
701 for j := 0; d.containerNext(j, containerLen, hasLen); j++ {
702 d.mapElemKey()
703 if ti.keyType == valueTypeString {
704 rvkencname = d.d.DecodeStringAsBytes()
705 } else {
706 rvkencname = decStructFieldKeyNotString(d.d, ti.keyType, &d.b)
707 }
708 d.mapElemValue()
709 if si := ti.siForEncName(rvkencname); si != nil {
710 d.kStructField(si, rv)
711 } else if mf != nil {
712
713 name2 = append(name2[:0], rvkencname...)
714 var f interface{}
715 d.decode(&f)
716 if !mf.CodecMissingField(name2, f) && d.h.ErrorIfNoField {
717 d.errorf("no matching struct field when decoding stream map with key: %s ", stringView(name2))
718 }
719 } else {
720 d.structFieldNotFound(-1, stringView(rvkencname))
721 }
722 }
723 d.mapEnd()
724 } else if ctyp == valueTypeArray {
725 containerLen := d.arrayStart(d.d.ReadArrayStart())
726 if containerLen == 0 {
727 d.arrayEnd()
728 return
729 }
730
731
732 tisfi := ti.sfi.source()
733 hasLen := containerLen >= 0
734
735
736
737
738 for j := 0; d.containerNext(j, containerLen, hasLen); j++ {
739 d.arrayElem()
740 if j < len(tisfi) {
741 d.kStructField(tisfi[j], rv)
742 } else {
743 d.structFieldNotFound(j, "")
744 }
745 }
746
747 d.arrayEnd()
748 } else {
749 d.onerror(errNeedMapOrArrayDecodeToStruct)
750 }
751 }
752
753 func (d *Decoder) kSlice(f *codecFnInfo, rv reflect.Value) {
754
755
756
757
758
759 ti := f.ti
760 rvCanset := rv.CanSet()
761
762 ctyp := d.d.ContainerType()
763 if ctyp == valueTypeBytes || ctyp == valueTypeString {
764
765 if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) {
766 d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt)
767 }
768 rvbs := rvGetBytes(rv)
769 if !rvCanset {
770
771 rvbs = rvbs[:len(rvbs):len(rvbs)]
772 }
773 bs2 := d.decodeBytesInto(rvbs)
774
775 if !(len(bs2) > 0 && len(bs2) == len(rvbs) && &bs2[0] == &rvbs[0]) {
776 if rvCanset {
777 rvSetBytes(rv, bs2)
778 } else if len(rvbs) > 0 && len(bs2) > 0 {
779 copy(rvbs, bs2)
780 }
781 }
782 return
783 }
784
785 slh, containerLenS := d.decSliceHelperStart()
786
787
788 if containerLenS == 0 {
789 if rvCanset {
790 if rvIsNil(rv) {
791 rvSetDirect(rv, rvSliceZeroCap(ti.rt))
792 } else {
793 rvSetSliceLen(rv, 0)
794 }
795 }
796 slh.End()
797 return
798 }
799
800 rtelem0Mut := !scalarBitset.isset(ti.elemkind)
801 rtelem := ti.elem
802
803 for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
804 rtelem = rtelem.Elem()
805 }
806
807 var fn *codecFn
808
809 var rvChanged bool
810
811 var rv0 = rv
812 var rv9 reflect.Value
813
814 rvlen := rvLenSlice(rv)
815 rvcap := rvCapSlice(rv)
816 hasLen := containerLenS > 0
817 if hasLen {
818 if containerLenS > rvcap {
819 oldRvlenGtZero := rvlen > 0
820 rvlen1 := decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
821 if rvlen1 == rvlen {
822 } else if rvlen1 <= rvcap {
823 if rvCanset {
824 rvlen = rvlen1
825 rvSetSliceLen(rv, rvlen)
826 }
827 } else if rvCanset {
828 rvlen = rvlen1
829 rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen)
830 rvcap = rvlen
831 rvChanged = !rvCanset
832 } else {
833 d.errorf("cannot decode into non-settable slice")
834 }
835 if rvChanged && oldRvlenGtZero && rtelem0Mut {
836 rvCopySlice(rv, rv0, rtelem)
837 }
838 } else if containerLenS != rvlen {
839 if rvCanset {
840 rvlen = containerLenS
841 rvSetSliceLen(rv, rvlen)
842 }
843 }
844 }
845
846
847 var elemReset = d.h.SliceElementReset
848
849 var j int
850
851 for ; d.containerNext(j, containerLenS, hasLen); j++ {
852 if j == 0 {
853 if rvIsNil(rv) {
854 if rvCanset {
855 rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
856 rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen)
857 rvcap = rvlen
858 rvChanged = !rvCanset
859 } else {
860 d.errorf("cannot decode into non-settable slice")
861 }
862 }
863 if fn == nil {
864 fn = d.h.fn(rtelem)
865 }
866 }
867
868 if j >= rvlen {
869 slh.ElemContainerState(j)
870
871
872
873
874 if rvlen < rvcap {
875 rvlen = rvcap
876 if rvCanset {
877 rvSetSliceLen(rv, rvlen)
878 } else if rvChanged {
879 rv = rvSlice(rv, rvlen)
880 } else {
881 d.onerror(errExpandSliceCannotChange)
882 }
883 } else {
884 if !(rvCanset || rvChanged) {
885 d.onerror(errExpandSliceCannotChange)
886 }
887 rv, rvcap, rvCanset = rvGrowSlice(rv, f.ti, rvcap, 1)
888 rvlen = rvcap
889 rvChanged = !rvCanset
890 }
891 } else {
892 slh.ElemContainerState(j)
893 }
894 rv9 = rvSliceIndex(rv, j, f.ti)
895 if elemReset {
896 rvSetZero(rv9)
897 }
898 d.decodeValue(rv9, fn)
899 }
900 if j < rvlen {
901 if rvCanset {
902 rvSetSliceLen(rv, j)
903 } else if rvChanged {
904 rv = rvSlice(rv, j)
905 }
906
907 } else if j == 0 && rvIsNil(rv) {
908 if rvCanset {
909 rv = rvSliceZeroCap(ti.rt)
910 rvCanset = false
911 rvChanged = true
912 }
913 }
914 slh.End()
915
916 if rvChanged {
917 rvSetDirect(rv0, rv)
918 }
919 }
920
921 func (d *Decoder) kArray(f *codecFnInfo, rv reflect.Value) {
922
923
924 ctyp := d.d.ContainerType()
925 if handleBytesWithinKArray && (ctyp == valueTypeBytes || ctyp == valueTypeString) {
926
927 if f.ti.elemkind != uint8(reflect.Uint8) {
928 d.errorf("bytes/string in stream can decode into array of bytes, but not %v", f.ti.rt)
929 }
930 rvbs := rvGetArrayBytes(rv, nil)
931 bs2 := d.decodeBytesInto(rvbs)
932 if !byteSliceSameData(rvbs, bs2) && len(rvbs) > 0 && len(bs2) > 0 {
933 copy(rvbs, bs2)
934 }
935 return
936 }
937
938 slh, containerLenS := d.decSliceHelperStart()
939
940
941 if containerLenS == 0 {
942 slh.End()
943 return
944 }
945
946 rtelem := f.ti.elem
947 for k := reflect.Kind(f.ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
948 rtelem = rtelem.Elem()
949 }
950
951 var fn *codecFn
952
953 var rv9 reflect.Value
954
955 rvlen := rv.Len()
956 hasLen := containerLenS > 0
957 if hasLen && containerLenS > rvlen {
958 d.errorf("cannot decode into array with length: %v, less than container length: %v", rvlen, containerLenS)
959 }
960
961
962 var elemReset = d.h.SliceElementReset
963
964 for j := 0; d.containerNext(j, containerLenS, hasLen); j++ {
965
966 if j >= rvlen {
967 slh.arrayCannotExpand(hasLen, rvlen, j, containerLenS)
968 return
969 }
970
971 slh.ElemContainerState(j)
972 rv9 = rvArrayIndex(rv, j, f.ti)
973 if elemReset {
974 rvSetZero(rv9)
975 }
976
977 if fn == nil {
978 fn = d.h.fn(rtelem)
979 }
980 d.decodeValue(rv9, fn)
981 }
982 slh.End()
983 }
984
985 func (d *Decoder) kChan(f *codecFnInfo, rv reflect.Value) {
986
987
988
989 ti := f.ti
990 if ti.chandir&uint8(reflect.SendDir) == 0 {
991 d.errorf("receive-only channel cannot be decoded")
992 }
993 ctyp := d.d.ContainerType()
994 if ctyp == valueTypeBytes || ctyp == valueTypeString {
995
996 if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) {
997 d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt)
998 }
999 bs2 := d.d.DecodeBytes(nil)
1000 irv := rv2i(rv)
1001 ch, ok := irv.(chan<- byte)
1002 if !ok {
1003 ch = irv.(chan byte)
1004 }
1005 for _, b := range bs2 {
1006 ch <- b
1007 }
1008 return
1009 }
1010
1011 var rvCanset = rv.CanSet()
1012
1013
1014 slh, containerLenS := d.decSliceHelperStart()
1015
1016
1017 if containerLenS == 0 {
1018 if rvCanset && rvIsNil(rv) {
1019 rvSetDirect(rv, reflect.MakeChan(ti.rt, 0))
1020 }
1021 slh.End()
1022 return
1023 }
1024
1025 rtelem := ti.elem
1026 useTransient := decUseTransient && ti.elemkind != byte(reflect.Ptr) && ti.tielem.flagCanTransient
1027
1028 for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
1029 rtelem = rtelem.Elem()
1030 }
1031
1032 var fn *codecFn
1033
1034 var rvChanged bool
1035 var rv0 = rv
1036 var rv9 reflect.Value
1037
1038 var rvlen int
1039 hasLen := containerLenS > 0
1040
1041 for j := 0; d.containerNext(j, containerLenS, hasLen); j++ {
1042 if j == 0 {
1043 if rvIsNil(rv) {
1044 if hasLen {
1045 rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
1046 } else {
1047 rvlen = decDefChanCap
1048 }
1049 if rvCanset {
1050 rv = reflect.MakeChan(ti.rt, rvlen)
1051 rvChanged = true
1052 } else {
1053 d.errorf("cannot decode into non-settable chan")
1054 }
1055 }
1056 if fn == nil {
1057 fn = d.h.fn(rtelem)
1058 }
1059 }
1060 slh.ElemContainerState(j)
1061 if rv9.IsValid() {
1062 rvSetZero(rv9)
1063 } else if decUseTransient && useTransient {
1064 rv9 = d.perType.TransientAddrK(ti.elem, reflect.Kind(ti.elemkind))
1065 } else {
1066 rv9 = rvZeroAddrK(ti.elem, reflect.Kind(ti.elemkind))
1067 }
1068 if !d.d.TryNil() {
1069 d.decodeValueNoCheckNil(rv9, fn)
1070 }
1071 rv.Send(rv9)
1072 }
1073 slh.End()
1074
1075 if rvChanged {
1076 rvSetDirect(rv0, rv)
1077 }
1078
1079 }
1080
1081 func (d *Decoder) kMap(f *codecFnInfo, rv reflect.Value) {
1082 containerLen := d.mapStart(d.d.ReadMapStart())
1083 ti := f.ti
1084 if rvIsNil(rv) {
1085 rvlen := decInferLen(containerLen, d.h.MaxInitLen, int(ti.keysize+ti.elemsize))
1086 rvSetDirect(rv, makeMapReflect(ti.rt, rvlen))
1087 }
1088
1089 if containerLen == 0 {
1090 d.mapEnd()
1091 return
1092 }
1093
1094 ktype, vtype := ti.key, ti.elem
1095 ktypeId := rt2id(ktype)
1096 vtypeKind := reflect.Kind(ti.elemkind)
1097 ktypeKind := reflect.Kind(ti.keykind)
1098 kfast := mapKeyFastKindFor(ktypeKind)
1099 visindirect := mapStoresElemIndirect(uintptr(ti.elemsize))
1100 visref := refBitset.isset(ti.elemkind)
1101
1102 vtypePtr := vtypeKind == reflect.Ptr
1103 ktypePtr := ktypeKind == reflect.Ptr
1104
1105 vTransient := decUseTransient && !vtypePtr && ti.tielem.flagCanTransient
1106 kTransient := decUseTransient && !ktypePtr && ti.tikey.flagCanTransient
1107
1108 var vtypeElem reflect.Type
1109
1110 var keyFn, valFn *codecFn
1111 var ktypeLo, vtypeLo = ktype, vtype
1112
1113 if ktypeKind == reflect.Ptr {
1114 for ktypeLo = ktype.Elem(); ktypeLo.Kind() == reflect.Ptr; ktypeLo = ktypeLo.Elem() {
1115 }
1116 }
1117
1118 if vtypePtr {
1119 vtypeElem = vtype.Elem()
1120 for vtypeLo = vtypeElem; vtypeLo.Kind() == reflect.Ptr; vtypeLo = vtypeLo.Elem() {
1121 }
1122 }
1123
1124 rvkMut := !scalarBitset.isset(ti.keykind)
1125 rvvMut := !scalarBitset.isset(ti.elemkind)
1126 rvvCanNil := isnilBitset.isset(ti.elemkind)
1127
1128
1129
1130
1131
1132
1133
1134
1135 var rvk, rvkn, rvv, rvvn, rvva, rvvz reflect.Value
1136
1137
1138 var doMapGet, doMapSet bool
1139
1140 if !d.h.MapValueReset {
1141 if rvvMut && (vtypeKind != reflect.Interface || !d.h.InterfaceReset) {
1142 doMapGet = true
1143 rvva = mapAddrLoopvarRV(vtype, vtypeKind)
1144 }
1145 }
1146
1147 ktypeIsString := ktypeId == stringTypId
1148 ktypeIsIntf := ktypeId == intfTypId
1149
1150 hasLen := containerLen > 0
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 var kstrbs []byte
1161 var kstr2bs []byte
1162 var s string
1163
1164 var callFnRvk bool
1165
1166 fnRvk2 := func() (s string) {
1167 callFnRvk = false
1168 if len(kstr2bs) < 2 {
1169 return string(kstr2bs)
1170 }
1171 return d.mapKeyString(&callFnRvk, &kstrbs, &kstr2bs)
1172 }
1173
1174
1175
1176 for j := 0; d.containerNext(j, containerLen, hasLen); j++ {
1177 callFnRvk = false
1178 if j == 0 {
1179
1180
1181 if decUseTransient && vTransient && kTransient {
1182 rvk = d.perType.TransientAddr2K(ktype, ktypeKind)
1183 } else {
1184 rvk = rvZeroAddrK(ktype, ktypeKind)
1185 }
1186 if !rvkMut {
1187 rvkn = rvk
1188 }
1189 if !rvvMut {
1190 if decUseTransient && vTransient {
1191 rvvn = d.perType.TransientAddrK(vtype, vtypeKind)
1192 } else {
1193 rvvn = rvZeroAddrK(vtype, vtypeKind)
1194 }
1195 }
1196 if !ktypeIsString && keyFn == nil {
1197 keyFn = d.h.fn(ktypeLo)
1198 }
1199 if valFn == nil {
1200 valFn = d.h.fn(vtypeLo)
1201 }
1202 } else if rvkMut {
1203 rvSetZero(rvk)
1204 } else {
1205 rvk = rvkn
1206 }
1207
1208 d.mapElemKey()
1209 if ktypeIsString {
1210 kstr2bs = d.d.DecodeStringAsBytes()
1211 rvSetString(rvk, fnRvk2())
1212 } else {
1213 d.decByteState = decByteStateNone
1214 d.decodeValue(rvk, keyFn)
1215
1216 if ktypeIsIntf {
1217 if rvk2 := rvk.Elem(); rvk2.IsValid() && rvk2.Type() == uint8SliceTyp {
1218 kstr2bs = rvGetBytes(rvk2)
1219 rvSetIntf(rvk, rv4istr(fnRvk2()))
1220 }
1221
1222 }
1223 }
1224
1225 d.mapElemValue()
1226
1227 if d.d.TryNil() {
1228
1229 if !rvvz.IsValid() {
1230 rvvz = rvZeroK(vtype, vtypeKind)
1231 }
1232 if callFnRvk {
1233 s = d.string(kstr2bs)
1234 if ktypeIsString {
1235 rvSetString(rvk, s)
1236 } else {
1237 rvSetIntf(rvk, rv4istr(s))
1238 }
1239 }
1240 mapSet(rv, rvk, rvvz, kfast, visindirect, visref)
1241 continue
1242 }
1243
1244
1245
1246
1247
1248 doMapSet = true
1249
1250 if !rvvMut {
1251 rvv = rvvn
1252 } else if !doMapGet {
1253 goto NEW_RVV
1254 } else {
1255 rvv = mapGet(rv, rvk, rvva, kfast, visindirect, visref)
1256 if !rvv.IsValid() || (rvvCanNil && rvIsNil(rvv)) {
1257 goto NEW_RVV
1258 }
1259 switch vtypeKind {
1260 case reflect.Ptr, reflect.Map:
1261 doMapSet = false
1262 case reflect.Interface:
1263
1264 rvvn = rvv.Elem()
1265 if k := rvvn.Kind(); (k == reflect.Ptr || k == reflect.Map) && !rvIsNil(rvvn) {
1266 d.decodeValueNoCheckNil(rvvn, nil)
1267 continue
1268 }
1269
1270 rvvn = rvZeroAddrK(vtype, vtypeKind)
1271 rvSetIntf(rvvn, rvv)
1272 rvv = rvvn
1273 default:
1274
1275 if decUseTransient && vTransient {
1276 rvvn = d.perType.TransientAddrK(vtype, vtypeKind)
1277 } else {
1278 rvvn = rvZeroAddrK(vtype, vtypeKind)
1279 }
1280 rvSetDirect(rvvn, rvv)
1281 rvv = rvvn
1282 }
1283 }
1284 goto DECODE_VALUE_NO_CHECK_NIL
1285
1286 NEW_RVV:
1287 if vtypePtr {
1288 rvv = reflect.New(vtypeElem)
1289 } else if decUseTransient && vTransient {
1290 rvv = d.perType.TransientAddrK(vtype, vtypeKind)
1291 } else {
1292 rvv = rvZeroAddrK(vtype, vtypeKind)
1293 }
1294
1295 DECODE_VALUE_NO_CHECK_NIL:
1296 d.decodeValueNoCheckNil(rvv, valFn)
1297
1298 if doMapSet {
1299 if callFnRvk {
1300 s = d.string(kstr2bs)
1301 if ktypeIsString {
1302 rvSetString(rvk, s)
1303 } else {
1304 rvSetIntf(rvk, rv4istr(s))
1305 }
1306 }
1307 mapSet(rv, rvk, rvv, kfast, visindirect, visref)
1308 }
1309 }
1310
1311 d.mapEnd()
1312 }
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322 type Decoder struct {
1323 panicHdl
1324
1325 d decDriver
1326
1327
1328 mtid uintptr
1329 stid uintptr
1330
1331 h *BasicHandle
1332
1333 blist bytesFreelist
1334
1335
1336 decRd
1337
1338
1339 n fauxUnion
1340
1341 hh Handle
1342 err error
1343
1344 perType decPerType
1345
1346
1347 is internerMap
1348
1349
1350
1351 maxdepth int16
1352 depth int16
1353
1354
1355
1356
1357 calls uint16
1358
1359 c containerState
1360
1361 decByteState
1362
1363
1364
1365
1366 b [decScratchByteArrayLen]byte
1367 }
1368
1369
1370
1371
1372
1373 func NewDecoder(r io.Reader, h Handle) *Decoder {
1374 d := h.newDecDriver().decoder()
1375 if r != nil {
1376 d.Reset(r)
1377 }
1378 return d
1379 }
1380
1381
1382
1383 func NewDecoderBytes(in []byte, h Handle) *Decoder {
1384 d := h.newDecDriver().decoder()
1385 if in != nil {
1386 d.ResetBytes(in)
1387 }
1388 return d
1389 }
1390
1391
1392
1393
1394
1395
1396
1397
1398 func NewDecoderString(s string, h Handle) *Decoder {
1399 return NewDecoderBytes(bytesView(s), h)
1400 }
1401
1402 func (d *Decoder) HandleName() string {
1403 return d.hh.Name()
1404 }
1405
1406 func (d *Decoder) r() *decRd {
1407 return &d.decRd
1408 }
1409
1410 func (d *Decoder) init(h Handle) {
1411 initHandle(h)
1412 d.cbreak = d.js || d.cbor
1413 d.bytes = true
1414 d.err = errDecoderNotInitialized
1415 d.h = h.getBasicHandle()
1416 d.hh = h
1417 d.be = h.isBinary()
1418 if d.h.InternString && d.is == nil {
1419 d.is.init()
1420 }
1421
1422 }
1423
1424 func (d *Decoder) resetCommon() {
1425 d.d.reset()
1426 d.err = nil
1427 d.c = 0
1428 d.decByteState = decByteStateNone
1429 d.depth = 0
1430 d.calls = 0
1431
1432 d.maxdepth = decDefMaxDepth
1433 if d.h.MaxDepth > 0 {
1434 d.maxdepth = d.h.MaxDepth
1435 }
1436 d.mtid = 0
1437 d.stid = 0
1438 d.mtr = false
1439 d.str = false
1440 if d.h.MapType != nil {
1441 d.mtid = rt2id(d.h.MapType)
1442 d.mtr = fastpathAvIndex(d.mtid) != -1
1443 }
1444 if d.h.SliceType != nil {
1445 d.stid = rt2id(d.h.SliceType)
1446 d.str = fastpathAvIndex(d.stid) != -1
1447 }
1448 }
1449
1450
1451
1452 func (d *Decoder) Reset(r io.Reader) {
1453 if r == nil {
1454 r = &eofReader
1455 }
1456 d.bytes = false
1457 if d.ri == nil {
1458 d.ri = new(ioDecReader)
1459 }
1460 d.ri.reset(r, d.h.ReaderBufferSize, &d.blist)
1461 d.decReader = d.ri
1462 d.resetCommon()
1463 }
1464
1465
1466
1467 func (d *Decoder) ResetBytes(in []byte) {
1468 if in == nil {
1469 in = []byte{}
1470 }
1471 d.bytes = true
1472 d.decReader = &d.rb
1473 d.rb.reset(in)
1474 d.resetCommon()
1475 }
1476
1477
1478
1479
1480
1481
1482
1483
1484 func (d *Decoder) ResetString(s string) {
1485 d.ResetBytes(bytesView(s))
1486 }
1487
1488 func (d *Decoder) naked() *fauxUnion {
1489 return &d.n
1490 }
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555 func (d *Decoder) Decode(v interface{}) (err error) {
1556
1557
1558
1559 if !debugging {
1560 defer func() {
1561 if x := recover(); x != nil {
1562 panicValToErr(d, x, &d.err)
1563 err = d.err
1564 }
1565 }()
1566 }
1567
1568 d.MustDecode(v)
1569 return
1570 }
1571
1572
1573
1574
1575 func (d *Decoder) MustDecode(v interface{}) {
1576 halt.onerror(d.err)
1577 if d.hh == nil {
1578 halt.onerror(errNoFormatHandle)
1579 }
1580
1581
1582 d.calls++
1583 d.decode(v)
1584 d.calls--
1585 }
1586
1587
1588
1589
1590
1591 func (d *Decoder) Release() {
1592 }
1593
1594 func (d *Decoder) swallow() {
1595 d.d.nextValueBytes(nil)
1596 }
1597
1598 func (d *Decoder) swallowErr() (err error) {
1599 if !debugging {
1600 defer func() {
1601 if x := recover(); x != nil {
1602 panicValToErr(d, x, &err)
1603 }
1604 }()
1605 }
1606 d.swallow()
1607 return
1608 }
1609
1610 func setZero(iv interface{}) {
1611 if iv == nil {
1612 return
1613 }
1614 rv, ok := isNil(iv)
1615 if ok {
1616 return
1617 }
1618
1619 switch v := iv.(type) {
1620 case *string:
1621 *v = ""
1622 case *bool:
1623 *v = false
1624 case *int:
1625 *v = 0
1626 case *int8:
1627 *v = 0
1628 case *int16:
1629 *v = 0
1630 case *int32:
1631 *v = 0
1632 case *int64:
1633 *v = 0
1634 case *uint:
1635 *v = 0
1636 case *uint8:
1637 *v = 0
1638 case *uint16:
1639 *v = 0
1640 case *uint32:
1641 *v = 0
1642 case *uint64:
1643 *v = 0
1644 case *float32:
1645 *v = 0
1646 case *float64:
1647 *v = 0
1648 case *complex64:
1649 *v = 0
1650 case *complex128:
1651 *v = 0
1652 case *[]byte:
1653 *v = nil
1654 case *Raw:
1655 *v = nil
1656 case *time.Time:
1657 *v = time.Time{}
1658 case reflect.Value:
1659 decSetNonNilRV2Zero(v)
1660 default:
1661 if !fastpathDecodeSetZeroTypeSwitch(iv) {
1662 decSetNonNilRV2Zero(rv)
1663 }
1664 }
1665 }
1666
1667
1668 func decSetNonNilRV2Zero(v reflect.Value) {
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678 k := v.Kind()
1679 if k == reflect.Interface {
1680 decSetNonNilRV2Zero4Intf(v)
1681 } else if k == reflect.Ptr {
1682 decSetNonNilRV2Zero4Ptr(v)
1683 } else if v.CanSet() {
1684 rvSetDirectZero(v)
1685 }
1686 }
1687
1688 func decSetNonNilRV2Zero4Ptr(v reflect.Value) {
1689 ve := v.Elem()
1690 if ve.CanSet() {
1691 rvSetZero(ve)
1692 } else if v.CanSet() {
1693 rvSetZero(v)
1694 }
1695 }
1696
1697 func decSetNonNilRV2Zero4Intf(v reflect.Value) {
1698 ve := v.Elem()
1699 if ve.CanSet() {
1700 rvSetDirectZero(ve)
1701 } else if v.CanSet() {
1702 rvSetZero(v)
1703 }
1704 }
1705
1706 func (d *Decoder) decode(iv interface{}) {
1707
1708
1709
1710 if iv == nil {
1711 d.onerror(errCannotDecodeIntoNil)
1712 }
1713
1714 switch v := iv.(type) {
1715
1716
1717 case reflect.Value:
1718 if x, _ := isDecodeable(v); !x {
1719 d.haltAsNotDecodeable(v)
1720 }
1721 d.decodeValue(v, nil)
1722 case *string:
1723 *v = d.stringZC(d.d.DecodeStringAsBytes())
1724 case *bool:
1725 *v = d.d.DecodeBool()
1726 case *int:
1727 *v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
1728 case *int8:
1729 *v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8))
1730 case *int16:
1731 *v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16))
1732 case *int32:
1733 *v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32))
1734 case *int64:
1735 *v = d.d.DecodeInt64()
1736 case *uint:
1737 *v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
1738 case *uint8:
1739 *v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))
1740 case *uint16:
1741 *v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))
1742 case *uint32:
1743 *v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))
1744 case *uint64:
1745 *v = d.d.DecodeUint64()
1746 case *float32:
1747 *v = d.decodeFloat32()
1748 case *float64:
1749 *v = d.d.DecodeFloat64()
1750 case *complex64:
1751 *v = complex(d.decodeFloat32(), 0)
1752 case *complex128:
1753 *v = complex(d.d.DecodeFloat64(), 0)
1754 case *[]byte:
1755 *v = d.decodeBytesInto(*v)
1756 case []byte:
1757
1758 b := d.decodeBytesInto(v[:len(v):len(v)])
1759 if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) {
1760 copy(v, b)
1761 }
1762 case *time.Time:
1763 *v = d.d.DecodeTime()
1764 case *Raw:
1765 *v = d.rawBytes()
1766
1767 case *interface{}:
1768 d.decodeValue(rv4iptr(v), nil)
1769
1770 default:
1771
1772 if skipFastpathTypeSwitchInDirectCall || !fastpathDecodeTypeSwitch(iv, d) {
1773 v := reflect.ValueOf(iv)
1774 if x, _ := isDecodeable(v); !x {
1775 d.haltAsNotDecodeable(v)
1776 }
1777 d.decodeValue(v, nil)
1778 }
1779 }
1780 }
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790 func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn) {
1791 if d.d.TryNil() {
1792 decSetNonNilRV2Zero(rv)
1793 return
1794 }
1795 d.decodeValueNoCheckNil(rv, fn)
1796 }
1797
1798 func (d *Decoder) decodeValueNoCheckNil(rv reflect.Value, fn *codecFn) {
1799
1800
1801 var rvp reflect.Value
1802 var rvpValid bool
1803 PTR:
1804 if rv.Kind() == reflect.Ptr {
1805 rvpValid = true
1806 if rvIsNil(rv) {
1807 rvSetDirect(rv, reflect.New(rv.Type().Elem()))
1808 }
1809 rvp = rv
1810 rv = rv.Elem()
1811 goto PTR
1812 }
1813
1814 if fn == nil {
1815 fn = d.h.fn(rv.Type())
1816 }
1817 if fn.i.addrD {
1818 if rvpValid {
1819 rv = rvp
1820 } else if rv.CanAddr() {
1821 rv = rvAddr(rv, fn.i.ti.ptr)
1822 } else if fn.i.addrDf {
1823 d.errorf("cannot decode into a non-pointer value")
1824 }
1825 }
1826 fn.fd(d, &fn.i, rv)
1827 }
1828
1829 func (d *Decoder) structFieldNotFound(index int, rvkencname string) {
1830
1831
1832
1833 if d.h.ErrorIfNoField {
1834 if index >= 0 {
1835 d.errorf("no matching struct field found when decoding stream array at index %v", index)
1836 } else if rvkencname != "" {
1837 d.errorf("no matching struct field found when decoding stream map with key " + rvkencname)
1838 }
1839 }
1840 d.swallow()
1841 }
1842
1843 func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) {
1844 if d.h.ErrorIfNoArrayExpand {
1845 d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen)
1846 }
1847 }
1848
1849 func (d *Decoder) haltAsNotDecodeable(rv reflect.Value) {
1850 if !rv.IsValid() {
1851 d.onerror(errCannotDecodeIntoNil)
1852 }
1853
1854 if !rv.CanInterface() {
1855 d.errorf("cannot decode into a value without an interface: %v", rv)
1856 }
1857 d.errorf("cannot decode into value of kind: %v, %#v", rv.Kind(), rv2i(rv))
1858 }
1859
1860 func (d *Decoder) depthIncr() {
1861 d.depth++
1862 if d.depth >= d.maxdepth {
1863 d.onerror(errMaxDepthExceeded)
1864 }
1865 }
1866
1867 func (d *Decoder) depthDecr() {
1868 d.depth--
1869 }
1870
1871
1872
1873
1874
1875 func (d *Decoder) string(v []byte) (s string) {
1876 if d.is == nil || d.c != containerMapKey || len(v) < 2 || len(v) > internMaxStrLen {
1877 return string(v)
1878 }
1879 return d.is.string(v)
1880 }
1881
1882 func (d *Decoder) zerocopy() bool {
1883 return d.bytes && d.h.ZeroCopy
1884 }
1885
1886
1887
1888
1889 func (d *Decoder) decodeBytesInto(in []byte) (v []byte) {
1890 if in == nil {
1891 in = []byte{}
1892 }
1893 return d.d.DecodeBytes(in)
1894 }
1895
1896 func (d *Decoder) rawBytes() (v []byte) {
1897
1898
1899 v = d.d.nextValueBytes([]byte{})
1900 if d.bytes && !d.h.ZeroCopy {
1901 vv := make([]byte, len(v))
1902 copy(vv, v)
1903 v = vv
1904 }
1905 return
1906 }
1907
1908 func (d *Decoder) wrapErr(v error, err *error) {
1909 *err = wrapCodecErr(v, d.hh.Name(), d.NumBytesRead(), false)
1910 }
1911
1912
1913 func (d *Decoder) NumBytesRead() int {
1914 return int(d.r().numread())
1915 }
1916
1917
1918
1919
1920
1921 func (d *Decoder) decodeFloat32() float32 {
1922 if d.js {
1923 return d.jsondriver().DecodeFloat32()
1924 }
1925 return float32(chkOvf.Float32V(d.d.DecodeFloat64()))
1926 }
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941 func (d *Decoder) checkBreak() (v bool) {
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953 if d.cbreak {
1954 v = d.d.CheckBreak()
1955 }
1956 return
1957 }
1958
1959 func (d *Decoder) containerNext(j, containerLen int, hasLen bool) bool {
1960
1961
1962
1963 if hasLen {
1964 return j < containerLen
1965 }
1966 return !d.checkBreak()
1967 }
1968
1969 func (d *Decoder) mapStart(v int) int {
1970 if v != containerLenNil {
1971 d.depthIncr()
1972 d.c = containerMapStart
1973 }
1974 return v
1975 }
1976
1977 func (d *Decoder) mapElemKey() {
1978 if d.js {
1979 d.jsondriver().ReadMapElemKey()
1980 }
1981 d.c = containerMapKey
1982 }
1983
1984 func (d *Decoder) mapElemValue() {
1985 if d.js {
1986 d.jsondriver().ReadMapElemValue()
1987 }
1988 d.c = containerMapValue
1989 }
1990
1991 func (d *Decoder) mapEnd() {
1992 if d.js {
1993 d.jsondriver().ReadMapEnd()
1994 }
1995
1996 d.depthDecr()
1997 d.c = 0
1998 }
1999
2000 func (d *Decoder) arrayStart(v int) int {
2001 if v != containerLenNil {
2002 d.depthIncr()
2003 d.c = containerArrayStart
2004 }
2005 return v
2006 }
2007
2008 func (d *Decoder) arrayElem() {
2009 if d.js {
2010 d.jsondriver().ReadArrayElem()
2011 }
2012 d.c = containerArrayElem
2013 }
2014
2015 func (d *Decoder) arrayEnd() {
2016 if d.js {
2017 d.jsondriver().ReadArrayEnd()
2018 }
2019
2020 d.depthDecr()
2021 d.c = 0
2022 }
2023
2024 func (d *Decoder) interfaceExtConvertAndDecode(v interface{}, ext InterfaceExt) {
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037 var s interface{}
2038 rv := reflect.ValueOf(v)
2039 rv2 := rv.Elem()
2040 rvk := rv2.Kind()
2041 if rvk == reflect.Struct || rvk == reflect.Array {
2042 s = ext.ConvertExt(v)
2043 } else {
2044 s = ext.ConvertExt(rv2i(rv2))
2045 }
2046 rv = reflect.ValueOf(s)
2047
2048
2049
2050
2051
2052 if !rv.CanAddr() {
2053 rvk = rv.Kind()
2054 rv2 = d.oneShotAddrRV(rv.Type(), rvk)
2055 if rvk == reflect.Interface {
2056 rvSetIntf(rv2, rv)
2057 } else {
2058 rvSetDirect(rv2, rv)
2059 }
2060 rv = rv2
2061 }
2062
2063 d.decodeValue(rv, nil)
2064 ext.UpdateExt(v, rv2i(rv))
2065 }
2066
2067 func (d *Decoder) sideDecode(v interface{}, basetype reflect.Type, bs []byte) {
2068
2069
2070 defer func(rb bytesDecReader, bytes bool,
2071 c containerState, dbs decByteState, depth int16, r decReader, state interface{}) {
2072 d.rb = rb
2073 d.bytes = bytes
2074 d.c = c
2075 d.decByteState = dbs
2076 d.depth = depth
2077 d.decReader = r
2078 d.d.restoreState(state)
2079 }(d.rb, d.bytes, d.c, d.decByteState, d.depth, d.decReader, d.d.captureState())
2080
2081
2082 d.rb = bytesDecReader{bs[:len(bs):len(bs)], 0}
2083 d.bytes = true
2084 d.decReader = &d.rb
2085 d.d.resetState()
2086 d.c = 0
2087 d.decByteState = decByteStateNone
2088 d.depth = 0
2089
2090
2091 d.decodeValue(baseRV(v), d.h.fnNoExt(basetype))
2092 }
2093
2094 func (d *Decoder) fauxUnionReadRawBytes(asString bool) {
2095 if asString || d.h.RawToString {
2096 d.n.v = valueTypeString
2097
2098 d.n.s = d.stringZC(d.d.DecodeBytes(nil))
2099 } else {
2100 d.n.v = valueTypeBytes
2101 d.n.l = d.d.DecodeBytes([]byte{})
2102 }
2103 }
2104
2105 func (d *Decoder) oneShotAddrRV(rvt reflect.Type, rvk reflect.Kind) reflect.Value {
2106 if decUseTransient &&
2107 (numBoolStrSliceBitset.isset(byte(rvk)) ||
2108 ((rvk == reflect.Struct || rvk == reflect.Array) &&
2109 d.h.getTypeInfo(rt2id(rvt), rvt).flagCanTransient)) {
2110 return d.perType.TransientAddrK(rvt, rvk)
2111 }
2112 return rvZeroAddrK(rvt, rvk)
2113 }
2114
2115
2116
2117
2118
2119
2120
2121 type decSliceHelper struct {
2122 d *Decoder
2123 ct valueType
2124 Array bool
2125 IsNil bool
2126 }
2127
2128 func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) {
2129 x.ct = d.d.ContainerType()
2130 x.d = d
2131 switch x.ct {
2132 case valueTypeNil:
2133 x.IsNil = true
2134 case valueTypeArray:
2135 x.Array = true
2136 clen = d.arrayStart(d.d.ReadArrayStart())
2137 case valueTypeMap:
2138 clen = d.mapStart(d.d.ReadMapStart())
2139 clen += clen
2140 default:
2141 d.errorf("only encoded map or array can be decoded into a slice (%d)", x.ct)
2142 }
2143 return
2144 }
2145
2146 func (x decSliceHelper) End() {
2147 if x.IsNil {
2148 } else if x.Array {
2149 x.d.arrayEnd()
2150 } else {
2151 x.d.mapEnd()
2152 }
2153 }
2154
2155 func (x decSliceHelper) ElemContainerState(index int) {
2156
2157
2158 if x.Array {
2159 x.d.arrayElem()
2160 } else if index&1 == 0 {
2161 x.d.mapElemKey()
2162 } else {
2163 x.d.mapElemValue()
2164 }
2165 }
2166
2167 func (x decSliceHelper) arrayCannotExpand(hasLen bool, lenv, j, containerLenS int) {
2168 x.d.arrayCannotExpand(lenv, j+1)
2169
2170 x.ElemContainerState(j)
2171 x.d.swallow()
2172 j++
2173 for ; x.d.containerNext(j, containerLenS, hasLen); j++ {
2174 x.ElemContainerState(j)
2175 x.d.swallow()
2176 }
2177 x.End()
2178 }
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188 type decNextValueBytesHelper struct {
2189 d *Decoder
2190 }
2191
2192 func (x decNextValueBytesHelper) append1(v *[]byte, b byte) {
2193 if *v != nil && !x.d.bytes {
2194 *v = append(*v, b)
2195 }
2196 }
2197
2198 func (x decNextValueBytesHelper) appendN(v *[]byte, b ...byte) {
2199 if *v != nil && !x.d.bytes {
2200 *v = append(*v, b...)
2201 }
2202 }
2203
2204 func (x decNextValueBytesHelper) appendS(v *[]byte, b string) {
2205 if *v != nil && !x.d.bytes {
2206 *v = append(*v, b...)
2207 }
2208 }
2209
2210 func (x decNextValueBytesHelper) bytesRdV(v *[]byte, startpos uint) {
2211 if x.d.bytes {
2212 *v = x.d.rb.b[startpos:x.d.rb.c]
2213 }
2214 }
2215
2216
2217
2218
2219
2220
2221 type decNegintPosintFloatNumberHelper struct {
2222 d *Decoder
2223 }
2224
2225 func (x decNegintPosintFloatNumberHelper) uint64(ui uint64, neg, ok bool) uint64 {
2226 if ok && !neg {
2227 return ui
2228 }
2229 return x.uint64TryFloat(ok)
2230 }
2231
2232 func (x decNegintPosintFloatNumberHelper) uint64TryFloat(ok bool) (ui uint64) {
2233 if ok {
2234 x.d.errorf("assigning negative signed value to unsigned type")
2235 }
2236 f, ok := x.d.d.decFloat()
2237 if ok && f >= 0 && noFrac64(math.Float64bits(f)) {
2238 ui = uint64(f)
2239 } else {
2240 x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd())
2241 }
2242 return ui
2243 }
2244
2245 func decNegintPosintFloatNumberHelperInt64v(ui uint64, neg, incrIfNeg bool) (i int64) {
2246 if neg && incrIfNeg {
2247 ui++
2248 }
2249 i = chkOvf.SignedIntV(ui)
2250 if neg {
2251 i = -i
2252 }
2253 return
2254 }
2255
2256 func (x decNegintPosintFloatNumberHelper) int64(ui uint64, neg, ok bool) (i int64) {
2257 if ok {
2258 return decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor)
2259 }
2260
2261
2262
2263 f, ok := x.d.d.decFloat()
2264 if ok && noFrac64(math.Float64bits(f)) {
2265 i = int64(f)
2266 } else {
2267 x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd())
2268 }
2269 return
2270 }
2271
2272 func (x decNegintPosintFloatNumberHelper) float64(f float64, ok bool) float64 {
2273 if ok {
2274 return f
2275 }
2276 return x.float64TryInteger()
2277 }
2278
2279 func (x decNegintPosintFloatNumberHelper) float64TryInteger() float64 {
2280 ui, neg, ok := x.d.d.decInteger()
2281 if !ok {
2282 x.d.errorf("invalid descriptor for float: %v", x.d.d.descBd())
2283 }
2284 return float64(decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor))
2285 }
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298 func isDecodeable(rv reflect.Value) (canDecode bool, reason decNotDecodeableReason) {
2299 switch rv.Kind() {
2300 case reflect.Ptr, reflect.Slice, reflect.Chan, reflect.Map:
2301 canDecode = !rvIsNil(rv)
2302 reason = decNotDecodeableReasonNilReference
2303 case reflect.Func, reflect.Interface, reflect.Invalid, reflect.UnsafePointer:
2304 reason = decNotDecodeableReasonBadKind
2305 default:
2306 canDecode = rv.CanAddr()
2307 reason = decNotDecodeableReasonNonAddrValue
2308 }
2309 return
2310 }
2311
2312 func decByteSlice(r *decRd, clen, maxInitLen int, bs []byte) (bsOut []byte) {
2313 if clen <= 0 {
2314 bsOut = zeroByteSlice
2315 } else if cap(bs) >= clen {
2316 bsOut = bs[:clen]
2317 r.readb(bsOut)
2318 } else {
2319 var len2 int
2320 for len2 < clen {
2321 len3 := decInferLen(clen-len2, maxInitLen, 1)
2322 bs3 := bsOut
2323 bsOut = make([]byte, len2+len3)
2324 copy(bsOut, bs3)
2325 r.readb(bsOut[len2:])
2326 len2 += len3
2327 }
2328 }
2329 return
2330 }
2331
2332
2333
2334
2335
2336
2337 func decInferLen(clen, maxlen, unit int) int {
2338
2339
2340
2341 const (
2342 minLenIfUnset = 8
2343 maxMem = 256 * 1024
2344 )
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354 if clen == 0 || clen == containerLenNil {
2355 return 0
2356 }
2357 if clen < 0 {
2358
2359 clen = 64 / unit
2360 if clen > minLenIfUnset {
2361 return clen
2362 }
2363 return minLenIfUnset
2364 }
2365 if unit <= 0 {
2366 return clen
2367 }
2368 if maxlen <= 0 {
2369 maxlen = maxMem / unit
2370 }
2371 if clen < maxlen {
2372 return clen
2373 }
2374 return maxlen
2375 }
2376
View as plain text