|
| 1 | +# Pcf — Partitioned Container Format (C# / .NET implementation) |
| 2 | + |
| 3 | +A C# reader/writer for **PCF v1.0**, a language-agnostic binary container that |
| 4 | +stores multiple independent byte regions ("partitions") in one file. |
| 5 | + |
| 6 | +This is the first port of the Rust [reference implementation](../../reference/PCF-v1.0) |
| 7 | +and tracks the written specification ([`PCF-spec-v1.0.txt`](../../specs/PCF-spec-v1.0.txt)) |
| 8 | +field-for-field. It is verified byte-for-byte against the spec's canonical |
| 9 | +395-byte test vector (section 15) and, like the reference, favours auditability |
| 10 | +over performance. |
| 11 | + |
| 12 | +## Layout |
| 13 | + |
| 14 | +``` |
| 15 | +[ 20-byte header ] [ table block(s) ] [ partition data regions ] |
| 16 | +``` |
| 17 | + |
| 18 | +* **Header** (20 B): magic `0x89 K P R T 0x0D 0x0A 0x1A`, major/minor version, |
| 19 | + absolute offset of the first table block. |
| 20 | +* **Table block**: 74-byte header (`partition_count`, `next_table_offset`, |
| 21 | + hash algo + 64-byte block hash) followed by `partition_count` entries. |
| 22 | + Blocks form a singly linked chain to hold more than 255 partitions. |
| 23 | +* **Entry** (141 B): `type`, 16-byte UID, 32-byte ASCII label, `start_offset`, |
| 24 | + `max_length`, `used_bytes`, 1-byte data-hash algorithm, 64-byte data hash. |
| 25 | + |
| 26 | +All integers are little-endian. Free space is `max_length - used_bytes`. |
| 27 | + |
| 28 | +## Hash registry |
| 29 | + |
| 30 | +| id | algorithm | id | algorithm | |
| 31 | +|----|------------------|----|-----------| |
| 32 | +| 0 | none | 5 | SHA-1 | |
| 33 | +| 1 | CRC-32/ISO-HDLC | 16 | SHA-256 (default) | |
| 34 | +| 2 | CRC-32C | 17 | SHA-512 | |
| 35 | +| 3 | CRC-64/XZ | 18 | BLAKE3 | |
| 36 | +| 4 | MD5 | | | |
| 37 | + |
| 38 | +SHA-1/2 and MD5 use `System.Security.Cryptography`; the three CRCs are |
| 39 | +self-contained reflected-CRC routines (`Crc.cs`); BLAKE3 uses the |
| 40 | +[`Blake3`](https://www.nuget.org/packages/Blake3) NuGet package. |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +```csharp |
| 45 | +using System.IO; |
| 46 | +using System.Text; |
| 47 | +using Pcf; |
| 48 | + |
| 49 | +var c = Container.Create(new MemoryStream()); |
| 50 | +c.AddPartition( |
| 51 | + partitionType: 0x10, |
| 52 | + uid: new byte[16] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 53 | + label: "notes", |
| 54 | + data: Encoding.ASCII.GetBytes("hello"), |
| 55 | + extraReserve: 64, |
| 56 | + dataHashAlgo: HashAlgo.Sha256); |
| 57 | + |
| 58 | +c.Verify(); |
| 59 | + |
| 60 | +foreach (PartitionEntry e in c.Entries()) |
| 61 | +{ |
| 62 | + byte[] data = c.ReadPartitionData(e); |
| 63 | + // ... |
| 64 | +} |
| 65 | + |
| 66 | +// Reclaim unused reservations and produce the canonical, tightly packed form: |
| 67 | +byte[] image = c.CompactedImage(); |
| 68 | +``` |
| 69 | + |
| 70 | +`Container` is backed by any readable/writable/seekable `System.IO.Stream` |
| 71 | +(both `MemoryStream` and `FileStream` work) — the C# analogue of the |
| 72 | +reference's `Read + Write + Seek` store. Operations raise `PcfException`, whose |
| 73 | +`Kind` (`PcfError`) identifies the exact failure. |
| 74 | + |
| 75 | +## Project structure |
| 76 | + |
| 77 | +``` |
| 78 | +implementations/dotnet/ |
| 79 | +├── Pcf.sln |
| 80 | +├── src/Pcf/ # the library (targets netstandard2.0) |
| 81 | +│ ├── Constants.cs # on-disk constants (spec Appendix A) |
| 82 | +│ ├── PcfError.cs # PcfError + PcfException |
| 83 | +│ ├── LittleEndian.cs # explicit little-endian helpers |
| 84 | +│ ├── Crc.cs # CRC-32 / CRC-32C / CRC-64 |
| 85 | +│ ├── HashAlgo.cs # hash-algorithm registry (spec 8) |
| 86 | +│ ├── FileHeader.cs # 20-byte header (spec 4) |
| 87 | +│ ├── PartitionEntry.cs # 141-byte entry + labels (spec 5.2, 10) |
| 88 | +│ ├── TableBlockHeader.cs # 74-byte block header + table hash (spec 5.1, 8.4) |
| 89 | +│ └── Container.cs # high-level reader/writer |
| 90 | +└── tests/Pcf.Tests/ # xUnit suite (targets net8.0) |
| 91 | + ├── SpecComplianceTests.cs # one assertion per normative MUST/SHALL |
| 92 | + ├── RoundtripTests.cs # end-to-end create/read/verify/update/remove/compact |
| 93 | + └── CoverageTests.cs # error paths, every hash algo, label edge cases |
| 94 | +``` |
| 95 | + |
| 96 | +The library targets `netstandard2.0` for broad reach; the test project targets |
| 97 | +`net8.0`. BLAKE3 is provided by `Blake3` 0.6.1 — the last release that ships a |
| 98 | +`netstandard2.0` assembly together with native runtimes for linux/macOS/windows |
| 99 | +(x64 + arm64). |
| 100 | + |
| 101 | +## Building and testing |
| 102 | + |
| 103 | +```sh |
| 104 | +cd implementations/dotnet |
| 105 | +dotnet build -c Release |
| 106 | +dotnet test -c Release |
| 107 | +``` |
| 108 | + |
| 109 | +The section-15 test (`S15_canonical_vector_is_byte_exact`) proves this |
| 110 | +implementation emits the spec's exact 395-byte file. |
0 commit comments