1
2
3
4
5 package gob
6
7 import (
8 "bufio"
9 "errors"
10 "internal/saferio"
11 "io"
12 "reflect"
13 "sync"
14 )
15
16
17
18
19 const tooBig = (1 << 30) << (^uint(0) >> 62)
20
21
22
23
24
25
26
27
28 type Decoder struct {
29 mutex sync.Mutex
30 r io.Reader
31 buf decBuffer
32 wireType map[typeId]*wireType
33 decoderCache map[reflect.Type]map[typeId]**decEngine
34 ignorerCache map[typeId]**decEngine
35 freeList *decoderState
36 countBuf []byte
37 err error
38
39 ignoreDepth int
40 }
41
42
43
44
45 func NewDecoder(r io.Reader) *Decoder {
46 dec := new(Decoder)
47
48 if _, ok := r.(io.ByteReader); !ok {
49 r = bufio.NewReader(r)
50 }
51 dec.r = r
52 dec.wireType = make(map[typeId]*wireType)
53 dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
54 dec.ignorerCache = make(map[typeId]**decEngine)
55 dec.countBuf = make([]byte, 9)
56
57 return dec
58 }
59
60
61 func (dec *Decoder) recvType(id typeId) {
62
63 if id < firstUserId || dec.wireType[id] != nil {
64 dec.err = errors.New("gob: duplicate type received")
65 return
66 }
67
68
69 wire := new(wireType)
70 dec.decodeValue(tWireType, reflect.ValueOf(wire))
71 if dec.err != nil {
72 return
73 }
74
75 dec.wireType[id] = wire
76 }
77
78 var errBadCount = errors.New("invalid message length")
79
80
81
82 func (dec *Decoder) recvMessage() bool {
83
84 nbytes, _, err := decodeUintReader(dec.r, dec.countBuf)
85 if err != nil {
86 dec.err = err
87 return false
88 }
89 if nbytes >= tooBig {
90 dec.err = errBadCount
91 return false
92 }
93 dec.readMessage(int(nbytes))
94 return dec.err == nil
95 }
96
97
98 func (dec *Decoder) readMessage(nbytes int) {
99 if dec.buf.Len() != 0 {
100
101 panic("non-empty decoder buffer")
102 }
103
104 var buf []byte
105 buf, dec.err = saferio.ReadData(dec.r, uint64(nbytes))
106 dec.buf.SetBytes(buf)
107 if dec.err == io.EOF {
108 dec.err = io.ErrUnexpectedEOF
109 }
110 }
111
112
113 func toInt(x uint64) int64 {
114 i := int64(x >> 1)
115 if x&1 != 0 {
116 i = ^i
117 }
118 return i
119 }
120
121 func (dec *Decoder) nextInt() int64 {
122 n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
123 if err != nil {
124 dec.err = err
125 }
126 return toInt(n)
127 }
128
129 func (dec *Decoder) nextUint() uint64 {
130 n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
131 if err != nil {
132 dec.err = err
133 }
134 return n
135 }
136
137
138
139
140
141
142
143
144
145
146 func (dec *Decoder) decodeTypeSequence(isInterface bool) typeId {
147 firstMessage := true
148 for dec.err == nil {
149 if dec.buf.Len() == 0 {
150 if !dec.recvMessage() {
151
152
153
154
155 if !firstMessage && dec.err == io.EOF {
156 dec.err = io.ErrUnexpectedEOF
157 }
158 break
159 }
160 }
161
162 id := typeId(dec.nextInt())
163 if id >= 0 {
164
165 return id
166 }
167
168 dec.recvType(-id)
169 if dec.err != nil {
170 break
171 }
172
173
174
175
176 if dec.buf.Len() > 0 {
177 if !isInterface {
178 dec.err = errors.New("extra data in buffer")
179 break
180 }
181 dec.nextUint()
182 }
183 firstMessage = false
184 }
185 return -1
186 }
187
188
189
190
191
192
193
194
195 func (dec *Decoder) Decode(e any) error {
196 if e == nil {
197 return dec.DecodeValue(reflect.Value{})
198 }
199 value := reflect.ValueOf(e)
200
201
202 if value.Type().Kind() != reflect.Pointer {
203 dec.err = errors.New("gob: attempt to decode into a non-pointer")
204 return dec.err
205 }
206 return dec.DecodeValue(value)
207 }
208
209
210
211
212
213
214
215 func (dec *Decoder) DecodeValue(v reflect.Value) error {
216 if v.IsValid() {
217 if v.Kind() == reflect.Pointer && !v.IsNil() {
218
219 } else if !v.CanSet() {
220 return errors.New("gob: DecodeValue of unassignable value")
221 }
222 }
223
224 dec.mutex.Lock()
225 defer dec.mutex.Unlock()
226
227 dec.buf.Reset()
228 dec.err = nil
229 id := dec.decodeTypeSequence(false)
230 if dec.err == nil {
231 dec.decodeValue(id, v)
232 }
233 return dec.err
234 }
235
236
237
238
239 var debugFunc func(io.Reader)
240
View as plain text