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