+ if (compr_img != NULL) {
+ free(compr_img);
+ compr_img = NULL;
+ }
+ return -1;
+}
+
+static int handlecbin(const char *isofile) {
+ struct
+ {
+ char magic[4];
+ unsigned int header_size;
+ unsigned long long total_bytes;
+ unsigned int block_size;
+ unsigned char ver; // 1
+ unsigned char align;
+ unsigned char rsv_06[2];
+ } ciso_hdr;
+ const char *ext = NULL;
+ unsigned int *index_table = NULL;
+ unsigned int index = 0, plain;
+ int i, ret;
+
+ if (strlen(isofile) >= 5)
+ ext = isofile + strlen(isofile) - 5;
+ if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
+ return -1;
+
+ fseek(cdHandle, 0, SEEK_SET);
+
+ ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
+ if (ret != sizeof(ciso_hdr)) {
+ SysPrintf("failed to read ciso header\n");
+ return -1;
+ }
+
+ if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
+ SysPrintf("bad ciso header\n");
+ return -1;
+ }
+ if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
+ ret = fseeko(cdHandle, ciso_hdr.header_size, SEEK_SET);
+ if (ret != 0) {
+ SysPrintf("failed to seek to %x\n", ciso_hdr.header_size);
+ return -1;
+ }
+ }
+
+ compr_img = calloc(1, sizeof(*compr_img));
+ if (compr_img == NULL)
+ goto fail_io;
+
+ compr_img->block_shift = 0;
+ compr_img->current_block = (unsigned int)-1;
+
+ compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
+ index_table = malloc((compr_img->index_len + 1) * sizeof(index_table[0]));
+ if (index_table == NULL)
+ goto fail_io;
+
+ ret = fread(index_table, sizeof(index_table[0]), compr_img->index_len, cdHandle);
+ if (ret != compr_img->index_len) {
+ SysPrintf("failed to read index table\n");
+ goto fail_index;
+ }
+
+ compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
+ if (compr_img->index_table == NULL)
+ goto fail_index;
+
+ for (i = 0; i < compr_img->index_len + 1; i++) {
+ index = index_table[i];
+ plain = index & 0x80000000;
+ index &= 0x7fffffff;
+ compr_img->index_table[i] = (off_t)index << ciso_hdr.align;
+ if (plain)
+ compr_img->index_table[i] |= OFF_T_MSB;
+ }
+
+ return 0;
+
+fail_index:
+ free(index_table);
+fail_io:
+ if (compr_img != NULL) {
+ free(compr_img);
+ compr_img = NULL;
+ }
+ 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;
+ }