github.com/patricebensoussan/go/codec@v1.2.99/decode.go (about)

     1  // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
     2  // Use of this source code is governed by a MIT license found in the LICENSE file.
     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            // maximum depth
    20  	decDefChanCap          = 64              // should be large, as cap cannot be expanded
    21  	decScratchByteArrayLen = (8 + 2 + 2) * 8 // around cacheLineSize ie ~64, depending on Decoder size
    22  
    23  	// MARKER: massage decScratchByteArrayLen to ensure xxxDecDriver structs fit within cacheLine*N
    24  
    25  	// decFailNonEmptyIntf configures whether we error
    26  	// when decoding naked into a non-empty interface.
    27  	//
    28  	// Typically, we cannot decode non-nil stream value into
    29  	// nil interface with methods (e.g. io.Reader).
    30  	// However, in some scenarios, this should be allowed:
    31  	//   - MapType
    32  	//   - SliceType
    33  	//   - Extensions
    34  	//
    35  	// Consequently, we should relax this. Put it behind a const flag for now.
    36  	decFailNonEmptyIntf = false
    37  
    38  	// decUseTransient says that we should not use the transient optimization.
    39  	//
    40  	// There's potential for GC corruption or memory overwrites if transient isn't
    41  	// used carefully, so this flag helps turn it off quickly if needed.
    42  	//
    43  	// Use it everywhere needed so we can completely remove unused code blocks.
    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  // decByteState tracks where the []byte returned by the last call
    62  // to DecodeBytes or DecodeStringAsByte came from
    63  type decByteState uint8
    64  
    65  const (
    66  	decByteStateNone     decByteState = iota
    67  	decByteStateZerocopy              // view into []byte that we are decoding from
    68  	decByteStateReuseBuf              // view into transient buffer used internally by decDriver
    69  	// decByteStateNewAlloc
    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  	// this will check if the next token is a break.
    83  	CheckBreak() bool
    84  
    85  	// TryNil tries to decode as nil.
    86  	// If a nil is in the stream, it consumes it and returns true.
    87  	//
    88  	// Note: if TryNil returns true, that must be handled.
    89  	TryNil() bool
    90  
    91  	// ContainerType returns one of: Bytes, String, Nil, Slice or Map.
    92  	//
    93  	// Return unSet if not known.
    94  	//
    95  	// Note: Implementations MUST fully consume sentinel container types, specifically Nil.
    96  	ContainerType() (vt valueType)
    97  
    98  	// DecodeNaked will decode primitives (number, bool, string, []byte) and RawExt.
    99  	// For maps and arrays, it will not do the decoding in-band, but will signal
   100  	// the decoder, so that is done later, by setting the fauxUnion.valueType field.
   101  	//
   102  	// Note: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types).
   103  	// for extensions, DecodeNaked must read the tag and the []byte if it exists.
   104  	// if the []byte is not read, then kInterfaceNaked will treat it as a Handle
   105  	// that stores the subsequent value in-band, and complete reading the RawExt.
   106  	//
   107  	// extensions should also use readx to decode them, for efficiency.
   108  	// kInterface will extract the detached byte slice if it has to pass it outside its realm.
   109  	DecodeNaked()
   110  
   111  	DecodeInt64() (i int64)
   112  	DecodeUint64() (ui uint64)
   113  
   114  	DecodeFloat64() (f float64)
   115  	DecodeBool() (b bool)
   116  
   117  	// DecodeStringAsBytes returns the bytes representing a string.
   118  	// It will return a view into scratch buffer or input []byte (if applicable).
   119  	//
   120  	// Note: This can also decode symbols, if supported.
   121  	//
   122  	// Users should consume it right away and not store it for later use.
   123  	DecodeStringAsBytes() (v []byte)
   124  
   125  	// DecodeBytes returns the bytes representing a binary value.
   126  	// It will return a view into scratch buffer or input []byte (if applicable).
   127  	//
   128  	// All implementations must honor the contract below:
   129  	//    if ZeroCopy and applicable, return a view into input []byte we are decoding from
   130  	//    else if in == nil,          return a view into scratch buffer
   131  	//    else                        append decoded value to in[:0] and return that
   132  	//                                (this can be simulated by passing []byte{} as in parameter)
   133  	//
   134  	// Implementations must also update Decoder.decByteState on each call to
   135  	// DecodeBytes or DecodeStringAsBytes. Some callers may check that and work appropriately.
   136  	//
   137  	// Note: DecodeBytes may decode past the length of the passed byte slice, up to the cap.
   138  	// Consequently, it is ok to pass a zero-len slice to DecodeBytes, as the returned
   139  	// byte slice will have the appropriate length.
   140  	DecodeBytes(in []byte) (out []byte)
   141  	// DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte)
   142  
   143  	// DecodeExt will decode into a *RawExt or into an extension.
   144  	DecodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext)
   145  	// decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte)
   146  
   147  	DecodeTime() (t time.Time)
   148  
   149  	// ReadArrayStart will return the length of the array.
   150  	// If the format doesn't prefix the length, it returns containerLenUnknown.
   151  	// If the expected array was a nil in the stream, it returns containerLenNil.
   152  	ReadArrayStart() int
   153  	ReadArrayEnd()
   154  
   155  	// ReadMapStart will return the length of the array.
   156  	// If the format doesn't prefix the length, it returns containerLenUnknown.
   157  	// If the expected array was a nil in the stream, it returns containerLenNil.
   158  	ReadMapStart() int
   159  	ReadMapEnd()
   160  
   161  	reset()
   162  
   163  	// atEndOfDecode()
   164  
   165  	// nextValueBytes will return the bytes representing the next value in the stream.
   166  	//
   167  	// if start is nil, then treat it as a request to discard the next set of bytes,
   168  	// and the return response does not matter.
   169  	// Typically, this means that the returned []byte is nil/empty/undefined.
   170  	//
   171  	// Optimize for decoding from a []byte, where the nextValueBytes will just be a sub-slice
   172  	// of the input slice. Callers that need to use this to not be a view into the input bytes
   173  	// should handle it appropriately.
   174  	nextValueBytes(start []byte) []byte
   175  
   176  	// descBd will describe the token descriptor that signifies what type was decoded
   177  	descBd() string
   178  
   179  	decoder() *Decoder
   180  
   181  	driverStateManager
   182  	decNegintPosintFloatNumber
   183  }
   184  
   185  type decDriverContainerTracker interface {
   186  	ReadArrayElem()
   187  	ReadMapElemKey()
   188  	ReadMapElemValue()
   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  func (x decDriverNoopContainerReader) ReadArrayStart() (v int) { panic("ReadArrayStart unsupported") }
   206  func (x decDriverNoopContainerReader) ReadArrayEnd()           {}
   207  func (x decDriverNoopContainerReader) ReadMapStart() (v int)   { panic("ReadMapStart unsupported") }
   208  func (x decDriverNoopContainerReader) ReadMapEnd()             {}
   209  func (x decDriverNoopContainerReader) CheckBreak() (v bool)    { return }
   210  
   211  // DecodeOptions captures configuration options during decode.
   212  type DecodeOptions struct {
   213  	// MapType specifies type to use during schema-less decoding of a map in the stream.
   214  	// If nil (unset), we default to map[string]interface{} iff json handle and MapKeyAsString=true,
   215  	// else map[interface{}]interface{}.
   216  	MapType reflect.Type
   217  
   218  	// SliceType specifies type to use during schema-less decoding of an array in the stream.
   219  	// If nil (unset), we default to []interface{} for all formats.
   220  	SliceType reflect.Type
   221  
   222  	// MaxInitLen defines the maxinum initial length that we "make" a collection
   223  	// (string, slice, map, chan). If 0 or negative, we default to a sensible value
   224  	// based on the size of an element in the collection.
   225  	//
   226  	// For example, when decoding, a stream may say that it has 2^64 elements.
   227  	// We should not auto-matically provision a slice of that size, to prevent Out-Of-Memory crash.
   228  	// Instead, we provision up to MaxInitLen, fill that up, and start appending after that.
   229  	MaxInitLen int
   230  
   231  	// ReaderBufferSize is the size of the buffer used when reading.
   232  	//
   233  	// if > 0, we use a smart buffer internally for performance purposes.
   234  	ReaderBufferSize int
   235  
   236  	// MaxDepth defines the maximum depth when decoding nested
   237  	// maps and slices. If 0 or negative, we default to a suitably large number (currently 1024).
   238  	MaxDepth int16
   239  
   240  	// If ErrorIfNoField, return an error when decoding a map
   241  	// from a codec stream into a struct, and no matching struct field is found.
   242  	ErrorIfNoField bool
   243  
   244  	// If ErrorIfNoArrayExpand, return an error when decoding a slice/array that cannot be expanded.
   245  	// For example, the stream contains an array of 8 items, but you are decoding into a [4]T array,
   246  	// or you are decoding into a slice of length 4 which is non-addressable (and so cannot be set).
   247  	ErrorIfNoArrayExpand bool
   248  
   249  	// If SignedInteger, use the int64 during schema-less decoding of unsigned values (not uint64).
   250  	SignedInteger bool
   251  
   252  	// MapValueReset controls how we decode into a map value.
   253  	//
   254  	// By default, we MAY retrieve the mapping for a key, and then decode into that.
   255  	// However, especially with big maps, that retrieval may be expensive and unnecessary
   256  	// if the stream already contains all that is necessary to recreate the value.
   257  	//
   258  	// If true, we will never retrieve the previous mapping,
   259  	// but rather decode into a new value and set that in the map.
   260  	//
   261  	// If false, we will retrieve the previous mapping if necessary e.g.
   262  	// the previous mapping is a pointer, or is a struct or array with pre-set state,
   263  	// or is an interface.
   264  	MapValueReset bool
   265  
   266  	// SliceElementReset: on decoding a slice, reset the element to a zero value first.
   267  	//
   268  	// concern: if the slice already contained some garbage, we will decode into that garbage.
   269  	SliceElementReset bool
   270  
   271  	// InterfaceReset controls how we decode into an interface.
   272  	//
   273  	// By default, when we see a field that is an interface{...},
   274  	// or a map with interface{...} value, we will attempt decoding into the
   275  	// "contained" value.
   276  	//
   277  	// However, this prevents us from reading a string into an interface{}
   278  	// that formerly contained a number.
   279  	//
   280  	// If true, we will decode into a new "blank" value, and set that in the interface.
   281  	// If false, we will decode into whatever is contained in the interface.
   282  	InterfaceReset bool
   283  
   284  	// InternString controls interning of strings during decoding.
   285  	//
   286  	// Some handles, e.g. json, typically will read map keys as strings.
   287  	// If the set of keys are finite, it may help reduce allocation to
   288  	// look them up from a map (than to allocate them afresh).
   289  	//
   290  	// Note: Handles will be smart when using the intern functionality.
   291  	// Every string should not be interned.
   292  	// An excellent use-case for interning is struct field names,
   293  	// or map keys where key type is string.
   294  	InternString bool
   295  
   296  	// PreferArrayOverSlice controls whether to decode to an array or a slice.
   297  	//
   298  	// This only impacts decoding into a nil interface{}.
   299  	//
   300  	// Consequently, it has no effect on codecgen.
   301  	//
   302  	// *Note*: This only applies if using go1.5 and above,
   303  	// as it requires reflect.ArrayOf support which was absent before go1.5.
   304  	PreferArrayOverSlice bool
   305  
   306  	// DeleteOnNilMapValue controls how to decode a nil value in the stream.
   307  	//
   308  	// If true, we will delete the mapping of the key.
   309  	// Else, just set the mapping to the zero value of the type.
   310  	//
   311  	// Deprecated: This does NOTHING and is left behind for compiling compatibility.
   312  	// This change is necessitated because 'nil' in a stream now consistently
   313  	// means the zero value (ie reset the value to its zero state).
   314  	DeleteOnNilMapValue bool
   315  
   316  	// RawToString controls how raw bytes in a stream are decoded into a nil interface{}.
   317  	// By default, they are decoded as []byte, but can be decoded as string (if configured).
   318  	RawToString bool
   319  
   320  	// ZeroCopy controls whether decoded values of []byte or string type
   321  	// point into the input []byte parameter passed to a NewDecoderBytes/ResetBytes(...) call.
   322  	//
   323  	// To illustrate, if ZeroCopy and decoding from a []byte (not io.Writer),
   324  	// then a []byte or string in the output result may just be a slice of (point into)
   325  	// the input bytes.
   326  	//
   327  	// This optimization prevents unnecessary copying.
   328  	//
   329  	// However, it is made optional, as the caller MUST ensure that the input parameter []byte is
   330  	// not modified after the Decode() happens, as any changes are mirrored in the decoded result.
   331  	ZeroCopy bool
   332  
   333  	// PreferPointerForStructOrArray controls whether a struct or array
   334  	// is stored in a nil interface{}, or a pointer to it.
   335  	//
   336  	// This mostly impacts when we decode registered extensions.
   337  	PreferPointerForStructOrArray bool
   338  }
   339  
   340  // ----------------------------------------
   341  
   342  func (d *Decoder) rawExt(f *codecFnInfo, rv reflect.Value) {
   343  	d.d.DecodeExt(rv2i(rv), f.ti.rt, 0, nil)
   344  }
   345  
   346  func (d *Decoder) ext(f *codecFnInfo, rv reflect.Value) {
   347  	d.d.DecodeExt(rv2i(rv), f.ti.rt, f.xfTag, f.xfFn)
   348  }
   349  
   350  func (d *Decoder) selferUnmarshal(f *codecFnInfo, rv reflect.Value) {
   351  	rv2i(rv).(Selfer).CodecDecodeSelf(d)
   352  }
   353  
   354  func (d *Decoder) binaryUnmarshal(f *codecFnInfo, rv reflect.Value) {
   355  	bm := rv2i(rv).(encoding.BinaryUnmarshaler)
   356  	xbs := d.d.DecodeBytes(nil)
   357  	fnerr := bm.UnmarshalBinary(xbs)
   358  	d.onerror(fnerr)
   359  }
   360  
   361  func (d *Decoder) textUnmarshal(f *codecFnInfo, rv reflect.Value) {
   362  	tm := rv2i(rv).(encoding.TextUnmarshaler)
   363  	fnerr := tm.UnmarshalText(d.d.DecodeStringAsBytes())
   364  	d.onerror(fnerr)
   365  }
   366  
   367  func (d *Decoder) jsonUnmarshal(f *codecFnInfo, rv reflect.Value) {
   368  	d.jsonUnmarshalV(rv2i(rv).(jsonUnmarshaler))
   369  }
   370  
   371  func (d *Decoder) jsonUnmarshalV(tm jsonUnmarshaler) {
   372  	// grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself.
   373  	var bs0 = []byte{}
   374  	if !d.bytes {
   375  		bs0 = d.blist.get(256)
   376  	}
   377  	bs := d.d.nextValueBytes(bs0)
   378  	fnerr := tm.UnmarshalJSON(bs)
   379  	if !d.bytes {
   380  		d.blist.put(bs)
   381  		if !byteSliceSameData(bs0, bs) {
   382  			d.blist.put(bs0)
   383  		}
   384  	}
   385  	d.onerror(fnerr)
   386  }
   387  
   388  func (d *Decoder) kErr(f *codecFnInfo, rv reflect.Value) {
   389  	d.errorf("no decoding function defined for kind %v", rv.Kind())
   390  }
   391  
   392  func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
   393  	rvSetBytes(rv, d.rawBytes())
   394  }
   395  
   396  func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
   397  	rvSetString(rv, d.stringZC(d.d.DecodeStringAsBytes()))
   398  }
   399  
   400  func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
   401  	rvSetBool(rv, d.d.DecodeBool())
   402  }
   403  
   404  func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
   405  	rvSetTime(rv, d.d.DecodeTime())
   406  }
   407  
   408  func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
   409  	rvSetFloat32(rv, d.decodeFloat32())
   410  }
   411  
   412  func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
   413  	rvSetFloat64(rv, d.d.DecodeFloat64())
   414  }
   415  
   416  func (d *Decoder) kComplex64(f *codecFnInfo, rv reflect.Value) {
   417  	rvSetComplex64(rv, complex(d.decodeFloat32(), 0))
   418  }
   419  
   420  func (d *Decoder) kComplex128(f *codecFnInfo, rv reflect.Value) {
   421  	rvSetComplex128(rv, complex(d.d.DecodeFloat64(), 0))
   422  }
   423  
   424  func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
   425  	rvSetInt(rv, int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize)))
   426  }
   427  
   428  func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
   429  	rvSetInt8(rv, int8(chkOvf.IntV(d.d.DecodeInt64(), 8)))
   430  }
   431  
   432  func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
   433  	rvSetInt16(rv, int16(chkOvf.IntV(d.d.DecodeInt64(), 16)))
   434  }
   435  
   436  func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
   437  	rvSetInt32(rv, int32(chkOvf.IntV(d.d.DecodeInt64(), 32)))
   438  }
   439  
   440  func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
   441  	rvSetInt64(rv, d.d.DecodeInt64())
   442  }
   443  
   444  func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
   445  	rvSetUint(rv, uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
   446  }
   447  
   448  func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
   449  	rvSetUintptr(rv, uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
   450  }
   451  
   452  func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
   453  	rvSetUint8(rv, uint8(chkOvf.UintV(d.d.DecodeUint64(), 8)))
   454  }
   455  
   456  func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
   457  	rvSetUint16(rv, uint16(chkOvf.UintV(d.d.DecodeUint64(), 16)))
   458  }
   459  
   460  func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
   461  	rvSetUint32(rv, uint32(chkOvf.UintV(d.d.DecodeUint64(), 32)))
   462  }
   463  
   464  func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
   465  	rvSetUint64(rv, d.d.DecodeUint64())
   466  }
   467  
   468  func (d *Decoder) kInterfaceNaked(f *codecFnInfo) (rvn reflect.Value) {
   469  	// nil interface:
   470  	// use some hieristics to decode it appropriately
   471  	// based on the detected next value in the stream.
   472  	n := d.naked()
   473  	d.d.DecodeNaked()
   474  
   475  	// We cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader).
   476  	// Howver, it is possible that the user has ways to pass in a type for a given interface
   477  	//   - MapType
   478  	//   - SliceType
   479  	//   - Extensions
   480  	//
   481  	// Consequently, we should relax this. Put it behind a const flag for now.
   482  	if decFailNonEmptyIntf && f.ti.numMeth > 0 {
   483  		d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth)
   484  	}
   485  	switch n.v {
   486  	case valueTypeMap:
   487  		mtid := d.mtid
   488  		if mtid == 0 {
   489  			if d.jsms { // if json, default to a map type with string keys
   490  				mtid = mapStrIntfTypId // for json performance
   491  			} else {
   492  				mtid = mapIntfIntfTypId
   493  			}
   494  		}
   495  		if mtid == mapStrIntfTypId {
   496  			var v2 map[string]interface{}
   497  			d.decode(&v2)
   498  			rvn = rv4iptr(&v2).Elem()
   499  		} else if mtid == mapIntfIntfTypId {
   500  			var v2 map[interface{}]interface{}
   501  			d.decode(&v2)
   502  			rvn = rv4iptr(&v2).Elem()
   503  		} else if d.mtr {
   504  			rvn = reflect.New(d.h.MapType)
   505  			d.decode(rv2i(rvn))
   506  			rvn = rvn.Elem()
   507  		} else {
   508  			rvn = rvZeroAddrK(d.h.MapType, reflect.Map)
   509  			d.decodeValue(rvn, nil)
   510  		}
   511  	case valueTypeArray:
   512  		if d.stid == 0 || d.stid == intfSliceTypId {
   513  			var v2 []interface{}
   514  			d.decode(&v2)
   515  			rvn = rv4iptr(&v2).Elem()
   516  		} else if d.str {
   517  			rvn = reflect.New(d.h.SliceType)
   518  			d.decode(rv2i(rvn))
   519  			rvn = rvn.Elem()
   520  		} else {
   521  			rvn = rvZeroAddrK(d.h.SliceType, reflect.Slice)
   522  			d.decodeValue(rvn, nil)
   523  		}
   524  		if reflectArrayOfSupported && d.h.PreferArrayOverSlice {
   525  			rvn = rvGetArray4Slice(rvn)
   526  		}
   527  	case valueTypeExt:
   528  		tag, bytes := n.u, n.l // calling decode below might taint the values
   529  		bfn := d.h.getExtForTag(tag)
   530  		var re = RawExt{Tag: tag}
   531  		if bytes == nil {
   532  			// it is one of the InterfaceExt ones: json and cbor.
   533  			// most likely cbor, as json decoding never reveals valueTypeExt (no tagging support)
   534  			if bfn == nil {
   535  				d.decode(&re.Value)
   536  				rvn = rv4iptr(&re).Elem()
   537  			} else {
   538  				if bfn.ext == SelfExt {
   539  					rvn = rvZeroAddrK(bfn.rt, bfn.rt.Kind())
   540  					d.decodeValue(rvn, d.h.fnNoExt(bfn.rt))
   541  				} else {
   542  					rvn = reflect.New(bfn.rt)
   543  					d.interfaceExtConvertAndDecode(rv2i(rvn), bfn.ext)
   544  					rvn = rvn.Elem()
   545  				}
   546  			}
   547  		} else {
   548  			// one of the BytesExt ones: binc, msgpack, simple
   549  			if bfn == nil {
   550  				re.setData(bytes, false)
   551  				rvn = rv4iptr(&re).Elem()
   552  			} else {
   553  				rvn = reflect.New(bfn.rt)
   554  				if bfn.ext == SelfExt {
   555  					d.sideDecode(rv2i(rvn), bfn.rt, bytes)
   556  				} else {
   557  					bfn.ext.ReadExt(rv2i(rvn), bytes)
   558  				}
   559  				rvn = rvn.Elem()
   560  			}
   561  		}
   562  		// if struct/array, directly store pointer into the interface
   563  		if d.h.PreferPointerForStructOrArray && rvn.CanAddr() {
   564  			if rk := rvn.Kind(); rk == reflect.Array || rk == reflect.Struct {
   565  				rvn = rvn.Addr()
   566  			}
   567  		}
   568  	case valueTypeNil:
   569  		// rvn = reflect.Zero(f.ti.rt)
   570  		// no-op
   571  	case valueTypeInt:
   572  		rvn = n.ri()
   573  	case valueTypeUint:
   574  		rvn = n.ru()
   575  	case valueTypeFloat:
   576  		rvn = n.rf()
   577  	case valueTypeBool:
   578  		rvn = n.rb()
   579  	case valueTypeString, valueTypeSymbol:
   580  		rvn = n.rs()
   581  	case valueTypeBytes:
   582  		rvn = n.rl()
   583  	case valueTypeTime:
   584  		rvn = n.rt()
   585  	default:
   586  		halt.errorf("kInterfaceNaked: unexpected valueType: %d", n.v)
   587  	}
   588  	return
   589  }
   590  
   591  func (d *Decoder) kInterface(f *codecFnInfo, rv reflect.Value) {
   592  	// Note: A consequence of how kInterface works, is that
   593  	// if an interface already contains something, we try
   594  	// to decode into what was there before.
   595  	// We do not replace with a generic value (as got from decodeNaked).
   596  	//
   597  	// every interface passed here MUST be settable.
   598  	//
   599  	// ensure you call rvSetIntf(...) before returning.
   600  
   601  	isnilrv := rvIsNil(rv)
   602  
   603  	var rvn reflect.Value
   604  
   605  	if d.h.InterfaceReset {
   606  		// check if mapping to a type: if so, initialize it and move on
   607  		rvn = d.h.intf2impl(f.ti.rtid)
   608  		if !rvn.IsValid() {
   609  			rvn = d.kInterfaceNaked(f)
   610  			if rvn.IsValid() {
   611  				rvSetIntf(rv, rvn)
   612  			} else if !isnilrv {
   613  				decSetNonNilRV2Zero4Intf(rv)
   614  			}
   615  			return
   616  		}
   617  	} else if isnilrv {
   618  		// check if mapping to a type: if so, initialize it and move on
   619  		rvn = d.h.intf2impl(f.ti.rtid)
   620  		if !rvn.IsValid() {
   621  			rvn = d.kInterfaceNaked(f)
   622  			if rvn.IsValid() {
   623  				rvSetIntf(rv, rvn)
   624  			}
   625  			return
   626  		}
   627  	} else {
   628  		// now we have a non-nil interface value, meaning it contains a type
   629  		rvn = rv.Elem()
   630  	}
   631  
   632  	// rvn is now a non-interface type
   633  
   634  	canDecode, _ := isDecodeable(rvn)
   635  
   636  	// Note: interface{} is settable, but underlying type may not be.
   637  	// Consequently, we MAY have to allocate a value (containing the underlying value),
   638  	// decode into it, and reset the interface to that new value.
   639  
   640  	if !canDecode {
   641  		rvn2 := d.oneShotAddrRV(rvType(rvn), rvn.Kind())
   642  		rvSetDirect(rvn2, rvn)
   643  		rvn = rvn2
   644  	}
   645  
   646  	d.decodeValue(rvn, nil)
   647  	rvSetIntf(rv, rvn)
   648  }
   649  
   650  func decStructFieldKeyNotString(dd decDriver, keyType valueType, b *[decScratchByteArrayLen]byte) (rvkencname []byte) {
   651  	if keyType == valueTypeInt {
   652  		rvkencname = strconv.AppendInt(b[:0], dd.DecodeInt64(), 10)
   653  	} else if keyType == valueTypeUint {
   654  		rvkencname = strconv.AppendUint(b[:0], dd.DecodeUint64(), 10)
   655  	} else if keyType == valueTypeFloat {
   656  		rvkencname = strconv.AppendFloat(b[:0], dd.DecodeFloat64(), 'f', -1, 64)
   657  	} else {
   658  		halt.errorf("invalid struct key type: %v", keyType)
   659  	}
   660  	return
   661  }
   662  
   663  func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) {
   664  	ctyp := d.d.ContainerType()
   665  	ti := f.ti
   666  	var mf MissingFielder
   667  	if ti.flagMissingFielder {
   668  		mf = rv2i(rv).(MissingFielder)
   669  	} else if ti.flagMissingFielderPtr {
   670  		mf = rv2i(rvAddr(rv, ti.ptr)).(MissingFielder)
   671  	}
   672  	if ctyp == valueTypeMap {
   673  		containerLen := d.mapStart(d.d.ReadMapStart())
   674  		if containerLen == 0 {
   675  			d.mapEnd()
   676  			return
   677  		}
   678  		hasLen := containerLen >= 0
   679  		var name2 []byte
   680  		if mf != nil {
   681  			var namearr2 [16]byte
   682  			name2 = namearr2[:0]
   683  		}
   684  		var rvkencname []byte
   685  		for j := 0; d.containerNext(j, containerLen, hasLen); j++ {
   686  			d.mapElemKey()
   687  			if ti.keyType == valueTypeString {
   688  				rvkencname = d.d.DecodeStringAsBytes()
   689  			} else {
   690  				rvkencname = decStructFieldKeyNotString(d.d, ti.keyType, &d.b)
   691  			}
   692  			d.mapElemValue()
   693  			if si := ti.siForEncName(rvkencname); si != nil {
   694  				d.decodeValue(si.path.fieldAlloc(rv), nil)
   695  			} else if mf != nil {
   696  				// store rvkencname in new []byte, as it previously shares Decoder.b, which is used in decode
   697  				name2 = append(name2[:0], rvkencname...)
   698  				var f interface{}
   699  				d.decode(&f)
   700  				if !mf.CodecMissingField(name2, f) && d.h.ErrorIfNoField {
   701  					d.errorf("no matching struct field when decoding stream map with key: %s ", stringView(name2))
   702  				}
   703  			} else {
   704  				d.structFieldNotFound(-1, stringView(rvkencname))
   705  			}
   706  		}
   707  		d.mapEnd()
   708  	} else if ctyp == valueTypeArray {
   709  		containerLen := d.arrayStart(d.d.ReadArrayStart())
   710  		if containerLen == 0 {
   711  			d.arrayEnd()
   712  			return
   713  		}
   714  		// Not much gain from doing it two ways for array.
   715  		// Arrays are not used as much for structs.
   716  		hasLen := containerLen >= 0
   717  		var checkbreak bool
   718  		tisfi := ti.sfi.source()
   719  		for j, si := range tisfi {
   720  			if hasLen {
   721  				if j == containerLen {
   722  					break
   723  				}
   724  			} else if d.checkBreak() {
   725  				checkbreak = true
   726  				break
   727  			}
   728  			d.arrayElem()
   729  			d.decodeValue(si.path.fieldAlloc(rv), nil)
   730  		}
   731  		var proceed bool
   732  		if hasLen {
   733  			proceed = containerLen > len(tisfi)
   734  		} else {
   735  			proceed = !checkbreak
   736  		}
   737  		// if (hasLen && containerLen > len(tisfi)) || (!hasLen && !checkbreak) {
   738  		if proceed {
   739  			// read remaining values and throw away
   740  			for j := len(tisfi); ; j++ {
   741  				if !d.containerNext(j, containerLen, hasLen) {
   742  					break
   743  				}
   744  				d.arrayElem()
   745  				d.structFieldNotFound(j, "")
   746  			}
   747  		}
   748  		d.arrayEnd()
   749  	} else {
   750  		d.onerror(errNeedMapOrArrayDecodeToStruct)
   751  	}
   752  }
   753  
   754  func (d *Decoder) kSlice(f *codecFnInfo, rv reflect.Value) {
   755  	// A slice can be set from a map or array in stream.
   756  	// This way, the order can be kept (as order is lost with map).
   757  
   758  	// Note: rv is a slice type here - guaranteed
   759  
   760  	ti := f.ti
   761  	rvCanset := rv.CanSet()
   762  
   763  	ctyp := d.d.ContainerType()
   764  	if ctyp == valueTypeBytes || ctyp == valueTypeString {
   765  		// you can only decode bytes or string in the stream into a slice or array of bytes
   766  		if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) {
   767  			d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt)
   768  		}
   769  		rvbs := rvGetBytes(rv)
   770  		if !rvCanset {
   771  			// not addressable byte slice, so do not decode into it past the length
   772  			rvbs = rvbs[:len(rvbs):len(rvbs)]
   773  		}
   774  		bs2 := d.decodeBytesInto(rvbs)
   775  		// if !(len(bs2) == len(rvbs) && byteSliceSameData(rvbs, bs2)) {
   776  		if !(len(bs2) > 0 && len(bs2) == len(rvbs) && &bs2[0] == &rvbs[0]) {
   777  			if rvCanset {
   778  				rvSetBytes(rv, bs2)
   779  			} else if len(rvbs) > 0 && len(bs2) > 0 {
   780  				copy(rvbs, bs2)
   781  			}
   782  		}
   783  		return
   784  	}
   785  
   786  	slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map) - never Nil
   787  
   788  	// an array can never return a nil slice. so no need to check f.array here.
   789  	if containerLenS == 0 {
   790  		if rvCanset {
   791  			if rvIsNil(rv) {
   792  				rvSetDirect(rv, rvSliceZeroCap(ti.rt))
   793  			} else {
   794  				rvSetSliceLen(rv, 0)
   795  			}
   796  		}
   797  		slh.End()
   798  		return
   799  	}
   800  
   801  	rtelem0Mut := !scalarBitset.isset(ti.elemkind)
   802  	rtelem := ti.elem
   803  
   804  	for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
   805  		rtelem = rtelem.Elem()
   806  	}
   807  
   808  	var fn *codecFn
   809  
   810  	var rvChanged bool
   811  
   812  	var rv0 = rv
   813  	var rv9 reflect.Value
   814  
   815  	rvlen := rvLenSlice(rv)
   816  	rvcap := rvCapSlice(rv)
   817  	hasLen := containerLenS > 0
   818  	if hasLen {
   819  		if containerLenS > rvcap {
   820  			oldRvlenGtZero := rvlen > 0
   821  			rvlen1 := decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
   822  			if rvlen1 == rvlen {
   823  			} else if rvlen1 <= rvcap {
   824  				if rvCanset {
   825  					rvlen = rvlen1
   826  					rvSetSliceLen(rv, rvlen)
   827  				}
   828  			} else if rvCanset { // rvlen1 > rvcap
   829  				rvlen = rvlen1
   830  				rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen)
   831  				rvcap = rvlen
   832  				rvChanged = !rvCanset
   833  			} else { // rvlen1 > rvcap && !canSet
   834  				d.errorf("cannot decode into non-settable slice")
   835  			}
   836  			if rvChanged && oldRvlenGtZero && rtelem0Mut {
   837  				rvCopySlice(rv, rv0, rtelem) // only copy up to length NOT cap i.e. rv0.Slice(0, rvcap)
   838  			}
   839  		} else if containerLenS != rvlen {
   840  			if rvCanset {
   841  				rvlen = containerLenS
   842  				rvSetSliceLen(rv, rvlen)
   843  			}
   844  		}
   845  	}
   846  
   847  	// consider creating new element once, and just decoding into it.
   848  	var elemReset = d.h.SliceElementReset
   849  
   850  	var j int
   851  
   852  	for ; d.containerNext(j, containerLenS, hasLen); j++ {
   853  		if j == 0 {
   854  			if rvIsNil(rv) { // means hasLen = false
   855  				if rvCanset {
   856  					rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
   857  					rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen)
   858  					rvcap = rvlen
   859  					rvChanged = !rvCanset
   860  				} else {
   861  					d.errorf("cannot decode into non-settable slice")
   862  				}
   863  			}
   864  			if fn == nil {
   865  				fn = d.h.fn(rtelem)
   866  			}
   867  		}
   868  		// if indefinite, etc, then expand the slice if necessary
   869  		if j >= rvlen {
   870  			slh.ElemContainerState(j)
   871  
   872  			// expand the slice up to the cap.
   873  			// Note that we did, so we have to reset it later.
   874  
   875  			if rvlen < rvcap {
   876  				rvlen = rvcap
   877  				if rvCanset {
   878  					rvSetSliceLen(rv, rvlen)
   879  				} else if rvChanged {
   880  					rv = rvSlice(rv, rvlen)
   881  				} else {
   882  					d.onerror(errExpandSliceCannotChange)
   883  				}
   884  			} else {
   885  				if !(rvCanset || rvChanged) {
   886  					d.onerror(errExpandSliceCannotChange)
   887  				}
   888  				rv, rvcap, rvCanset = rvGrowSlice(rv, f.ti, rvcap, 1)
   889  				rvlen = rvcap
   890  				rvChanged = !rvCanset
   891  			}
   892  		} else {
   893  			slh.ElemContainerState(j)
   894  		}
   895  		rv9 = rvSliceIndex(rv, j, f.ti)
   896  		if elemReset {
   897  			rvSetZero(rv9)
   898  		}
   899  		d.decodeValue(rv9, fn)
   900  	}
   901  	if j < rvlen {
   902  		if rvCanset {
   903  			rvSetSliceLen(rv, j)
   904  		} else if rvChanged {
   905  			rv = rvSlice(rv, j)
   906  		}
   907  		// rvlen = j
   908  	} else if j == 0 && rvIsNil(rv) {
   909  		if rvCanset {
   910  			rv = rvSliceZeroCap(ti.rt)
   911  			rvCanset = false
   912  			rvChanged = true
   913  		}
   914  	}
   915  	slh.End()
   916  
   917  	if rvChanged { // infers rvCanset=true, so it can be reset
   918  		rvSetDirect(rv0, rv)
   919  	}
   920  }
   921  
   922  func (d *Decoder) kArray(f *codecFnInfo, rv reflect.Value) {
   923  	// An array can be set from a map or array in stream.
   924  
   925  	ctyp := d.d.ContainerType()
   926  	if handleBytesWithinKArray && (ctyp == valueTypeBytes || ctyp == valueTypeString) {
   927  		// you can only decode bytes or string in the stream into a slice or array of bytes
   928  		if f.ti.elemkind != uint8(reflect.Uint8) {
   929  			d.errorf("bytes/string in stream can decode into array of bytes, but not %v", f.ti.rt)
   930  		}
   931  		rvbs := rvGetArrayBytes(rv, nil)
   932  		bs2 := d.decodeBytesInto(rvbs)
   933  		if !byteSliceSameData(rvbs, bs2) && len(rvbs) > 0 && len(bs2) > 0 {
   934  			copy(rvbs, bs2)
   935  		}
   936  		return
   937  	}
   938  
   939  	slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map) - never Nil
   940  
   941  	// an array can never return a nil slice. so no need to check f.array here.
   942  	if containerLenS == 0 {
   943  		slh.End()
   944  		return
   945  	}
   946  
   947  	rtelem := f.ti.elem
   948  	for k := reflect.Kind(f.ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
   949  		rtelem = rtelem.Elem()
   950  	}
   951  
   952  	var fn *codecFn
   953  
   954  	var rv9 reflect.Value
   955  
   956  	rvlen := rv.Len() // same as cap
   957  	hasLen := containerLenS > 0
   958  	if hasLen && containerLenS > rvlen {
   959  		d.errorf("cannot decode into array with length: %v, less than container length: %v", rvlen, containerLenS)
   960  	}
   961  
   962  	// consider creating new element once, and just decoding into it.
   963  	var elemReset = d.h.SliceElementReset
   964  
   965  	for j := 0; d.containerNext(j, containerLenS, hasLen); j++ {
   966  		// note that you cannot expand the array if indefinite and we go past array length
   967  		if j >= rvlen {
   968  			slh.arrayCannotExpand(hasLen, rvlen, j, containerLenS)
   969  			return
   970  		}
   971  
   972  		slh.ElemContainerState(j)
   973  		rv9 = rvArrayIndex(rv, j, f.ti)
   974  		if elemReset {
   975  			rvSetZero(rv9)
   976  		}
   977  
   978  		if fn == nil {
   979  			fn = d.h.fn(rtelem)
   980  		}
   981  		d.decodeValue(rv9, fn)
   982  	}
   983  	slh.End()
   984  }
   985  
   986  func (d *Decoder) kChan(f *codecFnInfo, rv reflect.Value) {
   987  	// A slice can be set from a map or array in stream.
   988  	// This way, the order can be kept (as order is lost with map).
   989  
   990  	ti := f.ti
   991  	if ti.chandir&uint8(reflect.SendDir) == 0 {
   992  		d.errorf("receive-only channel cannot be decoded")
   993  	}
   994  	ctyp := d.d.ContainerType()
   995  	if ctyp == valueTypeBytes || ctyp == valueTypeString {
   996  		// you can only decode bytes or string in the stream into a slice or array of bytes
   997  		if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) {
   998  			d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt)
   999  		}
  1000  		bs2 := d.d.DecodeBytes(nil)
  1001  		irv := rv2i(rv)
  1002  		ch, ok := irv.(chan<- byte)
  1003  		if !ok {
  1004  			ch = irv.(chan byte)
  1005  		}
  1006  		for _, b := range bs2 {
  1007  			ch <- b
  1008  		}
  1009  		return
  1010  	}
  1011  
  1012  	var rvCanset = rv.CanSet()
  1013  
  1014  	// only expects valueType(Array|Map - nil handled above)
  1015  	slh, containerLenS := d.decSliceHelperStart()
  1016  
  1017  	// an array can never return a nil slice. so no need to check f.array here.
  1018  	if containerLenS == 0 {
  1019  		if rvCanset && rvIsNil(rv) {
  1020  			rvSetDirect(rv, reflect.MakeChan(ti.rt, 0))
  1021  		}
  1022  		slh.End()
  1023  		return
  1024  	}
  1025  
  1026  	rtelem := ti.elem
  1027  	useTransient := decUseTransient && ti.elemkind != byte(reflect.Ptr) && ti.tielem.flagCanTransient
  1028  
  1029  	for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
  1030  		rtelem = rtelem.Elem()
  1031  	}
  1032  
  1033  	var fn *codecFn
  1034  
  1035  	var rvChanged bool
  1036  	var rv0 = rv
  1037  	var rv9 reflect.Value
  1038  
  1039  	var rvlen int // = rv.Len()
  1040  	hasLen := containerLenS > 0
  1041  
  1042  	for j := 0; d.containerNext(j, containerLenS, hasLen); j++ {
  1043  		if j == 0 {
  1044  			if rvIsNil(rv) {
  1045  				if hasLen {
  1046  					rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
  1047  				} else {
  1048  					rvlen = decDefChanCap
  1049  				}
  1050  				if rvCanset {
  1051  					rv = reflect.MakeChan(ti.rt, rvlen)
  1052  					rvChanged = true
  1053  				} else {
  1054  					d.errorf("cannot decode into non-settable chan")
  1055  				}
  1056  			}
  1057  			if fn == nil {
  1058  				fn = d.h.fn(rtelem)
  1059  			}
  1060  		}
  1061  		slh.ElemContainerState(j)
  1062  		if rv9.IsValid() {
  1063  			rvSetZero(rv9)
  1064  		} else if decUseTransient && useTransient {
  1065  			rv9 = d.perType.TransientAddrK(ti.elem, reflect.Kind(ti.elemkind))
  1066  		} else {
  1067  			rv9 = rvZeroAddrK(ti.elem, reflect.Kind(ti.elemkind))
  1068  		}
  1069  		if !d.d.TryNil() {
  1070  			d.decodeValueNoCheckNil(rv9, fn)
  1071  		}
  1072  		rv.Send(rv9)
  1073  	}
  1074  	slh.End()
  1075  
  1076  	if rvChanged { // infers rvCanset=true, so it can be reset
  1077  		rvSetDirect(rv0, rv)
  1078  	}
  1079  
  1080  }
  1081  
  1082  func (d *Decoder) kMap(f *codecFnInfo, rv reflect.Value) {
  1083  	containerLen := d.mapStart(d.d.ReadMapStart())
  1084  	ti := f.ti
  1085  	if rvIsNil(rv) {
  1086  		rvlen := decInferLen(containerLen, d.h.MaxInitLen, int(ti.keysize+ti.elemsize))
  1087  		rvSetDirect(rv, makeMapReflect(ti.rt, rvlen))
  1088  	}
  1089  
  1090  	if containerLen == 0 {
  1091  		d.mapEnd()
  1092  		return
  1093  	}
  1094  
  1095  	ktype, vtype := ti.key, ti.elem
  1096  	ktypeId := rt2id(ktype)
  1097  	vtypeKind := reflect.Kind(ti.elemkind)
  1098  	ktypeKind := reflect.Kind(ti.keykind)
  1099  	kfast := mapKeyFastKindFor(ktypeKind)
  1100  	visindirect := mapStoresElemIndirect(uintptr(ti.elemsize))
  1101  	visref := refBitset.isset(ti.elemkind)
  1102  
  1103  	vtypePtr := vtypeKind == reflect.Ptr
  1104  	ktypePtr := ktypeKind == reflect.Ptr
  1105  
  1106  	vTransient := decUseTransient && !vtypePtr && ti.tielem.flagCanTransient
  1107  	kTransient := decUseTransient && !ktypePtr && ti.tikey.flagCanTransient
  1108  
  1109  	var vtypeElem reflect.Type
  1110  
  1111  	var keyFn, valFn *codecFn
  1112  	var ktypeLo, vtypeLo = ktype, vtype
  1113  
  1114  	if ktypeKind == reflect.Ptr {
  1115  		for ktypeLo = ktype.Elem(); ktypeLo.Kind() == reflect.Ptr; ktypeLo = ktypeLo.Elem() {
  1116  		}
  1117  	}
  1118  
  1119  	if vtypePtr {
  1120  		vtypeElem = vtype.Elem()
  1121  		for vtypeLo = vtypeElem; vtypeLo.Kind() == reflect.Ptr; vtypeLo = vtypeLo.Elem() {
  1122  		}
  1123  	}
  1124  
  1125  	rvkMut := !scalarBitset.isset(ti.keykind) // if ktype is immutable, then re-use the same rvk.
  1126  	rvvMut := !scalarBitset.isset(ti.elemkind)
  1127  	rvvCanNil := isnilBitset.isset(ti.elemkind)
  1128  
  1129  	// rvk: key
  1130  	// rvkn: if non-mutable, on each iteration of loop, set rvk to this
  1131  	// rvv: value
  1132  	// rvvn: if non-mutable, on each iteration of loop, set rvv to this
  1133  	//       if mutable, may be used as a temporary value for local-scoped operations
  1134  	// rvva: if mutable, used as transient value for use for key lookup
  1135  	// rvvz: zero value of map value type, used to do a map set when nil is found in stream
  1136  	var rvk, rvkn, rvv, rvvn, rvva, rvvz reflect.Value
  1137  
  1138  	// we do a doMapGet if kind is mutable, and InterfaceReset=true if interface
  1139  	var doMapGet, doMapSet bool
  1140  
  1141  	if !d.h.MapValueReset {
  1142  		if rvvMut && (vtypeKind != reflect.Interface || !d.h.InterfaceReset) {
  1143  			doMapGet = true
  1144  			rvva = mapAddrLoopvarRV(vtype, vtypeKind)
  1145  		}
  1146  	}
  1147  
  1148  	ktypeIsString := ktypeId == stringTypId
  1149  	ktypeIsIntf := ktypeId == intfTypId
  1150  
  1151  	hasLen := containerLen > 0
  1152  
  1153  	// kstrbs is used locally for the key bytes, so we can reduce allocation.
  1154  	// When we read keys, we copy to this local bytes array, and use a stringView for lookup.
  1155  	// We only convert it into a true string if we have to do a set on the map.
  1156  
  1157  	// Since kstr2bs will usually escape to the heap, declaring a [64]byte array may be wasteful.
  1158  	// It is only valuable if we are sure that it is declared on the stack.
  1159  	// var kstrarr [64]byte // most keys are less than 32 bytes, and even more less than 64
  1160  	// var kstrbs = kstrarr[:0]
  1161  	var kstrbs []byte
  1162  	var kstr2bs []byte
  1163  	var s string
  1164  
  1165  	var callFnRvk bool
  1166  
  1167  	fnRvk2 := func() (s string) {
  1168  		callFnRvk = false
  1169  		if len(kstr2bs) < 2 {
  1170  			return string(kstr2bs)
  1171  		}
  1172  		return d.mapKeyString(&callFnRvk, &kstrbs, &kstr2bs)
  1173  	}
  1174  
  1175  	// Use a possibly transient (map) value (and key), to reduce allocation
  1176  
  1177  	for j := 0; d.containerNext(j, containerLen, hasLen); j++ {
  1178  		callFnRvk = false
  1179  		if j == 0 {
  1180  			// if vtypekind is a scalar and thus value will be decoded using TransientAddrK,
  1181  			// then it is ok to use TransientAddr2K for the map key.
  1182  			if decUseTransient && vTransient && kTransient {
  1183  				rvk = d.perType.TransientAddr2K(ktype, ktypeKind)
  1184  			} else {
  1185  				rvk = rvZeroAddrK(ktype, ktypeKind)
  1186  			}
  1187  			if !rvkMut {
  1188  				rvkn = rvk
  1189  			}
  1190  			if !rvvMut {
  1191  				if decUseTransient && vTransient {
  1192  					rvvn = d.perType.TransientAddrK(vtype, vtypeKind)
  1193  				} else {
  1194  					rvvn = rvZeroAddrK(vtype, vtypeKind)
  1195  				}
  1196  			}
  1197  			if !ktypeIsString && keyFn == nil {
  1198  				keyFn = d.h.fn(ktypeLo)
  1199  			}
  1200  			if valFn == nil {
  1201  				valFn = d.h.fn(vtypeLo)
  1202  			}
  1203  		} else if rvkMut {
  1204  			rvSetZero(rvk)
  1205  		} else {
  1206  			rvk = rvkn
  1207  		}
  1208  
  1209  		d.mapElemKey()
  1210  		if ktypeIsString {
  1211  			kstr2bs = d.d.DecodeStringAsBytes()
  1212  			rvSetString(rvk, fnRvk2())
  1213  		} else {
  1214  			d.decByteState = decByteStateNone
  1215  			d.decodeValue(rvk, keyFn)
  1216  			// special case if interface wrapping a byte slice
  1217  			if ktypeIsIntf {
  1218  				if rvk2 := rvk.Elem(); rvk2.IsValid() && rvType(rvk2) == uint8SliceTyp {
  1219  					kstr2bs = rvGetBytes(rvk2)
  1220  					rvSetIntf(rvk, rv4istr(fnRvk2()))
  1221  				}
  1222  				// NOTE: consider failing early if map/slice/func
  1223  			}
  1224  		}
  1225  
  1226  		d.mapElemValue()
  1227  
  1228  		if d.d.TryNil() {
  1229  			// since a map, we have to set zero value if needed
  1230  			if !rvvz.IsValid() {
  1231  				rvvz = rvZeroK(vtype, vtypeKind)
  1232  			}
  1233  			if callFnRvk {
  1234  				s = d.string(kstr2bs)
  1235  				if ktypeIsString {
  1236  					rvSetString(rvk, s)
  1237  				} else { // ktypeIsIntf
  1238  					rvSetIntf(rvk, rv4istr(s))
  1239  				}
  1240  			}
  1241  			mapSet(rv, rvk, rvvz, kfast, visindirect, visref)
  1242  			continue
  1243  		}
  1244  
  1245  		// there is non-nil content in the stream to decode ...
  1246  		// consequently, it's ok to just directly create new value to the pointer (if vtypePtr)
  1247  
  1248  		// set doMapSet to false iff u do a get, and the return value is a non-nil pointer
  1249  		doMapSet = true
  1250  
  1251  		if !rvvMut {
  1252  			rvv = rvvn
  1253  		} else if !doMapGet {
  1254  			goto NEW_RVV
  1255  		} else {
  1256  			rvv = mapGet(rv, rvk, rvva, kfast, visindirect, visref)
  1257  			if !rvv.IsValid() || (rvvCanNil && rvIsNil(rvv)) {
  1258  				goto NEW_RVV
  1259  			}
  1260  			switch vtypeKind {
  1261  			case reflect.Ptr, reflect.Map: // ok to decode directly into map
  1262  				doMapSet = false
  1263  			case reflect.Interface:
  1264  				// if an interface{}, just decode into it iff a non-nil ptr/map, else allocate afresh
  1265  				rvvn = rvv.Elem()
  1266  				if k := rvvn.Kind(); (k == reflect.Ptr || k == reflect.Map) && !rvIsNil(rvvn) {
  1267  					d.decodeValueNoCheckNil(rvvn, nil) // valFn is incorrect here
  1268  					continue
  1269  				}
  1270  				// make addressable (so we can set the interface)
  1271  				rvvn = rvZeroAddrK(vtype, vtypeKind)
  1272  				rvSetIntf(rvvn, rvv)
  1273  				rvv = rvvn
  1274  			default:
  1275  				// make addressable (so you can set the slice/array elements, etc)
  1276  				if decUseTransient && vTransient {
  1277  					rvvn = d.perType.TransientAddrK(vtype, vtypeKind)
  1278  				} else {
  1279  					rvvn = rvZeroAddrK(vtype, vtypeKind)
  1280  				}
  1281  				rvSetDirect(rvvn, rvv)
  1282  				rvv = rvvn
  1283  			}
  1284  		}
  1285  		goto DECODE_VALUE_NO_CHECK_NIL
  1286  
  1287  	NEW_RVV:
  1288  		if vtypePtr {
  1289  			rvv = reflect.New(vtypeElem) // non-nil in stream, so allocate value
  1290  		} else if decUseTransient && vTransient {
  1291  			rvv = d.perType.TransientAddrK(vtype, vtypeKind)
  1292  		} else {
  1293  			rvv = rvZeroAddrK(vtype, vtypeKind)
  1294  		}
  1295  
  1296  	DECODE_VALUE_NO_CHECK_NIL:
  1297  		d.decodeValueNoCheckNil(rvv, valFn)
  1298  
  1299  		if doMapSet {
  1300  			if callFnRvk {
  1301  				s = d.string(kstr2bs)
  1302  				if ktypeIsString {
  1303  					rvSetString(rvk, s)
  1304  				} else { // ktypeIsIntf
  1305  					rvSetIntf(rvk, rv4istr(s))
  1306  				}
  1307  			}
  1308  			mapSet(rv, rvk, rvv, kfast, visindirect, visref)
  1309  		}
  1310  	}
  1311  
  1312  	d.mapEnd()
  1313  }
  1314  
  1315  // Decoder reads and decodes an object from an input stream in a supported format.
  1316  //
  1317  // Decoder is NOT safe for concurrent use i.e. a Decoder cannot be used
  1318  // concurrently in multiple goroutines.
  1319  //
  1320  // However, as Decoder could be allocation heavy to initialize, a Reset method is provided
  1321  // so its state can be reused to decode new input streams repeatedly.
  1322  // This is the idiomatic way to use.
  1323  type Decoder struct {
  1324  	panicHdl
  1325  
  1326  	d decDriver
  1327  
  1328  	// cache the mapTypeId and sliceTypeId for faster comparisons
  1329  	mtid uintptr
  1330  	stid uintptr
  1331  
  1332  	h *BasicHandle
  1333  
  1334  	blist bytesFreelist
  1335  
  1336  	// ---- cpu cache line boundary?
  1337  	decRd
  1338  
  1339  	// ---- cpu cache line boundary?
  1340  	n fauxUnion
  1341  
  1342  	hh  Handle
  1343  	err error
  1344  
  1345  	perType decPerType
  1346  
  1347  	// used for interning strings
  1348  	is internerMap
  1349  
  1350  	// ---- cpu cache line boundary?
  1351  	// ---- writable fields during execution --- *try* to keep in sep cache line
  1352  	maxdepth int16
  1353  	depth    int16
  1354  
  1355  	// Extensions can call Decode() within a current Decode() call.
  1356  	// We need to know when the top level Decode() call returns,
  1357  	// so we can decide whether to Release() or not.
  1358  	calls uint16 // what depth in mustDecode are we in now.
  1359  
  1360  	c containerState
  1361  
  1362  	decByteState
  1363  
  1364  	// b is an always-available scratch buffer used by Decoder and decDrivers.
  1365  	// By being always-available, it can be used for one-off things without
  1366  	// having to get from freelist, use, and return back to freelist.
  1367  	b [decScratchByteArrayLen]byte
  1368  }
  1369  
  1370  // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader.
  1371  //
  1372  // For efficiency, Users are encouraged to configure ReaderBufferSize on the handle
  1373  // OR pass in a memory buffered reader (eg bufio.Reader, bytes.Buffer).
  1374  func NewDecoder(r io.Reader, h Handle) *Decoder {
  1375  	d := h.newDecDriver().decoder()
  1376  	if r != nil {
  1377  		d.Reset(r)
  1378  	}
  1379  	return d
  1380  }
  1381  
  1382  // NewDecoderBytes returns a Decoder which efficiently decodes directly
  1383  // from a byte slice with zero copying.
  1384  func NewDecoderBytes(in []byte, h Handle) *Decoder {
  1385  	d := h.newDecDriver().decoder()
  1386  	if in != nil {
  1387  		d.ResetBytes(in)
  1388  	}
  1389  	return d
  1390  }
  1391  
  1392  // NewDecoderString returns a Decoder which efficiently decodes directly
  1393  // from a string with zero copying.
  1394  //
  1395  // It is a convenience function that calls NewDecoderBytes with a
  1396  // []byte view into the string.
  1397  //
  1398  // This can be an efficient zero-copy if using default mode i.e. without codec.safe tag.
  1399  func NewDecoderString(s string, h Handle) *Decoder {
  1400  	return NewDecoderBytes(bytesView(s), h)
  1401  }
  1402  
  1403  func (d *Decoder) r() *decRd {
  1404  	return &d.decRd
  1405  }
  1406  
  1407  func (d *Decoder) init(h Handle) {
  1408  	initHandle(h)
  1409  	d.bytes = true
  1410  	d.err = errDecoderNotInitialized
  1411  	d.h = h.getBasicHandle()
  1412  	d.hh = h
  1413  	d.be = h.isBinary()
  1414  	if d.h.InternString && d.is == nil {
  1415  		d.is.init()
  1416  	}
  1417  	// NOTE: do not initialize d.n here. It is lazily initialized in d.naked()
  1418  }
  1419  
  1420  func (d *Decoder) resetCommon() {
  1421  	d.d.reset()
  1422  	d.err = nil
  1423  	d.c = 0
  1424  	d.decByteState = decByteStateNone
  1425  	d.depth = 0
  1426  	d.calls = 0
  1427  	// reset all things which were cached from the Handle, but could change
  1428  	d.maxdepth = decDefMaxDepth
  1429  	if d.h.MaxDepth > 0 {
  1430  		d.maxdepth = d.h.MaxDepth
  1431  	}
  1432  	d.mtid = 0
  1433  	d.stid = 0
  1434  	d.mtr = false
  1435  	d.str = false
  1436  	if d.h.MapType != nil {
  1437  		d.mtid = rt2id(d.h.MapType)
  1438  		d.mtr = fastpathAvIndex(d.mtid) != -1
  1439  	}
  1440  	if d.h.SliceType != nil {
  1441  		d.stid = rt2id(d.h.SliceType)
  1442  		d.str = fastpathAvIndex(d.stid) != -1
  1443  	}
  1444  }
  1445  
  1446  // Reset the Decoder with a new Reader to decode from,
  1447  // clearing all state from last run(s).
  1448  func (d *Decoder) Reset(r io.Reader) {
  1449  	if r == nil {
  1450  		r = &eofReader
  1451  	}
  1452  	d.bytes = false
  1453  	if d.h.ReaderBufferSize > 0 {
  1454  		if d.bi == nil {
  1455  			d.bi = new(bufioDecReader)
  1456  		}
  1457  		d.bi.reset(r, d.h.ReaderBufferSize, &d.blist)
  1458  		d.bufio = true
  1459  		d.decReader = d.bi
  1460  	} else {
  1461  		if d.ri == nil {
  1462  			d.ri = new(ioDecReader)
  1463  		}
  1464  		d.ri.reset(r, &d.blist)
  1465  		d.bufio = false
  1466  		d.decReader = d.ri
  1467  	}
  1468  	d.resetCommon()
  1469  }
  1470  
  1471  // ResetBytes resets the Decoder with a new []byte to decode from,
  1472  // clearing all state from last run(s).
  1473  func (d *Decoder) ResetBytes(in []byte) {
  1474  	if in == nil {
  1475  		in = []byte{}
  1476  	}
  1477  	d.bufio = false
  1478  	d.bytes = true
  1479  	d.decReader = &d.rb
  1480  	d.rb.reset(in)
  1481  	d.resetCommon()
  1482  }
  1483  
  1484  // ResetString resets the Decoder with a new string to decode from,
  1485  // clearing all state from last run(s).
  1486  //
  1487  // It is a convenience function that calls ResetBytes with a
  1488  // []byte view into the string.
  1489  //
  1490  // This can be an efficient zero-copy if using default mode i.e. without codec.safe tag.
  1491  func (d *Decoder) ResetString(s string) {
  1492  	d.ResetBytes(bytesView(s))
  1493  }
  1494  
  1495  func (d *Decoder) naked() *fauxUnion {
  1496  	return &d.n
  1497  }
  1498  
  1499  // Decode decodes the stream from reader and stores the result in the
  1500  // value pointed to by v. v cannot be a nil pointer. v can also be
  1501  // a reflect.Value of a pointer.
  1502  //
  1503  // Note that a pointer to a nil interface is not a nil pointer.
  1504  // If you do not know what type of stream it is, pass in a pointer to a nil interface.
  1505  // We will decode and store a value in that nil interface.
  1506  //
  1507  // Sample usages:
  1508  //   // Decoding into a non-nil typed value
  1509  //   var f float32
  1510  //   err = codec.NewDecoder(r, handle).Decode(&f)
  1511  //
  1512  //   // Decoding into nil interface
  1513  //   var v interface{}
  1514  //   dec := codec.NewDecoder(r, handle)
  1515  //   err = dec.Decode(&v)
  1516  //
  1517  // When decoding into a nil interface{}, we will decode into an appropriate value based
  1518  // on the contents of the stream:
  1519  //   - Numbers are decoded as float64, int64 or uint64.
  1520  //   - Other values are decoded appropriately depending on the type:
  1521  //     bool, string, []byte, time.Time, etc
  1522  //   - Extensions are decoded as RawExt (if no ext function registered for the tag)
  1523  // Configurations exist on the Handle to override defaults
  1524  // (e.g. for MapType, SliceType and how to decode raw bytes).
  1525  //
  1526  // When decoding into a non-nil interface{} value, the mode of encoding is based on the
  1527  // type of the value. When a value is seen:
  1528  //   - If an extension is registered for it, call that extension function
  1529  //   - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error
  1530  //   - Else decode it based on its reflect.Kind
  1531  //
  1532  // There are some special rules when decoding into containers (slice/array/map/struct).
  1533  // Decode will typically use the stream contents to UPDATE the container i.e. the values
  1534  // in these containers will not be zero'ed before decoding.
  1535  //   - A map can be decoded from a stream map, by updating matching keys.
  1536  //   - A slice can be decoded from a stream array,
  1537  //     by updating the first n elements, where n is length of the stream.
  1538  //   - A slice can be decoded from a stream map, by decoding as if
  1539  //     it contains a sequence of key-value pairs.
  1540  //   - A struct can be decoded from a stream map, by updating matching fields.
  1541  //   - A struct can be decoded from a stream array,
  1542  //     by updating fields as they occur in the struct (by index).
  1543  //
  1544  // This in-place update maintains consistency in the decoding philosophy (i.e. we ALWAYS update
  1545  // in place by default). However, the consequence of this is that values in slices or maps
  1546  // which are not zero'ed before hand, will have part of the prior values in place after decode
  1547  // if the stream doesn't contain an update for those parts.
  1548  //
  1549  // This in-place update can be disabled by configuring the MapValueReset and SliceElementReset
  1550  // decode options available on every handle.
  1551  //
  1552  // Furthermore, when decoding a stream map or array with length of 0 into a nil map or slice,
  1553  // we reset the destination map or slice to a zero-length value.
  1554  //
  1555  // However, when decoding a stream nil, we reset the destination container
  1556  // to its "zero" value (e.g. nil for slice/map, etc).
  1557  //
  1558  // Note: we allow nil values in the stream anywhere except for map keys.
  1559  // A nil value in the encoded stream where a map key is expected is treated as an error.
  1560  func (d *Decoder) Decode(v interface{}) (err error) {
  1561  	// tried to use closure, as runtime optimizes defer with no params.
  1562  	// This seemed to be causing weird issues (like circular reference found, unexpected panic, etc).
  1563  	// Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139
  1564  	if !debugging {
  1565  		defer func() {
  1566  			if x := recover(); x != nil {
  1567  				panicValToErr(d, x, &d.err)
  1568  				err = d.err
  1569  			}
  1570  		}()
  1571  	}
  1572  
  1573  	d.MustDecode(v)
  1574  	return
  1575  }
  1576  
  1577  // MustDecode is like Decode, but panics if unable to Decode.
  1578  //
  1579  // Note: This provides insight to the code location that triggered the error.
  1580  func (d *Decoder) MustDecode(v interface{}) {
  1581  	halt.onerror(d.err)
  1582  	if d.hh == nil {
  1583  		halt.onerror(errNoFormatHandle)
  1584  	}
  1585  
  1586  	// Top-level: v is a pointer and not nil.
  1587  	d.calls++
  1588  	d.decode(v)
  1589  	d.calls--
  1590  }
  1591  
  1592  // Release releases shared (pooled) resources.
  1593  //
  1594  // It is important to call Release() when done with a Decoder, so those resources
  1595  // are released instantly for use by subsequently created Decoders.
  1596  //
  1597  // By default, Release() is automatically called unless the option ExplicitRelease is set.
  1598  //
  1599  // Deprecated: Release is a no-op as pooled resources are not used with an Decoder.
  1600  // This method is kept for compatibility reasons only.
  1601  func (d *Decoder) Release() {
  1602  }
  1603  
  1604  func (d *Decoder) swallow() {
  1605  	d.d.nextValueBytes(nil)
  1606  }
  1607  
  1608  func (d *Decoder) swallowErr() (err error) {
  1609  	if !debugging {
  1610  		defer func() {
  1611  			if x := recover(); x != nil {
  1612  				panicValToErr(d, x, &err)
  1613  			}
  1614  		}()
  1615  	}
  1616  	d.swallow()
  1617  	return
  1618  }
  1619  
  1620  func setZero(iv interface{}) {
  1621  	if iv == nil {
  1622  		return
  1623  	}
  1624  	rv, ok := isNil(iv)
  1625  	if ok {
  1626  		return
  1627  	}
  1628  	// var canDecode bool
  1629  	switch v := iv.(type) {
  1630  	case *string:
  1631  		*v = ""
  1632  	case *bool:
  1633  		*v = false
  1634  	case *int:
  1635  		*v = 0
  1636  	case *int8:
  1637  		*v = 0
  1638  	case *int16:
  1639  		*v = 0
  1640  	case *int32:
  1641  		*v = 0
  1642  	case *int64:
  1643  		*v = 0
  1644  	case *uint:
  1645  		*v = 0
  1646  	case *uint8:
  1647  		*v = 0
  1648  	case *uint16:
  1649  		*v = 0
  1650  	case *uint32:
  1651  		*v = 0
  1652  	case *uint64:
  1653  		*v = 0
  1654  	case *float32:
  1655  		*v = 0
  1656  	case *float64:
  1657  		*v = 0
  1658  	case *complex64:
  1659  		*v = 0
  1660  	case *complex128:
  1661  		*v = 0
  1662  	case *[]byte:
  1663  		*v = nil
  1664  	case *Raw:
  1665  		*v = nil
  1666  	case *time.Time:
  1667  		*v = time.Time{}
  1668  	case reflect.Value:
  1669  		decSetNonNilRV2Zero(v)
  1670  	default:
  1671  		if !fastpathDecodeSetZeroTypeSwitch(iv) {
  1672  			decSetNonNilRV2Zero(rv)
  1673  		}
  1674  	}
  1675  }
  1676  
  1677  // decSetNonNilRV2Zero will set the non-nil value to its zero value.
  1678  func decSetNonNilRV2Zero(v reflect.Value) {
  1679  	// If not decodeable (settable), we do not touch it.
  1680  	// We considered empty'ing it if not decodeable e.g.
  1681  	//    - if chan, drain it
  1682  	//    - if map, clear it
  1683  	//    - if slice or array, zero all elements up to len
  1684  	//
  1685  	// However, we decided instead that we either will set the
  1686  	// whole value to the zero value, or leave AS IS.
  1687  
  1688  	k := v.Kind()
  1689  	if k == reflect.Interface {
  1690  		decSetNonNilRV2Zero4Intf(v)
  1691  	} else if k == reflect.Ptr {
  1692  		decSetNonNilRV2Zero4Ptr(v)
  1693  	} else if v.CanSet() {
  1694  		rvSetDirectZero(v)
  1695  	}
  1696  }
  1697  
  1698  func decSetNonNilRV2Zero4Ptr(v reflect.Value) {
  1699  	ve := v.Elem()
  1700  	if ve.CanSet() {
  1701  		rvSetZero(ve) // we can have a pointer to an interface
  1702  	} else if v.CanSet() {
  1703  		rvSetZero(v)
  1704  	}
  1705  }
  1706  
  1707  func decSetNonNilRV2Zero4Intf(v reflect.Value) {
  1708  	ve := v.Elem()
  1709  	if ve.CanSet() {
  1710  		rvSetDirectZero(ve) // interfaces always have element as a non-interface
  1711  	} else if v.CanSet() {
  1712  		rvSetZero(v)
  1713  	}
  1714  }
  1715  
  1716  func (d *Decoder) decode(iv interface{}) {
  1717  	// a switch with only concrete types can be optimized.
  1718  	// consequently, we deal with nil and interfaces outside the switch.
  1719  
  1720  	if iv == nil {
  1721  		d.onerror(errCannotDecodeIntoNil)
  1722  	}
  1723  
  1724  	switch v := iv.(type) {
  1725  	// case nil:
  1726  	// case Selfer:
  1727  	case reflect.Value:
  1728  		if x, _ := isDecodeable(v); !x {
  1729  			d.haltAsNotDecodeable(v)
  1730  		}
  1731  		d.decodeValue(v, nil)
  1732  	case *string:
  1733  		*v = d.stringZC(d.d.DecodeStringAsBytes())
  1734  	case *bool:
  1735  		*v = d.d.DecodeBool()
  1736  	case *int:
  1737  		*v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  1738  	case *int8:
  1739  		*v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8))
  1740  	case *int16:
  1741  		*v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16))
  1742  	case *int32:
  1743  		*v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32))
  1744  	case *int64:
  1745  		*v = d.d.DecodeInt64()
  1746  	case *uint:
  1747  		*v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  1748  	case *uint8:
  1749  		*v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))
  1750  	case *uint16:
  1751  		*v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))
  1752  	case *uint32:
  1753  		*v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))
  1754  	case *uint64:
  1755  		*v = d.d.DecodeUint64()
  1756  	case *float32:
  1757  		*v = d.decodeFloat32()
  1758  	case *float64:
  1759  		*v = d.d.DecodeFloat64()
  1760  	case *complex64:
  1761  		*v = complex(d.decodeFloat32(), 0)
  1762  	case *complex128:
  1763  		*v = complex(d.d.DecodeFloat64(), 0)
  1764  	case *[]byte:
  1765  		*v = d.decodeBytesInto(*v)
  1766  	case []byte:
  1767  		// not addressable byte slice, so do not decode into it past the length
  1768  		b := d.decodeBytesInto(v[:len(v):len(v)])
  1769  		if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) { // not same slice
  1770  			copy(v, b)
  1771  		}
  1772  	case *time.Time:
  1773  		*v = d.d.DecodeTime()
  1774  	case *Raw:
  1775  		*v = d.rawBytes()
  1776  
  1777  	case *interface{}:
  1778  		d.decodeValue(rv4iptr(v), nil)
  1779  
  1780  	default:
  1781  		// we can't check non-predefined types, as they might be a Selfer or extension.
  1782  		if skipFastpathTypeSwitchInDirectCall || !fastpathDecodeTypeSwitch(iv, d) {
  1783  			v := reflect.ValueOf(iv)
  1784  			if x, _ := isDecodeable(v); !x {
  1785  				d.haltAsNotDecodeable(v)
  1786  			}
  1787  			d.decodeValue(v, nil)
  1788  		}
  1789  	}
  1790  }
  1791  
  1792  // decodeValue MUST be called by the actual value we want to decode into,
  1793  // not its addr or a reference to it.
  1794  //
  1795  // This way, we know if it is itself a pointer, and can handle nil in
  1796  // the stream effectively.
  1797  //
  1798  // Note that decodeValue will handle nil in the stream early, so that the
  1799  // subsequent calls i.e. kXXX methods, etc do not have to handle it themselves.
  1800  func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn) {
  1801  	if d.d.TryNil() {
  1802  		decSetNonNilRV2Zero(rv)
  1803  		return
  1804  	}
  1805  	d.decodeValueNoCheckNil(rv, fn)
  1806  }
  1807  
  1808  func (d *Decoder) decodeValueNoCheckNil(rv reflect.Value, fn *codecFn) {
  1809  	// If stream is not containing a nil value, then we can deref to the base
  1810  	// non-pointer value, and decode into that.
  1811  	var rvp reflect.Value
  1812  	var rvpValid bool
  1813  PTR:
  1814  	if rv.Kind() == reflect.Ptr {
  1815  		rvpValid = true
  1816  		if rvIsNil(rv) {
  1817  			rvSetDirect(rv, reflect.New(rvType(rv).Elem()))
  1818  		}
  1819  		rvp = rv
  1820  		rv = rv.Elem()
  1821  		goto PTR
  1822  	}
  1823  
  1824  	if fn == nil {
  1825  		fn = d.h.fn(rvType(rv))
  1826  	}
  1827  	if fn.i.addrD {
  1828  		if rvpValid {
  1829  			rv = rvp
  1830  		} else if rv.CanAddr() {
  1831  			rv = rvAddr(rv, fn.i.ti.ptr)
  1832  		} else if fn.i.addrDf {
  1833  			d.errorf("cannot decode into a non-pointer value")
  1834  		}
  1835  	}
  1836  	fn.fd(d, &fn.i, rv)
  1837  }
  1838  
  1839  func (d *Decoder) structFieldNotFound(index int, rvkencname string) {
  1840  	// Note: rvkencname is used only if there is an error, to pass into d.errorf.
  1841  	// Consequently, it is ok to pass in a stringView
  1842  	// Since rvkencname may be a stringView, do NOT pass it to another function.
  1843  	if d.h.ErrorIfNoField {
  1844  		if index >= 0 {
  1845  			d.errorf("no matching struct field found when decoding stream array at index %v", index)
  1846  		} else if rvkencname != "" {
  1847  			d.errorf("no matching struct field found when decoding stream map with key " + rvkencname)
  1848  		}
  1849  	}
  1850  	d.swallow()
  1851  }
  1852  
  1853  func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) {
  1854  	if d.h.ErrorIfNoArrayExpand {
  1855  		d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen)
  1856  	}
  1857  }
  1858  
  1859  func (d *Decoder) haltAsNotDecodeable(rv reflect.Value) {
  1860  	if !rv.IsValid() {
  1861  		d.onerror(errCannotDecodeIntoNil)
  1862  	}
  1863  	// check if an interface can be retrieved, before grabbing an interface
  1864  	if !rv.CanInterface() {
  1865  		d.errorf("cannot decode into a value without an interface: %v", rv)
  1866  	}
  1867  	d.errorf("cannot decode into value of kind: %v, %#v", rv.Kind(), rv2i(rv))
  1868  }
  1869  
  1870  func (d *Decoder) depthIncr() {
  1871  	d.depth++
  1872  	if d.depth >= d.maxdepth {
  1873  		d.onerror(errMaxDepthExceeded)
  1874  	}
  1875  }
  1876  
  1877  func (d *Decoder) depthDecr() {
  1878  	d.depth--
  1879  }
  1880  
  1881  // Possibly get an interned version of a string, iff InternString=true and decoding a map key.
  1882  //
  1883  // This should mostly be used for map keys, where the key type is string.
  1884  // This is because keys of a map/struct are typically reused across many objects.
  1885  func (d *Decoder) string(v []byte) (s string) {
  1886  	if d.is == nil || d.c != containerMapKey || len(v) < 2 || len(v) > internMaxStrLen {
  1887  		return string(v)
  1888  	}
  1889  	return d.is.string(v)
  1890  }
  1891  
  1892  func (d *Decoder) zerocopy() bool {
  1893  	return d.bytes && d.h.ZeroCopy
  1894  }
  1895  
  1896  // decodeBytesInto is a convenience delegate function to decDriver.DecodeBytes.
  1897  // It ensures that `in` is not a nil byte, before calling decDriver.DecodeBytes,
  1898  // as decDriver.DecodeBytes treats a nil as a hint to use its internal scratch buffer.
  1899  func (d *Decoder) decodeBytesInto(in []byte) (v []byte) {
  1900  	if in == nil {
  1901  		in = []byte{}
  1902  	}
  1903  	return d.d.DecodeBytes(in)
  1904  }
  1905  
  1906  func (d *Decoder) rawBytes() (v []byte) {
  1907  	// ensure that this is not a view into the bytes
  1908  	// i.e. if necessary, make new copy always.
  1909  	v = d.d.nextValueBytes([]byte{})
  1910  	if d.bytes && !d.h.ZeroCopy {
  1911  		v0 := v
  1912  		v = make([]byte, len(v))
  1913  		copy(v, v0)
  1914  	}
  1915  	return
  1916  }
  1917  
  1918  func (d *Decoder) wrapErr(v error, err *error) {
  1919  	*err = wrapCodecErr(v, d.hh.Name(), d.NumBytesRead(), false)
  1920  }
  1921  
  1922  // NumBytesRead returns the number of bytes read
  1923  func (d *Decoder) NumBytesRead() int {
  1924  	return int(d.r().numread())
  1925  }
  1926  
  1927  // decodeFloat32 will delegate to an appropriate DecodeFloat32 implementation (if exists),
  1928  // else if will call DecodeFloat64 and ensure the value doesn't overflow.
  1929  //
  1930  // Note that we return float64 to reduce unnecessary conversions
  1931  func (d *Decoder) decodeFloat32() float32 {
  1932  	if d.js {
  1933  		return d.jsondriver().DecodeFloat32() // custom implementation for 32-bit
  1934  	}
  1935  	return float32(chkOvf.Float32V(d.d.DecodeFloat64()))
  1936  }
  1937  
  1938  // ---- container tracking
  1939  // Note: We update the .c after calling the callback.
  1940  // This way, the callback can know what the last status was.
  1941  
  1942  // MARKER: do not call mapEnd if mapStart returns containerLenNil.
  1943  
  1944  func (d *Decoder) containerNext(j, containerLen int, hasLen bool) bool {
  1945  	// return (hasLen && j < containerLen) || !(hasLen || slh.d.checkBreak())
  1946  	if hasLen {
  1947  		return j < containerLen
  1948  	}
  1949  	return !d.checkBreak()
  1950  }
  1951  
  1952  func (d *Decoder) mapStart(v int) int {
  1953  	if v != containerLenNil {
  1954  		d.depthIncr()
  1955  		d.c = containerMapStart
  1956  	}
  1957  	return v
  1958  }
  1959  
  1960  func (d *Decoder) mapElemKey() {
  1961  	if d.js {
  1962  		d.jsondriver().ReadMapElemKey()
  1963  	}
  1964  	d.c = containerMapKey
  1965  }
  1966  
  1967  func (d *Decoder) mapElemValue() {
  1968  	if d.js {
  1969  		d.jsondriver().ReadMapElemValue()
  1970  	}
  1971  	d.c = containerMapValue
  1972  }
  1973  
  1974  func (d *Decoder) mapEnd() {
  1975  	d.d.ReadMapEnd()
  1976  	d.depthDecr()
  1977  	d.c = 0
  1978  }
  1979  
  1980  func (d *Decoder) arrayStart(v int) int {
  1981  	if v != containerLenNil {
  1982  		d.depthIncr()
  1983  		d.c = containerArrayStart
  1984  	}
  1985  	return v
  1986  }
  1987  
  1988  func (d *Decoder) arrayElem() {
  1989  	if d.js {
  1990  		d.jsondriver().ReadArrayElem()
  1991  	}
  1992  	d.c = containerArrayElem
  1993  }
  1994  
  1995  func (d *Decoder) arrayEnd() {
  1996  	d.d.ReadArrayEnd()
  1997  	d.depthDecr()
  1998  	d.c = 0
  1999  }
  2000  
  2001  func (d *Decoder) interfaceExtConvertAndDecode(v interface{}, ext InterfaceExt) {
  2002  	// var v interface{} = ext.ConvertExt(rv)
  2003  	// d.d.decode(&v)
  2004  	// ext.UpdateExt(rv, v)
  2005  
  2006  	// assume v is a pointer:
  2007  	// - if struct|array, pass as is to ConvertExt
  2008  	// - else make it non-addressable and pass to ConvertExt
  2009  	// - make return value from ConvertExt addressable
  2010  	// - decode into it
  2011  	// - return the interface for passing into UpdateExt.
  2012  	// - interface should be a pointer if struct|array, else a value
  2013  
  2014  	var s interface{}
  2015  	rv := reflect.ValueOf(v)
  2016  	rv2 := rv.Elem()
  2017  	rvk := rv2.Kind()
  2018  	if rvk == reflect.Struct || rvk == reflect.Array {
  2019  		s = ext.ConvertExt(v)
  2020  	} else {
  2021  		s = ext.ConvertExt(rv2i(rv2))
  2022  	}
  2023  	rv = reflect.ValueOf(s)
  2024  
  2025  	// We cannot use isDecodeable here, as the value converted may be nil,
  2026  	// or it may not be nil but is not addressable and thus we cannot extend it, etc.
  2027  	// Instead, we just ensure that the value is addressable.
  2028  
  2029  	if !rv.CanAddr() {
  2030  		rvk = rv.Kind()
  2031  		rv2 = d.oneShotAddrRV(rvType(rv), rvk)
  2032  		if rvk == reflect.Interface {
  2033  			rvSetIntf(rv2, rv)
  2034  		} else {
  2035  			rvSetDirect(rv2, rv)
  2036  		}
  2037  		rv = rv2
  2038  	}
  2039  
  2040  	d.decodeValue(rv, nil)
  2041  	ext.UpdateExt(v, rv2i(rv))
  2042  }
  2043  
  2044  func (d *Decoder) sideDecode(v interface{}, basetype reflect.Type, bs []byte) {
  2045  	// NewDecoderBytes(bs, d.hh).decodeValue(baseRV(v), d.h.fnNoExt(basetype))
  2046  
  2047  	defer func(rb bytesDecReader, bytes bool,
  2048  		c containerState, dbs decByteState, depth int16, r decReader, state interface{}) {
  2049  		d.rb = rb
  2050  		d.bytes = bytes
  2051  		d.c = c
  2052  		d.decByteState = dbs
  2053  		d.depth = depth
  2054  		d.decReader = r
  2055  		d.d.restoreState(state)
  2056  	}(d.rb, d.bytes, d.c, d.decByteState, d.depth, d.decReader, d.d.captureState())
  2057  
  2058  	// d.rb.reset(in)
  2059  	d.rb = bytesDecReader{bs[:len(bs):len(bs)], 0}
  2060  	d.bytes = true
  2061  	d.decReader = &d.rb
  2062  	d.d.resetState()
  2063  	d.c = 0
  2064  	d.decByteState = decByteStateNone
  2065  	d.depth = 0
  2066  
  2067  	// must call using fnNoExt
  2068  	d.decodeValue(baseRV(v), d.h.fnNoExt(basetype))
  2069  }
  2070  
  2071  func (d *Decoder) fauxUnionReadRawBytes(asString bool) {
  2072  	if asString || d.h.RawToString {
  2073  		d.n.v = valueTypeString
  2074  		// fauxUnion is only used within DecodeNaked calls; consequently, we should try to intern.
  2075  		d.n.s = d.stringZC(d.d.DecodeBytes(nil))
  2076  	} else {
  2077  		d.n.v = valueTypeBytes
  2078  		d.n.l = d.d.DecodeBytes([]byte{})
  2079  	}
  2080  }
  2081  
  2082  func (d *Decoder) oneShotAddrRV(rvt reflect.Type, rvk reflect.Kind) reflect.Value {
  2083  	if decUseTransient &&
  2084  		(numBoolStrSliceBitset.isset(byte(rvk)) ||
  2085  			((rvk == reflect.Struct || rvk == reflect.Array) &&
  2086  				d.h.getTypeInfo(rt2id(rvt), rvt).flagCanTransient)) {
  2087  		return d.perType.TransientAddrK(rvt, rvk)
  2088  	}
  2089  	return rvZeroAddrK(rvt, rvk)
  2090  }
  2091  
  2092  // --------------------------------------------------
  2093  
  2094  // decSliceHelper assists when decoding into a slice, from a map or an array in the stream.
  2095  // A slice can be set from a map or array in stream. This supports the MapBySlice interface.
  2096  //
  2097  // Note: if IsNil, do not call ElemContainerState.
  2098  type decSliceHelper struct {
  2099  	d     *Decoder
  2100  	ct    valueType
  2101  	Array bool
  2102  	IsNil bool
  2103  }
  2104  
  2105  func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) {
  2106  	x.ct = d.d.ContainerType()
  2107  	x.d = d
  2108  	switch x.ct {
  2109  	case valueTypeNil:
  2110  		x.IsNil = true
  2111  	case valueTypeArray:
  2112  		x.Array = true
  2113  		clen = d.arrayStart(d.d.ReadArrayStart())
  2114  	case valueTypeMap:
  2115  		clen = d.mapStart(d.d.ReadMapStart())
  2116  		clen += clen
  2117  	default:
  2118  		d.errorf("only encoded map or array can be decoded into a slice (%d)", x.ct)
  2119  	}
  2120  	return
  2121  }
  2122  
  2123  func (x decSliceHelper) End() {
  2124  	if x.IsNil {
  2125  	} else if x.Array {
  2126  		x.d.arrayEnd()
  2127  	} else {
  2128  		x.d.mapEnd()
  2129  	}
  2130  }
  2131  
  2132  func (x decSliceHelper) ElemContainerState(index int) {
  2133  	// Note: if isnil, clen=0, so we never call into ElemContainerState
  2134  
  2135  	if x.Array {
  2136  		x.d.arrayElem()
  2137  	} else if index&1 == 0 { // index%2 == 0 {
  2138  		x.d.mapElemKey()
  2139  	} else {
  2140  		x.d.mapElemValue()
  2141  	}
  2142  }
  2143  
  2144  func (x decSliceHelper) arrayCannotExpand(hasLen bool, lenv, j, containerLenS int) {
  2145  	x.d.arrayCannotExpand(lenv, j+1)
  2146  	// drain completely and return
  2147  	x.ElemContainerState(j)
  2148  	x.d.swallow()
  2149  	j++
  2150  	for ; x.d.containerNext(j, containerLenS, hasLen); j++ {
  2151  		x.ElemContainerState(j)
  2152  		x.d.swallow()
  2153  	}
  2154  	x.End()
  2155  }
  2156  
  2157  // decNextValueBytesHelper helps with NextValueBytes calls.
  2158  //
  2159  // Typical usage:
  2160  //    - each Handle's decDriver will implement a high level nextValueBytes,
  2161  //      which will track the current cursor, delegate to a nextValueBytesR
  2162  //      method, and then potentially call bytesRdV at the end.
  2163  //
  2164  // See simple.go for typical usage model.
  2165  type decNextValueBytesHelper struct {
  2166  	d *Decoder
  2167  }
  2168  
  2169  func (x decNextValueBytesHelper) append1(v *[]byte, b byte) {
  2170  	if *v != nil && !x.d.bytes {
  2171  		*v = append(*v, b)
  2172  	}
  2173  }
  2174  
  2175  func (x decNextValueBytesHelper) appendN(v *[]byte, b ...byte) {
  2176  	if *v != nil && !x.d.bytes {
  2177  		*v = append(*v, b...)
  2178  	}
  2179  }
  2180  
  2181  func (x decNextValueBytesHelper) bytesRdV(v *[]byte, startpos uint) {
  2182  	if x.d.bytes {
  2183  		*v = x.d.rb.b[startpos:x.d.rb.c]
  2184  	}
  2185  }
  2186  
  2187  // decNegintPosintFloatNumberHelper is used for formats that are binary
  2188  // and have distinct ways of storing positive integers vs negative integers
  2189  // vs floats, which are uniquely identified by the byte descriptor.
  2190  //
  2191  // Currently, these formats are binc, cbor and simple.
  2192  type decNegintPosintFloatNumberHelper struct {
  2193  	d *Decoder
  2194  }
  2195  
  2196  func (x decNegintPosintFloatNumberHelper) uint64(ui uint64, neg, ok bool) uint64 {
  2197  	if ok && !neg {
  2198  		return ui
  2199  	}
  2200  	return x.uint64TryFloat(ok)
  2201  }
  2202  
  2203  func (x decNegintPosintFloatNumberHelper) uint64TryFloat(ok bool) (ui uint64) {
  2204  	if ok { // neg = true
  2205  		x.d.errorf("assigning negative signed value to unsigned type")
  2206  	}
  2207  	f, ok := x.d.d.decFloat()
  2208  	if ok && f >= 0 && noFrac64(math.Float64bits(f)) {
  2209  		ui = uint64(f)
  2210  	} else {
  2211  		x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd())
  2212  	}
  2213  	return ui
  2214  }
  2215  
  2216  func decNegintPosintFloatNumberHelperInt64v(ui uint64, neg, incrIfNeg bool) (i int64) {
  2217  	if neg && incrIfNeg {
  2218  		ui++
  2219  	}
  2220  	i = chkOvf.SignedIntV(ui)
  2221  	if neg {
  2222  		i = -i
  2223  	}
  2224  	return
  2225  }
  2226  
  2227  func (x decNegintPosintFloatNumberHelper) int64(ui uint64, neg, ok bool) (i int64) {
  2228  	if ok {
  2229  		return decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor)
  2230  	}
  2231  	// 	return x.int64TryFloat()
  2232  	// }
  2233  	// func (x decNegintPosintFloatNumberHelper) int64TryFloat() (i int64) {
  2234  	f, ok := x.d.d.decFloat()
  2235  	if ok && noFrac64(math.Float64bits(f)) {
  2236  		i = int64(f)
  2237  	} else {
  2238  		x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd())
  2239  	}
  2240  	return
  2241  }
  2242  
  2243  func (x decNegintPosintFloatNumberHelper) float64(f float64, ok bool) float64 {
  2244  	if ok {
  2245  		return f
  2246  	}
  2247  	return x.float64TryInteger()
  2248  }
  2249  
  2250  func (x decNegintPosintFloatNumberHelper) float64TryInteger() float64 {
  2251  	ui, neg, ok := x.d.d.decInteger()
  2252  	if !ok {
  2253  		x.d.errorf("invalid descriptor for float: %v", x.d.d.descBd())
  2254  	}
  2255  	return float64(decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor))
  2256  }
  2257  
  2258  // isDecodeable checks if value can be decoded into
  2259  //
  2260  // decode can take any reflect.Value that is a inherently addressable i.e.
  2261  //   - non-nil chan    (we will SEND to it)
  2262  //   - non-nil slice   (we will set its elements)
  2263  //   - non-nil map     (we will put into it)
  2264  //   - non-nil pointer (we can "update" it)
  2265  //   - func: no
  2266  //   - interface: no
  2267  //   - array:                   if canAddr=true
  2268  //   - any other value pointer: if canAddr=true
  2269  func isDecodeable(rv reflect.Value) (canDecode bool, reason decNotDecodeableReason) {
  2270  	switch rv.Kind() {
  2271  	case reflect.Ptr, reflect.Slice, reflect.Chan, reflect.Map:
  2272  		canDecode = !rvIsNil(rv)
  2273  		reason = decNotDecodeableReasonNilReference
  2274  	case reflect.Func, reflect.Interface, reflect.Invalid, reflect.UnsafePointer:
  2275  		reason = decNotDecodeableReasonBadKind
  2276  	default:
  2277  		canDecode = rv.CanAddr()
  2278  		reason = decNotDecodeableReasonNonAddrValue
  2279  	}
  2280  	return
  2281  }
  2282  
  2283  func decByteSlice(r *decRd, clen, maxInitLen int, bs []byte) (bsOut []byte) {
  2284  	if clen == 0 {
  2285  		return zeroByteSlice
  2286  	}
  2287  	if len(bs) == clen {
  2288  		bsOut = bs
  2289  		r.readb(bsOut)
  2290  	} else if cap(bs) >= clen {
  2291  		bsOut = bs[:clen]
  2292  		r.readb(bsOut)
  2293  	} else {
  2294  		var len2 int
  2295  		for len2 < clen {
  2296  			len3 := decInferLen(clen-len2, maxInitLen, 1)
  2297  			bs3 := bsOut
  2298  			bsOut = make([]byte, len2+len3)
  2299  			copy(bsOut, bs3)
  2300  			r.readb(bsOut[len2:])
  2301  			len2 += len3
  2302  		}
  2303  	}
  2304  	return
  2305  }
  2306  
  2307  // decInferLen will infer a sensible length, given the following:
  2308  //    - clen: length wanted.
  2309  //    - maxlen: max length to be returned.
  2310  //      if <= 0, it is unset, and we infer it based on the unit size
  2311  //    - unit: number of bytes for each element of the collection
  2312  func decInferLen(clen, maxlen, unit int) int {
  2313  	// anecdotal testing showed increase in allocation with map length of 16.
  2314  	// We saw same typical alloc from 0-8, then a 20% increase at 16.
  2315  	// Thus, we set it to 8.
  2316  	const (
  2317  		minLenIfUnset = 8
  2318  		maxMem        = 256 * 1024 // 256Kb Memory
  2319  	)
  2320  
  2321  	// handle when maxlen is not set i.e. <= 0
  2322  
  2323  	// clen==0:           use 0
  2324  	// maxlen<=0, clen<0: use default
  2325  	// maxlen> 0, clen<0: use default
  2326  	// maxlen<=0, clen>0: infer maxlen, and cap on it
  2327  	// maxlen> 0, clen>0: cap at maxlen
  2328  
  2329  	if clen == 0 || clen == containerLenNil {
  2330  		return 0
  2331  	}
  2332  	if clen < 0 {
  2333  		// if unspecified, return 64 for bytes, ... 8 for uint64, ... and everything else
  2334  		clen = 64 / unit
  2335  		if clen > minLenIfUnset {
  2336  			return clen
  2337  		}
  2338  		return minLenIfUnset
  2339  	}
  2340  	if unit <= 0 {
  2341  		return clen
  2342  	}
  2343  	if maxlen <= 0 {
  2344  		maxlen = maxMem / unit
  2345  	}
  2346  	if clen < maxlen {
  2347  		return clen
  2348  	}
  2349  	return maxlen
  2350  }