Skip to content

Commit 6f5a9ef

Browse files
authored
Merge pull request #18 from IOExtend/feat/object-extents
Expose the decoded global stream and extent reads
2 parents b3d2ada + 560bccf commit 6f5a9ef

10 files changed

Lines changed: 271 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ API between minor releases.
2424
zero — the global window holds the buffer space, so directory positions keep addressing it. A back-reference beyond
2525
the decoded stream throws `InvalidDataException`.
2626

27+
- `PlaDocument.OpenStream` and `PlaStream`: the decoded global stream of the document (every block's payload decoded in
28+
block order) and the extents of it. A directory record's position and count name a byte extent of the stream — the
29+
object's data — so `PlaStream.ReadExtent(ObjectDirectoryRecord)` returns an object's bytes; extents can also be read
30+
by position and count. The stream is a snapshot that holds the whole decoded file in memory and the document may be
31+
disposed afterwards.
32+
- The position and count of `ObjectDirectoryRecord` are now documented: a byte extent of the decoded global stream,
33+
confirmed against the object directories of real files.
34+
2735
### Changed
2836

2937
- The release workflow follows the IOExtend release order: the NuGet packages are pushed before the GitHub release is

‎README.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,8 @@ included in the repository. Files from other versions are not tested.
207207
## Limitations
208208

209209
- Version 0.1.0 is a preview. The API can change before 1.0.
210-
- The meaning of the `pos` and `count` fields of a directory record is not known, so the addressing of objects is not
211-
supported. Class numbers are not mapped to class names, and the contents of objects (elements, parameters, geometry)
212-
are not read.
210+
- `PlaDocument.OpenStream().ReadExtent(record)` reads object bytes for directory records that address the reliable range
211+
of the decoded stream. Class names and the contents of objects (elements, parameters, geometry) are not interpreted.
213212
- Several header and block fields are exposed as raw values because their meaning is not documented.
214213
- The block index is built by scanning for block headers from offset 32. It is not checked against an index in the file,
215214
so completeness is not claimed. The scan can also take a byte run for a block header, so the index may contain false

‎docs/cli.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ The limits are fixed; the tool has no option to change them.
3737
| Mode 2 (LZ) payloads | Listed by `blocks`. The CLI `directory` command reads raw blocks only. |
3838
| Object directories | Searched in raw (mode 0) blocks of at least 1920 bytes, at most one per block. A directory needs at least 64 records whose extents mostly abut. The run that is reported is an estimate and can be shorter than the real directory. |
3939

40-
The meaning of `pos` and `count` in a directory record is not known, class numbers are not mapped to class names and the
41-
contents of objects are not read, so the tool reports none of them beyond the raw numbers.
40+
The CLI reports `pos`, `count` and class numbers as stored and does not read object contents. The library exposes
41+
extent reads through `PlaDocument.OpenStream()`.
4242

4343
## Output format
4444

‎docs/getting-started.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ The supported surface is listed in [Public API](public-api.md); every type is in
6969

7070
## Status
7171

72-
PlaIO 0.1.0 is a preview and the API can change before 1.0. It does not interpret the position and count of directory records, does not map class numbers to class names and does not read object contents. In real files a small share of mode 1 blocks does not decode to its declared size, and `ReadPayload` throws `InvalidDataException` for them. The block index is found by scanning and may contain false entries.
72+
PlaIO 0.1.0 is a preview and the API can change before 1.0. Use `PlaDocument.OpenStream().ReadExtent(record)` to read object bytes at a directory record's position and count. The stream rejects extents beyond its reliable range. Class names and object contents are not interpreted. In real files a small share of mode 1 blocks does not decode to its declared size, and `ReadPayload` throws `InvalidDataException` for them. The block index is found by scanning and may contain false entries.

‎docs/public-api.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ The `PlaIO` package is the supported surface listed here; every public type is i
2323
| `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. |
2424
| `PlaDocument.ReadPayload(PlaBlock block)` | The decoded payload of a block of this document. Decodes all three storage modes: raw (mode 0), zero masks (mode 1) and the LZ token stream (mode 2). A mode 2 payload may start with a 9-byte stream header with a 0x46 or 0x47 marker and matching packed and unpacked sizes. The token stream produces the block's declared unpacked size — some blocks declare a buffer larger than the stream produces, the tail of the buffer stays zero — and may reference the output of any preceding block, so the payload is produced by decoding every preceding block of the file in order into one global window; the cost of the call grows with the size of the file. Preceding blocks that do not decode (false entries of the scan) are skipped and leave the window unchanged. Throws `InvalidDataException` when a back-reference reaches beyond the decoded stream or the file is cut short, and `ArgumentException` when the block is not from this document. |
2525
| `PlaDocument.FindDirectories()` | Searches every raw (mode 0) block large enough for 64 records of 30 bytes for an object directory, at most one per block. Recomputed on each call; mode 1 payloads are not searched. |
26+
| `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. |
2627
| `PlaDocument.Dispose()` | Releases the stream unless the document was opened with `leaveOpen`. |
2728
| `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). |
2829
| `PlaBlockMode` | `Raw` (0), `ZeroMask` (1, flag 0x100), `Lz` (2, flag 0x200). |
2930
| `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`. |
3031
| `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. |
31-
| `ObjectDirectoryRecord` | The 30-byte record `[u16 class][u64 pos][u32 count][GUID16]` as `ClassNumber`, `Position`, `Count`, `ObjectId`. The units of position and count are not documented, and the class number is not mapped to a class name. |
32+
| `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. |
33+
| `PlaStream.Length` | Length of the decoded stream in bytes, including the bytes beyond the unreliable boundary. |
34+
| `PlaStream.UnreliableFrom` | The offset of the first indexed block that failed to decode, or null when every indexed block decoded. |
35+
| `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. |
36+
| `PlaStream.ReadExtent(ObjectDirectoryRecord record)` | The bytes addressed by a directory record. Throws `InvalidDataException` if the extent exceeds the stream or reaches the unreliable range. |
3237

3338
## BIMx file (`.bimx`)
3439

@@ -45,8 +50,9 @@ The `PlaIO` package is the supported surface listed here; every public type is i
4550
## Resources and threads
4651

4752
`PlaDocument` and `BimxDocument` never load the whole file into memory: the block index is built by scanning through the
48-
stream once, at `Open`, and payloads and the cover are read on request. A document is not thread-safe; use one per thread.
49-
After `Dispose`, `ReadPayload`, `FindDirectories` and `ReadCover` throw `ObjectDisposedException`.
53+
stream once, at `Open`, and payloads and the cover are read on request. The exception is `OpenStream`: its decoded global
54+
stream holds the whole decoded file in memory by design. A document is not thread-safe; use one per thread.
55+
After `Dispose`, `ReadPayload`, `FindDirectories`, `OpenStream` and `ReadCover` throw `ObjectDisposedException`.
5056

5157
## Diagnostics
5258

‎src/PlaIO/ObjectDirectory.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace PlaIO;
22

3-
/// <summary>One 30-byte directory record: <c>[u16 class][u64 pos][u32 count][GUID16]</c>. The units of
4-
/// <see cref="Position"/> and <see cref="Count"/> are not documented.</summary>
3+
/// <summary>One 30-byte directory record: <c>[u16 class][u64 pos][u32 count][GUID16]</c>. The position and count name a
4+
/// byte extent of the decoded global stream (see <see cref="PlaDocument.OpenStream"/>), the object's data.</summary>
55
/// <param name="ClassNumber">The class number of the object, raw. It is not mapped to a name.</param>
6-
/// <param name="Position">Start of the object's extent, raw.</param>
7-
/// <param name="Count">Length of the object's extent, raw.</param>
6+
/// <param name="Position">Start of the object's extent in the decoded global stream.</param>
7+
/// <param name="Count">Length of the object's extent in bytes.</param>
88
/// <param name="ObjectId">The object GUID.</param>
99
public readonly record struct ObjectDirectoryRecord(ushort ClassNumber, ulong Position, uint Count, Guid ObjectId);
1010

‎src/PlaIO/PlaDocument.cs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ internal byte[] ReadPayload(PlaBlock block, out int consumed)
158158
}
159159
}
160160

161+
/// <summary>Decodes the whole file once into the global stream: the raw payloads, the full zero-mask decodes and the
162+
/// mode-2 outputs, in block order. If any indexed block fails to decode, the stream rejects reads that reach
163+
/// the resulting unreliable range. The stream holds the decoded bytes in memory independently of the document's
164+
/// lifetime. Object directory records address the stream by position and count.</summary>
165+
public PlaStream OpenStream()
166+
{
167+
var window = DecodeGlobalWindow(out var skipped);
168+
return new PlaStream(window, skipped.Count == 0 ? null : skipped[0].WindowPosition);
169+
}
170+
161171
/// <summary>Appends the decoded payload of one block to the mode-2 global window, or leaves the window unchanged when
162172
/// the block does not decode to its declared size (a false entry of the scan is not part of the real stream, so a
163173
/// partial decode is rolled back). Returns whether the block was appended.</summary>
@@ -178,15 +188,17 @@ private bool TryAppendToWindow(PlaBlock b, List<byte> window)
178188

179189
/// <summary>Decodes the whole file once into the global window: the raw payloads, the full zero-mask decodes and the
180190
/// mode-2 outputs, in block order. Blocks that do not decode to their declared size (false entries of the scan) are
181-
/// skipped and reported in <paramref name="skipped"/>; the window stays as if they were not there.</summary>
182-
internal List<byte> DecodeGlobalWindow(out IReadOnlyList<PlaBlock> skipped)
191+
/// skipped and reported in <paramref name="skipped"/> with the window offset each would have contributed; the window
192+
/// stays as if they were not there.</summary>
193+
internal List<byte> DecodeGlobalWindow(out IReadOnlyList<(PlaBlock Block, long WindowPosition)> skipped)
183194
{
184195
ObjectDisposedException.ThrowIf(_disposed, this);
185196
var window = new List<byte>();
186-
var failed = new List<PlaBlock>();
197+
var failed = new List<(PlaBlock, long)>();
187198
foreach (var b in _blocks)
188199
{
189-
if (!TryAppendToWindow(b, window)) failed.Add(b);
200+
int before = window.Count;
201+
if (!TryAppendToWindow(b, window)) failed.Add((b, before));
190202
}
191203
skipped = failed.AsReadOnly();
192204
return window;

‎src/PlaIO/PlaStream.cs‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace PlaIO;
2+
3+
/// <summary>The decoded global stream of a document: every block's payload decoded in block order — raw payloads as
4+
/// they are, zero-mask payloads as their full decode, mode 2 payloads as their token-stream output. The object
5+
/// directories (<see cref="PlaDocument.FindDirectories"/>) address this stream: a record's
6+
/// <see cref="ObjectDirectoryRecord.Position"/> and <see cref="ObjectDirectoryRecord.Count"/> name a byte extent of the
7+
/// stream, the object's data. The stream is a snapshot: it holds the whole decoded file in memory and never touches the
8+
/// document again, so the document may be disposed. Not thread-safe.</summary>
9+
/// <remarks>When an indexed block fails to decode, offsets at and past that block may be shifted. An extent that
10+
/// reaches the unreliable range throws, and <see cref="UnreliableFrom"/> names the boundary.</remarks>
11+
public sealed class PlaStream
12+
{
13+
private readonly List<byte> _bytes;
14+
15+
/// <summary>The offset of the first indexed block that failed to decode, or null when every indexed block decoded.
16+
/// Extent reads that reach this boundary throw because the following offsets may be shifted.</summary>
17+
public long? UnreliableFrom { get; }
18+
19+
internal PlaStream(List<byte> bytes, long? unreliableFrom)
20+
{
21+
_bytes = bytes;
22+
UnreliableFrom = unreliableFrom;
23+
}
24+
25+
/// <summary>Length of the decoded stream in bytes.</summary>
26+
public long Length => _bytes.Count;
27+
28+
/// <summary>The bytes of one extent of the stream.</summary>
29+
/// <exception cref="ArgumentOutOfRangeException">The bounds are negative or reach past the stream.</exception>
30+
/// <exception cref="InvalidDataException">The extent reaches into the range whose offsets are shifted, because
31+
/// blocks before it failed to decode (<see cref="UnreliableFrom"/>).</exception>
32+
public byte[] ReadExtent(long position, long count)
33+
{
34+
ArgumentOutOfRangeException.ThrowIfNegative(position);
35+
ArgumentOutOfRangeException.ThrowIfNegative(count);
36+
if (position > Length || count > Length - position)
37+
throw new ArgumentOutOfRangeException(nameof(count), $"The extent at {position}+{count} reaches past the decoded stream ({Length} bytes).");
38+
if (UnreliableFrom is { } from && (position >= from || position + count > from))
39+
throw new InvalidDataException($"The extent at {position}+{count} reaches the offsets shifted by skipped blocks (from {from} on the offsets are not reliable).");
40+
var extent = new byte[count];
41+
for (long i = 0; i < count; i++) extent[i] = _bytes[(int)(position + i)];
42+
return extent;
43+
}
44+
45+
/// <summary>The bytes of the extent one directory record addresses.</summary>
46+
/// <exception cref="InvalidDataException">The record's extent reaches past the decoded stream, or reaches into the
47+
/// range whose offsets are shifted because blocks before it failed to decode.</exception>
48+
public byte[] ReadExtent(ObjectDirectoryRecord record)
49+
{
50+
// A record position wider than the stream's long is past the stream by definition; cast after the check.
51+
if (record.Position > (ulong)Length)
52+
throw new InvalidDataException($"The directory record of {record.ObjectId} (class {record.ClassNumber}) addresses {record.Position}+{record.Count}, past the decoded stream ({Length} bytes).");
53+
long position = (long)record.Position;
54+
long count = record.Count;
55+
if (count > Length - position)
56+
throw new InvalidDataException($"The directory record of {record.ObjectId} (class {record.ClassNumber}) addresses {position}+{count}, past the decoded stream ({Length} bytes).");
57+
if (UnreliableFrom is { } from && (position >= from || position + count > from))
58+
throw new InvalidDataException($"The directory record of {record.ObjectId} (class {record.ClassNumber}) addresses {position}+{count}, into the offsets shifted by skipped blocks (from {from} on the offsets are not reliable).");
59+
return ReadExtent(position, count);
60+
}
61+
}

0 commit comments

Comments
 (0)