Parent/Child CHDs
CHD supports delta (incremental) images: a child CHD stores only the hunks that differ from its parent; identical hunks become parent references. This is how MAME ships multi-disc or regional variants without duplicating identical data.
How it works
- The child header stores the parentβs
md5(V1βV3) and/orsha1(V3βV5) hashes. - Map entries of type
PARENTpoint into the parentβs data instead of local storage. - In V1βV4, a parent reference is a direct hunk index into the parent.
- In V5, a parent reference is a unit index (unit =
hunkbytes / unitbytessubdivision, e.g. 512-byte sectors inside 4096-byte hunks). References can be unaligned β a hunk may need the tail of parent hunk N and the head of parent hunk N+1; CHDSharp stitches the two halves.
child.chd parent.chd
βββββββββββββββββββββ βββββββββββββββββββββ
β hunk 0: compressedβ β hunk 0: data β
β hunk 1: PARENTβu4 ββββββββββββΆβ hunk 1: data β
β hunk 2: compressedβ β hunk 2: data β
β hunk 3: PARENTβu7 ββββββββ β hunk 3: data β
βββββββββββββββββββββ βββββΆβ hunk 4: data β
βββββββββββββββββββββ
Opening child CHDs
Three ways, matching the three Open overloads:
// 1. Path-based: the library opens the parent and owns it.
var err = ChdFile.Open("child.chd", "parent.chd", out var child);
using (child) { ... }
// 2. External parent instance: caller keeps ownership, may share it.
ChdFile.Open("parent.chd", out var parent);
using (parent)
{
foreach (var childPath in new[] { "child1.chd", "child2.chd" })
{
ChdFile.Open(childPath, parent, out var c);
using (c) { /* read hunks; parent hunks resolve through `parent` */ }
}
}
// 3. From streams.
using var childStream = File.OpenRead("child.chd");
ChdFile.Open(childStream, leaveOpen: false, parent, out var child2);
Async twins exist for all three (OpenAsync).
Error semantics
| Situation | Result |
|---|---|
| Child opened without a parent | Chderrrequiresparent |
Supplied parentβs md5/sha1 does not match the childβs stored parent hashes |
Chderrinvalidparent |
| Parent-referenced hunk read when no parent is attached | Chderrrequiresparent (from ReadHunk) |
Parent validation happens at open time: the childβs stored parentmd5/parentsha1 is compared against the actual parentβs Md5/Sha1 (when both are non-empty).
Verification
var result = Chd.CheckFileWithParent("child.chd", "parent.chd");
if (result.IsSuccess)
Console.WriteLine($"child V{result.Version} verified against parent");
CheckFileWithParent decompresses the child and the referenced parent hunks and validates every hash β this is the single-threaded counterpart of CheckFile (which is standalone-only).
Reading through a child
From the consumerβs point of view, reading is transparent:
var err = ChdFile.Open("child.chd", "parent.chd", out var child);
using (child)
{
var hunk = new byte[child.HunkBytes];
for (uint i = 0; i < child.HunkCount; i++)
{
var herr = child.ReadHunk(i, hunk); // local or parent data, same call
if (herr != ChdError.Chderrnone) break;
}
}
Read, ReadAllBytes, EnumerateHunks, and extraction all work identically on child CHDs.
Hunk resolution details
ReadHunk resolves a map entry as follows:
PARENTβReadParentHunk:- V1βV4 (and uncompressed V5 maps): direct parent hunk index.
- V5 compressed maps: convert the unit index to parent hunk(s):
- aligned β one parent hunk;
- unaligned β two adjacent parent hunks stitched at the unit boundary.
SELFβ follow the self-reference to the entry that holds real data (and use the decompressed cache when the block is repeated).- Otherwise β read local compressed bytes (stream or
Precachebuffer) and decompress.
The child keeps a reference to the parent (_parent); when the child was opened with a parent path, it owns the parent and disposes it together with itself.