1 /***************************************************************************
2 * Copyright (C) 2007 PCSX-df Team *
3 * Copyright (C) 2009 Wei Mingzhi *
4 * Copyright (C) 2012 notaz *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
20 ***************************************************************************/
22 #include "psxcommon.h"
35 #define strcasecmp _stricmp
37 #include <sys/types.h>
46 // to enable the USE_READ_THREAD code, fix:
47 // - https://github.com/notaz/pcsx_rearmed/issues/257
48 // - ISOgetBufferSub to not race with async code
49 #define USE_READ_THREAD 0 //P_HAVE_PTHREAD
51 #ifdef USE_LIBRETRO_VFS
52 #include <streams/file_stream_transforms.h>
58 #define rewind(f_) rfseek(f_, 0, SEEK_SET)
61 #define OFF_T_MSB ((off_t)1 << (sizeof(off_t) * 8 - 1))
63 unsigned int cdrIsoMultidiskCount;
64 unsigned int cdrIsoMultidiskSelect;
66 static FILE *cdHandle = NULL;
67 static FILE *cddaHandle = NULL;
68 static FILE *subHandle = NULL;
70 static boolean subChanMixed = FALSE;
71 static boolean subChanRaw = FALSE;
73 static boolean multifile = FALSE;
75 static unsigned char cdbuffer[CD_FRAMESIZE_RAW];
76 static unsigned char subbuffer[SUB_FRAMESIZE];
78 static boolean cddaBigEndian = FALSE;
79 /* Frame offset into CD image where pregap data would be found if it was there.
80 * If a game seeks there we must *not* return subchannel data since it's
81 * not in the CD image, so that cdrom code can fake subchannel data instead.
82 * XXX: there could be multiple pregaps but PSX dumps only have one? */
83 static unsigned int pregapOffset;
85 // compressed image stuff
87 unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
88 unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100];
90 unsigned int index_len;
91 unsigned int block_shift;
92 unsigned int current_block;
93 unsigned int sector_in_blk;
98 unsigned char *buffer;
100 const chd_header* header;
101 unsigned int sectors_per_hunk;
102 unsigned int current_hunk[2];
103 unsigned int current_buffer;
104 unsigned int sector_in_hunk;
110 static int (*cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector);
111 static int (*cdimg_read_sub_func)(FILE *f, int sector);
113 char* CALLBACK CDR__getDriveLetter(void);
114 long CALLBACK CDR__configure(void);
115 long CALLBACK CDR__test(void);
116 void CALLBACK CDR__about(void);
117 long CALLBACK CDR__setfilename(char *filename);
119 static void DecodeRawSubData(void);
122 enum {DATA=1, CDDA} type;
123 char start[3]; // MSF-format
124 char length[3]; // MSF-format
125 FILE *handle; // for multi-track images CDDA
126 unsigned int start_offset; // byte offset from start of above file (chd: sector offset)
129 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
131 static int numtracks = 0;
132 static struct trackinfo ti[MAXTRACKS];
134 // get a sector from a msf-array
135 static unsigned int msf2sec(char *msf) {
136 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
139 static void sec2msf(unsigned int s, char *msf) {
140 msf[0] = s / 75 / 60;
141 s = s - msf[0] * 75 * 60;
147 // divide a string of xx:yy:zz into m, s, f
148 static void tok2msf(char *time, char *msf) {
151 token = strtok(time, ":");
153 msf[0] = atoi(token);
159 token = strtok(NULL, ":");
161 msf[1] = atoi(token);
167 token = strtok(NULL, ":");
169 msf[2] = atoi(token);
176 static off_t get_size(FILE *f)
179 #if !defined(USE_LIBRETRO_VFS)
181 if (fstat(fileno(f), &st) == 0)
185 fseeko(f, 0, SEEK_END);
187 fseeko(f, old, SEEK_SET);
191 // this function tries to get the .toc file of the given .bin
192 // the necessary data is put into the ti (trackinformation)-array
193 static int parsetoc(const char *isofile) {
194 char tocname[MAXPATHLEN];
196 char linebuf[256], tmp[256], name[256];
198 char time[20], time2[20];
199 unsigned int t, sector_offs, sector_size;
200 unsigned int current_zero_gap = 0;
204 // copy name of the iso and change extension from .bin to .toc
205 strncpy(tocname, isofile, sizeof(tocname));
206 tocname[MAXPATHLEN - 1] = '\0';
207 if (strlen(tocname) >= 4) {
208 strcpy(tocname + strlen(tocname) - 4, ".toc");
214 if ((fi = fopen(tocname, "r")) == NULL) {
215 // try changing extension to .cue (to satisfy some stupid tutorials)
216 strcpy(tocname + strlen(tocname) - 4, ".cue");
217 if ((fi = fopen(tocname, "r")) == NULL) {
218 // if filename is image.toc.bin, try removing .bin (for Brasero)
219 strcpy(tocname, isofile);
221 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
222 tocname[t - 4] = '\0';
223 if ((fi = fopen(tocname, "r")) == NULL) {
231 // check if it's really a TOC named as a .cue
232 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
233 token = strtok(linebuf, " ");
234 if (token && strncmp(token, "CD", 2) != 0) {
235 // && strcmp(token, "CATALOG") != 0) - valid for a real .cue
240 fseek(fi, 0, SEEK_SET);
243 memset(&ti, 0, sizeof(ti));
244 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
246 sector_size = CD_FRAMESIZE_RAW;
247 sector_offs = 2 * 75;
249 // parse the .toc file
250 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
252 strncpy(tmp, linebuf, sizeof(linebuf));
253 token = strtok(tmp, " ");
255 if (token == NULL) continue;
257 if (!strcmp(token, "TRACK")) {
258 sector_offs += current_zero_gap;
259 current_zero_gap = 0;
262 token = strtok(NULL, " ");
265 if (!strncmp(token, "MODE2_RAW", 9)) {
266 ti[numtracks].type = DATA;
267 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
269 // check if this image contains mixed subchannel data
270 token = strtok(NULL, " ");
271 if (token != NULL && !strncmp(token, "RW", 2)) {
272 sector_size = CD_FRAMESIZE_RAW + SUB_FRAMESIZE;
274 if (!strncmp(token, "RW_RAW", 6))
278 else if (!strncmp(token, "AUDIO", 5)) {
279 ti[numtracks].type = CDDA;
282 else if (!strcmp(token, "DATAFILE")) {
283 if (ti[numtracks].type == CDDA) {
284 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
285 ti[numtracks].start_offset = t;
286 t = t / sector_size + sector_offs;
287 sec2msf(t, (char *)&ti[numtracks].start);
288 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
291 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
292 tok2msf((char *)&time, (char *)&ti[numtracks].length);
295 else if (!strcmp(token, "FILE")) {
296 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
297 tok2msf((char *)&time, (char *)&ti[numtracks].start);
298 t += msf2sec(ti[numtracks].start) * sector_size;
299 ti[numtracks].start_offset = t;
300 t = t / sector_size + sector_offs;
301 sec2msf(t, (char *)&ti[numtracks].start);
302 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
304 else if (!strcmp(token, "ZERO") || !strcmp(token, "SILENCE")) {
305 // skip unneeded optional fields
306 while (token != NULL) {
307 token = strtok(NULL, " ");
308 if (strchr(token, ':') != NULL)
313 current_zero_gap = msf2sec(tmp);
316 t = ti[numtracks - 1].start_offset;
318 pregapOffset = t + msf2sec(ti[numtracks - 1].length);
321 else if (!strcmp(token, "START")) {
322 token = strtok(NULL, " ");
323 if (token != NULL && strchr(token, ':')) {
326 ti[numtracks].start_offset += (t - current_zero_gap) * sector_size;
327 t = msf2sec(ti[numtracks].start) + t;
328 sec2msf(t, (char *)&ti[numtracks].start);
338 // this function tries to get the .cue file of the given .bin
339 // the necessary data is put into the ti (trackinformation)-array
340 static int parsecue(const char *isofile) {
341 char cuename[MAXPATHLEN];
342 char filepath[MAXPATHLEN];
349 char linebuf[256], tmpb[256], dummy[256];
350 unsigned int incue_max_len;
351 unsigned int t, file_len, mode, sector_offs;
352 unsigned int sector_size = 2352;
356 // copy name of the iso and change extension from .bin to .cue
357 strncpy(cuename, isofile, sizeof(cuename));
358 cuename[MAXPATHLEN - 1] = '\0';
359 if (strlen(cuename) < 4)
361 if (strcasecmp(cuename + strlen(cuename) - 4, ".cue") == 0) {
362 // it's already open as cdHandle
366 // If 'isofile' is a '.cd<X>' file, use it as a .cue file
367 // and don't try to search the additional .cue file
368 if (strncasecmp(cuename + strlen(cuename) - 4, ".cd", 3) != 0 )
369 strcpy(cuename + strlen(cuename) - 4, ".cue");
371 if ((ftmp = fopen(cuename, "r")) == NULL)
376 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
377 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
379 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
380 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
381 // Don't proceed further, as this is actually a .toc file rather
385 return parsetoc(isofile);
390 // build a path for files referenced in .cue
391 strncpy(filepath, cuename, sizeof(filepath));
392 tmp = strrchr(filepath, '/');
394 tmp = strrchr(filepath, '\\');
400 filepath[sizeof(filepath) - 1] = 0;
402 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
404 memset(&ti, 0, sizeof(ti));
407 sector_offs = 2 * 75;
409 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
410 strncpy(dummy, linebuf, sizeof(linebuf));
411 token = strtok(dummy, " ");
417 if (!strcmp(token, "TRACK")) {
421 if (strstr(linebuf, "AUDIO") != NULL) {
422 ti[numtracks].type = CDDA;
425 else if (sscanf(linebuf, " TRACK %u MODE%u/%u", &t, &mode, §or_size) == 3)
426 ti[numtracks].type = DATA;
428 SysPrintf(".cue: failed to parse TRACK\n");
429 ti[numtracks].type = numtracks == 1 ? DATA : CDDA;
431 if (sector_size == 0)
434 else if (!strcmp(token, "INDEX")) {
435 if (sscanf(linebuf, " INDEX %02d %8s", &t, time) != 2)
436 SysPrintf(".cue: failed to parse INDEX\n");
437 tok2msf(time, (char *)&ti[numtracks].start);
439 t = msf2sec(ti[numtracks].start);
440 ti[numtracks].start_offset = t * sector_size;
442 sec2msf(t, ti[numtracks].start);
444 // default track length to file length
445 t = file_len - ti[numtracks].start_offset / sector_size;
446 sec2msf(t, ti[numtracks].length);
448 if (numtracks > 1 && ti[numtracks].handle == NULL) {
449 // this track uses the same file as the last,
450 // start of this track is last track's end
451 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
452 sec2msf(t, ti[numtracks - 1].length);
454 if (numtracks > 1 && pregapOffset == -1)
455 pregapOffset = ti[numtracks].start_offset / sector_size;
457 else if (!strcmp(token, "PREGAP")) {
458 if (sscanf(linebuf, " PREGAP %8s", time) == 1) {
459 tok2msf(time, dummy);
460 sector_offs += msf2sec(dummy);
462 pregapOffset = -1; // mark to fill track start_offset
464 else if (!strcmp(token, "FILE")) {
465 t = sscanf(linebuf, " FILE \"%255[^\"]\"", tmpb);
467 sscanf(linebuf, " FILE %255s", tmpb);
469 tmp = strrchr(tmpb, '\\');
471 tmp = strrchr(tmpb, '/');
476 strncpy(incue_fname, tmp, incue_max_len);
477 ti[numtracks + 1].handle = fopen(filepath, "rb");
479 // update global offset if this is not first file in this .cue
480 if (numtracks + 1 > 1) {
482 sector_offs += file_len;
486 if (ti[numtracks + 1].handle == NULL) {
487 SysPrintf(_("\ncould not open: %s\n"), filepath);
490 file_len = get_size(ti[numtracks + 1].handle) / 2352;
497 // if there are no tracks detected, then it's not a cue file
501 // the data track handle is always in cdHandle
504 cdHandle = ti[1].handle;
510 // this function tries to get the .ccd file of the given .img
511 // the necessary data is put into the ti (trackinformation)-array
512 static int parseccd(const char *isofile) {
513 char ccdname[MAXPATHLEN];
520 // copy name of the iso and change extension from .img to .ccd
521 strncpy(ccdname, isofile, sizeof(ccdname));
522 ccdname[MAXPATHLEN - 1] = '\0';
523 if (strlen(ccdname) >= 4) {
524 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
530 if ((fi = fopen(ccdname, "r")) == NULL) {
534 memset(&ti, 0, sizeof(ti));
536 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
537 if (!strncmp(linebuf, "[TRACK", 6)){
540 else if (!strncmp(linebuf, "MODE=", 5)) {
541 sscanf(linebuf, "MODE=%d", &t);
542 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
544 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
545 sscanf(linebuf, "INDEX 1=%d", &t);
546 sec2msf(t + 2 * 75, ti[numtracks].start);
547 ti[numtracks].start_offset = t * 2352;
549 // If we've already seen another track, this is its end
551 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
552 sec2msf(t, ti[numtracks - 1].length);
559 // Fill out the last track's end based on size
560 if (numtracks >= 1) {
561 t = get_size(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
562 sec2msf(t, ti[numtracks].length);
568 // this function tries to get the .mds file of the given .mdf
569 // the necessary data is put into the ti (trackinformation)-array
570 static int parsemds(const char *isofile) {
571 char mdsname[MAXPATHLEN];
573 unsigned int offset, extra_offset, l, i;
578 // copy name of the iso and change extension from .mdf to .mds
579 strncpy(mdsname, isofile, sizeof(mdsname));
580 mdsname[MAXPATHLEN - 1] = '\0';
581 if (strlen(mdsname) >= 4) {
582 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
588 if ((fi = fopen(mdsname, "rb")) == NULL) {
592 memset(&ti, 0, sizeof(ti));
594 // check if it's a valid mds file
595 if (fread(&i, 1, sizeof(i), fi) != sizeof(i))
598 if (i != 0x4944454D) {
599 // not an valid mds file
604 // get offset to session block
605 fseek(fi, 0x50, SEEK_SET);
606 if (fread(&offset, 1, sizeof(offset), fi) != sizeof(offset))
608 offset = SWAP32(offset);
610 // get total number of tracks
612 fseek(fi, offset, SEEK_SET);
613 if (fread(&s, 1, sizeof(s), fi) != sizeof(s))
618 // get offset to track blocks
619 fseek(fi, 4, SEEK_CUR);
620 if (fread(&offset, 1, sizeof(offset), fi) != sizeof(offset))
622 offset = SWAP32(offset);
626 fseek(fi, offset + 4, SEEK_SET);
627 if (fgetc(fi) < 0xA0) {
633 // check if the image contains mixed subchannel data
634 fseek(fi, offset + 1, SEEK_SET);
635 subChanMixed = subChanRaw = (fgetc(fi) ? TRUE : FALSE);
638 for (i = 1; i <= numtracks; i++) {
639 fseek(fi, offset, SEEK_SET);
641 // get the track type
642 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
643 fseek(fi, 8, SEEK_CUR);
645 // get the track starting point
646 ti[i].start[0] = fgetc(fi);
647 ti[i].start[1] = fgetc(fi);
648 ti[i].start[2] = fgetc(fi);
650 if (fread(&extra_offset, 1, sizeof(extra_offset), fi) != sizeof(extra_offset))
652 extra_offset = SWAP32(extra_offset);
654 // get track start offset (in .mdf)
655 fseek(fi, offset + 0x28, SEEK_SET);
656 if (fread(&l, 1, sizeof(l), fi) != sizeof(l))
659 ti[i].start_offset = l;
662 fseek(fi, extra_offset, SEEK_SET);
663 if (fread(&l, 1, sizeof(l), fi) != sizeof(l))
667 pregapOffset = msf2sec(ti[i].start);
669 // get the track length
670 if (fread(&l, 1, sizeof(l), fi) != sizeof(l))
673 sec2msf(l, ti[i].length);
681 SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
687 static int handlepbp(const char *isofile) {
690 unsigned int dontcare[8];
691 unsigned int psar_offs;
704 unsigned int dontcare[6];
707 off_t psisoimg_offs, cdimg_base;
708 unsigned int t, cd_length;
709 unsigned int offsettab[8];
710 unsigned int psar_offs, index_entry_size, index_entry_offset;
711 const char *ext = NULL;
714 if (strlen(isofile) >= 4)
715 ext = isofile + strlen(isofile) - 4;
716 if (ext == NULL || strcasecmp(ext, ".pbp") != 0)
721 ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
722 if (ret != sizeof(pbp_hdr)) {
723 SysPrintf("failed to read pbp\n");
727 psar_offs = SWAP32(pbp_hdr.psar_offs);
729 ret = fseeko(cdHandle, psar_offs, SEEK_SET);
731 SysPrintf("failed to seek to %x\n", psar_offs);
735 psisoimg_offs = psar_offs;
736 if (fread(psar_sig, 1, sizeof(psar_sig), cdHandle) != sizeof(psar_sig))
739 if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
741 ret = fseeko(cdHandle, psar_offs + 0x200, SEEK_SET);
743 SysPrintf("failed to seek to %x\n", psar_offs + 0x200);
747 if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) {
748 SysPrintf("failed to read offsettab\n");
752 for (i = 0; i < sizeof(offsettab) / sizeof(offsettab[0]); i++) {
753 if (offsettab[i] == 0)
756 cdrIsoMultidiskCount = i;
757 if (cdrIsoMultidiskCount == 0) {
758 SysPrintf("multidisk eboot has 0 images?\n");
762 if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount)
763 cdrIsoMultidiskSelect = 0;
765 psisoimg_offs += SWAP32(offsettab[cdrIsoMultidiskSelect]);
767 ret = fseeko(cdHandle, psisoimg_offs, SEEK_SET);
769 SysPrintf("failed to seek to %llx\n", (long long)psisoimg_offs);
773 if (fread(psar_sig, 1, sizeof(psar_sig), cdHandle) != sizeof(psar_sig))
778 if (strcmp(psar_sig, "PSISOIMG00") != 0) {
779 SysPrintf("bad psar_sig: %s\n", psar_sig);
784 ret = fseeko(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
786 SysPrintf("failed to seek to %llx\n", (long long)psisoimg_offs + 0x800);
790 // first 3 entries are special
791 fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
792 if (fread(&toc_entry, 1, sizeof(toc_entry), cdHandle) != sizeof(toc_entry))
794 numtracks = btoi(toc_entry.index1[0]);
796 if (fread(&toc_entry, 1, sizeof(toc_entry), cdHandle) != sizeof(toc_entry))
798 cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
799 btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
801 for (i = 1; i <= numtracks; i++) {
802 if (fread(&toc_entry, 1, sizeof(toc_entry), cdHandle) != sizeof(toc_entry))
805 ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
807 ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
808 btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
809 ti[i].start_offset *= 2352;
810 ti[i].start[0] = btoi(toc_entry.index1[0]);
811 ti[i].start[1] = btoi(toc_entry.index1[1]);
812 ti[i].start[2] = btoi(toc_entry.index1[2]);
815 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
816 sec2msf(t, ti[i - 1].length);
819 t = cd_length - ti[numtracks].start_offset / 2352;
820 sec2msf(t, ti[numtracks].length);
823 ret = fseeko(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
825 SysPrintf("failed to seek to ISO index\n");
829 compr_img = calloc(1, sizeof(*compr_img));
830 if (compr_img == NULL)
833 compr_img->block_shift = 4;
834 compr_img->current_block = (unsigned int)-1;
836 compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
837 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
838 if (compr_img->index_table == NULL)
841 cdimg_base = psisoimg_offs + 0x100000;
842 for (i = 0; i < compr_img->index_len; i++) {
843 ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
844 if (ret != sizeof(index_entry)) {
845 SysPrintf("failed to read index_entry #%d\n", i);
849 index_entry_size = SWAP32(index_entry.size);
850 index_entry_offset = SWAP32(index_entry.offset);
852 if (index_entry_size == 0)
855 compr_img->index_table[i] = cdimg_base + index_entry_offset;
857 compr_img->index_table[i] = cdimg_base + index_entry_offset + index_entry_size;
862 free(compr_img->index_table);
863 compr_img->index_table = NULL;
867 SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
871 if (compr_img != NULL) {
878 static int handlecbin(const char *isofile) {
882 unsigned int header_size;
883 unsigned long long total_bytes;
884 unsigned int block_size;
885 unsigned char ver; // 1
887 unsigned char rsv_06[2];
889 const char *ext = NULL;
890 unsigned int *index_table = NULL;
891 unsigned int index = 0, plain;
894 if (strlen(isofile) >= 5)
895 ext = isofile + strlen(isofile) - 5;
896 if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
899 ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
900 if (ret != sizeof(ciso_hdr)) {
901 SysPrintf("failed to read ciso header\n");
905 if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
906 SysPrintf("bad ciso header\n");
909 if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
910 ret = fseeko(cdHandle, ciso_hdr.header_size, SEEK_SET);
912 SysPrintf("failed to seek to %x\n", ciso_hdr.header_size);
917 compr_img = calloc(1, sizeof(*compr_img));
918 if (compr_img == NULL)
921 compr_img->block_shift = 0;
922 compr_img->current_block = (unsigned int)-1;
924 compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
925 index_table = malloc((compr_img->index_len + 1) * sizeof(index_table[0]));
926 if (index_table == NULL)
929 ret = fread(index_table, sizeof(index_table[0]), compr_img->index_len, cdHandle);
930 if (ret != compr_img->index_len) {
931 SysPrintf("failed to read index table\n");
935 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
936 if (compr_img->index_table == NULL)
939 for (i = 0; i < compr_img->index_len + 1; i++) {
940 index = index_table[i];
941 plain = index & 0x80000000;
943 compr_img->index_table[i] = (off_t)index << ciso_hdr.align;
945 compr_img->index_table[i] |= OFF_T_MSB;
953 if (compr_img != NULL) {
962 static int handlechd(const char *isofile) {
963 int frame_offset = 150;
966 chd_img = calloc(1, sizeof(*chd_img));
970 if(chd_open(isofile, CHD_OPEN_READ, NULL, &chd_img->chd) != CHDERR_NONE)
973 if (Config.CHD_Precache && (chd_precache(chd_img->chd) != CHDERR_NONE))
976 chd_img->header = chd_get_header(chd_img->chd);
978 chd_img->buffer = malloc(chd_img->header->hunkbytes * 2);
979 if (chd_img->buffer == NULL)
982 chd_img->sectors_per_hunk = chd_img->header->hunkbytes / (CD_FRAMESIZE_RAW + SUB_FRAMESIZE);
983 chd_img->current_hunk[0] = (unsigned int)-1;
984 chd_img->current_hunk[1] = (unsigned int)-1;
986 cddaBigEndian = TRUE;
989 memset(ti, 0, sizeof(ti));
1004 uint32_t meta_size = 0;
1006 if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA2_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE)
1007 sscanf(meta, CDROM_TRACK_METADATA2_FORMAT, &md.track, md.type, md.subtype, &md.frames, &md.pregap, md.pgtype, md.pgsub, &md.postgap);
1008 else if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE)
1009 sscanf(meta, CDROM_TRACK_METADATA_FORMAT, &md.track, md.type, md.subtype, &md.frames);
1013 SysPrintf("chd: %s\n", meta);
1015 if (md.track == 1) {
1016 if (!strncmp(md.subtype, "RW", 2)) {
1017 subChanMixed = TRUE;
1018 if (!strcmp(md.subtype, "RW_RAW"))
1023 ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
1025 sec2msf(frame_offset + md.pregap, ti[md.track].start);
1026 sec2msf(md.frames, ti[md.track].length);
1028 ti[md.track].start_offset = file_offset + md.pregap;
1030 // XXX: what about postgap?
1031 frame_offset += md.frames;
1032 file_offset += md.frames;
1040 if (chd_img != NULL) {
1041 free(chd_img->buffer);
1049 // this function tries to get the .sub file of the given .img
1050 static int opensubfile(const char *isoname) {
1051 char subname[MAXPATHLEN];
1053 // copy name of the iso and change extension from .img to .sub
1054 strncpy(subname, isoname, sizeof(subname));
1055 subname[MAXPATHLEN - 1] = '\0';
1056 if (strlen(subname) >= 4) {
1057 strcpy(subname + strlen(subname) - 4, ".sub");
1063 subHandle = fopen(subname, "rb");
1064 if (subHandle == NULL) {
1071 static int opensbifile(const char *isoname) {
1072 char sbiname[MAXPATHLEN], disknum[MAXPATHLEN] = "0";
1075 strncpy(sbiname, isoname, sizeof(sbiname));
1076 sbiname[MAXPATHLEN - 1] = '\0';
1077 if (strlen(sbiname) >= 4) {
1078 if (cdrIsoMultidiskCount > 1) {
1079 sprintf(disknum, "_%i.sbi", cdrIsoMultidiskSelect + 1);
1080 strcpy(sbiname + strlen(sbiname) - 4, disknum);
1083 strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
1089 s = msf2sec(ti[1].length);
1091 return LoadSBI(sbiname, s);
1094 #if !USE_READ_THREAD
1095 static void readThreadStop() {}
1096 static void readThreadStart() {}
1098 static pthread_t read_thread_id;
1100 static pthread_cond_t read_thread_msg_avail;
1101 static pthread_cond_t read_thread_msg_done;
1102 static pthread_mutex_t read_thread_msg_lock;
1104 static pthread_cond_t sectorbuffer_cond;
1105 static pthread_mutex_t sectorbuffer_lock;
1107 static boolean read_thread_running = FALSE;
1108 static int read_thread_sector_start = -1;
1109 static int read_thread_sector_end = -1;
1114 unsigned char data[CD_FRAMESIZE_RAW];
1115 } SectorBufferEntry;
1117 #define SECTOR_BUFFER_SIZE 4096
1119 static SectorBufferEntry *sectorbuffer;
1120 static size_t sectorbuffer_index;
1122 int (*sync_cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector);
1123 unsigned char *(*sync_CDR_getBuffer)(void);
1125 static unsigned char * CALLBACK ISOgetBuffer_async(void);
1126 static int cdread_async(FILE *f, unsigned int base, void *dest, int sector);
1128 static void *readThreadMain(void *param) {
1129 int max_sector = -1;
1130 int requested_sector_start = -1;
1131 int requested_sector_end = -1;
1132 int last_read_sector = -1;
1138 int speedmult_ra = 4;
1141 int how_far_ahead = 0;
1143 unsigned char tmpdata[CD_FRAMESIZE_RAW];
1146 max_sector = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
1149 pthread_mutex_lock(&read_thread_msg_lock);
1151 // If we don't have readahead and we don't have a sector request, wait for one.
1152 // If we still have readahead to go, don't block, just keep going.
1153 // And if we ever have a sector request pending, acknowledge and reset it.
1156 if (read_thread_sector_start == -1 && read_thread_running) {
1157 pthread_cond_wait(&read_thread_msg_avail, &read_thread_msg_lock);
1161 if (read_thread_sector_start != -1) {
1162 requested_sector_start = read_thread_sector_start;
1163 requested_sector_end = read_thread_sector_end;
1164 read_thread_sector_start = -1;
1165 read_thread_sector_end = -1;
1166 pthread_cond_signal(&read_thread_msg_done);
1169 pthread_mutex_unlock(&read_thread_msg_lock);
1171 if (!read_thread_running)
1174 // Readahead code, based on the implementation in mednafen psx's cdromif.cpp
1175 if (requested_sector_start != -1) {
1176 if (last_read_sector != -1 && last_read_sector == (requested_sector_start - 1)) {
1177 how_far_ahead = ra_sector - requested_sector_end;
1179 if(how_far_ahead <= max_ra)
1180 ra_count = (max_ra - how_far_ahead + 1 ? max_ra - how_far_ahead + 1 : speedmult_ra);
1183 } else if (requested_sector_end != last_read_sector) {
1184 ra_sector = requested_sector_end;
1185 ra_count = initial_ra;
1188 last_read_sector = requested_sector_end;
1191 index = ra_sector % SECTOR_BUFFER_SIZE;
1193 // check for end of CD
1194 if (ra_count && ra_sector >= max_sector) {
1196 pthread_mutex_lock(§orbuffer_lock);
1197 sectorbuffer[index].ret = -1;
1198 sectorbuffer[index].sector = ra_sector;
1199 pthread_cond_signal(§orbuffer_cond);
1200 pthread_mutex_unlock(§orbuffer_lock);
1204 pthread_mutex_lock(§orbuffer_lock);
1205 if (sectorbuffer[index].sector != ra_sector) {
1206 pthread_mutex_unlock(§orbuffer_lock);
1208 ret = sync_cdimg_read_func(cdHandle, 0, tmpdata, ra_sector);
1210 pthread_mutex_lock(§orbuffer_lock);
1211 sectorbuffer[index].ret = ret;
1212 sectorbuffer[index].sector = ra_sector;
1213 memcpy(sectorbuffer[index].data, tmpdata, CD_FRAMESIZE_RAW);
1215 pthread_cond_signal(§orbuffer_cond);
1216 pthread_mutex_unlock(§orbuffer_lock);
1226 static void readThreadStop() {
1227 if (read_thread_running == TRUE) {
1228 read_thread_running = FALSE;
1229 pthread_cond_signal(&read_thread_msg_avail);
1230 pthread_join(read_thread_id, NULL);
1233 pthread_cond_destroy(&read_thread_msg_done);
1234 pthread_cond_destroy(&read_thread_msg_avail);
1235 pthread_mutex_destroy(&read_thread_msg_lock);
1237 pthread_cond_destroy(§orbuffer_cond);
1238 pthread_mutex_destroy(§orbuffer_lock);
1240 CDR_getBuffer = sync_CDR_getBuffer;
1241 cdimg_read_func = sync_cdimg_read_func;
1244 sectorbuffer = NULL;
1247 static void readThreadStart() {
1248 SysPrintf("Starting async CD thread\n");
1250 if (read_thread_running == TRUE)
1253 read_thread_running = TRUE;
1254 read_thread_sector_start = -1;
1255 read_thread_sector_end = -1;
1256 sectorbuffer_index = 0;
1258 sectorbuffer = calloc(SECTOR_BUFFER_SIZE, sizeof(SectorBufferEntry));
1262 sectorbuffer[0].sector = -1; // Otherwise we might think we've already fetched sector 0!
1264 sync_CDR_getBuffer = CDR_getBuffer;
1265 CDR_getBuffer = ISOgetBuffer_async;
1266 sync_cdimg_read_func = cdimg_read_func;
1267 cdimg_read_func = cdread_async;
1269 if (pthread_cond_init(&read_thread_msg_avail, NULL) ||
1270 pthread_cond_init(&read_thread_msg_done, NULL) ||
1271 pthread_mutex_init(&read_thread_msg_lock, NULL) ||
1272 pthread_cond_init(§orbuffer_cond, NULL) ||
1273 pthread_mutex_init(§orbuffer_lock, NULL) ||
1274 pthread_create(&read_thread_id, NULL, readThreadMain, NULL))
1280 SysPrintf("Error starting async CD thread\n");
1281 SysPrintf("Falling back to sync\n");
1287 static int cdread_normal(FILE *f, unsigned int base, void *dest, int sector)
1292 if (fseeko(f, base + sector * CD_FRAMESIZE_RAW, SEEK_SET))
1294 ret = fread(dest, 1, CD_FRAMESIZE_RAW, f);
1300 // often happens in cdda gaps of a split cue/bin, so not logged
1301 //SysPrintf("File IO error %d, base %u, sector %u\n", errno, base, sector);
1305 static int cdread_sub_mixed(FILE *f, unsigned int base, void *dest, int sector)
1311 if (fseeko(f, base + sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE), SEEK_SET))
1313 ret = fread(dest, 1, CD_FRAMESIZE_RAW, f);
1319 //SysPrintf("File IO error %d, base %u, sector %u\n", errno, base, sector);
1323 static int cdread_sub_sub_mixed(FILE *f, int sector)
1327 if (fseeko(f, sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + CD_FRAMESIZE_RAW, SEEK_SET))
1329 if (fread(subbuffer, 1, SUB_FRAMESIZE, f) != SUB_FRAMESIZE)
1332 return SUB_FRAMESIZE;
1335 SysPrintf("subchannel: file IO error %d, sector %u\n", errno, sector);
1339 static int uncompress2_pcsx(void *out, unsigned long *out_size, void *in, unsigned long in_size)
1344 if (z.zalloc == NULL) {
1345 // XXX: one-time leak here..
1351 ret = inflateInit2(&z, -15);
1354 ret = inflateReset(&z);
1359 z.avail_in = in_size;
1361 z.avail_out = *out_size;
1363 ret = inflate(&z, Z_NO_FLUSH);
1366 *out_size -= z.avail_out;
1367 return ret == 1 ? 0 : ret;
1370 static int cdread_compressed(FILE *f, unsigned int base, void *dest, int sector)
1372 unsigned long cdbuffer_size, cdbuffer_size_expect;
1381 sector += base / 2352;
1383 block = sector >> compr_img->block_shift;
1384 compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1);
1386 if (block == compr_img->current_block) {
1387 //printf("hit sect %d\n", sector);
1391 if (sector >= compr_img->index_len * 16) {
1392 SysPrintf("sector %d is past img end\n", sector);
1396 start_byte = compr_img->index_table[block] & ~OFF_T_MSB;
1397 if (fseeko(cdHandle, start_byte, SEEK_SET) != 0) {
1398 SysPrintf("seek error for block %d at %llx: ",
1399 block, (long long)start_byte);
1404 is_compressed = !(compr_img->index_table[block] & OFF_T_MSB);
1405 size = (compr_img->index_table[block + 1] & ~OFF_T_MSB) - start_byte;
1406 if (size > sizeof(compr_img->buff_compressed)) {
1407 SysPrintf("block %d is too large: %u\n", block, size);
1411 if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0],
1412 1, size, cdHandle) != size) {
1413 SysPrintf("read error for block %d at %x: ", block, start_byte);
1418 if (is_compressed) {
1419 cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift;
1420 cdbuffer_size = cdbuffer_size_expect;
1421 ret = uncompress2_pcsx(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
1423 SysPrintf("uncompress failed with %d for block %d, sector %d\n",
1424 ret, block, sector);
1427 if (cdbuffer_size != cdbuffer_size_expect)
1428 SysPrintf("cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
1429 cdbuffer_size_expect, sector);
1433 compr_img->current_block = block;
1436 if (dest != cdbuffer) // copy avoid HACK
1437 memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk],
1439 return CD_FRAMESIZE_RAW;
1443 static unsigned char *chd_get_sector(unsigned int current_buffer, unsigned int sector_in_hunk)
1445 return chd_img->buffer
1446 + current_buffer * chd_img->header->hunkbytes
1447 + sector_in_hunk * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE);
1450 static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
1456 hunk = sector / chd_img->sectors_per_hunk;
1457 chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk;
1459 if (hunk == chd_img->current_hunk[0])
1460 chd_img->current_buffer = 0;
1461 else if (hunk == chd_img->current_hunk[1])
1462 chd_img->current_buffer = 1;
1465 chd_read(chd_img->chd, hunk, chd_img->buffer +
1466 chd_img->current_buffer * chd_img->header->hunkbytes);
1467 chd_img->current_hunk[chd_img->current_buffer] = hunk;
1470 if (dest != cdbuffer) // copy avoid HACK
1471 memcpy(dest, chd_get_sector(chd_img->current_buffer, chd_img->sector_in_hunk),
1473 return CD_FRAMESIZE_RAW;
1476 static int cdread_sub_chd(FILE *f, int sector)
1478 unsigned int sector_in_hunk;
1479 unsigned int buffer;
1485 hunk = sector / chd_img->sectors_per_hunk;
1486 sector_in_hunk = sector % chd_img->sectors_per_hunk;
1488 if (hunk == chd_img->current_hunk[0])
1490 else if (hunk == chd_img->current_hunk[1])
1494 buffer = chd_img->current_buffer ^ 1;
1495 chd_read(chd_img->chd, hunk, chd_img->buffer +
1496 buffer * chd_img->header->hunkbytes);
1497 chd_img->current_hunk[buffer] = hunk;
1500 memcpy(subbuffer, chd_get_sector(buffer, sector_in_hunk) + CD_FRAMESIZE_RAW, SUB_FRAMESIZE);
1501 return SUB_FRAMESIZE;
1505 static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector)
1511 fseeko(f, base + sector * 2048, SEEK_SET);
1512 ret = fread((char *)dest + 12 * 2, 1, 2048, f);
1514 // not really necessary, fake mode 2 header
1515 memset(cdbuffer, 0, 12 * 2);
1516 sec2msf(sector + 2 * 75, (char *)&cdbuffer[12]);
1517 cdbuffer[12 + 3] = 1;
1524 static int cdread_async(FILE *f, unsigned int base, void *dest, int sector) {
1525 boolean found = FALSE;
1526 int i = sector % SECTOR_BUFFER_SIZE;
1529 if (f != cdHandle || base != 0 || dest != cdbuffer) {
1530 // Async reads are only supported for cdbuffer, so call the sync
1531 // function directly.
1532 return sync_cdimg_read_func(f, base, dest, sector);
1535 pthread_mutex_lock(&read_thread_msg_lock);
1537 // Only wait if we're not trying to read the next sector and
1538 // sector_start is set (meaning the last request hasn't been
1540 while(read_thread_sector_start != -1 && read_thread_sector_end + 1 != sector) {
1541 pthread_cond_wait(&read_thread_msg_done, &read_thread_msg_lock);
1544 if (read_thread_sector_start == -1)
1545 read_thread_sector_start = sector;
1547 read_thread_sector_end = sector;
1548 pthread_cond_signal(&read_thread_msg_avail);
1549 pthread_mutex_unlock(&read_thread_msg_lock);
1552 pthread_mutex_lock(§orbuffer_lock);
1553 if (sectorbuffer[i].sector == sector) {
1554 sectorbuffer_index = i;
1555 ret = sectorbuffer[i].ret;
1560 pthread_cond_wait(§orbuffer_cond, §orbuffer_lock);
1562 pthread_mutex_unlock(§orbuffer_lock);
1570 static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
1571 return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
1575 static unsigned char * CALLBACK ISOgetBuffer_chd(void) {
1576 return chd_get_sector(chd_img->current_buffer, chd_img->sector_in_hunk) + 12;
1581 static unsigned char * CALLBACK ISOgetBuffer_async(void) {
1582 unsigned char *buffer;
1583 pthread_mutex_lock(§orbuffer_lock);
1584 buffer = sectorbuffer[sectorbuffer_index].data;
1585 pthread_mutex_unlock(§orbuffer_lock);
1590 static unsigned char * CALLBACK ISOgetBuffer(void) {
1591 return cdbuffer + 12;
1594 static void PrintTracks(void) {
1597 for (i = 1; i <= numtracks; i++) {
1598 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
1599 i, (ti[i].type == DATA ? "DATA" :
1600 (ti[i].type == CDDA ? "AUDIO" : "UNKNOWN")),
1601 ti[i].start[0], ti[i].start[1], ti[i].start[2],
1602 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
1606 // This function is invoked by the front-end when opening an ISO
1607 // file for playback
1608 static long CALLBACK ISOopen(void) {
1609 boolean isMode1ISO = FALSE;
1610 char alt_bin_filename[MAXPATHLEN];
1611 const char *bin_filename;
1612 char image_str[1024];
1615 if (cdHandle || chd_img) {
1616 return 0; // it's already open
1619 cdHandle = fopen(GetIsoFile(), "rb");
1620 if (cdHandle == NULL) {
1621 SysPrintf(_("Could't open '%s' for reading: %s\n"),
1622 GetIsoFile(), strerror(errno));
1625 size_main = get_size(cdHandle);
1627 snprintf(image_str, sizeof(image_str) - 6*4 - 1,
1628 "Loaded CD Image: %s", GetIsoFile());
1630 cddaBigEndian = FALSE;
1631 subChanMixed = FALSE;
1634 cdrIsoMultidiskCount = 1;
1637 CDR_getBuffer = ISOgetBuffer;
1638 cdimg_read_func = cdread_normal;
1639 cdimg_read_sub_func = NULL;
1641 if (parsetoc(GetIsoFile()) == 0) {
1642 strcat(image_str, "[+toc]");
1644 else if (parseccd(GetIsoFile()) == 0) {
1645 strcat(image_str, "[+ccd]");
1647 else if (parsemds(GetIsoFile()) == 0) {
1648 strcat(image_str, "[+mds]");
1650 else if (parsecue(GetIsoFile()) == 0) {
1651 strcat(image_str, "[+cue]");
1653 if (handlepbp(GetIsoFile()) == 0) {
1654 strcat(image_str, "[+pbp]");
1655 CDR_getBuffer = ISOgetBuffer_compr;
1656 cdimg_read_func = cdread_compressed;
1658 else if (handlecbin(GetIsoFile()) == 0) {
1659 strcat(image_str, "[+cbin]");
1660 CDR_getBuffer = ISOgetBuffer_compr;
1661 cdimg_read_func = cdread_compressed;
1664 else if (handlechd(GetIsoFile()) == 0) {
1665 strcat(image_str, "[+chd]");
1666 CDR_getBuffer = ISOgetBuffer_chd;
1667 cdimg_read_func = cdread_chd;
1668 cdimg_read_sub_func = cdread_sub_chd;
1674 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
1675 strcat(image_str, "[+sub]");
1677 if (opensbifile(GetIsoFile()) == 0) {
1678 strcat(image_str, "[+sbi]");
1681 // maybe user selected metadata file instead of main .bin ..
1682 bin_filename = GetIsoFile();
1683 if (cdHandle && size_main < 2352 * 0x10) {
1684 static const char *exts[] = { ".bin", ".BIN", ".img", ".IMG" };
1689 strncpy(alt_bin_filename, bin_filename, sizeof(alt_bin_filename));
1690 alt_bin_filename[MAXPATHLEN - 1] = '\0';
1691 if (strlen(alt_bin_filename) >= 4) {
1692 p = alt_bin_filename + strlen(alt_bin_filename) - 4;
1693 for (i = 0; i < sizeof(exts) / sizeof(exts[0]); i++) {
1695 tmpf = fopen(alt_bin_filename, "rb");
1701 bin_filename = alt_bin_filename;
1704 size_main = get_size(cdHandle);
1708 // guess whether it is mode1/2048
1709 if (cdHandle && cdimg_read_func == cdread_normal && size_main % 2048 == 0) {
1710 unsigned int modeTest = 0;
1711 if (!fread(&modeTest, sizeof(modeTest), 1, cdHandle)) {
1712 SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
1714 if (SWAP32(modeTest) != 0xffffff00) {
1715 strcat(image_str, "[2048]");
1720 SysPrintf("%s.\n", image_str);
1724 if (subChanMixed && cdimg_read_func == cdread_normal) {
1725 cdimg_read_func = cdread_sub_mixed;
1726 cdimg_read_sub_func = cdread_sub_sub_mixed;
1728 else if (isMode1ISO) {
1729 cdimg_read_func = cdread_2048;
1730 cdimg_read_sub_func = NULL;
1733 if (Config.AsyncCD) {
1739 static long CALLBACK ISOclose(void) {
1742 if (cdHandle != NULL) {
1746 if (subHandle != NULL) {
1752 if (compr_img != NULL) {
1753 free(compr_img->index_table);
1759 if (chd_img != NULL) {
1760 chd_close(chd_img->chd);
1761 free(chd_img->buffer);
1767 for (i = 1; i <= numtracks; i++) {
1768 if (ti[i].handle != NULL) {
1769 fclose(ti[i].handle);
1770 ti[i].handle = NULL;
1777 memset(cdbuffer, 0, sizeof(cdbuffer));
1778 CDR_getBuffer = ISOgetBuffer;
1780 if (Config.AsyncCD) {
1787 static long CALLBACK ISOinit(void) {
1788 assert(cdHandle == NULL);
1789 assert(subHandle == NULL);
1791 return 0; // do nothing
1794 static long CALLBACK ISOshutdown(void) {
1799 // return Starting and Ending Track
1801 // byte 0 - start track
1802 // byte 1 - end track
1803 static long CALLBACK ISOgetTN(unsigned char *buffer) {
1806 if (numtracks > 0) {
1807 buffer[1] = numtracks;
1816 // return Track Time
1821 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
1824 unsigned char time[3];
1825 sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
1826 sec2msf(sect, (char *)time);
1827 buffer[2] = time[0];
1828 buffer[1] = time[1];
1829 buffer[0] = time[2];
1831 else if (numtracks > 0 && track <= numtracks) {
1832 buffer[2] = ti[track].start[0];
1833 buffer[1] = ti[track].start[1];
1834 buffer[0] = ti[track].start[2];
1845 // decode 'raw' subchannel data ripped by cdrdao
1846 static void DecodeRawSubData(void) {
1847 unsigned char subQData[12];
1850 memset(subQData, 0, sizeof(subQData));
1852 for (i = 0; i < 8 * 12; i++) {
1853 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
1854 subQData[i >> 3] |= (1 << (7 - (i & 7)));
1858 memcpy(&subbuffer[12], subQData, 12);
1862 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
1864 static boolean CALLBACK ISOreadTrack(unsigned char *time) {
1865 int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
1868 if (!cdHandle && !chd_img)
1871 if (pregapOffset && sector >= pregapOffset)
1874 ret = cdimg_read_func(cdHandle, 0, cdbuffer, sector);
1875 if (ret < 12*2 + 2048)
1882 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
1883 // does NOT uses bcd format
1884 static long CALLBACK ISOplay(unsigned char *time) {
1889 static long CALLBACK ISOstop(void) {
1893 // gets subchannel data
1894 static unsigned char* CALLBACK ISOgetBufferSub(int sector) {
1895 if (pregapOffset && sector >= pregapOffset) {
1897 if (sector < pregapOffset) // ?
1901 if (cdimg_read_sub_func != NULL) {
1902 if (cdimg_read_sub_func(cdHandle, sector) != SUB_FRAMESIZE)
1905 else if (subHandle != NULL) {
1906 if (fseeko(subHandle, sector * SUB_FRAMESIZE, SEEK_SET))
1908 if (fread(subbuffer, 1, SUB_FRAMESIZE, subHandle) != SUB_FRAMESIZE)
1915 if (subChanRaw) DecodeRawSubData();
1919 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
1920 CDR__getStatus(stat);
1922 // BIOS - boot ID (CD type)
1923 stat->Type = ti[1].type;
1928 // read CDDA sector into buffer
1929 long CALLBACK ISOreadCDDA(unsigned char m, unsigned char s, unsigned char f, unsigned char *buffer) {
1930 unsigned char msf[3] = {m, s, f};
1931 unsigned int track, track_start = 0;
1932 FILE *handle = cdHandle;
1933 unsigned int cddaCurPos;
1936 cddaCurPos = msf2sec((char *)msf);
1938 // find current track index
1939 for (track = numtracks; ; track--) {
1940 track_start = msf2sec(ti[track].start);
1941 if (track_start <= cddaCurPos)
1947 // data tracks play silent
1948 if (ti[track].type != CDDA) {
1949 memset(buffer, 0, CD_FRAMESIZE_RAW);
1954 // find the file that contains this track
1956 for (file = track; file > 1; file--) {
1957 if (ti[file].handle != NULL) {
1958 handle = ti[file].handle;
1963 if (!handle && !chd_img) {
1964 memset(buffer, 0, CD_FRAMESIZE_RAW);
1968 ret = cdimg_read_func(handle, ti[track].start_offset,
1969 buffer, cddaCurPos - track_start);
1970 if (ret != CD_FRAMESIZE_RAW) {
1971 memset(buffer, 0, CD_FRAMESIZE_RAW);
1975 if (cddaBigEndian) {
1979 for (i = 0; i < CD_FRAMESIZE_RAW / 2; i++) {
1980 tmp = buffer[i * 2];
1981 buffer[i * 2] = buffer[i * 2 + 1];
1982 buffer[i * 2 + 1] = tmp;
1989 void cdrIsoInit(void) {
1991 CDR_shutdown = ISOshutdown;
1993 CDR_close = ISOclose;
1994 CDR_getTN = ISOgetTN;
1995 CDR_getTD = ISOgetTD;
1996 CDR_readTrack = ISOreadTrack;
1997 CDR_getBuffer = ISOgetBuffer;
2000 CDR_getBufferSub = ISOgetBufferSub;
2001 CDR_getStatus = ISOgetStatus;
2002 CDR_readCDDA = ISOreadCDDA;
2004 CDR_getDriveLetter = CDR__getDriveLetter;
2005 CDR_configure = CDR__configure;
2006 CDR_test = CDR__test;
2007 CDR_about = CDR__about;
2008 CDR_setfilename = CDR__setfilename;
2013 int cdrIsoActive(void) {
2014 return (cdHandle || chd_img);