diff --git a/CHANGELOG.md b/CHANGELOG.md index fb5df67..5f77afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,8 +37,26 @@ API between minor releases. - `CompressedPool.ReadSubgroups()` reads BL-tagged records from decoded pools and reports incomplete payloads. +- `PlaReaderOptions` (with `Default`) caps what the readers decode into memory: `MaxGlobalStreamBytes` bounds the + decoded global stream and the window a mode-2 `ReadPayload` builds (default: the most one in-memory stream can + hold), `MaxDecodedPoolBytes` bounds one decoded pool (default: 512 MiB) and `MaxCoverBytes` bounds the `.bimx` + cover image (default: 64 MiB, previously fixed). The options are immutable — build a new instance to change a + limit — and come through added overloads (`Open(string, PlaReaderOptions?)`, `Open(Stream, bool, + PlaReaderOptions?)`; the 0.1.0 entry-point signatures are unchanged). A read past a cap throws + `InvalidDataException` naming it. The caps are reader limits, not facts of the format. + +- `PlaStream.SkippedBlocks`: the indexed blocks that failed to decode to their declared size (false entries of the + scan), in file order — the blocks behind `UnreliableFrom`. + ### Changed +- The core library follows the IOExtend reference layout (RvtIO, NwcIO): the entry points (`PlaDocument`, + `BimxDocument`, `PlaFile`, `PlaReaderOptions`) stay in `PlaIO`, while the document types moved to the namespace + `PlaIO.Model` (`FdbHeader`, `PlaBlock` + `PlaBlockMode`, `ObjectDirectory` + `ObjectDirectoryRecord`, + `CompressedPool`, `PoolSubgroup`, `PlaStream`). The mode-2 global-window decode moved out of `PlaDocument` into + internal machinery; the public surface and behavior are unchanged apart from the namespaces, and the entry-point + signatures of the 0.1.0 surface keep their forms. + - The release workflow follows the IOExtend release order: the NuGet packages are pushed before the GitHub release is created (a refused first publication can be fixed and the job re-run, because `--skip-duplicate` makes the push repeatable), and a tag whose version contains a hyphen is published as a prerelease. The changelog section for the diff --git a/docs/getting-started.md b/docs/getting-started.md index 193b2b9..64e98f5 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -32,6 +32,7 @@ dotnet add package PlaIO using System; using System.IO; using PlaIO; +using PlaIO.Model; using (var doc = PlaDocument.Open("project.pln")) { diff --git a/docs/public-api.md b/docs/public-api.md index 12b7e40..a332161 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -1,8 +1,10 @@ # Public API -The `PlaIO` package is the supported surface listed here; every public type is in the namespace `PlaIO`. Everything else -(the block scanner, the header parser, the payload decoder, the directory search, the PNG chunk walker) is `internal` in -`PlaIO.Internal`; it can change in any release and is visible only to the test project through `InternalsVisibleTo`. +The `PlaIO` package is the supported surface listed here. The entry points (`PlaDocument`, `BimxDocument`, `PlaFile`, +`PlaReaderOptions`) are in the namespace `PlaIO`; the types that describe the opened document (`PlaBlock`, +`ObjectDirectory`, `CompressedPool`, `PoolSubgroup`, `PlaStream`, `FdbHeader`) are in `PlaIO.Model`. Everything else +(the block scanner, the header parser, the payload decoders, the directory search, the PNG chunk walker) is `internal` +in `PlaIO.Internal`; it can change in any release and is visible only to the test project through `InternalsVisibleTo`. ## File kind @@ -13,11 +15,26 @@ The `PlaIO` package is the supported surface listed here; every public type is i | `PlaFile.DetectKind(ReadOnlySpan head)` | The kind of a file whose first bytes are `head`. | | `PlaFile.DetectKind(string path)` | Reads at most 8 bytes of the file; I/O errors propagate. | +## Reader options + +| Member | Use | +|---|---| +| `PlaReaderOptions.Default` | The options a reader uses when the caller passes none. | +| `PlaReaderOptions.MaxGlobalStreamBytes` | Cap on the decoded global stream (`OpenStream`) and on the window a mode-2 `ReadPayload` builds (default: the most one in-memory stream can hold). A block whose declared size grows the window past the cap throws `InvalidDataException` before it is decoded. | +| `PlaReaderOptions.MaxDecodedPoolBytes` | Cap on the decoded size of one compressed pool (default: 512 MiB). A pool that declares more makes `Decode` throw `InvalidDataException`. | +| `PlaReaderOptions.MaxCoverBytes` | Cap on the `.bimx` cover image read into memory (default: 64 MiB). A larger cover makes `ReadCover` throw `InvalidDataException`. | + +The caps are reader limits, not facts of the format. The options are immutable — build a new instance to change a +limit; `Default` cannot be mutated, so one reader never changes the limits of another. The entry-point signatures of +the 0.1.0 surface are unchanged: the options come through added overloads (`Open(string, PlaReaderOptions?)` and +`Open(Stream, bool, PlaReaderOptions?)`; the same for `BimxDocument`). + ## FDB container (`.pla`, `.pln`) | Member | Use | |---|---| | `PlaDocument.Open(string path)`, `PlaDocument.Open(Stream stream, bool leaveOpen = false)` | Opens a container and builds its block index. The stream must be readable and seekable (`ArgumentException` otherwise); data that is not an FDB container or is shorter than 32 bytes throws `InvalidDataException`. The document owns the stream unless `leaveOpen` is true; when `Open` throws, the stream stays with the caller. | +| `PlaDocument.Open(string path, PlaReaderOptions? options)`, `PlaDocument.Open(Stream stream, bool leaveOpen, PlaReaderOptions? options)` | The same opens with explicit reader options. | | `PlaDocument.Length` | Length of the file in bytes. | | `PlaDocument.Header` | The `FdbHeader`. | | `PlaDocument.Blocks` | The block index in file order, found by scanning from offset 32. No on-disk block map is known, so completeness is not claimed. The scan can take a byte run for a block header, so the index may contain false entries. In real files a small share of mode 1 blocks does not decode to its declared size, and `ReadPayload` throws `InvalidDataException` for them. | @@ -26,12 +43,13 @@ The `PlaIO` package is the supported surface listed here; every public type is i | `PlaDocument.OpenStream()` | The document's decoded global stream (`PlaStream`): every block's payload decoded in block order. Blocks that fail to decode are skipped and mark the remaining offsets as unreliable. The stream holds the decoded bytes in memory and can outlive the document. | | `PlaDocument.Dispose()` | Releases the stream unless the document was opened with `leaveOpen`. | | `FdbHeader` | `Size` (64), `LayoutVersion` (u16 at offset 8), `FormatVersion` (u16 at offset 10), `Field12` (u32 at offset 12; meaning not documented), `Raw` (the first min(64, file length) bytes; bytes 32..63 overlap the first block). | -| `PlaBlockMode` | `Raw` (0), `ZeroMask` (1, flag 0x100), `Lz` (2, flag 0x200). | +| `PlaBlockMode` | `Raw` (0), `ZeroMask` (1, flag 0x100), `Lz` (2, flag 0x200): an LZ token stream decoded against the global window. | | `PlaBlock` | One indexed block: `Offset`, `Tag` (`BL` or `DY`), `Id` and `Kind` (raw), `Version` (0 or 1), `Flags` (raw), `SizeClass` (`Flags & 0xF`), `Mode`, `Guid` (present when flag 0x20 is set), `PackedSize`, `UnpackedSize`, `Extra` (present when flag 0x400 is set; raw), `PayloadOffset`, `HeaderLength`, `End`. | | `ObjectDirectory` | A run of directory records inside a raw block: `Block`, `RecordOffset` (payload offset of the first record), `Phase` (`RecordOffset % 30`), `Records`, `AbuttingPairs`, `OverlappingPairs`, `DuplicatePositions`. The run is an estimate and can be shorter than the real directory: it stops after more than 8 consecutive records without an abutting neighbour. | | `ObjectDirectoryRecord` | The 30-byte record `[u16 class][u64 pos][u32 count][GUID16]` as `ClassNumber`, `Position`, `Count`, `ObjectId`. The position and count name a byte extent of the decoded global stream — the object's data (see `PlaDocument.OpenStream`). The class number is not mapped to a class name. | | `PlaStream.Length` | Length of the decoded stream in bytes, including the bytes beyond the unreliable boundary. | | `PlaStream.UnreliableFrom` | The offset of the first indexed block that failed to decode, or null when every indexed block decoded. | +| `PlaStream.SkippedBlocks` | The indexed blocks that failed to decode to their declared size (false entries of the scan), in file order. | | `PlaStream.ReadExtent(long position, long count)` | The bytes of one extent. Throws `ArgumentOutOfRangeException` for negative or out-of-bounds extents and `InvalidDataException` for extents that reach the unreliable range. | | `PlaStream.ReadExtent(ObjectDirectoryRecord record)` | The bytes addressed by a directory record. Throws `InvalidDataException` if the extent exceeds the stream or reaches the unreliable range. | @@ -47,7 +65,7 @@ independent decoding of saved-file frames verified the correspondence. PlaIO has | `PlaDocument.FindCompressedPools()` | Finds pool markers outside indexed block payloads and validates frame headers and sizes. Computed on each call. | | `CompressedPool.Offset` and `End` | File offsets of the pool marker and of the end of the pool. | | `CompressedPool.TotalDecompressed` and `BlockCount` | Decoded byte count and number of frames in the pool. | -| `CompressedPool.Decode()` | Decoded frame payloads in order. Throws `InvalidDataException` on invalid framing or data and `ObjectDisposedException` after disposing the document. | +| `CompressedPool.Decode()` | Decoded frame payloads in order. Throws `InvalidDataException` on invalid framing or data (a pool past `PlaReaderOptions.MaxDecodedPoolBytes` included) and `ObjectDisposedException` after disposing the document. | ### Pool subgroups @@ -67,18 +85,20 @@ Offsets are relative to the decoded pool. The scanner retries an unmatched BL ca | Member | Use | |---|---| | `BimxDocument.Open(string path)`, `BimxDocument.Open(Stream stream, bool leaveOpen = false)` | Opens a `.bimx` file and measures its cover image without reading it. Same exceptions as `PlaDocument.Open`; a cover cut short also throws `InvalidDataException`. | +| `BimxDocument.Open(string path, PlaReaderOptions? options)`, `BimxDocument.Open(Stream stream, bool leaveOpen, PlaReaderOptions? options)` | The same opens with explicit reader options. | | `BimxDocument.Length` | Length of the file in bytes. | | `BimxDocument.CoverOffset` | Offset of the PNG cover image: always 4, right after the signature. | | `BimxDocument.CoverLength` | Length of the cover, from the PNG signature through the IEND chunk and its CRC. | | `BimxDocument.ContentOffset` | `CoverOffset + CoverLength`. The content after the cover is not interpreted. | -| `BimxDocument.ReadCover()` | The PNG cover image. Throws `InvalidDataException` when the cover is larger than 64 MiB. | +| `BimxDocument.ReadCover()` | The PNG cover image. Throws `InvalidDataException` when the cover is larger than `PlaReaderOptions.MaxCoverBytes` (64 MiB by default). | | `BimxDocument.Dispose()` | Releases the stream unless the document was opened with `leaveOpen`. | ## Resources and threads `PlaDocument` and `BimxDocument` never load the whole file into memory: the block index is built by scanning through the stream once, at `Open`, and payloads and the cover are read on request. The exception is `OpenStream`: its decoded global -stream holds the whole decoded file in memory by design. A document is not thread-safe; use one per thread. +stream holds the whole decoded file in memory by design, bounded by `PlaReaderOptions.MaxGlobalStreamBytes`. A document +is not thread-safe; use one per thread. After `Dispose`, `ReadPayload`, `FindDirectories`, `OpenStream` and `ReadCover` throw `ObjectDisposedException`. ## Diagnostics diff --git a/src/PlaIO.Cli/Cli.cs b/src/PlaIO.Cli/Cli.cs index b0056c6..aa86abf 100644 --- a/src/PlaIO.Cli/Cli.cs +++ b/src/PlaIO.Cli/Cli.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using System.Globalization; using System.Reflection; diff --git a/src/PlaIO.Cli/DirectoryJson.cs b/src/PlaIO.Cli/DirectoryJson.cs index 8f641f9..a09cc27 100644 --- a/src/PlaIO.Cli/DirectoryJson.cs +++ b/src/PlaIO.Cli/DirectoryJson.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using System.Text.Json; namespace PlaIO.CommandLine; diff --git a/src/PlaIO/BimxDocument.cs b/src/PlaIO/BimxDocument.cs index 692febe..74820ee 100644 --- a/src/PlaIO/BimxDocument.cs +++ b/src/PlaIO/BimxDocument.cs @@ -35,12 +35,17 @@ private BimxDocument(Stream stream, bool leaveOpen, long length, long coverLengt /// Opens the file at for reading. /// The file is not a .bimx file or its cover image is cut short. public static BimxDocument Open(string path) + => Open(path, options: null); + + /// Opens the file at for reading with explicit reader options. + /// The file is not a .bimx file or its cover image is cut short. + public static BimxDocument Open(string path, PlaReaderOptions? options) { ArgumentNullException.ThrowIfNull(path); var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, FileOptions.RandomAccess); try { - return Open(stream, leaveOpen: false); + return Open(stream, leaveOpen: false, options); } catch { @@ -54,13 +59,20 @@ public static BimxDocument Open(string path) /// the caller. /// The stream cannot read or seek. /// The data is not a .bimx file or its cover image is cut short. - public static BimxDocument Open(Stream stream, bool leaveOpen = false) => Open(stream, leaveOpen, FdbFormat.MaxCoverLength); + public static BimxDocument Open(Stream stream, bool leaveOpen = false) + => Open(stream, leaveOpen, options: null); - internal static BimxDocument Open(Stream stream, bool leaveOpen, long maxCoverLength) + /// Opens a .bimx file from a readable, seekable stream with explicit reader options and measures the cover + /// image without reading it. The document owns the stream unless is true; when this + /// method throws, the stream stays with the caller. + /// The stream cannot read or seek. + /// The data is not a .bimx file or its cover image is cut short. + public static BimxDocument Open(Stream stream, bool leaveOpen, PlaReaderOptions? options) { ArgumentNullException.ThrowIfNull(stream); if (!stream.CanRead || !stream.CanSeek) throw new ArgumentException("The stream must be readable and seekable.", nameof(stream)); + options ??= PlaReaderOptions.Default; long length = stream.Length; var head = new byte[Math.Min(PlaFile.SignatureLength, length)]; @@ -70,7 +82,7 @@ internal static BimxDocument Open(Stream stream, bool leaveOpen, long maxCoverLe throw new InvalidDataException($"Not a BIMx file (detected kind: {kind})."); long coverLength = PngChunks.Measure(stream, 4, length); - return new BimxDocument(stream, leaveOpen, length, coverLength, maxCoverLength); + return new BimxDocument(stream, leaveOpen, length, coverLength, options.MaxCoverBytes); } /// The PNG cover image. diff --git a/src/PlaIO/Internal/BlockHeaderParser.cs b/src/PlaIO/Internal/BlockHeaderParser.cs index 225fbb4..3d9ea4c 100644 --- a/src/PlaIO/Internal/BlockHeaderParser.cs +++ b/src/PlaIO/Internal/BlockHeaderParser.cs @@ -1,5 +1,6 @@ using System.Buffers.Binary; using System.Diagnostics.CodeAnalysis; +using PlaIO.Model; namespace PlaIO.Internal; diff --git a/src/PlaIO/Internal/BlockScanner.cs b/src/PlaIO/Internal/BlockScanner.cs index 2ad9004..d5cfb08 100644 --- a/src/PlaIO/Internal/BlockScanner.cs +++ b/src/PlaIO/Internal/BlockScanner.cs @@ -1,3 +1,5 @@ +using PlaIO.Model; + namespace PlaIO.Internal; /// Builds the block index in one forward pass: from offset 32, look for a "BL" or "DY" tag, accept a block whose diff --git a/src/PlaIO/Internal/DirectoryFinder.cs b/src/PlaIO/Internal/DirectoryFinder.cs index 3080bff..13ebbb9 100644 --- a/src/PlaIO/Internal/DirectoryFinder.cs +++ b/src/PlaIO/Internal/DirectoryFinder.cs @@ -1,5 +1,6 @@ using System.Buffers.Binary; using System.Numerics; +using PlaIO.Model; namespace PlaIO.Internal; diff --git a/src/PlaIO/Internal/FdbFormat.cs b/src/PlaIO/Internal/FdbFormat.cs index 003e42c..54991ed 100644 --- a/src/PlaIO/Internal/FdbFormat.cs +++ b/src/PlaIO/Internal/FdbFormat.cs @@ -46,4 +46,11 @@ internal static class FdbFormat /// Reader limit, not a format fact: the largest .bimx cover image that is read into memory. public const long MaxCoverLength = 64L << 20; + + /// Reader limit, not a format fact: the most bytes one decoded global stream may hold — the largest + /// in-memory buffer the reader can build on the running runtime. + public static readonly long MaxGlobalStreamBytes = Array.MaxLength; + + /// Reader limit, not a format fact: the largest pool one document may decode into memory. + public const long MaxDecodedPoolBytes = 512L << 20; } diff --git a/src/PlaIO/Internal/GlobalStreamDecoder.cs b/src/PlaIO/Internal/GlobalStreamDecoder.cs new file mode 100644 index 0000000..802d4a2 --- /dev/null +++ b/src/PlaIO/Internal/GlobalStreamDecoder.cs @@ -0,0 +1,77 @@ +using PlaIO.Model; + +namespace PlaIO.Internal; + +/// Decodes the global stream: the payloads of every indexed block in file order appended to one window, +/// because a mode-2 token stream may reference the output of any preceding block. A block that does not decode to its +/// declared size (a false entry of the scan) is not part of the real stream: it is skipped and reported with the window +/// offset it would have contributed, and the window stays as if it were not there. The window never grows past +/// maxBytes: a block whose declared unpacked size would cross the cap throws before it is decoded. +internal static class GlobalStreamDecoder +{ + /// Decodes every block of the document into a fresh window. The skipped blocks are reported in file + /// order. + public static List DecodeAll(PlaDocument document, long maxBytes, out IReadOnlyList<(PlaBlock Block, long WindowPosition)> skipped) + { + var window = new List(); + var failed = new List<(PlaBlock, long)>(); + foreach (var block in document.Blocks) + { + int before = window.Count; + if (!TryAppend(document, block, window, maxBytes)) failed.Add((block, before)); + } + skipped = failed.AsReadOnly(); + return window; + } + + /// Decodes every block before into a fresh window, skipping the blocks that do + /// not decode as does. The block itself is not decoded. + public static List DecodePrefix(PlaDocument document, PlaBlock through, long maxBytes) + { + var window = new List(); + foreach (var block in document.Blocks) + { + if (ReferenceEquals(block, through)) break; + TryAppend(document, block, window, maxBytes); + } + return window; + } + + /// Appends the decoded payload of one block to the window, or leaves the window unchanged and returns + /// false when the block does not decode to its declared size (a false entry of the scan, so a partial decode is + /// rolled back). + public static bool TryAppend(PlaDocument document, PlaBlock block, List window, long maxBytes) + { + // The cap is a property of the whole window, so it is checked against the declared contribution before any + // decoding; a block that fails the cap is an error, not a skipped false entry. + if (window.Count + block.UnpackedSize > maxBytes) + throw new InvalidDataException($"The decoded global stream would grow past {maxBytes} bytes (the reader's cap) at the block at 0x{block.Offset:x}."); + int before = window.Count; + try + { + Append(document, block, window); + return true; + } + catch (InvalidDataException) + { + window.RemoveRange(before, window.Count - before); + return false; + } + } + + /// Appends one block's full contribution to the window: raw payloads as they are, zero-mask payloads as + /// their full decode, mode-2 payloads as their token-stream output with the declared tail zero-filled (the + /// directory addresses the buffer space, so the window holds it). Throws when + /// the block does not decode to its declared size. + private static void Append(PlaDocument document, PlaBlock block, List window) + { + if (block.Mode != PlaBlockMode.Lz) + { + window.AddRange(document.ReadPayload(block)); + return; + } + + document.ReadLzPayload(block, window, out int produced, out _); + if (produced < block.UnpackedSize) window.AddRange(new byte[(int)(block.UnpackedSize - produced)]); + } +} diff --git a/src/PlaIO/Internal/PoolReader.cs b/src/PlaIO/Internal/PoolReader.cs index b2384fc..dd3b2a5 100644 --- a/src/PlaIO/Internal/PoolReader.cs +++ b/src/PlaIO/Internal/PoolReader.cs @@ -1,4 +1,5 @@ using System.Buffers.Binary; +using PlaIO.Model; namespace PlaIO.Internal; @@ -63,9 +64,11 @@ private static long ScanRange(Stream stream, long length, long pos, long end, by return pos; } - public static byte[] Decode(Stream stream, long length, long offset) + public static byte[] Decode(Stream stream, long length, long offset, long maxDecodedBytes) { var (range, frames) = ReadLayout(stream, length, offset); + if (range.UnpackedSize > maxDecodedBytes) + throw InvalidPool(offset, $"the pool decodes to {range.UnpackedSize} bytes, past the reader's cap of {maxDecodedBytes}"); byte[] decoded = new byte[range.UnpackedSize]; int written = 0; foreach (var frame in frames) diff --git a/src/PlaIO/Internal/PoolSubgroupReader.cs b/src/PlaIO/Internal/PoolSubgroupReader.cs index 3578e16..ee7e8b1 100644 --- a/src/PlaIO/Internal/PoolSubgroupReader.cs +++ b/src/PlaIO/Internal/PoolSubgroupReader.cs @@ -1,3 +1,5 @@ +using PlaIO.Model; + namespace PlaIO.Internal; internal static class PoolSubgroupReader diff --git a/src/PlaIO/CompressedPool.cs b/src/PlaIO/Model/CompressedPool.cs similarity index 97% rename from src/PlaIO/CompressedPool.cs rename to src/PlaIO/Model/CompressedPool.cs index 6e8e616..cf60aa1 100644 --- a/src/PlaIO/CompressedPool.cs +++ b/src/PlaIO/Model/CompressedPool.cs @@ -1,6 +1,7 @@ +using PlaIO; using PlaIO.Internal; -namespace PlaIO; +namespace PlaIO.Model; /// A pool of QuickLZ frames stored between indexed FDB block payloads. public sealed class CompressedPool diff --git a/src/PlaIO/FdbHeader.cs b/src/PlaIO/Model/FdbHeader.cs similarity index 98% rename from src/PlaIO/FdbHeader.cs rename to src/PlaIO/Model/FdbHeader.cs index c3f5dae..326e0df 100644 --- a/src/PlaIO/FdbHeader.cs +++ b/src/PlaIO/Model/FdbHeader.cs @@ -1,6 +1,6 @@ using System.Buffers.Binary; -namespace PlaIO; +namespace PlaIO.Model; /// The 64-byte header of an FDB container. Bytes 0..31 are fixed; bytes 32..63 overlap the first block and their /// meaning is not documented, so they are only kept raw. diff --git a/src/PlaIO/ObjectDirectory.cs b/src/PlaIO/Model/ObjectDirectory.cs similarity index 99% rename from src/PlaIO/ObjectDirectory.cs rename to src/PlaIO/Model/ObjectDirectory.cs index 9ba00b6..9995862 100644 --- a/src/PlaIO/ObjectDirectory.cs +++ b/src/PlaIO/Model/ObjectDirectory.cs @@ -1,4 +1,4 @@ -namespace PlaIO; +namespace PlaIO.Model; /// One 30-byte directory record: [u16 class][u64 pos][u32 count][GUID16]. The position and count name a /// byte extent of the decoded global stream (see ), the object's data. diff --git a/src/PlaIO/PlaBlock.cs b/src/PlaIO/Model/PlaBlock.cs similarity index 90% rename from src/PlaIO/PlaBlock.cs rename to src/PlaIO/Model/PlaBlock.cs index 3d1b60a..676eb8d 100644 --- a/src/PlaIO/PlaBlock.cs +++ b/src/PlaIO/Model/PlaBlock.cs @@ -1,4 +1,4 @@ -namespace PlaIO; +namespace PlaIO.Model; /// How the payload of a block is stored. public enum PlaBlockMode @@ -9,8 +9,9 @@ public enum PlaBlockMode /// Mode 1 (flag 0x100): groups of a mask byte and one byte per set bit; clear bits stand for zero bytes. ZeroMask = 1, - /// Mode 2 (flag 0x200): a compressed payload. Its compression is not documented, so it cannot be - /// decoded. + /// Mode 2 (flag 0x200): an LZ token stream, decoded by . + /// The stream may reference the decompressed output of any preceding block, so it is decoded against the global + /// window. Lz = 2, } diff --git a/src/PlaIO/PlaStream.cs b/src/PlaIO/Model/PlaStream.cs similarity index 86% rename from src/PlaIO/PlaStream.cs rename to src/PlaIO/Model/PlaStream.cs index 98de6bf..b1a30a2 100644 --- a/src/PlaIO/PlaStream.cs +++ b/src/PlaIO/Model/PlaStream.cs @@ -1,4 +1,4 @@ -namespace PlaIO; +namespace PlaIO.Model; /// The decoded global stream of a document: every block's payload decoded in block order — raw payloads as /// they are, zero-mask payloads as their full decode, mode 2 payloads as their token-stream output. The object @@ -16,10 +16,16 @@ public sealed class PlaStream /// Extent reads that reach this boundary throw because the following offsets may be shifted. public long? UnreliableFrom { get; } - internal PlaStream(List bytes, long? unreliableFrom) + /// The indexed blocks that failed to decode to their declared size (false entries of the scan), in file + /// order; their payloads are missing from the stream, which shifts the offsets that follow + /// . + public IReadOnlyList SkippedBlocks { get; } + + internal PlaStream(List bytes, IReadOnlyList<(PlaBlock Block, long WindowPosition)> skipped) { _bytes = bytes; - UnreliableFrom = unreliableFrom; + SkippedBlocks = skipped.Select(pair => pair.Block).ToArray(); + UnreliableFrom = skipped.Count == 0 ? null : skipped[0].WindowPosition; } /// Length of the decoded stream in bytes. diff --git a/src/PlaIO/PoolSubgroup.cs b/src/PlaIO/Model/PoolSubgroup.cs similarity index 98% rename from src/PlaIO/PoolSubgroup.cs rename to src/PlaIO/Model/PoolSubgroup.cs index 09144c8..a19ea7c 100644 --- a/src/PlaIO/PoolSubgroup.cs +++ b/src/PlaIO/Model/PoolSubgroup.cs @@ -1,4 +1,4 @@ -namespace PlaIO; +namespace PlaIO.Model; /// A BL-tagged record in a decoded compressed pool. public sealed class PoolSubgroup diff --git a/src/PlaIO/PlaDocument.cs b/src/PlaIO/PlaDocument.cs index 3185851..3f59e20 100644 --- a/src/PlaIO/PlaDocument.cs +++ b/src/PlaIO/PlaDocument.cs @@ -1,4 +1,5 @@ using PlaIO.Internal; +using PlaIO.Model; namespace PlaIO; @@ -11,9 +12,10 @@ public sealed class PlaDocument : IDisposable private readonly Stream _stream; private readonly bool _leaveOpen; private readonly List _blocks; + private readonly PlaReaderOptions _options; private bool _disposed; - private PlaDocument(Stream stream, bool leaveOpen, long length, FdbHeader header, List blocks) + private PlaDocument(Stream stream, bool leaveOpen, long length, FdbHeader header, List blocks, PlaReaderOptions options) { _stream = stream; _leaveOpen = leaveOpen; @@ -21,6 +23,7 @@ private PlaDocument(Stream stream, bool leaveOpen, long length, FdbHeader header Header = header; _blocks = blocks; Blocks = blocks.AsReadOnly(); + _options = options; } /// Length of the file in bytes. @@ -29,7 +32,7 @@ private PlaDocument(Stream stream, bool leaveOpen, long length, FdbHeader header /// The container header. public FdbHeader Header { get; } - /// The block index, in file order, built once by . No on-disk block map is + /// The block index, in file order, built once by . No on-disk block map is /// known, so the index is found by scanning (from offset 32, one byte at a time, skipping accepted payloads) and makes no /// claim of completeness. The scan can take a byte run for a block header, so the index may contain false entries, and /// in real files a small share of mode 1 blocks does not decode to its declared size. @@ -38,12 +41,17 @@ private PlaDocument(Stream stream, bool leaveOpen, long length, FdbHeader header /// Opens the file at for reading. /// The file is not an FDB container or is cut short. public static PlaDocument Open(string path) + => Open(path, options: null); + + /// Opens the file at for reading with explicit reader options. + /// The file is not an FDB container or is cut short. + public static PlaDocument Open(string path, PlaReaderOptions? options) { ArgumentNullException.ThrowIfNull(path); var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, FileOptions.RandomAccess); try { - return Open(stream, leaveOpen: false); + return Open(stream, leaveOpen: false, options); } catch { @@ -57,10 +65,19 @@ public static PlaDocument Open(string path) /// The stream cannot read or seek. /// The data is not an FDB container or is cut short. public static PlaDocument Open(Stream stream, bool leaveOpen = false) + => Open(stream, leaveOpen, options: null); + + /// Opens a container from a readable, seekable stream with explicit reader options. The document owns + /// the stream unless is true; when this method throws, the stream stays with the + /// caller. + /// The stream cannot read or seek. + /// The data is not an FDB container or is cut short. + public static PlaDocument Open(Stream stream, bool leaveOpen, PlaReaderOptions? options) { ArgumentNullException.ThrowIfNull(stream); if (!stream.CanRead || !stream.CanSeek) throw new ArgumentException("The stream must be readable and seekable.", nameof(stream)); + options ??= PlaReaderOptions.Default; long length = stream.Length; if (length < FdbFormat.FirstBlockOffset) @@ -72,7 +89,7 @@ public static PlaDocument Open(Stream stream, bool leaveOpen = false) throw new InvalidDataException($"Not an FDB container (detected kind: {kind})."); var blocks = new BlockScanner(stream, length).Scan(); - return new PlaDocument(stream, leaveOpen, length, new FdbHeader(raw), blocks); + return new PlaDocument(stream, leaveOpen, length, new FdbHeader(raw), blocks, options); } /// The decoded payload of a block of this document. @@ -129,29 +146,12 @@ internal byte[] ReadPayload(PlaBlock block, out int consumed) // file is decoded from the start and the requested block's output is returned. The cost of a call // therefore grows with the size of the file. A preceding block that does not decode is a scanner // false positive and not part of the real stream, so it is skipped. - var window = new List(); - foreach (var b in _blocks) - { - if (ReferenceEquals(b, block)) break; - TryAppendToWindow(b, window); - } - var blockPayload = new byte[block.PackedSize]; - StreamReads.ReadExactlyAt(_stream, block.PayloadOffset, blockPayload, $"the payload of the block at 0x{block.Offset:x}"); - // Mode 2 (LZ): the token stream produces the declared unpacked size (some blocks declare a buffer - // larger than the stream produces — the tail of the buffer stays zero) and may reference the window; - // the body tail after the output is complete is writer slack. - var output = new byte[block.UnpackedSize]; - int lzConsumed; - try - { - lzConsumed = LzPayloadDecoder.Decode(blockPayload, output, window, block.Offset, out _); - } - catch (InvalidDataException e) - { - throw new InvalidDataException($"Block at 0x{block.Offset:x}: {e.Message}", e); - } - consumed = lzConsumed; - return output; + var window = GlobalStreamDecoder.DecodePrefix(this, block, _options.MaxGlobalStreamBytes); + // The cap is a property of the whole window and the requested block is part of it, so the check runs + // before the block's buffer is allocated. + if (window.Count + block.UnpackedSize > _options.MaxGlobalStreamBytes) + throw new InvalidDataException($"The decoded global stream would grow past {_options.MaxGlobalStreamBytes} bytes (the reader's cap) at the block at 0x{block.Offset:x}."); + return ReadLzPayload(block, window, out _, out consumed); } default: throw new NotSupportedException($"Block at 0x{block.Offset:x} uses an unknown payload mode {block.Mode}."); @@ -164,66 +164,35 @@ internal byte[] ReadPayload(PlaBlock block, out int consumed) /// lifetime. Object directory records address the stream by position and count. public PlaStream OpenStream() { - var window = DecodeGlobalWindow(out var skipped); - return new PlaStream(window, skipped.Count == 0 ? null : skipped[0].WindowPosition); - } - - /// Appends the decoded payload of one block to the mode-2 global window, or leaves the window unchanged when - /// the block does not decode to its declared size (a false entry of the scan is not part of the real stream, so a - /// partial decode is rolled back). Returns whether the block was appended. - private bool TryAppendToWindow(PlaBlock b, List window) - { - int before = window.Count; - try - { - AppendToWindow(b, window); - return true; - } - catch (InvalidDataException) - { - window.RemoveRange(before, window.Count - before); - return false; - } + ObjectDisposedException.ThrowIf(_disposed, this); + var window = GlobalStreamDecoder.DecodeAll(this, _options.MaxGlobalStreamBytes, out var skipped); + return new PlaStream(window, skipped); } - /// Decodes the whole file once into the global window: the raw payloads, the full zero-mask decodes and the - /// mode-2 outputs, in block order. Blocks that do not decode to their declared size (false entries of the scan) are - /// skipped and reported in with the window offset each would have contributed; the window - /// stays as if they were not there. - internal List DecodeGlobalWindow(out IReadOnlyList<(PlaBlock Block, long WindowPosition)> skipped) + /// Reads the packed payload of a mode-2 block and decodes it against , the decoded + /// output of every preceding block, which the token stream's back-references reach into. Returns the declared + /// unpacked buffer — the produced bytes at its head and a zero tail when the stream produces less than declared (the + /// writer reserved a buffer larger than its output); receives the produced count and + /// the body bytes the stream consumed. + /// The token stream does not decode. + internal byte[] ReadLzPayload(PlaBlock block, List window, out int produced, out int consumed) { ObjectDisposedException.ThrowIf(_disposed, this); - var window = new List(); - var failed = new List<(PlaBlock, long)>(); - foreach (var b in _blocks) + var payload = new byte[block.PackedSize]; + StreamReads.ReadExactlyAt(_stream, block.PayloadOffset, payload, $"the payload of the block at 0x{block.Offset:x}"); + // Mode 2 (LZ): the token stream produces the declared unpacked size (some blocks declare a buffer + // larger than the stream produces — the tail of the buffer stays zero) and may reference the window; + // the body tail after the output is complete is writer slack. + var output = new byte[block.UnpackedSize]; + try { - int before = window.Count; - if (!TryAppendToWindow(b, window)) failed.Add((b, before)); + consumed = LzPayloadDecoder.Decode(payload, output, window, block.Offset, out produced); } - skipped = failed.AsReadOnly(); - return window; - } - - /// Appends the decoded payload of one block to the mode-2 global window. Raw payloads are copied as they - /// are; a zero-mask payload contributes its full decode; a mode 2 payload is decoded and the decoder appends every - /// produced byte. Throws when the block does not decode to its declared size. - private void AppendToWindow(PlaBlock b, List window) - { - if (b.Mode != PlaBlockMode.Lz) + catch (InvalidDataException e) { - window.AddRange(ReadPayload(b)); - return; + throw new InvalidDataException($"Block at 0x{block.Offset:x}: {e.Message}", e); } - - var payload = new byte[b.PackedSize]; - StreamReads.ReadExactlyAt(_stream, b.PayloadOffset, payload, $"the payload of the block at 0x{b.Offset:x}"); - // Mode 2 (LZ): the token stream produces the declared unpacked size (some blocks declare a buffer - // larger than the stream produces — the tail of the buffer stays zero) and the decoder appends every - // produced byte to the window; the body tail after the output is complete is writer slack. - var output = new byte[b.UnpackedSize]; - int produced; - LzPayloadDecoder.Decode(payload, output, window, b.Offset, out produced); - for (long i = produced; i < b.UnpackedSize; i++) window.Add(0); // the directory addresses the buffer space + return output; } /// Searches every raw block large enough for 64 records for an object directory (at most one per block). @@ -253,7 +222,7 @@ public IReadOnlyList FindCompressedPools() internal byte[] DecodePool(long offset) { ObjectDisposedException.ThrowIf(_disposed, this); - return PoolReader.Decode(_stream, Length, offset); + return PoolReader.Decode(_stream, Length, offset, _options.MaxDecodedPoolBytes); } /// Releases the stream unless the document was opened with leaveOpen. diff --git a/src/PlaIO/PlaReaderOptions.cs b/src/PlaIO/PlaReaderOptions.cs new file mode 100644 index 0000000..03b9fea --- /dev/null +++ b/src/PlaIO/PlaReaderOptions.cs @@ -0,0 +1,27 @@ +using PlaIO.Model; + +namespace PlaIO; + +/// Options that control how much the readers decode into memory. The defaults are reader limits the library +/// has always used; they are choices of this reader, not facts of the format. The options are immutable: build a new +/// instance (an object initializer) to change a limit — cannot be mutated, so one reader never +/// changes the limits of another. +public sealed class PlaReaderOptions +{ + /// The options a reader uses when the caller passes none. + public static PlaReaderOptions Default { get; } = new(); + + /// Cap on the size of the decoded global stream () and of the window a + /// mode-2 builds (default: the most one stream can hold). The + /// stream holds the whole decoded file in memory; a block whose declared size grows the window past the cap makes + /// the reader throw naming the cap. + public long MaxGlobalStreamBytes { get; init; } = Internal.FdbFormat.MaxGlobalStreamBytes; + + /// Cap on the decoded size of one compressed pool (default: 512 MiB). A pool that declares more makes + /// throw naming the cap. + public long MaxDecodedPoolBytes { get; init; } = Internal.FdbFormat.MaxDecodedPoolBytes; + + /// Cap on the .bimx cover image that is read into memory (default: 64 MiB). A larger cover makes + /// throw naming the cap. + public long MaxCoverBytes { get; init; } = Internal.FdbFormat.MaxCoverLength; +} diff --git a/tests/PlaIO.Tests/BimxDocumentTests.cs b/tests/PlaIO.Tests/BimxDocumentTests.cs index 68df1a2..90a96d1 100644 --- a/tests/PlaIO.Tests/BimxDocumentTests.cs +++ b/tests/PlaIO.Tests/BimxDocumentTests.cs @@ -59,7 +59,8 @@ public void MalformedFilesAreRejected(string reason, byte[] bytes) public void CoverAboveTheReaderLimitIsNotRead() { var png = Synthetic.SimplePng(); - using var document = BimxDocument.Open(new MemoryStream(Synthetic.Bimx(png, Trailing)), leaveOpen: false, maxCoverLength: png.Length - 1); + using var document = BimxDocument.Open(new MemoryStream(Synthetic.Bimx(png, Trailing)), + leaveOpen: false, options: new PlaReaderOptions { MaxCoverBytes = png.Length - 1 }); Assert.Equal(png.Length, document.CoverLength); Assert.Throws(() => document.ReadCover()); } diff --git a/tests/PlaIO.Tests/BlockHeaderParserTests.cs b/tests/PlaIO.Tests/BlockHeaderParserTests.cs index 9f4fe79..c322f98 100644 --- a/tests/PlaIO.Tests/BlockHeaderParserTests.cs +++ b/tests/PlaIO.Tests/BlockHeaderParserTests.cs @@ -1,4 +1,5 @@ using PlaIO.Internal; +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/BlockScannerTests.cs b/tests/PlaIO.Tests/BlockScannerTests.cs index 0935117..de87c61 100644 --- a/tests/PlaIO.Tests/BlockScannerTests.cs +++ b/tests/PlaIO.Tests/BlockScannerTests.cs @@ -1,4 +1,5 @@ using PlaIO.Internal; +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/CliTests.cs b/tests/PlaIO.Tests/CliTests.cs index 6010bd9..d21fadc 100644 --- a/tests/PlaIO.Tests/CliTests.cs +++ b/tests/PlaIO.Tests/CliTests.cs @@ -1,5 +1,6 @@ using System.Text.Json; using PlaIO.CommandLine; +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/CompressedPoolTests.cs b/tests/PlaIO.Tests/CompressedPoolTests.cs index b562c43..87c9d85 100644 --- a/tests/PlaIO.Tests/CompressedPoolTests.cs +++ b/tests/PlaIO.Tests/CompressedPoolTests.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using System.Buffers.Binary; using Xunit; diff --git a/tests/PlaIO.Tests/DirectoryFinderTests.cs b/tests/PlaIO.Tests/DirectoryFinderTests.cs index 7067d87..8638057 100644 --- a/tests/PlaIO.Tests/DirectoryFinderTests.cs +++ b/tests/PlaIO.Tests/DirectoryFinderTests.cs @@ -1,4 +1,5 @@ using PlaIO.Internal; +using PlaIO.Model; using Xunit; using static PlaIO.Internal.DirectoryFinder; diff --git a/tests/PlaIO.Tests/FdbHeaderTests.cs b/tests/PlaIO.Tests/FdbHeaderTests.cs index 1f4d7fb..83a46fe 100644 --- a/tests/PlaIO.Tests/FdbHeaderTests.cs +++ b/tests/PlaIO.Tests/FdbHeaderTests.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/LzPayloadTests.cs b/tests/PlaIO.Tests/LzPayloadTests.cs index 7167c15..6caf41e 100644 --- a/tests/PlaIO.Tests/LzPayloadTests.cs +++ b/tests/PlaIO.Tests/LzPayloadTests.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using System.Buffers.Binary; using Xunit; diff --git a/tests/PlaIO.Tests/NaiveScanner.cs b/tests/PlaIO.Tests/NaiveScanner.cs index cb1d4f7..1508baa 100644 --- a/tests/PlaIO.Tests/NaiveScanner.cs +++ b/tests/PlaIO.Tests/NaiveScanner.cs @@ -1,4 +1,5 @@ using PlaIO.Internal; +using PlaIO.Model; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/PlaDocumentTests.cs b/tests/PlaIO.Tests/PlaDocumentTests.cs index 4dd7c25..e6f8b24 100644 --- a/tests/PlaIO.Tests/PlaDocumentTests.cs +++ b/tests/PlaIO.Tests/PlaDocumentTests.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/PlaReaderOptionsTests.cs b/tests/PlaIO.Tests/PlaReaderOptionsTests.cs new file mode 100644 index 0000000..637a25b --- /dev/null +++ b/tests/PlaIO.Tests/PlaReaderOptionsTests.cs @@ -0,0 +1,86 @@ +using PlaIO.Internal; +using PlaIO.Model; +using Xunit; + +namespace PlaIO.Tests; + +public class PlaReaderOptionsTests +{ + [Fact] + public void DefaultsMatchTheReaderLimits() + { + var options = PlaReaderOptions.Default; + Assert.Equal(FdbFormat.MaxGlobalStreamBytes, options.MaxGlobalStreamBytes); + Assert.Equal(FdbFormat.MaxDecodedPoolBytes, options.MaxDecodedPoolBytes); + Assert.Equal(FdbFormat.MaxCoverLength, options.MaxCoverBytes); + } + + [Fact] + public void ABlockPastTheGlobalStreamCapIsRejectedBeforeItIsDecoded() + { + var bytes = Synthetic.Fdb(Synthetic.Block(PlaBlockMode.Raw, "ABCD"u8.ToArray())); + using var document = PlaDocument.Open(new MemoryStream(bytes), + leaveOpen: false, options: new PlaReaderOptions { MaxGlobalStreamBytes = 3 }); + + // The cap is a property of the global window: a single payload read stays under the scanner's own limits. + Assert.Equal("ABCD"u8.ToArray(), document.ReadPayload(document.Blocks[0])); + var e = Assert.Throws(() => document.OpenStream()); + Assert.Contains("cap", e.Message); + } + + [Fact] + public void ALzBlockItselfCountsAgainstTheGlobalStreamCap() + { + // A mode-2 block with four literal bytes of output and an empty prefix window. + var lz = Synthetic.Concat([0x40], "wxyz"u8.ToArray(), [0xFF, 0xFE]); + var bytes = Synthetic.Fdb(Synthetic.Block(PlaBlockMode.Lz, lz, unpacked: 4)); + using var document = PlaDocument.Open(new MemoryStream(bytes), + leaveOpen: false, options: new PlaReaderOptions { MaxGlobalStreamBytes = 3 }); + + var e = Assert.Throws(() => document.ReadPayload(document.Blocks[0])); + Assert.Contains("cap", e.Message); + } + + [Fact] + public void AGlobalStreamAtTheCapIsDecoded() + { + var bytes = Synthetic.Fdb( + Synthetic.Block(PlaBlockMode.Raw, "AB"u8.ToArray()), + Synthetic.Block(PlaBlockMode.Raw, "CD"u8.ToArray())); + using var document = PlaDocument.Open(new MemoryStream(bytes), + leaveOpen: false, options: new PlaReaderOptions { MaxGlobalStreamBytes = 4 }); + + Assert.Equal(4, document.OpenStream().Length); + } + + [Fact] + public void APoolPastTheCapIsRejected() + { + var bytes = Synthetic.Fdb(Synthetic.QuickLzPool( + (3, Synthetic.QuickLzFrame([1, 2, 3], 3, flags: 0x44)))); + using var document = PlaDocument.Open(new MemoryStream(bytes), + leaveOpen: false, options: new PlaReaderOptions { MaxDecodedPoolBytes = 2 }); + + var pool = Assert.Single(document.FindCompressedPools()); + Assert.Equal(3, pool.TotalDecompressed); + var e = Assert.Throws(() => pool.Decode()); + Assert.Contains("cap", e.Message); + } + + [Fact] + public void TheSkippedBlocksOfTheStreamAreExposedInFileOrder() + { + // The middle block does not decode (see PlaStreamTests.ExtentsPastASkippedBlockAreRejectedAsShifted). + var brokenLz = Synthetic.Concat([0x40], "ABCD"u8.ToArray(), [0xFF, 0xFF], [0x10]); + using var document = PlaDocument.Open(new MemoryStream(Synthetic.Fdb( + Synthetic.Block(PlaBlockMode.Raw, "AB"u8.ToArray()), + Synthetic.Block(PlaBlockMode.Lz, brokenLz, unpacked: 6), + Synthetic.Block(PlaBlockMode.Raw, "CD"u8.ToArray())))); + + var stream = document.OpenStream(); + var skipped = Assert.Single(stream.SkippedBlocks); + Assert.Equal(document.Blocks[1].Offset, skipped.Offset); + Assert.Equal(PlaBlockMode.Lz, skipped.Mode); + Assert.Equal(2, stream.UnreliableFrom); + } +} diff --git a/tests/PlaIO.Tests/PlaSceneReaderTests.cs b/tests/PlaIO.Tests/PlaSceneReaderTests.cs index 5cfe9fd..96616a6 100644 --- a/tests/PlaIO.Tests/PlaSceneReaderTests.cs +++ b/tests/PlaIO.Tests/PlaSceneReaderTests.cs @@ -1,5 +1,6 @@ using IOScene; using PlaIO.Scene; +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/PlaStreamTests.cs b/tests/PlaIO.Tests/PlaStreamTests.cs index 6b8be27..7026f35 100644 --- a/tests/PlaIO.Tests/PlaStreamTests.cs +++ b/tests/PlaIO.Tests/PlaStreamTests.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/PoolSubgroupTests.cs b/tests/PlaIO.Tests/PoolSubgroupTests.cs index 0562b02..c769ff5 100644 --- a/tests/PlaIO.Tests/PoolSubgroupTests.cs +++ b/tests/PlaIO.Tests/PoolSubgroupTests.cs @@ -1,3 +1,4 @@ +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/SampleTests.cs b/tests/PlaIO.Tests/SampleTests.cs index 8c5eb26..66d60f7 100644 --- a/tests/PlaIO.Tests/SampleTests.cs +++ b/tests/PlaIO.Tests/SampleTests.cs @@ -1,4 +1,5 @@ using PlaIO.Internal; +using PlaIO.Model; using Xunit; namespace PlaIO.Tests; @@ -141,9 +142,10 @@ public void EveryLzBlockDecodesAndOnlyFalseEntriesAreSkipped() { foreach (var sample in corpus.Fdb) { - sample.Document.DecodeGlobalWindow(out var skipped); + var decoded = sample.Document.OpenStream(); + var skipped = decoded.SkippedBlocks; string where = $"{sample.Name}: {skipped.Count} of {sample.Document.Blocks.Count} blocks skipped"; - Assert.True(skipped.All(s => s.Block.Mode != PlaBlockMode.Lz), $"{where}; a mode-2 block did not decode"); + Assert.True(skipped.All(b => b.Mode != PlaBlockMode.Lz), $"{where}; a mode-2 block did not decode"); Assert.True(skipped.Count <= 0.01 * sample.Document.Blocks.Count, where); } } diff --git a/tests/PlaIO.Tests/Synthetic.cs b/tests/PlaIO.Tests/Synthetic.cs index d037c8a..225fcf1 100644 --- a/tests/PlaIO.Tests/Synthetic.cs +++ b/tests/PlaIO.Tests/Synthetic.cs @@ -1,5 +1,6 @@ using System.Buffers.Binary; using PlaIO.Internal; +using PlaIO.Model; namespace PlaIO.Tests; diff --git a/tests/PlaIO.Tests/VectorTests.cs b/tests/PlaIO.Tests/VectorTests.cs index 4990eca..5ec3d7e 100644 --- a/tests/PlaIO.Tests/VectorTests.cs +++ b/tests/PlaIO.Tests/VectorTests.cs @@ -2,6 +2,8 @@ using PlaIO.Internal; using Xunit; +using PlaIO.Model; + namespace PlaIO.Tests; /// Checks against the private vectors (PLAIO_TEST_VECTORS). Each test loops over the manifest, so it is skipped as a