-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.go
603 lines (515 loc) · 13.4 KB
/
decode.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package bson
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"reflect"
)
// Decoder reads and decodes BSON values from an input stream.
type Decoder struct {
r io.Reader
data []byte
}
func NewDecodeBytes(buf []byte) *Decoder {
d := &Decoder{
data: buf,
}
return d
}
func (dec *Decoder) Decode(v any) error {
if len(dec.data) < 4 {
return errors.New("not enough data") // TODO(cristaloleg): static error?
}
if d, ok := v.(*D); ok {
return readD(dec.data, d)
}
rv := reflect.ValueOf(v)
switch {
case rv.Kind() != reflect.Ptr:
return errors.New("unmarshal non-pointer: " + rv.Type().String())
case rv.IsNil():
return errors.New("unmarshal nil: " + rv.Type().String())
}
switch rv := rv.Elem(); rv.Kind() {
case reflect.Struct:
return decodeStruct(dec.data, rv)
case reflect.Map:
return decodeMap(dec.data, rv)
default:
return errors.New("unmarshal unsupported: " + rv.Type().String())
}
}
func readD(data []byte, d *D) error {
iter, err := newReader(data)
if err != nil {
return err
}
for iter.Next() {
typ, name, element := iter.Peek()
key := trimlast(name)
switch typ {
case TypeDouble:
val := uint64(element[0]) |
uint64(element[1])<<8 |
uint64(element[2])<<16 |
uint64(element[3])<<24 |
uint64(element[4])<<32 |
uint64(element[5])<<40 |
uint64(element[6])<<48 |
uint64(element[7])<<56
*d = append(*d, e{K: key, V: math.Float64frombits(val)})
case TypeString:
*d = append(*d, e{K: key, V: trimlast(element)})
case TypeDocument:
m := make(map[string]any)
vv := reflect.ValueOf(m)
if err := decodeMap(element, vv); err != nil {
return err
}
*d = append(*d, e{K: key, V: m})
case TypeArray:
s := make([]any, 0)
if err := decodeSlice(element, &s); err != nil {
return err
}
*d = append(*d, e{K: key, V: s})
case TypeObjectID:
var oid ObjectID
copy(oid[:], element)
*d = append(*d, e{K: key, V: nil})
case TypeBool:
*d = append(*d, e{K: key, V: element[0] == 1})
case TypeInt32:
element := int32(element[0]) |
int32(element[1])<<8 |
int32(element[2])<<16 |
int32(element[3])<<24
*d = append(*d, e{K: key, V: element})
case TypeTimestamp:
ts := Timestamp(element[0]) |
Timestamp(element[1])<<8 |
Timestamp(element[2])<<16 |
Timestamp(element[3])<<24 |
Timestamp(element[4])<<32 |
Timestamp(element[5])<<40 |
Timestamp(element[6])<<48 |
Timestamp(element[7])<<56
*d = append(*d, e{K: key, V: ts})
case TypeInt64:
element := int64(element[0]) |
int64(element[1])<<8 |
int64(element[2])<<16 |
int64(element[3])<<24 |
int64(element[4])<<32 |
int64(element[5])<<40 |
int64(element[6])<<48 |
int64(element[7])<<56
*d = append(*d, e{K: key, V: element})
case TypeBinary,
TypeUndefined,
TypeDateTime,
TypeRegex,
TypeDBPointer,
TypeCodeWithScope,
TypeSymbol,
TypeJavaScriptScope,
TypeDecimal,
TypeMinKey,
TypeMaxKey:
return fmt.Errorf("unsupported type %x", typ)
default:
return fmt.Errorf("unknown element type %x", typ)
}
}
return iter.Err()
}
func decodeStruct(data []byte, v reflect.Value) error {
iter, err := newReader(data)
if err != nil {
return err
}
for iter.Next() {
typ, name, element := iter.Peek()
v := v.FieldByName(trimlast(name))
if !v.IsValid() {
continue
}
switch typ {
case TypeDouble:
val := uint64(element[0]) |
uint64(element[1])<<8 |
uint64(element[2])<<16 |
uint64(element[3])<<24 |
uint64(element[4])<<32 |
uint64(element[5])<<40 |
uint64(element[6])<<48 |
uint64(element[7])<<56
v.SetFloat(math.Float64frombits(val))
case TypeString:
v.SetString(trimlast(element))
case TypeDocument:
m := make(map[string]any)
vv := reflect.ValueOf(m)
if err := decodeMap(element, vv); err != nil {
return err
}
v.Set(vv)
case TypeArray:
s := make([]any, 0)
if err := decodeSlice(element, &s); err != nil {
return err
}
vv := reflect.ValueOf(s)
v.Set(vv)
case TypeObjectID:
var oid ObjectID
copy(oid[:], element)
v.Set(reflect.ValueOf(oid))
case TypeBool:
v.SetBool(element[0] == 1)
case TypeInt32:
element := int64(element[0]) |
int64(element[1])<<8 |
int64(element[2])<<16 |
int64(element[3])<<24
v.SetInt(element)
case TypeTimestamp:
ts := Timestamp(element[0]) |
Timestamp(element[1])<<8 |
Timestamp(element[2])<<16 |
Timestamp(element[3])<<24 |
Timestamp(element[4])<<32 |
Timestamp(element[5])<<40 |
Timestamp(element[6])<<48 |
Timestamp(element[7])<<56
v.Set(reflect.ValueOf(ts))
case TypeInt64:
element := int64(element[0]) |
int64(element[1])<<8 |
int64(element[2])<<16 |
int64(element[3])<<24 |
int64(element[4])<<32 |
int64(element[5])<<40 | int64(element[6])<<48 | int64(element[7])<<56
v.SetInt(element)
case TypeBinary,
TypeUndefined,
TypeDateTime,
TypeNull,
TypeRegex,
TypeDBPointer,
TypeCodeWithScope,
TypeSymbol,
TypeJavaScriptScope,
TypeDecimal,
TypeMinKey,
TypeMaxKey:
return fmt.Errorf("unsupported type %x", typ)
default:
return fmt.Errorf("unknown element type %x", typ)
}
}
return iter.Err()
}
func decodeMap(data []byte, v reflect.Value) error {
iter, err := newReader(data)
if err != nil {
return err
}
for iter.Next() {
typ, name, element := iter.Peek()
key := reflect.ValueOf(trimlast(name))
switch typ {
case TypeDouble:
val := uint64(element[0]) |
uint64(element[1])<<8 |
uint64(element[2])<<16 |
uint64(element[3])<<24 |
uint64(element[4])<<32 |
uint64(element[5])<<40 |
uint64(element[6])<<48 |
uint64(element[7])<<56
v.SetMapIndex(key, reflect.ValueOf(math.Float64frombits(val)))
case TypeString:
vv := reflect.ValueOf(trimlast(element))
v.SetMapIndex(key, vv)
case TypeDocument:
m := make(map[string]any)
vv := reflect.ValueOf(m)
if err := decodeMap(element, vv); err != nil {
return err
}
v.SetMapIndex(key, vv)
case TypeArray:
s := make([]any, 0)
if err := decodeSlice(element, &s); err != nil {
return err
}
vv := reflect.ValueOf(s)
v.SetMapIndex(key, vv)
case TypeObjectID:
var oid ObjectID
copy(oid[:], element)
v.SetMapIndex(key, reflect.ValueOf(oid))
case TypeBool:
v.SetMapIndex(key, reflect.ValueOf(element[0] == 1))
case TypeInt32:
element := int32(element[0]) |
int32(element[1])<<8 |
int32(element[2])<<16 |
int32(element[3])<<24
v.SetMapIndex(key, reflect.ValueOf(element))
case TypeTimestamp:
ts := Timestamp(element[0]) |
Timestamp(element[1])<<8 |
Timestamp(element[2])<<16 |
Timestamp(element[3])<<24 |
Timestamp(element[4])<<32 |
Timestamp(element[5])<<40 |
Timestamp(element[6])<<48 |
Timestamp(element[7])<<56
v.SetMapIndex(key, reflect.ValueOf(ts))
case TypeInt64:
element := int64(element[0]) |
int64(element[1])<<8 |
int64(element[2])<<16 |
int64(element[3])<<24 |
int64(element[4])<<32 |
int64(element[5])<<40 |
int64(element[6])<<48 |
int64(element[7])<<56
v.SetMapIndex(key, reflect.ValueOf(element))
case TypeBinary,
TypeUndefined,
TypeDateTime,
TypeRegex,
TypeDBPointer,
TypeCodeWithScope,
TypeSymbol,
TypeJavaScriptScope,
TypeDecimal,
TypeMinKey,
TypeMaxKey:
return fmt.Errorf("unsupported type %x", typ)
default:
return fmt.Errorf("unknown element type %x", typ)
}
}
return iter.Err()
}
func decodeSlice(data []byte, v *[]any) error {
iter, err := newReader(data)
if err != nil {
return err
}
for iter.Next() {
typ, _, element := iter.Peek()
switch typ {
case TypeDouble:
bits := uint64(element[0]) |
uint64(element[1])<<8 |
uint64(element[2])<<16 |
uint64(element[3])<<24 |
uint64(element[4])<<32 |
uint64(element[5])<<40 |
uint64(element[6])<<48 |
uint64(element[7])<<56
*v = append(*v, bits)
case TypeString:
*v = append(*v, trimlast(element))
case TypeDocument:
m := make(map[string]any)
vv := reflect.ValueOf(m)
if err := decodeMap(element, vv); err != nil {
return err
}
*v = append(*v, m)
case TypeArray:
s := make([]any, 0)
if err := decodeSlice(element, &s); err != nil {
return err
}
*v = append(*v, s)
case TypeObjectID:
var oid ObjectID
copy(oid[:], element)
*v = append(*v, oid)
case TypeBool:
b := element[0] == 1
*v = append(*v, b)
// case TypeDateTime:
// dt := Datetime(element[0]) | Datetime(element[1])<<8 | Datetime(element[2])<<16 | Datetime(element[3])<<24 | Datetime(element[4])<<32 | Datetime(element[5])<<40 | Datetime(element[6])<<48 | Datetime(element[7])<<56
// *v = append(*v, dt)
case TypeNull:
*v = append(*v, nil)
case TypeInt32:
element := int32(element[0]) | int32(element[1])<<8 | int32(element[2])<<16 | int32(element[3])<<24
*v = append(*v, element)
case TypeTimestamp:
ts := Timestamp(element[0]) | Timestamp(element[1])<<8 | Timestamp(element[2])<<16 | Timestamp(element[3])<<24 | Timestamp(element[4])<<32 | Timestamp(element[5])<<40 | Timestamp(element[6])<<48 | Timestamp(element[7])<<56
*v = append(*v, ts)
case TypeInt64:
element := int64(element[0]) | int64(element[1])<<8 | int64(element[2])<<16 | int64(element[3])<<24 | int64(element[4])<<32 | int64(element[5])<<40 | int64(element[6])<<48 | int64(element[7])<<56
*v = append(*v, element)
case TypeBinary,
TypeUndefined,
TypeDateTime,
TypeRegex,
TypeDBPointer,
TypeCodeWithScope,
TypeSymbol,
TypeJavaScriptScope,
TypeDecimal,
TypeMinKey,
TypeMaxKey:
return fmt.Errorf("unsupported type %x", typ)
default:
return fmt.Errorf("unknown element type %x", typ)
}
}
return iter.Err()
}
type reader struct {
data []byte // data to process.
name []byte // name of the current element.
element []byte // current element.
err error // err during reading.
}
func newReader(b []byte) (reader, error) {
if len(b) < 5 {
return reader{}, errors.New("not enough data")
}
return reader{
data: b[4 : len(b)-1], // TODO(cristaloleg): this can easily panic.
}, nil
}
func (r *reader) Err() error {
return r.err
}
func (r *reader) Peek() (Type, []byte, []byte) {
return Type(r.name[0]), r.name[1:], r.element
}
func (r *reader) Next() bool {
if r.err != nil || len(r.data) == 0 {
return false
}
if len(r.data) == 1 {
return r.setErr(errors.New("corrupt BSON, only 1 byte remains"))
}
i := bytes.IndexByte(r.data[1:], 0)
if i < 0 {
return r.setErr(errors.New("corrupt BSON name"))
}
i += 2
name, rest := r.data[:i], r.data[i:]
var element []byte
switch typ := name[0]; Type(typ) {
case TypeDouble:
if len(rest) < 8 {
return r.setErr(errors.New("corrupt BSON reading double"))
}
element, rest = rest[:8], rest[8:]
case TypeString:
if len(rest) < 5 {
return r.setErr(errors.New("corrupt BSON reading string len"))
}
elen, _ := readInt32(rest)
if len(rest) < elen {
return r.setErr(errors.New("corrupt BSON reading string"))
}
element = rest[4 : 4+elen]
if len(rest) < 4+elen {
return r.setErr(errors.New("corrupt BSON reading string"))
}
rest = rest[4+elen:]
case TypeDocument, TypeArray:
if len(rest) < 5 {
return r.setErr(errors.New("corrupt BSON reading string len"))
}
elen, _ := readInt32(rest)
if len(rest) < elen {
return r.setErr(fmt.Errorf("corrupt document: want %x bytes, have %x", elen, len(rest)))
}
element, rest = rest[:elen], rest[elen:]
case TypeObjectID:
if len(rest) < 12 {
return r.setErr(errors.New("corrupt BSON reading object id"))
}
element, rest = rest[:12], rest[12:]
case TypeBool:
if len(rest) < 1 {
return r.setErr(errors.New("corrupt BSON reading boolean"))
}
element, rest = rest[:1], rest[1:]
case TypeDateTime:
if len(rest) < 8 {
return r.setErr(errors.New("corrupt BSON reading utc datetime"))
}
element, rest = rest[:8], rest[8:]
case TypeNull:
element, rest = rest[:0], rest[0:]
// TODO(cristaloleg): fuzzer fails on 2nd IndexByte.
// case TypeRegex:
// if len(rest) < 2 {
// return r.setErr(errors.New("corrupt BSON reading regex"))
// }
// i := bytes.IndexByte(rest, 0)
// if i < 0 {
// return r.setErr(errors.New("corrupt BSON regex 1"))
// }
// i++
// j := bytes.IndexByte(rest[i+1:], 0)
// if j < 0 {
// return r.setErr(errors.New("corrupt BSON regex 2"))
// }
// j++
// element, rest = rest[:i+j+1], rest[i+j+1:]
case TypeInt32:
if len(rest) < 4 {
return r.setErr(errors.New("corrupt BSON reading int32"))
}
element, rest = rest[:4], rest[4:]
case TypeTimestamp:
fallthrough
case TypeInt64:
if len(rest) < 8 {
return r.setErr(errors.New("corrupt BSON reading int64"))
}
element, rest = rest[:8], rest[8:]
default:
return r.setErr(fmt.Errorf("unknown element type %x", typ))
}
r.data, r.name, r.element = rest, name, element
return true
}
func (r *reader) setErr(err error) bool {
if r.err == nil {
r.err = err
}
return false
}
// readInt32 the 4 bytes in little endian and return tail.
// Panics if less than 4 bytes is passed.
func readInt32(buf []byte) (int, []byte) {
v := int(buf[0]) |
int(buf[1])<<8 |
int(buf[2])<<16 |
int(buf[3])<<24
return v, buf[4:]
}
// readCstring returns CString including \0.
func readCstring(buf []byte) ([]byte, []byte, error) {
i := bytes.IndexByte(buf, 0)
if i == -1 {
return nil, nil, errors.New("cstring missing \\0")
}
i++
return buf[:i], buf[i:], nil
}
func trimlast(s []byte) string {
if len(s) == 0 {
return ""
}
return string(s[:len(s)-1])
}