forked from msgpack/msgpack
/
unpack.go
365 lines (349 loc) · 9.1 KB
/
unpack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package msgpack
import (
"io"
"reflect"
"strconv"
"unsafe"
)
func readByte(reader io.Reader) (v uint8, err error) {
data := [1]byte{}
_, e := reader.Read(data[0:])
if e != nil {
return 0, e
}
return data[0], nil
}
func readUint16(reader io.Reader) (v uint16, n int, err error) {
data := [2]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint16(data[0]) << 8) | uint16(data[1]), n, nil
}
func readUint32(reader io.Reader) (v uint32, n int, err error) {
data := [4]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint32(data[0]) << 24) | (uint32(data[1]) << 16) | (uint32(data[2]) << 8) | uint32(data[3]), n, nil
}
func readUint64(reader io.Reader) (v uint64, n int, err error) {
data := [8]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint64(data[0]) << 56) | (uint64(data[1]) << 48) | (uint64(data[2]) << 40) | (uint64(data[3]) << 32) | (uint64(data[4]) << 24) | (uint64(data[5]) << 16) | (uint64(data[6]) << 8) | uint64(data[7]), n, nil
}
func readInt16(reader io.Reader) (v int16, n int, err error) {
data := [2]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (int16(data[0]) << 8) | int16(data[1]), n, nil
}
func readInt32(reader io.Reader) (v int32, n int, err error) {
data := [4]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (int32(data[0]) << 24) | (int32(data[1]) << 16) | (int32(data[2]) << 8) | int32(data[3]), n, nil
}
func readInt64(reader io.Reader) (v int64, n int, err error) {
data := [8]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (int64(data[0]) << 56) | (int64(data[1]) << 48) | (int64(data[2]) << 40) | (int64(data[3]) << 32) | (int64(data[4]) << 24) | (int64(data[5]) << 16) | (int64(data[6]) << 8) | int64(data[7]), n, nil
}
func unpackArray(reader io.Reader, nelems uint) (v reflect.Value, n int, err error) {
retval := make([]interface{}, nelems)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
v, n, e := Unpack(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[i] = v.Interface()
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpackArrayReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err error) {
retval := make([]reflect.Value, nelems)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
v, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[i] = v
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpackMap(reader io.Reader, nelems uint) (v reflect.Value, n int, err error) {
retval := make(map[interface{}]interface{})
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
k, n, e := Unpack(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
v, n, e := Unpack(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[k.Interface()] = v.Interface()
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpackMapReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err error) {
retval := make(map[interface{}]reflect.Value)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
k, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
v, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[k] = v
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpack(reader io.Reader, reflected bool) (v reflect.Value, n int, err error) {
var retval reflect.Value
var nbytesread int = 0
c, e := readByte(reader)
if e != nil {
return reflect.Value{}, 0, e
}
nbytesread += 1
if c < 0x80 || c >= 0xe0 {
retval = reflect.ValueOf(int8(c))
} else if c >= 0x80 && c <= 0x8f {
if reflected {
retval, n, e = unpackMapReflected(reader, uint(c&0xf))
} else {
retval, n, e = unpackMap(reader, uint(c&0xf))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
nbytesread += n
} else if c >= 0x90 && c <= 0x9f {
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(c&0xf))
} else {
retval, n, e = unpackArray(reader, uint(c&0xf))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
nbytesread += n
} else if c >= 0xa0 && c <= 0xbf {
data := make([]byte, c&0x1f)
n, e := reader.Read(data)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
} else {
switch c {
case 0xc0:
retval = reflect.ValueOf(nil)
case 0xc2:
retval = reflect.ValueOf(false)
case 0xc3:
retval = reflect.ValueOf(true)
case 0xca:
data, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(*(*float32)(unsafe.Pointer(&data)))
case 0xcb:
data, n, e := readUint64(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(*(*float64)(unsafe.Pointer(&data)))
case 0xcc:
data, e := readByte(reader)
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(uint8(data))
nbytesread += 1
case 0xcd:
data, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xce:
data, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xcf:
data, n, e := readUint64(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xd0:
data, e := readByte(reader)
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(int8(data))
nbytesread += 1
case 0xd1:
data, n, e := readInt16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xd2:
data, n, e := readInt32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xd3:
data, n, e := readInt64(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xda:
nbytestoread, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
data := make([]byte, nbytestoread)
n, e = reader.Read(data)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xdb:
nbytestoread, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
data := make([]byte, nbytestoread)
n, e = reader.Read(data)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xdc:
nelemstoread, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackArray(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
case 0xdd:
nelemstoread, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackArray(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
case 0xde:
nelemstoread, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackMap(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
case 0xdf:
nelemstoread, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackMap(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
default:
panic("unsupported code: " + strconv.Itoa(int(c)))
}
}
return retval, nbytesread, nil
}
// Reads a value from the reader, unpack and returns it.
func Unpack(reader io.Reader) (v reflect.Value, n int, err error) {
return unpack(reader, false)
}
// Reads unpack a value from the reader, unpack and returns it. When the
// value is an array or map, leaves the elements wrapped by corresponding
// wrapper objects defined in reflect package.
func UnpackReflected(reader io.Reader) (v reflect.Value, n int, err error) {
return unpack(reader, true)
}