Skip to content

rsc8_audio: find a bank whose magic straddles the --limit window end - #6361

Open
cynbun wants to merge 1 commit into
ItsNotRexed:mainfrom
cynbun:rsc8-window-magic-straddle
Open

cynbun wants to merge 1 commit into
ItsNotRexed:mainfrom
cynbun:rsc8-window-magic-straddle

Conversation

@cynbun

@cynbun cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #6214 (#6214 (comment)). Our own finding; follows the window-edge work of #1457.

Cause

scan_rpf (tools/rsc8_audio.py:1449 on 561b65a8) read each scan chunk with f.read(min(SCAN_CHUNK_BYTES + 16, scan_end - pos)), i.e. never past the window end. An ADAT magic that starts 1-3 bytes before the end of a --limit window was therefore never matched (buf.find(ADAT_MAGIC, cur, chunk_lim + 4) had no bytes past the end to match against). The next window's _bank_covering then found a bank that started before it (window_starts_in_bank), which by design is not decoded or counted there either. The bank, its RSC8 clips and part of its bytes were in neither window. The stray-RSC8 pass (:1530) lost a header the same way when its magic or version word crossed the end (idx + 8 <= len(buf) failed).

The docstring (:1417) says "adjacent windows add up to the whole archive. A bank that starts inside the window is decoded whole and counted in adat_banks", and docs/rsc8_audio.md:372 says to "split a large archive with --start/--limit and add the summaries".

Fix

Read the 16-byte lookahead past the window end (up to EOF) and bound chunk_lim by the window instead: chunk_lim = min(len(buf), SCAN_CHUNK_BYTES, scan_end - pos). Matches are still taken only when they start before chunk_lim, so nothing that starts past the window is counted. Without a window (scan_end == sz) the reads are unchanged.

Tests

Tested on Linux with Python 3.14.8, synthetic inputs only, no game files, no GPU needed.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

scan_rpf read each chunk only up to the window end, so an ADAT magic
that starts 1-3 bytes before it was never matched; the next window then
saw a bank that started before it and did not count it either. The same
held for a stray RSC8 header whose magic or version word crossed the
end. Two adjacent windows therefore did not add up to the archive, as
the docstring and docs/rsc8_audio.md say they do.

Read the 16-byte lookahead past the window end (up to EOF) and bound
chunk_lim by the window instead, so only a match that starts inside the
window is taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested bc31fd3 on Windows 11, Python 3.12 (CPU only): pass

  • python -I tests/test_rsc8_audio.py: 38 tests OK. Negative control with tools/rsc8_audio.py from the merge base: test_window_end_inside_a_magic fails in 10 subtests (cut 257-259 and 5285-5291 among them).
  • Own property check, SCAN_CHUNK_BYTES set to 64, 100 and 4096 so the chunk edge also moves: synthetic archive of 3 banks and 3 stray RSC8 headers with random zero gaps (23.2 kB), path with a space. For every cut point 0..len (about 70,000 pairs), scan_rpf(limit=cut) + scan_rpf(start=cut) equals the full scan for adat_banks, adat_bytes, rsc8_in_adat, rsc8_outside_adat and span: 0 mismatches. Full scan: 3 banks, 3 in ADAT, 3 outside.
  • Not done: no real archive, no window larger than one chunk with real banks.

AI-assisted (Claude Code).

@AsgerCode

Copy link
Copy Markdown

Tested bc31fd3 on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): pass

What I ran (the earlier review checked two-window splits limit=cut + start=cut by summed counts; this adds windows that have both --start and --limit, bank identity per window, the CLI path, and the merge with current main):

Three-window partitions with bank identity: I built a synthetic archive with the test file's build_synthetic_awc / build_synthetic_rsc8. It is 22,704 bytes, with banks at 256, 5220, 12888 and 17788 (12888 and 17788 are adjacent, no gap) and a stray RSC8 header at 10152. Each bank holds one clip, so records[*].bank_offset names the bank. Window ends E: each bank magic -1..+4 and the stray header -1..+8. Window starts S: 0, inside bank 1, E-1..E-5, each bank magic -1..+3, and E minus one and two chunks. That gives the partitions [0,S) [S,E) [E,EOF). For each partition I checked these things:

  • The summed adat_banks, adat_bytes, rsc8_in_adat, rsc8_outside_adat and span equal the full scan.
  • No bank_offset occurs in two windows, and none is missing.
  • Each window reports exactly the banks and the stray header that start in [S,E).
SCAN_CHUNK_BYTES partitions PR merge base
32 MiB (default) 546 0 problems 309 with a bank missing, 363 with a bank in the wrong window, 131 with the stray header in the wrong window
4096 / 100 / 64 / 16 596 / 614 / 614 / 614 0 problems 324-333 with a bank missing

No partition reported a bank twice, on the PR or on the merge base. The merged tree also gives 0 problems.

CLI, rsc8_audio.py synth.rpf r.json --start 4000 --limit L (the window starts inside bank 256), then --start E. Bank 5220 is the bank under test:

Magic of bank 5220 vs. window end E PR: middle window / next window merge base: middle / next
starts 3, 2, 1 bytes before E [5220], window_starts_in_bank 256 / [12888, 17788], window_starts_in_bank 5220 [] / [12888, 17788]: bank 5220 in neither
starts exactly at E [] / [5220, 12888, 17788] same
starts 1 byte after E [] / [5220, 12888, 17788] same

All exit 0. On the PR, adat_bytes of the middle window grows by 1-3 bytes in the first three rows (1157-1159), and the next window loses the same bytes.

A bare .awc with a window goes through scan_rpf too. --limit 1..5 owns the bank (1 bank, 1 record), and --start at the same cut reports window_starts_in_bank 0. adat_bytes adds up to the bank size (4900).

Overlap: 10 other open PRs change tools/rsc8_audio.py (#4017, #4110, #4355, #5518, #5608, #6007, #6780, #7266, #8288, #8947). None changes the chunk read or chunk_lim, so none does the same fix. Each merges cleanly with this PR, except #4110, which already conflicts with main in this file.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants