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