Packfiles are the primary mechanism used by Anvil and Git for compact persistent storage and high-throughput network transmission. This document details the exact binary layout, delta encoding formats, and index lookup structures implemented in Anvil (src/odb/anvil_pack.pas, src/odb/anvil_pack_index.pas, src/odb/anvil_pack_writer.pas).
.pack)A packfile is a binary container starting with a 12-byte header, followed by $N$ contiguous indexed objects, terminated by a 20-byte SHA-1 trailer checksum.
+-------------------------------------------------------------------+
| 4-byte Signature: 'PACK' (0x5041434B) |
+-------------------------------------------------------------------+
| 4-byte Version: 2 (0x00000002) (Big-Endian Network Byte Order) |
+-------------------------------------------------------------------+
| 4-byte Object Count: N (Big-Endian Network Byte Order) |
+-------------------------------------------------------------------+
| Object Entry 1: |
| Variable-length type and uncompressed size header |
| [Optional: Delta base offset or 20-byte base object ID] |
| zlib-compressed payload (DEFLATE stream) |
+-------------------------------------------------------------------+
| Object Entry 2: ... |
+-------------------------------------------------------------------+
| ... |
+-------------------------------------------------------------------+
| Object Entry N: ... |
+-------------------------------------------------------------------+
| 20-byte SHA-1 Checksum across all preceding bytes |
+-------------------------------------------------------------------+
The first byte of each entry in a packfile combines the object type and the least-significant 4 bits of its uncompressed size:
Bit 7: MSB (1 = more size bytes follow; 0 = final byte of size)
Bit 6-4: Object type identifier
001 (1) = OBJ_COMMIT
010 (2) = OBJ_TREE
011 (3) = OBJ_BLOB
100 (4) = OBJ_TAG
110 (6) = OBJ_OFS_DELTA
111 (7) = OBJ_REF_DELTA
Bit 3-0: Bits 0-3 of uncompressed size
Subsequent size bytes provide 7 bits of size per byte:
Bit 7: MSB (1 = more size bytes follow; 0 = final byte)
Bit 6-0: Next 7 bits of uncompressed size (shifted by 4, 11, 18, 25, ...)
Anvil supports both OBJ_OFS_DELTA (preferred for local storage efficiency) and OBJ_REF_DELTA (used when base objects reside outside the packfile or during thin-pack network transfer).
An OBJ_OFS_DELTA entry encodes the negative byte offset from the start of the current object header back to the base object header using a variable-length biased encoding:
Offset := (Offset + 1) shl 7 + (B and $7F);
BaseOffset := CurrentOffset - Offset.Once decompressed via zlib, the delta payload begins with two variable-length integers:
Following the header is a stream of copy and insert operational opcodes:
MSB = 1):Instructs the engine to copy a slice of bytes from the base object into the output target:
Bit 7: 1 (Copy instruction)
Bit 3-0: Bitmask indicating which 4 bytes of source offset follow (Offset LSB to MSB)
Bit 6-4: Bitmask indicating which 2 or 3 bytes of slice length follow (Length LSB to MSB)
If length resolves to zero, it defaults to 65,536 bytes (0x10000).
MSB = 0):Instructs the engine to insert literal bytes following the opcode directly into the output target:
Bit 7: 0 (Insert instruction)
Bit 6-0: Byte count (1 to 127 bytes)
Followed immediately by N literal bytes.
.idx v2)Index v2 files (anvil_pack_index.pas) map object hashes directly to their byte offsets within the companion .pack file with $O(\log_2(N/256))$ lookup time.
+-------------------------------------------------------------------+
| 4-byte Magic: 0xFF744F63 ('\xFFtOc') |
+-------------------------------------------------------------------+
| 4-byte Version: 2 (0x00000002) |
+-------------------------------------------------------------------+
| 256-Entry Level-1 Fanout Table (256 * 4 bytes = 1,024 bytes) |
| Fanout[i] stores the cumulative count of objects whose SHA-1 |
| starts with a byte <= i. Fanout[255] equals total object count. |
+-------------------------------------------------------------------+
| Table of Object Names (N * 20 bytes): |
| 20-byte SHA-1 hashes sorted lexicographically. |
+-------------------------------------------------------------------+
| Table of CRC-32 Values (N * 4 bytes): |
| CRC-32 checksums of each object's compressed wire bytes. |
+-------------------------------------------------------------------+
| Table of Packfile Offsets (N * 4 bytes): |
| 31-bit byte offsets into the .pack file. If bit 31 is set, |
| the lower 31 bits index into the 64-bit Large Offset Table. |
+-------------------------------------------------------------------+
| Optional 64-bit Large Offset Table (M * 8 bytes): |
| Used when packfile size exceeds 2 GB. |
+-------------------------------------------------------------------+
| 20-byte SHA-1 of the corresponding .pack file |
+-------------------------------------------------------------------+
| 20-byte SHA-1 trailer of all preceding index bytes |
+-------------------------------------------------------------------+
To locate object digest $H$ in the index:
if B = 0 then Low := 0 else Low := Fanout[B - 1];
High := Fanout[B] - 1;
Anvil enforces rigorous multi-layered verification:
.idx must match SHA-1 over the entire index body..pack must match SHA-1 over all pack bytes..idx must exactly match the .pack file trailer.type + space + size + \0 + payload) to verify that their content digest matches their registered object ID.