Formats: treat XMA2 PacketSkipCount 0xff as end-of-stream #152

Open
VastyViz wants to merge 1 commit from upstream-pr/152 into main
VastyViz commented 2026-11-25 15:08:44 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by VastyViz on 2026-11-25T15:07:52Z.


Summary

Per the XMA2 packet header specification, the 8-bit PacketSkipCount field uses 0xff (255) as an end-of-stream sentinel ("no further packets belong to this stream"), while 0..=0xfe give the number of interleaved packets of other streams to skip.

Previously, victory-formats/src/xma.rs always advanced by 1 + skip:

  • next_frame: after the last frame of a packet, a packet with skip == 0xff jumped 256 packets forward instead of returning None. Because an XMA input buffer can hold up to 4095 packets, any multi-stream or banked buffer with at least 257 packets past the stream's last packet would continue walking into unrelated stream data instead of ending the buffer and flushing/switching in victory-hle's audio decoder.
  • Cursor::bit: a frame whose bit length ran past a packet with skip == 0xff jumped 256 packets forward and spliced bits from unrelated packets instead of failing to read.

Changes

  • Added END_OF_STREAM: u32 = 0xff and a shared next_packet(index, skip) -> Option<usize> helper in crates/victory-formats/src/xma.rs that returns None on skip == 0xff (and uses checked arithmetic for packet and bit offsets).
  • Updated Cursor::bit and next_frame to route packet advances through next_packet.
  • Added three synthetic unit tests in crates/victory-formats/src/xma.rs:
    • skip_count_ff_ends_the_stream_instead_of_jumping_255_packets (stop on the ending packet even when a valid packet sits 256 packets ahead).
    • skip_count_ff_stops_cross_packet_frame_reads (frame crossing a 0xff packet returns None instead of splicing bits from 256 packets ahead).
    • skip_count_fe_still_skips_254_packets (negative control verifying the largest non-sentinel skip 0xfe still advances 254 packets for both next_frame and cross-packet frame reads).

Verification

  • cargo test -p victory-formats --locked (27 passed, 0 failed)
  • cargo test -p victory-hle --locked -- audio (5 passed, 0 failed)
  • cargo clippy --workspace --all-targets --locked -- -D warnings (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (0 warnings)
  • cargo xtask policy (asset policy: 170 files ok)
  • git diff --check (clean)
<!-- greyfinch-sync:Zettrox/ztrx:issue:6607554832 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/152). Originally posted by [VastyViz](https://github.com/VastyViz) on 2026-11-25T15:07:52Z. --- ## Summary Per the XMA2 packet header specification, the 8-bit `PacketSkipCount` field uses `0xff` (`255`) as an **end-of-stream sentinel** ("no further packets belong to this stream"), while `0..=0xfe` give the number of interleaved packets of other streams to skip. Previously, `victory-formats/src/xma.rs` always advanced by `1 + skip`: - **`next_frame`**: after the last frame of a packet, a packet with `skip == 0xff` jumped 256 packets forward instead of returning `None`. Because an XMA input buffer can hold up to 4095 packets, any multi-stream or banked buffer with at least 257 packets past the stream's last packet would continue walking into unrelated stream data instead of ending the buffer and flushing/switching in `victory-hle`'s audio decoder. - **`Cursor::bit`**: a frame whose bit length ran past a packet with `skip == 0xff` jumped 256 packets forward and spliced bits from unrelated packets instead of failing to read. ## Changes - Added `END_OF_STREAM: u32 = 0xff` and a shared `next_packet(index, skip) -> Option<usize>` helper in `crates/victory-formats/src/xma.rs` that returns `None` on `skip == 0xff` (and uses checked arithmetic for packet and bit offsets). - Updated `Cursor::bit` and `next_frame` to route packet advances through `next_packet`. - Added three synthetic unit tests in `crates/victory-formats/src/xma.rs`: - `skip_count_ff_ends_the_stream_instead_of_jumping_255_packets` (stop on the ending packet even when a valid packet sits 256 packets ahead). - `skip_count_ff_stops_cross_packet_frame_reads` (frame crossing a `0xff` packet returns `None` instead of splicing bits from 256 packets ahead). - `skip_count_fe_still_skips_254_packets` (negative control verifying the largest non-sentinel skip `0xfe` still advances 254 packets for both `next_frame` and cross-packet frame reads). ## Verification - `cargo test -p victory-formats --locked` (27 passed, 0 failed) - `cargo test -p victory-hle --locked -- audio` (5 passed, 0 failed) - `cargo clippy --workspace --all-targets --locked -- -D warnings` (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (0 warnings) - `cargo xtask policy` (`asset policy: 170 files ok`) - `git diff --check` (clean)
xiaotu030202 commented 2026-11-25 16:13:08 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by xiaotu030202 on 2026-11-25T15:23:47Z.


I applied your patch here and ran it, since this is the one crate I can test without game files.

Environment: Windows 11 22631, Ryzen 7 9800X3D, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207 — a different machine and CPU vendor profile from yours, so the result is a second data point rather than a repeat.

Your patch applies clean to main and passes

$ git apply --check 152.diff    -> exit 0
$ cargo test -p victory-formats
test result: ok. 40 passed; 0 failed; 4 ignored     (37 before, +3 from you)

All seven existing xma tests still pass, including packet_headers_and_frame_walk. That one matters: it pads with vec![0xffu8; 2 * PACKET_BYTES], so every padding packet has skip == 0xff already. It asserted c.next == None before your change (because the next packet index ran past the buffer) and still asserts it after (because next_packet says so first). Same answer by a different route — so the change is silent for that test, which is the right outcome but also why it never caught the bug.

I checked your tests are load-bearing

A patch whose tests pass proves little, so I removed the sentinel check and re-ran:

(skip != END_OF_STREAM).then(...).flatten()   ->   index.checked_add(1 + skip as usize)
skip_count_ff_ends_the_stream_instead_of_jumping_255_packets ... FAILED
skip_count_ff_stops_cross_packet_frame_reads ... FAILED
test result: FAILED. 38 passed; 2 failed

skip_count_fe_still_skips_254_packets correctly still passes under that mutation, which is what a good boundary test does — it pins the value next to the sentinel, so it cannot pass for the wrong reason.

What I could not check

I did not verify the 0xff sentinel against an external specification. I looked for one and did not find it in the places I could reach: FFmpeg's xma_parser.c reads buf[packet * 2048 + 3] + 1 with no special case (so it cannot be cited either way), and vgmstream's decoder fetch returned nothing useful from here. The repository itself has no comment or test that states the semantics before your patch.

So the claim rests on your reading of the XMA2 packet header specification. Your three tests are consistent with it and nothing contradicts it — but if you can add a line pointing at the document (section, or the wording of the field), that is the part a reviewer will want and the part I could not supply.

One thing worth noting from the code side: PacketSkipCount in packet() is h & 0xff out of a header where frames is h >> 26 and first_frame_bit is (h >> 11) & 0x7fff — the three fields tile the 32 bits exactly. A padding packet of 0xff bytes therefore has frames = 0x3f and skip = 0xff, which is why an unguarded advance of 256 packets from a padding region walks into whatever follows. That is the shape of the bug you are describing, and it is visible from the struct alone.

Minor, not blocking

next_packet returns Option<usize> and both call sites then do packet(data, next)? — so None from a 0xff and None from an overflowing index are indistinguishable at the caller. That is fine for the behaviour you want (both end the stream), but if the two ever need different handling, a named error would be easier than a shared None. Not a request to change it.

<!-- greyfinch-sync:Zettrox/ztrx:comment:7166776354 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/152#issuecomment-7166776354). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T15:23:47Z. --- I applied your patch here and ran it, since this is the one crate I can test without game files. **Environment:** Windows 11 22631, Ryzen 7 9800X3D, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207 — a different machine and CPU vendor profile from yours, so the result is a second data point rather than a repeat. ## Your patch applies clean to `main` and passes ``` $ git apply --check 152.diff -> exit 0 $ cargo test -p victory-formats test result: ok. 40 passed; 0 failed; 4 ignored (37 before, +3 from you) ``` **All seven existing `xma` tests still pass**, including `packet_headers_and_frame_walk`. That one matters: it pads with `vec![0xffu8; 2 * PACKET_BYTES]`, so every padding packet has `skip == 0xff` already. It asserted `c.next == None` before your change (because the next packet index ran past the buffer) and still asserts it after (because `next_packet` says so first). Same answer by a different route — so the change is silent for that test, which is the right outcome but also why it never caught the bug. ## I checked your tests are load-bearing A patch whose tests pass proves little, so I removed the sentinel check and re-ran: ``` (skip != END_OF_STREAM).then(...).flatten() -> index.checked_add(1 + skip as usize) ``` ``` skip_count_ff_ends_the_stream_instead_of_jumping_255_packets ... FAILED skip_count_ff_stops_cross_packet_frame_reads ... FAILED test result: FAILED. 38 passed; 2 failed ``` `skip_count_fe_still_skips_254_packets` **correctly still passes** under that mutation, which is what a good boundary test does — it pins the value *next to* the sentinel, so it cannot pass for the wrong reason. ## What I could not check **I did not verify the `0xff` sentinel against an external specification.** I looked for one and did not find it in the places I could reach: FFmpeg's `xma_parser.c` reads `buf[packet * 2048 + 3] + 1` with no special case (so it cannot be cited either way), and vgmstream's decoder fetch returned nothing useful from here. The repository itself has no comment or test that states the semantics before your patch. So the claim rests on your reading of the XMA2 packet header specification. **Your three tests are consistent with it and nothing contradicts it** — but if you can add a line pointing at the document (section, or the wording of the field), that is the part a reviewer will want and the part I could not supply. One thing worth noting from the code side: `PacketSkipCount` in `packet()` is `h & 0xff` out of a header where `frames` is `h >> 26` and `first_frame_bit` is `(h >> 11) & 0x7fff` — the three fields tile the 32 bits exactly. A padding packet of `0xff` bytes therefore has `frames = 0x3f` and `skip = 0xff`, which is why an unguarded advance of 256 packets from a padding region walks into whatever follows. That is the shape of the bug you are describing, and it is visible from the struct alone. ## Minor, not blocking `next_packet` returns `Option<usize>` and both call sites then do `packet(data, next)?` — so `None` from a `0xff` and `None` from an overflowing index are indistinguishable at the caller. That is fine for the behaviour you want (both end the stream), but if the two ever need different handling, a named error would be easier than a shared `None`. Not a request to change it.
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/152:upstream-pr/152'
git switch 'upstream-pr/152'

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/152'
git switch 'upstream-pr/152'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/152'
git switch 'upstream-pr/152'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/152'
git switch 'main'
git merge --squash 'upstream-pr/152'
git switch 'main'
git merge --ff-only 'upstream-pr/152'
git switch 'main'
git merge 'upstream-pr/152'
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
Zettrox/ztrx!152
No description provided.