forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh76 lines, 2.8 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: fill fuzz/corpus/<target>/ from the RFC 6716 test vectors and the Ogg-Opus fixtures.
3#
4# decode_packet: each packet stream is re-packed as the target's layout (config byte, then
5# u16 BE length + packet). Test vectors are .bit records (u32 BE length, u32 BE final range,
6# payload). Each vector is written once as mono (config 8) and once as stereo (config 9). The
7# Ogg-Opus fixtures are stereo or mono (the 5.1 file is replayed as stereo). Only the first
8# MAX_PKTS packets go into each seed, so no seed is megabytes long.
9# parse_toc: the first packet of each stream, as-is (its TOC byte is the first byte).
10#
11# Corpus lives under fuzz/corpus, which is git-ignored.
12set -euo pipefail
13
14FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
15ROOT=$(cd -- "$FUZZ_DIR/../../.." && pwd)
16VECTORS="$ROOT/fixtures/vectors/opus-rfc6716/opus_testvectors"
17AUDIO="$ROOT/fixtures/audio"
18[ -d "$VECTORS" ] || { echo "seed.sh: $VECTORS missing; run scripts/link-fixtures.sh" >&2; exit 2; }
19
20PKT_DIR="$FUZZ_DIR/corpus/decode_packet"
21TOC_DIR="$FUZZ_DIR/corpus/parse_toc"
22mkdir -p "$PKT_DIR" "$TOC_DIR"
23
24python3 -I - "$VECTORS" "$AUDIO" "$PKT_DIR" "$TOC_DIR" <<'PY'
25import glob, os, struct, sys
26
27vectors, audio, pkt_dir, toc_dir = sys.argv[1:5]
28MAX_PKTS = 40
29
30def rfc_bit_packets(data):
31 i, out = 0, []
32 while i + 8 <= len(data):
33 n, _final = struct.unpack(">II", data[i:i + 8])
34 i += 8
35 out.append(data[i:i + n])
36 i += n
37 return out
38
39def ogg_packets(data):
40 i, cur, out = 0, b"", []
41 while data[i:i + 4] == b"OggS":
42 nseg = data[i + 26]
43 segtab = data[i + 27:i + 27 + nseg]
44 pos = i + 27 + nseg
45 for s in segtab:
46 cur += data[pos:pos + s]
47 pos += s
48 if s < 255:
49 out.append(cur)
50 cur = b""
51 i = pos
52 return out[2:] # drop OpusHead and OpusTags
53
54def write_pkt_seed(path, cfg, packets):
55 buf = bytearray([cfg])
56 for p in packets[:MAX_PKTS]:
57 p = p[:65535]
58 buf += len(p).to_bytes(2, "big") + p
59 open(path, "wb").write(bytes(buf))
60
61def write_toc_seed(path, packets):
62 first = next((p for p in packets if p), b"")
63 open(path, "wb").write(first[:1500])
64
65streams = []
66for path in sorted(glob.glob(os.path.join(vectors, "testvector*.bit"))):
67 streams.append((os.path.basename(path)[:-4], rfc_bit_packets(open(path, "rb").read())))
68for path in sorted(glob.glob(os.path.join(audio, "opus-ogg-*.opus"))):
69 streams.append((os.path.basename(path)[:-5], ogg_packets(open(path, "rb").read())))
70
71for stem, pkts in streams:
72 write_pkt_seed(os.path.join(pkt_dir, f"{stem}-mono"), 8, pkts)
73 write_pkt_seed(os.path.join(pkt_dir, f"{stem}-stereo"), 9, pkts)
74 write_toc_seed(os.path.join(toc_dir, stem), pkts)
75print(f"seeded {len(os.listdir(pkt_dir))} decode_packet and {len(os.listdir(toc_dir))} parse_toc seeds")
76PY