github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/encoding/gob/decode.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:generate go run decgen.go -output dec_helpers.go
     6  
     7  package gob
     8  
     9  import (
    10  	"encoding"
    11  	"errors"
    12  	"internal/saferio"
    13  	"io"
    14  	"math"
    15  	"math/bits"
    16  	"reflect"
    17  )
    18  
    19  var (
    20  	errBadUint = errors.New("gob: encoded unsigned integer out of range")
    21  	errBadType = errors.New("gob: unknown type id or corrupted data")
    22  	errRange   = errors.New("gob: bad data: field numbers out of bounds")
    23  )
    24  
    25  type decHelper func(state *decoderState, v reflect.Value, length int, ovfl error) bool
    26  
    27  // decoderState is the execution state of an instance of the decoder. A new state
    28  // is created for nested objects.
    29  type decoderState struct {
    30  	dec *Decoder
    31  	// The buffer is stored with an extra indirection because it may be replaced
    32  	// if we load a type during decode (when reading an interface value).
    33  	b        *decBuffer
    34  	fieldnum int           // the last field number read.
    35  	next     *decoderState // for free list
    36  }
    37  
    38  // decBuffer is an extremely simple, fast implementation of a read-only byte buffer.
    39  // It is initialized by calling Size and then copying the data into the slice returned by Bytes().
    40  type decBuffer struct {
    41  	data   []byte
    42  	offset int // Read offset.
    43  }
    44  
    45  func (d *decBuffer) Read(p []byte) (int, error) {
    46  	n := copy(p, d.data[d.offset:])
    47  	if n == 0 && len(p) != 0 {
    48  		return 0, io.EOF
    49  	}
    50  	d.offset += n
    51  	return n, nil
    52  }
    53  
    54  func (d *decBuffer) Drop(n int) {
    55  	if n > d.Len() {
    56  		panic("drop")
    57  	}
    58  	d.offset += n
    59  }
    60  
    61  func (d *decBuffer) ReadByte() (byte, error) {
    62  	if d.offset >= len(d.data) {
    63  		return 0, io.EOF
    64  	}
    65  	c := d.data[d.offset]
    66  	d.offset++
    67  	return c, nil
    68  }
    69  
    70  func (d *decBuffer) Len() int {
    71  	return len(d.data) - d.offset
    72  }
    73  
    74  func (d *decBuffer) Bytes() []byte {
    75  	return d.data[d.offset:]
    76  }
    77  
    78  // SetBytes sets the buffer to the bytes, discarding any existing data.
    79  func (d *decBuffer) SetBytes(data []byte) {
    80  	d.data = data
    81  	d.offset = 0
    82  }
    83  
    84  func (d *decBuffer) Reset() {
    85  	d.data = d.data[0:0]
    86  	d.offset = 0
    87  }
    88  
    89  // We pass the bytes.Buffer separately for easier testing of the infrastructure
    90  // without requiring a full Decoder.
    91  func (dec *Decoder) newDecoderState(buf *decBuffer) *decoderState {
    92  	d := dec.freeList
    93  	if d == nil {
    94  		d = new(decoderState)
    95  		d.dec = dec
    96  	} else {
    97  		dec.freeList = d.next
    98  	}
    99  	d.b = buf
   100  	return d
   101  }
   102  
   103  func (dec *Decoder) freeDecoderState(d *decoderState) {
   104  	d.next = dec.freeList
   105  	dec.freeList = d
   106  }
   107  
   108  func overflow(name string) error {
   109  	return errors.New(`value for "` + name + `" out of range`)
   110  }
   111  
   112  // decodeUintReader reads an encoded unsigned integer from an io.Reader.
   113  // Used only by the Decoder to read the message length.
   114  func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err error) {
   115  	width = 1
   116  	n, err := io.ReadFull(r, buf[0:width])
   117  	if n == 0 {
   118  		return
   119  	}
   120  	b := buf[0]
   121  	if b <= 0x7f {
   122  		return uint64(b), width, nil
   123  	}
   124  	n = -int(int8(b))
   125  	if n > uint64Size {
   126  		err = errBadUint
   127  		return
   128  	}
   129  	width, err = io.ReadFull(r, buf[0:n])
   130  	if err != nil {
   131  		if err == io.EOF {
   132  			err = io.ErrUnexpectedEOF
   133  		}
   134  		return
   135  	}
   136  	// Could check that the high byte is zero but it's not worth it.
   137  	for _, b := range buf[0:width] {
   138  		x = x<<8 | uint64(b)
   139  	}
   140  	width++ // +1 for length byte
   141  	return
   142  }
   143  
   144  // decodeUint reads an encoded unsigned integer from state.r.
   145  // Does not check for overflow.
   146  func (state *decoderState) decodeUint() (x uint64) {
   147  	b, err := state.b.ReadByte()
   148  	if err != nil {
   149  		error_(err)
   150  	}
   151  	if b <= 0x7f {
   152  		return uint64(b)
   153  	}
   154  	n := -int(int8(b))
   155  	if n > uint64Size {
   156  		error_(errBadUint)
   157  	}
   158  	buf := state.b.Bytes()
   159  	if len(buf) < n {
   160  		errorf("invalid uint data length %d: exceeds input size %d", n, len(buf))
   161  	}
   162  	// Don't need to check error; it's safe to loop regardless.
   163  	// Could check that the high byte is zero but it's not worth it.
   164  	for _, b := range buf[0:n] {
   165  		x = x<<8 | uint64(b)
   166  	}
   167  	state.b.Drop(n)
   168  	return x
   169  }
   170  
   171  // decodeInt reads an encoded signed integer from state.r.
   172  // Does not check for overflow.
   173  func (state *decoderState) decodeInt() int64 {
   174  	x := state.decodeUint()
   175  	if x&1 != 0 {
   176  		return ^int64(x >> 1)
   177  	}
   178  	return int64(x >> 1)
   179  }
   180  
   181  // getLength decodes the next uint and makes sure it is a possible
   182  // size for a data item that follows, which means it must fit in a
   183  // non-negative int and fit in the buffer.
   184  func (state *decoderState) getLength() (int, bool) {
   185  	n := int(state.decodeUint())
   186  	if n < 0 || state.b.Len() < n || tooBig <= n {
   187  		return 0, false
   188  	}
   189  	return n, true
   190  }
   191  
   192  // decOp is the signature of a decoding operator for a given type.
   193  type decOp func(i *decInstr, state *decoderState, v reflect.Value)
   194  
   195  // The 'instructions' of the decoding machine
   196  type decInstr struct {
   197  	op    decOp
   198  	field int   // field number of the wire type
   199  	index []int // field access indices for destination type
   200  	ovfl  error // error message for overflow/underflow (for arrays, of the elements)
   201  }
   202  
   203  // ignoreUint discards a uint value with no destination.
   204  func ignoreUint(i *decInstr, state *decoderState, v reflect.Value) {
   205  	state.decodeUint()
   206  }
   207  
   208  // ignoreTwoUints discards a uint value with no destination. It's used to skip
   209  // complex values.
   210  func ignoreTwoUints(i *decInstr, state *decoderState, v reflect.Value) {
   211  	state.decodeUint()
   212  	state.decodeUint()
   213  }
   214  
   215  // Since the encoder writes no zeros, if we arrive at a decoder we have
   216  // a value to extract and store. The field number has already been read
   217  // (it's how we knew to call this decoder).
   218  // Each decoder is responsible for handling any indirections associated
   219  // with the data structure. If any pointer so reached is nil, allocation must
   220  // be done.
   221  
   222  // decAlloc takes a value and returns a settable value that can
   223  // be assigned to. If the value is a pointer, decAlloc guarantees it points to storage.
   224  // The callers to the individual decoders are expected to have used decAlloc.
   225  // The individual decoders don't need to it.
   226  func decAlloc(v reflect.Value) reflect.Value {
   227  	for v.Kind() == reflect.Pointer {
   228  		if v.IsNil() {
   229  			v.Set(reflect.New(v.Type().Elem()))
   230  		}
   231  		v = v.Elem()
   232  	}
   233  	return v
   234  }
   235  
   236  // decBool decodes a uint and stores it as a boolean in value.
   237  func decBool(i *decInstr, state *decoderState, value reflect.Value) {
   238  	value.SetBool(state.decodeUint() != 0)
   239  }
   240  
   241  // decInt8 decodes an integer and stores it as an int8 in value.
   242  func decInt8(i *decInstr, state *decoderState, value reflect.Value) {
   243  	v := state.decodeInt()
   244  	if v < math.MinInt8 || math.MaxInt8 < v {
   245  		error_(i.ovfl)
   246  	}
   247  	value.SetInt(v)
   248  }
   249  
   250  // decUint8 decodes an unsigned integer and stores it as a uint8 in value.
   251  func decUint8(i *decInstr, state *decoderState, value reflect.Value) {
   252  	v := state.decodeUint()
   253  	if math.MaxUint8 < v {
   254  		error_(i.ovfl)
   255  	}
   256  	value.SetUint(v)
   257  }
   258  
   259  // decInt16 decodes an integer and stores it as an int16 in value.
   260  func decInt16(i *decInstr, state *decoderState, value reflect.Value) {
   261  	v := state.decodeInt()
   262  	if v < math.MinInt16 || math.MaxInt16 < v {
   263  		error_(i.ovfl)
   264  	}
   265  	value.SetInt(v)
   266  }
   267  
   268  // decUint16 decodes an unsigned integer and stores it as a uint16 in value.
   269  func decUint16(i *decInstr, state *decoderState, value reflect.Value) {
   270  	v := state.decodeUint()
   271  	if math.MaxUint16 < v {
   272  		error_(i.ovfl)
   273  	}
   274  	value.SetUint(v)
   275  }
   276  
   277  // decInt32 decodes an integer and stores it as an int32 in value.
   278  func decInt32(i *decInstr, state *decoderState, value reflect.Value) {
   279  	v := state.decodeInt()
   280  	if v < math.MinInt32 || math.MaxInt32 < v {
   281  		error_(i.ovfl)
   282  	}
   283  	value.SetInt(v)
   284  }
   285  
   286  // decUint32 decodes an unsigned integer and stores it as a uint32 in value.
   287  func decUint32(i *decInstr, state *decoderState, value reflect.Value) {
   288  	v := state.decodeUint()
   289  	if math.MaxUint32 < v {
   290  		error_(i.ovfl)
   291  	}
   292  	value.SetUint(v)
   293  }
   294  
   295  // decInt64 decodes an integer and stores it as an int64 in value.
   296  func decInt64(i *decInstr, state *decoderState, value reflect.Value) {
   297  	v := state.decodeInt()
   298  	value.SetInt(v)
   299  }
   300  
   301  // decUint64 decodes an unsigned integer and stores it as a uint64 in value.
   302  func decUint64(i *decInstr, state *decoderState, value reflect.Value) {
   303  	v := state.decodeUint()
   304  	value.SetUint(v)
   305  }
   306  
   307  // Floating-point numbers are transmitted as uint64s holding the bits
   308  // of the underlying representation. They are sent byte-reversed, with
   309  // the exponent end coming out first, so integer floating point numbers
   310  // (for example) transmit more compactly. This routine does the
   311  // unswizzling.
   312  func float64FromBits(u uint64) float64 {
   313  	v := bits.ReverseBytes64(u)
   314  	return math.Float64frombits(v)
   315  }
   316  
   317  // float32FromBits decodes an unsigned integer, treats it as a 32-bit floating-point
   318  // number, and returns it. It's a helper function for float32 and complex64.
   319  // It returns a float64 because that's what reflection needs, but its return
   320  // value is known to be accurately representable in a float32.
   321  func float32FromBits(u uint64, ovfl error) float64 {
   322  	v := float64FromBits(u)
   323  	av := v
   324  	if av < 0 {
   325  		av = -av
   326  	}
   327  	// +Inf is OK in both 32- and 64-bit floats. Underflow is always OK.
   328  	if math.MaxFloat32 < av && av <= math.MaxFloat64 {
   329  		error_(ovfl)
   330  	}
   331  	return v
   332  }
   333  
   334  // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
   335  // number, and stores it in value.
   336  func decFloat32(i *decInstr, state *decoderState, value reflect.Value) {
   337  	value.SetFloat(float32FromBits(state.decodeUint(), i.ovfl))
   338  }
   339  
   340  // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point
   341  // number, and stores it in value.
   342  func decFloat64(i *decInstr, state *decoderState, value reflect.Value) {
   343  	value.SetFloat(float64FromBits(state.decodeUint()))
   344  }
   345  
   346  // decComplex64 decodes a pair of unsigned integers, treats them as a
   347  // pair of floating point numbers, and stores them as a complex64 in value.
   348  // The real part comes first.
   349  func decComplex64(i *decInstr, state *decoderState, value reflect.Value) {
   350  	real := float32FromBits(state.decodeUint(), i.ovfl)
   351  	imag := float32FromBits(state.decodeUint(), i.ovfl)
   352  	value.SetComplex(complex(real, imag))
   353  }
   354  
   355  // decComplex128 decodes a pair of unsigned integers, treats them as a
   356  // pair of floating point numbers, and stores them as a complex128 in value.
   357  // The real part comes first.
   358  func decComplex128(i *decInstr, state *decoderState, value reflect.Value) {
   359  	real := float64FromBits(state.decodeUint())
   360  	imag := float64FromBits(state.decodeUint())
   361  	value.SetComplex(complex(real, imag))
   362  }
   363  
   364  // decUint8Slice decodes a byte slice and stores in value a slice header
   365  // describing the data.
   366  // uint8 slices are encoded as an unsigned count followed by the raw bytes.
   367  func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) {
   368  	n, ok := state.getLength()
   369  	if !ok {
   370  		errorf("bad %s slice length: %d", value.Type(), n)
   371  	}
   372  	if value.Cap() < n {
   373  		value.Set(reflect.MakeSlice(value.Type(), n, n))
   374  	} else {
   375  		value.SetLen(n)
   376  	}
   377  	if _, err := state.b.Read(value.Bytes()); err != nil {
   378  		errorf("error decoding []byte: %s", err)
   379  	}
   380  }
   381  
   382  // decString decodes byte array and stores in value a string header
   383  // describing the data.
   384  // Strings are encoded as an unsigned count followed by the raw bytes.
   385  func decString(i *decInstr, state *decoderState, value reflect.Value) {
   386  	n, ok := state.getLength()
   387  	if !ok {
   388  		errorf("bad %s slice length: %d", value.Type(), n)
   389  	}
   390  	// Read the data.
   391  	data := state.b.Bytes()
   392  	if len(data) < n {
   393  		errorf("invalid string length %d: exceeds input size %d", n, len(data))
   394  	}
   395  	s := string(data[:n])
   396  	state.b.Drop(n)
   397  	value.SetString(s)
   398  }
   399  
   400  // ignoreUint8Array skips over the data for a byte slice value with no destination.
   401  func ignoreUint8Array(i *decInstr, state *decoderState, value reflect.Value) {
   402  	n, ok := state.getLength()
   403  	if !ok {
   404  		errorf("slice length too large")
   405  	}
   406  	bn := state.b.Len()
   407  	if bn < n {
   408  		errorf("invalid slice length %d: exceeds input size %d", n, bn)
   409  	}
   410  	state.b.Drop(n)
   411  }
   412  
   413  // Execution engine
   414  
   415  // The encoder engine is an array of instructions indexed by field number of the incoming
   416  // decoder. It is executed with random access according to field number.
   417  type decEngine struct {
   418  	instr    []decInstr
   419  	numInstr int // the number of active instructions
   420  }
   421  
   422  // decodeSingle decodes a top-level value that is not a struct and stores it in value.
   423  // Such values are preceded by a zero, making them have the memory layout of a
   424  // struct field (although with an illegal field number).
   425  func (dec *Decoder) decodeSingle(engine *decEngine, value reflect.Value) {
   426  	state := dec.newDecoderState(&dec.buf)
   427  	defer dec.freeDecoderState(state)
   428  	state.fieldnum = singletonField
   429  	if state.decodeUint() != 0 {
   430  		errorf("decode: corrupted data: non-zero delta for singleton")
   431  	}
   432  	instr := &engine.instr[singletonField]
   433  	instr.op(instr, state, value)
   434  }
   435  
   436  // decodeStruct decodes a top-level struct and stores it in value.
   437  // Indir is for the value, not the type. At the time of the call it may
   438  // differ from ut.indir, which was computed when the engine was built.
   439  // This state cannot arise for decodeSingle, which is called directly
   440  // from the user's value, not from the innards of an engine.
   441  func (dec *Decoder) decodeStruct(engine *decEngine, value reflect.Value) {
   442  	state := dec.newDecoderState(&dec.buf)
   443  	defer dec.freeDecoderState(state)
   444  	state.fieldnum = -1
   445  	for state.b.Len() > 0 {
   446  		delta := int(state.decodeUint())
   447  		if delta < 0 {
   448  			errorf("decode: corrupted data: negative delta")
   449  		}
   450  		if delta == 0 { // struct terminator is zero delta fieldnum
   451  			break
   452  		}
   453  		if state.fieldnum >= len(engine.instr)-delta { // subtract to compare without overflow
   454  			error_(errRange)
   455  		}
   456  		fieldnum := state.fieldnum + delta
   457  		instr := &engine.instr[fieldnum]
   458  		var field reflect.Value
   459  		if instr.index != nil {
   460  			// Otherwise the field is unknown to us and instr.op is an ignore op.
   461  			field = value.FieldByIndex(instr.index)
   462  			if field.Kind() == reflect.Pointer {
   463  				field = decAlloc(field)
   464  			}
   465  		}
   466  		instr.op(instr, state, field)
   467  		state.fieldnum = fieldnum
   468  	}
   469  }
   470  
   471  var noValue reflect.Value
   472  
   473  // ignoreStruct discards the data for a struct with no destination.
   474  func (dec *Decoder) ignoreStruct(engine *decEngine) {
   475  	state := dec.newDecoderState(&dec.buf)
   476  	defer dec.freeDecoderState(state)
   477  	state.fieldnum = -1
   478  	for state.b.Len() > 0 {
   479  		delta := int(state.decodeUint())
   480  		if delta < 0 {
   481  			errorf("ignore decode: corrupted data: negative delta")
   482  		}
   483  		if delta == 0 { // struct terminator is zero delta fieldnum
   484  			break
   485  		}
   486  		fieldnum := state.fieldnum + delta
   487  		if fieldnum >= len(engine.instr) {
   488  			error_(errRange)
   489  		}
   490  		instr := &engine.instr[fieldnum]
   491  		instr.op(instr, state, noValue)
   492  		state.fieldnum = fieldnum
   493  	}
   494  }
   495  
   496  // ignoreSingle discards the data for a top-level non-struct value with no
   497  // destination. It's used when calling Decode with a nil value.
   498  func (dec *Decoder) ignoreSingle(engine *decEngine) {
   499  	state := dec.newDecoderState(&dec.buf)
   500  	defer dec.freeDecoderState(state)
   501  	state.fieldnum = singletonField
   502  	delta := int(state.decodeUint())
   503  	if delta != 0 {
   504  		errorf("decode: corrupted data: non-zero delta for singleton")
   505  	}
   506  	instr := &engine.instr[singletonField]
   507  	instr.op(instr, state, noValue)
   508  }
   509  
   510  // decodeArrayHelper does the work for decoding arrays and slices.
   511  func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) {
   512  	if helper != nil && helper(state, value, length, ovfl) {
   513  		return
   514  	}
   515  	instr := &decInstr{elemOp, 0, nil, ovfl}
   516  	isPtr := value.Type().Elem().Kind() == reflect.Pointer
   517  	ln := value.Len()
   518  	for i := 0; i < length; i++ {
   519  		if state.b.Len() == 0 {
   520  			errorf("decoding array or slice: length exceeds input size (%d elements)", length)
   521  		}
   522  		if i >= ln {
   523  			// This is a slice that we only partially allocated.
   524  			// Grow it using append, up to length.
   525  			value = reflect.Append(value, reflect.Zero(value.Type().Elem()))
   526  			cp := value.Cap()
   527  			if cp > length {
   528  				cp = length
   529  			}
   530  			value.SetLen(cp)
   531  			ln = cp
   532  		}
   533  		v := value.Index(i)
   534  		if isPtr {
   535  			v = decAlloc(v)
   536  		}
   537  		elemOp(instr, state, v)
   538  	}
   539  }
   540  
   541  // decodeArray decodes an array and stores it in value.
   542  // The length is an unsigned integer preceding the elements. Even though the length is redundant
   543  // (it's part of the type), it's a useful check and is included in the encoding.
   544  func (dec *Decoder) decodeArray(state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) {
   545  	if n := state.decodeUint(); n != uint64(length) {
   546  		errorf("length mismatch in decodeArray")
   547  	}
   548  	dec.decodeArrayHelper(state, value, elemOp, length, ovfl, helper)
   549  }
   550  
   551  // decodeIntoValue is a helper for map decoding.
   552  func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Value, instr *decInstr) reflect.Value {
   553  	v := value
   554  	if isPtr {
   555  		v = decAlloc(value)
   556  	}
   557  
   558  	op(instr, state, v)
   559  	return value
   560  }
   561  
   562  // decodeMap decodes a map and stores it in value.
   563  // Maps are encoded as a length followed by key:value pairs.
   564  // Because the internals of maps are not visible to us, we must
   565  // use reflection rather than pointer magic.
   566  func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value reflect.Value, keyOp, elemOp decOp, ovfl error) {
   567  	n := int(state.decodeUint())
   568  	if value.IsNil() {
   569  		value.Set(reflect.MakeMapWithSize(mtyp, n))
   570  	}
   571  	keyIsPtr := mtyp.Key().Kind() == reflect.Pointer
   572  	elemIsPtr := mtyp.Elem().Kind() == reflect.Pointer
   573  	keyInstr := &decInstr{keyOp, 0, nil, ovfl}
   574  	elemInstr := &decInstr{elemOp, 0, nil, ovfl}
   575  	keyP := reflect.New(mtyp.Key())
   576  	keyZ := reflect.Zero(mtyp.Key())
   577  	elemP := reflect.New(mtyp.Elem())
   578  	elemZ := reflect.Zero(mtyp.Elem())
   579  	for i := 0; i < n; i++ {
   580  		key := decodeIntoValue(state, keyOp, keyIsPtr, keyP.Elem(), keyInstr)
   581  		elem := decodeIntoValue(state, elemOp, elemIsPtr, elemP.Elem(), elemInstr)
   582  		value.SetMapIndex(key, elem)
   583  		keyP.Elem().Set(keyZ)
   584  		elemP.Elem().Set(elemZ)
   585  	}
   586  }
   587  
   588  // ignoreArrayHelper does the work for discarding arrays and slices.
   589  func (dec *Decoder) ignoreArrayHelper(state *decoderState, elemOp decOp, length int) {
   590  	instr := &decInstr{elemOp, 0, nil, errors.New("no error")}
   591  	for i := 0; i < length; i++ {
   592  		if state.b.Len() == 0 {
   593  			errorf("decoding array or slice: length exceeds input size (%d elements)", length)
   594  		}
   595  		elemOp(instr, state, noValue)
   596  	}
   597  }
   598  
   599  // ignoreArray discards the data for an array value with no destination.
   600  func (dec *Decoder) ignoreArray(state *decoderState, elemOp decOp, length int) {
   601  	if n := state.decodeUint(); n != uint64(length) {
   602  		errorf("length mismatch in ignoreArray")
   603  	}
   604  	dec.ignoreArrayHelper(state, elemOp, length)
   605  }
   606  
   607  // ignoreMap discards the data for a map value with no destination.
   608  func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) {
   609  	n := int(state.decodeUint())
   610  	keyInstr := &decInstr{keyOp, 0, nil, errors.New("no error")}
   611  	elemInstr := &decInstr{elemOp, 0, nil, errors.New("no error")}
   612  	for i := 0; i < n; i++ {
   613  		keyOp(keyInstr, state, noValue)
   614  		elemOp(elemInstr, state, noValue)
   615  	}
   616  }
   617  
   618  // decodeSlice decodes a slice and stores it in value.
   619  // Slices are encoded as an unsigned length followed by the elements.
   620  func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp decOp, ovfl error, helper decHelper) {
   621  	u := state.decodeUint()
   622  	typ := value.Type()
   623  	size := uint64(typ.Elem().Size())
   624  	nBytes := u * size
   625  	n := int(u)
   626  	// Take care with overflow in this calculation.
   627  	if n < 0 || uint64(n) != u || nBytes > tooBig || (size > 0 && nBytes/size != u) {
   628  		// We don't check n against buffer length here because if it's a slice
   629  		// of interfaces, there will be buffer reloads.
   630  		errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size)
   631  	}
   632  	if value.Cap() < n {
   633  		safe := saferio.SliceCap(reflect.Zero(reflect.PtrTo(typ.Elem())).Interface(), uint64(n))
   634  		if safe < 0 {
   635  			errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size)
   636  		}
   637  		value.Set(reflect.MakeSlice(typ, safe, safe))
   638  	} else {
   639  		value.SetLen(n)
   640  	}
   641  	dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper)
   642  }
   643  
   644  // ignoreSlice skips over the data for a slice value with no destination.
   645  func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) {
   646  	dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint()))
   647  }
   648  
   649  // decodeInterface decodes an interface value and stores it in value.
   650  // Interfaces are encoded as the name of a concrete type followed by a value.
   651  // If the name is empty, the value is nil and no value is sent.
   652  func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, value reflect.Value) {
   653  	// Read the name of the concrete type.
   654  	nr := state.decodeUint()
   655  	if nr > 1<<31 { // zero is permissible for anonymous types
   656  		errorf("invalid type name length %d", nr)
   657  	}
   658  	if nr > uint64(state.b.Len()) {
   659  		errorf("invalid type name length %d: exceeds input size", nr)
   660  	}
   661  	n := int(nr)
   662  	name := state.b.Bytes()[:n]
   663  	state.b.Drop(n)
   664  	// Allocate the destination interface value.
   665  	if len(name) == 0 {
   666  		// Copy the nil interface value to the target.
   667  		value.Set(reflect.Zero(value.Type()))
   668  		return
   669  	}
   670  	if len(name) > 1024 {
   671  		errorf("name too long (%d bytes): %.20q...", len(name), name)
   672  	}
   673  	// The concrete type must be registered.
   674  	typi, ok := nameToConcreteType.Load(string(name))
   675  	if !ok {
   676  		errorf("name not registered for interface: %q", name)
   677  	}
   678  	typ := typi.(reflect.Type)
   679  
   680  	// Read the type id of the concrete value.
   681  	concreteId := dec.decodeTypeSequence(true)
   682  	if concreteId < 0 {
   683  		error_(dec.err)
   684  	}
   685  	// Byte count of value is next; we don't care what it is (it's there
   686  	// in case we want to ignore the value by skipping it completely).
   687  	state.decodeUint()
   688  	// Read the concrete value.
   689  	v := allocValue(typ)
   690  	dec.decodeValue(concreteId, v)
   691  	if dec.err != nil {
   692  		error_(dec.err)
   693  	}
   694  	// Assign the concrete value to the interface.
   695  	// Tread carefully; it might not satisfy the interface.
   696  	if !typ.AssignableTo(ityp) {
   697  		errorf("%s is not assignable to type %s", typ, ityp)
   698  	}
   699  	// Copy the interface value to the target.
   700  	value.Set(v)
   701  }
   702  
   703  // ignoreInterface discards the data for an interface value with no destination.
   704  func (dec *Decoder) ignoreInterface(state *decoderState) {
   705  	// Read the name of the concrete type.
   706  	n, ok := state.getLength()
   707  	if !ok {
   708  		errorf("bad interface encoding: name too large for buffer")
   709  	}
   710  	bn := state.b.Len()
   711  	if bn < n {
   712  		errorf("invalid interface value length %d: exceeds input size %d", n, bn)
   713  	}
   714  	state.b.Drop(n)
   715  	id := dec.decodeTypeSequence(true)
   716  	if id < 0 {
   717  		error_(dec.err)
   718  	}
   719  	// At this point, the decoder buffer contains a delimited value. Just toss it.
   720  	n, ok = state.getLength()
   721  	if !ok {
   722  		errorf("bad interface encoding: data length too large for buffer")
   723  	}
   724  	state.b.Drop(n)
   725  }
   726  
   727  // decodeGobDecoder decodes something implementing the GobDecoder interface.
   728  // The data is encoded as a byte slice.
   729  func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, value reflect.Value) {
   730  	// Read the bytes for the value.
   731  	n, ok := state.getLength()
   732  	if !ok {
   733  		errorf("GobDecoder: length too large for buffer")
   734  	}
   735  	b := state.b.Bytes()
   736  	if len(b) < n {
   737  		errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, len(b))
   738  	}
   739  	b = b[:n]
   740  	state.b.Drop(n)
   741  	var err error
   742  	// We know it's one of these.
   743  	switch ut.externalDec {
   744  	case xGob:
   745  		err = value.Interface().(GobDecoder).GobDecode(b)
   746  	case xBinary:
   747  		err = value.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b)
   748  	case xText:
   749  		err = value.Interface().(encoding.TextUnmarshaler).UnmarshalText(b)
   750  	}
   751  	if err != nil {
   752  		error_(err)
   753  	}
   754  }
   755  
   756  // ignoreGobDecoder discards the data for a GobDecoder value with no destination.
   757  func (dec *Decoder) ignoreGobDecoder(state *decoderState) {
   758  	// Read the bytes for the value.
   759  	n, ok := state.getLength()
   760  	if !ok {
   761  		errorf("GobDecoder: length too large for buffer")
   762  	}
   763  	bn := state.b.Len()
   764  	if bn < n {
   765  		errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, bn)
   766  	}
   767  	state.b.Drop(n)
   768  }
   769  
   770  // Index by Go types.
   771  var decOpTable = [...]decOp{
   772  	reflect.Bool:       decBool,
   773  	reflect.Int8:       decInt8,
   774  	reflect.Int16:      decInt16,
   775  	reflect.Int32:      decInt32,
   776  	reflect.Int64:      decInt64,
   777  	reflect.Uint8:      decUint8,
   778  	reflect.Uint16:     decUint16,
   779  	reflect.Uint32:     decUint32,
   780  	reflect.Uint64:     decUint64,
   781  	reflect.Float32:    decFloat32,
   782  	reflect.Float64:    decFloat64,
   783  	reflect.Complex64:  decComplex64,
   784  	reflect.Complex128: decComplex128,
   785  	reflect.String:     decString,
   786  }
   787  
   788  // Indexed by gob types.  tComplex will be added during type.init().
   789  var decIgnoreOpMap = map[typeId]decOp{
   790  	tBool:    ignoreUint,
   791  	tInt:     ignoreUint,
   792  	tUint:    ignoreUint,
   793  	tFloat:   ignoreUint,
   794  	tBytes:   ignoreUint8Array,
   795  	tString:  ignoreUint8Array,
   796  	tComplex: ignoreTwoUints,
   797  }
   798  
   799  // decOpFor returns the decoding op for the base type under rt and
   800  // the indirection count to reach it.
   801  func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) *decOp {
   802  	ut := userType(rt)
   803  	// If the type implements GobEncoder, we handle it without further processing.
   804  	if ut.externalDec != 0 {
   805  		return dec.gobDecodeOpFor(ut)
   806  	}
   807  
   808  	// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
   809  	// Return the pointer to the op we're already building.
   810  	if opPtr := inProgress[rt]; opPtr != nil {
   811  		return opPtr
   812  	}
   813  	typ := ut.base
   814  	var op decOp
   815  	k := typ.Kind()
   816  	if int(k) < len(decOpTable) {
   817  		op = decOpTable[k]
   818  	}
   819  	if op == nil {
   820  		inProgress[rt] = &op
   821  		// Special cases
   822  		switch t := typ; t.Kind() {
   823  		case reflect.Array:
   824  			name = "element of " + name
   825  			elemId := dec.wireType[wireId].ArrayT.Elem
   826  			elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress)
   827  			ovfl := overflow(name)
   828  			helper := decArrayHelper[t.Elem().Kind()]
   829  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   830  				state.dec.decodeArray(state, value, *elemOp, t.Len(), ovfl, helper)
   831  			}
   832  
   833  		case reflect.Map:
   834  			keyId := dec.wireType[wireId].MapT.Key
   835  			elemId := dec.wireType[wireId].MapT.Elem
   836  			keyOp := dec.decOpFor(keyId, t.Key(), "key of "+name, inProgress)
   837  			elemOp := dec.decOpFor(elemId, t.Elem(), "element of "+name, inProgress)
   838  			ovfl := overflow(name)
   839  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   840  				state.dec.decodeMap(t, state, value, *keyOp, *elemOp, ovfl)
   841  			}
   842  
   843  		case reflect.Slice:
   844  			name = "element of " + name
   845  			if t.Elem().Kind() == reflect.Uint8 {
   846  				op = decUint8Slice
   847  				break
   848  			}
   849  			var elemId typeId
   850  			if tt, ok := builtinIdToType[wireId]; ok {
   851  				elemId = tt.(*sliceType).Elem
   852  			} else {
   853  				elemId = dec.wireType[wireId].SliceT.Elem
   854  			}
   855  			elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress)
   856  			ovfl := overflow(name)
   857  			helper := decSliceHelper[t.Elem().Kind()]
   858  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   859  				state.dec.decodeSlice(state, value, *elemOp, ovfl, helper)
   860  			}
   861  
   862  		case reflect.Struct:
   863  			// Generate a closure that calls out to the engine for the nested type.
   864  			ut := userType(typ)
   865  			enginePtr, err := dec.getDecEnginePtr(wireId, ut)
   866  			if err != nil {
   867  				error_(err)
   868  			}
   869  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   870  				// indirect through enginePtr to delay evaluation for recursive structs.
   871  				dec.decodeStruct(*enginePtr, value)
   872  			}
   873  		case reflect.Interface:
   874  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   875  				state.dec.decodeInterface(t, state, value)
   876  			}
   877  		}
   878  	}
   879  	if op == nil {
   880  		errorf("decode can't handle type %s", rt)
   881  	}
   882  	return &op
   883  }
   884  
   885  var maxIgnoreNestingDepth = 10000
   886  
   887  // decIgnoreOpFor returns the decoding op for a field that has no destination.
   888  func (dec *Decoder) decIgnoreOpFor(wireId typeId, inProgress map[typeId]*decOp, depth int) *decOp {
   889  	if depth > maxIgnoreNestingDepth {
   890  		error_(errors.New("invalid nesting depth"))
   891  	}
   892  	// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
   893  	// Return the pointer to the op we're already building.
   894  	if opPtr := inProgress[wireId]; opPtr != nil {
   895  		return opPtr
   896  	}
   897  	op, ok := decIgnoreOpMap[wireId]
   898  	if !ok {
   899  		inProgress[wireId] = &op
   900  		if wireId == tInterface {
   901  			// Special case because it's a method: the ignored item might
   902  			// define types and we need to record their state in the decoder.
   903  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   904  				state.dec.ignoreInterface(state)
   905  			}
   906  			return &op
   907  		}
   908  		// Special cases
   909  		wire := dec.wireType[wireId]
   910  		switch {
   911  		case wire == nil:
   912  			errorf("bad data: undefined type %s", wireId.string())
   913  		case wire.ArrayT != nil:
   914  			elemId := wire.ArrayT.Elem
   915  			elemOp := dec.decIgnoreOpFor(elemId, inProgress, depth+1)
   916  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   917  				state.dec.ignoreArray(state, *elemOp, wire.ArrayT.Len)
   918  			}
   919  
   920  		case wire.MapT != nil:
   921  			keyId := dec.wireType[wireId].MapT.Key
   922  			elemId := dec.wireType[wireId].MapT.Elem
   923  			keyOp := dec.decIgnoreOpFor(keyId, inProgress, depth+1)
   924  			elemOp := dec.decIgnoreOpFor(elemId, inProgress, depth+1)
   925  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   926  				state.dec.ignoreMap(state, *keyOp, *elemOp)
   927  			}
   928  
   929  		case wire.SliceT != nil:
   930  			elemId := wire.SliceT.Elem
   931  			elemOp := dec.decIgnoreOpFor(elemId, inProgress, depth+1)
   932  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   933  				state.dec.ignoreSlice(state, *elemOp)
   934  			}
   935  
   936  		case wire.StructT != nil:
   937  			// Generate a closure that calls out to the engine for the nested type.
   938  			enginePtr, err := dec.getIgnoreEnginePtr(wireId)
   939  			if err != nil {
   940  				error_(err)
   941  			}
   942  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   943  				// indirect through enginePtr to delay evaluation for recursive structs
   944  				state.dec.ignoreStruct(*enginePtr)
   945  			}
   946  
   947  		case wire.GobEncoderT != nil, wire.BinaryMarshalerT != nil, wire.TextMarshalerT != nil:
   948  			op = func(i *decInstr, state *decoderState, value reflect.Value) {
   949  				state.dec.ignoreGobDecoder(state)
   950  			}
   951  		}
   952  	}
   953  	if op == nil {
   954  		errorf("bad data: ignore can't handle type %s", wireId.string())
   955  	}
   956  	return &op
   957  }
   958  
   959  // gobDecodeOpFor returns the op for a type that is known to implement
   960  // GobDecoder.
   961  func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp {
   962  	rcvrType := ut.user
   963  	if ut.decIndir == -1 {
   964  		rcvrType = reflect.PointerTo(rcvrType)
   965  	} else if ut.decIndir > 0 {
   966  		for i := int8(0); i < ut.decIndir; i++ {
   967  			rcvrType = rcvrType.Elem()
   968  		}
   969  	}
   970  	var op decOp
   971  	op = func(i *decInstr, state *decoderState, value reflect.Value) {
   972  		// We now have the base type. We need its address if the receiver is a pointer.
   973  		if value.Kind() != reflect.Pointer && rcvrType.Kind() == reflect.Pointer {
   974  			value = value.Addr()
   975  		}
   976  		state.dec.decodeGobDecoder(ut, state, value)
   977  	}
   978  	return &op
   979  }
   980  
   981  // compatibleType asks: Are these two gob Types compatible?
   982  // Answers the question for basic types, arrays, maps and slices, plus
   983  // GobEncoder/Decoder pairs.
   984  // Structs are considered ok; fields will be checked later.
   985  func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool {
   986  	if rhs, ok := inProgress[fr]; ok {
   987  		return rhs == fw
   988  	}
   989  	inProgress[fr] = fw
   990  	ut := userType(fr)
   991  	wire, ok := dec.wireType[fw]
   992  	// If wire was encoded with an encoding method, fr must have that method.
   993  	// And if not, it must not.
   994  	// At most one of the booleans in ut is set.
   995  	// We could possibly relax this constraint in the future in order to
   996  	// choose the decoding method using the data in the wireType.
   997  	// The parentheses look odd but are correct.
   998  	if (ut.externalDec == xGob) != (ok && wire.GobEncoderT != nil) ||
   999  		(ut.externalDec == xBinary) != (ok && wire.BinaryMarshalerT != nil) ||
  1000  		(ut.externalDec == xText) != (ok && wire.TextMarshalerT != nil) {
  1001  		return false
  1002  	}
  1003  	if ut.externalDec != 0 { // This test trumps all others.
  1004  		return true
  1005  	}
  1006  	switch t := ut.base; t.Kind() {
  1007  	default:
  1008  		// chan, etc: cannot handle.
  1009  		return false
  1010  	case reflect.Bool:
  1011  		return fw == tBool
  1012  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1013  		return fw == tInt
  1014  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1015  		return fw == tUint
  1016  	case reflect.Float32, reflect.Float64:
  1017  		return fw == tFloat
  1018  	case reflect.Complex64, reflect.Complex128:
  1019  		return fw == tComplex
  1020  	case reflect.String:
  1021  		return fw == tString
  1022  	case reflect.Interface:
  1023  		return fw == tInterface
  1024  	case reflect.Array:
  1025  		if !ok || wire.ArrayT == nil {
  1026  			return false
  1027  		}
  1028  		array := wire.ArrayT
  1029  		return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress)
  1030  	case reflect.Map:
  1031  		if !ok || wire.MapT == nil {
  1032  			return false
  1033  		}
  1034  		MapType := wire.MapT
  1035  		return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress)
  1036  	case reflect.Slice:
  1037  		// Is it an array of bytes?
  1038  		if t.Elem().Kind() == reflect.Uint8 {
  1039  			return fw == tBytes
  1040  		}
  1041  		// Extract and compare element types.
  1042  		var sw *sliceType
  1043  		if tt, ok := builtinIdToType[fw]; ok {
  1044  			sw, _ = tt.(*sliceType)
  1045  		} else if wire != nil {
  1046  			sw = wire.SliceT
  1047  		}
  1048  		elem := userType(t.Elem()).base
  1049  		return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress)
  1050  	case reflect.Struct:
  1051  		return true
  1052  	}
  1053  }
  1054  
  1055  // typeString returns a human-readable description of the type identified by remoteId.
  1056  func (dec *Decoder) typeString(remoteId typeId) string {
  1057  	typeLock.Lock()
  1058  	defer typeLock.Unlock()
  1059  	if t := idToType[remoteId]; t != nil {
  1060  		// globally known type.
  1061  		return t.string()
  1062  	}
  1063  	return dec.wireType[remoteId].string()
  1064  }
  1065  
  1066  // compileSingle compiles the decoder engine for a non-struct top-level value, including
  1067  // GobDecoders.
  1068  func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
  1069  	rt := ut.user
  1070  	engine = new(decEngine)
  1071  	engine.instr = make([]decInstr, 1) // one item
  1072  	name := rt.String()                // best we can do
  1073  	if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) {
  1074  		remoteType := dec.typeString(remoteId)
  1075  		// Common confusing case: local interface type, remote concrete type.
  1076  		if ut.base.Kind() == reflect.Interface && remoteId != tInterface {
  1077  			return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType)
  1078  		}
  1079  		return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType)
  1080  	}
  1081  	op := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp))
  1082  	ovfl := errors.New(`value for "` + name + `" out of range`)
  1083  	engine.instr[singletonField] = decInstr{*op, singletonField, nil, ovfl}
  1084  	engine.numInstr = 1
  1085  	return
  1086  }
  1087  
  1088  // compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
  1089  func (dec *Decoder) compileIgnoreSingle(remoteId typeId) *decEngine {
  1090  	engine := new(decEngine)
  1091  	engine.instr = make([]decInstr, 1) // one item
  1092  	op := dec.decIgnoreOpFor(remoteId, make(map[typeId]*decOp), 0)
  1093  	ovfl := overflow(dec.typeString(remoteId))
  1094  	engine.instr[0] = decInstr{*op, 0, nil, ovfl}
  1095  	engine.numInstr = 1
  1096  	return engine
  1097  }
  1098  
  1099  // compileDec compiles the decoder engine for a value. If the value is not a struct,
  1100  // it calls out to compileSingle.
  1101  func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
  1102  	defer catchError(&err)
  1103  	rt := ut.base
  1104  	srt := rt
  1105  	if srt.Kind() != reflect.Struct || ut.externalDec != 0 {
  1106  		return dec.compileSingle(remoteId, ut)
  1107  	}
  1108  	var wireStruct *structType
  1109  	// Builtin types can come from global pool; the rest must be defined by the decoder.
  1110  	// Also we know we're decoding a struct now, so the client must have sent one.
  1111  	if t, ok := builtinIdToType[remoteId]; ok {
  1112  		wireStruct, _ = t.(*structType)
  1113  	} else {
  1114  		wire := dec.wireType[remoteId]
  1115  		if wire == nil {
  1116  			error_(errBadType)
  1117  		}
  1118  		wireStruct = wire.StructT
  1119  	}
  1120  	if wireStruct == nil {
  1121  		errorf("type mismatch in decoder: want struct type %s; got non-struct", rt)
  1122  	}
  1123  	engine = new(decEngine)
  1124  	engine.instr = make([]decInstr, len(wireStruct.Field))
  1125  	seen := make(map[reflect.Type]*decOp)
  1126  	// Loop over the fields of the wire type.
  1127  	for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ {
  1128  		wireField := wireStruct.Field[fieldnum]
  1129  		if wireField.Name == "" {
  1130  			errorf("empty name for remote field of type %s", wireStruct.Name)
  1131  		}
  1132  		ovfl := overflow(wireField.Name)
  1133  		// Find the field of the local type with the same name.
  1134  		localField, present := srt.FieldByName(wireField.Name)
  1135  		// TODO(r): anonymous names
  1136  		if !present || !isExported(wireField.Name) {
  1137  			op := dec.decIgnoreOpFor(wireField.Id, make(map[typeId]*decOp), 0)
  1138  			engine.instr[fieldnum] = decInstr{*op, fieldnum, nil, ovfl}
  1139  			continue
  1140  		}
  1141  		if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) {
  1142  			errorf("wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name)
  1143  		}
  1144  		op := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen)
  1145  		engine.instr[fieldnum] = decInstr{*op, fieldnum, localField.Index, ovfl}
  1146  		engine.numInstr++
  1147  	}
  1148  	return
  1149  }
  1150  
  1151  // getDecEnginePtr returns the engine for the specified type.
  1152  func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err error) {
  1153  	rt := ut.user
  1154  	decoderMap, ok := dec.decoderCache[rt]
  1155  	if !ok {
  1156  		decoderMap = make(map[typeId]**decEngine)
  1157  		dec.decoderCache[rt] = decoderMap
  1158  	}
  1159  	if enginePtr, ok = decoderMap[remoteId]; !ok {
  1160  		// To handle recursive types, mark this engine as underway before compiling.
  1161  		enginePtr = new(*decEngine)
  1162  		decoderMap[remoteId] = enginePtr
  1163  		*enginePtr, err = dec.compileDec(remoteId, ut)
  1164  		if err != nil {
  1165  			delete(decoderMap, remoteId)
  1166  		}
  1167  	}
  1168  	return
  1169  }
  1170  
  1171  // emptyStruct is the type we compile into when ignoring a struct value.
  1172  type emptyStruct struct{}
  1173  
  1174  var emptyStructType = reflect.TypeOf(emptyStruct{})
  1175  
  1176  // getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded.
  1177  func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) {
  1178  	var ok bool
  1179  	if enginePtr, ok = dec.ignorerCache[wireId]; !ok {
  1180  		// To handle recursive types, mark this engine as underway before compiling.
  1181  		enginePtr = new(*decEngine)
  1182  		dec.ignorerCache[wireId] = enginePtr
  1183  		wire := dec.wireType[wireId]
  1184  		if wire != nil && wire.StructT != nil {
  1185  			*enginePtr, err = dec.compileDec(wireId, userType(emptyStructType))
  1186  		} else {
  1187  			*enginePtr = dec.compileIgnoreSingle(wireId)
  1188  		}
  1189  		if err != nil {
  1190  			delete(dec.ignorerCache, wireId)
  1191  		}
  1192  	}
  1193  	return
  1194  }
  1195  
  1196  // decodeValue decodes the data stream representing a value and stores it in value.
  1197  func (dec *Decoder) decodeValue(wireId typeId, value reflect.Value) {
  1198  	defer catchError(&dec.err)
  1199  	// If the value is nil, it means we should just ignore this item.
  1200  	if !value.IsValid() {
  1201  		dec.decodeIgnoredValue(wireId)
  1202  		return
  1203  	}
  1204  	// Dereference down to the underlying type.
  1205  	ut := userType(value.Type())
  1206  	base := ut.base
  1207  	var enginePtr **decEngine
  1208  	enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut)
  1209  	if dec.err != nil {
  1210  		return
  1211  	}
  1212  	value = decAlloc(value)
  1213  	engine := *enginePtr
  1214  	if st := base; st.Kind() == reflect.Struct && ut.externalDec == 0 {
  1215  		wt := dec.wireType[wireId]
  1216  		if engine.numInstr == 0 && st.NumField() > 0 &&
  1217  			wt != nil && len(wt.StructT.Field) > 0 {
  1218  			name := base.Name()
  1219  			errorf("type mismatch: no fields matched compiling decoder for %s", name)
  1220  		}
  1221  		dec.decodeStruct(engine, value)
  1222  	} else {
  1223  		dec.decodeSingle(engine, value)
  1224  	}
  1225  }
  1226  
  1227  // decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it.
  1228  func (dec *Decoder) decodeIgnoredValue(wireId typeId) {
  1229  	var enginePtr **decEngine
  1230  	enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId)
  1231  	if dec.err != nil {
  1232  		return
  1233  	}
  1234  	wire := dec.wireType[wireId]
  1235  	if wire != nil && wire.StructT != nil {
  1236  		dec.ignoreStruct(*enginePtr)
  1237  	} else {
  1238  		dec.ignoreSingle(*enginePtr)
  1239  	}
  1240  }
  1241  
  1242  const (
  1243  	intBits     = 32 << (^uint(0) >> 63)
  1244  	uintptrBits = 32 << (^uintptr(0) >> 63)
  1245  )
  1246  
  1247  func init() {
  1248  	var iop, uop decOp
  1249  	switch intBits {
  1250  	case 32:
  1251  		iop = decInt32
  1252  		uop = decUint32
  1253  	case 64:
  1254  		iop = decInt64
  1255  		uop = decUint64
  1256  	default:
  1257  		panic("gob: unknown size of int/uint")
  1258  	}
  1259  	decOpTable[reflect.Int] = iop
  1260  	decOpTable[reflect.Uint] = uop
  1261  
  1262  	// Finally uintptr
  1263  	switch uintptrBits {
  1264  	case 32:
  1265  		uop = decUint32
  1266  	case 64:
  1267  		uop = decUint64
  1268  	default:
  1269  		panic("gob: unknown size of uintptr")
  1270  	}
  1271  	decOpTable[reflect.Uintptr] = uop
  1272  }
  1273  
  1274  // Gob depends on being able to take the address
  1275  // of zeroed Values it creates, so use this wrapper instead
  1276  // of the standard reflect.Zero.
  1277  // Each call allocates once.
  1278  func allocValue(t reflect.Type) reflect.Value {
  1279  	return reflect.New(t).Elem()
  1280  }