slb: the declared data size is an upper bound, not an exact one

Two comments claimed it is 'honest per sub-wave'. Measured: 5296 of 7586 banks
declare more than the entry holds and none declares exactly what it holds, so
the existing .min(slb.len()) clamp is load-bearing rather than defensive.
Comment-only change; 7 disc tests still pass.
This commit is contained in:
Sylpheed RE agent
2026-08-26 04:12:45 +00:00
parent 0abe908edd
commit b7b29dcb05

View File

@@ -264,7 +264,10 @@ pub fn to_xma_riffs(slb: &[u8]) -> Vec<Vec<u8>> {
}
let mut pos = 0usize;
while let Some(ri) = find(slb, b"RIFF", pos) {
// Parse this sub-wave's fmt + data (declared size is honest per sub-wave).
// Parse this sub-wave's fmt + data. The declared `data` size is an
// UPPER bound, not an exact one: 5 296 of 7 586 banks declare more than
// the entry holds and none declares exactly what it holds, so the clamp
// below is load-bearing (docs/re/structures/slb-data-offset.md).
let Some(fi) = find(slb, b"fmt ", ri) else { break };
let Some(fsz) = le32(slb, fi + 4) else { break };
let Some(fmt_end) = fi.checked_add(8).and_then(|v| v.checked_add(fsz as usize)) else {
@@ -298,10 +301,12 @@ pub fn to_xma_riff(slb: &[u8]) -> Option<Vec<u8>> {
if let Some(ri) = find(slb, b"RIFF", 0) {
// RIFF layout: a `.slb` is an XACT bank of one or more sub-waves, each
// `[seek][RIFF: fmt + Dmmy pad + data][declared_size XMA bytes]`. Take the
// FIRST sub-wave, bounded by its **declared `data` size** (which is
// honest per sub-wave). Decoding to end-of-file instead would append the
// later sub-waves — for multi-take story movies those are ALTERNATE takes,
// which is what made S10S16 play the wrong audio.
// FIRST sub-wave, bounded by its **declared `data` size** which is an
// upper bound only (69.8 % of banks over-declare it, so the clamp
// matters), but still the right boundary to cut at. Decoding to
// end-of-file instead would append the later sub-waves — for multi-take
// story movies those are ALTERNATE takes, which is what made S10S16
// play the wrong audio.
let fi = find(slb, b"fmt ", ri)?;
let fsz = le32(slb, fi + 4)? as usize;
let fmt_end = fi.checked_add(8)?.checked_add(fsz)?;