forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh80 lines, 3.0 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: fill fuzz/corpus/<target>/ from the repo's Ogg fixtures.
3#
4# The Ogg files are the Opus-in-Ogg and Vorbis-in-Ogg fixtures under fixtures/audio and the
5# Xiph Vorbis vectors under fixtures/vectors/vorbis-xiph. The RFC 6716 vectors under
6# fixtures/vectors/opus-rfc6716 are .bit records, not Ogg, and are left to ec-opus's seeds.
7#
8# page_parse, demux, demux_raw each get every file as-is. mapping gets the first 256 bytes of
9# each file, which is where the identification headers sit. roundtrip gets its own layout:
10# a 4-byte header and u16-BE-length-prefixed Opus packets taken from each file's audio pages.
11#
12# Corpus lives under fuzz/corpus, which is git-ignored.
13set -euo pipefail
14
15FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
16ROOT=$(cd -- "$FUZZ_DIR/../../.." && pwd)
17AUDIO="$ROOT/fixtures/audio"
18VORBIS="$ROOT/fixtures/vectors/vorbis-xiph"
19[ -d "$AUDIO" ] || { echo "seed.sh: $AUDIO missing; run scripts/link-fixtures.sh" >&2; exit 2; }
20
21CORPUS="$FUZZ_DIR/corpus"
22mkdir -p "$CORPUS"/{page_parse,demux,demux_raw,mapping,roundtrip}
23
24python3 -I - "$AUDIO" "$VORBIS" "$CORPUS" <<'PY'
25import glob, os, sys
26
27audio, vorbis, corpus = sys.argv[1], sys.argv[2], sys.argv[3]
28files = sorted(glob.glob(os.path.join(audio, "*.ogg")) + glob.glob(os.path.join(audio, "*.opus")))
29files += sorted(glob.glob(os.path.join(vorbis, "*.ogg")))
30if not files:
31 sys.exit("seed.sh: no Ogg fixtures found")
32
33def ogg_packets(data):
34 """Every complete packet of the file, in order, across all serials (header packets included)."""
35 i, cur, out = 0, b"", []
36 while data[i:i + 4] == b"OggS" and i + 27 <= len(data):
37 nseg = data[i + 26]
38 segtab = data[i + 27:i + 27 + nseg]
39 pos = i + 27 + nseg
40 for s in segtab:
41 cur += data[pos:pos + s]
42 pos += s
43 if s < 255:
44 out.append(cur)
45 cur = b""
46 i = pos
47 return out
48
49def opus_roundtrip_seed(packets):
50 # Opus packets only: the first two (OpusHead, OpusTags) go; the rest are audio.
51 buf = bytearray([64, 0, 0, 0]) # page target 64 + 1, mono, pre-skip 0
52 for p in packets[2:2 + 64]:
53 p = p[:65535]
54 if p:
55 buf += len(p).to_bytes(2, "big") + p
56 return bytes(buf)
57
58def write(path, data):
59 with open(path, "wb") as f:
60 f.write(data)
61
62count = {"page_parse": 0, "demux": 0, "demux_raw": 0, "mapping": 0, "roundtrip": 0}
63for path in files:
64 stem = os.path.splitext(os.path.basename(path))[0]
65 data = open(path, "rb").read()
66 write(os.path.join(corpus, "page_parse", stem), data)
67 write(os.path.join(corpus, "demux", stem), data)
68 write(os.path.join(corpus, "demux_raw", stem), data)
69 write(os.path.join(corpus, "mapping", stem), data[:256])
70 count["page_parse"] += 1
71 count["demux"] += 1
72 count["demux_raw"] += 1
73 count["mapping"] += 1
74 packets = ogg_packets(data)
75 if len(packets) >= 3:
76 write(os.path.join(corpus, "roundtrip", stem), opus_roundtrip_seed(packets))
77 count["roundtrip"] += 1
78for name, n in count.items():
79 print(f"{name}: {n} seeds")
80PY