Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ API between minor releases.
name, and the parts without a usable geometry; `TdbSceneResult.PartCount` counts the parts. The IFC, scene JSON and
HTML exports of `tdbio` warn on standard error how many parts are rectangular proxy boxes, and the last two also list
the findings. The warnings are not part of the exported data.
- `DbvDocument.DomainId`, `Generation`, `BaseOffset`, `CurrentOffset` and `LastEventOffset`: names for `Prologue[1]`,
`ListHeader[3]` and the byte offsets of the domain events.

### Breaking

- `DbvDocument` no longer has the raw properties `B`, `C`, `L1`, `L2` and `T` of 0.1.0. Use `BaseOffset` for `B`,
`CurrentOffset` for `T[1]`, `LastEventOffset` for `T[4]`, `ListHeader[3]` for `T[0]` (the reader rejects a file where
they differ) and `C9Records.Count` for `T[5]`. `C`, `L1`, `L2`, `T[2]` and `T[3]` have no property; their bytes stay
in `Payload`, at the offsets `docs/public-api.md` gives. The reader warns when `C` is not 5 or when `T[2]` or `T[3]`
is not 0; it does not check `L1` and `L2`.

### Changed

Expand Down
7 changes: 5 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ that breaks a consumer fails the pack. Record an intended removal in
exceptions, and values that are kept despite a mismatch as warnings.
- Treat every file as untrusted input: bound every read and size, and
report malformed data with `InvalidDataException` and its offset.
- Describe only what the code does. A field whose meaning is not known is
exposed as a raw value and documented as such.
- Describe only what the code does. A public property for a value whose meaning
is not known is named by its byte offset, as `Db1Document.IdMapField65`, and
documented as raw. A value whose role the files confirm gets a role name. A
value without a property of its own stays in the raw bytes that the type
gives, as `DbvDocument.Payload`.

## Validating output

Expand Down
9 changes: 8 additions & 1 deletion docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,17 @@ offset or line number. A warning never contains text from the file. Warnings are
| --- | --- |
| `TableSection` | A section of fixed-size rows, from `Db1Document.ReadTables()` or `DbvDocument.Sections`. `Ordinal`, `Offset`, `End`, `Key` (schema table number in `.db1`, else null), `RowSize`, `Descriptors` (primary and secondary index specifications), `RowsOffset` and `Rows`. `Rows` is a view over the data: a row is built when it is read. Typed bulk decoders (`AsObjects()`, `AsParts()`, `AsPoints()`, etc.) decode all rows into typed record structs. |
| `TableRow` | `Offset` (of the marker byte), `Marker` (0x04 active, 0x0C deleted/superseded in `.db1`), `Body` (`RowSize` raw bytes), `A` / `CreationTimeId` (creation transaction TimeId) and `B` / `DeletionTimeId` (deletion or superseding transaction TimeId). `IsActive` and `IsDeleted` inspect the marker. `TextEncoding`: the encoding the typed decoders read the text values with, the `TdbReadOptions.TextEncoding` the file was opened with or null. Typed decoders (`AsObject()`, `AsPart()`, `AsPoint()`, etc.) decode the row body into typed record structs. |
| `DbvDocument` | A DBV@ file: `Name`, `DomainId` (`Prologue[1]`), `Generation` (`ListHeader[3]`), `BaseOffset` (`B`), `EventLevel` (`C`), `CurrentOffset` (`T[1]`), `LastEventOffset` (`T[4]`), `Prologue`, `ListHeader`, `GuidRecords`, `B`, `C`, `L1`, `L2`, `T`, `C9Records`, `Sections` and `Payload`. The raw numbers are preserved; named aliases identify the domain, generation and event offsets. |
| `DbvDocument` | A DBV@ file: `Name`, `DomainId` (`Prologue[1]`), `Generation` (`ListHeader[3]`), `BaseOffset`, `CurrentOffset` and `LastEventOffset` (the byte offsets of the domain events), `Prologue`, `ListHeader`, `GuidRecords`, `C9Records`, `Sections` and `Payload`. Some of the numbers have no property; see below. |
| `DbvGuidRecord` | `Value` (raw), `Index` (1-based, checked against the position) and `Id`. |
| `DbvC9Record` | A record that starts with `C9 C0 CE DB`: four raw numbers `R0` to `R3`. |

After the GUID records of a DBV@ file come little-endian 32-bit words, from offset
`52 + Name.Length + 24 * GuidRecords.Count` of `Payload`: `B`, `C`, the length `K`, the `K` words of `L1`, the `K`
signed words of `L2` and the six words `T[0]` to `T[5]`. Warnings and errors name them like this. `BaseOffset` is `B`,
`CurrentOffset` is `T[1]` and `LastEventOffset` is `T[4]`. The reader warns when `C` is not 5, `T[1]` is not
`B + 32 * K`, `T[2]` or `T[3]` is not 0, or `T[4]` is not `T[1] - 32`. It rejects a file where `T[0]` differs from
`ListHeader[3]`. `T[5]` is the number of C9 records. `L1` and `L2` are not checked.

The walk of a model database starts at its first table section and must end exactly at the end of `Payload`; a section
without a key after one with a key, a gap or bytes after the last section is an error. The bytes before the first
section, other than the header and the id map, are not interpreted; read them from `Payload`.
Expand Down
76 changes: 76 additions & 0 deletions src/TdbIO/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Intentional removals of issue #11: the raw DBV@ fields B, C, L1, L2 and T are gone from DbvDocument.
When version 0.2.0 ships, PackageValidationBaselineVersion moves to it and this file can shrink. -->
<!-- https://learn.microsoft.com/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_B</Target>
<Left>lib/net10.0/TdbIO.dll</Left>
<Right>lib/net10.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_C</Target>
<Left>lib/net10.0/TdbIO.dll</Left>
<Right>lib/net10.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_L1</Target>
<Left>lib/net10.0/TdbIO.dll</Left>
<Right>lib/net10.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_L2</Target>
<Left>lib/net10.0/TdbIO.dll</Left>
<Right>lib/net10.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_T</Target>
<Left>lib/net10.0/TdbIO.dll</Left>
<Right>lib/net10.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_B</Target>
<Left>lib/net8.0/TdbIO.dll</Left>
<Right>lib/net8.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_C</Target>
<Left>lib/net8.0/TdbIO.dll</Left>
<Right>lib/net8.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_L1</Target>
<Left>lib/net8.0/TdbIO.dll</Left>
<Right>lib/net8.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_L2</Target>
<Left>lib/net8.0/TdbIO.dll</Left>
<Right>lib/net8.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:TdbIO.DbvDocument.get_T</Target>
<Left>lib/net8.0/TdbIO.dll</Left>
<Right>lib/net8.0/TdbIO.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
</Suppressions>
49 changes: 16 additions & 33 deletions src/TdbIO/DbvDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
namespace TdbIO;

/// <summary>A gzip file whose content starts with <c>DBV@</c>: a name, a GUID list, a few lists of numbers and table
/// sections. Most numbers have no known meaning and are given raw; values that differ from the ones always seen are kept
/// and reported in <see cref="TdbDocument.Warnings"/>.</summary>
/// sections. Some of the numbers have no property and stay in <see cref="Payload"/>. The reader checks only part of the
/// numbers; where one of those differs from what is always seen, it is reported in <see cref="TdbDocument.Warnings"/>
/// or the file is rejected.</summary>
public sealed class DbvDocument : TdbDocument
{
internal DbvDocument(IReadOnlyList<string> warnings, string name, IReadOnlyList<uint> prologue, IReadOnlyList<uint> listHeader,
IReadOnlyList<DbvGuidRecord> guidRecords, uint b, uint c, IReadOnlyList<uint> l1, IReadOnlyList<int> l2, IReadOnlyList<uint> t,
IReadOnlyList<DbvGuidRecord> guidRecords, uint baseOffset, uint currentOffset, uint lastEventOffset,
IReadOnlyList<DbvC9Record> c9Records, IReadOnlyList<TableSection> sections, ReadOnlyMemory<byte> payload)
: base(TdbFileKind.Dbv, warnings)
{
Name = name;
Prologue = prologue;
ListHeader = listHeader;
GuidRecords = guidRecords;
B = b;
C = c;
L1 = l1;
L2 = l2;
T = t;
BaseOffset = baseOffset;
CurrentOffset = currentOffset;
LastEventOffset = lastEventOffset;
C9Records = c9Records;
Sections = sections;
Payload = payload;
Expand All @@ -36,17 +35,17 @@ internal DbvDocument(IReadOnlyList<string> warnings, string name, IReadOnlyList<
/// <summary>The generation/version number of the domain file (from <see cref="ListHeader"/> word 3), equals <c>GuidRecords.Count + 1</c>.</summary>
public uint Generation => ListHeader[3];

/// <summary>The base byte offset of domain events (alias for <see cref="B"/>).</summary>
public uint BaseOffset => B;
/// <summary>The base byte offset of the domain events, as stored after the GUID records. The reader does not check
/// it on its own; it is part of the check of <see cref="CurrentOffset"/>.</summary>
public uint BaseOffset { get; }

/// <summary>The domain event level (alias for <see cref="C"/>; expected to be 5).</summary>
public uint EventLevel => C;
/// <summary>The byte offset after the domain events. The reader warns when it is not <c>BaseOffset + 32 · K</c>,
/// where <c>K</c> is the length stored before the lists.</summary>
public uint CurrentOffset { get; }

/// <summary>The current byte offset after domain events (from <see cref="T"/> word 1; equals <c>BaseOffset + 32 * L1.Count</c>).</summary>
public uint CurrentOffset => T[1];

/// <summary>The byte offset of the last domain event (from <see cref="T"/> word 4; equals <c>CurrentOffset - 32</c>).</summary>
public uint LastEventOffset => T[4];
/// <summary>The byte offset of the last domain event. The reader warns when it is not
/// <c>CurrentOffset - 32</c>.</summary>
public uint LastEventOffset { get; }

/// <summary>The six raw numbers after the name.</summary>
public IReadOnlyList<uint> Prologue { get; }
Expand All @@ -57,22 +56,6 @@ internal DbvDocument(IReadOnlyList<string> warnings, string name, IReadOnlyList<
/// <summary>The GUID records in file order.</summary>
public IReadOnlyList<DbvGuidRecord> GuidRecords { get; }

/// <summary>The raw number after the GUID records.</summary>
public uint B { get; }

/// <summary>The raw number after <see cref="B"/>.</summary>
public uint C { get; }

/// <summary>The first list of raw numbers; its length is stored before it.</summary>
public IReadOnlyList<uint> L1 { get; }

/// <summary>The second list of raw numbers, signed, as long as <see cref="L1"/>.</summary>
public IReadOnlyList<int> L2 { get; }

/// <summary>The six raw numbers after the lists. The first one equals the last number of <see cref="ListHeader"/>;
/// the last one is the number of <see cref="C9Records"/>.</summary>
public IReadOnlyList<uint> T { get; }

/// <summary>The records that start with <c>C9 C0 CE DB</c>.</summary>
public IReadOnlyList<DbvC9Record> C9Records { get; }

Expand Down
10 changes: 3 additions & 7 deletions src/TdbIO/Internal/DbvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ public static DbvDocument Read(byte[] payload, List<string> warnings, Encoding?
Expect(warnings, c == 5, "word C", p + 4);
long listLength = k32;
Bytes.Need(p, 12 + 8 * listLength + 24, length, "DBV@ lists");
p += 12;
var l1 = ReadU32s(d, p, (int)listLength);
p += 4 * listLength;
var l2 = new int[listLength];
for (int i = 0; i < l2.Length; i++, p += 4) l2[i] = Bytes.I32(d, p);
// L1 and L2 are skipped: the reader does not use or check them, and the payload keeps their bytes.
p += 12 + 8 * listLength;

var t = ReadU32s(d, p, 6);
if (t[0] != listHeader[3])
Expand All @@ -95,8 +92,7 @@ public static DbvDocument Read(byte[] payload, List<string> warnings, Encoding?

var sections = SectionWalker.Walk(payload, (int)p, 0, SectionMode.Dbv, warnings, textEncoding);
return new DbvDocument(warnings.AsReadOnly(), name, Array.AsReadOnly(prologue), Array.AsReadOnly(listHeader),
Array.AsReadOnly(records), b, c, Array.AsReadOnly(l1), Array.AsReadOnly(l2), Array.AsReadOnly(t), Array.AsReadOnly(c9),
sections, payload);
Array.AsReadOnly(records), b, t[1], t[4], Array.AsReadOnly(c9), sections, payload);
}

private static uint[] ReadU32s(ReadOnlySpan<byte> d, long at, int count)
Expand Down
4 changes: 3 additions & 1 deletion src/TdbIO/TdbIO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
<AssemblyName>TdbIO</AssemblyName>
<PackageId>TdbIO</PackageId>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- dotnet pack compares the public API of both target frameworks with the last published release. -->
<!-- dotnet pack compares the public API of both target frameworks with the last published release.
The suppressions name the removals of issue #11; move the baseline to 0.2.0 when that version ships. -->
<EnablePackageValidation>true</EnablePackageValidation>
<PackageValidationBaselineVersion>0.1.0</PackageValidationBaselineVersion>
<ApiCompatSuppressionFile>$(MSBuildProjectDirectory)\CompatibilitySuppressions.xml</ApiCompatSuppressionFile>
</PropertyGroup>

<!-- The native engine is everything except the target-format output in Ifc. CI compiles it together with the scene
Expand Down
18 changes: 10 additions & 8 deletions tests/TdbIO.Tests/DbvTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ public void FileWithoutSectionsIsRead()
Assert.Equal(2, doc.GuidRecords.Count);
Assert.Equal(new DbvGuidRecord(100, 1, new Guid("03020100-0504-0706-0809-0a0b0c0d0e0f")), doc.GuidRecords[0]);
Assert.Equal(new DbvGuidRecord(1, 2, new Guid("43424140-4544-4746-4849-4a4b4c4d4e4f")), doc.GuidRecords[1]);
Assert.Equal(0x400u, doc.B);
Assert.Equal(0x400u, doc.BaseOffset);
Assert.Equal(5u, doc.C);
Assert.Equal(5u, doc.EventLevel);
Assert.Equal(7u, doc.DomainId);
Assert.Equal(3u, doc.Generation);
Assert.Equal(new uint[] { 1, 2 }, doc.L1);
Assert.Equal(new[] { 32, -32 }, doc.L2);
Assert.Equal(new uint[] { 3, 0x440, 0, 0, 0x420, 2 }, doc.T);
Assert.Equal(0x440u, doc.CurrentOffset);
Assert.Equal(0x420u, doc.LastEventOffset);
Assert.Equal(new[] { new DbvC9Record(0, 1, 0, -1), new DbvC9Record(0, 1, 0, -1) }, doc.C9Records);
Assert.Empty(doc.Sections);
Assert.Equal(data, doc.Payload.ToArray());

// The layout docs/public-api.md gives for the numbers without a property, checked against the reader's offsets.
uint Word(int at) => BinaryPrimitives.ReadUInt32LittleEndian(doc.Payload.Span[at..]);
int raw = 52 + doc.Name.Length + 24 * doc.GuidRecords.Count;
int tail = raw + 12 + 8 * (int)Word(raw + 8);
Assert.Equal(doc.BaseOffset, Word(raw));
Assert.Equal(doc.CurrentOffset, Word(tail + 4));
Assert.Equal(doc.LastEventOffset, Word(tail + 16));
}

[Fact]
Expand Down Expand Up @@ -144,8 +146,9 @@ public void RecordCountWordOneMeansNoRecords()
{
var doc = DbvDocument.Create(new DbvParts { Records = [] }.Build());
Assert.Empty(doc.GuidRecords);
// The reader fails the whole file when the tail word T[0] differs from the record count word A[3], so a
// successful read implies T[0] = 1 here.
Assert.Equal(1u, doc.ListHeader[3]);
Assert.Equal(1u, doc.T[0]);
}

[Fact]
Expand Down Expand Up @@ -254,7 +257,6 @@ public void UnusualValuesAreKeptWithAWarning(string field)
string warning = Assert.Single(doc.Warnings);
Assert.Contains("offset 0x", warning);
Assert.DoesNotContain("Secret", warning);
if (field == "C") Assert.Equal(6u, doc.C);
if (field == "R") Assert.Equal(-2, doc.C9Records[0].R3);
}

Expand Down
1 change: 0 additions & 1 deletion tests/TdbIO.Tests/SampleTests.Tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public void EveryDbvFileOpens()
var doc = DbvDocument.Create(Gzip.Unpack(File.ReadAllBytes(file), TablesReadLimit));
long end = doc.Sections.Count > 0 ? doc.Sections[^1].End : -1;
if (doc.Sections.Count > 0 && end != doc.Payload.Length) failures.Add($"{at}: the sections do not end at the end of the data");
if (doc.T[0] != doc.ListHeader[3]) failures.Add($"{at}: T[0] differs from A[3]");
for (int i = 0; i < doc.GuidRecords.Count; i++)
if (doc.GuidRecords[i].Index != i + 1) failures.Add($"{at}: GUID record {i + 1} has another index");
foreach (string warning in doc.Warnings) output.WriteLine($"{at}: warning: {warning}");
Expand Down
Loading
Loading