1 /***************************************************************************
2 * Copyright (C) 2007 PCSX-df Team *
3 * Copyright (C) 2009 Wei Mingzhi *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
19 ***************************************************************************/
21 #include "psxcommon.h"
36 unsigned int cdrIsoMultidiskCount;
37 unsigned int cdrIsoMultidiskSelect;
39 static FILE *cdHandle = NULL;
40 static FILE *cddaHandle = NULL;
41 static FILE *subHandle = NULL;
43 static boolean subChanMixed = FALSE;
44 static boolean subChanRaw = FALSE;
45 static boolean subChanMissing = FALSE;
47 static unsigned char cdbuffer[DATA_SIZE];
48 static unsigned char subbuffer[SUB_FRAMESIZE];
50 static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10];
52 #define CDDA_FRAMETIME (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75)
55 static HANDLE threadid;
57 static pthread_t threadid;
59 static unsigned int initial_offset = 0;
60 static boolean playing = FALSE;
61 static boolean cddaBigEndian = FALSE;
62 // offsets of cddaHandle file
63 static unsigned int cdda_cur_sector;
64 static unsigned int cdda_first_sector;
65 // toc_sector - file_sector
66 static unsigned int cdda_toc_delta;
67 /* Frame offset into CD image where pregap data would be found if it was there.
68 * If a game seeks there we must *not* return subchannel data since it's
69 * not in the CD image, so that cdrom code can fake subchannel data instead.
70 * XXX: there could be multiple pregaps but PSX dumps only have one? */
71 static unsigned int pregapOffset;
73 // compressed image stuff
75 unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
76 unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100];
77 unsigned int *index_table;
78 unsigned int index_len;
79 unsigned int block_shift;
80 unsigned int current_block;
81 unsigned int sector_in_blk;
84 int (*cdimg_read_func)(FILE *f, void *dest, int sector, int offset);
86 char* CALLBACK CDR__getDriveLetter(void);
87 long CALLBACK CDR__configure(void);
88 long CALLBACK CDR__test(void);
89 void CALLBACK CDR__about(void);
90 long CALLBACK CDR__setfilename(char *filename);
91 long CALLBACK CDR__getStatus(struct CdrStat *stat);
93 static void DecodeRawSubData(void);
95 extern void *hCDRDriver;
98 enum {DATA, CDDA} type;
99 char start[3]; // MSF-format
100 char length[3]; // MSF-format
101 FILE *handle; // for multi-track images CDDA
102 int start_offset; // sector offset from start of above file
105 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
107 static int numtracks = 0;
108 static struct trackinfo ti[MAXTRACKS];
110 // get a sector from a msf-array
111 static unsigned int msf2sec(char *msf) {
112 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
115 static void sec2msf(unsigned int s, char *msf) {
116 msf[0] = s / 75 / 60;
117 s = s - msf[0] * 75 * 60;
123 // divide a string of xx:yy:zz into m, s, f
124 static void tok2msf(char *time, char *msf) {
127 token = strtok(time, ":");
129 msf[0] = atoi(token);
135 token = strtok(NULL, ":");
137 msf[1] = atoi(token);
143 token = strtok(NULL, ":");
145 msf[2] = atoi(token);
153 static long GetTickCount(void) {
154 static time_t initial_time = 0;
157 gettimeofday(&now, NULL);
159 if (initial_time == 0) {
160 initial_time = now.tv_sec;
163 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
167 // this thread plays audio data
169 static void playthread(void *param)
171 static void *playthread(void *param)
174 long osleep, d, t, i, s;
182 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
183 d = cdimg_read_func(cddaHandle, sndbuffer + s, cdda_cur_sector, 0);
184 if (d < CD_FRAMESIZE_RAW)
187 if (cdda_cur_sector < cdda_first_sector)
188 memset(sndbuffer + s, 0, CD_FRAMESIZE_RAW);
200 if (!cdr.Muted && playing) {
202 for (i = 0; i < s / 2; i++) {
203 tmp = sndbuffer[i * 2];
204 sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
205 sndbuffer[i * 2 + 1] = tmp;
210 ret = SPU_playCDDAchannel((short *)sndbuffer, s);
213 } while (ret == 0x7761 && playing); // rearmed_wait
216 if (ret != 0x676f) { // !rearmed_go
220 // HACK: stop feeding data while emu is paused
222 while (stop && playing)
225 now = GetTickCount();
231 else if (osleep > CDDA_FRAMETIME) {
232 osleep = CDDA_FRAMETIME;
236 usleep(osleep * 1000);
250 // stop the CDDA playback
251 static void stopCDDA() {
258 WaitForSingleObject(threadid, INFINITE);
260 pthread_join(threadid, NULL);
264 // start the CDDA playback
265 static void startCDDA(unsigned int sector) {
270 cdda_cur_sector = sector;
274 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
276 pthread_create(&threadid, NULL, playthread, NULL);
280 // this function tries to get the .toc file of the given .bin
281 // the necessary data is put into the ti (trackinformation)-array
282 static int parsetoc(const char *isofile) {
283 char tocname[MAXPATHLEN];
285 char linebuf[256], dummy[256], name[256];
287 char time[20], time2[20];
288 unsigned int t, sector_offs;
292 // copy name of the iso and change extension from .bin to .toc
293 strncpy(tocname, isofile, sizeof(tocname));
294 tocname[MAXPATHLEN - 1] = '\0';
295 if (strlen(tocname) >= 4) {
296 strcpy(tocname + strlen(tocname) - 4, ".toc");
302 if ((fi = fopen(tocname, "r")) == NULL) {
303 // try changing extension to .cue (to satisfy some stupid tutorials)
304 strcpy(tocname + strlen(tocname) - 4, ".cue");
305 if ((fi = fopen(tocname, "r")) == NULL) {
306 // if filename is image.toc.bin, try removing .bin (for Brasero)
307 strcpy(tocname, isofile);
309 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
310 tocname[t - 4] = '\0';
311 if ((fi = fopen(tocname, "r")) == NULL) {
321 memset(&ti, 0, sizeof(ti));
322 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
324 sector_offs = 2 * 75;
326 // parse the .toc file
327 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
329 strncpy(dummy, linebuf, sizeof(linebuf));
330 token = strtok(dummy, " ");
332 if (token == NULL) continue;
334 if (!strcmp(token, "TRACK")) {
336 token = strtok(NULL, " ");
339 if (!strncmp(token, "MODE2_RAW", 9)) {
340 ti[numtracks].type = DATA;
341 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
343 // check if this image contains mixed subchannel data
344 token = strtok(NULL, " ");
345 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
350 else if (!strncmp(token, "AUDIO", 5)) {
351 ti[numtracks].type = CDDA;
354 else if (!strcmp(token, "DATAFILE")) {
355 if (ti[numtracks].type == CDDA) {
356 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
357 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
358 ti[numtracks].start_offset = t;
360 sec2msf(t, (char *)&ti[numtracks].start);
361 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
364 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
365 tok2msf((char *)&time, (char *)&ti[numtracks].length);
368 else if (!strcmp(token, "FILE")) {
369 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
370 tok2msf((char *)&time, (char *)&ti[numtracks].start);
371 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
372 ti[numtracks].start_offset = t;
373 t += msf2sec(ti[numtracks].start) + sector_offs;
374 sec2msf(t, (char *)&ti[numtracks].start);
375 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
377 else if (!strcmp(token, "ZERO")) {
378 sscanf(linebuf, "ZERO AUDIO RW_RAW %8s", time);
379 tok2msf((char *)&time, dummy);
380 sector_offs += msf2sec(dummy);
382 t = ti[numtracks - 1].start_offset;
383 pregapOffset = t + msf2sec(ti[numtracks - 1].length);
393 // this function tries to get the .cue file of the given .bin
394 // the necessary data is put into the ti (trackinformation)-array
395 static int parsecue(const char *isofile) {
396 char cuename[MAXPATHLEN];
397 char filepath[MAXPATHLEN];
403 char linebuf[256], tmpb[256], dummy[256];
404 unsigned int incue_max_len;
405 unsigned int t, file_len, sector_offs;
409 // copy name of the iso and change extension from .bin to .cue
410 strncpy(cuename, isofile, sizeof(cuename));
411 cuename[MAXPATHLEN - 1] = '\0';
412 if (strlen(cuename) >= 4) {
413 strcpy(cuename + strlen(cuename) - 4, ".cue");
419 if ((fi = fopen(cuename, "r")) == NULL) {
423 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
424 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
426 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
427 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
428 // Don't proceed further, as this is actually a .toc file rather
431 return parsetoc(isofile);
433 fseek(fi, 0, SEEK_SET);
436 // build a path for files referenced in .cue
437 strncpy(filepath, cuename, sizeof(filepath));
438 tmp = strrchr(filepath, '/') + 1;
440 tmp = strrchr(filepath, '\\') + 1;
444 filepath[sizeof(filepath) - 1] = 0;
446 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
448 memset(&ti, 0, sizeof(ti));
451 sector_offs = 2 * 75;
453 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
454 strncpy(dummy, linebuf, sizeof(linebuf));
455 token = strtok(dummy, " ");
461 if (!strcmp(token, "TRACK")) {
464 if (strstr(linebuf, "AUDIO") != NULL) {
465 ti[numtracks].type = CDDA;
467 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
468 ti[numtracks].type = DATA;
471 else if (!strcmp(token, "INDEX")) {
472 sscanf(linebuf, " INDEX %02d %8s", &t, time);
473 tok2msf(time, (char *)&ti[numtracks].start);
475 t = msf2sec(ti[numtracks].start);
476 ti[numtracks].start_offset = t;
478 sec2msf(t, ti[numtracks].start);
480 // default track length to file length
481 t = file_len - ti[numtracks].start_offset;
482 sec2msf(t, ti[numtracks].length);
484 if (numtracks > 1 && ti[numtracks].handle == NULL) {
485 // this track uses the same file as the last,
486 // start of this track is last track's end
487 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
488 sec2msf(t, ti[numtracks - 1].length);
490 if (numtracks > 1 && pregapOffset == -1)
491 pregapOffset = ti[numtracks].start_offset;
493 else if (!strcmp(token, "PREGAP")) {
494 if (sscanf(linebuf, " PREGAP %8s", time) == 1) {
495 tok2msf(time, dummy);
496 sector_offs += msf2sec(dummy);
498 pregapOffset = -1; // mark to fill track start_offset
500 else if (!strcmp(token, "FILE")) {
501 sscanf(linebuf, " FILE \"%[^\"]\"", tmpb);
504 ti[numtracks + 1].handle = fopen(tmpb, "rb");
505 if (ti[numtracks + 1].handle == NULL) {
507 tmp = strrchr(tmpb, '\\');
509 tmp = strrchr(tmpb, '/');
514 strncpy(incue_fname, tmp, incue_max_len);
515 ti[numtracks + 1].handle = fopen(filepath, "rb");
518 // update global offset if this is not first file in this .cue
519 if (numtracks + 1 > 1)
520 sector_offs += file_len;
523 if (ti[numtracks + 1].handle == NULL) {
524 SysPrintf(_("\ncould not open: %s\n"), filepath);
527 fseek(ti[numtracks + 1].handle, 0, SEEK_END);
528 file_len = ftell(ti[numtracks + 1].handle) / 2352;
530 if (numtracks == 0 && strlen(isofile) >= 4 &&
531 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
533 // user selected .cue as image file, use it's data track instead
535 cdHandle = fopen(filepath, "rb");
545 // this function tries to get the .ccd file of the given .img
546 // the necessary data is put into the ti (trackinformation)-array
547 static int parseccd(const char *isofile) {
548 char ccdname[MAXPATHLEN];
555 // copy name of the iso and change extension from .img to .ccd
556 strncpy(ccdname, isofile, sizeof(ccdname));
557 ccdname[MAXPATHLEN - 1] = '\0';
558 if (strlen(ccdname) >= 4) {
559 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
565 if ((fi = fopen(ccdname, "r")) == NULL) {
569 memset(&ti, 0, sizeof(ti));
571 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
572 if (!strncmp(linebuf, "[TRACK", 6)){
575 else if (!strncmp(linebuf, "MODE=", 5)) {
576 sscanf(linebuf, "MODE=%d", &t);
577 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
579 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
580 sscanf(linebuf, "INDEX 1=%d", &t);
581 sec2msf(t + 2 * 75, ti[numtracks].start);
582 ti[numtracks].start_offset = t;
584 // If we've already seen another track, this is its end
586 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
587 sec2msf(t, ti[numtracks - 1].length);
594 // Fill out the last track's end based on size
595 if (numtracks >= 1) {
596 fseek(cdHandle, 0, SEEK_END);
597 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
598 sec2msf(t, ti[numtracks].length);
604 // this function tries to get the .mds file of the given .mdf
605 // the necessary data is put into the ti (trackinformation)-array
606 static int parsemds(const char *isofile) {
607 char mdsname[MAXPATHLEN];
609 unsigned int offset, extra_offset, l, i;
614 // copy name of the iso and change extension from .mdf to .mds
615 strncpy(mdsname, isofile, sizeof(mdsname));
616 mdsname[MAXPATHLEN - 1] = '\0';
617 if (strlen(mdsname) >= 4) {
618 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
624 if ((fi = fopen(mdsname, "rb")) == NULL) {
628 memset(&ti, 0, sizeof(ti));
630 // check if it's a valid mds file
631 fread(&i, 1, sizeof(unsigned int), fi);
633 if (i != 0x4944454D) {
634 // not an valid mds file
639 // get offset to session block
640 fseek(fi, 0x50, SEEK_SET);
641 fread(&offset, 1, sizeof(unsigned int), fi);
642 offset = SWAP32(offset);
644 // get total number of tracks
646 fseek(fi, offset, SEEK_SET);
647 fread(&s, 1, sizeof(unsigned short), fi);
651 // get offset to track blocks
652 fseek(fi, 4, SEEK_CUR);
653 fread(&offset, 1, sizeof(unsigned int), fi);
654 offset = SWAP32(offset);
658 fseek(fi, offset + 4, SEEK_SET);
659 if (fgetc(fi) < 0xA0) {
665 // check if the image contains mixed subchannel data
666 fseek(fi, offset + 1, SEEK_SET);
667 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
670 for (i = 1; i <= numtracks; i++) {
671 fseek(fi, offset, SEEK_SET);
673 // get the track type
674 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
675 fseek(fi, 8, SEEK_CUR);
677 // get the track starting point
678 ti[i].start[0] = fgetc(fi);
679 ti[i].start[1] = fgetc(fi);
680 ti[i].start[2] = fgetc(fi);
682 fread(&extra_offset, 1, sizeof(unsigned int), fi);
683 extra_offset = SWAP32(extra_offset);
685 // get track start offset (in .mdf)
686 fseek(fi, offset + 0x28, SEEK_SET);
687 fread(&l, 1, sizeof(unsigned int), fi);
689 ti[i].start_offset = l / CD_FRAMESIZE_RAW;
692 fseek(fi, extra_offset, SEEK_SET);
693 fread(&l, 1, sizeof(unsigned int), fi);
696 pregapOffset = ti[i].start_offset;
698 // get the track length
699 fread(&l, 1, sizeof(unsigned int), fi);
701 sec2msf(l, ti[i].length);
710 static int handlepbp(const char *isofile) {
713 unsigned int dontcare[8];
714 unsigned int psar_offs;
727 unsigned int dontcare[6];
730 unsigned int t, cd_length, cdimg_base;
731 unsigned int offsettab[8], psisoimg_offs;
732 const char *ext = NULL;
735 if (strlen(isofile) >= 4)
736 ext = isofile + strlen(isofile) - 4;
737 if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
742 ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
743 if (ret != sizeof(pbp_hdr)) {
744 fprintf(stderr, "failed to read pbp\n");
748 ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
750 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
754 psisoimg_offs = pbp_hdr.psar_offs;
755 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
757 if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
759 ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
761 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
765 if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) {
766 fprintf(stderr, "failed to read offsettab\n");
770 for (i = 0; i < sizeof(offsettab) / sizeof(offsettab[0]); i++) {
771 if (offsettab[i] == 0)
774 cdrIsoMultidiskCount = i;
775 if (cdrIsoMultidiskCount == 0) {
776 fprintf(stderr, "multidisk eboot has 0 images?\n");
780 if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount)
781 cdrIsoMultidiskSelect = 0;
783 psisoimg_offs += offsettab[cdrIsoMultidiskSelect];
785 ret = fseek(cdHandle, psisoimg_offs, SEEK_SET);
787 fprintf(stderr, "failed to seek to %x\n", psisoimg_offs);
791 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
795 if (strcmp(psar_sig, "PSISOIMG00") != 0) {
796 fprintf(stderr, "bad psar_sig: %s\n", psar_sig);
801 ret = fseek(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
803 fprintf(stderr, "failed to seek to %x\n", psisoimg_offs + 0x800);
807 // first 3 entries are special
808 fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
809 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
810 numtracks = btoi(toc_entry.index1[0]);
812 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
813 cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
814 btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
816 for (i = 1; i <= numtracks; i++) {
817 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
819 ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
821 ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
822 btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
823 ti[i].start[0] = btoi(toc_entry.index1[0]);
824 ti[i].start[1] = btoi(toc_entry.index1[1]);
825 ti[i].start[2] = btoi(toc_entry.index1[2]);
828 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
829 sec2msf(t, ti[i - 1].length);
832 t = cd_length - ti[numtracks].start_offset;
833 sec2msf(t, ti[numtracks].length);
836 ret = fseek(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
838 fprintf(stderr, "failed to seek to ISO index\n");
842 compr_img = calloc(1, sizeof(*compr_img));
843 if (compr_img == NULL)
846 compr_img->block_shift = 4;
847 compr_img->current_block = (unsigned int)-1;
849 compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
850 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
851 if (compr_img->index_table == NULL)
854 cdimg_base = psisoimg_offs + 0x100000;
855 for (i = 0; i < compr_img->index_len; i++) {
856 ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
857 if (ret != sizeof(index_entry)) {
858 fprintf(stderr, "failed to read index_entry #%d\n", i);
862 if (index_entry.size == 0)
865 compr_img->index_table[i] = cdimg_base + index_entry.offset;
867 compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
872 free(compr_img->index_table);
873 compr_img->index_table = NULL;
875 if (compr_img != NULL) {
882 static int handlecbin(const char *isofile) {
886 unsigned int header_size;
887 unsigned long long total_bytes;
888 unsigned int block_size;
889 unsigned char ver; // 1
891 unsigned char rsv_06[2];
893 const char *ext = NULL;
894 unsigned int index = 0, plain;
897 if (strlen(isofile) >= 5)
898 ext = isofile + strlen(isofile) - 5;
899 if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
902 ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
903 if (ret != sizeof(ciso_hdr)) {
904 fprintf(stderr, "failed to read ciso header\n");
908 if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
909 fprintf(stderr, "bad ciso header\n");
912 if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
913 ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
915 fprintf(stderr, "failed to seek to %x\n", ciso_hdr.header_size);
920 compr_img = calloc(1, sizeof(*compr_img));
921 if (compr_img == NULL)
924 compr_img->block_shift = 0;
925 compr_img->current_block = (unsigned int)-1;
927 compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
928 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
929 if (compr_img->index_table == NULL)
932 ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
933 if (ret != compr_img->index_len) {
934 fprintf(stderr, "failed to read index table\n");
938 for (i = 0; i < compr_img->index_len + 1; i++) {
939 index = compr_img->index_table[i];
940 plain = index & 0x80000000;
942 compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
944 if ((long long)index << ciso_hdr.align >= 0x80000000ll)
945 fprintf(stderr, "warning: ciso img too large, expect problems\n");
950 free(compr_img->index_table);
951 compr_img->index_table = NULL;
953 if (compr_img != NULL) {
960 // this function tries to get the .sub file of the given .img
961 static int opensubfile(const char *isoname) {
962 char subname[MAXPATHLEN];
964 // copy name of the iso and change extension from .img to .sub
965 strncpy(subname, isoname, sizeof(subname));
966 subname[MAXPATHLEN - 1] = '\0';
967 if (strlen(subname) >= 4) {
968 strcpy(subname + strlen(subname) - 4, ".sub");
974 subHandle = fopen(subname, "rb");
975 if (subHandle == NULL) {
982 static int opensbifile(const char *isoname) {
983 char sbiname[MAXPATHLEN];
986 strncpy(sbiname, isoname, sizeof(sbiname));
987 sbiname[MAXPATHLEN - 1] = '\0';
988 if (strlen(sbiname) >= 4) {
989 strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
995 fseek(cdHandle, 0, SEEK_END);
996 s = ftell(cdHandle) / 2352;
998 return LoadSBI(sbiname, s);
1001 static int cdread_normal(FILE *f, void *dest, int sector, int offset)
1003 fseek(f, sector * CD_FRAMESIZE_RAW + offset, SEEK_SET);
1004 return fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
1007 static int cdread_sub_mixed(FILE *f, void *dest, int sector, int offset)
1011 fseek(f, sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + offset, SEEK_SET);
1012 ret = fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
1013 fread(subbuffer, 1, SUB_FRAMESIZE, f);
1015 if (subChanRaw) DecodeRawSubData();
1020 static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned long in_size)
1025 if (z.zalloc == NULL) {
1026 // XXX: one-time leak here..
1032 ret = inflateInit2(&z, -15);
1035 ret = inflateReset(&z);
1040 z.avail_in = in_size;
1042 z.avail_out = *out_size;
1044 ret = inflate(&z, Z_NO_FLUSH);
1047 *out_size -= z.avail_out;
1048 return ret == 1 ? 0 : ret;
1051 static int cdread_compressed(FILE *f, void *dest, int sector, int offset)
1053 unsigned long cdbuffer_size, cdbuffer_size_expect;
1054 unsigned int start_byte, size;
1058 block = sector >> compr_img->block_shift;
1059 compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1);
1061 if (block == compr_img->current_block) {
1062 //printf("hit sect %d\n", sector);
1066 if (sector >= compr_img->index_len * 16) {
1067 fprintf(stderr, "sector %d is past img end\n", sector);
1071 start_byte = compr_img->index_table[block] & 0x7fffffff;
1072 if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
1073 fprintf(stderr, "seek error for block %d at %x: ",
1079 is_compressed = !(compr_img->index_table[block] & 0x80000000);
1080 size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte;
1081 if (size > sizeof(compr_img->buff_compressed)) {
1082 fprintf(stderr, "block %d is too large: %u\n", block, size);
1086 if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0],
1087 1, size, cdHandle) != size) {
1088 fprintf(stderr, "read error for block %d at %x: ", block, start_byte);
1093 if (is_compressed) {
1094 cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift;
1095 cdbuffer_size = cdbuffer_size_expect;
1096 ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
1098 fprintf(stderr, "uncompress failed with %d for block %d, sector %d\n",
1099 ret, block, sector);
1102 if (cdbuffer_size != cdbuffer_size_expect)
1103 fprintf(stderr, "cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
1104 cdbuffer_size_expect, sector);
1108 compr_img->current_block = block;
1111 if (dest != cdbuffer) // copy avoid HACK
1112 memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk] + offset,
1113 CD_FRAMESIZE_RAW - offset);
1114 return CD_FRAMESIZE_RAW - offset;
1117 static int cdread_2048(FILE *f, void *dest, int sector, int offset)
1121 fseek(f, sector * 2048, SEEK_SET);
1122 ret = fread((char *)dest + 12, 1, 2048, f);
1124 // not really necessary, fake mode 2 header
1125 memset(cdbuffer, 0, 12);
1126 sec2msf(sector + 2 * 75, (char *)cdbuffer);
1132 static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
1133 return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
1136 static unsigned char * CALLBACK ISOgetBuffer(void) {
1140 static void PrintTracks(void) {
1143 for (i = 1; i <= numtracks; i++) {
1144 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
1145 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
1146 ti[i].start[0], ti[i].start[1], ti[i].start[2],
1147 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
1151 // This function is invoked by the front-end when opening an ISO
1152 // file for playback
1153 static long CALLBACK ISOopen(void) {
1154 boolean isMode1ISO = FALSE;
1156 if (cdHandle != NULL) {
1157 return 0; // it's already open
1160 cdHandle = fopen(GetIsoFile(), "rb");
1161 if (cdHandle == NULL) {
1165 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
1167 cddaBigEndian = FALSE;
1168 subChanMixed = FALSE;
1171 cdrIsoMultidiskCount = 1;
1173 CDR_getBuffer = ISOgetBuffer;
1174 cdimg_read_func = cdread_normal;
1176 if (parsecue(GetIsoFile()) == 0) {
1177 SysPrintf("[+cue]");
1179 else if (parsetoc(GetIsoFile()) == 0) {
1180 SysPrintf("[+toc]");
1182 else if (parseccd(GetIsoFile()) == 0) {
1183 SysPrintf("[+ccd]");
1185 else if (parsemds(GetIsoFile()) == 0) {
1186 SysPrintf("[+mds]");
1188 if (handlepbp(GetIsoFile()) == 0) {
1190 CDR_getBuffer = ISOgetBuffer_compr;
1191 cdimg_read_func = cdread_compressed;
1193 else if (handlecbin(GetIsoFile()) == 0) {
1194 SysPrintf("[cbin]");
1195 CDR_getBuffer = ISOgetBuffer_compr;
1196 cdimg_read_func = cdread_compressed;
1199 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
1200 SysPrintf("[+sub]");
1202 if (opensbifile(GetIsoFile()) == 0) {
1203 SysPrintf("[+sbi]");
1206 // guess whether it is mode1/2048
1207 fseek(cdHandle, 0, SEEK_END);
1208 if (ftell(cdHandle) % 2048 == 0) {
1209 unsigned int modeTest = 0;
1210 fseek(cdHandle, 0, SEEK_SET);
1211 fread(&modeTest, 4, 1, cdHandle);
1212 if (SWAP32(modeTest) != 0xffffff00) {
1213 SysPrintf("[2048]");
1217 fseek(cdHandle, 0, SEEK_SET);
1224 cdimg_read_func = cdread_sub_mixed;
1225 else if (isMode1ISO)
1226 cdimg_read_func = cdread_2048;
1228 // make sure we have another handle open for cdda
1229 if (numtracks > 1 && ti[1].handle == NULL) {
1230 ti[1].handle = fopen(GetIsoFile(), "rb");
1232 cdda_cur_sector = cdda_toc_delta = 0;
1237 static long CALLBACK ISOclose(void) {
1240 if (cdHandle != NULL) {
1244 if (subHandle != NULL) {
1251 if (compr_img != NULL) {
1252 free(compr_img->index_table);
1257 for (i = 1; i <= numtracks; i++) {
1258 if (ti[i].handle != NULL) {
1259 fclose(ti[i].handle);
1260 ti[i].handle = NULL;
1269 static long CALLBACK ISOinit(void) {
1270 assert(cdHandle == NULL);
1271 assert(subHandle == NULL);
1273 return 0; // do nothing
1276 static long CALLBACK ISOshutdown(void) {
1281 // return Starting and Ending Track
1283 // byte 0 - start track
1284 // byte 1 - end track
1285 static long CALLBACK ISOgetTN(unsigned char *buffer) {
1288 if (numtracks > 0) {
1289 buffer[1] = numtracks;
1298 // return Track Time
1303 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
1305 // CD length according pcsxr-svn (done a bit different here)
1307 unsigned char time[3];
1308 sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
1309 sec2msf(sect, (char *)time);
1310 buffer[2] = time[0];
1311 buffer[1] = time[1];
1312 buffer[0] = time[2];
1314 else if (numtracks > 0 && track <= numtracks) {
1315 buffer[2] = ti[track].start[0];
1316 buffer[1] = ti[track].start[1];
1317 buffer[0] = ti[track].start[2];
1328 // decode 'raw' subchannel data ripped by cdrdao
1329 static void DecodeRawSubData(void) {
1330 unsigned char subQData[12];
1333 memset(subQData, 0, sizeof(subQData));
1335 for (i = 0; i < 8 * 12; i++) {
1336 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
1337 subQData[i >> 3] |= (1 << (7 - (i & 7)));
1341 memcpy(&subbuffer[12], subQData, 12);
1345 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
1347 static long CALLBACK ISOreadTrack(unsigned char *time) {
1348 int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
1350 if (cdHandle == NULL) {
1355 subChanMissing = FALSE;
1356 if (sector >= pregapOffset) {
1358 if (sector < pregapOffset)
1359 subChanMissing = TRUE;
1363 cdimg_read_func(cdHandle, cdbuffer, sector, 12);
1365 if (subHandle != NULL) {
1366 fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
1367 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
1369 if (subChanRaw) DecodeRawSubData();
1376 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
1377 // does NOT uses bcd format
1378 static long CALLBACK ISOplay(unsigned char *time) {
1379 unsigned int i, abs_sect, start_sect = 0;
1380 int track_offset, file_sect;
1386 abs_sect = msf2sec((char *)time);
1387 for (i = numtracks; i > 1; i--) {
1388 start_sect = msf2sec(ti[i].start);
1389 if (start_sect <= abs_sect + 2 * 75)
1393 track_offset = abs_sect - start_sect;
1394 file_sect = ti[i].start_offset + track_offset;
1398 // find the file that contains this track
1400 if (ti[i].handle != NULL)
1403 cdda_first_sector = 0;
1405 cdda_first_sector = file_sect - track_offset;
1407 cdda_toc_delta = abs_sect - file_sect;
1408 cddaHandle = ti[i].handle;
1410 if (SPU_playCDDAchannel != NULL) {
1411 startCDDA(file_sect);
1417 static long CALLBACK ISOstop(void) {
1422 // gets subchannel data
1423 static unsigned char* CALLBACK ISOgetBufferSub(void) {
1424 if ((subHandle != NULL || subChanMixed) && !subChanMissing) {
1431 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
1434 CDR__getStatus(stat);
1438 stat->Status |= 0x80;
1444 sec = cdda_cur_sector + cdda_toc_delta;
1445 sec2msf(sec, (char *)stat->Time);
1450 void cdrIsoInit(void) {
1452 CDR_shutdown = ISOshutdown;
1454 CDR_close = ISOclose;
1455 CDR_getTN = ISOgetTN;
1456 CDR_getTD = ISOgetTD;
1457 CDR_readTrack = ISOreadTrack;
1458 CDR_getBuffer = ISOgetBuffer;
1461 CDR_getBufferSub = ISOgetBufferSub;
1462 CDR_getStatus = ISOgetStatus;
1464 CDR_getDriveLetter = CDR__getDriveLetter;
1465 CDR_configure = CDR__configure;
1466 CDR_test = CDR__test;
1467 CDR_about = CDR__about;
1468 CDR_setfilename = CDR__setfilename;
1473 int cdrIsoActive(void) {
1474 return (cdHandle != NULL);