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 firstMessage := true
142 for dec.err == nil {
143 if dec.buf.Len() == 0 {
144 if !dec.recvMessage() {
145
146
147
148
149 if !firstMessage && dec.err == io.EOF {
150 dec.err = io.ErrUnexpectedEOF
151 }
152 break
153 }
154 }
155
156 id := typeId(dec.nextInt())
157 if id >= 0 {
158
159 return id
160 }
161
162 dec.recvType(-id)
163 if dec.err != nil {
164 break
165 }
166
167
168
169
170 if dec.buf.Len() > 0 {
171 if !isInterface {
172 dec.err = errors.New("extra data in buffer")
173 break
174 }
175 dec.nextUint()
176 }
177 firstMessage = false
178 }
179 return -1
180 }
181
182
183
184
185
186
187
188
189 func (dec *Decoder) Decode(e any) error {
190 if e == nil {
191 return dec.DecodeValue(reflect.Value{})
192 }
193 value := reflect.ValueOf(e)
194
195
196 if value.Type().Kind() != reflect.Pointer {
197 dec.err = errors.New("gob: attempt to decode into a non-pointer")
198 return dec.err
199 }
200 return dec.DecodeValue(value)
201 }
202
203
204
205
206
207
208
209 func (dec *Decoder) DecodeValue(v reflect.Value) error {
210 if v.IsValid() {
211 if v.Kind() == reflect.Pointer && !v.IsNil() {
212
213 } else if !v.CanSet() {
214 return errors.New("gob: DecodeValue of unassignable value")
215 }
216 }
217
218 dec.mutex.Lock()
219 defer dec.mutex.Unlock()
220
221 dec.buf.Reset()
222 dec.err = nil
223 id := dec.decodeTypeSequence(false)
224 if dec.err == nil {
225 dec.decodeValue(id, v)
226 }
227 return dec.err
228 }
229
230
231
232
233 var debugFunc func(io.Reader)
234
View as plain text