Path: blob/a-new-beginning/SharedDependencies/Sources/libchdr/include/chd.h
2 views
/***************************************************************************12chd.h34MAME Compressed Hunks of Data file format56****************************************************************************78Copyright Aaron Giles9All rights reserved.1011Redistribution and use in source and binary forms, with or without12modification, are permitted provided that the following conditions are13met:1415* Redistributions of source code must retain the above copyright16notice, this list of conditions and the following disclaimer.17* Redistributions in binary form must reproduce the above copyright18notice, this list of conditions and the following disclaimer in19the documentation and/or other materials provided with the20distribution.21* Neither the name 'MAME' nor the names of its contributors may be22used to endorse or promote products derived from this software23without specific prior written permission.2425THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR26IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED27WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE28DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,29INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES30(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR31SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)32HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,33STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING34IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE35POSSIBILITY OF SUCH DAMAGE.3637***************************************************************************/3839#pragma once4041#ifndef __CHD_H__42#define __CHD_H__4344#ifdef __cplusplus45extern "C" {46#endif4748#include "coretypes.h"495051/***************************************************************************5253Compressed Hunks of Data header format. All numbers are stored in54Motorola (big-endian) byte ordering. The header is 76 (V1) or 80 (V2)55bytes long.5657V1 header:5859[ 0] char tag[8]; // 'MComprHD'60[ 8] UINT32 length; // length of header (including tag and length fields)61[ 12] UINT32 version; // drive format version62[ 16] UINT32 flags; // flags (see below)63[ 20] UINT32 compression; // compression type64[ 24] UINT32 hunksize; // 512-byte sectors per hunk65[ 28] UINT32 totalhunks; // total # of hunks represented66[ 32] UINT32 cylinders; // number of cylinders on hard disk67[ 36] UINT32 heads; // number of heads on hard disk68[ 40] UINT32 sectors; // number of sectors on hard disk69[ 44] UINT8 md5[16]; // MD5 checksum of raw data70[ 60] UINT8 parentmd5[16]; // MD5 checksum of parent file71[ 76] (V1 header length)7273V2 header:7475[ 0] char tag[8]; // 'MComprHD'76[ 8] UINT32 length; // length of header (including tag and length fields)77[ 12] UINT32 version; // drive format version78[ 16] UINT32 flags; // flags (see below)79[ 20] UINT32 compression; // compression type80[ 24] UINT32 hunksize; // seclen-byte sectors per hunk81[ 28] UINT32 totalhunks; // total # of hunks represented82[ 32] UINT32 cylinders; // number of cylinders on hard disk83[ 36] UINT32 heads; // number of heads on hard disk84[ 40] UINT32 sectors; // number of sectors on hard disk85[ 44] UINT8 md5[16]; // MD5 checksum of raw data86[ 60] UINT8 parentmd5[16]; // MD5 checksum of parent file87[ 76] UINT32 seclen; // number of bytes per sector88[ 80] (V2 header length)8990V3 header:9192[ 0] char tag[8]; // 'MComprHD'93[ 8] UINT32 length; // length of header (including tag and length fields)94[ 12] UINT32 version; // drive format version95[ 16] UINT32 flags; // flags (see below)96[ 20] UINT32 compression; // compression type97[ 24] UINT32 totalhunks; // total # of hunks represented98[ 28] UINT64 logicalbytes; // logical size of the data (in bytes)99[ 36] UINT64 metaoffset; // offset to the first blob of metadata100[ 44] UINT8 md5[16]; // MD5 checksum of raw data101[ 60] UINT8 parentmd5[16]; // MD5 checksum of parent file102[ 76] UINT32 hunkbytes; // number of bytes per hunk103[ 80] UINT8 sha1[20]; // SHA1 checksum of raw data104[100] UINT8 parentsha1[20];// SHA1 checksum of parent file105[120] (V3 header length)106107V4 header:108109[ 0] char tag[8]; // 'MComprHD'110[ 8] UINT32 length; // length of header (including tag and length fields)111[ 12] UINT32 version; // drive format version112[ 16] UINT32 flags; // flags (see below)113[ 20] UINT32 compression; // compression type114[ 24] UINT32 totalhunks; // total # of hunks represented115[ 28] UINT64 logicalbytes; // logical size of the data (in bytes)116[ 36] UINT64 metaoffset; // offset to the first blob of metadata117[ 44] UINT32 hunkbytes; // number of bytes per hunk118[ 48] UINT8 sha1[20]; // combined raw+meta SHA1119[ 68] UINT8 parentsha1[20];// combined raw+meta SHA1 of parent120[ 88] UINT8 rawsha1[20]; // raw data SHA1121[108] (V4 header length)122123Flags:1240x00000001 - set if this drive has a parent1250x00000002 - set if this drive allows writes126127=========================================================================128129V5 header:130131[ 0] char tag[8]; // 'MComprHD'132[ 8] uint32_t length; // length of header (including tag and length fields)133[ 12] uint32_t version; // drive format version134[ 16] uint32_t compressors[4];// which custom compressors are used?135[ 32] uint64_t logicalbytes; // logical size of the data (in bytes)136[ 40] uint64_t mapoffset; // offset to the map137[ 48] uint64_t metaoffset; // offset to the first blob of metadata138[ 56] uint32_t hunkbytes; // number of bytes per hunk (512k maximum)139[ 60] uint32_t unitbytes; // number of bytes per unit within each hunk140[ 64] uint8_t rawsha1[20]; // raw data SHA1141[ 84] uint8_t sha1[20]; // combined raw+meta SHA1142[104] uint8_t parentsha1[20];// combined raw+meta SHA1 of parent143[124] (V5 header length)144145If parentsha1 != 0, we have a parent (no need for flags)146If compressors[0] == 0, we are uncompressed (including maps)147148V5 uncompressed map format:149150[ 0] uint32_t offset; // starting offset / hunk size151152V5 compressed map format header:153154[ 0] uint32_t length; // length of compressed map155[ 4] UINT48 datastart; // offset of first block156[ 10] uint16_t crc; // crc-16 of the map157[ 12] uint8_t lengthbits; // bits used to encode complength158[ 13] uint8_t hunkbits; // bits used to encode self-refs159[ 14] uint8_t parentunitbits; // bits used to encode parent unit refs160[ 15] uint8_t reserved; // future use161[ 16] (compressed header length)162163Each compressed map entry, once expanded, looks like:164165[ 0] uint8_t compression; // compression type166[ 1] UINT24 complength; // compressed length167[ 4] UINT48 offset; // offset168[ 10] uint16_t crc; // crc-16 of the data169170***************************************************************************/171172173/***************************************************************************174CONSTANTS175***************************************************************************/176177/* header information */178#define CHD_HEADER_VERSION 5179#define CHD_V1_HEADER_SIZE 76180#define CHD_V2_HEADER_SIZE 80181#define CHD_V3_HEADER_SIZE 120182#define CHD_V4_HEADER_SIZE 108183#define CHD_V5_HEADER_SIZE 124184185#define CHD_MAX_HEADER_SIZE CHD_V5_HEADER_SIZE186187/* checksumming information */188#define CHD_MD5_BYTES 16189#define CHD_SHA1_BYTES 20190191/* CHD global flags */192#define CHDFLAGS_HAS_PARENT 0x00000001193#define CHDFLAGS_IS_WRITEABLE 0x00000002194#define CHDFLAGS_UNDEFINED 0xfffffffc195196#define CHD_MAKE_TAG(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))197198/* compression types */199#define CHDCOMPRESSION_NONE 0200#define CHDCOMPRESSION_ZLIB 1201#define CHDCOMPRESSION_ZLIB_PLUS 2202#define CHDCOMPRESSION_AV 3203204#define CHD_CODEC_NONE 0205#define CHD_CODEC_ZLIB CHD_MAKE_TAG('z','l','i','b')206/* general codecs with CD frontend */207#define CHD_CODEC_CD_ZLIB CHD_MAKE_TAG('c','d','z','l')208#define CHD_CODEC_CD_LZMA CHD_MAKE_TAG('c','d','l','z')209#define CHD_CODEC_CD_FLAC CHD_MAKE_TAG('c','d','f','l')210211/* A/V codec configuration parameters */212#define AV_CODEC_COMPRESS_CONFIG 1213#define AV_CODEC_DECOMPRESS_CONFIG 2214215/* metadata parameters */216#define CHDMETATAG_WILDCARD 0217#define CHD_METAINDEX_APPEND ((UINT32)-1)218219/* metadata flags */220#define CHD_MDFLAGS_CHECKSUM 0x01 /* indicates data is checksummed */221222/* standard hard disk metadata */223#define HARD_DISK_METADATA_TAG CHD_MAKE_TAG('G','D','D','D')224#define HARD_DISK_METADATA_FORMAT "CYLS:%d,HEADS:%d,SECS:%d,BPS:%d"225226/* hard disk identify information */227#define HARD_DISK_IDENT_METADATA_TAG CHD_MAKE_TAG('I','D','N','T')228229/* hard disk key information */230#define HARD_DISK_KEY_METADATA_TAG CHD_MAKE_TAG('K','E','Y',' ')231232/* pcmcia CIS information */233#define PCMCIA_CIS_METADATA_TAG CHD_MAKE_TAG('C','I','S',' ')234235/* standard CD-ROM metadata */236#define CDROM_OLD_METADATA_TAG CHD_MAKE_TAG('C','H','C','D')237#define CDROM_TRACK_METADATA_TAG CHD_MAKE_TAG('C','H','T','R')238#define CDROM_TRACK_METADATA_FORMAT "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d"239#define CDROM_TRACK_METADATA2_TAG CHD_MAKE_TAG('C','H','T','2')240#define CDROM_TRACK_METADATA2_FORMAT "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d PREGAP:%d PGTYPE:%s PGSUB:%s POSTGAP:%d"241#define GDROM_OLD_METADATA_TAG CHD_MAKE_TAG('C','H','G','T')242#define GDROM_TRACK_METADATA_TAG CHD_MAKE_TAG('C', 'H', 'G', 'D')243#define GDROM_TRACK_METADATA_FORMAT "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%s PGSUB:%s POSTGAP:%d"244245/* standard A/V metadata */246#define AV_METADATA_TAG CHD_MAKE_TAG('A','V','A','V')247#define AV_METADATA_FORMAT "FPS:%d.%06d WIDTH:%d HEIGHT:%d INTERLACED:%d CHANNELS:%d SAMPLERATE:%d"248249/* A/V laserdisc frame metadata */250#define AV_LD_METADATA_TAG CHD_MAKE_TAG('A','V','L','D')251252/* CHD open values */253#define CHD_OPEN_READ 1254#define CHD_OPEN_READWRITE 2255256/* error types */257enum _chd_error258{259CHDERR_NONE,260CHDERR_NO_INTERFACE,261CHDERR_OUT_OF_MEMORY,262CHDERR_INVALID_FILE,263CHDERR_INVALID_PARAMETER,264CHDERR_INVALID_DATA,265CHDERR_FILE_NOT_FOUND,266CHDERR_REQUIRES_PARENT,267CHDERR_FILE_NOT_WRITEABLE,268CHDERR_READ_ERROR,269CHDERR_WRITE_ERROR,270CHDERR_CODEC_ERROR,271CHDERR_INVALID_PARENT,272CHDERR_HUNK_OUT_OF_RANGE,273CHDERR_DECOMPRESSION_ERROR,274CHDERR_COMPRESSION_ERROR,275CHDERR_CANT_CREATE_FILE,276CHDERR_CANT_VERIFY,277CHDERR_NOT_SUPPORTED,278CHDERR_METADATA_NOT_FOUND,279CHDERR_INVALID_METADATA_SIZE,280CHDERR_UNSUPPORTED_VERSION,281CHDERR_VERIFY_INCOMPLETE,282CHDERR_INVALID_METADATA,283CHDERR_INVALID_STATE,284CHDERR_OPERATION_PENDING,285CHDERR_NO_ASYNC_OPERATION,286CHDERR_UNSUPPORTED_FORMAT287};288typedef enum _chd_error chd_error;289290291292/***************************************************************************293TYPE DEFINITIONS294***************************************************************************/295296/* opaque types */297typedef struct _chd_file chd_file;298299300/* extract header structure (NOT the on-disk header structure) */301typedef struct _chd_header chd_header;302struct _chd_header303{304UINT32 length; /* length of header data */305UINT32 version; /* drive format version */306UINT32 flags; /* flags field */307UINT32 compression[4]; /* compression type */308UINT32 hunkbytes; /* number of bytes per hunk */309UINT32 totalhunks; /* total # of hunks represented */310UINT64 logicalbytes; /* logical size of the data */311UINT64 metaoffset; /* offset in file of first metadata */312UINT64 mapoffset; /* TOOD V5 */313UINT8 md5[CHD_MD5_BYTES]; /* overall MD5 checksum */314UINT8 parentmd5[CHD_MD5_BYTES]; /* overall MD5 checksum of parent */315UINT8 sha1[CHD_SHA1_BYTES]; /* overall SHA1 checksum */316UINT8 rawsha1[CHD_SHA1_BYTES]; /* SHA1 checksum of raw data */317UINT8 parentsha1[CHD_SHA1_BYTES]; /* overall SHA1 checksum of parent */318UINT32 unitbytes; /* TODO V5 */319UINT64 unitcount; /* TODO V5 */320UINT32 hunkcount; /* TODO V5 */321322/* map information */323UINT32 mapentrybytes; /* length of each entry in a map (V5) */324UINT8* rawmap; /* raw map data */325326UINT32 obsolete_cylinders; /* obsolete field -- do not use! */327UINT32 obsolete_sectors; /* obsolete field -- do not use! */328UINT32 obsolete_heads; /* obsolete field -- do not use! */329UINT32 obsolete_hunksize; /* obsolete field -- do not use! */330};331332333/* structure for returning information about a verification pass */334typedef struct _chd_verify_result chd_verify_result;335struct _chd_verify_result336{337UINT8 md5[CHD_MD5_BYTES]; /* overall MD5 checksum */338UINT8 sha1[CHD_SHA1_BYTES]; /* overall SHA1 checksum */339UINT8 rawsha1[CHD_SHA1_BYTES]; /* SHA1 checksum of raw data */340UINT8 metasha1[CHD_SHA1_BYTES]; /* SHA1 checksum of metadata */341};342343344345/***************************************************************************346FUNCTION PROTOTYPES347***************************************************************************/348349350/* ----- CHD file management ----- */351352/* create a new CHD file fitting the given description */353/* chd_error chd_create(const char *filename, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); */354355/* same as chd_create(), but accepts an already-opened core_file object */356/* chd_error chd_create_file(core_file *file, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); */357358/* open an existing CHD file */359chd_error chd_open(const char *filename, int mode, chd_file *parent, chd_file **chd);360361362/* close a CHD file */363void chd_close(chd_file *chd);364365/* return the associated core_file */366core_file *chd_core_file(chd_file *chd);367368/* return an error string for the given CHD error */369const char *chd_error_string(chd_error err);370371372373/* ----- CHD header management ----- */374375/* return a pointer to the extracted CHD header data */376const chd_header *chd_get_header(chd_file *chd);377378379380381/* ----- core data read/write ----- */382383/* read one hunk from the CHD file */384chd_error chd_read(chd_file *chd, UINT32 hunknum, void *buffer);385386387388/* ----- metadata management ----- */389390/* get indexed metadata of a particular sort */391chd_error chd_get_metadata(chd_file *chd, UINT32 searchtag, UINT32 searchindex, void *output, UINT32 outputlen, UINT32 *resultlen, UINT32 *resulttag, UINT8 *resultflags);392393394395396/* ----- codec interfaces ----- */397398/* set internal codec parameters */399chd_error chd_codec_config(chd_file *chd, int param, void *config);400401/* return a string description of a codec */402const char *chd_get_codec_name(UINT32 codec);403404#ifdef __cplusplus405}406#endif407408#endif /* __CHD_H__ */409410411