RetroArch
chd.h
Go to the documentation of this file.
1 /***************************************************************************
2 
3  chd.h
4 
5  MAME Compressed Hunks of Data file format
6 
7 ****************************************************************************
8 
9  Copyright Aaron Giles
10  All rights reserved.
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions are
14  met:
15 
16  * Redistributions of source code must retain the above copyright
17  notice, this list of conditions and the following disclaimer.
18  * Redistributions in binary form must reproduce the above copyright
19  notice, this list of conditions and the following disclaimer in
20  the documentation and/or other materials provided with the
21  distribution.
22  * Neither the name 'MAME' nor the names of its contributors may be
23  used to endorse or promote products derived from this software
24  without specific prior written permission.
25 
26  THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38 ***************************************************************************/
39 
40 #pragma once
41 
42 #ifndef __CHD_H__
43 #define __CHD_H__
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #include "coretypes.h"
50 #include <streams/file_stream.h>
51 
52 
53 /***************************************************************************
54 
55  Compressed Hunks of Data header format. All numbers are stored in
56  Motorola (big-endian) byte ordering. The header is 76 (V1) or 80 (V2)
57  bytes long.
58 
59  V1 header:
60 
61  [ 0] char tag[8]; // 'MComprHD'
62  [ 8] UINT32 length; // length of header (including tag and length fields)
63  [ 12] UINT32 version; // drive format version
64  [ 16] UINT32 flags; // flags (see below)
65  [ 20] UINT32 compression; // compression type
66  [ 24] UINT32 hunksize; // 512-byte sectors per hunk
67  [ 28] UINT32 totalhunks; // total # of hunks represented
68  [ 32] UINT32 cylinders; // number of cylinders on hard disk
69  [ 36] UINT32 heads; // number of heads on hard disk
70  [ 40] UINT32 sectors; // number of sectors on hard disk
71  [ 44] UINT8 md5[16]; // MD5 checksum of raw data
72  [ 60] UINT8 parentmd5[16]; // MD5 checksum of parent file
73  [ 76] (V1 header length)
74 
75  V2 header:
76 
77  [ 0] char tag[8]; // 'MComprHD'
78  [ 8] UINT32 length; // length of header (including tag and length fields)
79  [ 12] UINT32 version; // drive format version
80  [ 16] UINT32 flags; // flags (see below)
81  [ 20] UINT32 compression; // compression type
82  [ 24] UINT32 hunksize; // seclen-byte sectors per hunk
83  [ 28] UINT32 totalhunks; // total # of hunks represented
84  [ 32] UINT32 cylinders; // number of cylinders on hard disk
85  [ 36] UINT32 heads; // number of heads on hard disk
86  [ 40] UINT32 sectors; // number of sectors on hard disk
87  [ 44] UINT8 md5[16]; // MD5 checksum of raw data
88  [ 60] UINT8 parentmd5[16]; // MD5 checksum of parent file
89  [ 76] UINT32 seclen; // number of bytes per sector
90  [ 80] (V2 header length)
91 
92  V3 header:
93 
94  [ 0] char tag[8]; // 'MComprHD'
95  [ 8] UINT32 length; // length of header (including tag and length fields)
96  [ 12] UINT32 version; // drive format version
97  [ 16] UINT32 flags; // flags (see below)
98  [ 20] UINT32 compression; // compression type
99  [ 24] UINT32 totalhunks; // total # of hunks represented
100  [ 28] UINT64 logicalbytes; // logical size of the data (in bytes)
101  [ 36] UINT64 metaoffset; // offset to the first blob of metadata
102  [ 44] UINT8 md5[16]; // MD5 checksum of raw data
103  [ 60] UINT8 parentmd5[16]; // MD5 checksum of parent file
104  [ 76] UINT32 hunkbytes; // number of bytes per hunk
105  [ 80] UINT8 sha1[20]; // SHA1 checksum of raw data
106  [100] UINT8 parentsha1[20];// SHA1 checksum of parent file
107  [120] (V3 header length)
108 
109  V4 header:
110 
111  [ 0] char tag[8]; // 'MComprHD'
112  [ 8] UINT32 length; // length of header (including tag and length fields)
113  [ 12] UINT32 version; // drive format version
114  [ 16] UINT32 flags; // flags (see below)
115  [ 20] UINT32 compression; // compression type
116  [ 24] UINT32 totalhunks; // total # of hunks represented
117  [ 28] UINT64 logicalbytes; // logical size of the data (in bytes)
118  [ 36] UINT64 metaoffset; // offset to the first blob of metadata
119  [ 44] UINT32 hunkbytes; // number of bytes per hunk
120  [ 48] UINT8 sha1[20]; // combined raw+meta SHA1
121  [ 68] UINT8 parentsha1[20];// combined raw+meta SHA1 of parent
122  [ 88] UINT8 rawsha1[20]; // raw data SHA1
123  [108] (V4 header length)
124 
125  Flags:
126  0x00000001 - set if this drive has a parent
127  0x00000002 - set if this drive allows writes
128 
129  =========================================================================
130 
131  V5 header:
132 
133  [ 0] char tag[8]; // 'MComprHD'
134  [ 8] uint32_t length; // length of header (including tag and length fields)
135  [ 12] uint32_t version; // drive format version
136  [ 16] uint32_t compressors[4];// which custom compressors are used?
137  [ 32] uint64_t logicalbytes; // logical size of the data (in bytes)
138  [ 40] uint64_t mapoffset; // offset to the map
139  [ 48] uint64_t metaoffset; // offset to the first blob of metadata
140  [ 56] uint32_t hunkbytes; // number of bytes per hunk (512k maximum)
141  [ 60] uint32_t unitbytes; // number of bytes per unit within each hunk
142  [ 64] uint8_t rawsha1[20]; // raw data SHA1
143  [ 84] uint8_t sha1[20]; // combined raw+meta SHA1
144  [104] uint8_t parentsha1[20];// combined raw+meta SHA1 of parent
145  [124] (V5 header length)
146 
147  If parentsha1 != 0, we have a parent (no need for flags)
148  If compressors[0] == 0, we are uncompressed (including maps)
149 
150  V5 uncompressed map format:
151 
152  [ 0] uint32_t offset; // starting offset / hunk size
153 
154  V5 compressed map format header:
155 
156  [ 0] uint32_t length; // length of compressed map
157  [ 4] UINT48 datastart; // offset of first block
158  [ 10] uint16_t crc; // crc-16 of the map
159  [ 12] uint8_t lengthbits; // bits used to encode complength
160  [ 13] uint8_t hunkbits; // bits used to encode self-refs
161  [ 14] uint8_t parentunitbits; // bits used to encode parent unit refs
162  [ 15] uint8_t reserved; // future use
163  [ 16] (compressed header length)
164 
165  Each compressed map entry, once expanded, looks like:
166 
167  [ 0] uint8_t compression; // compression type
168  [ 1] UINT24 complength; // compressed length
169  [ 4] UINT48 offset; // offset
170  [ 10] uint16_t crc; // crc-16 of the data
171 
172 ***************************************************************************/
173 
174 
175 /***************************************************************************
176  CONSTANTS
177 ***************************************************************************/
178 
179 /* header information */
180 #define CHD_HEADER_VERSION 5
181 #define CHD_V1_HEADER_SIZE 76
182 #define CHD_V2_HEADER_SIZE 80
183 #define CHD_V3_HEADER_SIZE 120
184 #define CHD_V4_HEADER_SIZE 108
185 #define CHD_V5_HEADER_SIZE 124
186 
187 #define CHD_MAX_HEADER_SIZE CHD_V5_HEADER_SIZE
188 
189 /* checksumming information */
190 #define CHD_MD5_BYTES 16
191 #define CHD_SHA1_BYTES 20
192 
193 /* CHD global flags */
194 #define CHDFLAGS_HAS_PARENT 0x00000001
195 #define CHDFLAGS_IS_WRITEABLE 0x00000002
196 #define CHDFLAGS_UNDEFINED 0xfffffffc
197 
198 /* compression types */
199 #define CHDCOMPRESSION_NONE 0
200 #define CHDCOMPRESSION_ZLIB 1
201 #define CHDCOMPRESSION_ZLIB_PLUS 2
202 #define CHDCOMPRESSION_AV 3
203 
204 /* A/V codec configuration parameters */
205 #define AV_CODEC_COMPRESS_CONFIG 1
206 #define AV_CODEC_DECOMPRESS_CONFIG 2
207 
208 /* metadata parameters */
209 #define CHDMETATAG_WILDCARD 0
210 #define CHD_METAINDEX_APPEND ((UINT32)-1)
211 
212 /* metadata flags */
213 #define CHD_MDFLAGS_CHECKSUM 0x01 /* indicates data is checksummed */
214 
215 /* standard hard disk metadata */
216 #define HARD_DISK_METADATA_TAG 0x47444444 /* 'GDDD' */
217 #define HARD_DISK_METADATA_FORMAT "CYLS:%d,HEADS:%d,SECS:%d,BPS:%d"
218 
219 /* hard disk identify information */
220 #define HARD_DISK_IDENT_METADATA_TAG 0x49444e54 /* 'IDNT' */
221 
222 /* hard disk key information */
223 #define HARD_DISK_KEY_METADATA_TAG 0x4b455920 /* 'KEY ' */
224 
225 /* pcmcia CIS information */
226 #define PCMCIA_CIS_METADATA_TAG 0x43495320 /* 'CIS ' */
227 
228 /* standard CD-ROM metadata */
229 #define CDROM_OLD_METADATA_TAG 0x43484344 /* 'CHCD' */
230 #define CDROM_TRACK_METADATA_TAG 0x43485452 /* 'CHTR' */
231 #define CDROM_TRACK_METADATA_FORMAT "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d"
232 #define CDROM_TRACK_METADATA2_TAG 0x43485432 /* 'CHT2' */
233 #define CDROM_TRACK_METADATA2_FORMAT "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d PREGAP:%d PGTYPE:%s PGSUB:%s POSTGAP:%d"
234 #define GDROM_TRACK_METADATA_TAG 0x43484744 /* 'CHTD' */
235 #define GDROM_TRACK_METADATA_FORMAT "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%s PGSUB:%s POSTGAP:%d"
236 
237 /* standard A/V metadata */
238 #define AV_METADATA_TAG 0x41564156 /* 'AVAV' */
239 #define AV_METADATA_FORMAT "FPS:%d.%06d WIDTH:%d HEIGHT:%d INTERLACED:%d CHANNELS:%d SAMPLERATE:%d"
240 
241 /* A/V laserdisc frame metadata */
242 #define AV_LD_METADATA_TAG 0x41564C44 /* 'AVLD' */
243 
244 /* CHD open values */
245 #define CHD_OPEN_READ 1
246 #define CHD_OPEN_READWRITE 2
247 
248 /* error types */
250 {
279 };
280 typedef enum _chd_error chd_error;
281 
282 
283 
284 /***************************************************************************
285  TYPE DEFINITIONS
286 ***************************************************************************/
287 
288 /* opaque types */
289 typedef struct _chd_file chd_file;
290 
291 
292 /* extract header structure (NOT the on-disk header structure) */
293 typedef struct _chd_header chd_header;
295 {
296  UINT32 length; /* length of header data */
297  UINT32 version; /* drive format version */
298  UINT32 flags; /* flags field */
299  UINT32 compression[4]; /* compression type */
300  UINT32 hunkbytes; /* number of bytes per hunk */
301  UINT32 totalhunks; /* total # of hunks represented */
302  UINT64 logicalbytes; /* logical size of the data */
303  UINT64 metaoffset; /* offset in file of first metadata */
304  UINT64 mapoffset; /* TOOD V5 */
305  UINT8 md5[CHD_MD5_BYTES]; /* overall MD5 checksum */
306  UINT8 parentmd5[CHD_MD5_BYTES]; /* overall MD5 checksum of parent */
307  UINT8 sha1[CHD_SHA1_BYTES]; /* overall SHA1 checksum */
308  UINT8 rawsha1[CHD_SHA1_BYTES]; /* SHA1 checksum of raw data */
309  UINT8 parentsha1[CHD_SHA1_BYTES]; /* overall SHA1 checksum of parent */
310  UINT32 unitbytes; /* TODO V5 */
311  UINT64 unitcount; /* TODO V5 */
312  UINT32 hunkcount; /* TODO V5 */
313 
314  /* map information */
315  UINT32 mapentrybytes; /* length of each entry in a map (V5) */
316  UINT8* rawmap; /* raw map data */
317 
318  UINT32 obsolete_cylinders; /* obsolete field -- do not use! */
319  UINT32 obsolete_sectors; /* obsolete field -- do not use! */
320  UINT32 obsolete_heads; /* obsolete field -- do not use! */
321  UINT32 obsolete_hunksize; /* obsolete field -- do not use! */
322 };
323 
324 
325 /* structure for returning information about a verification pass */
328 {
329  UINT8 md5[CHD_MD5_BYTES]; /* overall MD5 checksum */
330  UINT8 sha1[CHD_SHA1_BYTES]; /* overall SHA1 checksum */
331  UINT8 rawsha1[CHD_SHA1_BYTES]; /* SHA1 checksum of raw data */
332  UINT8 metasha1[CHD_SHA1_BYTES]; /* SHA1 checksum of metadata */
333 };
334 
335 
336 
337 /***************************************************************************
338  FUNCTION PROTOTYPES
339 ***************************************************************************/
340 
341 
342 /* ----- CHD file management ----- */
343 
344 /* create a new CHD file fitting the given description */
345 /* chd_error chd_create(const char *filename, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); */
346 
347 /* same as chd_create(), but accepts an already-opened core_file object */
348 /* chd_error chd_create_file(core_file *file, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); */
349 
350 /* open an existing CHD file */
351 chd_error chd_open_file(RFILE *file, int mode, chd_file *parent, chd_file **chd);
352 
353 chd_error chd_open(const char *filename, int mode, chd_file *parent, chd_file **chd);
354 
355 /* precache underlying file */
357 
358 /* close a CHD file */
359 void chd_close(chd_file *chd);
360 
361 /* return the associated core_file */
363 
364 /* return an error string for the given CHD error */
365 const char *chd_error_string(chd_error err);
366 
367 
368 
369 /* ----- CHD header management ----- */
370 
371 /* return a pointer to the extracted CHD header data */
372 const chd_header *chd_get_header(chd_file *chd);
373 
374 
375 
376 
377 /* ----- core data read/write ----- */
378 
379 /* read one hunk from the CHD file */
380 chd_error chd_read(chd_file *chd, UINT32 hunknum, void *buffer);
381 
382 
383 
384 /* ----- metadata management ----- */
385 
386 /* get indexed metadata of a particular sort */
387 chd_error chd_get_metadata(chd_file *chd, UINT32 searchtag, UINT32 searchindex, void *output, UINT32 outputlen, UINT32 *resultlen, UINT32 *resulttag, UINT8 *resultflags);
388 
389 
390 
391 
392 /* ----- codec interfaces ----- */
393 
394 /* set internal codec parameters */
396 
397 /* return a string description of a codec */
398 const char *chd_get_codec_name(UINT32 codec);
399 
400 extern const uint8_t s_cd_sync_header[12];
401 
402 #ifdef __cplusplus
403 }
404 #endif
405 
406 #endif /* __CHD_H__ */
Definition: chd.h:264
UINT32 obsolete_hunksize
Definition: chd.h:321
UINT8 md5[CHD_MD5_BYTES]
Definition: chd.h:305
const uint8_t s_cd_sync_header[12]
GLenum mode
Definition: glext.h:6857
Definition: chd.h:256
Definition: chd.h:278
Definition: chd.h:275
Definition: chd.h:272
UINT32 flags
Definition: chd.h:298
Definition: chd.h:255
chd_error chd_read(chd_file *chd, UINT32 hunknum, void *buffer)
Definition: libchdr_chd.c:1168
UINT32 compression[4]
Definition: chd.h:299
UINT32 unitbytes
Definition: chd.h:310
UINT8 * rawmap
Definition: chd.h:316
const char * chd_error_string(chd_error err)
Definition: libchdr_chd.c:1105
Definition: chd.h:277
Definition: chd.h:252
UINT32 obsolete_heads
Definition: chd.h:320
uint64_t UINT64
Definition: coretypes.h:8
UINT8 md5[CHD_MD5_BYTES]
Definition: chd.h:329
enum _chd_error chd_error
Definition: chd.h:280
chd_error chd_precache(chd_file *chd)
Definition: libchdr_chd.c:979
UINT8 metasha1[CHD_SHA1_BYTES]
Definition: chd.h:332
Definition: chd.h:273
UINT32 mapentrybytes
Definition: chd.h:315
Definition: chd.h:294
Definition: file_stream.c:53
chd_error chd_open_file(RFILE *file, int mode, chd_file *parent, chd_file **chd)
Definition: libchdr_chd.c:754
UINT32 obsolete_sectors
Definition: chd.h:319
Definition: chd.h:261
const char * chd_get_codec_name(UINT32 codec)
Definition: libchdr_chd.c:1256
Definition: chd.h:262
#define CHD_SHA1_BYTES
Definition: chd.h:191
Definition: chd.h:259
UINT8 sha1[CHD_SHA1_BYTES]
Definition: chd.h:307
Definition: chd.h:267
GLfloat param
Definition: glext.h:6480
UINT32 obsolete_cylinders
Definition: chd.h:318
Definition: chd.h:257
Definition: chd.h:274
UINT32 version
Definition: chd.h:297
_chd_error
Definition: chd.h:249
chd_error chd_get_metadata(chd_file *chd, UINT32 searchtag, UINT32 searchindex, void *output, UINT32 outputlen, UINT32 *resultlen, UINT32 *resulttag, UINT8 *resultflags)
Definition: libchdr_chd.c:1187
UINT64 metaoffset
Definition: chd.h:303
Definition: chd.h:251
RFILE * chd_core_file(chd_file *chd)
Definition: libchdr_chd.c:1095
Definition: chd.h:271
void chd_close(chd_file *chd)
Definition: libchdr_chd.c:1009
UINT32 totalhunks
Definition: chd.h:301
UINT32 hunkcount
Definition: chd.h:312
UINT8 sha1[CHD_SHA1_BYTES]
Definition: chd.h:330
Definition: deflate.c:120
Definition: chd.h:253
Definition: chd.h:327
Definition: libchdr_chd.c:194
Definition: chd.h:268
chd_error chd_codec_config(chd_file *chd, int param, void *config)
Definition: libchdr_chd.c:1246
uint32_t UINT32
Definition: coretypes.h:10
std::string output
Definition: Config.FromFile.cpp:44
Definition: chd.h:265
UINT8 parentmd5[CHD_MD5_BYTES]
Definition: chd.h:306
UINT64 mapoffset
Definition: chd.h:304
Definition: chd.h:263
UINT8 rawsha1[CHD_SHA1_BYTES]
Definition: chd.h:308
Definition: chd.h:266
UINT32 hunkbytes
Definition: chd.h:300
UINT8 parentsha1[CHD_SHA1_BYTES]
Definition: chd.h:309
Definition: chd.h:258
UINT32 length
Definition: chd.h:296
Definition: chd.h:270
Definition: chd.h:260
UINT64 logicalbytes
Definition: chd.h:302
Definition: civetweb.c:1024
Definition: video4linux2.c:51
Definition: chd.h:276
#define CHD_MD5_BYTES
Definition: chd.h:190
const chd_header * chd_get_header(chd_file *chd)
Definition: libchdr_chd.c:1150
unsigned char uint8_t
Definition: stdint.h:124
UINT8 rawsha1[CHD_SHA1_BYTES]
Definition: chd.h:331
chd_error chd_open(const char *filename, int mode, chd_file *parent, chd_file **chd)
Definition: libchdr_chd.c:938
Definition: chd.h:269
uint8_t UINT8
Definition: coretypes.h:12
Definition: chd.h:254
UINT64 unitcount
Definition: chd.h:311