2018-11-11 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / encoding / gob / decoder.go
blob5ef03888621146da0d1f397ab7464a3ffa728429
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.
5 package gob
7 import (
8 "bufio"
9 "errors"
10 "io"
11 "reflect"
12 "sync"
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.
26 type Decoder struct {
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
35 err error
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
40 // bufio.Reader.
41 func NewDecoder(r io.Reader) *Decoder {
42 dec := new(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)
47 dec.r = 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
53 return dec
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")
61 return
64 // Type:
65 wire := new(wireType)
66 dec.decodeValue(tWireType, reflect.ValueOf(wire))
67 if dec.err != nil {
68 return
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 {
79 // Read a count.
80 nbytes, _, err := decodeUintReader(dec.r, dec.countBuf)
81 if err != nil {
82 dec.err = err
83 return false
85 if nbytes >= tooBig {
86 dec.err = errBadCount
87 return false
89 dec.readMessage(int(nbytes))
90 return dec.err == nil
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")
99 // Read the data
100 dec.buf.Size(nbytes)
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 {
109 i := int64(x >> 1)
110 if x&1 != 0 {
111 i = ^i
113 return i
116 func (dec *Decoder) nextInt() int64 {
117 n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
118 if err != nil {
119 dec.err = err
121 return toInt(n)
124 func (dec *Decoder) nextUint() uint64 {
125 n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
126 if err != nil {
127 dec.err = err
129 return n
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
146 // Receive a type id.
147 id := typeId(dec.nextInt())
148 if id >= 0 {
149 // Value follows.
150 return id
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
163 dec.nextUint()
166 return -1
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 {
177 if e == nil {
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")
185 return dec.err
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 {
197 if v.IsValid() {
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.
205 dec.mutex.Lock()
206 defer dec.mutex.Unlock()
208 dec.buf.Reset() // In case data lingers from previous invocation.
209 dec.err = nil
210 id := dec.decodeTypeSequence(false)
211 if dec.err == nil {
212 dec.decodeValue(id, v)
214 return dec.err
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)