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