1 #include <formats/cdfs.h>
3 #include <retro_miscellaneous.h>
4 #include <compat/strl.h>
5 #include <file/file_path.h>
6 #include <string/stdstring.h>
9 #include <streams/chd_stream.h>
12 static void cdfs_determine_sector_size(cdfs_track_t* track)
15 const int toc_sector = 16;
17 /* MODE information is normally found in the CUE sheet, but we can try to determine it from the raw data.
19 * MODE1/2048 - CDROM Mode1 Data (cooked) [no header, no footer]
20 * MODE1/2352 - CDROM Mode1 Data (raw) [16 byte header, 288 byte footer]
21 * MODE2/2336 - CDROM-XA Mode2 Data [8 byte header, 280 byte footer]
22 * MODE2/2352 - CDROM-XA Mode2 Data [24 byte header, 280 byte footer]
24 * Note that MODE is actually a property on each sector and can change between 1 and 2 depending on how much error
25 * correction the author desired. To support that, the data format must be "/2352" to include the full header and
26 * data without error correction information, at which point the CUE sheet information becomes just a hint.
29 /* The boot record or primary volume descriptor is always at sector 16 and will contain a "CD001" marker */
30 intfstream_seek(track->stream, toc_sector * 2352 + track->first_sector_offset, SEEK_SET);
31 if (intfstream_read(track->stream, &buffer, sizeof(buffer)) != (int64_t)sizeof(buffer))
34 /* if this is a CDROM-XA data source, the "CD001" tag will be 25 bytes into the sector */
35 if ( buffer[25] == 0x43
39 && buffer[29] == 0x31)
41 track->stream_sector_size = 2352;
42 track->stream_sector_header_size = 24;
44 /* otherwise it should be 17 bytes into the sector */
45 else if (buffer[17] == 0x43
49 && buffer[21] == 0x31)
51 track->stream_sector_size = 2352;
52 track->stream_sector_header_size = 16;
56 /* 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 */
71 /* if we didn't find a CD001 tag, this format may predate ISO-9660 */
73 /* after the 12 byte sync pattern is three bytes identifying the sector and then one byte for the mode (total 16 bytes) */
74 track->stream_sector_size = 2352;
75 track->stream_sector_header_size = 16;
80 static void cdfs_determine_sector_size_from_file_size(cdfs_track_t* track)
82 /* attempt to determine stream_sector_size from file size */
83 size_t size = intfstream_get_size(track->stream);
85 if ((size % 2352) == 0)
87 /* raw tracks use all 2352 bytes and have a 24 byte header */
88 track->stream_sector_size = 2352;
89 track->stream_sector_header_size = 24;
91 else if ((size % 2048) == 0)
93 /* cooked tracks eliminate all header/footer data */
94 track->stream_sector_size = 2048;
95 track->stream_sector_header_size = 0;
97 else if ((size % 2336) == 0)
99 /* MODE 2 format without 16-byte sync data */
100 track->stream_sector_size = 2336;
101 track->stream_sector_header_size = 8;
105 static void cdfs_seek_track_sector(cdfs_track_t* track, unsigned int sector)
107 intfstream_seek(track->stream,
108 sector * track->stream_sector_size
109 + track->stream_sector_header_size
110 + track->first_sector_offset, SEEK_SET);
113 void cdfs_seek_sector(cdfs_file_t* file, unsigned int sector)
115 /* only allowed if open_file was called with a NULL path */
116 if (file->first_sector == 0)
118 if (file->current_sector != (int)sector)
120 file->current_sector = (int)sector;
121 file->sector_buffer_valid = 0;
124 file->pos = sector * 2048;
125 file->current_sector_offset = 0;
129 uint32_t cdfs_get_num_sectors(cdfs_file_t* file)
131 uint32_t frame_size = intfstream_get_frame_size(file->track->stream);
134 frame_size = file->track->stream_sector_size;
136 frame_size = 1; /* prevent divide by 0 error if sector size is unknown */
138 return (uint32_t)(intfstream_get_size(file->track->stream) / frame_size);
141 uint32_t cdfs_get_first_sector(cdfs_file_t* file)
143 return file->track->first_sector_index;
146 static int cdfs_find_file(cdfs_file_t* file, const char* path)
150 uint8_t buffer[2048], *tmp;
151 const char* slash = strrchr(path, '\\');
155 /* navigate the path to the directory record for the file */
156 const int dir_length = (int)(slash - path);
157 memcpy(buffer, path, dir_length);
158 buffer[dir_length] = '\0';
160 sector = cdfs_find_file(file, (const char*)buffer);
164 path += dir_length + 1;
170 /* find the CD information (always 16 frames in) */
171 cdfs_seek_track_sector(file->track, 16);
172 intfstream_read(file->track->stream, buffer, sizeof(buffer));
174 /* the directory_record starts at 156 bytes into the sector.
175 * the sector containing the root directory contents is a
176 * 3 byte value that is 2 bytes into the directory_record. */
178 sector = buffer[offset] | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 16);
181 /* process the contents of the directory */
182 cdfs_seek_track_sector(file->track, sector);
183 intfstream_read(file->track->stream, buffer, sizeof(buffer));
185 path_length = strlen(path);
188 while (tmp < buffer + sizeof(buffer))
190 /* The first byte of the record is the length of
191 * the record - if 0, we reached the end of the data */
195 /* filename is 33 bytes into the record and
196 * the format is "FILENAME;version" or "DIRECTORY" */
197 if ( (tmp[33 + path_length] == ';'
198 || (tmp[33 + path_length] == '\0'))
199 && strncasecmp((const char*)(tmp + 33), path, path_length) == 0)
201 /* the file size is in bytes 10-13 of the record */
208 /* the file contents are in the sector identified
209 * in bytes 2-4 of the record */
210 sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16);
214 /* the first byte of the record is the length of the record */
221 int cdfs_open_file(cdfs_file_t* file, cdfs_track_t* track, const char* path)
226 memset(file, 0, sizeof(*file));
229 file->current_sector = -1;
230 file->first_sector = -1;
233 file->first_sector = cdfs_find_file(file, path);
234 else if (file->track->stream_sector_size)
236 file->first_sector = 0;
237 file->size = (unsigned int)((intfstream_get_size(
238 file->track->stream) / file->track->stream_sector_size)
243 return (file->first_sector >= 0);
246 int64_t cdfs_read_file(cdfs_file_t* file, void* buffer, uint64_t len)
250 if (!file || file->first_sector < 0 || !buffer)
253 if (len > file->size - file->pos)
254 len = file->size - file->pos;
259 if (file->sector_buffer_valid)
261 size_t remaining = 2048 - file->current_sector_offset;
264 if (remaining >= len)
267 &file->sector_buffer[file->current_sector_offset],
269 file->current_sector_offset += len;
274 &file->sector_buffer[file->current_sector_offset], remaining);
275 buffer = (char*)buffer + remaining;
276 bytes_read += remaining;
279 file->current_sector_offset += remaining;
282 ++file->current_sector;
283 file->current_sector_offset = 0;
284 file->sector_buffer_valid = 0;
286 else if (file->current_sector < file->first_sector)
288 file->current_sector = file->first_sector;
289 file->current_sector_offset = 0;
294 cdfs_seek_track_sector(file->track, file->current_sector);
295 intfstream_read(file->track->stream, buffer, 2048);
297 buffer = (char*)buffer + 2048;
300 ++file->current_sector;
307 cdfs_seek_track_sector(file->track, file->current_sector);
308 intfstream_read(file->track->stream, file->sector_buffer, 2048);
309 memcpy(buffer, file->sector_buffer, (size_t)len);
310 file->current_sector_offset = (unsigned int)len;
311 file->sector_buffer_valid = 1;
316 file->pos += bytes_read;
320 void cdfs_close_file(cdfs_file_t* file)
322 /* Not really anything to do here, just
323 * clear out the first_sector so
324 * read() won't do anything */
326 file->first_sector = -1;
329 int64_t cdfs_get_size(cdfs_file_t* file)
331 if (!file || file->first_sector < 0)
336 int64_t cdfs_tell(cdfs_file_t* file)
338 if (!file || file->first_sector < 0)
343 int64_t cdfs_seek(cdfs_file_t* file, int64_t offset, int whence)
348 if (!file || file->first_sector < 0)
358 new_pos = file->pos + offset;
362 new_pos = file->size - offset;
371 else if (new_pos > file->size)
374 file->pos = (unsigned int)new_pos;
375 file->current_sector_offset = file->pos % 2048;
377 new_sector = file->pos / 2048;
378 if (new_sector != file->current_sector)
380 file->current_sector = new_sector;
381 file->sector_buffer_valid = false;
387 static void cdfs_skip_spaces(const char** ptr)
389 while (**ptr && (**ptr == ' ' || **ptr == '\t'))
393 static cdfs_track_t* cdfs_wrap_stream(
394 intfstream_t* stream,
395 unsigned first_sector_offset,
396 unsigned first_sector_index)
398 cdfs_track_t* track = NULL;
403 track = (cdfs_track_t*)
404 calloc(1, sizeof(*track));
405 track->stream = stream;
406 track->first_sector_offset = first_sector_offset;
407 track->first_sector_index = first_sector_index;
409 cdfs_determine_sector_size(track);
414 static cdfs_track_t* cdfs_open_cue_track(
415 const char* path, unsigned int track_index)
418 const char* line = NULL;
420 char current_track_path[PATH_MAX_LENGTH] = {0};
421 char track_path[PATH_MAX_LENGTH] = {0};
422 unsigned int sector_size = 0;
423 unsigned int previous_sector_size = 0;
424 unsigned int previous_index_sector_offset= 0;
425 unsigned int track_offset = 0;
426 intfstream_t *cue_stream = intfstream_open_file(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
427 int64_t stream_size = intfstream_get_size(cue_stream);
428 char *cue_contents = (char*)malloc((size_t)(stream_size + 1));
429 cdfs_track_t* track = NULL;
433 intfstream_close(cue_stream);
437 intfstream_read(cue_stream, cue_contents, stream_size);
438 intfstream_close(cue_stream);
440 cue_contents[stream_size] = '\0';
445 cdfs_skip_spaces((const char**)&cue);
448 while (*cue && *cue != '\n')
453 if (!strncasecmp(line, "FILE", 4))
455 const char *file = line + 4;
456 cdfs_skip_spaces(&file);
460 const char *file_end = cue - 1;
461 while (file_end > file && *file_end != ' ' && *file_end != '\t')
465 && file_end[-1] == '"')
471 memcpy(current_track_path, file, file_end - file);
472 current_track_path[file_end - file] = '\0';
475 previous_sector_size = 0;
476 previous_index_sector_offset = 0;
479 else if (!strncasecmp(line, "TRACK", 5))
482 unsigned track_number = 0;
483 const char *track = line + 5;
484 cdfs_skip_spaces(&track);
486 track_number = (unsigned)strtol(track, &ptr, 10);
487 while (*track && *track != ' ' && *track != '\n')
490 previous_sector_size = sector_size;
492 cdfs_skip_spaces(&track);
494 if (!strncasecmp(track, "MODE", 4))
496 /* track_index = 0 means find the first data track */
497 if (!track_index || track_index == track_number)
498 found_track = track_number;
500 sector_size = atoi(track + 6);
502 else /* assume AUDIO */
505 else if (!strncasecmp(line, "INDEX", 5))
507 unsigned sector_offset;
508 unsigned min = 0, sec = 0, frame = 0;
509 unsigned index_number = 0;
510 const char *index = line + 5;
512 cdfs_skip_spaces(&index);
513 sscanf(index, "%u", &index_number);
514 while (*index && *index != ' ' && *index != '\n')
516 cdfs_skip_spaces(&index);
518 sscanf(index, "%u:%u:%u", &min, &sec, &frame);
519 sector_offset = ((min * 60) + sec) * 75 + frame;
520 sector_offset -= previous_index_sector_offset;
521 track_offset += sector_offset * previous_sector_size;
522 previous_sector_size = sector_size;
523 previous_index_sector_offset += sector_offset;
525 if (found_track && index_number == 1)
527 if ( strstr(current_track_path, "/")
528 || strstr(current_track_path, "\\"))
529 strncpy(track_path, current_track_path, sizeof(track_path));
532 fill_pathname_basedir(track_path, path, sizeof(track_path));
533 strlcat(track_path, current_track_path, sizeof(track_path));
543 if (string_is_empty(track_path))
546 /* NOTE: previous_index_sector_offset will only be valid if all tracks are in the same BIN file.
547 * Otherwise, we need to determine how many tracks are in each previous BIN file, which is not
548 * stored in the CUE file. This will affect cdfs_get_first_sector, which luckily isn't used much. */
549 track = cdfs_wrap_stream(intfstream_open_file(
550 track_path, RETRO_VFS_FILE_ACCESS_READ,
551 RETRO_VFS_FILE_ACCESS_HINT_NONE), track_offset, previous_index_sector_offset);
553 if (track && track->stream_sector_size == 0)
555 track->stream_sector_size = sector_size;
557 if (sector_size == 2352)
558 track->stream_sector_header_size = 16;
559 else if (sector_size == 2336)
560 track->stream_sector_header_size = 8;
567 static cdfs_track_t* cdfs_open_chd_track(const char* path, int32_t track_index)
570 intfstream_t *intf_stream = intfstream_open_chd_track(path,
571 RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE,
576 track = cdfs_wrap_stream(intf_stream,
577 intfstream_get_offset_to_start(intf_stream),
578 intfstream_get_first_sector(intf_stream));
580 if (track && track->stream_sector_header_size == 0)
582 track->stream_sector_size = intfstream_get_frame_size(intf_stream);
584 if (track->stream_sector_size == 2352)
585 track->stream_sector_header_size = 16;
586 else if (track->stream_sector_size == 2336)
587 track->stream_sector_header_size = 8;
594 struct cdfs_track_t* cdfs_open_track(const char* path,
595 unsigned int track_index)
597 const char* ext = path_get_extension(path);
599 if (string_is_equal_noncase(ext, "cue"))
600 return cdfs_open_cue_track(path, track_index);
603 if (string_is_equal_noncase(ext, "chd"))
604 return cdfs_open_chd_track(path, track_index);
607 /* if opening track 1, try opening as a raw track */
608 if (track_index == 1)
609 return cdfs_open_raw_track(path);
611 /* unsupported file type */
615 struct cdfs_track_t* cdfs_open_data_track(const char* path)
617 const char* ext = path_get_extension(path);
619 if (string_is_equal_noncase(ext, "cue"))
620 return cdfs_open_cue_track(path, 0);
623 if (string_is_equal_noncase(ext, "chd"))
624 return cdfs_open_chd_track(path, CHDSTREAM_TRACK_PRIMARY);
627 /* unsupported file type - try opening as a raw track */
628 return cdfs_open_raw_track(path);
631 cdfs_track_t* cdfs_open_raw_track(const char* path)
633 const char *ext = path_get_extension(path);
634 cdfs_track_t *track = NULL;
636 if ( string_is_equal_noncase(ext, "bin")
637 || string_is_equal_noncase(ext, "iso"))
639 intfstream_t* file = intfstream_open_file(path,
640 RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
642 track = cdfs_wrap_stream(file, 0, 0);
643 if (track && track->stream_sector_size == 0)
645 cdfs_determine_sector_size_from_file_size(track);
646 if (track->stream_sector_size == 0)
648 cdfs_close_track(track);
657 void cdfs_close_track(cdfs_track_t* track)
663 intfstream_close(track->stream);