Formats: treat XMA2 PacketSkipCount 0xff as end-of-stream #152
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/152"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Zettrox/ztrx. Originally posted by VastyViz on 2026-11-25T15:07:52Z.
Summary
Per the XMA2 packet header specification, the 8-bit
PacketSkipCountfield uses0xff(255) as an end-of-stream sentinel ("no further packets belong to this stream"), while0..=0xfegive the number of interleaved packets of other streams to skip.Previously,
victory-formats/src/xma.rsalways advanced by1 + skip:next_frame: after the last frame of a packet, a packet withskip == 0xffjumped 256 packets forward instead of returningNone. 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 invictory-hle's audio decoder.Cursor::bit: a frame whose bit length ran past a packet withskip == 0xffjumped 256 packets forward and spliced bits from unrelated packets instead of failing to read.Changes
END_OF_STREAM: u32 = 0xffand a sharednext_packet(index, skip) -> Option<usize>helper incrates/victory-formats/src/xma.rsthat returnsNoneonskip == 0xff(and uses checked arithmetic for packet and bit offsets).Cursor::bitandnext_frameto route packet advances throughnext_packet.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 a0xffpacket returnsNoneinstead of splicing bits from 256 packets ahead).skip_count_fe_still_skips_254_packets(negative control verifying the largest non-sentinel skip0xfestill advances 254 packets for bothnext_frameand 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)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
mainand passesAll seven existing
xmatests still pass, includingpacket_headers_and_frame_walk. That one matters: it pads withvec![0xffu8; 2 * PACKET_BYTES], so every padding packet hasskip == 0xffalready. It assertedc.next == Nonebefore your change (because the next packet index ran past the buffer) and still asserts it after (becausenext_packetsays 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_count_fe_still_skips_254_packetscorrectly 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
0xffsentinel against an external specification. I looked for one and did not find it in the places I could reach: FFmpeg'sxma_parser.creadsbuf[packet * 2048 + 3] + 1with 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:
PacketSkipCountinpacket()ish & 0xffout of a header whereframesish >> 26andfirst_frame_bitis(h >> 11) & 0x7fff— the three fields tile the 32 bits exactly. A padding packet of0xffbytes therefore hasframes = 0x3fandskip = 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_packetreturnsOption<usize>and both call sites then dopacket(data, next)?— soNonefrom a0xffandNonefrom 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 sharedNone. Not a request to change it.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.