-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebml.go
More file actions
317 lines (278 loc) · 7.92 KB
/
ebml.go
File metadata and controls
317 lines (278 loc) · 7.92 KB
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
//go:generate go run make_doctype.go
// Package ebml implements a simple EBML parser.
//
// The EBML specification is RFC 8794.
package ebml
import (
"encoding/xml"
"errors"
"fmt"
"github.com/coding-socks/ebml/ebmltext"
"github.com/coding-socks/ebml/schema"
"io"
"iter"
"maps"
"reflect"
"slices"
"sort"
"strings"
"sync"
)
var ErrInvalidVINTLength = ebmltext.ErrInvalidVINTWidth
var (
docTypesMu sync.RWMutex
docTypes = make(map[string]*Def)
headerDocType schema.Schema
HeaderDef *Def
DefaultMaxIDLength uint = 4
DefaultMaxSizeLength uint = 8
)
func init() {
err := xml.Unmarshal(schemaDefinition, &headerDocType)
if err != nil {
panic("cannot parse header definition")
}
HeaderDef, _ = NewDef(headerDocType)
}
type Def struct {
m map[schema.ElementID]schema.Element
mfield map[string][]schema.Element
Root schema.Element
}
func NewDef(s schema.Schema) (*Def, error) {
def := Def{
m: make(map[schema.ElementID]schema.Element, len(s.Elements)),
mfield: make(map[string][]schema.Element, len(s.Elements)),
}
set := make(map[schema.ElementID]bool, len(s.Elements))
var bodyRoots []schema.Element
for _, el := range s.Elements {
if el.Type == TypeMaster && el.Default != nil {
return nil, fmt.Errorf("ebml: master Element %v MUST NOT declare a default value.", el.ID)
}
set[el.ID] = true
def.m[el.ID] = el
if el.Type != TypeMaster {
i := strings.LastIndex(el.Path, "\\")
parent := el.Path[:i]
def.mfield[parent] = append(def.mfield[parent], el)
}
if strings.Count(el.Path, "\\") == 1 && el.ID != IDVoid {
bodyRoots = append(bodyRoots, el)
}
}
if len(bodyRoots) != 1 {
return nil, errors.New("ebml: an EBML schema MUST declare exactly one EBML element at root level")
}
def.Root = bodyRoots[0]
for _, el := range headerDocType.Elements {
if set[el.ID] {
continue
}
def.m[el.ID] = el
}
return &def, nil
}
func (d *Def) Get(id schema.ElementID) (schema.Element, bool) {
el, ok := d.m[id]
if !ok {
el = UnknownSchema
}
return el, ok
}
func (d *Def) Fields(path string) iter.Seq[schema.Element] {
return slices.Values(d.mfield[path])
}
func (d *Def) All() iter.Seq[schema.Element] {
return maps.Values(d.m)
}
// Register makes a schema.Schema available by the provided doc type.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(docType string, s schema.Schema) {
docTypesMu.Lock()
defer docTypesMu.Unlock()
// TODO: Validate schema
if _, dup := docTypes[docType]; dup {
panic("ebml: register called twice for docType " + docType)
}
def, err := NewDef(s)
if err != nil {
panic(err)
}
docTypes[docType] = def
}
// DocTypes returns a sorted list of the names of the registered document types.
func DocTypes() []string {
docTypesMu.RLock()
defer docTypesMu.RUnlock()
list := make([]string, 0, len(docTypes))
for name := range docTypes {
list = append(list, name)
}
sort.Strings(list)
return list
}
type UnknownDocTypeError struct {
DocType string
}
func (e UnknownDocTypeError) Error() string {
return fmt.Sprintf("ebml: unknown DocType %q (forgotten import?)", e.DocType)
}
func Definition(docType string) (*Def, error) {
docTypesMu.RLock()
dt, ok := docTypes[docType]
docTypesMu.RUnlock()
if !ok {
return nil, UnknownDocTypeError{DocType: docType}
}
return dt, nil
}
var UnknownSchema = schema.Element{
Name: "Unknown element",
Documentation: []schema.Documentation{{Content: "The purpose of this object is to signal an error."}},
}
type Element struct {
ID schema.ElementID
// DataSize expresses the length of Element Data. Unknown data length is
// represented with `-1`.
//
// With 8 octets it can have 2^56-2 possible values. That fits into int64.
DataSize int64
Schema schema.Element
}
// A Decoder represents an EBML parser reading a particular input stream.
type Decoder struct {
r *ebmltext.Decoder
def *Def
el *Element
n int
// skippedErrs signals to return errors at the end of Decode.
skippedErrs error
window []byte
typeInfos map[reflect.Type]*typeInfo
callback Callbacker
}
// NewDecoder reads and parses an EBML Document from r.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
r: ebmltext.NewDecoder(r),
def: HeaderDef,
typeInfos: make(map[reflect.Type]*typeInfo),
}
}
// SetCallback adds a Callbacker which is triggered when NextOf reads element id
// and data size, and when a value is successfully decoded.
func (d *Decoder) SetCallback(c Callbacker) {
d.callback = c
}
// next reads the following element id and data size.
//
// When next encounters an ErrInvalidVINTLength or the element has UnknownSchema,
// it could be caused by damaged data or garbage in the stream. It is up
// to the caller to decide if they want to skip to the next element or
// move the reader forward by seeking one byte using io.SeekCurrent whence.
func (d *Decoder) next() (el Element, n int, err error) {
el.ID, err = d.r.ReadElementID()
if err != nil {
return Element{}, n, err
}
n += d.r.Release()
el.DataSize, err = d.r.ReadElementDataSize()
if err != nil {
return Element{}, n, err
}
n += d.r.Release()
d.n = n
sch, ok := d.def.Get(el.ID)
if !ok {
el.Schema = UnknownSchema
} else {
el.Schema = sch
}
if d.callback != nil {
d.callback = d.callback.Found(el, d.r.InputOffset()-int64(n), n)
}
return el, n, err
}
var RootEl = Element{DataSize: -1, Schema: schema.Element{Name: "root", Type: TypeMaster}}
// NextOf reads the following element id and data size
// related to the given parent Element.
//
// When NextOf encounters io.EOF or end-of-element condition, it
// returns io.EOF.
//
// When NextOf encounters ErrElementOverflow fo known data size,
// you can skip the parent object, or you can read until the parent ends.
//
// See Next about ErrInvalidVINTLength.
func (d *Decoder) NextOf(parent Element, offset int64) (el Element, n int, err error) {
if end := d.EndOfKnownDataSize(parent, offset); end {
return Element{}, 0, io.EOF
}
if d.el != nil {
el = *d.el
d.el = nil
} else {
el, n, err = d.next()
if err != nil {
return Element{}, n, err
}
}
if parent.DataSize != -1 && offset+el.DataSize > parent.DataSize {
err = ErrElementOverflow
}
if end := d.EndOfUnknownDataSize(parent, el); end {
tmp := el // This is unexpected. I cannot use the pointer to the return parameter variable.
d.el = &tmp
return Element{}, 0, io.EOF
}
return el, n, err
}
func (d *Decoder) AsSeeker() (io.Seeker, bool) {
s, ok := d.r.AsSeeker()
if !ok {
return nil, false
}
return DecodeSeeker{d: d, ss: s}, ok
}
type DecodeSeeker struct {
d *Decoder
ss io.Seeker
}
func (s DecodeSeeker) Seek(offset int64, whence int) (ret int64, err error) {
d, ss := s.d, s.ss
if offset != 0 && whence != io.SeekCurrent {
d.el = nil
}
return ss.Seek(offset, whence)
}
// EndOfKnownDataSize tries to guess the end of an element which has a know data size.
//
// A parent with unknown data size won't raise an error but not handled as the end of the parent.
func (d *Decoder) EndOfKnownDataSize(parent Element, offset int64) bool {
if parent.DataSize == -1 {
return false
}
return offset >= parent.DataSize
}
// EndOfUnknownDataSize tries to guess the end of an element which has an unknown data size.
//
// A parent with known data size won't raise an error but not handled as the end of the parent.
func (d *Decoder) EndOfUnknownDataSize(parent Element, el Element) bool {
if parent.DataSize != -1 {
return false
}
if el.ID == IDCRC32 || el.ID == IDVoid { // global elements are child of anything
return false
}
parentSch := parent.Schema
elSch := el.Schema
return !strings.HasPrefix(elSch.Path, parentSch.Path) || len(elSch.Path) == len(parentSch.Path)
}
type Callbacker interface {
// Found is called whenever a new element is found in the target io.Reader.
Found(el Element, offset int64, headerSize int) Callbacker
// Decoded is called whenever an element is decoded from the target io.Reader.
Decoded(el Element, offset int64, headerSize int, val any) Callbacker
}