...
1
2
3
4
5 package gob
6
7 import (
8 "bufio"
9 "errors"
10 "io"
11 "reflect"
12 "sync"
13 )
14
15
16
17
18 const tooBig = (1 << 30) << (^uint(0) >> 62)
19
20
21
22
23
24
25
26
27 type Decoder struct {
28 mutex sync.Mutex
29 r io.Reader
30 buf decBuffer
31 wireType map[typeId]*wireType
32 decoderCache map[reflect.Type]map[typeId]**decEngine
33 ignorerCache map[typeId]**decEngine
34 freeList *decoderState
35 countBuf []byte
36 err error
37 }
38
39
40
41
42 func NewDecoder(r io.Reader) *Decoder {
43 dec := new(Decoder)
44
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)
53
54 return dec
55 }
56
57
58 func (dec *Decoder) recvType(id typeId) {
59
60 if id < firstUserId || dec.wireType[id] != nil {
61 dec.err = errors.New("gob: duplicate type received")
62 return
63 }
64
65
66 wire := new(wireType)
67 dec.decodeValue(tWireType, reflect.ValueOf(wire))
68 if dec.err != nil {
69 return
70 }
71
72 dec.wireType[id] = wire
73 }
74
75 var errBadCount = errors.New("invalid message length")
76
77
78
79 func (dec *Decoder) recvMessage() bool {
80
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
95 func (dec *Decoder) readMessage(nbytes int) {
96 if dec.buf.Len() != 0 {
97
98 panic("non-empty decoder buffer")
99 }
100
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
109 func toInt(x uint64) int64 {
110 i := int64(x >> 1)
111 if x&1 != 0 {
112 i = ^i
113 }
114 return i
115 }
116
117 func (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
125 func (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
134
135
136
137
138
139
140 func (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
148 id := typeId(dec.nextInt())
149 if id >= 0 {
150
151 return id
152 }
153
154 dec.recvType(-id)
155 if dec.err != nil {
156 break
157 }
158
159
160
161
162 if dec.buf.Len() > 0 {
163 if !isInterface {
164 dec.err = errors.New("extra data in buffer")
165 break
166 }
167 dec.nextUint()
168 }
169 }
170 return -1
171 }
172
173
174
175
176
177
178
179
180 func (dec *Decoder) Decode(e interface{}) error {
181 if e == nil {
182 return dec.DecodeValue(reflect.Value{})
183 }
184 value := reflect.ValueOf(e)
185
186
187 if value.Type().Kind() != reflect.Ptr {
188 dec.err = errors.New("gob: attempt to decode into a non-pointer")
189 return dec.err
190 }
191 return dec.DecodeValue(value)
192 }
193
194
195
196
197
198
199
200 func (dec *Decoder) DecodeValue(v reflect.Value) error {
201 if v.IsValid() {
202 if v.Kind() == reflect.Ptr && !v.IsNil() {
203
204 } else if !v.CanSet() {
205 return errors.New("gob: DecodeValue of unassignable value")
206 }
207 }
208
209 dec.mutex.Lock()
210 defer dec.mutex.Unlock()
211
212 dec.buf.Reset()
213 dec.err = nil
214 id := dec.decodeTypeSequence(false)
215 if dec.err == nil {
216 dec.decodeValue(id, v)
217 }
218 return dec.err
219 }
220
221
222
223
224 var debugFunc func(io.Reader)
225
View as plain text