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