CHD is quickly becoming the de facto format for ROM image archival. This is problematic because there is very little if any documentation on the CHD format besides its implementations. To alleviate this issue, chd-rs is a from-scratch, memory safe, and well-documented decoder implementation of the CHD format written in pure Rust.

chd-rs supports all CHD versions from V1-5 and all CHD codecs for feature parity with the MAME reference implementation and wider support than libchdr:

  • Deflate (zlib)
  • Raw LZMA (lzma)
  • Raw FLAC (flac)
  • Huffman (huff)
  • CD Deflate (cdzl)
  • CD LZMA (cdlz)
  • CD FLAC (cdfl)
  • AV Huffman (avhu)

chd-rs also provides C bindings that are compatible with a limited subset of libchdr.

Out of the three prior existing implementations, only one (Aaru) is not a direct derivative of the reference implementation but lacks full support for reading CHD V5. The reference implementation being an integrated part of MAME has a high barrier to usage, and because of its goals, libchdr does not support the full suite of possible codecs. libchdr and the MAME reference implementation are also optimized for speed rather than readability. Being written in C, libchdr tightly ties together external library concerns further hurting readibility and making it unclear how to actually decode the CHD format.

chd-rs is a clean and complete implementation written in descriptive, idiomatic Rust with documentation on the quirks of the CHD format. Library concerns such as allocation primitives and callbacks are cleanly abstracted away thanks to Rust and do not pollute the codebase. Idiosyncratic primitives like bitstream.h are avoided where possible. The format implementation portions of chd-rs do not use unsafe code and is memory-safe. chd-rs has also been fuzzed to fix some validation bugs present in the reference implementation.

As chd-rs prioritizes readability and safety, chd-rs is not as performant as libchdr, but that’s OK: emulators should continue to use libchdr due to its speed and ease of integration compared to chd-rs.

My hope is that chd-rs will serve as useful documentation for further alternate implementations of the CHD format in the future. Having multiple, readable implementations of a file format is important for the long term feasibility of an archival format. If you are an emulator developer for an emulator not written in a C-compatible language, my hope is that chd-rs will be a useful reference in implementing your own decoder for CHD.

Built with ❤️ by @chyyran. Text content licensed under CC-BY-SA 4.0.