OpenTPW

WCT decoder bounds audit#

Review base: 0af4a608f4770c316652d4a7db767b907ebc5c27 (2026-10-13). This audit uses synthetic inputs only. Original asset reads are blocked; fresh retail corpus compatibility and original-runtime qualification remain deferred.

Three safe regression tests failed before the fixes: a zero-width image was accepted, a one-byte compressed chunk raised IndexOutOfRangeException, and an 8x8 colour payload with only 80 one-byte coefficients was silently accepted although Y/Cb/Cr require 64 + 16 + 16. The channel's in-place shift retained the old array length, allowing missing Cr bytes to come from duplicated earlier data.

Inspection also found unchecked image/scratch array products, unvalidated chunk counts, ignored BILZ size metadata, and an LZSS EOF path that repeatedly consumed fabricated zero bits and grew its output indefinitely. The old unbounded EOF path was not executed during the red run; the checked implementation is exercised by bounded truncated-stream tests.

Boundaries enforced#

Malformed WCT headers, dimensions, chunk lengths, BILZ sizes and coefficient truncation now report InvalidDataException. Two pre-existing malformed synthetic pins intentionally change from IndexOutOfRangeException to this exception. Valid RGBA hash pins are unchanged. Unknown flag bits, version/bpp values, quantizers, WCT checksum treatment and null-stream fallback are not reinterpreted.

Verification and limits#

WctBoundsTests covers valid BILZ/LZSS coefficients and Int16 escapes, alpha, truncated headers/chunks/bitstreams, signed and oversized claims, mismatch of BILZ actual sizes, array arithmetic, finite allocation bounds for invalid large images, nonseekable short reads, unused inflation padding, and unchanged legacy bit-read padding. Existing synthetic RGBA pins, quadratic scratch-allocation tests and LZSS window/overlap tests provide compatibility checks.

Final verification passed 46 synthetic/related cases, including all eleven new bounds cases; eighteen original-data cases skipped with their corpus environment unset. Build/typecheck succeeded with existing repository warnings. The changed C# style check, new-test whitespace check, CRLF-aware diff check, fidelity register check and its eleven Python tests passed. No RGBA hash pin changed.

These checks remove malformed-input allocation and truncation defects; they are not a universal workload budget. Valid large images still require substantial memory. Valid compressed tails are drained to validate size/checksum, so CPU work remains proportional to the complete decompressed stream. LZSS retains its valid decoded output rather than introducing a new stream/window algorithm. Original corpus validation must be rerun when the private asset volume is readable; no original assets, binary execution or disassembly are part of this change.

dotnet test source/OpenTPW.Tests/OpenTPW.Tests.csproj --filter 'FullyQualifiedName~WctBoundsTests|FullyQualifiedName~TextureDecoderIndexingSyntheticTests|FullyQualifiedName~TextureDecoderScratchAllocationTests|FullyQualifiedName~LzssWindowTests'

Independent review additionally reproduced an incomplete 1024x1 escaped-coefficient payload allocating 36,220,592 bytes before refusal. A structural coefficient scan now rejects it before image-plane allocation; the regression bounds allocation below 16 MiB. Added tests also validate a corrupted padding checksum after the retained prefix and compare valid escaped alpha plus BILZ/LZSS padding pixels. These checks use synthetic inputs; fresh retail compatibility remains deferred.

Retail corpus correction (October 13, 2026)#

Run against the original data, the checks above refused 5,055 of 8,416 shipped WCTs (990 BILZ, 4,061 LZSS), so the lobby and every park failed to load. Two assumptions made from synthetic inputs were wrong:

With both, every texture the pre-audit decoder handled decodes again with byte-identical RGBA (8,412 SHA-256 hashes compared); the same four malformed textures fail, two of them now as InvalidDataException. EveryOriginalTextureDecodesExceptThePinnedFailures walks every shipped WCT (with OPENTPW_GAME_PATH) so a synthetic-only change cannot refuse the corpus again.

Source: docs/WCT-ROBUSTNESS.md in the project repository.