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