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.
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
20 // A Decoder manages the receipt of type and data information read from the
21 // remote side of a connection.
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.
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
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
41 func NewDecoder(r io
.Reader
) *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
)
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
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")
66 dec
.decodeValue(tWireType
, reflect
.ValueOf(wire
))
70 // Remember we've seen this type.
71 dec
.wireType
[id
] = wire
74 var errBadCount
= errors
.New("invalid message length")
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 {
80 nbytes
, _
, err
:= decodeUintReader(dec
.r
, dec
.countBuf
)
89 dec
.readMessage(int(nbytes
))
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")
101 _
, dec
.err
= io
.ReadFull(dec
.r
, dec
.buf
.Bytes())
102 if dec
.err
== io
.EOF
{
103 dec
.err
= io
.ErrUnexpectedEOF
107 // toInt turns an encoded uint64 into an int, according to the marshaling rules.
108 func toInt(x
uint64) int64 {
116 func (dec
*Decoder
) nextInt() int64 {
117 n
, _
, err
:= decodeUintReader(&dec
.buf
, dec
.countBuf
)
124 func (dec
*Decoder
) nextUint() uint64 {
125 n
, _
, err
:= decodeUintReader(&dec
.buf
, dec
.countBuf
)
132 // decodeTypeSequence parses:
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
{
141 if dec
.buf
.Len() == 0 {
142 if !dec
.recvMessage() {
146 // Receive a type id.
147 id
:= typeId(dec
.nextInt())
152 // Type definition for (-id) follows.
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 {
160 dec
.err
= errors
.New("extra data in buffer")
169 // Decode reads the next value from the input stream 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 // If the input is at EOF, Decode returns io.EOF and
175 // does not modify e.
176 func (dec
*Decoder
) Decode(e
interface{}) error
{
178 return dec
.DecodeValue(reflect
.Value
{})
180 value
:= reflect
.ValueOf(e
)
181 // If e represents a value as opposed to a pointer, the answer won't
182 // get back to the caller. Make sure it's a pointer.
183 if value
.Type().Kind() != reflect
.Ptr
{
184 dec
.err
= errors
.New("gob: attempt to decode into a non-pointer")
187 return dec
.DecodeValue(value
)
190 // DecodeValue reads the next value from the input stream.
191 // If v is the zero reflect.Value (v.Kind() == Invalid), DecodeValue discards the value.
192 // Otherwise, it stores the value into v. In that case, v must represent
193 // a non-nil pointer to data or be an assignable reflect.Value (v.CanSet())
194 // If the input is at EOF, DecodeValue returns io.EOF and
195 // does not modify v.
196 func (dec
*Decoder
) DecodeValue(v reflect
.Value
) error
{
198 if v
.Kind() == reflect
.Ptr
&& !v
.IsNil() {
199 // That's okay, we'll store through the pointer.
200 } else if !v
.CanSet() {
201 return errors
.New("gob: DecodeValue of unassignable value")
204 // Make sure we're single-threaded through here.
206 defer dec
.mutex
.Unlock()
208 dec
.buf
.Reset() // In case data lingers from previous invocation.
210 id
:= dec
.decodeTypeSequence(false)
212 dec
.decodeValue(id
, v
)
217 // If debug.go is compiled into the program , debugFunc prints a human-readable
218 // representation of the gob data read from r by calling that file's Debug function.
219 // Otherwise it is nil.
220 var debugFunc
func(io
.Reader
)