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