Crate chd

source · []
Expand description

An implementation of the MAME CHD (Compressed Hunks of Data) format in pure Safe Rust, with support for CHD V1-5.

Supported Compression Codecs

chd-rs supports the following compression codecs.

  • None
  • Zlib/Zlib+/Zlib V5
  • CDZL (CD Zlib)
  • CDLZ (CD LZMA)
  • CDFL (CD FLAC)
  • FLAC (Raw FLAC)
  • LZMA (Raw LZMA)
  • Huffman (Huff)

AVHuff decompression is experimental and can be enabled with the avhuff feature.

Iterating over hunks

Because ChdHunk keeps a mutable reference to its owning ChdFile, direct iteration of hunks is not possible without Generic Associated Types. Instead, the hunk indices should be iterated over.

use std::fs::File;
use std::io::BufReader;
use chd::ChdFile;

let mut f = BufReader::new(File::open("file.chd")?);
let mut chd = ChdFile::open(&mut f, None)?;
let hunk_count = chd.header().hunk_count();
let hunk_size = chd.header().hunk_size();

// buffer to store uncompressed hunk data must be the same length as the hunk size.
let mut hunk_buf = vec![0u8; hunk_size as usize];
// buffer to store compressed data.
let mut cmp_buf = Vec::new();

for hunk_num in 0..hunk_count {
    let mut hunk = chd.hunk(hunk_num)?;
    hunk.read_hunk_in(&mut cmp_buf, &mut hunk_buf)?;
}

Iterating over Metadata

Metadata in a CHD file consists of a list of entries that contain offsets to the byte data of the metadata contents in the CHD file. The individual metadata entries can be iterated directly, but a reference to the source stream has to be provided to read the data.

use std::fs::File;
use std::io::BufReader;
use chd::ChdFile;

let mut f = BufReader::new(File::open("file.chd")?);
let mut chd = ChdFile::open(&mut f, None)?;
let entries = chd.metadata()?;
for entry in entries {
    let metadata = entry.read(&mut f)?;
}

chd-rs provides a helper to retrieve all metadata content at once for convenience.

use std::fs::File;
use std::io::BufReader;
use chd::ChdFile;
use chd::metadata::ChdMetadata;

let mut f = BufReader::new(File::open("file.chd")?);
let mut chd = ChdFile::open(&mut f, None)?;
let entries = chd.metadata()?;
let metadatas: Vec<ChdMetadata> = entries.try_into()?;

Modules

codecscodec_api

Implementations of decompression codecs used in MAME CHD.

Types and methods relating to header data for a CHD file.

huffmanhuffman_api

Implementation of the MAME CHD Huffman Decoder.

Types and methods relating to the CHD hunk map.

Types and methods relating to metadata stored in a CHD file.

Helpers and adapters for reading CHD files and hunks.

Structs

A CHD (MAME Compressed Hunks of Data) file.

A reference to a compressed Hunk in a CHD file.

Enums

Error types that may occur when reading a CHD file or hunk.

Type Definitions

Result type for chd-rs.