formats/xma: handle zero-frame continuation/empty packets in next_frame #627

Open
AscilCH wants to merge 1 commit from upstream-pr/627 into main
AscilCH commented 2026-11-25 03:26:11 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T03:26:11Z.


Problem

Two bugs in xma::next_frame (crates/victory-formats/src/xma.rs):

  1. Cross-packet frame ending inside a frames == 0 continuation packet: if a frame started in packet index and finished inside a subsequent packet end_pkt whose header has frames == 0 (tail bits followed by 0xFF padding), neither end.is_multiple_of(PACKET_BITS) nor end / PACKET_BITS == index held. next_frame returned Some(end) inside 0xFF padding, causing the next read_frame to see length 0x7fff and silently truncate the rest of the audio stream.
  2. Advancing into a frames == 0 packet (first_frame_bit == 0x7fff): in XMA2, packets starting no frames set first_frame_bit = 0x7fff (32767 > PACKET_BITS - HEADER_BITS). next_frame unconditionally computed next * PACKET_BITS + HEADER_BITS + header.first_frame_bit, jumping ~2 packets forward right into another packet's header and bypassing that packet's skip count.

Fix

  • Trigger packet advancement when end / PACKET_BITS != index && packet(data, end_pkt)?.frames == 0 in addition to packet-boundary and intra-packet last-frame cases.
  • Follow the skip chain across any frames == 0 packets until finding a packet with frames > 0, checking HEADER_BITS + first_frame_bit < PACKET_BITS (and on the starting packet as well) with checked arithmetic.
  • Add synthetic unit test next_frame_handles_zero_frame_continuation_and_empty_packets covering continuation packets, empty packets with skip, and invalid first_frame_bit.

Verification

  • cargo test -p victory-formats --locked xma: 6 passed, 0 failed, 2 ignored (external dumps)
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6596492290 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/627). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T03:26:11Z. --- ## Problem Two bugs in `xma::next_frame` (`crates/victory-formats/src/xma.rs`): 1. **Cross-packet frame ending inside a `frames == 0` continuation packet**: if a frame started in packet `index` and finished inside a subsequent packet `end_pkt` whose header has `frames == 0` (tail bits followed by `0xFF` padding), neither `end.is_multiple_of(PACKET_BITS)` nor `end / PACKET_BITS == index` held. `next_frame` returned `Some(end)` inside `0xFF` padding, causing the next `read_frame` to see length `0x7fff` and silently truncate the rest of the audio stream. 2. **Advancing into a `frames == 0` packet (`first_frame_bit == 0x7fff`)**: in XMA2, packets starting no frames set `first_frame_bit = 0x7fff` (`32767 > PACKET_BITS - HEADER_BITS`). `next_frame` unconditionally computed `next * PACKET_BITS + HEADER_BITS + header.first_frame_bit`, jumping ~2 packets forward right into another packet's header and bypassing that packet's `skip` count. ## Fix - Trigger packet advancement when `end / PACKET_BITS != index && packet(data, end_pkt)?.frames == 0` in addition to packet-boundary and intra-packet last-frame cases. - Follow the `skip` chain across any `frames == 0` packets until finding a packet with `frames > 0`, checking `HEADER_BITS + first_frame_bit < PACKET_BITS` (and on the starting packet as well) with checked arithmetic. - Add synthetic unit test `next_frame_handles_zero_frame_continuation_and_empty_packets` covering continuation packets, empty packets with `skip`, and invalid `first_frame_bit`. ## Verification - `cargo test -p victory-formats --locked xma`: 6 passed, 0 failed, 2 ignored (external dumps) - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/627:upstream-pr/627'
git switch 'upstream-pr/627'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/627'
git switch 'upstream-pr/627'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/627'
git switch 'upstream-pr/627'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/627'
git switch 'main'
git merge --squash 'upstream-pr/627'
git switch 'main'
git merge --ff-only 'upstream-pr/627'
git switch 'main'
git merge 'upstream-pr/627'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!627
No description provided.