-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
542 lines (498 loc) · 12.6 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
package ebml
import (
"encoding/binary"
"errors"
"fmt"
"github.com/coding-socks/ebml/schema"
"io"
"math"
"reflect"
"strconv"
"time"
)
// An DecodeTypeError describes an EBML value that was
// not appropriate for a value of a specific Go type.
type DecodeTypeError struct {
EBMLType string // description of EBML type - "integer", "binary", "master"
Type reflect.Type // type of Go value it could not be assigned to
Offset int64 // error occurred after reading Offset bytes
Path string // the full path from root node to the field
}
func (e *DecodeTypeError) Error() string {
if e.Path != "" {
return fmt.Sprintf("ebml: cannot unmarshal %s into Go struct field %s of type %s", e.EBMLType, e.Path, e.Type)
}
return fmt.Sprintf("ebml: cannot unmarshal %s into Go value of type %s", e.EBMLType, e.Type)
}
func (e *DecodeTypeError) extendError(p string) {
if e.Path == "" {
e.Path = p
return
}
e.Path = p + "." + e.Path
}
// An InvalidDecodeError describes an invalid argument passed to Unmarshal.
// (The argument to Unmarshal must be a non-nil pointer.)
type InvalidDecodeError struct {
Type reflect.Type
}
func (e *InvalidDecodeError) Error() string {
if e.Type == nil {
return "ebml: Unmarshal(nil)"
}
if e.Type.Kind() != reflect.Ptr {
return "ebml: Unmarshal(non-pointer " + e.Type.String() + ")"
}
return "ebml: Unmarshal(nil " + e.Type.String() + ")"
}
// ErrElementOverflow signals that an element signals a length
// greater than the parent DataSize.
var ErrElementOverflow = errors.New("ebml: element overflow")
// DecodeHeader decodes the document header.
func (d *Decoder) DecodeHeader() (*EBML, error) {
for {
el, _, err := d.Next()
if err != nil {
return nil, err
}
switch el.ID {
default:
return nil, fmt.Errorf("ebml: unexpected element %v in root", el.ID)
case IDVoid:
if _, err := d.Seek(el.DataSize.Size(), io.SeekCurrent); err != nil {
return nil, fmt.Errorf("ebml: could not skip Void element: %w", err)
}
continue
case IDEBML:
d.def = HeaderDef
d.r.MaxIDLength = DefaultMaxIDLength
d.r.MaxSizeLength = DefaultMaxSizeLength
var h EBML
err := d.Decode(&h)
if err != nil {
return nil, err
}
d.def, err = Definition(h.DocType)
if err != nil {
return nil, err
}
d.r.MaxIDLength = h.EBMLMaxIDLength
d.r.MaxSizeLength = h.EBMLMaxSizeLength
return &h, err
}
}
}
// DecodeBody decodes the EBML Body and stores the result in the value
// pointed to by v. If v is nil or not a pointer, DecodeBody returns
// an InvalidDecodeError.
func (d *Decoder) DecodeBody(v interface{}) error {
for {
el, _, err := d.Next()
if err != nil {
return err
}
switch el.ID {
default:
return fmt.Errorf("ebml: unexpected element %v in root", el.ID)
case IDVoid:
if _, err := d.Seek(el.DataSize.Size(), io.SeekCurrent); err != nil {
return fmt.Errorf("ebml: could not skip Void element: %w", err)
}
continue
case d.def.Root.ID:
return d.Decode(v)
}
}
}
func (d *Decoder) Decode(v interface{}) error {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr || val.IsNil() {
return &InvalidDecodeError{reflect.TypeOf(v)}
}
if d.el == nil {
return fmt.Errorf("ebml: missing decoded element (forgotten call Next?)")
}
err := d.decodeSingle(*d.el, val.Elem())
d.el = nil
if err == nil && d.elOverflow {
return ErrElementOverflow
}
return err
}
var (
typeTime = reflect.TypeOf(time.Time{})
typeDuration = reflect.TypeOf(time.Duration(0))
typeElementID = reflect.TypeOf(schema.ElementID(0))
thirdMillennium = time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC)
)
func findField(val reflect.Value, tinfo *typeInfo, name string) (fieldv reflect.Value, found bool) {
for i := range tinfo.fields {
finfo := tinfo.fields[i]
if name != finfo.name {
continue
}
found = true
fieldv = val.Field(finfo.idx[0])
break
}
return
}
func (d *Decoder) decodeMaster(val reflect.Value, current Element) error {
// Load value from interface, but only if the result will be
// usefully addressable.
if val.Kind() == reflect.Interface && !val.IsNil() {
e := val.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() {
val = e
}
}
if val.Kind() == reflect.Ptr {
if val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
val = val.Elem()
}
switch v := val; v.Kind() {
default:
return errors.New("unknown master element type: " + val.Type().String())
case reflect.Slice:
// TODO: Consider checking max / min occurrence.
e := v.Type().Elem()
n := v.Len()
v.Set(reflect.Append(v, reflect.Zero(e)))
val = v.Index(n)
case reflect.Struct:
// Everything is ok
}
typ := val.Type()
tinfo, err := getTypeInfo(typ)
if err != nil {
return err
}
occurrences := make(map[schema.ElementID]int)
var offset int64
for { // 538158, 1692138 | 21848480, 988541
el, n, err := d.NextOf(current, offset)
if current.DataSize.Known() {
offset += int64(n) // Skip garbage eg. ErrInvalidVINTLength.
}
if err != nil {
if err == ErrInvalidVINTLength {
continue
}
if err == io.EOF {
break
}
var e *UnknownElementError
if !current.DataSize.Known() && errors.As(err, &e) {
break
}
return err
}
if current.DataSize.Known() {
// detect element overflow early to pretend the element is smaller
if current.DataSize.Size() < offset+el.DataSize.Size() {
el.DataSize = NewKnownDataSize(current.DataSize.Size() - offset)
d.elOverflow = true
}
offset += el.DataSize.Size()
}
def, _ := d.def.Get(el.ID)
occurrences[el.ID]++
fieldv, found := findField(val, tinfo, def.Name)
if !found {
if el.DataSize.Known() {
if _, err := d.Seek(el.DataSize.Size(), io.SeekCurrent); err != nil {
return fmt.Errorf("ebml: was not able to skip element: %w", err)
}
continue
} else if def.Type == TypeMaster {
if err := d.decodeMaster(val, el); err != nil {
return err
}
continue
} else {
return errors.New("ebml: only a master element is allowed to be of unknown size")
}
}
if err := d.decodeSingle(el, fieldv); err != nil {
if e, ok := err.(*DecodeTypeError); ok {
e.extendError(val.Type().Name())
}
return err
}
}
if current.DataSize.Known() && offset < current.DataSize.Size() {
return io.ErrUnexpectedEOF
}
elements := d.def.Values()
for i := range elements {
sel := elements[i]
if sel.Default == nil || occurrences[sel.ID] > 0 {
continue
}
fieldv, found := findField(val, tinfo, sel.Name)
if !found {
continue
}
if fieldv.Kind() == reflect.Ptr {
if fieldv.IsNil() {
fieldv.Set(reflect.New(fieldv.Type().Elem()))
}
fieldv = fieldv.Elem()
}
if v := fieldv; v.Kind() == reflect.Slice {
e := v.Type().Elem()
if !(sel.Type == TypeBinary && e.Kind() == reflect.Uint8) {
n := v.Len()
v.Set(reflect.Append(v, reflect.Zero(e)))
fieldv = v.Index(n)
}
}
if err := validateReflectType(fieldv, sel, 0); err != nil {
if e, ok := err.(*DecodeTypeError); ok {
e.extendError(sel.Name)
e.extendError(val.Type().Name())
}
return err
}
switch sel.Type {
case TypeInteger:
x, _ := strconv.ParseInt(*sel.Default, 10, 64)
fieldv.SetInt(x)
case TypeUinteger:
if fieldv.Type() == typeDuration {
x, _ := strconv.ParseInt(*sel.Default, 10, 64)
fieldv.SetInt(x)
} else {
x, _ := strconv.ParseUint(*sel.Default, 10, 64)
fieldv.SetUint(x)
}
case TypeFloat:
x, _ := strconv.ParseFloat(*sel.Default, 64)
fieldv.SetFloat(x)
case TypeString:
fieldv.SetString(*sel.Default)
default:
return fmt.Errorf("default not supported: %s", sel.Type)
}
}
return nil
}
func validateReflectType(v reflect.Value, def schema.Element, position int64) error {
switch def.Type {
default:
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
case TypeMaster:
switch v.Kind() {
default:
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
case reflect.Struct:
// valid type
}
case TypeBinary:
switch v.Kind() {
default:
switch v.Type() {
default:
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
case typeElementID:
// valid type
}
case reflect.Slice:
e := v.Type().Elem()
if e.Kind() != reflect.Uint8 {
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
}
}
case TypeDate:
switch v.Type() {
default:
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
case typeTime:
// valid type
}
case TypeFloat:
switch v.Kind() {
default:
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
case reflect.Float32, reflect.Float64:
// valid type
}
case TypeInteger:
switch v.Kind() {
default:
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
case reflect.Int, reflect.Int64, reflect.Int32:
// valid type
}
case TypeUinteger:
switch v.Kind() {
default:
switch v.Type() {
default:
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
case typeDuration:
// valid type
}
case reflect.Uint, reflect.Uint64, reflect.Uint32:
// valid type
}
case TypeString, TypeUTF8:
if v.Kind() != reflect.String {
return &DecodeTypeError{EBMLType: def.Type, Type: v.Type(), Offset: position}
}
}
return nil
}
func (d *Decoder) decodeSingle(el Element, val reflect.Value) error {
def, _ := d.def.Get(el.ID)
if val.Kind() == reflect.Ptr {
if val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
val = val.Elem()
}
if v := val; v.Kind() == reflect.Slice {
e := v.Type().Elem()
if !(def.Type == TypeBinary && e.Kind() == reflect.Uint8) {
n := v.Len()
v.Set(reflect.Append(v, reflect.Zero(e)))
val = v.Index(n)
}
}
if err := validateReflectType(val, def, 0); err != nil {
if e, ok := err.(*DecodeTypeError); ok {
e.extendError(def.Name)
}
return err
}
switch def.Type {
case TypeMaster:
if err := d.decodeMaster(val, el); err != nil {
return err
}
case TypeBinary:
switch val.Type() {
default:
b, err := d.readByteSlice(el.DataSize.Size())
if err != nil {
return err
}
val.SetBytes(b)
case typeElementID:
i, err := d.readUint(el.DataSize.Size())
if err != nil {
return err
}
val.SetUint(i)
}
case TypeDate:
t, err := d.readDate(el.DataSize.Size())
if err != nil {
return err
}
val.Set(reflect.ValueOf(t))
case TypeFloat:
f, err := d.readFloat(el.DataSize.Size())
if err != nil {
return err
}
val.SetFloat(f)
case TypeInteger:
i, err := d.readInt(el.DataSize.Size())
if err != nil {
return err
}
val.SetInt(i)
case TypeUinteger:
switch val.Type() {
default:
i, err := d.readUint(el.DataSize.Size())
if err != nil {
return err
}
val.SetUint(i)
case typeDuration:
i, err := d.readInt(el.DataSize.Size())
if err != nil {
return err
}
val.SetInt(i)
}
case TypeString, TypeUTF8:
str, err := d.readString(el.DataSize.Size())
if err != nil {
return err
}
val.SetString(str)
}
return nil
}
func (d *Decoder) readByteSlice(ds int64) ([]byte, error) {
b := make([]byte, ds)
_, err := io.ReadFull(d.r, b)
return b, err
}
func (d *Decoder) readDate(ds int64) (time.Time, error) {
i, err := d.readInt(ds)
if err != nil {
return time.Time{}, err
}
return thirdMillennium.Add(time.Nanosecond * time.Duration(i)), nil
}
func (d *Decoder) readString(ds int64) (string, error) {
b, err := d.readByteSlice(ds)
if err != nil {
return "", err
}
// TODO: detect value greater than VINTMAX
return string(b), err
}
func (d *Decoder) readFloat(ds int64) (float64, error) {
b, err := d.readByteSlice(ds)
if err != nil {
return 0, err
}
// A Float Element MUST declare a length of either
// zero octets (0 bit), four octets (32 bit),
// or eight octets (64 bit).
switch len(b) {
case 0:
return 0, nil
case 4:
return float64(math.Float32frombits(binary.BigEndian.Uint32(b))), nil
case 8:
return math.Float64frombits(binary.BigEndian.Uint64(b)), nil
default:
return 0, errors.New("ebml: data length must be 0 bit, 32 bit or 64 bit for a float")
}
}
func (d *Decoder) readInt(ds int64) (int64, error) {
b, err := d.readByteSlice(ds)
if err != nil {
return 0, err
}
if len(b) > 8 {
return 0, errors.New("ebml: max length for an unsigned integer is eight octets")
}
i := int64(0)
for _, bb := range b {
i = (i << 8) | int64(bb)
}
return i, nil
}
func (d *Decoder) readUint(ds int64) (uint64, error) {
b, err := d.readByteSlice(ds)
if err != nil {
return 0, err
}
if len(b) > 8 {
return 0, errors.New("ebml: max length for an unsigned integer is eight octets")
}
i := uint64(0)
for _, bb := range b {
i = (i << 8) | uint64(bb)
}
return i, nil
}