forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh49 lines, 2.1 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: fill fuzz/corpus/<target>/ from the repo's AC-3 and E-AC-3 fixtures (root fixtures/audio).
3#
4# decode_stream gets each fixture truncated to its first few syncframes (5 frames).
5# parse_header gets the first 256 bytes of each fixture, which covers syncinfo and the BSI.
6#
7# Frame boundaries are found by walking the 0x0B77 syncwords, so a stray match inside a frame
8# only makes a seed slightly longer or shorter; it is still a valid starting point.
9#
10# Corpus lives under fuzz/corpus, which is git-ignored.
11set -euo pipefail
12
13FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
14FIXTURES=$(cd -- "$FUZZ_DIR/../../.." && pwd)/fixtures/audio
15[ -d "$FIXTURES" ] || { echo "seed.sh: $FIXTURES missing; run scripts/link-fixtures.sh" >&2; exit 2; }
16
17FILES=("$FIXTURES"/ac3-*.ac3 "$FIXTURES"/eac3-*.eac3 "$FIXTURES"/ac3dep-*.eac3)
18[ -e "${FILES[0]}" ] || { echo "seed.sh: no ac3-*.ac3 or eac3-*.eac3 fixtures in $FIXTURES" >&2; exit 2; }
19
20STREAM_DIR="$FUZZ_DIR/corpus/decode_stream"
21HEADER_DIR="$FUZZ_DIR/corpus/parse_header"
22mkdir -p "$STREAM_DIR" "$HEADER_DIR"
23
24python3 -I - "$FIXTURES" "$STREAM_DIR" "$HEADER_DIR" <<'PY'
25import glob, os, sys
26
27fixtures, stream_dir, header_dir = sys.argv[1], sys.argv[2], sys.argv[3]
28FRAMES = 5
29
30paths = sorted(glob.glob(os.path.join(fixtures, "ac3-*.ac3")) +
31 glob.glob(os.path.join(fixtures, "eac3-*.eac3")) +
32 glob.glob(os.path.join(fixtures, "ac3dep-*.eac3")))
33for path in paths:
34 name = os.path.splitext(os.path.basename(path))[0]
35 data = open(path, "rb").read()
36 starts = []
37 i = 0
38 while i + 2 <= len(data) and len(starts) <= FRAMES:
39 if data[i] == 0x0B and data[i + 1] == 0x77:
40 starts.append(i)
41 i += 2
42 else:
43 i += 1
44 # Keep the first FRAMES frames: cut at the start of frame FRAMES (or the file end).
45 end = starts[FRAMES] if len(starts) > FRAMES else len(data)
46 open(os.path.join(stream_dir, name), "wb").write(data[:end])
47 open(os.path.join(header_dir, name), "wb").write(data[:256])
48print(f"seeded {len(os.listdir(stream_dir))} decode_stream and {len(os.listdir(header_dir))} parse_header seeds")
49PY