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