github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/encoding/gob/decoder.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  package gob
     6  
     7  import (
     8  	"bufio"
     9  	"errors"
    10  	"io"
    11  	"reflect"
    12  	"sync"
    13  )
    14  
    15  // tooBig provides a sanity check for sizes; used in several places.
    16  // Upper limit of 1GB, allowing room to grow a little without overflow.
    17  // TODO: make this adjustable?
    18  const tooBig = 1 << 30
    19  
    20  // A Decoder manages the receipt of type and data information read from the
    21  // remote side of a connection.
    22  //
    23  // The Decoder does only basic sanity checking on decoded input sizes,
    24  // and its limits are not configurable. Take caution when decoding gob data
    25  // from untrusted sources.
    26  type Decoder struct {
    27  	mutex        sync.Mutex                              // each item must be received atomically
    28  	r            io.Reader                               // source of the data
    29  	buf          decBuffer                               // buffer for more efficient i/o from r
    30  	wireType     map[typeId]*wireType                    // map from remote ID to local description
    31  	decoderCache map[reflect.Type]map[typeId]**decEngine // cache of compiled engines
    32  	ignorerCache map[typeId]**decEngine                  // ditto for ignored objects
    33  	freeList     *decoderState                           // list of free decoderStates; avoids reallocation
    34  	countBuf     []byte                                  // used for decoding integers while parsing messages
    35  	err          error
    36  }
    37  
    38  // NewDecoder returns a new decoder that reads from the io.Reader.
    39  // If r does not also implement io.ByteReader, it will be wrapped in a
    40  // bufio.Reader.
    41  func NewDecoder(r io.Reader) *Decoder {
    42  	dec := new(Decoder)
    43  	// We use the ability to read bytes as a plausible surrogate for buffering.
    44  	if _, ok := r.(io.ByteReader); !ok {
    45  		r = bufio.NewReader(r)
    46  	}
    47  	dec.r = r
    48  	dec.wireType = make(map[typeId]*wireType)
    49  	dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
    50  	dec.ignorerCache = make(map[typeId]**decEngine)
    51  	dec.countBuf = make([]byte, 9) // counts may be uint64s (unlikely!), require 9 bytes
    52  
    53  	return dec
    54  }
    55  
    56  // recvType loads the definition of a type.
    57  func (dec *Decoder) recvType(id typeId) {
    58  	// Have we already seen this type?  That's an error
    59  	if id < firstUserId || dec.wireType[id] != nil {
    60  		dec.err = errors.New("gob: duplicate type received")
    61  		return
    62  	}
    63  
    64  	// Type:
    65  	wire := new(wireType)
    66  	dec.decodeValue(tWireType, reflect.ValueOf(wire))
    67  	if dec.err != nil {
    68  		return
    69  	}
    70  	// Remember we've seen this type.
    71  	dec.wireType[id] = wire
    72  }
    73  
    74  var errBadCount = errors.New("invalid message length")
    75  
    76  // recvMessage reads the next count-delimited item from the input. It is the converse
    77  // of Encoder.writeMessage. It returns false on EOF or other error reading the message.
    78  func (dec *Decoder) recvMessage() bool {
    79  	// Read a count.
    80  	nbytes, _, err := decodeUintReader(dec.r, dec.countBuf)
    81  	if err != nil {
    82  		dec.err = err
    83  		return false
    84  	}
    85  	if nbytes >= tooBig {
    86  		dec.err = errBadCount
    87  		return false
    88  	}
    89  	dec.readMessage(int(nbytes))
    90  	return dec.err == nil
    91  }
    92  
    93  // readMessage reads the next nbytes bytes from the input.
    94  func (dec *Decoder) readMessage(nbytes int) {
    95  	if dec.buf.Len() != 0 {
    96  		// The buffer should always be empty now.
    97  		panic("non-empty decoder buffer")
    98  	}
    99  	// Read the data
   100  	dec.buf.Size(nbytes)
   101  	_, dec.err = io.ReadFull(dec.r, dec.buf.Bytes())
   102  	if dec.err != nil {
   103  		if dec.err == io.EOF {
   104  			dec.err = io.ErrUnexpectedEOF
   105  		}
   106  	}
   107  }
   108  
   109  // toInt turns an encoded uint64 into an int, according to the marshaling rules.
   110  func toInt(x uint64) int64 {
   111  	i := int64(x >> 1)
   112  	if x&1 != 0 {
   113  		i = ^i
   114  	}
   115  	return i
   116  }
   117  
   118  func (dec *Decoder) nextInt() int64 {
   119  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   120  	if err != nil {
   121  		dec.err = err
   122  	}
   123  	return toInt(n)
   124  }
   125  
   126  func (dec *Decoder) nextUint() uint64 {
   127  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   128  	if err != nil {
   129  		dec.err = err
   130  	}
   131  	return n
   132  }
   133  
   134  // decodeTypeSequence parses:
   135  // TypeSequence
   136  //	(TypeDefinition DelimitedTypeDefinition*)?
   137  // and returns the type id of the next value. It returns -1 at
   138  // EOF.  Upon return, the remainder of dec.buf is the value to be
   139  // decoded. If this is an interface value, it can be ignored by
   140  // resetting that buffer.
   141  func (dec *Decoder) decodeTypeSequence(isInterface bool) typeId {
   142  	for dec.err == nil {
   143  		if dec.buf.Len() == 0 {
   144  			if !dec.recvMessage() {
   145  				break
   146  			}
   147  		}
   148  		// Receive a type id.
   149  		id := typeId(dec.nextInt())
   150  		if id >= 0 {
   151  			// Value follows.
   152  			return id
   153  		}
   154  		// Type definition for (-id) follows.
   155  		dec.recvType(-id)
   156  		// When decoding an interface, after a type there may be a
   157  		// DelimitedValue still in the buffer. Skip its count.
   158  		// (Alternatively, the buffer is empty and the byte count
   159  		// will be absorbed by recvMessage.)
   160  		if dec.buf.Len() > 0 {
   161  			if !isInterface {
   162  				dec.err = errors.New("extra data in buffer")
   163  				break
   164  			}
   165  			dec.nextUint()
   166  		}
   167  	}
   168  	return -1
   169  }
   170  
   171  // Decode reads the next value from the input stream and stores
   172  // it in the data represented by the empty interface value.
   173  // If e is nil, the value will be discarded. Otherwise,
   174  // the value underlying e must be a pointer to the
   175  // correct type for the next data item received.
   176  // If the input is at EOF, Decode returns io.EOF and
   177  // does not modify e.
   178  func (dec *Decoder) Decode(e interface{}) error {
   179  	if e == nil {
   180  		return dec.DecodeValue(reflect.Value{})
   181  	}
   182  	value := reflect.ValueOf(e)
   183  	// If e represents a value as opposed to a pointer, the answer won't
   184  	// get back to the caller. Make sure it's a pointer.
   185  	if value.Type().Kind() != reflect.Ptr {
   186  		dec.err = errors.New("gob: attempt to decode into a non-pointer")
   187  		return dec.err
   188  	}
   189  	return dec.DecodeValue(value)
   190  }
   191  
   192  // DecodeValue reads the next value from the input stream.
   193  // If v is the zero reflect.Value (v.Kind() == Invalid), DecodeValue discards the value.
   194  // Otherwise, it stores the value into v. In that case, v must represent
   195  // a non-nil pointer to data or be an assignable reflect.Value (v.CanSet())
   196  // If the input is at EOF, DecodeValue returns io.EOF and
   197  // does not modify v.
   198  func (dec *Decoder) DecodeValue(v reflect.Value) error {
   199  	if v.IsValid() {
   200  		if v.Kind() == reflect.Ptr && !v.IsNil() {
   201  			// That's okay, we'll store through the pointer.
   202  		} else if !v.CanSet() {
   203  			return errors.New("gob: DecodeValue of unassignable value")
   204  		}
   205  	}
   206  	// Make sure we're single-threaded through here.
   207  	dec.mutex.Lock()
   208  	defer dec.mutex.Unlock()
   209  
   210  	dec.buf.Reset() // In case data lingers from previous invocation.
   211  	dec.err = nil
   212  	id := dec.decodeTypeSequence(false)
   213  	if dec.err == nil {
   214  		dec.decodeValue(id, v)
   215  	}
   216  	return dec.err
   217  }
   218  
   219  // If debug.go is compiled into the program , debugFunc prints a human-readable
   220  // representation of the gob data read from r by calling that file's Debug function.
   221  // Otherwise it is nil.
   222  var debugFunc func(io.Reader)