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