|
6 | 6 | package ebml |
7 | 7 |
|
8 | 8 | import ( |
9 | | - "encoding/binary" |
10 | 9 | "encoding/xml" |
11 | 10 | "errors" |
12 | 11 | "fmt" |
@@ -168,19 +167,18 @@ func NewDecoder(r io.ReadSeeker) *Decoder { |
168 | 167 | // Next reads the following element id and data size. |
169 | 168 | // It must be called before Decode. |
170 | 169 | func (d *Decoder) Next() (el Element, n int, err error) { |
171 | | - start := d.r.InputOffset() |
172 | 170 | el.ID, err = d.r.ReadElementID() |
173 | 171 | if err != nil { |
174 | | - return Element{}, int(d.r.InputOffset() - start), err |
| 172 | + return Element{}, n, err |
175 | 173 | } |
176 | | - d.r.Release() |
| 174 | + n += d.r.Release() |
177 | 175 | el.DataSize, err = d.r.ReadElementDataSize() |
178 | 176 | if err != nil { |
179 | | - return Element{}, int(d.r.InputOffset() - start), err |
| 177 | + return Element{}, n, err |
180 | 178 | } |
181 | | - d.r.Release() |
| 179 | + n += d.r.Release() |
182 | 180 | d.el = &el |
183 | | - return el, int(d.r.InputOffset() - start), err |
| 181 | + return el, n, err |
184 | 182 | } |
185 | 183 |
|
186 | 184 | // NextOf reads the following element id and data size |
@@ -260,71 +258,4 @@ func (d *Decoder) EndOfUnknownDataSize(parent Element, el Element) (bool, error) |
260 | 258 | return !strings.HasPrefix(nextDef.Path, def.Path) || len(nextDef.Path) == len(def.Path), nil |
261 | 259 | } |
262 | 260 |
|
263 | | -type UnknownElementError struct { |
264 | | - el Element |
265 | | -} |
266 | | - |
267 | | -func (e UnknownElementError) Error() string { |
268 | | - return fmt.Sprintf("ebml: unknown element: %v", e.el.ID) |
269 | | -} |
270 | | - |
271 | | -var ErrInvalidVINTLength = fmt.Errorf("ebml: invalid length descriptor") |
272 | | - |
273 | | -// ReadElementID reads an Element ID based on |
274 | | -// https://datatracker.ietf.org/doc/html/rfc8794#section-5 |
275 | | -func ReadElementID(r io.Reader, maxIDLength uint) (id schema.ElementID, n int, err error) { |
276 | | - b := make([]byte, maxIDLength) |
277 | | - // TODO: EBMLMaxIDLength can be greater than 8 |
278 | | - // https://datatracker.ietf.org/doc/html/rfc8794#section-11.2.4 |
279 | | - n, err = r.Read(b[:1]) |
280 | | - if err != nil { |
281 | | - return 0, n, err |
282 | | - } |
283 | | - w := vintOctetLength(b) |
284 | | - if w > len(b) { |
285 | | - return 0, 1, ErrInvalidVINTLength |
286 | | - } |
287 | | - if w > 1 { |
288 | | - m, err := r.Read(b[1:w]) |
289 | | - n += m |
290 | | - if err != nil { |
291 | | - return 0, n, err |
292 | | - } |
293 | | - } |
294 | | - data := vintData(b, w) |
295 | | - if vintDataAllOne(data, w) { |
296 | | - return 0, n, errors.New("VINT_DATA MUST NOT be set to all 1") |
297 | | - } |
298 | | - i := binary.BigEndian.Uint64(dataPad(b[:w])) |
299 | | - return schema.ElementID(i), n, nil |
300 | | -} |
301 | | - |
302 | | -func dataPad(b []byte) []byte { |
303 | | - db := make([]byte, 8) |
304 | | - copy(db[8-len(b):], b) |
305 | | - return db |
306 | | -} |
307 | | - |
308 | | -// ReadElementDataSize reads an Element ID based on |
309 | | -// https://datatracker.ietf.org/doc/html/rfc8794#section-6 |
310 | | -func ReadElementDataSize(r io.Reader, maxSizeLength uint) (ds int64, n int, err error) { |
311 | | - b := make([]byte, maxSizeLength) |
312 | | - // TODO: EBMLMaxSizeLength can be greater than 8 |
313 | | - // https://datatracker.ietf.org/doc/html/rfc8794#section-11.2.5 |
314 | | - n, err = r.Read(b[:1]) |
315 | | - if err != nil { |
316 | | - return 0, n, err |
317 | | - } |
318 | | - w := vintOctetLength(b) |
319 | | - m, err := r.Read(b[1:w]) |
320 | | - n += m |
321 | | - if err != nil { |
322 | | - return 0, n, err |
323 | | - } |
324 | | - d := vintData(b, w) |
325 | | - if vintDataAllOne(d, w) { |
326 | | - return -1, n, nil |
327 | | - } |
328 | | - i := binary.BigEndian.Uint64(dataPad(d)) |
329 | | - return int64(i), n, nil |
330 | | -} |
| 261 | +var ErrInvalidVINTLength = ebmltext.ErrInvalidVINTWidth |
0 commit comments