1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
#include <formats/cdfs.h>
#include <retro_miscellaneous.h>
#include <compat/strl.h>
#include <file/file_path.h>
#include <string/stdstring.h>
#ifdef HAVE_CHD
#include <streams/chd_stream.h>
#endif
static void cdfs_determine_sector_size(cdfs_track_t* track)
{
uint8_t buffer[32];
const int toc_sector = 16;
/* MODE information is normally found in the CUE sheet, but we can try to determine it from the raw data.
*
* MODE1/2048 - CDROM Mode1 Data (cooked) [no header, no footer]
* MODE1/2352 - CDROM Mode1 Data (raw) [16 byte header, 288 byte footer]
* MODE2/2336 - CDROM-XA Mode2 Data [8 byte header, 280 byte footer]
* MODE2/2352 - CDROM-XA Mode2 Data [24 byte header, 280 byte footer]
*
* Note that MODE is actually a property on each sector and can change between 1 and 2 depending on how much error
* correction the author desired. To support that, the data format must be "/2352" to include the full header and
* data without error correction information, at which point the CUE sheet information becomes just a hint.
*/
/* The boot record or primary volume descriptor is always at sector 16 and will contain a "CD001" marker */
intfstream_seek(track->stream, toc_sector * 2352 + track->first_sector_offset, SEEK_SET);
if (intfstream_read(track->stream, &buffer, sizeof(buffer)) != (int64_t)sizeof(buffer))
return;
/* if this is a CDROM-XA data source, the "CD001" tag will be 25 bytes into the sector */
if ( buffer[25] == 0x43
&& buffer[26] == 0x44
&& buffer[27] == 0x30
&& buffer[28] == 0x30
&& buffer[29] == 0x31)
{
track->stream_sector_size = 2352;
track->stream_sector_header_size = 24;
}
/* otherwise it should be 17 bytes into the sector */
else if (buffer[17] == 0x43
&& buffer[18] == 0x44
&& buffer[19] == 0x30
&& buffer[20] == 0x30
&& buffer[21] == 0x31)
{
track->stream_sector_size = 2352;
track->stream_sector_header_size = 16;
}
else
{
/* ISO-9660 says the first twelve bytes of a sector should be the sync pattern 00 FF FF FF FF FF FF FF FF FF FF 00 */
if (
buffer[ 0] == 0
&& buffer[ 1] == 0xFF
&& buffer[ 2] == 0xFF
&& buffer[ 3] == 0xFF
&& buffer[ 4] == 0xFF
&& buffer[ 5] == 0xFF
&& buffer[ 6] == 0xFF
&& buffer[ 7] == 0xFF
&& buffer[ 8] == 0xFF
&& buffer[ 9] == 0xFF
&& buffer[10] == 0xFF
&& buffer[11] == 0)
{
/* if we didn't find a CD001 tag, this format may predate ISO-9660 */
/* after the 12 byte sync pattern is three bytes identifying the sector and then one byte for the mode (total 16 bytes) */
track->stream_sector_size = 2352;
track->stream_sector_header_size = 16;
}
}
}
static void cdfs_determine_sector_size_from_file_size(cdfs_track_t* track)
{
/* attempt to determine stream_sector_size from file size */
size_t size = intfstream_get_size(track->stream);
if ((size % 2352) == 0)
{
/* raw tracks use all 2352 bytes and have a 24 byte header */
track->stream_sector_size = 2352;
track->stream_sector_header_size = 24;
}
else if ((size % 2048) == 0)
{
/* cooked tracks eliminate all header/footer data */
track->stream_sector_size = 2048;
track->stream_sector_header_size = 0;
}
else if ((size % 2336) == 0)
{
/* MODE 2 format without 16-byte sync data */
track->stream_sector_size = 2336;
track->stream_sector_header_size = 8;
}
}
static void cdfs_seek_track_sector(cdfs_track_t* track, unsigned int sector)
{
intfstream_seek(track->stream,
sector * track->stream_sector_size
+ track->stream_sector_header_size
+ track->first_sector_offset, SEEK_SET);
}
void cdfs_seek_sector(cdfs_file_t* file, unsigned int sector)
{
/* only allowed if open_file was called with a NULL path */
if (file->first_sector == 0)
{
if (file->current_sector != (int)sector)
{
file->current_sector = (int)sector;
file->sector_buffer_valid = 0;
}
file->pos = sector * 2048;
file->current_sector_offset = 0;
}
}
uint32_t cdfs_get_num_sectors(cdfs_file_t* file)
{
uint32_t frame_size = intfstream_get_frame_size(file->track->stream);
if (frame_size == 0)
{
frame_size = file->track->stream_sector_size;
if (frame_size == 0)
frame_size = 1; /* prevent divide by 0 error if sector size is unknown */
}
return (uint32_t)(intfstream_get_size(file->track->stream) / frame_size);
}
uint32_t cdfs_get_first_sector(cdfs_file_t* file)
{
return file->track->first_sector_index;
}
static int cdfs_find_file(cdfs_file_t* file, const char* path)
{
size_t path_length;
int sector;
uint8_t buffer[2048], *tmp;
const char* slash = strrchr(path, '\\');
if (slash)
{
/* navigate the path to the directory record for the file */
const int dir_length = (int)(slash - path);
memcpy(buffer, path, dir_length);
buffer[dir_length] = '\0';
sector = cdfs_find_file(file, (const char*)buffer);
if (sector < 0)
return sector;
path += dir_length + 1;
}
else
{
int offset;
/* find the CD information (always 16 frames in) */
cdfs_seek_track_sector(file->track, 16);
intfstream_read(file->track->stream, buffer, sizeof(buffer));
/* the directory_record starts at 156 bytes into the sector.
* the sector containing the root directory contents is a
* 3 byte value that is 2 bytes into the directory_record. */
offset = 156 + 2;
sector = buffer[offset] | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 16);
}
/* process the contents of the directory */
cdfs_seek_track_sector(file->track, sector);
intfstream_read(file->track->stream, buffer, sizeof(buffer));
path_length = strlen(path);
tmp = buffer;
while (tmp < buffer + sizeof(buffer))
{
/* The first byte of the record is the length of
* the record - if 0, we reached the end of the data */
if (!*tmp)
break;
/* filename is 33 bytes into the record and
* the format is "FILENAME;version" or "DIRECTORY" */
if ( (tmp[33 + path_length] == ';'
|| (tmp[33 + path_length] == '\0'))
&& strncasecmp((const char*)(tmp + 33), path, path_length) == 0)
{
/* the file size is in bytes 10-13 of the record */
file->size =
(tmp[10])
| (tmp[11] << 8)
| (tmp[12] << 16)
| (tmp[13] << 24);
/* the file contents are in the sector identified
* in bytes 2-4 of the record */
sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16);
return sector;
}
/* the first byte of the record is the length of the record */
tmp += tmp[0];
}
return -1;
}
int cdfs_open_file(cdfs_file_t* file, cdfs_track_t* track, const char* path)
{
if (!file || !track)
return 0;
memset(file, 0, sizeof(*file));
file->track = track;
file->current_sector = -1;
file->first_sector = -1;
if (path)
file->first_sector = cdfs_find_file(file, path);
else if (file->track->stream_sector_size)
{
file->first_sector = 0;
file->size = (unsigned int)((intfstream_get_size(
file->track->stream) / file->track->stream_sector_size)
* 2048);
return 1;
}
return (file->first_sector >= 0);
}
int64_t cdfs_read_file(cdfs_file_t* file, void* buffer, uint64_t len)
{
int bytes_read = 0;
if (!file || file->first_sector < 0 || !buffer)
return 0;
if (len > file->size - file->pos)
len = file->size - file->pos;
if (len == 0)
return 0;
if (file->sector_buffer_valid)
{
size_t remaining = 2048 - file->current_sector_offset;
if (remaining > 0)
{
if (remaining >= len)
{
memcpy(buffer,
&file->sector_buffer[file->current_sector_offset],
(size_t)len);
file->current_sector_offset += len;
return len;
}
memcpy(buffer,
&file->sector_buffer[file->current_sector_offset], remaining);
buffer = (char*)buffer + remaining;
bytes_read += remaining;
len -= remaining;
file->current_sector_offset += remaining;
}
++file->current_sector;
file->current_sector_offset = 0;
file->sector_buffer_valid = 0;
}
else if (file->current_sector < file->first_sector)
{
file->current_sector = file->first_sector;
file->current_sector_offset = 0;
}
while (len >= 2048)
{
cdfs_seek_track_sector(file->track, file->current_sector);
intfstream_read(file->track->stream, buffer, 2048);
buffer = (char*)buffer + 2048;
bytes_read += 2048;
++file->current_sector;
len -= 2048;
}
if (len > 0)
{
cdfs_seek_track_sector(file->track, file->current_sector);
intfstream_read(file->track->stream, file->sector_buffer, 2048);
memcpy(buffer, file->sector_buffer, (size_t)len);
file->current_sector_offset = (unsigned int)len;
file->sector_buffer_valid = 1;
bytes_read += len;
}
file->pos += bytes_read;
return bytes_read;
}
void cdfs_close_file(cdfs_file_t* file)
{
/* Not really anything to do here, just
* clear out the first_sector so
* read() won't do anything */
if (file)
file->first_sector = -1;
}
int64_t cdfs_get_size(cdfs_file_t* file)
{
if (!file || file->first_sector < 0)
return 0;
return file->size;
}
int64_t cdfs_tell(cdfs_file_t* file)
{
if (!file || file->first_sector < 0)
return -1;
return file->pos;
}
int64_t cdfs_seek(cdfs_file_t* file, int64_t offset, int whence)
{
int64_t new_pos;
int new_sector;
if (!file || file->first_sector < 0)
return -1;
switch (whence)
{
case SEEK_SET:
new_pos = offset;
break;
case SEEK_CUR:
new_pos = file->pos + offset;
break;
case SEEK_END:
new_pos = file->size - offset;
break;
default:
return -1;
}
if (new_pos < 0)
return -1;
else if (new_pos > file->size)
return -1;
file->pos = (unsigned int)new_pos;
file->current_sector_offset = file->pos % 2048;
new_sector = file->pos / 2048;
if (new_sector != file->current_sector)
{
file->current_sector = new_sector;
file->sector_buffer_valid = false;
}
return 0;
}
static void cdfs_skip_spaces(const char** ptr)
{
while (**ptr && (**ptr == ' ' || **ptr == '\t'))
++(*ptr);
}
static cdfs_track_t* cdfs_wrap_stream(
intfstream_t* stream,
unsigned first_sector_offset,
unsigned first_sector_index)
{
cdfs_track_t* track = NULL;
if (!stream)
return NULL;
track = (cdfs_track_t*)
calloc(1, sizeof(*track));
track->stream = stream;
track->first_sector_offset = first_sector_offset;
track->first_sector_index = first_sector_index;
cdfs_determine_sector_size(track);
return track;
}
static cdfs_track_t* cdfs_open_cue_track(
const char* path, unsigned int track_index)
{
char* cue = NULL;
const char* line = NULL;
int found_track = 0;
char current_track_path[PATH_MAX_LENGTH] = {0};
char track_path[PATH_MAX_LENGTH] = {0};
unsigned int sector_size = 0;
unsigned int previous_sector_size = 0;
unsigned int previous_index_sector_offset= 0;
unsigned int track_offset = 0;
intfstream_t *cue_stream = intfstream_open_file(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
int64_t stream_size = intfstream_get_size(cue_stream);
char *cue_contents = (char*)malloc((size_t)(stream_size + 1));
cdfs_track_t* track = NULL;
if (!cue_contents)
{
intfstream_close(cue_stream);
return NULL;
}
intfstream_read(cue_stream, cue_contents, stream_size);
intfstream_close(cue_stream);
cue_contents[stream_size] = '\0';
cue = cue_contents;
while (*cue)
{
cdfs_skip_spaces((const char**)&cue);
line = cue;
while (*cue && *cue != '\n')
++cue;
if (*cue)
*cue++ = '\0';
if (!strncasecmp(line, "FILE", 4))
{
const char *file = line + 4;
cdfs_skip_spaces(&file);
if (file[0])
{
const char *file_end = cue - 1;
while (file_end > file && *file_end != ' ' && *file_end != '\t')
--file_end;
if ( file[0] == '"'
&& file_end[-1] == '"')
{
++file;
--file_end;
}
memcpy(current_track_path, file, file_end - file);
current_track_path[file_end - file] = '\0';
}
previous_sector_size = 0;
previous_index_sector_offset = 0;
track_offset = 0;
}
else if (!strncasecmp(line, "TRACK", 5))
{
char *ptr = NULL;
unsigned track_number = 0;
const char *track = line + 5;
cdfs_skip_spaces(&track);
track_number = (unsigned)strtol(track, &ptr, 10);
while (*track && *track != ' ' && *track != '\n')
++track;
previous_sector_size = sector_size;
cdfs_skip_spaces(&track);
if (!strncasecmp(track, "MODE", 4))
{
/* track_index = 0 means find the first data track */
if (!track_index || track_index == track_number)
found_track = track_number;
sector_size = atoi(track + 6);
}
else /* assume AUDIO */
sector_size = 2352;
}
else if (!strncasecmp(line, "INDEX", 5))
{
unsigned sector_offset;
unsigned min = 0, sec = 0, frame = 0;
unsigned index_number = 0;
const char *index = line + 5;
cdfs_skip_spaces(&index);
sscanf(index, "%u", &index_number);
while (*index && *index != ' ' && *index != '\n')
++index;
cdfs_skip_spaces(&index);
sscanf(index, "%u:%u:%u", &min, &sec, &frame);
sector_offset = ((min * 60) + sec) * 75 + frame;
sector_offset -= previous_index_sector_offset;
track_offset += sector_offset * previous_sector_size;
previous_sector_size = sector_size;
previous_index_sector_offset += sector_offset;
if (found_track && index_number == 1)
{
if ( strstr(current_track_path, "/")
|| strstr(current_track_path, "\\"))
strncpy(track_path, current_track_path, sizeof(track_path));
else
{
fill_pathname_basedir(track_path, path, sizeof(track_path));
strlcat(track_path, current_track_path, sizeof(track_path));
}
break;
}
}
}
free(cue_contents);
if (string_is_empty(track_path))
return NULL;
/* NOTE: previous_index_sector_offset will only be valid if all tracks are in the same BIN file.
* Otherwise, we need to determine how many tracks are in each previous BIN file, which is not
* stored in the CUE file. This will affect cdfs_get_first_sector, which luckily isn't used much. */
track = cdfs_wrap_stream(intfstream_open_file(
track_path, RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE), track_offset, previous_index_sector_offset);
if (track && track->stream_sector_size == 0)
{
track->stream_sector_size = sector_size;
if (sector_size == 2352)
track->stream_sector_header_size = 16;
else if (sector_size == 2336)
track->stream_sector_header_size = 8;
}
return track;
}
#ifdef HAVE_CHD
static cdfs_track_t* cdfs_open_chd_track(const char* path, int32_t track_index)
{
cdfs_track_t *track;
intfstream_t *intf_stream = intfstream_open_chd_track(path,
RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE,
track_index);
if (!intf_stream)
return NULL;
track = cdfs_wrap_stream(intf_stream,
intfstream_get_offset_to_start(intf_stream),
intfstream_get_first_sector(intf_stream));
if (track && track->stream_sector_header_size == 0)
{
track->stream_sector_size = intfstream_get_frame_size(intf_stream);
if (track->stream_sector_size == 2352)
track->stream_sector_header_size = 16;
else if (track->stream_sector_size == 2336)
track->stream_sector_header_size = 8;
}
return track;
}
#endif
struct cdfs_track_t* cdfs_open_track(const char* path,
unsigned int track_index)
{
const char* ext = path_get_extension(path);
if (string_is_equal_noncase(ext, "cue"))
return cdfs_open_cue_track(path, track_index);
#ifdef HAVE_CHD
if (string_is_equal_noncase(ext, "chd"))
return cdfs_open_chd_track(path, track_index);
#endif
/* if opening track 1, try opening as a raw track */
if (track_index == 1)
return cdfs_open_raw_track(path);
/* unsupported file type */
return NULL;
}
struct cdfs_track_t* cdfs_open_data_track(const char* path)
{
const char* ext = path_get_extension(path);
if (string_is_equal_noncase(ext, "cue"))
return cdfs_open_cue_track(path, 0);
#ifdef HAVE_CHD
if (string_is_equal_noncase(ext, "chd"))
return cdfs_open_chd_track(path, CHDSTREAM_TRACK_PRIMARY);
#endif
/* unsupported file type - try opening as a raw track */
return cdfs_open_raw_track(path);
}
cdfs_track_t* cdfs_open_raw_track(const char* path)
{
const char *ext = path_get_extension(path);
cdfs_track_t *track = NULL;
if ( string_is_equal_noncase(ext, "bin")
|| string_is_equal_noncase(ext, "iso"))
{
intfstream_t* file = intfstream_open_file(path,
RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
track = cdfs_wrap_stream(file, 0, 0);
if (track && track->stream_sector_size == 0)
{
cdfs_determine_sector_size_from_file_size(track);
if (track->stream_sector_size == 0)
{
cdfs_close_track(track);
track = NULL;
}
}
}
return track;
}
void cdfs_close_track(cdfs_track_t* track)
{
if (track)
{
if (track->stream)
{
intfstream_close(track->stream);
free(track->stream);
}
free(track);
}
}
|