SGD5: a marker inside a record's packed bytes is not another record #273

Closed
github-bot wants to merge 72 commits from gh-pr/273/head into gh-pr/273/base
Owner

@⁠SkippySteve on GitHub · 2026-11-24 01:58 UTC · original
SkippySteve/sgd5-nested-marker → main · +4677 −27 · 8 files · 72 commits · closed without merging

sgd5::records matched every 0x000DDCE7 marker in the file, including ones inside an already-found record's packed bytes. Deflate stored blocks carry the container's bytes as they are, so a container that holds a record-shaped run of bytes was reported as an extra record (and the shader CLI would try to unpack it).

The search now resumes after each record's packed bytes. A marker that does not form a record still advances by one byte, as before.

Game tests: every known build's hash is one identify() can match (was #303)

KNOWN_BUILDS is matched against the hex BLAKE3 that run.rs computes, so an entry with a typo, a truncated hash, stray whitespace or a duplicate would silently never identify the build it names. Three tests in crates/victory-game/src/lib.rs check that each entry is 64 lowercase hex digits with unique label and hash, that identify() finds each build by its own hash in either case, and that an unknown digest, an empty string or non-hex text identifies nothing. The list is empty today, so the first two guard future entries. Test-only; no game data.

This PR now carries #303, which is closed in its favour.

A record's header cannot sit inside the previous record

After a record, the search resumed right at the end of its packed bytes, so a marker found there read its NUL, kind and n from the last bytes of the previous record (and its name from further back). Bytes after the end of a deflate stream are not checked, so this produced a phantom record. The search now starts 6 bytes after the previous record (or the magic), and a record's header and name are read only from bytes after it.

  • New header_inside_a_record_is_not_another_record: before [Some("PS_first"), None], after [Some("PS_first")].
  • New empty_container_unpacks and damaged_file_yields_disjoint_records (every single-byte change to a two-record file, 8 values each: no panic, records in order, inside the file, not overlapping).

The shader survey does not count a stored container twice

Deflate stored blocks carry a record's container as it is, so victory shader's raw DXBC scan (apps/victory/src/shader.rs, sources) found it inside the record's packed bytes and counted it again, next to the unpacked record. sgd5::Record::span (marker to end of packed stream) and sgd5::in_record (binary search over the records, which are in file order and disjoint) let the scan skip those offsets. Containers between or after records, and in files with no SGD5 magic, are still found as before.

  • New offsets_inside_records: a level-0 record holding a DXBC run, then DXBC between and after records. The stored one is inside a record; the magic, the gap, the tail and each record's header are not.

A truncated record does not expose the records it stores

A record whose packed bytes run past the end of the file (a cut-off file) was dropped, but the search then went on one byte at a time through its bytes, so a record stored as is inside it (deflate stored blocks) was found and reported. The search now ends at such a record: every byte after its marker belongs to it. Sizes are added with checked_add.

  • New truncated_record_hides_the_records_it_stores: the file cut right after the stored inner record. Before [PS_first, PS_inner], after [PS_first].

A marker with sizes deflate cannot produce is not a record

Bytes between records (a container stored as is, say) that read as a marker with a huge packed size were taken for a truncated record, and a truncated record ends the search, so every record after them was lost. A record's packed size may now be at most what deflate can take for its stated unpacked size (9 bits a byte for fixed codes, which also covers stored blocks' 5 bytes per 65535) plus 4096 bytes of slack, since the stream's end is not checked.

  • New marker_with_impossible_sizes_does_not_end_the_search: FFFFFFFF E7DC0D00 FFFFFFFF 10000000 before a record. Before [], after [PS_after].
  • New packed_size_bound_holds_for_every_level: incompressible input of 0 to 200000 bytes at miniz levels 0 to 10 never exceeds the bound.

A marker running past the end is truncated only if a deflate stream starts there

Bytes between records (a container stored as is, say) that read as a marker with sizes that fit the bound above, but whose packed bytes run past the end of the file, were still taken for a truncated record, which ends the search and loses every record after them. Such a record now counts as truncated only if its bytes so far are a deflate stream of the stated unpacked size: inflating them runs out of input mid-stream, or the stream ends having produced exactly that many bytes. Otherwise the marker is not a record and the search goes on one byte later. A truncated record that is a real one (a cut-off file) still ends the search as before.

  • New marker_running_past_the_end_without_a_stream_does_not_end_the_search: a 64 KiB/64 KiB marker followed by a reserved deflate block type, between two records. Before [PS_first], after [PS_first, PS_after].
  • New truncated_stream_is_told_from_noise: every prefix of a 4000-byte stream at miniz levels 0, 1, 6 and 10 starts a stream; the whole stream does only for its own size; a reserved block type and a stored block whose length check fails do not.

Seeded random files yield exactly the planted records

New random_files_yield_exactly_the_planted_records (200 seeds): 1 to 6 records with gaps of noise and near-miss markers (sizes that disagree, sizes no deflate stream could have) between them, half of them stored as is (level 0) and holding a record and a DXBC magic of their own. records() returns exactly the planted records, with their offsets, names and containers, and in_record() agrees with a linear scan of their spans at every offset. Reverting the skip over packed bytes or the packed-size bound makes it fail. Test-only.

An empty name is no name

A length byte of 1 right before the NUL read as the name "", so any 01 00 before a record's kind (noise, or a nameless record) named it Some(""), and victory shader labelled it with nothing instead of ?. Such a name is now None.

  • New empty_name_is_no_name: before [Some(""), Some("PS_named")], after [None, Some("PS_named")].
  • New archive_with_two_sgd5_files_yields_both: two SGD5 files in one archive, the second magic in a gap between them; both files' records are found. Test-only.

The shader survey does not count a container kept in another one twice

victory shader's raw DXBC scan accepted every magic that parses, so a container kept in a part of another container stored as is (outside any SGD5 record) was counted a second time. New victory_shader::dxbc::find_all returns containers stored as is, in file order, bounded to their stated size, and skips a magic that lies inside a found container's parts. Only parts count: a magic in junk right before a real container can read its size and part count from the real header and parse with no parts, and it must not hide the real one. shader.rs sources uses it, passing sgd5::in_record as the skip. victory-shader now depends on the workspace memchr.

  • New container_in_a_part_is_not_another_container: a container holding another in an ILDB part, then one after a gap. Found: the outer and the last; skipping the outer exposes the inner.
  • New header_reading_into_a_container_does_not_hide_it and finds_nothing_in_noise.
  • cargo test -p victory-shader (59) passes; cargo xwin check/clippy -p victory --target x86_64-pc-windows-msvc are clean.

Records and the raw scan, tested together as the survey runs them

victory shader's sources combines sgd5::records, sgd5::in_record and dxbc::find_all, but the CLI does not build on Linux, so nothing ran that combination. New crates/victory-game/tests/survey.rs (victory-shader as a dev-dependency) does the same thing sources does:

  • stored_record_container_is_counted_once: a level-0 record's container counts as the record only, not also as a raw container; the container between records is raw; without the SGD5 magic the stored container is found as is.
  • random_archives_survey_each_container_once (200 seeds): another file's container before the magic, records (some stored as is, holding a container, a record and a container of their own), and gaps with noise, magics that do not parse, and containers (some keeping another in a part). Exactly the planted records and top-level raw containers are found, each once, and no raw container overlaps a record.
  • Passing |_| false as the skip, or dropping find_all's part check, makes them fail. Test-only; synthetic bytes. cargo test -p victory-game (20 + 2) and cargo clippy -p victory-game --all-targets -- -D warnings pass.

A marker with too few packed bytes for its size is not a record

A marker whose packed size is less than any deflate stream of its stated unpacked size needs (2 bytes, and no fewer than unpacked / 1032) was yielded as a record that never unpacks, and with any packed bytes its span covered the next record's length byte, so that record lost its name.

  • New packed_size_lower_bound_holds_for_every_level (zeros up to 4 MiB at every miniz level), marker_with_too_few_packed_bytes_does_not_hide_the_next_record (before [PS_first, None, PS_after] or [PS_first, None, None], after [PS_first, PS_after]), smallest_containers_are_records.

Bytes that are no deflate stream do not hide the record they cover

Bytes between records that read as a marker with sizes inside both bounds and inside the file were yielded as a corrupt record, and the search resumed after their packed bytes, so a real record whose header they covered was lost. If a record whose packed bytes are a deflate stream starts inside such a marker's span, it now wins and the marker is skipped. A corrupt record that covers no other record is still yielded (unpack() is None), so victory shader still reports it. Streams are inflated only when a span covers another record's header.

  • New marker_without_a_stream_does_not_hide_the_record_it_covers: before [PS_first, None], after [PS_first, PS_after]. Disabling the check makes it fail.
  • New corrupt_record_is_still_a_record: a damaged record and the one after it are both yielded.
  • cargo test -p victory-game (25 + 2) and cargo clippy -p victory-game --all-targets -- -D warnings pass.

Markers laid over one another are each looked at once

A marker whose span covers a stream record, but whose own bytes are no stream, is skipped and the search goes on one byte later. Each such marker scanned its whole span again and inflated every stream record it found, so a file with many markers that each cover the rest of the file took n² work or more: 8000 of them (150 KB) took 43 s. records() now remembers which markers start stream records as the search moves forward through the file, so each marker is looked at once. The records found are unchanged (all earlier tests, including the random-file test, pass as before).

  • New nested_markers_without_streams_take_linear_work: 8000 such markers, then a real record. Before: 43 s (fails its 5 s bound); after: 0.03 s, and only PS_real is found.
  • cargo test -p victory-game (26 + 2) and cargo clippy -p victory-game --all-targets -- -D warnings pass.

Chained markers do not make the stream check read the whole file

Every marker that might be a record has its bytes checked for the start of a deflate stream (when its packed bytes run past the end, when it covers another record, and for each record it covers). Stored blocks that each end where the next marker's stored block begins make every marker's bytes a valid stream through all the markers after it, so each check inflated the rest of the chain: 16000 markers (1 MB) took 2.5 s, 32000 (2 MB) took 9 s, growing as n². starts_stream now reads at most 4096 packed bytes and makes at most 4096 unpacked ones (PROBE). A stream that ends inside the probe must still have the stated size; one that goes on past it counts as a stream.

  • New chained_markers_inside_the_file_take_linear_work: 32000 markers whose packed bytes end at the end of the file. Before: 9 s (fails its 5 s bound); after: milliseconds, same result (one corrupt record spanning them).
  • New chained_markers_past_the_end_take_linear_work: the same chain with packed sizes past the end. Before: 9 s; after: milliseconds. Trade-off: the chain is valid deflate for far longer than the probe, so its first marker is now taken for a truncated record and ends the search (before, the record after the chain was found, after 9 s). Noise that is no deflate stream within 4 KiB still does not end the search (marker_running_past_the_end_without_a_stream_does_not_end_the_search is unchanged).
  • cargo test -p victory-game (28 + 2), cargo clippy -p victory-game --all-targets -- -D warnings and cargo fmt --check pass.

Linear-work tests count work instead of timing it

The three linear-work tests bounded records() by wall clock (5 s), so a slow or loaded machine could fail them and a fast one could hide a regression. A test-only, per-thread counter in starts_stream now records the stream checks made and the packed bytes they read, and the tests bound those: at most two checks per marker, each reading at most PROBE bytes. The code under test is unchanged.

  • Checked against the regressions they guard: with the Streams memo disabled, nested_markers_without_streams_take_linear_work makes 31.6M checks for 8000 markers (bound 16002) and fails; with the probe removed, both chained-marker tests read about 2 MB per check and fail.
  • cargo test -p victory-game (28 + 2), cargo clippy -p victory-game --all-targets -- -D warnings and cargo fmt --check pass.

Test

  • New marker_inside_a_record_is_not_another_record: a synthetic record whose payload is another record, packed at level 0 (stored blocks), followed by a normal record. Before: [PS_outer, PS_inner, PS_after]; after: [PS_outer, PS_after].

  • cargo test -p victory-game (20 tests) and cargo clippy -p victory-game --all-targets -- -D warnings pass on Linux.

  • victory (CLI) does not build on Linux on main (victory-gpu host_resource), so the shader.rs change was checked with cargo check -p victory --target x86_64-pc-windows-msvc.

A printable byte before a long name is not its length byte

From 32 characters on, a name's length byte is printable, so name_before reads back through the bytes before it too (the previous record's packed tail, say). It took the earliest byte equal to its distance to the NUL, so a stray byte there (one in about 256 for names right after random bytes) made the name read as .PS_.... Shader names are identifiers, and a length byte for 32–46 characters is punctuation, so the earliest length byte whose name is all letters, digits and _ now wins; failing that, the earliest, as before. (Taking the nearest instead would misread names of 48+ characters holding a character equal to its distance, like the 4 in PS_4_0_www….)

  • New printable_bytes_before_a_long_name_are_not_part_of_it: a 45-character name after /, 0A, x.. Before: Some(".PS_DeferredLightingComposite_0011aabb_Wrapped"); after: the name. Also a 107-character name and PS_4_0_ + 48 w, read whole.
  • cargo test -p victory-game (29 + 2), cargo clippy -p victory-game --all-targets -- -D warnings and cargo fmt --check pass.

A name does not start with its length byte

A name of 47 to 56 characters has a digit (0–9) for its length byte, so a stray byte right before it that equals its distance to the NUL (one in 256 after random bytes) read as a length byte whose name, the digit then the real name, is all letters, digits and _, and won as the earliest: 0PS_www…. Names are identifiers, which do not start with a digit, so such a candidate is no longer taken for one. (Names of 64+ characters have a letter for a length byte, which this does not tell apart.)

  • New a_name_does_not_start_with_its_length_byte: names of 47, 49 and 56 characters after such a byte. Before: Some("0PS_…"); after: the name.
  • cargo test -p victory-game (30 + 2), cargo clippy -p victory-game --all-targets -- -D warnings and cargo fmt -p victory-game --check pass.

Bytes without a stream over a cut-off record hide nothing

A marker with sizes that fit, followed by bytes that are no deflate stream, laid over the header of a record that the end of the file cuts off, was not counted as covering a record, because only whole records counted. So it was yielded as a corrupt record, and the search went on inside the cut-off record and found the records it stores as is. The whole file yields neither of them. A cut-off record whose bytes so far are a deflate stream now counts as covered, so the phantom is skipped and the cut-off record ends the search, as it does without the phantom.

  • New marker_without_a_stream_over_a_cut_off_record_hides_nothing: whole file [PS_first, PS_outer]; cut after the stored inner record, before [PS_first, None, PS_inner], after [PS_first].
  • cargo test -p victory-game (32 + 2), cargo clippy -p victory-game --all-targets -- -D warnings and cargo fmt -p victory-game --check pass.

Names of every length, the magic in a name, and records in an archive

Test-only: three hermetic synthetic tests in crates/victory-game/src/sgd5.rs that pin down behaviour the search already has.

  • New every_name_length_a_byte_can_state_is_read_whole: 254 back-to-back records named with 1 to 254 characters. Their length bytes run through control bytes, punctuation, digits, letters and bytes past 0x7F. Each name is read whole and each container unpacks.
  • New magic_in_a_name_is_part_of_the_name: records named PS_SGD5_Wrapped and SGD5 after the file's magic. They keep their whole names, and the search does not restart at the magic in a name.
  • New records_in_an_archive_are_the_bare_files_shifted: the same three-record file after 0 to 40 bytes of zeros, printable length-byte look-alikes or marker bytes. It yields the same records, with every offset and span moved by the prefix length and the same names, kinds and containers.
**@⁠SkippySteve** on GitHub · 2026-11-24 01:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/273) `SkippySteve/sgd5-nested-marker` → `main` · +4677 −27 · 8 files · 72 commits · closed without merging `sgd5::records` matched every `0x000DDCE7` marker in the file, including ones inside an already-found record's packed bytes. Deflate stored blocks carry the container's bytes as they are, so a container that holds a record-shaped run of bytes was reported as an extra record (and the shader CLI would try to unpack it). The search now resumes after each record's packed bytes. A marker that does not form a record still advances by one byte, as before. ## Game tests: every known build's hash is one `identify()` can match (was #303) `KNOWN_BUILDS` is matched against the hex BLAKE3 that `run.rs` computes, so an entry with a typo, a truncated hash, stray whitespace or a duplicate would silently never identify the build it names. Three tests in `crates/victory-game/src/lib.rs` check that each entry is 64 lowercase hex digits with unique label and hash, that `identify()` finds each build by its own hash in either case, and that an unknown digest, an empty string or non-hex text identifies nothing. The list is empty today, so the first two guard future entries. Test-only; no game data. This PR now carries #303, which is closed in its favour. ## A record's header cannot sit inside the previous record After a record, the search resumed right at the end of its packed bytes, so a marker found there read its NUL, kind and `n` from the last bytes of the previous record (and its name from further back). Bytes after the end of a deflate stream are not checked, so this produced a phantom record. The search now starts 6 bytes after the previous record (or the magic), and a record's header and name are read only from bytes after it. - New `header_inside_a_record_is_not_another_record`: before `[Some("PS_first"), None]`, after `[Some("PS_first")]`. - New `empty_container_unpacks` and `damaged_file_yields_disjoint_records` (every single-byte change to a two-record file, 8 values each: no panic, records in order, inside the file, not overlapping). ## The shader survey does not count a stored container twice Deflate stored blocks carry a record's container as it is, so `victory shader`'s raw `DXBC` scan (`apps/victory/src/shader.rs`, `sources`) found it inside the record's packed bytes and counted it again, next to the unpacked record. `sgd5::Record::span` (marker to end of packed stream) and `sgd5::in_record` (binary search over the records, which are in file order and disjoint) let the scan skip those offsets. Containers between or after records, and in files with no SGD5 magic, are still found as before. - New `offsets_inside_records`: a level-0 record holding a `DXBC` run, then `DXBC` between and after records. The stored one is inside a record; the magic, the gap, the tail and each record's header are not. ## A truncated record does not expose the records it stores A record whose packed bytes run past the end of the file (a cut-off file) was dropped, but the search then went on one byte at a time through its bytes, so a record stored as is inside it (deflate stored blocks) was found and reported. The search now ends at such a record: every byte after its marker belongs to it. Sizes are added with `checked_add`. - New `truncated_record_hides_the_records_it_stores`: the file cut right after the stored inner record. Before `[PS_first, PS_inner]`, after `[PS_first]`. ## A marker with sizes deflate cannot produce is not a record Bytes between records (a container stored as is, say) that read as a marker with a huge packed size were taken for a truncated record, and a truncated record ends the search, so every record after them was lost. A record's packed size may now be at most what deflate can take for its stated unpacked size (9 bits a byte for fixed codes, which also covers stored blocks' 5 bytes per 65535) plus 4096 bytes of slack, since the stream's end is not checked. - New `marker_with_impossible_sizes_does_not_end_the_search`: `FFFFFFFF E7DC0D00 FFFFFFFF 10000000` before a record. Before `[]`, after `[PS_after]`. - New `packed_size_bound_holds_for_every_level`: incompressible input of 0 to 200000 bytes at miniz levels 0 to 10 never exceeds the bound. ## A marker running past the end is truncated only if a deflate stream starts there Bytes between records (a container stored as is, say) that read as a marker with sizes that fit the bound above, but whose packed bytes run past the end of the file, were still taken for a truncated record, which ends the search and loses every record after them. Such a record now counts as truncated only if its bytes so far are a deflate stream of the stated unpacked size: inflating them runs out of input mid-stream, or the stream ends having produced exactly that many bytes. Otherwise the marker is not a record and the search goes on one byte later. A truncated record that is a real one (a cut-off file) still ends the search as before. - New `marker_running_past_the_end_without_a_stream_does_not_end_the_search`: a 64 KiB/64 KiB marker followed by a reserved deflate block type, between two records. Before `[PS_first]`, after `[PS_first, PS_after]`. - New `truncated_stream_is_told_from_noise`: every prefix of a 4000-byte stream at miniz levels 0, 1, 6 and 10 starts a stream; the whole stream does only for its own size; a reserved block type and a stored block whose length check fails do not. ## Seeded random files yield exactly the planted records New `random_files_yield_exactly_the_planted_records` (200 seeds): 1 to 6 records with gaps of noise and near-miss markers (sizes that disagree, sizes no deflate stream could have) between them, half of them stored as is (level 0) and holding a record and a `DXBC` magic of their own. `records()` returns exactly the planted records, with their offsets, names and containers, and `in_record()` agrees with a linear scan of their spans at every offset. Reverting the skip over packed bytes or the packed-size bound makes it fail. Test-only. ## An empty name is no name A length byte of 1 right before the NUL read as the name `""`, so any `01 00` before a record's kind (noise, or a nameless record) named it `Some("")`, and `victory shader` labelled it with nothing instead of `?`. Such a name is now `None`. - New `empty_name_is_no_name`: before `[Some(""), Some("PS_named")]`, after `[None, Some("PS_named")]`. - New `archive_with_two_sgd5_files_yields_both`: two SGD5 files in one archive, the second magic in a gap between them; both files' records are found. Test-only. ## The shader survey does not count a container kept in another one twice `victory shader`'s raw `DXBC` scan accepted every magic that parses, so a container kept in a part of another container stored as is (outside any SGD5 record) was counted a second time. New `victory_shader::dxbc::find_all` returns containers stored as is, in file order, bounded to their stated size, and skips a magic that lies inside a found container's parts. Only parts count: a magic in junk right before a real container can read its size and part count from the real header and parse with no parts, and it must not hide the real one. `shader.rs` `sources` uses it, passing `sgd5::in_record` as the skip. `victory-shader` now depends on the workspace `memchr`. - New `container_in_a_part_is_not_another_container`: a container holding another in an `ILDB` part, then one after a gap. Found: the outer and the last; skipping the outer exposes the inner. - New `header_reading_into_a_container_does_not_hide_it` and `finds_nothing_in_noise`. - `cargo test -p victory-shader` (59) passes; `cargo xwin check`/`clippy -p victory --target x86_64-pc-windows-msvc` are clean. ## Records and the raw scan, tested together as the survey runs them `victory shader`'s `sources` combines `sgd5::records`, `sgd5::in_record` and `dxbc::find_all`, but the CLI does not build on Linux, so nothing ran that combination. New `crates/victory-game/tests/survey.rs` (`victory-shader` as a dev-dependency) does the same thing `sources` does: - `stored_record_container_is_counted_once`: a level-0 record's container counts as the record only, not also as a raw container; the container between records is raw; without the SGD5 magic the stored container is found as is. - `random_archives_survey_each_container_once` (200 seeds): another file's container before the magic, records (some stored as is, holding a container, a record and a container of their own), and gaps with noise, magics that do not parse, and containers (some keeping another in a part). Exactly the planted records and top-level raw containers are found, each once, and no raw container overlaps a record. - Passing `|_| false` as the skip, or dropping `find_all`'s part check, makes them fail. Test-only; synthetic bytes. `cargo test -p victory-game` (20 + 2) and `cargo clippy -p victory-game --all-targets -- -D warnings` pass. ## A marker with too few packed bytes for its size is not a record A marker whose packed size is less than any deflate stream of its stated unpacked size needs (2 bytes, and no fewer than unpacked / 1032) was yielded as a record that never unpacks, and with any packed bytes its span covered the next record's length byte, so that record lost its name. - New `packed_size_lower_bound_holds_for_every_level` (zeros up to 4 MiB at every miniz level), `marker_with_too_few_packed_bytes_does_not_hide_the_next_record` (before `[PS_first, None, PS_after]` or `[PS_first, None, None]`, after `[PS_first, PS_after]`), `smallest_containers_are_records`. ## Bytes that are no deflate stream do not hide the record they cover Bytes between records that read as a marker with sizes inside both bounds and inside the file were yielded as a corrupt record, and the search resumed after their packed bytes, so a real record whose header they covered was lost. If a record whose packed bytes are a deflate stream starts inside such a marker's span, it now wins and the marker is skipped. A corrupt record that covers no other record is still yielded (`unpack()` is `None`), so `victory shader` still reports it. Streams are inflated only when a span covers another record's header. - New `marker_without_a_stream_does_not_hide_the_record_it_covers`: before `[PS_first, None]`, after `[PS_first, PS_after]`. Disabling the check makes it fail. - New `corrupt_record_is_still_a_record`: a damaged record and the one after it are both yielded. - `cargo test -p victory-game` (25 + 2) and `cargo clippy -p victory-game --all-targets -- -D warnings` pass. ## Markers laid over one another are each looked at once A marker whose span covers a stream record, but whose own bytes are no stream, is skipped and the search goes on one byte later. Each such marker scanned its whole span again and inflated every stream record it found, so a file with many markers that each cover the rest of the file took n² work or more: 8000 of them (150 KB) took 43 s. `records()` now remembers which markers start stream records as the search moves forward through the file, so each marker is looked at once. The records found are unchanged (all earlier tests, including the random-file test, pass as before). - New `nested_markers_without_streams_take_linear_work`: 8000 such markers, then a real record. Before: 43 s (fails its 5 s bound); after: 0.03 s, and only `PS_real` is found. - `cargo test -p victory-game` (26 + 2) and `cargo clippy -p victory-game --all-targets -- -D warnings` pass. ## Chained markers do not make the stream check read the whole file Every marker that might be a record has its bytes checked for the start of a deflate stream (when its packed bytes run past the end, when it covers another record, and for each record it covers). Stored blocks that each end where the next marker's stored block begins make every marker's bytes a valid stream through all the markers after it, so each check inflated the rest of the chain: 16000 markers (1 MB) took 2.5 s, 32000 (2 MB) took 9 s, growing as n². `starts_stream` now reads at most 4096 packed bytes and makes at most 4096 unpacked ones (`PROBE`). A stream that ends inside the probe must still have the stated size; one that goes on past it counts as a stream. - New `chained_markers_inside_the_file_take_linear_work`: 32000 markers whose packed bytes end at the end of the file. Before: 9 s (fails its 5 s bound); after: milliseconds, same result (one corrupt record spanning them). - New `chained_markers_past_the_end_take_linear_work`: the same chain with packed sizes past the end. Before: 9 s; after: milliseconds. Trade-off: the chain is valid deflate for far longer than the probe, so its first marker is now taken for a truncated record and ends the search (before, the record after the chain was found, after 9 s). Noise that is no deflate stream within 4 KiB still does not end the search (`marker_running_past_the_end_without_a_stream_does_not_end_the_search` is unchanged). - `cargo test -p victory-game` (28 + 2), `cargo clippy -p victory-game --all-targets -- -D warnings` and `cargo fmt --check` pass. ## Linear-work tests count work instead of timing it The three linear-work tests bounded `records()` by wall clock (5 s), so a slow or loaded machine could fail them and a fast one could hide a regression. A test-only, per-thread counter in `starts_stream` now records the stream checks made and the packed bytes they read, and the tests bound those: at most two checks per marker, each reading at most `PROBE` bytes. The code under test is unchanged. - Checked against the regressions they guard: with the `Streams` memo disabled, `nested_markers_without_streams_take_linear_work` makes 31.6M checks for 8000 markers (bound 16002) and fails; with the probe removed, both chained-marker tests read about 2 MB per check and fail. - `cargo test -p victory-game` (28 + 2), `cargo clippy -p victory-game --all-targets -- -D warnings` and `cargo fmt --check` pass. ## Test - New `marker_inside_a_record_is_not_another_record`: a synthetic record whose payload is another record, packed at level 0 (stored blocks), followed by a normal record. Before: `[PS_outer, PS_inner, PS_after]`; after: `[PS_outer, PS_after]`. - `cargo test -p victory-game` (20 tests) and `cargo clippy -p victory-game --all-targets -- -D warnings` pass on Linux. - `victory` (CLI) does not build on Linux on `main` (victory-gpu `host_resource`), so the `shader.rs` change was checked with `cargo check -p victory --target x86_64-pc-windows-msvc`. ## A printable byte before a long name is not its length byte From 32 characters on, a name's length byte is printable, so `name_before` reads back through the bytes before it too (the previous record's packed tail, say). It took the earliest byte equal to its distance to the NUL, so a stray byte there (one in about 256 for names right after random bytes) made the name read as `.PS_...`. Shader names are identifiers, and a length byte for 32–46 characters is punctuation, so the earliest length byte whose name is all letters, digits and `_` now wins; failing that, the earliest, as before. (Taking the nearest instead would misread names of 48+ characters holding a character equal to its distance, like the `4` in `PS_4_0_www…`.) - New `printable_bytes_before_a_long_name_are_not_part_of_it`: a 45-character name after `/`, `0A`, `x.`. Before: `Some(".PS_DeferredLightingComposite_0011aabb_Wrapped")`; after: the name. Also a 107-character name and `PS_4_0_` + 48 `w`, read whole. - `cargo test -p victory-game` (29 + 2), `cargo clippy -p victory-game --all-targets -- -D warnings` and `cargo fmt --check` pass. ## A name does not start with its length byte A name of 47 to 56 characters has a digit (`0`–`9`) for its length byte, so a stray byte right before it that equals its distance to the NUL (one in 256 after random bytes) read as a length byte whose name, the digit then the real name, is all letters, digits and `_`, and won as the earliest: `0PS_www…`. Names are identifiers, which do not start with a digit, so such a candidate is no longer taken for one. (Names of 64+ characters have a letter for a length byte, which this does not tell apart.) - New `a_name_does_not_start_with_its_length_byte`: names of 47, 49 and 56 characters after such a byte. Before: `Some("0PS_…")`; after: the name. - `cargo test -p victory-game` (30 + 2), `cargo clippy -p victory-game --all-targets -- -D warnings` and `cargo fmt -p victory-game --check` pass. ## Bytes without a stream over a cut-off record hide nothing A marker with sizes that fit, followed by bytes that are no deflate stream, laid over the header of a record that the end of the file cuts off, was not counted as covering a record, because only whole records counted. So it was yielded as a corrupt record, and the search went on inside the cut-off record and found the records it stores as is. The whole file yields neither of them. A cut-off record whose bytes so far are a deflate stream now counts as covered, so the phantom is skipped and the cut-off record ends the search, as it does without the phantom. - New `marker_without_a_stream_over_a_cut_off_record_hides_nothing`: whole file `[PS_first, PS_outer]`; cut after the stored inner record, before `[PS_first, None, PS_inner]`, after `[PS_first]`. - `cargo test -p victory-game` (32 + 2), `cargo clippy -p victory-game --all-targets -- -D warnings` and `cargo fmt -p victory-game --check` pass. ## Names of every length, the magic in a name, and records in an archive Test-only: three hermetic synthetic tests in `crates/victory-game/src/sgd5.rs` that pin down behaviour the search already has. - New `every_name_length_a_byte_can_state_is_read_whole`: 254 back-to-back records named with 1 to 254 characters. Their length bytes run through control bytes, punctuation, digits, letters and bytes past 0x7F. Each name is read whole and each container unpacks. - New `magic_in_a_name_is_part_of_the_name`: records named `PS_SGD5_Wrapped` and `SGD5` after the file's magic. They keep their whole names, and the search does not restart at the magic in a name. - New `records_in_an_archive_are_the_bare_files_shifted`: the same three-record file after 0 to 40 bytes of zeros, printable length-byte look-alikes or marker bytes. It yields the same records, with every offset and span moved by the prefix length and the same names, kinds and containers.
records() matched every marker in the file, so one inside a record's
packed bytes (a stored deflate block carries the container as it is)
could be reported as a second record. Resume the search after each
record's packed bytes. Synthetic test: a record whose payload is
another record, packed with stored blocks.
KNOWN_BUILDS is matched against the hex BLAKE3 that run.rs computes, so an
entry with a typo, a truncated hash, stray whitespace or a duplicate would
silently never identify the build it names. Check each entry is 64 lowercase
hex digits (the form blake3::Hash::to_hex writes), that labels and hashes are
unique, that identify() finds each build by its own hash in either case, and
that an unknown digest, an empty string or non-hex text identifies nothing.

The list is empty today, so the first two checks guard future entries; all
three run on every host without game files.
A well-formed record in bytes before the first SGD5 magic (another file
in the same archive) was reported as one of the SGD5 file's records.
Tests also cut a two-record file at every length: only whole records
are found, and each one unpacks.
The search resumed right after a record's packed bytes, so a marker found
there read its NUL, kind and size from the end of those packed bytes, and
its name from before them. Bytes past the end of the deflate stream are
not checked, so such a phantom record was reported. The search now starts
six bytes after the previous record (or the magic), and a record's header
and name are read only from bytes after it.

Tests: the phantom case (failed before: [PS_first, None]), an empty
container, and every single-byte change to a two-record file finds only
disjoint records inside the file, without panicking.
Deflate stored blocks carry a record's container as it is, so the raw DXBC
scan in `victory shader` also found it and counted it a second time.
sgd5::Record::span and sgd5::in_record let the scan skip offsets inside a
record.
A record whose packed bytes run past the end of the file was skipped one
byte at a time, so a record stored as is inside it (deflate stored
blocks) was found and reported. The search now ends there: every byte
after its marker belongs to it.
Bytes between records (a container stored as is, say) that read as a
marker with a huge packed size were taken for a truncated record and
ended the search, hiding every record after them. A packed size beyond
what deflate can take for the stated unpacked size (9 bits a byte, plus
4096 bytes of slack) is now not a record.
Gaps of noise and near-miss markers between records, some records stored
as is holding records and a DXBC magic of their own: records() finds the
planted records with their names and containers, and in_record() agrees
with a scan of their spans. Reverting the skip over packed bytes or the
packed-size bound fails it.
Bytes between records that read as a marker with sizes that fit, but run
past the end of the file, were taken for a truncated record, which ends the
search and loses every record after them. Such a record now counts as
truncated only if its bytes so far are a deflate stream of the stated size
(they run out mid-stream, or the stream ends with exactly that many bytes);
otherwise the marker is not a record and the search goes on.
A length byte of 1 followed by the NUL read as the name "", so any
`01 00` before a record's kind (noise, or a nameless record) gave it
Some(""), and the shader survey labelled it with an empty name instead
of "?". Such a name is now None.

Also test that an archive holding two SGD5 files, the second magic in a
gap, yields the records of both.
victory_shader::dxbc::find_all finds containers stored as is, skipping a
DXBC magic inside a found container's parts. A magic whose header reads
into a real container (zero parts) does not hide it. victory shader uses
it for its raw scan.
victory shader (apps/victory/src/shader.rs, sources) combines sgd5::records,
sgd5::in_record and dxbc::find_all, but the CLI does not build on Linux, so
the composition had no test that runs. New crates/victory-game/tests/survey.rs
(victory-shader as a dev-dependency) mirrors sources and checks:

- stored_record_container_is_counted_once: a level-0 record's container is
  the record's, not also a raw one; the container between records is raw;
  without the SGD5 magic the stored container is found as is.
- random_archives_survey_each_container_once (200 seeds): another file's
  container before the magic, records (some stored as is, holding a
  container, a record and a container of their own), gaps with noise,
  magics that do not parse and containers (some keeping another in a part).
  Exactly the planted records and top-level raw containers are found, each
  once, and no raw container overlaps a record.

Passing |_| false as the skip, or dropping find_all's part check, makes them
fail. Test-only; synthetic bytes, no game data.
records() refused a marker whose packed size is more than deflate could
take (max_packed), but not one with fewer packed bytes than any deflate
stream of the stated unpacked size needs. Bytes between records reading as
such a marker (n = 12..15, a 1 MiB container) were yielded as a record that
never unpacks, and with any packed bytes its span started over the next
record's length byte, so that record lost its name.

min_packed: 2 bytes (one empty fixed-code block), and no fewer than
unpacked / 1032 (one 258-byte match per 2 bits). Tests:

- packed_size_lower_bound_holds_for_every_level: zeros up to 4 MiB at
  every miniz level never go under the bound.
- marker_with_too_few_packed_bytes_does_not_hide_the_next_record: 0 to 3
  packed bytes before a real record. Before: [PS_first, None, PS_after]
  for 0, [PS_first, None, None] for 1 to 3.
- smallest_containers_are_records: 0 to 3 byte containers at every level
  are still found and unpack.

Synthetic bytes, no game data.
Bytes between records (a container stored as is, say) that read as a marker
with sizes inside both bounds and inside the file were yielded as a record
whose stream is corrupt, and the search resumed after their packed bytes, so
a real record whose header they covered was lost.

A record whose header starts inside such a marker's span and whose packed
bytes are a deflate stream now wins: the marker is skipped and the search
goes on one byte later. A corrupt record that covers no other record is still
yielded (unpack() is None), so `victory shader` still reports it. Streams are
only inflated when a marker's span covers another record's header.

- marker_without_a_stream_does_not_hide_the_record_it_covers: spans that
  cover PS_after's header. Before: [PS_first, None]; after: [PS_first,
  PS_after].
- corrupt_record_is_still_a_record: a damaged record and the one after it
  are both yielded.
records() skips a marker whose span covers a record that is a deflate
stream when its own bytes are none, and goes on one byte later. Each such
marker scanned its whole span for stream records anew, inflating each one
it found, so bytes holding many markers that each cover the rest of the file
cost n² work and more: 8000 of them (150 KB) took 43 s in a test build.

The search's questions move forward through the file, so which markers
start stream records is now kept as it is found (no stream record from
`from` to `to`, and whether the marker at `to` is one) and each marker is
looked at once. The records found are unchanged.

- nested_markers_without_streams_take_linear_work: 8000 such markers, then
  a real record. Before: 43 s (fails its 5 s bound); after: 0.03 s, and
  only PS_real is found.
Every marker that might be a record has its bytes checked for the start of a
deflate stream. Stored blocks that each end where the next marker's begin made
each check inflate the rest of the chain: 32000 such markers (2 MB) took 9 s,
growing as n². The check now reads at most 4096 packed bytes and makes at most
4096 unpacked ones.
The three linear-work tests bounded records() by wall clock (5 s), so a
slow or loaded machine could fail them and a fast one could hide a
regression. A test-only per-thread counter in starts_stream now records
the stream checks made and the packed bytes they read, and the tests
bound those: at most two checks per marker, each reading at most PROBE
bytes. Disabling the Streams memo (31.6M checks for 8000 markers) or the
probe (2 MB per check) fails them; the code under test is unchanged.
From 32 characters on, a name's length byte is printable, so the bytes
before it (the previous record's packed tail) run on into the name, and
one equal to its distance to the NUL was taken for the length byte: the
name came out as ".PS_...". The earliest length byte whose name is an
identifier now wins, falling back to the earliest as before.
A name of 47 to 56 characters has a digit for its length byte, so a stray
byte right before it equal to its distance to the NUL made the name read
as "0PS_...". Names are identifiers, which do not start with a digit.
Seeded, damaged files with records stored as is (holding records of their
own), searched at every prefix: each record that unpacks is one the whole
file yields, the same in every field. A file still being written or copied
never reports a container that later turns out to be another record's bytes.
Corrupt records over corrupt records are left out: which one is the record
is not known, and neither yields a container.
A marker with sizes that fit and bytes that are no deflate stream, laid
over the header of a record the end of the file cuts off, was not taken to
cover a record (only whole records counted), so it was yielded as a
corrupt record and the search went on inside the cut-off record, finding
the records it stores as is. The whole file yields neither. A cut-off
record whose bytes so far are a deflate stream now counts as covered.
Streams::any_in took hi <= file.len() on trust: a hi past the end read no
window yet moved its remembered frontier to hi - 3, so a later question
inside the file missed a stream marker it never looked at. It now asks only
up to the end. A marker within 6 bytes of the start (no header before it)
is skipped instead of underflowing.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:57:31 +00:00
github-bot closed this pull request 2026-11-26 03:57:31 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:11 UTC · original · edited 2026-11-24 04:13 UTC

Tested PR #273 at head 7c033d911d1deda65306692dc0e94beed1507b9b (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at 7c033d911d1deda65306692dc0e94beed1507b9b.

  • cargo test --locked -p victory-game — passed (25 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out); cases included sgd5::tests::marker_running_past_the_end_without_a_stream_does_not_end_the_search, sgd5::tests::reads_records, sgd5::tests::empty_container_unpacks, sgd5::tests::corrupt_stream_does_not_unpack, and 21 others.
  • cargo test --locked -p victory-shader container_in_a_part_is_not_another_container — passed (1 passed; 0 failed; 71 filtered out): dxbc::tests::container_in_a_part_is_not_another_container.

Behavior covered: SGD5 record spans remain disjoint, and the DXBC survey does not count containers inside packed records. Relevant code: sgd5.rs, dxbc.rs, shader.rs.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/273#issuecomment-7121790338) · edited 2026-11-24 04:13 UTC Tested PR #273 at head `7c033d911d1deda65306692dc0e94beed1507b9b` (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at `7c033d911d1deda65306692dc0e94beed1507b9b`. - `cargo test --locked -p victory-game` — passed (25 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out); cases included `sgd5::tests::marker_running_past_the_end_without_a_stream_does_not_end_the_search`, `sgd5::tests::reads_records`, `sgd5::tests::empty_container_unpacks`, `sgd5::tests::corrupt_stream_does_not_unpack`, and 21 others. - `cargo test --locked -p victory-shader container_in_a_part_is_not_another_container` — passed (1 passed; 0 failed; 71 filtered out): `dxbc::tests::container_in_a_part_is_not_another_container`. Behavior covered: SGD5 record spans remain disjoint, and the DXBC survey does not count containers inside packed records. Relevant code: [sgd5.rs](https://github.com/Marvisak/VIctory/blob/7c033d911d1deda65306692dc0e94beed1507b9b/crates/victory-game/src/sgd5.rs), [dxbc.rs](https://github.com/Marvisak/VIctory/blob/7c033d911d1deda65306692dc0e94beed1507b9b/crates/victory-shader/src/dxbc.rs), [shader.rs](https://github.com/Marvisak/VIctory/blob/7c033d911d1deda65306692dc0e94beed1507b9b/apps/victory/src/shader.rs).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!273
No description provided.