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