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