#include <errno.h>
#include <zlib.h>
+#ifdef HAVE_CHD
+#include "chd.h"
+#endif
+
#define OFF_T_MSB ((off_t)1 << (sizeof(off_t) * 8 - 1))
unsigned int cdrIsoMultidiskCount;
unsigned int sector_in_blk;
} *compr_img;
+#ifdef HAVE_CHD
+typedef struct {
+ unsigned char (*buffer)[CD_FRAMESIZE_RAW + SUB_FRAMESIZE];
+ chd_file* chd;
+ const chd_header* header;
+ unsigned int sectors_per_hunk;
+ unsigned int current_hunk;
+ unsigned int sector_in_hunk;
+} CHD_IMG;
+
+static CHD_IMG *chd_img;
+#endif
+
int (*cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector);
char* CALLBACK CDR__getDriveLetter(void);
return -1;
}
+#ifdef HAVE_CHD
+static int handlechd(const char *isofile) {
+ int frame_offset = 0;
+ int file_offset = 0;
+
+ chd_img = (CHD_IMG *)calloc(1, sizeof(*chd_img));
+ if (chd_img == NULL)
+ goto fail_io;
+
+ if(chd_open(isofile, CHD_OPEN_READ, NULL, &chd_img->chd) != CHDERR_NONE)
+ goto fail_io;
+
+ chd_img->header = chd_get_header(chd_img->chd);
+
+ chd_img->buffer = (unsigned char (*)[CD_FRAMESIZE_RAW + SUB_FRAMESIZE])malloc(chd_img->header->hunkbytes);
+ if (chd_img->buffer == NULL)
+ goto fail_io;
+
+ chd_img->sectors_per_hunk = chd_img->header->hunkbytes / (CD_FRAMESIZE_RAW + SUB_FRAMESIZE);
+ chd_img->current_hunk = (unsigned int)-1;
+
+ cddaBigEndian = TRUE;
+
+ numtracks = 0;
+ memset(ti, 0, sizeof(ti));
+
+ while (1)
+ {
+ struct {
+ char type[64];
+ char subtype[32];
+ char pgtype[32];
+ char pgsub[32];
+ uint32_t track;
+ uint32_t frames;
+ uint32_t pregap;
+ uint32_t postgap;
+ } md = {};
+ char meta[256];
+ uint32_t meta_size = 0;
+
+ if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA2_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE)
+ sscanf(meta, CDROM_TRACK_METADATA2_FORMAT, &md.track, md.type, md.subtype, &md.frames, &md.pregap, md.pgtype, md.pgsub, &md.postgap);
+ else if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE)
+ sscanf(meta, CDROM_TRACK_METADATA_FORMAT, &md.track, md.type, md.subtype, &md.frames);
+ else
+ break;
+
+ if(md.track == 1)
+ md.pregap = 150;
+ else
+ sec2msf(msf2sec(ti[md.track-1].length) + md.pregap, ti[md.track-1].length);
+
+ ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
+
+ sec2msf(frame_offset + md.pregap, ti[md.track].start);
+ sec2msf(md.frames, ti[md.track].length);
+
+ ti[md.track].start_offset = file_offset;
+
+ frame_offset += md.pregap + md.frames + md.postgap;
+ file_offset += md.frames + md.postgap;
+ numtracks++;
+ }
+
+ if (numtracks)
+ return 0;
+
+fail_io:
+ if (chd_img != NULL) {
+ free(chd_img->buffer);
+ free(chd_img);
+ chd_img = NULL;
+ }
+ return -1;
+}
+#endif
+
// this function tries to get the .sub file of the given .img
static int opensubfile(const char *isoname) {
char subname[MAXPATHLEN];
return CD_FRAMESIZE_RAW;
}
+#ifdef HAVE_CHD
+static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
+{
+ int hunk;
+
+ if (base)
+ sector += base;
+
+ hunk = sector / chd_img->sectors_per_hunk;
+ chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk;
+
+ if (hunk != chd_img->current_hunk)
+ {
+ chd_read(chd_img->chd, hunk, chd_img->buffer);
+ chd_img->current_hunk = hunk;
+ }
+
+ if (dest != cdbuffer) // copy avoid HACK
+ memcpy(dest, chd_img->buffer[chd_img->sector_in_hunk],
+ CD_FRAMESIZE_RAW);
+ return CD_FRAMESIZE_RAW;
+}
+#endif
+
static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector)
{
int ret;
return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
}
+#ifdef HAVE_CHD
+static unsigned char *ISOgetBuffer_chd(void) {
+ return chd_img->buffer[chd_img->sector_in_hunk] + 12;
+}
+#endif
+
static unsigned char * CALLBACK ISOgetBuffer(void) {
return cdbuffer + 12;
}
CDR_getBuffer = ISOgetBuffer_compr;
cdimg_read_func = cdread_compressed;
}
+
+#ifdef HAVE_CHD
+ else if (handlechd(GetIsoFile()) == 0) {
+ printf("[chd]");
+ CDR_getBuffer = ISOgetBuffer_chd;
+ cdimg_read_func = cdread_chd;
+ }
+#endif
if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
SysPrintf("[+sub]");
free(compr_img);
compr_img = NULL;
}
+
+#ifdef HAVE_CHD
+ if (chd_img != NULL) {
+ chd_close(chd_img->chd);
+ free(chd_img->buffer);
+ free(chd_img);
+ chd_img = NULL;
+ }
+#endif
for (i = 1; i <= numtracks; i++) {
if (ti[i].handle != NULL) {