Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ instead of a dated snapshot.
- `chd_read_header_core_file_callbacks` public API.
- pkg-config file (`libchdr.pc`) installed alongside the library.
- Unity-build script (`unity.c`).
- CD sector access by LBA: `chd_cd_open` parses the track metadata
into a TOC (`chd_cd_get_toc`), and `chd_cd_read_sector` reads user
data, raw sectors or subcode by LBA, handling track padding and
stored or unstored pregaps (#155).
- Embedded cue sheets: a CD CHD may carry its cue sheet as `CUES`
metadata (`CDROM_CUESHEET_METADATA_TAG`), which keeps the INDEX
02+, CATALOG and ISRC entries CHDv5 track metadata drops.
`chd_cd_open` reads it into the TOC and `chd_cd_get_index` returns
any INDEX by track (#164).

### Changed

Expand Down Expand Up @@ -64,6 +73,10 @@ instead of a dated snapshot.
- `fseek` errors are now propagated as `CHDERR_READ_ERROR`.
- Only reads as much header data as the declared header length.
- Various memory and overflow bugs in hunk decoding (#132).
- CD track metadata gains bounded `CDROM_TRACK_METADATA_SCANF`,
`CDROM_TRACK_METADATA2_SCANF` and `GDROM_TRACK_METADATA_SCANF`
formats for `sscanf`, with `CD_METADATA_STRING_SIZE`; the
unbounded `*_FORMAT` macros overflow on crafted metadata (#165).

### Removed

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ endif()
set(CHDR_SOURCES
src/libchdr_bitstream.c
src/libchdr_cdrom.c
src/libchdr_cdrom_toc.c
src/libchdr_chd.c
src/libchdr_codec_avhuff.c
src/libchdr_codec_cdfl.c
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ libchdr is a standalone library for reading MAME's CHDv1-v5 formats.

The code is based off of MAME's old C codebase which read up to CHDv4 with OS-dependent features removed, and CHDv5 support backported from MAME's current C++ codebase.

## Embedded cue sheets

CHDv5 track metadata has no room for INDEX 02 and later, CATALOG or ISRC, which some Saturn and Dreamcast titles rely on. libchdr reads the original cue sheet when it is stored in the CHD as `CUES` metadata, and exposes those entries through `chd_cd_get_toc()` and `chd_cd_get_index()` (see `include/libchdr/cdrom.h`). `chdman addmeta` writes only to uncompressed CHDs, so add the cue sheet before compressing:

chdman createcd -c none -i game.cue -o tmp.chd
chdman addmeta -i tmp.chd --tag CUES --valuefile game.cue
chdman copy -i tmp.chd -o game.chd

Other readers ignore the tag, so the result is still an ordinary CHDv5 file.

libchdr is licensed under the BSD 3-Clause (see [LICENSE.txt](LICENSE.txt)) and uses third party libraries that are each distributed under their own terms (see each library's license in [deps/](deps/)).
82 changes: 82 additions & 0 deletions include/libchdr/cdrom.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,88 @@ enum
#define CD_FLAG_GDROM 0x00000001 /* disc is a GD-ROM, all tracks should be stored with GD-ROM metadata */
#define CD_FLAG_GDROMLE 0x00000002 /* legacy GD-ROM, with little-endian CDDA data */

/***************************************************************************
SECTOR ACCESS BY LBA

A CD CHD stores tracks back to back as 2448-byte frames (2352 sector
bytes, 96 subcode bytes), each track padded to CD_TRACK_PADDING frames,
and the layout lives in per-track metadata. chd_cd_open() parses that
metadata into a TOC and chd_cd_read_sector() reads by LBA, so callers no
longer redo the hunk and padding arithmetic themselves (#155).

LBAs follow MAME's logical addressing: LBA 0 is the start of track 1,
and pregaps and postgaps take up LBAs whether or not their sectors are
stored. Use msf_to_lba() / lba_to_msf() below to address by MSF.
***************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _chd_cd_track chd_cd_track;
struct _chd_cd_track
{
uint32_t number; /* track number, 1-based */
uint32_t type; /* CD_TRACK_* */
uint32_t subtype; /* CD_SUB_* */
uint32_t datasize; /* sector bytes stored per frame */
uint32_t subsize; /* subcode bytes stored per frame */
uint32_t frames; /* frames stored, pregap included when pgdatasize != 0 */
uint32_t pregap; /* pregap frames, INDEX 00 to INDEX 01 */
uint32_t pgdatasize; /* non-zero when the pregap is stored in the CHD */
uint32_t postgap; /* postgap frames, never stored */
uint32_t logframeofs; /* LBA of INDEX 01 */
uint32_t chdframeofs; /* first frame of the track within the CHD */
char isrc[13]; /* from an embedded cue sheet, "" if none */
};

typedef struct _chd_cd_toc chd_cd_toc;
struct _chd_cd_toc
{
uint32_t numtracks;
uint32_t flags; /* CD_FLAG_* */
uint32_t leadout; /* first LBA past the last track */
char catalog[14]; /* from an embedded cue sheet, "" if none */
chd_cd_track tracks[CD_MAX_TRACKS];
};

/* what chd_cd_read_sector() returns, and how many bytes it writes */
enum
{
CHD_CD_READ_DATA = 0, /* 2048 bytes of user data; data tracks only */
CHD_CD_READ_RAW, /* CD_MAX_SECTOR_DATA bytes as a .bin would hold them:
* audio little-endian, a cooked track's datasize
* bytes followed by zeros */
CHD_CD_READ_SUBCODE /* CD_MAX_SUBCODE_DATA bytes, zeros if none stored */
};

typedef struct _chd_cd chd_cd;

/* Parse the track metadata of an open CD CHD. The handle borrows `chd`,
* which must outlive it. CHDERR_INVALID_METADATA when the track metadata is
* missing or inconsistent. */
CHD_EXPORT chd_error chd_cd_open(chd_file *chd, chd_cd **cd);
CHD_EXPORT void chd_cd_close(chd_cd *cd);
CHD_EXPORT const chd_cd_toc *chd_cd_get_toc(const chd_cd *cd);

/* Read one sector. Sectors of a pregap or postgap that the CHD does not
* store read as zeros. The last decoded hunk is kept, so reading the frames
* of a hunk in a row decodes it once. CHDERR_HUNK_OUT_OF_RANGE past the
* leadout; CHDERR_INVALID_PARAMETER for CHD_CD_READ_DATA on an audio or
* Mode 2 Form 2 track. */
CHD_EXPORT chd_error chd_cd_read_sector(chd_cd *cd, uint32_t lba, void *buffer, uint32_t what);

/* LBA of INDEX `index` (0-99) of track `track` (1-based). INDEX 01 is always
* known, INDEX 00 when the track has a pregap, and any other INDEX when an
* embedded cue sheet (CDROM_CUESHEET_METADATA_TAG) lists it; otherwise
* CHDERR_METADATA_NOT_FOUND. A cue sheet whose tracks do not match the
* CHD's is ignored as a whole, catalog and ISRCs included. */
CHD_EXPORT chd_error chd_cd_get_index(const chd_cd *cd, uint32_t track, uint32_t index, uint32_t *lba);

#ifdef __cplusplus
}
#endif

/***************************************************************************
FUNCTION PROTOTYPES
***************************************************************************/
Expand Down
18 changes: 18 additions & 0 deletions include/libchdr/chd.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ extern "C" {
#define GDROM_TRACK_METADATA_TAG CHD_MAKE_TAG('C', 'H', 'G', 'D')
#define GDROM_TRACK_METADATA_FORMAT "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%s PGSUB:%s POSTGAP:%d"

/* The cue sheet a CD CHD was made from, as plain text. A libchdr extension,
* not written by chdman createcd: CHDv5 track metadata has no room for
* INDEX 02+, CATALOG or ISRC, and the cue sheet keeps them (#164). Add it to
* an uncompressed CHD, then compress - chdman addmeta cannot write to a
* compressed one:
* chdman createcd -c none -i game.cue -o tmp.chd
* chdman addmeta -i tmp.chd --tag CUES --valuefile game.cue
* chdman copy -i tmp.chd -o game.chd */
#define CDROM_CUESHEET_METADATA_TAG CHD_MAKE_TAG('C','U','E','S')
/* Bounded variants of the three formats above, for sscanf(). The %s fields
* would overflow on a crafted CHD; every string target here must be
* char[CD_METADATA_STRING_SIZE]. The *_FORMAT macros stay unbounded on
* purpose: in printf a width pads rather than limits (#165). */
#define CD_METADATA_STRING_SIZE 16
#define CDROM_TRACK_METADATA_SCANF "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d"
#define CDROM_TRACK_METADATA2_SCANF "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"
#define GDROM_TRACK_METADATA_SCANF "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"

/* standard A/V metadata */
#define AV_METADATA_TAG CHD_MAKE_TAG('A','V','A','V')
#define AV_METADATA_FORMAT "FPS:%d.%06d WIDTH:%d HEIGHT:%d INTERLACED:%d CHANNELS:%d SAMPLERATE:%d"
Expand Down
Loading
Loading