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