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
5package gob
6
7import (
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.
18const 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.
27type 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.
42func 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.
58func (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
75var 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.
79func (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.
95func (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.
109func toInt(x uint64) int64 {
110	i := int64(x >> 1)
111	if x&1 != 0 {
112		i = ^i
113	}
114	return i
115}
116
117func (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
125func (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.
140func (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		// When decoding an interface, after a type there may be a
156		// DelimitedValue still in the buffer. Skip its count.
157		// (Alternatively, the buffer is empty and the byte count
158		// will be absorbed by recvMessage.)
159		if dec.buf.Len() > 0 {
160			if !isInterface {
161				dec.err = errors.New("extra data in buffer")
162				break
163			}
164			dec.nextUint()
165		}
166	}
167	return -1
168}
169
170// Decode reads the next value from the input stream and stores
171// it in the data represented by the empty interface value.
172// If e is nil, the value will be discarded. Otherwise,
173// the value underlying e must be a pointer to the
174// correct type for the next data item received.
175// If the input is at EOF, Decode returns io.EOF and
176// does not modify e.
177func (dec *Decoder) Decode(e interface{}) error {
178	if e == nil {
179		return dec.DecodeValue(reflect.Value{})
180	}
181	value := reflect.ValueOf(e)
182	// If e represents a value as opposed to a pointer, the answer won't
183	// get back to the caller. Make sure it's a pointer.
184	if value.Type().Kind() != reflect.Ptr {
185		dec.err = errors.New("gob: attempt to decode into a non-pointer")
186		return dec.err
187	}
188	return dec.DecodeValue(value)
189}
190
191// DecodeValue reads the next value from the input stream.
192// If v is the zero reflect.Value (v.Kind() == Invalid), DecodeValue discards the value.
193// Otherwise, it stores the value into v. In that case, v must represent
194// a non-nil pointer to data or be an assignable reflect.Value (v.CanSet())
195// If the input is at EOF, DecodeValue returns io.EOF and
196// does not modify v.
197func (dec *Decoder) DecodeValue(v reflect.Value) error {
198	if v.IsValid() {
199		if v.Kind() == reflect.Ptr && !v.IsNil() {
200			// That's okay, we'll store through the pointer.
201		} else if !v.CanSet() {
202			return errors.New("gob: DecodeValue of unassignable value")
203		}
204	}
205	// Make sure we're single-threaded through here.
206	dec.mutex.Lock()
207	defer dec.mutex.Unlock()
208
209	dec.buf.Reset() // In case data lingers from previous invocation.
210	dec.err = nil
211	id := dec.decodeTypeSequence(false)
212	if dec.err == nil {
213		dec.decodeValue(id, v)
214	}
215	return dec.err
216}
217
218// If debug.go is compiled into the program , debugFunc prints a human-readable
219// representation of the gob data read from r by calling that file's Debug function.
220// Otherwise it is nil.
221var debugFunc func(io.Reader)
222