1// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
2// Use of this source code is governed by a MIT license found in the LICENSE file.
3
4package codec
5
6import (
7	"encoding"
8	"errors"
9	"fmt"
10	"io"
11	"reflect"
12	"time"
13)
14
15// Some tagging information for error messages.
16const (
17	msgBadDesc            = "Unrecognized descriptor byte"
18	msgDecCannotExpandArr = "cannot expand go array from %v to stream length: %v"
19)
20
21var (
22	onlyMapOrArrayCanDecodeIntoStructErr = errors.New("only encoded map or array can be decoded into a struct")
23	cannotDecodeIntoNilErr               = errors.New("cannot decode into nil")
24)
25
26// decReader abstracts the reading source, allowing implementations that can
27// read from an io.Reader or directly off a byte slice with zero-copying.
28type decReader interface {
29	unreadn1()
30
31	// readx will use the implementation scratch buffer if possible i.e. n < len(scratchbuf), OR
32	// just return a view of the []byte being decoded from.
33	// Ensure you call detachZeroCopyBytes later if this needs to be sent outside codec control.
34	readx(n int) []byte
35	readb([]byte)
36	readn1() uint8
37	readn1eof() (v uint8, eof bool)
38	numread() int // number of bytes read
39	track()
40	stopTrack() []byte
41}
42
43type decReaderByteScanner interface {
44	io.Reader
45	io.ByteScanner
46}
47
48type decDriver interface {
49	// this will check if the next token is a break.
50	CheckBreak() bool
51	TryDecodeAsNil() bool
52	// vt is one of: Bytes, String, Nil, Slice or Map. Return unSet if not known.
53	ContainerType() (vt valueType)
54	IsBuiltinType(rt uintptr) bool
55	DecodeBuiltin(rt uintptr, v interface{})
56
57	// DecodeNaked will decode primitives (number, bool, string, []byte) and RawExt.
58	// For maps and arrays, it will not do the decoding in-band, but will signal
59	// the decoder, so that is done later, by setting the decNaked.valueType field.
60	//
61	// Note: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types).
62	// for extensions, DecodeNaked must read the tag and the []byte if it exists.
63	// if the []byte is not read, then kInterfaceNaked will treat it as a Handle
64	// that stores the subsequent value in-band, and complete reading the RawExt.
65	//
66	// extensions should also use readx to decode them, for efficiency.
67	// kInterface will extract the detached byte slice if it has to pass it outside its realm.
68	DecodeNaked()
69	DecodeInt(bitsize uint8) (i int64)
70	DecodeUint(bitsize uint8) (ui uint64)
71	DecodeFloat(chkOverflow32 bool) (f float64)
72	DecodeBool() (b bool)
73	// DecodeString can also decode symbols.
74	// It looks redundant as DecodeBytes is available.
75	// However, some codecs (e.g. binc) support symbols and can
76	// return a pre-stored string value, meaning that it can bypass
77	// the cost of []byte->string conversion.
78	DecodeString() (s string)
79
80	// DecodeBytes may be called directly, without going through reflection.
81	// Consequently, it must be designed to handle possible nil.
82	DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte)
83
84	// decodeExt will decode into a *RawExt or into an extension.
85	DecodeExt(v interface{}, xtag uint64, ext Ext) (realxtag uint64)
86	// decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte)
87	ReadMapStart() int
88	ReadArrayStart() int
89
90	reset()
91	uncacheRead()
92}
93
94type decNoSeparator struct{}
95
96func (_ decNoSeparator) ReadEnd()     {}
97func (_ decNoSeparator) uncacheRead() {}
98
99type DecodeOptions struct {
100	// MapType specifies type to use during schema-less decoding of a map in the stream.
101	// If nil, we use map[interface{}]interface{}
102	MapType reflect.Type
103
104	// SliceType specifies type to use during schema-less decoding of an array in the stream.
105	// If nil, we use []interface{}
106	SliceType reflect.Type
107
108	// MaxInitLen defines the initial length that we "make" a collection (slice, chan or map) with.
109	// If 0 or negative, we default to a sensible value based on the size of an element in the collection.
110	//
111	// For example, when decoding, a stream may say that it has MAX_UINT elements.
112	// We should not auto-matically provision a slice of that length, to prevent Out-Of-Memory crash.
113	// Instead, we provision up to MaxInitLen, fill that up, and start appending after that.
114	MaxInitLen int
115
116	// If ErrorIfNoField, return an error when decoding a map
117	// from a codec stream into a struct, and no matching struct field is found.
118	ErrorIfNoField bool
119
120	// If ErrorIfNoArrayExpand, return an error when decoding a slice/array that cannot be expanded.
121	// For example, the stream contains an array of 8 items, but you are decoding into a [4]T array,
122	// or you are decoding into a slice of length 4 which is non-addressable (and so cannot be set).
123	ErrorIfNoArrayExpand bool
124
125	// If SignedInteger, use the int64 during schema-less decoding of unsigned values (not uint64).
126	SignedInteger bool
127
128	// MapValueReset controls how we decode into a map value.
129	//
130	// By default, we MAY retrieve the mapping for a key, and then decode into that.
131	// However, especially with big maps, that retrieval may be expensive and unnecessary
132	// if the stream already contains all that is necessary to recreate the value.
133	//
134	// If true, we will never retrieve the previous mapping,
135	// but rather decode into a new value and set that in the map.
136	//
137	// If false, we will retrieve the previous mapping if necessary e.g.
138	// the previous mapping is a pointer, or is a struct or array with pre-set state,
139	// or is an interface.
140	MapValueReset bool
141
142	// InterfaceReset controls how we decode into an interface.
143	//
144	// By default, when we see a field that is an interface{...},
145	// or a map with interface{...} value, we will attempt decoding into the
146	// "contained" value.
147	//
148	// However, this prevents us from reading a string into an interface{}
149	// that formerly contained a number.
150	//
151	// If true, we will decode into a new "blank" value, and set that in the interface.
152	// If false, we will decode into whatever is contained in the interface.
153	InterfaceReset bool
154
155	// InternString controls interning of strings during decoding.
156	//
157	// Some handles, e.g. json, typically will read map keys as strings.
158	// If the set of keys are finite, it may help reduce allocation to
159	// look them up from a map (than to allocate them afresh).
160	//
161	// Note: Handles will be smart when using the intern functionality.
162	// So everything will not be interned.
163	InternString bool
164}
165
166// ------------------------------------
167
168// ioDecByteScanner implements Read(), ReadByte(...), UnreadByte(...) methods
169// of io.Reader, io.ByteScanner.
170type ioDecByteScanner struct {
171	r  io.Reader
172	l  byte    // last byte
173	ls byte    // last byte status. 0: init-canDoNothing, 1: canRead, 2: canUnread
174	b  [1]byte // tiny buffer for reading single bytes
175}
176
177func (z *ioDecByteScanner) Read(p []byte) (n int, err error) {
178	var firstByte bool
179	if z.ls == 1 {
180		z.ls = 2
181		p[0] = z.l
182		if len(p) == 1 {
183			n = 1
184			return
185		}
186		firstByte = true
187		p = p[1:]
188	}
189	n, err = z.r.Read(p)
190	if n > 0 {
191		if err == io.EOF && n == len(p) {
192			err = nil // read was successful, so postpone EOF (till next time)
193		}
194		z.l = p[n-1]
195		z.ls = 2
196	}
197	if firstByte {
198		n++
199	}
200	return
201}
202
203func (z *ioDecByteScanner) ReadByte() (c byte, err error) {
204	n, err := z.Read(z.b[:])
205	if n == 1 {
206		c = z.b[0]
207		if err == io.EOF {
208			err = nil // read was successful, so postpone EOF (till next time)
209		}
210	}
211	return
212}
213
214func (z *ioDecByteScanner) UnreadByte() (err error) {
215	x := z.ls
216	if x == 0 {
217		err = errors.New("cannot unread - nothing has been read")
218	} else if x == 1 {
219		err = errors.New("cannot unread - last byte has not been read")
220	} else if x == 2 {
221		z.ls = 1
222	}
223	return
224}
225
226// ioDecReader is a decReader that reads off an io.Reader
227type ioDecReader struct {
228	br decReaderByteScanner
229	// temp byte array re-used internally for efficiency during read.
230	// shares buffer with Decoder, so we keep size of struct within 8 words.
231	x   *[scratchByteArrayLen]byte
232	bs  ioDecByteScanner
233	n   int    // num read
234	tr  []byte // tracking bytes read
235	trb bool
236}
237
238func (z *ioDecReader) numread() int {
239	return z.n
240}
241
242func (z *ioDecReader) readx(n int) (bs []byte) {
243	if n <= 0 {
244		return
245	}
246	if n < len(z.x) {
247		bs = z.x[:n]
248	} else {
249		bs = make([]byte, n)
250	}
251	if _, err := io.ReadAtLeast(z.br, bs, n); err != nil {
252		panic(err)
253	}
254	z.n += len(bs)
255	if z.trb {
256		z.tr = append(z.tr, bs...)
257	}
258	return
259}
260
261func (z *ioDecReader) readb(bs []byte) {
262	if len(bs) == 0 {
263		return
264	}
265	n, err := io.ReadAtLeast(z.br, bs, len(bs))
266	z.n += n
267	if err != nil {
268		panic(err)
269	}
270	if z.trb {
271		z.tr = append(z.tr, bs...)
272	}
273}
274
275func (z *ioDecReader) readn1() (b uint8) {
276	b, err := z.br.ReadByte()
277	if err != nil {
278		panic(err)
279	}
280	z.n++
281	if z.trb {
282		z.tr = append(z.tr, b)
283	}
284	return b
285}
286
287func (z *ioDecReader) readn1eof() (b uint8, eof bool) {
288	b, err := z.br.ReadByte()
289	if err == nil {
290		z.n++
291		if z.trb {
292			z.tr = append(z.tr, b)
293		}
294	} else if err == io.EOF {
295		eof = true
296	} else {
297		panic(err)
298	}
299	return
300}
301
302func (z *ioDecReader) unreadn1() {
303	err := z.br.UnreadByte()
304	if err != nil {
305		panic(err)
306	}
307	z.n--
308	if z.trb {
309		if l := len(z.tr) - 1; l >= 0 {
310			z.tr = z.tr[:l]
311		}
312	}
313}
314
315func (z *ioDecReader) track() {
316	if z.tr != nil {
317		z.tr = z.tr[:0]
318	}
319	z.trb = true
320}
321
322func (z *ioDecReader) stopTrack() (bs []byte) {
323	z.trb = false
324	return z.tr
325}
326
327// ------------------------------------
328
329var bytesDecReaderCannotUnreadErr = errors.New("cannot unread last byte read")
330
331// bytesDecReader is a decReader that reads off a byte slice with zero copying
332type bytesDecReader struct {
333	b []byte // data
334	c int    // cursor
335	a int    // available
336	t int    // track start
337}
338
339func (z *bytesDecReader) reset(in []byte) {
340	z.b = in
341	z.a = len(in)
342	z.c = 0
343	z.t = 0
344}
345
346func (z *bytesDecReader) numread() int {
347	return z.c
348}
349
350func (z *bytesDecReader) unreadn1() {
351	if z.c == 0 || len(z.b) == 0 {
352		panic(bytesDecReaderCannotUnreadErr)
353	}
354	z.c--
355	z.a++
356	return
357}
358
359func (z *bytesDecReader) readx(n int) (bs []byte) {
360	// slicing from a non-constant start position is more expensive,
361	// as more computation is required to decipher the pointer start position.
362	// However, we do it only once, and it's better than reslicing both z.b and return value.
363
364	if n <= 0 {
365	} else if z.a == 0 {
366		panic(io.EOF)
367	} else if n > z.a {
368		panic(io.ErrUnexpectedEOF)
369	} else {
370		c0 := z.c
371		z.c = c0 + n
372		z.a = z.a - n
373		bs = z.b[c0:z.c]
374	}
375	return
376}
377
378func (z *bytesDecReader) readn1() (v uint8) {
379	if z.a == 0 {
380		panic(io.EOF)
381	}
382	v = z.b[z.c]
383	z.c++
384	z.a--
385	return
386}
387
388func (z *bytesDecReader) readn1eof() (v uint8, eof bool) {
389	if z.a == 0 {
390		eof = true
391		return
392	}
393	v = z.b[z.c]
394	z.c++
395	z.a--
396	return
397}
398
399func (z *bytesDecReader) readb(bs []byte) {
400	copy(bs, z.readx(len(bs)))
401}
402
403func (z *bytesDecReader) track() {
404	z.t = z.c
405}
406
407func (z *bytesDecReader) stopTrack() (bs []byte) {
408	return z.b[z.t:z.c]
409}
410
411// ------------------------------------
412
413type decFnInfo struct {
414	d     *Decoder
415	ti    *typeInfo
416	xfFn  Ext
417	xfTag uint64
418	seq   seqType
419}
420
421// ----------------------------------------
422
423type decFn struct {
424	i decFnInfo
425	f func(*decFnInfo, reflect.Value)
426}
427
428func (f *decFnInfo) builtin(rv reflect.Value) {
429	f.d.d.DecodeBuiltin(f.ti.rtid, rv.Addr().Interface())
430}
431
432func (f *decFnInfo) rawExt(rv reflect.Value) {
433	f.d.d.DecodeExt(rv.Addr().Interface(), 0, nil)
434}
435
436func (f *decFnInfo) ext(rv reflect.Value) {
437	f.d.d.DecodeExt(rv.Addr().Interface(), f.xfTag, f.xfFn)
438}
439
440func (f *decFnInfo) getValueForUnmarshalInterface(rv reflect.Value, indir int8) (v interface{}) {
441	if indir == -1 {
442		v = rv.Addr().Interface()
443	} else if indir == 0 {
444		v = rv.Interface()
445	} else {
446		for j := int8(0); j < indir; j++ {
447			if rv.IsNil() {
448				rv.Set(reflect.New(rv.Type().Elem()))
449			}
450			rv = rv.Elem()
451		}
452		v = rv.Interface()
453	}
454	return
455}
456
457func (f *decFnInfo) selferUnmarshal(rv reflect.Value) {
458	f.getValueForUnmarshalInterface(rv, f.ti.csIndir).(Selfer).CodecDecodeSelf(f.d)
459}
460
461func (f *decFnInfo) binaryUnmarshal(rv reflect.Value) {
462	bm := f.getValueForUnmarshalInterface(rv, f.ti.bunmIndir).(encoding.BinaryUnmarshaler)
463	xbs := f.d.d.DecodeBytes(nil, false, true)
464	if fnerr := bm.UnmarshalBinary(xbs); fnerr != nil {
465		panic(fnerr)
466	}
467}
468
469func (f *decFnInfo) textUnmarshal(rv reflect.Value) {
470	tm := f.getValueForUnmarshalInterface(rv, f.ti.tunmIndir).(encoding.TextUnmarshaler)
471	fnerr := tm.UnmarshalText(f.d.d.DecodeBytes(f.d.b[:], true, true))
472	if fnerr != nil {
473		panic(fnerr)
474	}
475}
476
477func (f *decFnInfo) jsonUnmarshal(rv reflect.Value) {
478	tm := f.getValueForUnmarshalInterface(rv, f.ti.junmIndir).(jsonUnmarshaler)
479	// bs := f.d.d.DecodeBytes(f.d.b[:], true, true)
480	// grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself.
481	fnerr := tm.UnmarshalJSON(f.d.nextValueBytes())
482	if fnerr != nil {
483		panic(fnerr)
484	}
485}
486
487func (f *decFnInfo) kErr(rv reflect.Value) {
488	f.d.errorf("no decoding function defined for kind %v", rv.Kind())
489}
490
491func (f *decFnInfo) kString(rv reflect.Value) {
492	rv.SetString(f.d.d.DecodeString())
493}
494
495func (f *decFnInfo) kBool(rv reflect.Value) {
496	rv.SetBool(f.d.d.DecodeBool())
497}
498
499func (f *decFnInfo) kInt(rv reflect.Value) {
500	rv.SetInt(f.d.d.DecodeInt(intBitsize))
501}
502
503func (f *decFnInfo) kInt64(rv reflect.Value) {
504	rv.SetInt(f.d.d.DecodeInt(64))
505}
506
507func (f *decFnInfo) kInt32(rv reflect.Value) {
508	rv.SetInt(f.d.d.DecodeInt(32))
509}
510
511func (f *decFnInfo) kInt8(rv reflect.Value) {
512	rv.SetInt(f.d.d.DecodeInt(8))
513}
514
515func (f *decFnInfo) kInt16(rv reflect.Value) {
516	rv.SetInt(f.d.d.DecodeInt(16))
517}
518
519func (f *decFnInfo) kFloat32(rv reflect.Value) {
520	rv.SetFloat(f.d.d.DecodeFloat(true))
521}
522
523func (f *decFnInfo) kFloat64(rv reflect.Value) {
524	rv.SetFloat(f.d.d.DecodeFloat(false))
525}
526
527func (f *decFnInfo) kUint8(rv reflect.Value) {
528	rv.SetUint(f.d.d.DecodeUint(8))
529}
530
531func (f *decFnInfo) kUint64(rv reflect.Value) {
532	rv.SetUint(f.d.d.DecodeUint(64))
533}
534
535func (f *decFnInfo) kUint(rv reflect.Value) {
536	rv.SetUint(f.d.d.DecodeUint(uintBitsize))
537}
538
539func (f *decFnInfo) kUintptr(rv reflect.Value) {
540	rv.SetUint(f.d.d.DecodeUint(uintBitsize))
541}
542
543func (f *decFnInfo) kUint32(rv reflect.Value) {
544	rv.SetUint(f.d.d.DecodeUint(32))
545}
546
547func (f *decFnInfo) kUint16(rv reflect.Value) {
548	rv.SetUint(f.d.d.DecodeUint(16))
549}
550
551// func (f *decFnInfo) kPtr(rv reflect.Value) {
552// 	debugf(">>>>>>> ??? decode kPtr called - shouldn't get called")
553// 	if rv.IsNil() {
554// 		rv.Set(reflect.New(rv.Type().Elem()))
555// 	}
556// 	f.d.decodeValue(rv.Elem())
557// }
558
559// var kIntfCtr uint64
560
561func (f *decFnInfo) kInterfaceNaked() (rvn reflect.Value) {
562	// nil interface:
563	// use some hieristics to decode it appropriately
564	// based on the detected next value in the stream.
565	d := f.d
566	d.d.DecodeNaked()
567	n := &d.n
568	if n.v == valueTypeNil {
569		return
570	}
571	// We cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader).
572	// if num := f.ti.rt.NumMethod(); num > 0 {
573	if f.ti.numMeth > 0 {
574		d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth)
575		return
576	}
577	// var useRvn bool
578	switch n.v {
579	case valueTypeMap:
580		// if d.h.MapType == nil || d.h.MapType == mapIntfIntfTyp {
581		// } else if d.h.MapType == mapStrIntfTyp { // for json performance
582		// }
583		if d.mtid == 0 || d.mtid == mapIntfIntfTypId {
584			l := len(n.ms)
585			n.ms = append(n.ms, nil)
586			d.decode(&n.ms[l])
587			rvn = reflect.ValueOf(&n.ms[l]).Elem()
588			n.ms = n.ms[:l]
589		} else if d.mtid == mapStrIntfTypId { // for json performance
590			l := len(n.ns)
591			n.ns = append(n.ns, nil)
592			d.decode(&n.ns[l])
593			rvn = reflect.ValueOf(&n.ns[l]).Elem()
594			n.ns = n.ns[:l]
595		} else {
596			rvn = reflect.New(d.h.MapType).Elem()
597			d.decodeValue(rvn, nil)
598		}
599	case valueTypeArray:
600		// if d.h.SliceType == nil || d.h.SliceType == intfSliceTyp {
601		if d.stid == 0 || d.stid == intfSliceTypId {
602			l := len(n.ss)
603			n.ss = append(n.ss, nil)
604			d.decode(&n.ss[l])
605			rvn = reflect.ValueOf(&n.ss[l]).Elem()
606			n.ss = n.ss[:l]
607		} else {
608			rvn = reflect.New(d.h.SliceType).Elem()
609			d.decodeValue(rvn, nil)
610		}
611	case valueTypeExt:
612		var v interface{}
613		tag, bytes := n.u, n.l // calling decode below might taint the values
614		if bytes == nil {
615			l := len(n.is)
616			n.is = append(n.is, nil)
617			v2 := &n.is[l]
618			n.is = n.is[:l]
619			d.decode(v2)
620			v = *v2
621		}
622		bfn := d.h.getExtForTag(tag)
623		if bfn == nil {
624			var re RawExt
625			re.Tag = tag
626			re.Data = detachZeroCopyBytes(d.bytes, nil, bytes)
627			rvn = reflect.ValueOf(re)
628		} else {
629			rvnA := reflect.New(bfn.rt)
630			rvn = rvnA.Elem()
631			if bytes != nil {
632				bfn.ext.ReadExt(rvnA.Interface(), bytes)
633			} else {
634				bfn.ext.UpdateExt(rvnA.Interface(), v)
635			}
636		}
637	case valueTypeNil:
638		// no-op
639	case valueTypeInt:
640		rvn = reflect.ValueOf(&n.i).Elem()
641	case valueTypeUint:
642		rvn = reflect.ValueOf(&n.u).Elem()
643	case valueTypeFloat:
644		rvn = reflect.ValueOf(&n.f).Elem()
645	case valueTypeBool:
646		rvn = reflect.ValueOf(&n.b).Elem()
647	case valueTypeString, valueTypeSymbol:
648		rvn = reflect.ValueOf(&n.s).Elem()
649	case valueTypeBytes:
650		rvn = reflect.ValueOf(&n.l).Elem()
651	case valueTypeTimestamp:
652		rvn = reflect.ValueOf(&n.t).Elem()
653	default:
654		panic(fmt.Errorf("kInterfaceNaked: unexpected valueType: %d", n.v))
655	}
656	return
657}
658
659func (f *decFnInfo) kInterface(rv reflect.Value) {
660	// debugf("\t===> kInterface")
661
662	// Note:
663	// A consequence of how kInterface works, is that
664	// if an interface already contains something, we try
665	// to decode into what was there before.
666	// We do not replace with a generic value (as got from decodeNaked).
667
668	var rvn reflect.Value
669	if rv.IsNil() {
670		rvn = f.kInterfaceNaked()
671		if rvn.IsValid() {
672			rv.Set(rvn)
673		}
674	} else if f.d.h.InterfaceReset {
675		rvn = f.kInterfaceNaked()
676		if rvn.IsValid() {
677			rv.Set(rvn)
678		} else {
679			// reset to zero value based on current type in there.
680			rv.Set(reflect.Zero(rv.Elem().Type()))
681		}
682	} else {
683		rvn = rv.Elem()
684		// Note: interface{} is settable, but underlying type may not be.
685		// Consequently, we have to set the reflect.Value directly.
686		// if underlying type is settable (e.g. ptr or interface),
687		// we just decode into it.
688		// Else we create a settable value, decode into it, and set on the interface.
689		if rvn.CanSet() {
690			f.d.decodeValue(rvn, nil)
691		} else {
692			rvn2 := reflect.New(rvn.Type()).Elem()
693			rvn2.Set(rvn)
694			f.d.decodeValue(rvn2, nil)
695			rv.Set(rvn2)
696		}
697	}
698}
699
700func (f *decFnInfo) kStruct(rv reflect.Value) {
701	fti := f.ti
702	d := f.d
703	dd := d.d
704	cr := d.cr
705	ctyp := dd.ContainerType()
706	if ctyp == valueTypeMap {
707		containerLen := dd.ReadMapStart()
708		if containerLen == 0 {
709			if cr != nil {
710				cr.sendContainerState(containerMapEnd)
711			}
712			return
713		}
714		tisfi := fti.sfi
715		hasLen := containerLen >= 0
716		if hasLen {
717			for j := 0; j < containerLen; j++ {
718				// rvkencname := dd.DecodeString()
719				if cr != nil {
720					cr.sendContainerState(containerMapKey)
721				}
722				rvkencname := stringView(dd.DecodeBytes(f.d.b[:], true, true))
723				// rvksi := ti.getForEncName(rvkencname)
724				if cr != nil {
725					cr.sendContainerState(containerMapValue)
726				}
727				if k := fti.indexForEncName(rvkencname); k > -1 {
728					si := tisfi[k]
729					if dd.TryDecodeAsNil() {
730						si.setToZeroValue(rv)
731					} else {
732						d.decodeValue(si.field(rv, true), nil)
733					}
734				} else {
735					d.structFieldNotFound(-1, rvkencname)
736				}
737			}
738		} else {
739			for j := 0; !dd.CheckBreak(); j++ {
740				// rvkencname := dd.DecodeString()
741				if cr != nil {
742					cr.sendContainerState(containerMapKey)
743				}
744				rvkencname := stringView(dd.DecodeBytes(f.d.b[:], true, true))
745				// rvksi := ti.getForEncName(rvkencname)
746				if cr != nil {
747					cr.sendContainerState(containerMapValue)
748				}
749				if k := fti.indexForEncName(rvkencname); k > -1 {
750					si := tisfi[k]
751					if dd.TryDecodeAsNil() {
752						si.setToZeroValue(rv)
753					} else {
754						d.decodeValue(si.field(rv, true), nil)
755					}
756				} else {
757					d.structFieldNotFound(-1, rvkencname)
758				}
759			}
760		}
761		if cr != nil {
762			cr.sendContainerState(containerMapEnd)
763		}
764	} else if ctyp == valueTypeArray {
765		containerLen := dd.ReadArrayStart()
766		if containerLen == 0 {
767			if cr != nil {
768				cr.sendContainerState(containerArrayEnd)
769			}
770			return
771		}
772		// Not much gain from doing it two ways for array.
773		// Arrays are not used as much for structs.
774		hasLen := containerLen >= 0
775		for j, si := range fti.sfip {
776			if hasLen {
777				if j == containerLen {
778					break
779				}
780			} else if dd.CheckBreak() {
781				break
782			}
783			if cr != nil {
784				cr.sendContainerState(containerArrayElem)
785			}
786			if dd.TryDecodeAsNil() {
787				si.setToZeroValue(rv)
788			} else {
789				d.decodeValue(si.field(rv, true), nil)
790			}
791		}
792		if containerLen > len(fti.sfip) {
793			// read remaining values and throw away
794			for j := len(fti.sfip); j < containerLen; j++ {
795				if cr != nil {
796					cr.sendContainerState(containerArrayElem)
797				}
798				d.structFieldNotFound(j, "")
799			}
800		}
801		if cr != nil {
802			cr.sendContainerState(containerArrayEnd)
803		}
804	} else {
805		f.d.error(onlyMapOrArrayCanDecodeIntoStructErr)
806		return
807	}
808}
809
810func (f *decFnInfo) kSlice(rv reflect.Value) {
811	// A slice can be set from a map or array in stream.
812	// This way, the order can be kept (as order is lost with map).
813	ti := f.ti
814	d := f.d
815	dd := d.d
816	rtelem0 := ti.rt.Elem()
817	ctyp := dd.ContainerType()
818	if ctyp == valueTypeBytes || ctyp == valueTypeString {
819		// you can only decode bytes or string in the stream into a slice or array of bytes
820		if !(ti.rtid == uint8SliceTypId || rtelem0.Kind() == reflect.Uint8) {
821			f.d.errorf("bytes or string in the stream must be decoded into a slice or array of bytes, not %v", ti.rt)
822		}
823		if f.seq == seqTypeChan {
824			bs2 := dd.DecodeBytes(nil, false, true)
825			ch := rv.Interface().(chan<- byte)
826			for _, b := range bs2 {
827				ch <- b
828			}
829		} else {
830			rvbs := rv.Bytes()
831			bs2 := dd.DecodeBytes(rvbs, false, false)
832			if rvbs == nil && bs2 != nil || rvbs != nil && bs2 == nil || len(bs2) != len(rvbs) {
833				if rv.CanSet() {
834					rv.SetBytes(bs2)
835				} else {
836					copy(rvbs, bs2)
837				}
838			}
839		}
840		return
841	}
842
843	// array := f.seq == seqTypeChan
844
845	slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map)
846
847	// // an array can never return a nil slice. so no need to check f.array here.
848	if containerLenS == 0 {
849		if f.seq == seqTypeSlice {
850			if rv.IsNil() {
851				rv.Set(reflect.MakeSlice(ti.rt, 0, 0))
852			} else {
853				rv.SetLen(0)
854			}
855		} else if f.seq == seqTypeChan {
856			if rv.IsNil() {
857				rv.Set(reflect.MakeChan(ti.rt, 0))
858			}
859		}
860		slh.End()
861		return
862	}
863
864	rtelem := rtelem0
865	for rtelem.Kind() == reflect.Ptr {
866		rtelem = rtelem.Elem()
867	}
868	fn := d.getDecFn(rtelem, true, true)
869
870	var rv0, rv9 reflect.Value
871	rv0 = rv
872	rvChanged := false
873
874	// for j := 0; j < containerLenS; j++ {
875	var rvlen int
876	if containerLenS > 0 { // hasLen
877		if f.seq == seqTypeChan {
878			if rv.IsNil() {
879				rvlen, _ = decInferLen(containerLenS, f.d.h.MaxInitLen, int(rtelem0.Size()))
880				rv.Set(reflect.MakeChan(ti.rt, rvlen))
881			}
882			// handle chan specially:
883			for j := 0; j < containerLenS; j++ {
884				rv9 = reflect.New(rtelem0).Elem()
885				slh.ElemContainerState(j)
886				d.decodeValue(rv9, fn)
887				rv.Send(rv9)
888			}
889		} else { // slice or array
890			var truncated bool         // says len of sequence is not same as expected number of elements
891			numToRead := containerLenS // if truncated, reset numToRead
892
893			rvcap := rv.Cap()
894			rvlen = rv.Len()
895			if containerLenS > rvcap {
896				if f.seq == seqTypeArray {
897					d.arrayCannotExpand(rvlen, containerLenS)
898				} else {
899					oldRvlenGtZero := rvlen > 0
900					rvlen, truncated = decInferLen(containerLenS, f.d.h.MaxInitLen, int(rtelem0.Size()))
901					if truncated {
902						if rvlen <= rvcap {
903							rv.SetLen(rvlen)
904						} else {
905							rv = reflect.MakeSlice(ti.rt, rvlen, rvlen)
906							rvChanged = true
907						}
908					} else {
909						rv = reflect.MakeSlice(ti.rt, rvlen, rvlen)
910						rvChanged = true
911					}
912					if rvChanged && oldRvlenGtZero && !isImmutableKind(rtelem0.Kind()) {
913						reflect.Copy(rv, rv0) // only copy up to length NOT cap i.e. rv0.Slice(0, rvcap)
914					}
915					rvcap = rvlen
916				}
917				numToRead = rvlen
918			} else if containerLenS != rvlen {
919				if f.seq == seqTypeSlice {
920					rv.SetLen(containerLenS)
921					rvlen = containerLenS
922				}
923			}
924			j := 0
925			// we read up to the numToRead
926			for ; j < numToRead; j++ {
927				slh.ElemContainerState(j)
928				d.decodeValue(rv.Index(j), fn)
929			}
930
931			// if slice, expand and read up to containerLenS (or EOF) iff truncated
932			// if array, swallow all the rest.
933
934			if f.seq == seqTypeArray {
935				for ; j < containerLenS; j++ {
936					slh.ElemContainerState(j)
937					d.swallow()
938				}
939			} else if truncated { // slice was truncated, as chan NOT in this block
940				for ; j < containerLenS; j++ {
941					rv = expandSliceValue(rv, 1)
942					rv9 = rv.Index(j)
943					if resetSliceElemToZeroValue {
944						rv9.Set(reflect.Zero(rtelem0))
945					}
946					slh.ElemContainerState(j)
947					d.decodeValue(rv9, fn)
948				}
949			}
950		}
951	} else {
952		rvlen = rv.Len()
953		j := 0
954		for ; !dd.CheckBreak(); j++ {
955			if f.seq == seqTypeChan {
956				slh.ElemContainerState(j)
957				rv9 = reflect.New(rtelem0).Elem()
958				d.decodeValue(rv9, fn)
959				rv.Send(rv9)
960			} else {
961				// if indefinite, etc, then expand the slice if necessary
962				var decodeIntoBlank bool
963				if j >= rvlen {
964					if f.seq == seqTypeArray {
965						d.arrayCannotExpand(rvlen, j+1)
966						decodeIntoBlank = true
967					} else { // if f.seq == seqTypeSlice
968						// rv = reflect.Append(rv, reflect.Zero(rtelem0)) // uses append logic, plus varargs
969						rv = expandSliceValue(rv, 1)
970						rv9 = rv.Index(j)
971						// rv.Index(rv.Len() - 1).Set(reflect.Zero(rtelem0))
972						if resetSliceElemToZeroValue {
973							rv9.Set(reflect.Zero(rtelem0))
974						}
975						rvlen++
976						rvChanged = true
977					}
978				} else { // slice or array
979					rv9 = rv.Index(j)
980				}
981				slh.ElemContainerState(j)
982				if decodeIntoBlank {
983					d.swallow()
984				} else { // seqTypeSlice
985					d.decodeValue(rv9, fn)
986				}
987			}
988		}
989		if f.seq == seqTypeSlice {
990			if j < rvlen {
991				rv.SetLen(j)
992			} else if j == 0 && rv.IsNil() {
993				rv = reflect.MakeSlice(ti.rt, 0, 0)
994				rvChanged = true
995			}
996		}
997	}
998	slh.End()
999
1000	if rvChanged {
1001		rv0.Set(rv)
1002	}
1003}
1004
1005func (f *decFnInfo) kArray(rv reflect.Value) {
1006	// f.d.decodeValue(rv.Slice(0, rv.Len()))
1007	f.kSlice(rv.Slice(0, rv.Len()))
1008}
1009
1010func (f *decFnInfo) kMap(rv reflect.Value) {
1011	d := f.d
1012	dd := d.d
1013	containerLen := dd.ReadMapStart()
1014	cr := d.cr
1015	ti := f.ti
1016	if rv.IsNil() {
1017		rv.Set(reflect.MakeMap(ti.rt))
1018	}
1019
1020	if containerLen == 0 {
1021		if cr != nil {
1022			cr.sendContainerState(containerMapEnd)
1023		}
1024		return
1025	}
1026
1027	ktype, vtype := ti.rt.Key(), ti.rt.Elem()
1028	ktypeId := reflect.ValueOf(ktype).Pointer()
1029	vtypeKind := vtype.Kind()
1030	var keyFn, valFn *decFn
1031	var xtyp reflect.Type
1032	for xtyp = ktype; xtyp.Kind() == reflect.Ptr; xtyp = xtyp.Elem() {
1033	}
1034	keyFn = d.getDecFn(xtyp, true, true)
1035	for xtyp = vtype; xtyp.Kind() == reflect.Ptr; xtyp = xtyp.Elem() {
1036	}
1037	valFn = d.getDecFn(xtyp, true, true)
1038	var mapGet, mapSet bool
1039	if !f.d.h.MapValueReset {
1040		// if pointer, mapGet = true
1041		// if interface, mapGet = true if !DecodeNakedAlways (else false)
1042		// if builtin, mapGet = false
1043		// else mapGet = true
1044		if vtypeKind == reflect.Ptr {
1045			mapGet = true
1046		} else if vtypeKind == reflect.Interface {
1047			if !f.d.h.InterfaceReset {
1048				mapGet = true
1049			}
1050		} else if !isImmutableKind(vtypeKind) {
1051			mapGet = true
1052		}
1053	}
1054
1055	var rvk, rvv, rvz reflect.Value
1056
1057	// for j := 0; j < containerLen; j++ {
1058	if containerLen > 0 {
1059		for j := 0; j < containerLen; j++ {
1060			rvk = reflect.New(ktype).Elem()
1061			if cr != nil {
1062				cr.sendContainerState(containerMapKey)
1063			}
1064			d.decodeValue(rvk, keyFn)
1065
1066			// special case if a byte array.
1067			if ktypeId == intfTypId {
1068				rvk = rvk.Elem()
1069				if rvk.Type() == uint8SliceTyp {
1070					rvk = reflect.ValueOf(d.string(rvk.Bytes()))
1071				}
1072			}
1073			mapSet = true // set to false if u do a get, and its a pointer, and exists
1074			if mapGet {
1075				rvv = rv.MapIndex(rvk)
1076				if rvv.IsValid() {
1077					if vtypeKind == reflect.Ptr {
1078						mapSet = false
1079					}
1080				} else {
1081					if rvz.IsValid() {
1082						rvz.Set(reflect.Zero(vtype))
1083					} else {
1084						rvz = reflect.New(vtype).Elem()
1085					}
1086					rvv = rvz
1087				}
1088			} else {
1089				if rvz.IsValid() {
1090					rvz.Set(reflect.Zero(vtype))
1091				} else {
1092					rvz = reflect.New(vtype).Elem()
1093				}
1094				rvv = rvz
1095			}
1096			if cr != nil {
1097				cr.sendContainerState(containerMapValue)
1098			}
1099			d.decodeValue(rvv, valFn)
1100			if mapSet {
1101				rv.SetMapIndex(rvk, rvv)
1102			}
1103		}
1104	} else {
1105		for j := 0; !dd.CheckBreak(); j++ {
1106			rvk = reflect.New(ktype).Elem()
1107			if cr != nil {
1108				cr.sendContainerState(containerMapKey)
1109			}
1110			d.decodeValue(rvk, keyFn)
1111
1112			// special case if a byte array.
1113			if ktypeId == intfTypId {
1114				rvk = rvk.Elem()
1115				if rvk.Type() == uint8SliceTyp {
1116					rvk = reflect.ValueOf(d.string(rvk.Bytes()))
1117				}
1118			}
1119			mapSet = true // set to false if u do a get, and its a pointer, and exists
1120			if mapGet {
1121				rvv = rv.MapIndex(rvk)
1122				if rvv.IsValid() {
1123					if vtypeKind == reflect.Ptr {
1124						mapSet = false
1125					}
1126				} else {
1127					if rvz.IsValid() {
1128						rvz.Set(reflect.Zero(vtype))
1129					} else {
1130						rvz = reflect.New(vtype).Elem()
1131					}
1132					rvv = rvz
1133				}
1134			} else {
1135				if rvz.IsValid() {
1136					rvz.Set(reflect.Zero(vtype))
1137				} else {
1138					rvz = reflect.New(vtype).Elem()
1139				}
1140				rvv = rvz
1141			}
1142			if cr != nil {
1143				cr.sendContainerState(containerMapValue)
1144			}
1145			d.decodeValue(rvv, valFn)
1146			if mapSet {
1147				rv.SetMapIndex(rvk, rvv)
1148			}
1149		}
1150	}
1151	if cr != nil {
1152		cr.sendContainerState(containerMapEnd)
1153	}
1154}
1155
1156type decRtidFn struct {
1157	rtid uintptr
1158	fn   decFn
1159}
1160
1161// decNaked is used to keep track of the primitives decoded.
1162// Without it, we would have to decode each primitive and wrap it
1163// in an interface{}, causing an allocation.
1164// In this model, the primitives are decoded in a "pseudo-atomic" fashion,
1165// so we can rest assured that no other decoding happens while these
1166// primitives are being decoded.
1167//
1168// maps and arrays are not handled by this mechanism.
1169// However, RawExt is, and we accomodate for extensions that decode
1170// RawExt from DecodeNaked, but need to decode the value subsequently.
1171// kInterfaceNaked and swallow, which call DecodeNaked, handle this caveat.
1172//
1173// However, decNaked also keeps some arrays of default maps and slices
1174// used in DecodeNaked. This way, we can get a pointer to it
1175// without causing a new heap allocation.
1176//
1177// kInterfaceNaked will ensure that there is no allocation for the common
1178// uses.
1179type decNaked struct {
1180	// r RawExt // used for RawExt, uint, []byte.
1181	u uint64
1182	i int64
1183	f float64
1184	l []byte
1185	s string
1186	t time.Time
1187	b bool
1188	v valueType
1189
1190	// stacks for reducing allocation
1191	is []interface{}
1192	ms []map[interface{}]interface{}
1193	ns []map[string]interface{}
1194	ss [][]interface{}
1195	// rs []RawExt
1196
1197	// keep arrays at the bottom? Chance is that they are not used much.
1198	ia [4]interface{}
1199	ma [4]map[interface{}]interface{}
1200	na [4]map[string]interface{}
1201	sa [4][]interface{}
1202	// ra [2]RawExt
1203}
1204
1205func (n *decNaked) reset() {
1206	if n.ss != nil {
1207		n.ss = n.ss[:0]
1208	}
1209	if n.is != nil {
1210		n.is = n.is[:0]
1211	}
1212	if n.ms != nil {
1213		n.ms = n.ms[:0]
1214	}
1215	if n.ns != nil {
1216		n.ns = n.ns[:0]
1217	}
1218}
1219
1220// A Decoder reads and decodes an object from an input stream in the codec format.
1221type Decoder struct {
1222	// hopefully, reduce derefencing cost by laying the decReader inside the Decoder.
1223	// Try to put things that go together to fit within a cache line (8 words).
1224
1225	d decDriver
1226	// NOTE: Decoder shouldn't call it's read methods,
1227	// as the handler MAY need to do some coordination.
1228	r decReader
1229	// sa [initCollectionCap]decRtidFn
1230	h  *BasicHandle
1231	hh Handle
1232
1233	be    bool // is binary encoding
1234	bytes bool // is bytes reader
1235	js    bool // is json handle
1236
1237	rb bytesDecReader
1238	ri ioDecReader
1239	cr containerStateRecv
1240
1241	s []decRtidFn
1242	f map[uintptr]*decFn
1243
1244	// _  uintptr // for alignment purposes, so next one starts from a cache line
1245
1246	// cache the mapTypeId and sliceTypeId for faster comparisons
1247	mtid uintptr
1248	stid uintptr
1249
1250	n  decNaked
1251	b  [scratchByteArrayLen]byte
1252	is map[string]string // used for interning strings
1253}
1254
1255// NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader.
1256//
1257// For efficiency, Users are encouraged to pass in a memory buffered reader
1258// (eg bufio.Reader, bytes.Buffer).
1259func NewDecoder(r io.Reader, h Handle) *Decoder {
1260	d := newDecoder(h)
1261	d.Reset(r)
1262	return d
1263}
1264
1265// NewDecoderBytes returns a Decoder which efficiently decodes directly
1266// from a byte slice with zero copying.
1267func NewDecoderBytes(in []byte, h Handle) *Decoder {
1268	d := newDecoder(h)
1269	d.ResetBytes(in)
1270	return d
1271}
1272
1273func newDecoder(h Handle) *Decoder {
1274	d := &Decoder{hh: h, h: h.getBasicHandle(), be: h.isBinary()}
1275	n := &d.n
1276	// n.rs = n.ra[:0]
1277	n.ms = n.ma[:0]
1278	n.is = n.ia[:0]
1279	n.ns = n.na[:0]
1280	n.ss = n.sa[:0]
1281	_, d.js = h.(*JsonHandle)
1282	if d.h.InternString {
1283		d.is = make(map[string]string, 32)
1284	}
1285	d.d = h.newDecDriver(d)
1286	d.cr, _ = d.d.(containerStateRecv)
1287	// d.d = h.newDecDriver(decReaderT{true, &d.rb, &d.ri})
1288	return d
1289}
1290
1291func (d *Decoder) resetCommon() {
1292	d.n.reset()
1293	d.d.reset()
1294	// reset all things which were cached from the Handle,
1295	// but could be changed.
1296	d.mtid, d.stid = 0, 0
1297	if d.h.MapType != nil {
1298		d.mtid = reflect.ValueOf(d.h.MapType).Pointer()
1299	}
1300	if d.h.SliceType != nil {
1301		d.stid = reflect.ValueOf(d.h.SliceType).Pointer()
1302	}
1303}
1304
1305func (d *Decoder) Reset(r io.Reader) {
1306	d.ri.x = &d.b
1307	// d.s = d.sa[:0]
1308	d.ri.bs.r = r
1309	var ok bool
1310	d.ri.br, ok = r.(decReaderByteScanner)
1311	if !ok {
1312		d.ri.br = &d.ri.bs
1313	}
1314	d.r = &d.ri
1315	d.resetCommon()
1316}
1317
1318func (d *Decoder) ResetBytes(in []byte) {
1319	// d.s = d.sa[:0]
1320	d.rb.reset(in)
1321	d.r = &d.rb
1322	d.resetCommon()
1323}
1324
1325// func (d *Decoder) sendContainerState(c containerState) {
1326// 	if d.cr != nil {
1327// 		d.cr.sendContainerState(c)
1328// 	}
1329// }
1330
1331// Decode decodes the stream from reader and stores the result in the
1332// value pointed to by v. v cannot be a nil pointer. v can also be
1333// a reflect.Value of a pointer.
1334//
1335// Note that a pointer to a nil interface is not a nil pointer.
1336// If you do not know what type of stream it is, pass in a pointer to a nil interface.
1337// We will decode and store a value in that nil interface.
1338//
1339// Sample usages:
1340//   // Decoding into a non-nil typed value
1341//   var f float32
1342//   err = codec.NewDecoder(r, handle).Decode(&f)
1343//
1344//   // Decoding into nil interface
1345//   var v interface{}
1346//   dec := codec.NewDecoder(r, handle)
1347//   err = dec.Decode(&v)
1348//
1349// When decoding into a nil interface{}, we will decode into an appropriate value based
1350// on the contents of the stream:
1351//   - Numbers are decoded as float64, int64 or uint64.
1352//   - Other values are decoded appropriately depending on the type:
1353//     bool, string, []byte, time.Time, etc
1354//   - Extensions are decoded as RawExt (if no ext function registered for the tag)
1355// Configurations exist on the Handle to override defaults
1356// (e.g. for MapType, SliceType and how to decode raw bytes).
1357//
1358// When decoding into a non-nil interface{} value, the mode of encoding is based on the
1359// type of the value. When a value is seen:
1360//   - If an extension is registered for it, call that extension function
1361//   - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error
1362//   - Else decode it based on its reflect.Kind
1363//
1364// There are some special rules when decoding into containers (slice/array/map/struct).
1365// Decode will typically use the stream contents to UPDATE the container.
1366//   - A map can be decoded from a stream map, by updating matching keys.
1367//   - A slice can be decoded from a stream array,
1368//     by updating the first n elements, where n is length of the stream.
1369//   - A slice can be decoded from a stream map, by decoding as if
1370//     it contains a sequence of key-value pairs.
1371//   - A struct can be decoded from a stream map, by updating matching fields.
1372//   - A struct can be decoded from a stream array,
1373//     by updating fields as they occur in the struct (by index).
1374//
1375// When decoding a stream map or array with length of 0 into a nil map or slice,
1376// we reset the destination map or slice to a zero-length value.
1377//
1378// However, when decoding a stream nil, we reset the destination container
1379// to its "zero" value (e.g. nil for slice/map, etc).
1380//
1381func (d *Decoder) Decode(v interface{}) (err error) {
1382	defer panicToErr(&err)
1383	d.decode(v)
1384	return
1385}
1386
1387// this is not a smart swallow, as it allocates objects and does unnecessary work.
1388func (d *Decoder) swallowViaHammer() {
1389	var blank interface{}
1390	d.decodeValue(reflect.ValueOf(&blank).Elem(), nil)
1391}
1392
1393func (d *Decoder) swallow() {
1394	// smarter decode that just swallows the content
1395	dd := d.d
1396	if dd.TryDecodeAsNil() {
1397		return
1398	}
1399	cr := d.cr
1400	switch dd.ContainerType() {
1401	case valueTypeMap:
1402		containerLen := dd.ReadMapStart()
1403		clenGtEqualZero := containerLen >= 0
1404		for j := 0; ; j++ {
1405			if clenGtEqualZero {
1406				if j >= containerLen {
1407					break
1408				}
1409			} else if dd.CheckBreak() {
1410				break
1411			}
1412			if cr != nil {
1413				cr.sendContainerState(containerMapKey)
1414			}
1415			d.swallow()
1416			if cr != nil {
1417				cr.sendContainerState(containerMapValue)
1418			}
1419			d.swallow()
1420		}
1421		if cr != nil {
1422			cr.sendContainerState(containerMapEnd)
1423		}
1424	case valueTypeArray:
1425		containerLenS := dd.ReadArrayStart()
1426		clenGtEqualZero := containerLenS >= 0
1427		for j := 0; ; j++ {
1428			if clenGtEqualZero {
1429				if j >= containerLenS {
1430					break
1431				}
1432			} else if dd.CheckBreak() {
1433				break
1434			}
1435			if cr != nil {
1436				cr.sendContainerState(containerArrayElem)
1437			}
1438			d.swallow()
1439		}
1440		if cr != nil {
1441			cr.sendContainerState(containerArrayEnd)
1442		}
1443	case valueTypeBytes:
1444		dd.DecodeBytes(d.b[:], false, true)
1445	case valueTypeString:
1446		dd.DecodeBytes(d.b[:], true, true)
1447		// dd.DecodeStringAsBytes(d.b[:])
1448	default:
1449		// these are all primitives, which we can get from decodeNaked
1450		// if RawExt using Value, complete the processing.
1451		dd.DecodeNaked()
1452		if n := &d.n; n.v == valueTypeExt && n.l == nil {
1453			l := len(n.is)
1454			n.is = append(n.is, nil)
1455			v2 := &n.is[l]
1456			n.is = n.is[:l]
1457			d.decode(v2)
1458		}
1459	}
1460}
1461
1462// MustDecode is like Decode, but panics if unable to Decode.
1463// This provides insight to the code location that triggered the error.
1464func (d *Decoder) MustDecode(v interface{}) {
1465	d.decode(v)
1466}
1467
1468func (d *Decoder) decode(iv interface{}) {
1469	// if ics, ok := iv.(Selfer); ok {
1470	// 	ics.CodecDecodeSelf(d)
1471	// 	return
1472	// }
1473
1474	if d.d.TryDecodeAsNil() {
1475		switch v := iv.(type) {
1476		case nil:
1477		case *string:
1478			*v = ""
1479		case *bool:
1480			*v = false
1481		case *int:
1482			*v = 0
1483		case *int8:
1484			*v = 0
1485		case *int16:
1486			*v = 0
1487		case *int32:
1488			*v = 0
1489		case *int64:
1490			*v = 0
1491		case *uint:
1492			*v = 0
1493		case *uint8:
1494			*v = 0
1495		case *uint16:
1496			*v = 0
1497		case *uint32:
1498			*v = 0
1499		case *uint64:
1500			*v = 0
1501		case *float32:
1502			*v = 0
1503		case *float64:
1504			*v = 0
1505		case *[]uint8:
1506			*v = nil
1507		case reflect.Value:
1508			if v.Kind() != reflect.Ptr || v.IsNil() {
1509				d.errNotValidPtrValue(v)
1510			}
1511			// d.chkPtrValue(v)
1512			v = v.Elem()
1513			if v.IsValid() {
1514				v.Set(reflect.Zero(v.Type()))
1515			}
1516		default:
1517			rv := reflect.ValueOf(iv)
1518			if rv.Kind() != reflect.Ptr || rv.IsNil() {
1519				d.errNotValidPtrValue(rv)
1520			}
1521			// d.chkPtrValue(rv)
1522			rv = rv.Elem()
1523			if rv.IsValid() {
1524				rv.Set(reflect.Zero(rv.Type()))
1525			}
1526		}
1527		return
1528	}
1529
1530	switch v := iv.(type) {
1531	case nil:
1532		d.error(cannotDecodeIntoNilErr)
1533		return
1534
1535	case Selfer:
1536		v.CodecDecodeSelf(d)
1537
1538	case reflect.Value:
1539		if v.Kind() != reflect.Ptr || v.IsNil() {
1540			d.errNotValidPtrValue(v)
1541		}
1542		// d.chkPtrValue(v)
1543		d.decodeValueNotNil(v.Elem(), nil)
1544
1545	case *string:
1546
1547		*v = d.d.DecodeString()
1548	case *bool:
1549		*v = d.d.DecodeBool()
1550	case *int:
1551		*v = int(d.d.DecodeInt(intBitsize))
1552	case *int8:
1553		*v = int8(d.d.DecodeInt(8))
1554	case *int16:
1555		*v = int16(d.d.DecodeInt(16))
1556	case *int32:
1557		*v = int32(d.d.DecodeInt(32))
1558	case *int64:
1559		*v = d.d.DecodeInt(64)
1560	case *uint:
1561		*v = uint(d.d.DecodeUint(uintBitsize))
1562	case *uint8:
1563		*v = uint8(d.d.DecodeUint(8))
1564	case *uint16:
1565		*v = uint16(d.d.DecodeUint(16))
1566	case *uint32:
1567		*v = uint32(d.d.DecodeUint(32))
1568	case *uint64:
1569		*v = d.d.DecodeUint(64)
1570	case *float32:
1571		*v = float32(d.d.DecodeFloat(true))
1572	case *float64:
1573		*v = d.d.DecodeFloat(false)
1574	case *[]uint8:
1575		*v = d.d.DecodeBytes(*v, false, false)
1576
1577	case *interface{}:
1578		d.decodeValueNotNil(reflect.ValueOf(iv).Elem(), nil)
1579
1580	default:
1581		if !fastpathDecodeTypeSwitch(iv, d) {
1582			d.decodeI(iv, true, false, false, false)
1583		}
1584	}
1585}
1586
1587func (d *Decoder) preDecodeValue(rv reflect.Value, tryNil bool) (rv2 reflect.Value, proceed bool) {
1588	if tryNil && d.d.TryDecodeAsNil() {
1589		// No need to check if a ptr, recursively, to determine
1590		// whether to set value to nil.
1591		// Just always set value to its zero type.
1592		if rv.IsValid() { // rv.CanSet() // always settable, except it's invalid
1593			rv.Set(reflect.Zero(rv.Type()))
1594		}
1595		return
1596	}
1597
1598	// If stream is not containing a nil value, then we can deref to the base
1599	// non-pointer value, and decode into that.
1600	for rv.Kind() == reflect.Ptr {
1601		if rv.IsNil() {
1602			rv.Set(reflect.New(rv.Type().Elem()))
1603		}
1604		rv = rv.Elem()
1605	}
1606	return rv, true
1607}
1608
1609func (d *Decoder) decodeI(iv interface{}, checkPtr, tryNil, checkFastpath, checkCodecSelfer bool) {
1610	rv := reflect.ValueOf(iv)
1611	if checkPtr {
1612		if rv.Kind() != reflect.Ptr || rv.IsNil() {
1613			d.errNotValidPtrValue(rv)
1614		}
1615		// d.chkPtrValue(rv)
1616	}
1617	rv, proceed := d.preDecodeValue(rv, tryNil)
1618	if proceed {
1619		fn := d.getDecFn(rv.Type(), checkFastpath, checkCodecSelfer)
1620		fn.f(&fn.i, rv)
1621	}
1622}
1623
1624func (d *Decoder) decodeValue(rv reflect.Value, fn *decFn) {
1625	if rv, proceed := d.preDecodeValue(rv, true); proceed {
1626		if fn == nil {
1627			fn = d.getDecFn(rv.Type(), true, true)
1628		}
1629		fn.f(&fn.i, rv)
1630	}
1631}
1632
1633func (d *Decoder) decodeValueNotNil(rv reflect.Value, fn *decFn) {
1634	if rv, proceed := d.preDecodeValue(rv, false); proceed {
1635		if fn == nil {
1636			fn = d.getDecFn(rv.Type(), true, true)
1637		}
1638		fn.f(&fn.i, rv)
1639	}
1640}
1641
1642func (d *Decoder) getDecFn(rt reflect.Type, checkFastpath, checkCodecSelfer bool) (fn *decFn) {
1643	rtid := reflect.ValueOf(rt).Pointer()
1644
1645	// retrieve or register a focus'ed function for this type
1646	// to eliminate need to do the retrieval multiple times
1647
1648	// if d.f == nil && d.s == nil { debugf("---->Creating new dec f map for type: %v\n", rt) }
1649	var ok bool
1650	if useMapForCodecCache {
1651		fn, ok = d.f[rtid]
1652	} else {
1653		for i := range d.s {
1654			v := &(d.s[i])
1655			if v.rtid == rtid {
1656				fn, ok = &(v.fn), true
1657				break
1658			}
1659		}
1660	}
1661	if ok {
1662		return
1663	}
1664
1665	if useMapForCodecCache {
1666		if d.f == nil {
1667			d.f = make(map[uintptr]*decFn, initCollectionCap)
1668		}
1669		fn = new(decFn)
1670		d.f[rtid] = fn
1671	} else {
1672		if d.s == nil {
1673			d.s = make([]decRtidFn, 0, initCollectionCap)
1674		}
1675		d.s = append(d.s, decRtidFn{rtid: rtid})
1676		fn = &(d.s[len(d.s)-1]).fn
1677	}
1678
1679	// debugf("\tCreating new dec fn for type: %v\n", rt)
1680	ti := d.h.getTypeInfo(rtid, rt)
1681	fi := &(fn.i)
1682	fi.d = d
1683	fi.ti = ti
1684
1685	// An extension can be registered for any type, regardless of the Kind
1686	// (e.g. type BitSet int64, type MyStruct { / * unexported fields * / }, type X []int, etc.
1687	//
1688	// We can't check if it's an extension byte here first, because the user may have
1689	// registered a pointer or non-pointer type, meaning we may have to recurse first
1690	// before matching a mapped type, even though the extension byte is already detected.
1691	//
1692	// NOTE: if decoding into a nil interface{}, we return a non-nil
1693	// value except even if the container registers a length of 0.
1694	if checkCodecSelfer && ti.cs {
1695		fn.f = (*decFnInfo).selferUnmarshal
1696	} else if rtid == rawExtTypId {
1697		fn.f = (*decFnInfo).rawExt
1698	} else if d.d.IsBuiltinType(rtid) {
1699		fn.f = (*decFnInfo).builtin
1700	} else if xfFn := d.h.getExt(rtid); xfFn != nil {
1701		fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
1702		fn.f = (*decFnInfo).ext
1703	} else if supportMarshalInterfaces && d.be && ti.bunm {
1704		fn.f = (*decFnInfo).binaryUnmarshal
1705	} else if supportMarshalInterfaces && !d.be && d.js && ti.junm {
1706		//If JSON, we should check JSONUnmarshal before textUnmarshal
1707		fn.f = (*decFnInfo).jsonUnmarshal
1708	} else if supportMarshalInterfaces && !d.be && ti.tunm {
1709		fn.f = (*decFnInfo).textUnmarshal
1710	} else {
1711		rk := rt.Kind()
1712		if fastpathEnabled && checkFastpath && (rk == reflect.Map || rk == reflect.Slice) {
1713			if rt.PkgPath() == "" {
1714				if idx := fastpathAV.index(rtid); idx != -1 {
1715					fn.f = fastpathAV[idx].decfn
1716				}
1717			} else {
1718				// use mapping for underlying type if there
1719				ok = false
1720				var rtu reflect.Type
1721				if rk == reflect.Map {
1722					rtu = reflect.MapOf(rt.Key(), rt.Elem())
1723				} else {
1724					rtu = reflect.SliceOf(rt.Elem())
1725				}
1726				rtuid := reflect.ValueOf(rtu).Pointer()
1727				if idx := fastpathAV.index(rtuid); idx != -1 {
1728					xfnf := fastpathAV[idx].decfn
1729					xrt := fastpathAV[idx].rt
1730					fn.f = func(xf *decFnInfo, xrv reflect.Value) {
1731						// xfnf(xf, xrv.Convert(xrt))
1732						xfnf(xf, xrv.Addr().Convert(reflect.PtrTo(xrt)).Elem())
1733					}
1734				}
1735			}
1736		}
1737		if fn.f == nil {
1738			switch rk {
1739			case reflect.String:
1740				fn.f = (*decFnInfo).kString
1741			case reflect.Bool:
1742				fn.f = (*decFnInfo).kBool
1743			case reflect.Int:
1744				fn.f = (*decFnInfo).kInt
1745			case reflect.Int64:
1746				fn.f = (*decFnInfo).kInt64
1747			case reflect.Int32:
1748				fn.f = (*decFnInfo).kInt32
1749			case reflect.Int8:
1750				fn.f = (*decFnInfo).kInt8
1751			case reflect.Int16:
1752				fn.f = (*decFnInfo).kInt16
1753			case reflect.Float32:
1754				fn.f = (*decFnInfo).kFloat32
1755			case reflect.Float64:
1756				fn.f = (*decFnInfo).kFloat64
1757			case reflect.Uint8:
1758				fn.f = (*decFnInfo).kUint8
1759			case reflect.Uint64:
1760				fn.f = (*decFnInfo).kUint64
1761			case reflect.Uint:
1762				fn.f = (*decFnInfo).kUint
1763			case reflect.Uint32:
1764				fn.f = (*decFnInfo).kUint32
1765			case reflect.Uint16:
1766				fn.f = (*decFnInfo).kUint16
1767				// case reflect.Ptr:
1768				// 	fn.f = (*decFnInfo).kPtr
1769			case reflect.Uintptr:
1770				fn.f = (*decFnInfo).kUintptr
1771			case reflect.Interface:
1772				fn.f = (*decFnInfo).kInterface
1773			case reflect.Struct:
1774				fn.f = (*decFnInfo).kStruct
1775			case reflect.Chan:
1776				fi.seq = seqTypeChan
1777				fn.f = (*decFnInfo).kSlice
1778			case reflect.Slice:
1779				fi.seq = seqTypeSlice
1780				fn.f = (*decFnInfo).kSlice
1781			case reflect.Array:
1782				fi.seq = seqTypeArray
1783				fn.f = (*decFnInfo).kArray
1784			case reflect.Map:
1785				fn.f = (*decFnInfo).kMap
1786			default:
1787				fn.f = (*decFnInfo).kErr
1788			}
1789		}
1790	}
1791
1792	return
1793}
1794
1795func (d *Decoder) structFieldNotFound(index int, rvkencname string) {
1796	if d.h.ErrorIfNoField {
1797		if index >= 0 {
1798			d.errorf("no matching struct field found when decoding stream array at index %v", index)
1799			return
1800		} else if rvkencname != "" {
1801			d.errorf("no matching struct field found when decoding stream map with key %s", rvkencname)
1802			return
1803		}
1804	}
1805	d.swallow()
1806}
1807
1808func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) {
1809	if d.h.ErrorIfNoArrayExpand {
1810		d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen)
1811	}
1812}
1813
1814func (d *Decoder) chkPtrValue(rv reflect.Value) {
1815	// We can only decode into a non-nil pointer
1816	if rv.Kind() == reflect.Ptr && !rv.IsNil() {
1817		return
1818	}
1819	d.errNotValidPtrValue(rv)
1820}
1821
1822func (d *Decoder) errNotValidPtrValue(rv reflect.Value) {
1823	if !rv.IsValid() {
1824		d.error(cannotDecodeIntoNilErr)
1825		return
1826	}
1827	if !rv.CanInterface() {
1828		d.errorf("cannot decode into a value without an interface: %v", rv)
1829		return
1830	}
1831	rvi := rv.Interface()
1832	d.errorf("cannot decode into non-pointer or nil pointer. Got: %v, %T, %v", rv.Kind(), rvi, rvi)
1833}
1834
1835func (d *Decoder) error(err error) {
1836	panic(err)
1837}
1838
1839func (d *Decoder) errorf(format string, params ...interface{}) {
1840	params2 := make([]interface{}, len(params)+1)
1841	params2[0] = d.r.numread()
1842	copy(params2[1:], params)
1843	err := fmt.Errorf("[pos %d]: "+format, params2...)
1844	panic(err)
1845}
1846
1847func (d *Decoder) string(v []byte) (s string) {
1848	if d.is != nil {
1849		s, ok := d.is[string(v)] // no allocation here.
1850		if !ok {
1851			s = string(v)
1852			d.is[s] = s
1853		}
1854		return s
1855	}
1856	return string(v) // don't return stringView, as we need a real string here.
1857}
1858
1859func (d *Decoder) intern(s string) {
1860	if d.is != nil {
1861		d.is[s] = s
1862	}
1863}
1864
1865func (d *Decoder) nextValueBytes() []byte {
1866	d.d.uncacheRead()
1867	d.r.track()
1868	d.swallow()
1869	return d.r.stopTrack()
1870}
1871
1872// --------------------------------------------------
1873
1874// decSliceHelper assists when decoding into a slice, from a map or an array in the stream.
1875// A slice can be set from a map or array in stream. This supports the MapBySlice interface.
1876type decSliceHelper struct {
1877	d *Decoder
1878	// ct valueType
1879	array bool
1880}
1881
1882func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) {
1883	dd := d.d
1884	ctyp := dd.ContainerType()
1885	if ctyp == valueTypeArray {
1886		x.array = true
1887		clen = dd.ReadArrayStart()
1888	} else if ctyp == valueTypeMap {
1889		clen = dd.ReadMapStart() * 2
1890	} else {
1891		d.errorf("only encoded map or array can be decoded into a slice (%d)", ctyp)
1892	}
1893	// x.ct = ctyp
1894	x.d = d
1895	return
1896}
1897
1898func (x decSliceHelper) End() {
1899	cr := x.d.cr
1900	if cr == nil {
1901		return
1902	}
1903	if x.array {
1904		cr.sendContainerState(containerArrayEnd)
1905	} else {
1906		cr.sendContainerState(containerMapEnd)
1907	}
1908}
1909
1910func (x decSliceHelper) ElemContainerState(index int) {
1911	cr := x.d.cr
1912	if cr == nil {
1913		return
1914	}
1915	if x.array {
1916		cr.sendContainerState(containerArrayElem)
1917	} else {
1918		if index%2 == 0 {
1919			cr.sendContainerState(containerMapKey)
1920		} else {
1921			cr.sendContainerState(containerMapValue)
1922		}
1923	}
1924}
1925
1926func decByteSlice(r decReader, clen int, bs []byte) (bsOut []byte) {
1927	if clen == 0 {
1928		return zeroByteSlice
1929	}
1930	if len(bs) == clen {
1931		bsOut = bs
1932	} else if cap(bs) >= clen {
1933		bsOut = bs[:clen]
1934	} else {
1935		bsOut = make([]byte, clen)
1936	}
1937	r.readb(bsOut)
1938	return
1939}
1940
1941func detachZeroCopyBytes(isBytesReader bool, dest []byte, in []byte) (out []byte) {
1942	if xlen := len(in); xlen > 0 {
1943		if isBytesReader || xlen <= scratchByteArrayLen {
1944			if cap(dest) >= xlen {
1945				out = dest[:xlen]
1946			} else {
1947				out = make([]byte, xlen)
1948			}
1949			copy(out, in)
1950			return
1951		}
1952	}
1953	return in
1954}
1955
1956// decInferLen will infer a sensible length, given the following:
1957//    - clen: length wanted.
1958//    - maxlen: max length to be returned.
1959//      if <= 0, it is unset, and we infer it based on the unit size
1960//    - unit: number of bytes for each element of the collection
1961func decInferLen(clen, maxlen, unit int) (rvlen int, truncated bool) {
1962	// handle when maxlen is not set i.e. <= 0
1963	if clen <= 0 {
1964		return
1965	}
1966	if maxlen <= 0 {
1967		// no maxlen defined. Use maximum of 256K memory, with a floor of 4K items.
1968		// maxlen = 256 * 1024 / unit
1969		// if maxlen < (4 * 1024) {
1970		// 	maxlen = 4 * 1024
1971		// }
1972		if unit < (256 / 4) {
1973			maxlen = 256 * 1024 / unit
1974		} else {
1975			maxlen = 4 * 1024
1976		}
1977	}
1978	if clen > maxlen {
1979		rvlen = maxlen
1980		truncated = true
1981	} else {
1982		rvlen = clen
1983	}
1984	return
1985	// if clen <= 0 {
1986	// 	rvlen = 0
1987	// } else if maxlen > 0 && clen > maxlen {
1988	// 	rvlen = maxlen
1989	// 	truncated = true
1990	// } else {
1991	// 	rvlen = clen
1992	// }
1993	// return
1994}
1995
1996// // implement overall decReader wrapping both, for possible use inline:
1997// type decReaderT struct {
1998// 	bytes bool
1999// 	rb    *bytesDecReader
2000// 	ri    *ioDecReader
2001// }
2002//
2003// // implement *Decoder as a decReader.
2004// // Using decReaderT (defined just above) caused performance degradation
2005// // possibly because of constant copying the value,
2006// // and some value->interface conversion causing allocation.
2007// func (d *Decoder) unreadn1() {
2008// 	if d.bytes {
2009// 		d.rb.unreadn1()
2010// 	} else {
2011// 		d.ri.unreadn1()
2012// 	}
2013// }
2014// ... for other methods of decReader.
2015// Testing showed that performance improvement was negligible.
2016