RetroArch
Classes | Macros | Typedefs | Enumerations | Functions | Variables
libchdr_chd.c File Reference
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libchdr/chd.h>
#include <libchdr/minmax.h>
#include <libchdr/cdrom.h>
#include <libchdr/huffman.h>
#include <retro_inline.h>
#include <streams/file_stream.h>
Include dependency graph for libchdr_chd.c:

Classes

struct  _codec_interface
 
struct  _map_entry
 
struct  _metadata_entry
 
struct  _chd_file
 

Macros

#define TRUE   1
 
#define FALSE   0
 
#define CHD_MAKE_TAG(a, b, c, d)   (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
 
#define PRINTF_MAX_HUNK   (0)
 
#define MAP_STACK_ENTRIES   512 /* max number of entries to use on the stack */
 
#define MAP_ENTRY_SIZE   16 /* V3 and later */
 
#define OLD_MAP_ENTRY_SIZE   8 /* V1-V2 */
 
#define METADATA_HEADER_SIZE   16 /* metadata header size */
 
#define CRCMAP_HASH_SIZE   4095 /* number of CRC hashtable entries */
 
#define MAP_ENTRY_FLAG_TYPE_MASK   0x0f /* what type of hunk */
 
#define MAP_ENTRY_FLAG_NO_CRC   0x10 /* no CRC is present */
 
#define CHD_V1_SECTOR_SIZE   512 /* size of a "sector" in the V1 header */
 
#define COOKIE_VALUE   0xbaadf00d
 
#define END_OF_LIST_COOKIE   "EndOfListCookie"
 
#define NO_MATCH   (~0)
 
#define MAP_ENTRY_TYPE_INVALID   0x0000 /* invalid type */
 
#define MAP_ENTRY_TYPE_COMPRESSED   0x0001 /* standard compression */
 
#define MAP_ENTRY_TYPE_UNCOMPRESSED   0x0002 /* uncompressed data */
 
#define MAP_ENTRY_TYPE_MINI   0x0003 /* mini: use offset as raw data */
 
#define MAP_ENTRY_TYPE_SELF_HUNK   0x0004 /* same as another hunk in this file */
 
#define MAP_ENTRY_TYPE_PARENT_HUNK   0x0005 /* same as a hunk in the parent file */
 
#define MAP_ENTRY_TYPE_2ND_COMPRESSED   0x0006 /* compressed with secondary algorithm (usually FLAC CDDA) */
 
#define SET_ERROR_AND_CLEANUP(err)   do { last_error = (err); goto cleanup; } while (0)
 
#define EARLY_EXIT(x)   do { (void)(x); goto cleanup; } while (0)
 
#define CHD_MAKE_TAG(a, b, c, d)   (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
 
#define CHD_CODEC_CD_ZLIB   CHD_MAKE_TAG('c','d','z','l')
 
#define CHD_CODEC_CD_LZMA   CHD_MAKE_TAG('c','d','l','z')
 
#define CHD_CODEC_CD_FLAC   CHD_MAKE_TAG('c','d','f','l')
 

Typedefs

typedef struct _codec_interface codec_interface
 
typedef struct _map_entry map_entry
 
typedef struct _metadata_entry metadata_entry
 

Enumerations

enum  {
  COMPRESSION_TYPE_0 = 0, COMPRESSION_TYPE_1 = 1, COMPRESSION_TYPE_2 = 2, COMPRESSION_TYPE_3 = 3,
  COMPRESSION_NONE = 4, COMPRESSION_SELF = 5, COMPRESSION_PARENT = 6, COMPRESSION_RLE_SMALL,
  COMPRESSION_RLE_LARGE, COMPRESSION_SELF_0, COMPRESSION_SELF_1, COMPRESSION_PARENT_SELF,
  COMPRESSION_PARENT_0, COMPRESSION_PARENT_1
}
 

Functions

static chd_error header_validate (const chd_header *header)
 
static chd_error header_read (chd_file *chd, chd_header *header)
 
static chd_error hunk_read_into_memory (chd_file *chd, UINT32 hunknum, UINT8 *dest)
 
static chd_error map_read (chd_file *chd)
 
static chd_error metadata_find_entry (chd_file *chd, UINT32 metatag, UINT32 metaindex, metadata_entry *metaentry)
 
static INLINE UINT64 get_bigendian_uint64 (const UINT8 *base)
 
static INLINE void put_bigendian_uint64 (UINT8 *base, UINT64 value)
 
static INLINE UINT64 get_bigendian_uint48 (const UINT8 *base)
 
static INLINE void put_bigendian_uint48 (UINT8 *base, UINT64 value)
 
static INLINE UINT32 get_bigendian_uint32 (const UINT8 *base)
 
static INLINE void put_bigendian_uint24 (UINT8 *base, UINT32 value)
 
static INLINE void put_bigendian_uint32 (UINT8 *base, UINT32 value)
 
static INLINE UINT32 get_bigendian_uint24 (const UINT8 *base)
 
static INLINE UINT16 get_bigendian_uint16 (const UINT8 *base)
 
static INLINE void put_bigendian_uint16 (UINT8 *base, UINT16 value)
 
static INLINE void map_extract (const UINT8 *base, map_entry *entry)
 
static INLINE void map_assemble (UINT8 *base, map_entry *entry)
 
static INLINE int map_size_v5 (chd_header *header)
 
uint16_t crc16 (const void *data, uint32_t length)
 
static chd_error decompress_v5_map (chd_file *chd, chd_header *header)
 
static INLINE void map_extract_old (const UINT8 *base, map_entry *entry, UINT32 hunkbytes)
 
chd_error chd_open_file (RFILE *file, int mode, chd_file *parent, chd_file **chd)
 
chd_error chd_open (const char *filename, int mode, chd_file *parent, chd_file **chd)
 
chd_error chd_precache (chd_file *chd)
 
void chd_close (chd_file *chd)
 
RFILEchd_core_file (chd_file *chd)
 
const char * chd_error_string (chd_error err)
 
const chd_headerchd_get_header (chd_file *chd)
 
chd_error chd_read (chd_file *chd, UINT32 hunknum, void *buffer)
 
chd_error chd_get_metadata (chd_file *chd, UINT32 searchtag, UINT32 searchindex, void *output, UINT32 outputlen, UINT32 *resultlen, UINT32 *resulttag, UINT8 *resultflags)
 
chd_error chd_codec_config (chd_file *chd, int param, void *config)
 
const char * chd_get_codec_name (UINT32 codec)
 
static UINT32 header_guess_unitbytes (chd_file *chd)
 
static UINT8read_compressed (chd_file *chd, UINT64 offset, size_t size)
 
static chd_error read_uncompressed (chd_file *chd, UINT64 offset, size_t size, UINT8 *dest)
 
static size_t core_fsize (RFILE *f)
 

Variables

static const UINT8 nullmd5 [CHD_MD5_BYTES] = { 0 }
 
static const UINT8 nullsha1 [CHD_SHA1_BYTES] = { 0 }
 
static const codec_interface codec_interfaces []
 

Macro Definition Documentation

◆ CHD_CODEC_CD_FLAC

#define CHD_CODEC_CD_FLAC   CHD_MAKE_TAG('c','d','f','l')

◆ CHD_CODEC_CD_LZMA

#define CHD_CODEC_CD_LZMA   CHD_MAKE_TAG('c','d','l','z')

◆ CHD_CODEC_CD_ZLIB

#define CHD_CODEC_CD_ZLIB   CHD_MAKE_TAG('c','d','z','l')

◆ CHD_MAKE_TAG [1/2]

#define CHD_MAKE_TAG (   a,
  b,
  c,
 
)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))

◆ CHD_MAKE_TAG [2/2]

#define CHD_MAKE_TAG (   a,
  b,
  c,
 
)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))

◆ CHD_V1_SECTOR_SIZE

#define CHD_V1_SECTOR_SIZE   512 /* size of a "sector" in the V1 header */

◆ COOKIE_VALUE

#define COOKIE_VALUE   0xbaadf00d

◆ CRCMAP_HASH_SIZE

#define CRCMAP_HASH_SIZE   4095 /* number of CRC hashtable entries */

◆ EARLY_EXIT

#define EARLY_EXIT (   x)    do { (void)(x); goto cleanup; } while (0)

◆ END_OF_LIST_COOKIE

#define END_OF_LIST_COOKIE   "EndOfListCookie"

◆ FALSE

#define FALSE   0

◆ MAP_ENTRY_FLAG_NO_CRC

#define MAP_ENTRY_FLAG_NO_CRC   0x10 /* no CRC is present */

◆ MAP_ENTRY_FLAG_TYPE_MASK

#define MAP_ENTRY_FLAG_TYPE_MASK   0x0f /* what type of hunk */

◆ MAP_ENTRY_SIZE

#define MAP_ENTRY_SIZE   16 /* V3 and later */

◆ MAP_ENTRY_TYPE_2ND_COMPRESSED

#define MAP_ENTRY_TYPE_2ND_COMPRESSED   0x0006 /* compressed with secondary algorithm (usually FLAC CDDA) */

◆ MAP_ENTRY_TYPE_COMPRESSED

#define MAP_ENTRY_TYPE_COMPRESSED   0x0001 /* standard compression */

◆ MAP_ENTRY_TYPE_INVALID

#define MAP_ENTRY_TYPE_INVALID   0x0000 /* invalid type */

◆ MAP_ENTRY_TYPE_MINI

#define MAP_ENTRY_TYPE_MINI   0x0003 /* mini: use offset as raw data */

◆ MAP_ENTRY_TYPE_PARENT_HUNK

#define MAP_ENTRY_TYPE_PARENT_HUNK   0x0005 /* same as a hunk in the parent file */

◆ MAP_ENTRY_TYPE_SELF_HUNK

#define MAP_ENTRY_TYPE_SELF_HUNK   0x0004 /* same as another hunk in this file */

◆ MAP_ENTRY_TYPE_UNCOMPRESSED

#define MAP_ENTRY_TYPE_UNCOMPRESSED   0x0002 /* uncompressed data */

◆ MAP_STACK_ENTRIES

#define MAP_STACK_ENTRIES   512 /* max number of entries to use on the stack */

◆ METADATA_HEADER_SIZE

#define METADATA_HEADER_SIZE   16 /* metadata header size */

◆ NO_MATCH

#define NO_MATCH   (~0)

◆ OLD_MAP_ENTRY_SIZE

#define OLD_MAP_ENTRY_SIZE   8 /* V1-V2 */

◆ PRINTF_MAX_HUNK

#define PRINTF_MAX_HUNK   (0)

◆ SET_ERROR_AND_CLEANUP

#define SET_ERROR_AND_CLEANUP (   err)    do { last_error = (err); goto cleanup; } while (0)

◆ TRUE

#define TRUE   1

Typedef Documentation

◆ codec_interface

◆ map_entry

typedef struct _map_entry map_entry

◆ metadata_entry

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
COMPRESSION_TYPE_0 
COMPRESSION_TYPE_1 
COMPRESSION_TYPE_2 
COMPRESSION_TYPE_3 
COMPRESSION_NONE 
COMPRESSION_SELF 
COMPRESSION_PARENT 
COMPRESSION_RLE_SMALL 
COMPRESSION_RLE_LARGE 
COMPRESSION_SELF_0 
COMPRESSION_SELF_1 
COMPRESSION_PARENT_SELF 
COMPRESSION_PARENT_0 
COMPRESSION_PARENT_1 

Function Documentation

◆ chd_close()

void chd_close ( chd_file chd)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ chd_codec_config()

chd_error chd_codec_config ( chd_file chd,
int  param,
void config 
)

◆ chd_core_file()

RFILE* chd_core_file ( chd_file chd)

◆ chd_error_string()

const char* chd_error_string ( chd_error  err)

◆ chd_get_codec_name()

const char* chd_get_codec_name ( UINT32  codec)

◆ chd_get_header()

const chd_header* chd_get_header ( chd_file chd)
Here is the caller graph for this function:

◆ chd_get_metadata()

chd_error chd_get_metadata ( chd_file chd,
UINT32  searchtag,
UINT32  searchindex,
void output,
UINT32  outputlen,
UINT32 resultlen,
UINT32 resulttag,
UINT8 resultflags 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ chd_open()

chd_error chd_open ( const char *  filename,
int  mode,
chd_file parent,
chd_file **  chd 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ chd_open_file()

chd_error chd_open_file ( RFILE file,
int  mode,
chd_file parent,
chd_file **  chd 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ chd_precache()

chd_error chd_precache ( chd_file chd)
Here is the call graph for this function:

◆ chd_read()

chd_error chd_read ( chd_file chd,
UINT32  hunknum,
void buffer 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ core_fsize()

static size_t core_fsize ( RFILE f)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ crc16()

uint16_t crc16 ( const void data,
uint32_t  length 
)
Here is the caller graph for this function:

◆ decompress_v5_map()

static chd_error decompress_v5_map ( chd_file chd,
chd_header header 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_bigendian_uint16()

static INLINE UINT16 get_bigendian_uint16 ( const UINT8 base)
static
Here is the caller graph for this function:

◆ get_bigendian_uint24()

static INLINE UINT32 get_bigendian_uint24 ( const UINT8 base)
static
Here is the caller graph for this function:

◆ get_bigendian_uint32()

static INLINE UINT32 get_bigendian_uint32 ( const UINT8 base)
static
Here is the caller graph for this function:

◆ get_bigendian_uint48()

static INLINE UINT64 get_bigendian_uint48 ( const UINT8 base)
static
Here is the caller graph for this function:

◆ get_bigendian_uint64()

static INLINE UINT64 get_bigendian_uint64 ( const UINT8 base)
static
Here is the caller graph for this function:

◆ header_guess_unitbytes()

static UINT32 header_guess_unitbytes ( chd_file chd)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ header_read()

static chd_error header_read ( chd_file chd,
chd_header header 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ header_validate()

static chd_error header_validate ( const chd_header header)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hunk_read_into_memory()

static chd_error hunk_read_into_memory ( chd_file chd,
UINT32  hunknum,
UINT8 dest 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ map_assemble()

static INLINE void map_assemble ( UINT8 base,
map_entry entry 
)
static
Here is the call graph for this function:

◆ map_extract()

static INLINE void map_extract ( const UINT8 base,
map_entry entry 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ map_extract_old()

static INLINE void map_extract_old ( const UINT8 base,
map_entry entry,
UINT32  hunkbytes 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ map_read()

static chd_error map_read ( chd_file chd)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ map_size_v5()

static INLINE int map_size_v5 ( chd_header header)
static
Here is the caller graph for this function:

◆ metadata_find_entry()

static chd_error metadata_find_entry ( chd_file chd,
UINT32  metatag,
UINT32  metaindex,
metadata_entry metaentry 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ put_bigendian_uint16()

static INLINE void put_bigendian_uint16 ( UINT8 base,
UINT16  value 
)
static
Here is the caller graph for this function:

◆ put_bigendian_uint24()

static INLINE void put_bigendian_uint24 ( UINT8 base,
UINT32  value 
)
static
Here is the caller graph for this function:

◆ put_bigendian_uint32()

static INLINE void put_bigendian_uint32 ( UINT8 base,
UINT32  value 
)
static
Here is the caller graph for this function:

◆ put_bigendian_uint48()

static INLINE void put_bigendian_uint48 ( UINT8 base,
UINT64  value 
)
static
Here is the caller graph for this function:

◆ put_bigendian_uint64()

static INLINE void put_bigendian_uint64 ( UINT8 base,
UINT64  value 
)
static
Here is the caller graph for this function:

◆ read_compressed()

static UINT8* read_compressed ( chd_file chd,
UINT64  offset,
size_t  size 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_uncompressed()

static chd_error read_uncompressed ( chd_file chd,
UINT64  offset,
size_t  size,
UINT8 dest 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ codec_interfaces

const codec_interface codec_interfaces[]
static

◆ nullmd5

const UINT8 nullmd5[CHD_MD5_BYTES] = { 0 }
static

◆ nullsha1

const UINT8 nullsha1[CHD_SHA1_BYTES] = { 0 }
static