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