-
Notifications
You must be signed in to change notification settings - Fork 14
/
unmarshal.go
318 lines (292 loc) · 7.65 KB
/
unmarshal.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
// Copyright 2019 The ebml-go authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ebml
import (
"bytes"
"errors"
"io"
"reflect"
)
// ErrUnknownElement means that a decoded element is not known.
var ErrUnknownElement = errors.New("unknown element")
// ErrIndefiniteType means that a marshal/unmarshal destination type is not valid.
var ErrIndefiniteType = errors.New("marshal/unmarshal to indefinite type")
// ErrIncompatibleType means that an element is not convertible to a corresponding struct field.
var ErrIncompatibleType = errors.New("marshal/unmarshal to incompatible type")
// ErrInvalidElementSize means that an element has inconsistent size. e.g. element size is larger than its parent element size.
var ErrInvalidElementSize = errors.New("invalid element size")
// ErrReadStopped is returned if unmarshaler finished to read element which has stop tag.
var ErrReadStopped = errors.New("read stopped")
// Unmarshal EBML stream.
func Unmarshal(r io.Reader, val interface{}, opts ...UnmarshalOption) error {
options := &UnmarshalOptions{}
for _, o := range opts {
if err := o(options); err != nil {
return err
}
}
vo := reflect.ValueOf(val)
if !vo.IsValid() {
return wrapErrorf(ErrIndefiniteType, "unmarshalling to %T", val)
}
if vo.Kind() != reflect.Ptr {
return wrapErrorf(ErrIncompatibleType, "unmarshalling to %T", val)
}
vd := &valueDecoder{}
voe := vo.Elem()
for {
if _, err := vd.readElement(r, SizeUnknown, voe, 0, 0, nil, options); err != nil {
if err == io.EOF {
return nil
}
return err
}
}
}
func (vd *valueDecoder) readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64, parent *Element, options *UnmarshalOptions) (io.Reader, error) {
pos0 := pos
var r rollbackReader
if options.ignoreUnknown {
r = &rollbackReaderImpl{}
} else {
r = &rollbackReaderNop{}
}
if n != SizeUnknown {
r.Set(io.LimitReader(r0, n))
} else {
r.Set(r0)
}
var mapOut bool
type fieldDef struct {
v reflect.Value
stop bool
}
fieldMap := make(map[ElementType]fieldDef)
switch vo.Kind() {
case reflect.Struct:
for i := 0; i < vo.NumField(); i++ {
f := fieldDef{
v: vo.Field(i),
}
var name string
if n, ok := vo.Type().Field(i).Tag.Lookup("ebml"); ok {
t, err := parseTag(n)
if err != nil {
return nil, err
}
name = t.name
f.stop = t.stop
}
if name == "" {
name = vo.Type().Field(i).Name
}
t, err := ElementTypeFromString(name)
if err != nil {
return nil, err
}
fieldMap[t] = f
}
case reflect.Map:
mapOut = true
}
for {
r.Reset()
var headerSize uint64
e, nb, err := vd.readVUInt(r)
headerSize += uint64(nb)
if err != nil {
if nb == 0 && err == io.ErrUnexpectedEOF {
return nil, io.EOF
}
if options.ignoreUnknown {
return nil, nil
}
return nil, err
}
v, ok := revTable[uint32(e)]
if !ok {
if options.ignoreUnknown {
r.RollbackTo(1)
pos++
continue
}
return nil, wrapErrorf(ErrUnknownElement, "unmarshalling element 0x%x", e)
}
size, nb, err := vd.readDataSize(r)
headerSize += uint64(nb)
if n != SizeUnknown && pos+headerSize+size > pos0+uint64(n) {
err = ErrInvalidElementSize
}
if err != nil {
if options.ignoreUnknown {
r.RollbackTo(1)
pos++
continue
}
return nil, err
}
var vnext reflect.Value
var stopHere bool
if vn, ok := fieldMap[v.e]; ok {
if !mapOut {
vnext = vn.v
}
stopHere = vn.stop
}
var chanSend reflect.Value
var elem *Element
if len(options.hooks) > 0 && vnext.IsValid() {
elem = &Element{
Name: v.e.String(),
Type: v.e,
Position: pos,
Size: size,
Parent: parent,
}
}
if vnext.Kind() == reflect.Chan {
chanSend = vnext
vnext = reflect.New(vnext.Type().Elem()).Elem()
}
switch v.t {
case DataTypeMaster:
if v.top && depth > 1 {
b := bytes.Join([][]byte{table[v.e].b, encodeDataSize(size, uint64(nb))}, []byte{})
return bytes.NewBuffer(b), io.EOF
}
var vn reflect.Value
if mapOut {
vnext = reflect.ValueOf(make(map[string]interface{}))
vn = vnext
} else {
if vnext.IsValid() && vnext.CanSet() {
switch vnext.Kind() {
case reflect.Ptr:
vnext.Set(reflect.New(vnext.Type().Elem()))
vn = vnext.Elem()
case reflect.Slice:
vnext.Set(reflect.Append(vnext, reflect.New(vnext.Type().Elem()).Elem()))
vn = vnext.Index(vnext.Len() - 1)
default:
vn = vnext
}
}
}
if elem != nil {
elem.Value = vn.Interface()
}
r0, err := vd.readElement(r, int64(size), vn, depth+1, pos+headerSize, elem, options)
if err != nil && err != io.EOF {
return r0, err
}
if r0 != nil {
r.Set(io.MultiReader(r0, r.Get()))
}
default:
val, err := vd.decode(v.t, r, size)
if err != nil {
if options.ignoreUnknown {
r.RollbackTo(1)
pos++
continue
}
return nil, err
}
vr := reflect.ValueOf(val)
if mapOut {
vnext = vr
} else {
if vnext.IsValid() && vnext.CanSet() {
switch {
case vr.Type() == vnext.Type():
vnext.Set(vr)
case isConvertible(vr.Type(), vnext.Type()):
vnext.Set(vr.Convert(vnext.Type()))
case vnext.Kind() == reflect.Slice:
t := vnext.Type().Elem()
switch {
case vr.Type() == t:
vnext.Set(reflect.Append(vnext, vr))
case isConvertible(vr.Type(), t):
vnext.Set(reflect.Append(vnext, vr.Convert(t)))
default:
return nil, wrapErrorf(
ErrIncompatibleType, "unmarshalling %s to %s", vnext.Type(), vr.Type(),
)
}
default:
return nil, wrapErrorf(
ErrIncompatibleType, "unmarshalling %s to %s", vnext.Type(), vr.Type(),
)
}
}
}
if elem != nil {
elem.Value = vr.Interface()
}
}
if mapOut {
t := vo.Type()
if vo.IsNil() && t.Kind() == reflect.Map {
vo.Set(reflect.MakeMap(t))
}
key := reflect.ValueOf(v.e.String())
if e := vo.MapIndex(key); e.IsValid() {
switch {
case e.Elem().Kind() == reflect.Slice && v.t != DataTypeBinary:
vnext = reflect.Append(e.Elem(), vnext)
default:
vnext = reflect.ValueOf([]interface{}{
e.Elem().Interface(),
vnext.Interface()},
)
}
}
vo.SetMapIndex(key, vnext)
}
if chanSend.IsValid() {
chanSend.Send(vnext)
}
if elem != nil {
for _, hook := range options.hooks {
hook(elem)
}
}
pos += headerSize + size
if stopHere {
return nil, ErrReadStopped
}
}
}
// UnmarshalOption configures a UnmarshalOptions struct.
type UnmarshalOption func(*UnmarshalOptions) error
// UnmarshalOptions stores options for unmarshalling.
type UnmarshalOptions struct {
hooks []func(elem *Element)
ignoreUnknown bool
}
// WithElementReadHooks returns an UnmarshalOption which registers element hooks.
func WithElementReadHooks(hooks ...func(*Element)) UnmarshalOption {
return func(opts *UnmarshalOptions) error {
opts.hooks = hooks
return nil
}
}
// WithIgnoreUnknown returns an UnmarshalOption which makes Unmarshal ignoring unknown element with static length.
func WithIgnoreUnknown(ignore bool) UnmarshalOption {
return func(opts *UnmarshalOptions) error {
opts.ignoreUnknown = ignore
return nil
}
}