forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

fuzz_api.rs70 lines, 2.4 KiBHistoryRawDownload
1//! Entry points the fuzz targets reach below the public API: the EBML walk, lacing,
2//! block headers, `Tracks` (with `CodecPrivate` and `ContentEncodings`), and the
3//! `Cues`/`SeekHead` index. Each function is a thin call into the module that owns
4//! the parser. Hidden from the docs; not a stable interface.
5
6use std::ops::Range;
7
8use ec_core::{Result, TimeBase};
9
10use crate::{demux, ebml};
11
12/// How deep [`ebml_walk`] descends into nested masters.
13const WALK_DEPTH: u32 = 8;
14
15/// Walks `buf` as EBML: the head element, then every child of every master up to
16/// [`WALK_DEPTH`], each payload read as an unsigned, float and string. Returns the
17/// number of elements visited.
18pub fn ebml_walk(buf: &[u8]) -> usize {
19 let _ = ebml::header(buf);
20 let _ = ebml::vint(buf, true);
21 let _ = ebml::vint(buf, false);
22 walk(buf, 0)
23}
24
25fn walk(buf: &[u8], depth: u32) -> usize {
26 let mut count = 0;
27 for (_, range) in ebml::Elements::new(buf) {
28 let body = &buf[range];
29 let _ = (
30 ebml::uint_of(body),
31 ebml::float_of(body),
32 ebml::string_of(body),
33 );
34 count += 1;
35 if depth < WALK_DEPTH {
36 count += walk(body, depth + 1);
37 }
38 }
39 count
40}
41
42/// `buf` as the payload of a laced block with the given lacing (1 Xiph, 2 fixed,
43/// 3 EBML). Returns the frame ranges.
44pub fn lace(buf: &[u8], lacing: u8) -> Result<Vec<Range<usize>>> {
45 demux::lace(buf, 0, buf.len(), lacing)
46}
47
48/// `buf` as a whole `SimpleBlock` or `Block` body: track number, timecode, flags and
49/// the lace. Returns the number of frames it holds.
50pub fn block(buf: &[u8]) -> Result<usize> {
51 demux::parse_block(buf, 0..buf.len()).map(|head| head.frames.len())
52}
53
54/// `buf` as a `Tracks` body, with `scale_ns` as the file's `TimestampScale`. Returns
55/// the number of streams that resolve.
56pub fn tracks(buf: &[u8], scale_ns: u64) -> Result<usize> {
57 let time_base = TimeBase::try_new(scale_ns as i64, 1_000_000_000)?;
58 demux::parse_tracks(buf, scale_ns, time_base, Some(1_000)).map(|(streams, _)| streams.len())
59}
60
61/// `buf` as a `Cues` body in a segment that starts at `segment`. Returns the cue
62/// points that resolve.
63pub fn cues(buf: &[u8], segment: u64) -> usize {
64 demux::parse_cues(buf, segment).len()
65}
66
67/// `buf` as a `SeekHead` body. Returns the entries that resolve.
68pub fn seek_head(buf: &[u8]) -> usize {
69 demux::seek_positions(buf).len()
70}