fix maemo build
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
1 /***************************************************************************
2  *   Copyright (C) 2007 PCSX-df Team                                       *
3  *   Copyright (C) 2009 Wei Mingzhi                                        *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "psxcommon.h"
22 #include "plugins.h"
23 #include "cdrom.h"
24 #include "cdriso.h"
25 #include "ppf.h"
26
27 #ifdef _WIN32
28 #include <process.h>
29 #include <windows.h>
30 #else
31 #include <pthread.h>
32 #include <sys/time.h>
33 #endif
34 #include <zlib.h>
35
36 static FILE *cdHandle = NULL;
37 static FILE *cddaHandle = NULL;
38 static FILE *subHandle = NULL;
39
40 static boolean subChanMixed = FALSE;
41 static boolean subChanRaw = FALSE;
42 static boolean subChanMissing = FALSE;
43
44 static unsigned char cdbuffer[DATA_SIZE];
45 static unsigned char subbuffer[SUB_FRAMESIZE];
46
47 static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10];
48
49 #define CDDA_FRAMETIME                  (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75)
50
51 #ifdef _WIN32
52 static HANDLE threadid;
53 #else
54 static pthread_t threadid;
55 #endif
56 static unsigned int initial_offset = 0;
57 static boolean playing = FALSE;
58 static boolean cddaBigEndian = FALSE;
59 static unsigned int cdda_cur_sector;
60 static unsigned int cdda_start_sector;
61 /* Frame offset into CD image where pregap data would be found if it was there.
62  * If a game seeks there we must *not* return subchannel data since it's
63  * not in the CD image, so that cdrom code can fake subchannel data instead.
64  * XXX: there could be multiple pregaps but PSX dumps only have one? */
65 static unsigned int pregapOffset;
66
67 // compressed image stuff
68 static struct {
69         unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
70         unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100];
71         unsigned int *index_table;
72         unsigned int index_len;
73         unsigned int block_shift;
74         unsigned int current_block;
75         unsigned int sector_in_blk;
76 } *compr_img;
77
78 int (*cdimg_read_func)(FILE *f, void *dest, int sector, int offset);
79
80 char* CALLBACK CDR__getDriveLetter(void);
81 long CALLBACK CDR__configure(void);
82 long CALLBACK CDR__test(void);
83 void CALLBACK CDR__about(void);
84 long CALLBACK CDR__setfilename(char *filename);
85 long CALLBACK CDR__getStatus(struct CdrStat *stat);
86
87 static void DecodeRawSubData(void);
88
89 extern void *hCDRDriver;
90
91 struct trackinfo {
92         enum {DATA, CDDA} type;
93         char start[3];          // MSF-format
94         char length[3];         // MSF-format
95         FILE *handle;           // for multi-track images CDDA
96         int start_offset;       // sector offset from start of above file
97 };
98
99 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
100
101 static int numtracks = 0;
102 static struct trackinfo ti[MAXTRACKS];
103
104 // get a sector from a msf-array
105 static unsigned int msf2sec(char *msf) {
106         return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
107 }
108
109 static void sec2msf(unsigned int s, char *msf) {
110         msf[0] = s / 75 / 60;
111         s = s - msf[0] * 75 * 60;
112         msf[1] = s / 75;
113         s = s - msf[1] * 75;
114         msf[2] = s;
115 }
116
117 // divide a string of xx:yy:zz into m, s, f
118 static void tok2msf(char *time, char *msf) {
119         char *token;
120
121         token = strtok(time, ":");
122         if (token) {
123                 msf[0] = atoi(token);
124         }
125         else {
126                 msf[0] = 0;
127         }
128
129         token = strtok(NULL, ":");
130         if (token) {
131                 msf[1] = atoi(token);
132         }
133         else {
134                 msf[1] = 0;
135         }
136
137         token = strtok(NULL, ":");
138         if (token) {
139                 msf[2] = atoi(token);
140         }
141         else {
142                 msf[2] = 0;
143         }
144 }
145
146 #ifndef _WIN32
147 static long GetTickCount(void) {
148         static time_t           initial_time = 0;
149         struct timeval          now;
150
151         gettimeofday(&now, NULL);
152
153         if (initial_time == 0) {
154                 initial_time = now.tv_sec;
155         }
156
157         return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
158 }
159 #endif
160
161 // this thread plays audio data
162 #ifdef _WIN32
163 static void playthread(void *param)
164 #else
165 static void *playthread(void *param)
166 #endif
167 {
168         long osleep, d, t, i, s;
169         unsigned char   tmp;
170         int ret = 0;
171
172         t = GetTickCount();
173
174         while (playing) {
175                 s = 0;
176                 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
177                         d = cdimg_read_func(cddaHandle, sndbuffer + s, cdda_cur_sector, 0);
178                         if (d < CD_FRAMESIZE_RAW)
179                                 break;
180
181                         s += d;
182                         cdda_cur_sector++;
183                 }
184
185                 if (subHandle != NULL) {
186                         fseek(subHandle, cdda_cur_sector * SUB_FRAMESIZE, SEEK_SET);
187                         fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
188
189                         if (subChanRaw) DecodeRawSubData();
190                 }
191
192                 if (s == 0) {
193                         playing = FALSE;
194                         initial_offset = 0;
195                         break;
196                 }
197
198                 if (!cdr.Muted && playing) {
199                         if (cddaBigEndian) {
200                                 for (i = 0; i < s / 2; i++) {
201                                         tmp = sndbuffer[i * 2];
202                                         sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
203                                         sndbuffer[i * 2 + 1] = tmp;
204                                 }
205                         }
206
207                         do {
208                                 ret = SPU_playCDDAchannel((short *)sndbuffer, s);
209                                 if (ret == 0x7761)
210                                         usleep(6 * 1000);
211                         } while (ret == 0x7761 && playing); // rearmed_wait
212                 }
213
214                 if (ret != 0x676f) { // !rearmed_go
215                         // do approx sleep
216                         long now;
217
218                         // HACK: stop feeding data while emu is paused
219                         extern int stop;
220                         while (stop && playing)
221                                 usleep(10000);
222
223                         now = GetTickCount();
224                         osleep = t - now;
225                         if (osleep <= 0) {
226                                 osleep = 1;
227                                 t = now;
228                         }
229                         else if (osleep > CDDA_FRAMETIME) {
230                                 osleep = CDDA_FRAMETIME;
231                                 t = now;
232                         }
233
234                         usleep(osleep * 1000);
235                         t += CDDA_FRAMETIME;
236                 }
237
238         }
239
240 #ifdef _WIN32
241         _endthread();
242 #else
243         pthread_exit(0);
244         return NULL;
245 #endif
246 }
247
248 // stop the CDDA playback
249 static void stopCDDA() {
250         if (!playing) {
251                 return;
252         }
253
254         playing = FALSE;
255 #ifdef _WIN32
256         WaitForSingleObject(threadid, INFINITE);
257 #else
258         pthread_join(threadid, NULL);
259 #endif
260 }
261
262 // start the CDDA playback
263 static void startCDDA(unsigned int sector) {
264         if (playing) {
265                 stopCDDA();
266         }
267
268         cdda_cur_sector = sector;
269         playing = TRUE;
270
271 #ifdef _WIN32
272         threadid = (HANDLE)_beginthread(playthread, 0, NULL);
273 #else
274         pthread_create(&threadid, NULL, playthread, NULL);
275 #endif
276 }
277
278 // this function tries to get the .toc file of the given .bin
279 // the necessary data is put into the ti (trackinformation)-array
280 static int parsetoc(const char *isofile) {
281         char                    tocname[MAXPATHLEN];
282         FILE                    *fi;
283         char                    linebuf[256], dummy[256], name[256];
284         char                    *token;
285         char                    time[20], time2[20];
286         unsigned int    t, sector_offs;
287
288         numtracks = 0;
289
290         // copy name of the iso and change extension from .bin to .toc
291         strncpy(tocname, isofile, sizeof(tocname));
292         tocname[MAXPATHLEN - 1] = '\0';
293         if (strlen(tocname) >= 4) {
294                 strcpy(tocname + strlen(tocname) - 4, ".toc");
295         }
296         else {
297                 return -1;
298         }
299
300         if ((fi = fopen(tocname, "r")) == NULL) {
301                 // try changing extension to .cue (to satisfy some stupid tutorials)
302                 strcpy(tocname + strlen(tocname) - 4, ".cue");
303                 if ((fi = fopen(tocname, "r")) == NULL) {
304                         // if filename is image.toc.bin, try removing .bin (for Brasero)
305                         strcpy(tocname, isofile);
306                         t = strlen(tocname);
307                         if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
308                                 tocname[t - 4] = '\0';
309                                 if ((fi = fopen(tocname, "r")) == NULL) {
310                                         return -1;
311                                 }
312                         }
313                         else {
314                                 return -1;
315                         }
316                 }
317         }
318
319         memset(&ti, 0, sizeof(ti));
320         cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
321
322         sector_offs = 2 * 75;
323
324         // parse the .toc file
325         while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
326                 // search for tracks
327                 strncpy(dummy, linebuf, sizeof(linebuf));
328                 token = strtok(dummy, " ");
329
330                 if (token == NULL) continue;
331
332                 if (!strcmp(token, "TRACK")) {
333                         // get type of track
334                         token = strtok(NULL, " ");
335                         numtracks++;
336
337                         if (!strncmp(token, "MODE2_RAW", 9)) {
338                                 ti[numtracks].type = DATA;
339                                 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
340
341                                 // check if this image contains mixed subchannel data
342                                 token = strtok(NULL, " ");
343                                 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
344                                         subChanMixed = TRUE;
345                                         subChanRaw = TRUE;
346                                 }
347                         }
348                         else if (!strncmp(token, "AUDIO", 5)) {
349                                 ti[numtracks].type = CDDA;
350                         }
351                 }
352                 else if (!strcmp(token, "DATAFILE")) {
353                         if (ti[numtracks].type == CDDA) {
354                                 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
355                                 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
356                                 ti[numtracks].start_offset = t;
357                                 t += sector_offs;
358                                 sec2msf(t, (char *)&ti[numtracks].start);
359                                 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
360                         }
361                         else {
362                                 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
363                                 tok2msf((char *)&time, (char *)&ti[numtracks].length);
364                         }
365                 }
366                 else if (!strcmp(token, "FILE")) {
367                         sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
368                         tok2msf((char *)&time, (char *)&ti[numtracks].start);
369                         t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
370                         ti[numtracks].start_offset = t;
371                         t += msf2sec(ti[numtracks].start) + sector_offs;
372                         sec2msf(t, (char *)&ti[numtracks].start);
373                         tok2msf((char *)&time2, (char *)&ti[numtracks].length);
374                 }
375                 else if (!strcmp(token, "ZERO")) {
376                         sscanf(linebuf, "ZERO AUDIO RW_RAW %8s", time);
377                         tok2msf((char *)&time, dummy);
378                         sector_offs += msf2sec(dummy);
379                         if (numtracks > 1) {
380                                 t = ti[numtracks - 1].start_offset;
381                                 pregapOffset = t + msf2sec(ti[numtracks - 1].length);
382                         }
383                 }
384         }
385
386         fclose(fi);
387
388         return 0;
389 }
390
391 // this function tries to get the .cue file of the given .bin
392 // the necessary data is put into the ti (trackinformation)-array
393 static int parsecue(const char *isofile) {
394         char                    cuename[MAXPATHLEN];
395         char                    filepath[MAXPATHLEN];
396         char                    *incue_fname;
397         FILE                    *fi;
398         char                    *token;
399         char                    time[20];
400         char                    *tmp;
401         char                    linebuf[256], tmpb[256], dummy[256];
402         unsigned int    incue_max_len;
403         unsigned int    t, file_len, sector_offs;
404
405         numtracks = 0;
406
407         // copy name of the iso and change extension from .bin to .cue
408         strncpy(cuename, isofile, sizeof(cuename));
409         cuename[MAXPATHLEN - 1] = '\0';
410         if (strlen(cuename) >= 4) {
411                 strcpy(cuename + strlen(cuename) - 4, ".cue");
412         }
413         else {
414                 return -1;
415         }
416
417         if ((fi = fopen(cuename, "r")) == NULL) {
418                 return -1;
419         }
420
421         // Some stupid tutorials wrongly tell users to use cdrdao to rip a
422         // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
423         // that...
424         if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
425                 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
426                         // Don't proceed further, as this is actually a .toc file rather
427                         // than a .cue file.
428                         fclose(fi);
429                         return parsetoc(isofile);
430                 }
431                 fseek(fi, 0, SEEK_SET);
432         }
433
434         // build a path for files referenced in .cue
435         strncpy(filepath, cuename, sizeof(filepath));
436         tmp = strrchr(filepath, '/') + 1;
437         if (tmp == NULL)
438                 tmp = strrchr(filepath, '\\') + 1;
439         if (tmp == NULL)
440                 tmp = filepath;
441         *tmp = 0;
442         filepath[sizeof(filepath) - 1] = 0;
443         incue_fname = tmp;
444         incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
445
446         memset(&ti, 0, sizeof(ti));
447
448         file_len = 0;
449         sector_offs = 2 * 75;
450
451         while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
452                 strncpy(dummy, linebuf, sizeof(linebuf));
453                 token = strtok(dummy, " ");
454
455                 if (token == NULL) {
456                         continue;
457                 }
458
459                 if (!strcmp(token, "TRACK")) {
460                         numtracks++;
461
462                         if (strstr(linebuf, "AUDIO") != NULL) {
463                                 ti[numtracks].type = CDDA;
464                         }
465                         else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
466                                 ti[numtracks].type = DATA;
467                         }
468                 }
469                 else if (!strcmp(token, "INDEX")) {
470                         sscanf(linebuf, " INDEX %02d %8s", &t, time);
471                         tok2msf(time, (char *)&ti[numtracks].start);
472
473                         t = msf2sec(ti[numtracks].start);
474                         ti[numtracks].start_offset = t;
475                         t += sector_offs;
476                         sec2msf(t, ti[numtracks].start);
477
478                         // default track length to file length
479                         t = file_len - ti[numtracks].start_offset;
480                         sec2msf(t, ti[numtracks].length);
481
482                         if (numtracks > 1 && ti[numtracks].handle == NULL) {
483                                 // this track uses the same file as the last,
484                                 // start of this track is last track's end
485                                 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
486                                 sec2msf(t, ti[numtracks - 1].length);
487                         }
488                         if (numtracks > 1 && pregapOffset == -1)
489                                 pregapOffset = ti[numtracks].start_offset;
490                 }
491                 else if (!strcmp(token, "PREGAP")) {
492                         if (sscanf(linebuf, " PREGAP %8s", time) == 1) {
493                                 tok2msf(time, dummy);
494                                 sector_offs += msf2sec(dummy);
495                         }
496                         pregapOffset = -1; // mark to fill track start_offset
497                 }
498                 else if (!strcmp(token, "FILE")) {
499                         sscanf(linebuf, " FILE \"%[^\"]\"", tmpb);
500
501                         // absolute path?
502                         ti[numtracks + 1].handle = fopen(tmpb, "rb");
503                         if (ti[numtracks + 1].handle == NULL) {
504                                 // relative to .cue?
505                                 tmp = strrchr(tmpb, '\\');
506                                 if (tmp == NULL)
507                                         tmp = strrchr(tmpb, '/');
508                                 if (tmp != NULL)
509                                         tmp++;
510                                 else
511                                         tmp = tmpb;
512                                 strncpy(incue_fname, tmp, incue_max_len);
513                                 ti[numtracks + 1].handle = fopen(filepath, "rb");
514                         }
515
516                         // update global offset if this is not first file in this .cue
517                         if (numtracks + 1 > 1)
518                                 sector_offs += file_len;
519
520                         file_len = 0;
521                         if (ti[numtracks + 1].handle == NULL) {
522                                 SysPrintf(_("\ncould not open: %s\n"), filepath);
523                                 continue;
524                         }
525                         fseek(ti[numtracks + 1].handle, 0, SEEK_END);
526                         file_len = ftell(ti[numtracks + 1].handle) / 2352;
527
528                         if (numtracks == 0 && strlen(isofile) >= 4 &&
529                                 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
530                         {
531                                 // user selected .cue as image file, use it's data track instead
532                                 fclose(cdHandle);
533                                 cdHandle = fopen(filepath, "rb");
534                         }
535                 }
536         }
537
538         fclose(fi);
539
540         return 0;
541 }
542
543 // this function tries to get the .ccd file of the given .img
544 // the necessary data is put into the ti (trackinformation)-array
545 static int parseccd(const char *isofile) {
546         char                    ccdname[MAXPATHLEN];
547         FILE                    *fi;
548         char                    linebuf[256];
549         unsigned int    t;
550
551         numtracks = 0;
552
553         // copy name of the iso and change extension from .img to .ccd
554         strncpy(ccdname, isofile, sizeof(ccdname));
555         ccdname[MAXPATHLEN - 1] = '\0';
556         if (strlen(ccdname) >= 4) {
557                 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
558         }
559         else {
560                 return -1;
561         }
562
563         if ((fi = fopen(ccdname, "r")) == NULL) {
564                 return -1;
565         }
566
567         memset(&ti, 0, sizeof(ti));
568
569         while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
570                 if (!strncmp(linebuf, "[TRACK", 6)){
571                         numtracks++;
572                 }
573                 else if (!strncmp(linebuf, "MODE=", 5)) {
574                         sscanf(linebuf, "MODE=%d", &t);
575                         ti[numtracks].type = ((t == 0) ? CDDA : DATA);
576                 }
577                 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
578                         sscanf(linebuf, "INDEX 1=%d", &t);
579                         sec2msf(t + 2 * 75, ti[numtracks].start);
580                         ti[numtracks].start_offset = t;
581
582                         // If we've already seen another track, this is its end
583                         if (numtracks > 1) {
584                                 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
585                                 sec2msf(t, ti[numtracks - 1].length);
586                         }
587                 }
588         }
589
590         fclose(fi);
591
592         // Fill out the last track's end based on size
593         if (numtracks >= 1) {
594                 fseek(cdHandle, 0, SEEK_END);
595                 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
596                 sec2msf(t, ti[numtracks].length);
597         }
598
599         return 0;
600 }
601
602 // this function tries to get the .mds file of the given .mdf
603 // the necessary data is put into the ti (trackinformation)-array
604 static int parsemds(const char *isofile) {
605         char                    mdsname[MAXPATHLEN];
606         FILE                    *fi;
607         unsigned int    offset, extra_offset, l, i;
608         unsigned short  s;
609
610         numtracks = 0;
611
612         // copy name of the iso and change extension from .mdf to .mds
613         strncpy(mdsname, isofile, sizeof(mdsname));
614         mdsname[MAXPATHLEN - 1] = '\0';
615         if (strlen(mdsname) >= 4) {
616                 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
617         }
618         else {
619                 return -1;
620         }
621
622         if ((fi = fopen(mdsname, "rb")) == NULL) {
623                 return -1;
624         }
625
626         memset(&ti, 0, sizeof(ti));
627
628         // check if it's a valid mds file
629         fread(&i, 1, sizeof(unsigned int), fi);
630         i = SWAP32(i);
631         if (i != 0x4944454D) {
632                 // not an valid mds file
633                 fclose(fi);
634                 return -1;
635         }
636
637         // get offset to session block
638         fseek(fi, 0x50, SEEK_SET);
639         fread(&offset, 1, sizeof(unsigned int), fi);
640         offset = SWAP32(offset);
641
642         // get total number of tracks
643         offset += 14;
644         fseek(fi, offset, SEEK_SET);
645         fread(&s, 1, sizeof(unsigned short), fi);
646         s = SWAP16(s);
647         numtracks = s;
648
649         // get offset to track blocks
650         fseek(fi, 4, SEEK_CUR);
651         fread(&offset, 1, sizeof(unsigned int), fi);
652         offset = SWAP32(offset);
653
654         // skip lead-in data
655         while (1) {
656                 fseek(fi, offset + 4, SEEK_SET);
657                 if (fgetc(fi) < 0xA0) {
658                         break;
659                 }
660                 offset += 0x50;
661         }
662
663         // check if the image contains mixed subchannel data
664         fseek(fi, offset + 1, SEEK_SET);
665         subChanMixed = (fgetc(fi) ? TRUE : FALSE);
666
667         // read track data
668         for (i = 1; i <= numtracks; i++) {
669                 fseek(fi, offset, SEEK_SET);
670
671                 // get the track type
672                 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
673                 fseek(fi, 8, SEEK_CUR);
674
675                 // get the track starting point
676                 ti[i].start[0] = fgetc(fi);
677                 ti[i].start[1] = fgetc(fi);
678                 ti[i].start[2] = fgetc(fi);
679
680                 fread(&extra_offset, 1, sizeof(unsigned int), fi);
681                 extra_offset = SWAP32(extra_offset);
682
683                 // get track start offset (in .mdf)
684                 fseek(fi, offset + 0x28, SEEK_SET);
685                 fread(&l, 1, sizeof(unsigned int), fi);
686                 l = SWAP32(l);
687                 ti[i].start_offset = l / CD_FRAMESIZE_RAW;
688
689                 // get pregap
690                 fseek(fi, extra_offset, SEEK_SET);
691                 fread(&l, 1, sizeof(unsigned int), fi);
692                 l = SWAP32(l);
693                 if (l != 0 && i > 1)
694                         pregapOffset = ti[i].start_offset;
695
696                 // get the track length
697                 fread(&l, 1, sizeof(unsigned int), fi);
698                 l = SWAP32(l);
699                 sec2msf(l, ti[i].length);
700
701                 offset += 0x50;
702         }
703
704         fclose(fi);
705         return 0;
706 }
707
708 static int handlepbp(const char *isofile) {
709         struct {
710                 unsigned int sig;
711                 unsigned int dontcare[8];
712                 unsigned int psar_offs;
713         } pbp_hdr;
714         struct {
715                 unsigned char type;
716                 unsigned char pad0;
717                 unsigned char track;
718                 char index0[3];
719                 char pad1;
720                 char index1[3];
721         } toc_entry;
722         struct {
723                 unsigned int offset;
724                 unsigned int size;
725                 unsigned int dontcare[6];
726         } index_entry;
727         char psar_sig[9];
728         unsigned int t, cd_length, cdimg_base;
729         const char *ext = NULL;
730         int i, ret;
731
732         if (strlen(isofile) >= 4)
733                 ext = isofile + strlen(isofile) - 4;
734         if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
735                 return -1;
736
737         numtracks = 0;
738
739         ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
740         if (ret != sizeof(pbp_hdr)) {
741                 fprintf(stderr, "failed to read pbp\n");
742                 goto fail_io;
743         }
744
745         ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
746         if (ret != 0) {
747                 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
748                 goto fail_io;
749         }
750
751         fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
752         psar_sig[8] = 0;
753         if (strcmp(psar_sig, "PSISOIMG") != 0) {
754                 fprintf(stderr, "bad psar_sig: %s\n", psar_sig);
755                 goto fail_io;
756         }
757
758         // seek to TOC
759         ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x800, SEEK_SET);
760         if (ret != 0) {
761                 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
762                 goto fail_io;
763         }
764
765         // first 3 entries are special
766         fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
767         fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
768         numtracks = btoi(toc_entry.index1[0]);
769
770         fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
771         cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
772                 btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
773
774         for (i = 1; i <= numtracks; i++) {
775                 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
776
777                 ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
778
779                 ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
780                         btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
781                 ti[i].start[0] = btoi(toc_entry.index1[0]);
782                 ti[i].start[1] = btoi(toc_entry.index1[1]);
783                 ti[i].start[2] = btoi(toc_entry.index1[2]);
784
785                 if (i > 1) {
786                         t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
787                         sec2msf(t, ti[i - 1].length);
788                 }
789         }
790         t = cd_length - ti[numtracks].start_offset;
791         sec2msf(t, ti[numtracks].length);
792
793         // seek to ISO index
794         ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x4000, SEEK_SET);
795         if (ret != 0) {
796                 fprintf(stderr, "failed to seek to ISO index\n");
797                 goto fail_io;
798         }
799
800         compr_img = calloc(1, sizeof(*compr_img));
801         if (compr_img == NULL)
802                 goto fail_io;
803
804         compr_img->block_shift = 4;
805         compr_img->current_block = (unsigned int)-1;
806
807         compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
808         compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
809         if (compr_img->index_table == NULL)
810                 goto fail_io;
811
812         cdimg_base = pbp_hdr.psar_offs + 0x100000;
813         for (i = 0; i < compr_img->index_len; i++) {
814                 ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
815                 if (ret != sizeof(index_entry)) {
816                         fprintf(stderr, "failed to read index_entry #%d\n", i);
817                         goto fail_index;
818                 }
819
820                 if (index_entry.size == 0)
821                         break;
822
823                 compr_img->index_table[i] = cdimg_base + index_entry.offset;
824         }
825         compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
826
827         return 0;
828
829 fail_index:
830         free(compr_img->index_table);
831         compr_img->index_table = NULL;
832 fail_io:
833         if (compr_img != NULL) {
834                 free(compr_img);
835                 compr_img = NULL;
836         }
837         return -1;
838 }
839
840 static int handlecbin(const char *isofile) {
841         struct
842         {
843                 char magic[4];
844                 unsigned int header_size;
845                 unsigned long long total_bytes;
846                 unsigned int block_size;
847                 unsigned char ver;              // 1
848                 unsigned char align;
849                 unsigned char rsv_06[2];
850         } ciso_hdr;
851         const char *ext = NULL;
852         unsigned int index, plain;
853         int i, ret;
854
855         if (strlen(isofile) >= 5)
856                 ext = isofile + strlen(isofile) - 5;
857         if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
858                 return -1;
859
860         ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
861         if (ret != sizeof(ciso_hdr)) {
862                 fprintf(stderr, "failed to read ciso header\n");
863                 return -1;
864         }
865
866         if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
867                 fprintf(stderr, "bad ciso header\n");
868                 return -1;
869         }
870         if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
871                 ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
872                 if (ret != 0) {
873                         fprintf(stderr, "failed to seek to %x\n", ciso_hdr.header_size);
874                         return -1;
875                 }
876         }
877
878         compr_img = calloc(1, sizeof(*compr_img));
879         if (compr_img == NULL)
880                 goto fail_io;
881
882         compr_img->block_shift = 0;
883         compr_img->current_block = (unsigned int)-1;
884
885         compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
886         compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
887         if (compr_img->index_table == NULL)
888                 goto fail_io;
889
890         ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
891         if (ret != compr_img->index_len) {
892                 fprintf(stderr, "failed to read index table\n");
893                 goto fail_index;
894         }
895
896         for (i = 0; i < compr_img->index_len + 1; i++) {
897                 index = compr_img->index_table[i];
898                 plain = index & 0x80000000;
899                 index &= 0x7fffffff;
900                 compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
901         }
902         if ((long long)index << ciso_hdr.align >= 0x80000000ll)
903                 fprintf(stderr, "warning: ciso img too large, expect problems\n");
904
905         return 0;
906
907 fail_index:
908         free(compr_img->index_table);
909         compr_img->index_table = NULL;
910 fail_io:
911         if (compr_img != NULL) {
912                 free(compr_img);
913                 compr_img = NULL;
914         }
915         return -1;
916 }
917
918 // this function tries to get the .sub file of the given .img
919 static int opensubfile(const char *isoname) {
920         char            subname[MAXPATHLEN];
921
922         // copy name of the iso and change extension from .img to .sub
923         strncpy(subname, isoname, sizeof(subname));
924         subname[MAXPATHLEN - 1] = '\0';
925         if (strlen(subname) >= 4) {
926                 strcpy(subname + strlen(subname) - 4, ".sub");
927         }
928         else {
929                 return -1;
930         }
931
932         subHandle = fopen(subname, "rb");
933         if (subHandle == NULL) {
934                 return -1;
935         }
936
937         return 0;
938 }
939
940 static int opensbifile(const char *isoname) {
941         char            sbiname[MAXPATHLEN];
942         int             s;
943
944         strncpy(sbiname, isoname, sizeof(sbiname));
945         sbiname[MAXPATHLEN - 1] = '\0';
946         if (strlen(sbiname) >= 4) {
947                 strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
948         }
949         else {
950                 return -1;
951         }
952
953         fseek(cdHandle, 0, SEEK_END);
954         s = ftell(cdHandle) / 2352;
955
956         return LoadSBI(sbiname, s);
957 }
958
959 static int cdread_normal(FILE *f, void *dest, int sector, int offset)
960 {
961         fseek(f, sector * CD_FRAMESIZE_RAW + offset, SEEK_SET);
962         return fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
963 }
964
965 static int cdread_sub_mixed(FILE *f, void *dest, int sector, int offset)
966 {
967         int ret;
968
969         fseek(f, sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + offset, SEEK_SET);
970         ret = fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
971         fread(subbuffer, 1, SUB_FRAMESIZE, f);
972
973         if (subChanRaw) DecodeRawSubData();
974
975         return ret;
976 }
977
978 static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned long in_size)
979 {
980         static z_stream z;
981         int ret = 0;
982
983         if (z.zalloc == NULL) {
984                 // XXX: one-time leak here..
985                 z.next_in = Z_NULL;
986                 z.avail_in = 0;
987                 z.zalloc = Z_NULL;
988                 z.zfree = Z_NULL;
989                 z.opaque = Z_NULL;
990                 ret = inflateInit2(&z, -15);
991         }
992         else
993                 ret = inflateReset(&z);
994         if (ret != Z_OK)
995                 return ret;
996
997         z.next_in = in;
998         z.avail_in = in_size;
999         z.next_out = out;
1000         z.avail_out = *out_size;
1001
1002         ret = inflate(&z, Z_NO_FLUSH);
1003         //inflateEnd(&z);
1004
1005         *out_size -= z.avail_out;
1006         return ret == 1 ? 0 : ret;
1007 }
1008
1009 static int cdread_compressed(FILE *f, void *dest, int sector, int offset)
1010 {
1011         unsigned long cdbuffer_size, cdbuffer_size_expect;
1012         unsigned int start_byte, size;
1013         int is_compressed;
1014         int ret, block;
1015
1016         block = sector >> compr_img->block_shift;
1017         compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1);
1018
1019         if (block == compr_img->current_block) {
1020                 //printf("hit sect %d\n", sector);
1021                 goto finish;
1022         }
1023
1024         if (sector >= compr_img->index_len * 16) {
1025                 fprintf(stderr, "sector %d is past img end\n", sector);
1026                 return -1;
1027         }
1028
1029         start_byte = compr_img->index_table[block] & 0x7fffffff;
1030         if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
1031                 fprintf(stderr, "seek error for block %d at %x: ",
1032                         block, start_byte);
1033                 perror(NULL);
1034                 return -1;
1035         }
1036
1037         is_compressed = !(compr_img->index_table[block] & 0x80000000);
1038         size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte;
1039         if (size > sizeof(compr_img->buff_compressed)) {
1040                 fprintf(stderr, "block %d is too large: %u\n", block, size);
1041                 return -1;
1042         }
1043
1044         if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0],
1045                                 1, size, cdHandle) != size) {
1046                 fprintf(stderr, "read error for block %d at %x: ", block, start_byte);
1047                 perror(NULL);
1048                 return -1;
1049         }
1050
1051         if (is_compressed) {
1052                 cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift;
1053                 cdbuffer_size = cdbuffer_size_expect;
1054                 ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
1055                 if (ret != 0) {
1056                         fprintf(stderr, "uncompress failed with %d for block %d, sector %d\n",
1057                                         ret, block, sector);
1058                         return -1;
1059                 }
1060                 if (cdbuffer_size != cdbuffer_size_expect)
1061                         fprintf(stderr, "cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
1062                                         cdbuffer_size_expect, sector);
1063         }
1064
1065         // done at last!
1066         compr_img->current_block = block;
1067
1068 finish:
1069         if (dest != cdbuffer) // copy avoid HACK
1070                 memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk] + offset,
1071                         CD_FRAMESIZE_RAW - offset);
1072         return CD_FRAMESIZE_RAW - offset;
1073 }
1074
1075 static int cdread_2048(FILE *f, void *dest, int sector, int offset)
1076 {
1077         int ret;
1078
1079         fseek(f, sector * 2048, SEEK_SET);
1080         ret = fread((char *)dest + 12, 1, 2048, f);
1081
1082         // not really necessary, fake mode 2 header
1083         memset(cdbuffer, 0, 12);
1084         sec2msf(sector + 2 * 75, (char *)cdbuffer);
1085         cdbuffer[3] = 1;
1086
1087         return ret;
1088 }
1089
1090 static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
1091         return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
1092 }
1093
1094 static unsigned char * CALLBACK ISOgetBuffer(void) {
1095         return cdbuffer;
1096 }
1097
1098 static void PrintTracks(void) {
1099         int i;
1100
1101         for (i = 1; i <= numtracks; i++) {
1102                 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
1103                         i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
1104                         ti[i].start[0], ti[i].start[1], ti[i].start[2],
1105                         ti[i].length[0], ti[i].length[1], ti[i].length[2]);
1106         }
1107 }
1108
1109 // This function is invoked by the front-end when opening an ISO
1110 // file for playback
1111 static long CALLBACK ISOopen(void) {
1112         boolean isMode1ISO = FALSE;
1113
1114         if (cdHandle != NULL) {
1115                 return 0; // it's already open
1116         }
1117
1118         cdHandle = fopen(GetIsoFile(), "rb");
1119         if (cdHandle == NULL) {
1120                 return -1;
1121         }
1122
1123         SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
1124
1125         cddaBigEndian = FALSE;
1126         subChanMixed = FALSE;
1127         subChanRaw = FALSE;
1128         pregapOffset = 0;
1129
1130         CDR_getBuffer = ISOgetBuffer;
1131         cdimg_read_func = cdread_normal;
1132
1133         if (parsecue(GetIsoFile()) == 0) {
1134                 SysPrintf("[+cue]");
1135         }
1136         else if (parsetoc(GetIsoFile()) == 0) {
1137                 SysPrintf("[+toc]");
1138         }
1139         else if (parseccd(GetIsoFile()) == 0) {
1140                 SysPrintf("[+ccd]");
1141         }
1142         else if (parsemds(GetIsoFile()) == 0) {
1143                 SysPrintf("[+mds]");
1144         }
1145         if (handlepbp(GetIsoFile()) == 0) {
1146                 SysPrintf("[pbp]");
1147                 CDR_getBuffer = ISOgetBuffer_compr;
1148                 cdimg_read_func = cdread_compressed;
1149         }
1150         else if (handlecbin(GetIsoFile()) == 0) {
1151                 SysPrintf("[cbin]");
1152                 CDR_getBuffer = ISOgetBuffer_compr;
1153                 cdimg_read_func = cdread_compressed;
1154         }
1155
1156         if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
1157                 SysPrintf("[+sub]");
1158         }
1159         if (opensbifile(GetIsoFile()) == 0) {
1160                 SysPrintf("[+sbi]");
1161         }
1162
1163         // guess whether it is mode1/2048
1164         fseek(cdHandle, 0, SEEK_END);
1165         if (ftell(cdHandle) % 2048 == 0) {
1166                 unsigned int modeTest = 0;
1167                 fseek(cdHandle, 0, SEEK_SET);
1168                 fread(&modeTest, 4, 1, cdHandle);
1169                 if (SWAP32(modeTest) != 0xffffff00) {
1170                         SysPrintf("[2048]");
1171                         isMode1ISO = TRUE;
1172                 }
1173         }
1174         fseek(cdHandle, 0, SEEK_SET);
1175
1176         SysPrintf(".\n");
1177
1178         PrintTracks();
1179
1180         if (subChanMixed)
1181                 cdimg_read_func = cdread_sub_mixed;
1182         else if (isMode1ISO)
1183                 cdimg_read_func = cdread_2048;
1184
1185         // make sure we have another handle open for cdda
1186         if (numtracks > 1 && ti[1].handle == NULL) {
1187                 ti[1].handle = fopen(GetIsoFile(), "rb");
1188         }
1189         cdda_cur_sector = cdda_start_sector = 0;
1190
1191         return 0;
1192 }
1193
1194 static long CALLBACK ISOclose(void) {
1195         int i;
1196
1197         if (cdHandle != NULL) {
1198                 fclose(cdHandle);
1199                 cdHandle = NULL;
1200         }
1201         if (subHandle != NULL) {
1202                 fclose(subHandle);
1203                 subHandle = NULL;
1204         }
1205         stopCDDA();
1206         cddaHandle = NULL;
1207
1208         if (compr_img != NULL) {
1209                 free(compr_img->index_table);
1210                 free(compr_img);
1211                 compr_img = NULL;
1212         }
1213
1214         for (i = 1; i <= numtracks; i++) {
1215                 if (ti[i].handle != NULL) {
1216                         fclose(ti[i].handle);
1217                         ti[i].handle = NULL;
1218                 }
1219         }
1220         numtracks = 0;
1221         UnloadSBI();
1222
1223         return 0;
1224 }
1225
1226 static long CALLBACK ISOinit(void) {
1227         assert(cdHandle == NULL);
1228         assert(subHandle == NULL);
1229
1230         return 0; // do nothing
1231 }
1232
1233 static long CALLBACK ISOshutdown(void) {
1234         ISOclose();
1235         return 0;
1236 }
1237
1238 // return Starting and Ending Track
1239 // buffer:
1240 //  byte 0 - start track
1241 //  byte 1 - end track
1242 static long CALLBACK ISOgetTN(unsigned char *buffer) {
1243         buffer[0] = 1;
1244
1245         if (numtracks > 0) {
1246                 buffer[1] = numtracks;
1247         }
1248         else {
1249                 buffer[1] = 1;
1250         }
1251
1252         return 0;
1253 }
1254
1255 // return Track Time
1256 // buffer:
1257 //  byte 0 - frame
1258 //  byte 1 - second
1259 //  byte 2 - minute
1260 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
1261         if (track == 0) {
1262                 // CD length according pcsxr-svn (done a bit different here)
1263                 unsigned int sect;
1264                 unsigned char time[3];
1265                 sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
1266                 sec2msf(sect, (char *)time);
1267                 buffer[2] = time[0];
1268                 buffer[1] = time[1];
1269                 buffer[0] = time[2];
1270         }
1271         else if (numtracks > 0 && track <= numtracks) {
1272                 buffer[2] = ti[track].start[0];
1273                 buffer[1] = ti[track].start[1];
1274                 buffer[0] = ti[track].start[2];
1275         }
1276         else {
1277                 buffer[2] = 0;
1278                 buffer[1] = 2;
1279                 buffer[0] = 0;
1280         }
1281
1282         return 0;
1283 }
1284
1285 // decode 'raw' subchannel data ripped by cdrdao
1286 static void DecodeRawSubData(void) {
1287         unsigned char subQData[12];
1288         int i;
1289
1290         memset(subQData, 0, sizeof(subQData));
1291
1292         for (i = 0; i < 8 * 12; i++) {
1293                 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
1294                         subQData[i >> 3] |= (1 << (7 - (i & 7)));
1295                 }
1296         }
1297
1298         memcpy(&subbuffer[12], subQData, 12);
1299 }
1300
1301 // read track
1302 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
1303 // uses bcd format
1304 static long CALLBACK ISOreadTrack(unsigned char *time) {
1305         int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
1306
1307         if (cdHandle == NULL) {
1308                 return -1;
1309         }
1310
1311         if (pregapOffset) {
1312                 subChanMissing = FALSE;
1313                 if (sector >= pregapOffset) {
1314                         sector -= 2 * 75;
1315                         if (sector < pregapOffset)
1316                                 subChanMissing = TRUE;
1317                 }
1318         }
1319
1320         cdimg_read_func(cdHandle, cdbuffer, sector, 12);
1321
1322         if (subHandle != NULL) {
1323                 fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
1324                 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
1325
1326                 if (subChanRaw) DecodeRawSubData();
1327         }
1328
1329         return 0;
1330 }
1331
1332 // plays cdda audio
1333 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
1334 // does NOT uses bcd format
1335 static long CALLBACK ISOplay(unsigned char *time) {
1336         unsigned int i, abs_sect;
1337         int file_sect;
1338
1339         if (numtracks <= 1)
1340                 return 0;
1341
1342         // find the track
1343         abs_sect = msf2sec((char *)time);
1344         for (i = numtracks; i > 1; i--)
1345                 if (msf2sec(ti[i].start) <= abs_sect + 2 * 75)
1346                         break;
1347
1348         file_sect = ti[i].start_offset + (abs_sect - msf2sec(ti[i].start));
1349         if (file_sect < 0)
1350                 file_sect = 0;
1351
1352         // find the file that contains this track
1353         for (; i > 1; i--)
1354                 if (ti[i].handle != NULL)
1355                         break;
1356
1357         cdda_start_sector = abs_sect - file_sect;
1358         cddaHandle = ti[i].handle;
1359
1360         if (pregapOffset && (unsigned int)(pregapOffset - file_sect) < 2 * 75) {
1361                 // get out of the missing pregap to avoid noise
1362                 file_sect = pregapOffset;
1363         }
1364
1365         if (SPU_playCDDAchannel != NULL) {
1366                 startCDDA(file_sect);
1367         }
1368         return 0;
1369 }
1370
1371 // stops cdda audio
1372 static long CALLBACK ISOstop(void) {
1373         stopCDDA();
1374         return 0;
1375 }
1376
1377 // gets subchannel data
1378 static unsigned char* CALLBACK ISOgetBufferSub(void) {
1379         if ((subHandle != NULL || subChanMixed) && !subChanMissing) {
1380                 return subbuffer;
1381         }
1382
1383         return NULL;
1384 }
1385
1386 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
1387         int sec;
1388
1389         CDR__getStatus(stat);
1390
1391         if (playing) {
1392                 stat->Type = 0x02;
1393                 stat->Status |= 0x80;
1394         }
1395         else {
1396                 stat->Type = 0x01;
1397         }
1398
1399         sec = cdda_start_sector + cdda_cur_sector;
1400         sec2msf(sec, (char *)stat->Time);
1401
1402         return 0;
1403 }
1404
1405 void cdrIsoInit(void) {
1406         CDR_init = ISOinit;
1407         CDR_shutdown = ISOshutdown;
1408         CDR_open = ISOopen;
1409         CDR_close = ISOclose;
1410         CDR_getTN = ISOgetTN;
1411         CDR_getTD = ISOgetTD;
1412         CDR_readTrack = ISOreadTrack;
1413         CDR_getBuffer = ISOgetBuffer;
1414         CDR_play = ISOplay;
1415         CDR_stop = ISOstop;
1416         CDR_getBufferSub = ISOgetBufferSub;
1417         CDR_getStatus = ISOgetStatus;
1418
1419         CDR_getDriveLetter = CDR__getDriveLetter;
1420         CDR_configure = CDR__configure;
1421         CDR_test = CDR__test;
1422         CDR_about = CDR__about;
1423         CDR_setfilename = CDR__setfilename;
1424
1425         numtracks = 0;
1426 }
1427
1428 int cdrIsoActive(void) {
1429         return (cdHandle != NULL);
1430 }