...

Source file src/encoding/gob/decoder.go

Documentation: encoding/gob

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

View as plain text