1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (media_detect_cd.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <media/media_detect_cd.h>
24 #include <streams/file_stream.h>
25 #include <string/stdstring.h>
26 #include <file/file_path.h>
27 #include <retro_miscellaneous.h>
29 /*#define MEDIA_CUE_PARSE_DEBUG*/
31 static void media_zero_trailing_spaces(char *buf, size_t len)
35 for (i = len - 1; i >= 0; i--)
39 else if (buf[i] != '\0')
44 static bool media_skip_spaces(const char **buf, size_t len)
49 if (!buf || !*buf || !**buf)
52 for (i = 0; i < len; i++)
54 if ((*buf)[i] == ' ' || (*buf)[i] == '\t')
68 /* Fill in "info" with detected CD info. Use this when you have a cue file and want it parsed to find the first data track and any pregap info. */
69 bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
73 char track_path[PATH_MAX_LENGTH] = {0};
74 char track_abs_path[PATH_MAX_LENGTH] = {0};
75 char track_mode[11] = {0};
76 bool found_file = false;
77 bool found_track = false;
78 unsigned first_data_track = 0;
79 uint64_t data_track_pregap_bytes = 0;
81 if (string_is_empty(path) || !info)
84 file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0);
88 #ifdef MEDIA_CUE_PARSE_DEBUG
89 printf("[MEDIA] Could not open cue path for reading: %s\n", path);
95 while (!filestream_eof(file) && (line = filestream_getline(file)))
98 const char *command = NULL;
100 if (string_is_empty(line))
109 media_skip_spaces(&command, len);
111 if (!found_file && !strncasecmp(command, "FILE", 4))
113 const char *file = command + 4;
114 media_skip_spaces(&file, len - 4);
116 if (!string_is_empty(file))
118 const char *file_end = NULL;
129 file_end = strchr(file, '\"');
131 file_end = strchr(file, ' ');
135 file_len = file_end - file;
136 memcpy(track_path, file, file_len);
138 #ifdef MEDIA_CUE_PARSE_DEBUG
139 printf("Found file: %s\n", track_path);
145 else if (found_file && !found_track && !strncasecmp(command, "TRACK", 5))
147 const char *track = command + 5;
148 media_skip_spaces(&track, len - 5);
150 if (!string_is_empty(track))
153 unsigned track_number = (unsigned)strtol(track, &ptr, 10);
154 #ifdef MEDIA_CUE_PARSE_DEBUG
155 printf("Found track: %d\n", track_number);
160 if (track[0] && track[0] != ' ' && track[0] != '\t')
163 if (!string_is_empty(track))
165 media_skip_spaces(&track, strlen(track));
166 #ifdef MEDIA_CUE_PARSE_DEBUG
167 printf("Found track type: %s\n", track);
170 if (!strncasecmp(track, "MODE", 4))
172 first_data_track = track_number;
174 strlcpy(track_mode, track, sizeof(track_mode));
181 else if (found_file && found_track && first_data_track && !strncasecmp(command, "INDEX", 5))
183 const char *index = command + 5;
184 media_skip_spaces(&index, len - 5);
186 if (!string_is_empty(index))
189 unsigned index_number = (unsigned)strtol(index, &ptr, 10);
191 if (index_number == 1)
193 const char *pregap = index + 1;
195 if (pregap[0] && pregap[0] != ' ' && pregap[0] != '\t')
198 if (!string_is_empty(pregap))
200 media_skip_spaces(&pregap, strlen(pregap));
204 if (first_data_track && !string_is_empty(track_mode))
206 unsigned track_sector_size = 0;
207 unsigned track_mode_number = 0;
209 if (strlen(track_mode) == 10)
211 sscanf(track_mode, "MODE%d/%d", (int*)&track_mode_number, (int*)&track_sector_size);
212 #ifdef MEDIA_CUE_PARSE_DEBUG
213 printf("Found track mode %d with sector size %d\n", track_mode_number, track_sector_size);
216 if ((track_mode_number == 1 || track_mode_number == 2) && track_sector_size)
221 sscanf(pregap, "%02d:%02d:%02d", (int*)&min, (int*)&sec, (int*)&frame);
223 if (min || sec || frame || strstr(pregap, "00:00:00"))
225 data_track_pregap_bytes = ((min * 60 + sec) * 75 + frame) * track_sector_size;
226 #ifdef MEDIA_CUE_PARSE_DEBUG
227 printf("Found pregap of %02d:%02d:%02d (bytes: %" PRIu64 ")\n", min, sec, frame, data_track_pregap_bytes);
243 filestream_close(file);
245 if (!string_is_empty(track_path))
247 if (strstr(track_path, "/") || strstr(track_path, "\\"))
249 #ifdef MEDIA_CUE_PARSE_DEBUG
250 printf("using path %s\n", track_path);
253 return media_detect_cd_info(track_path, data_track_pregap_bytes, info);
256 fill_pathname_basedir(track_abs_path, path, sizeof(track_abs_path));
257 strlcat(track_abs_path, track_path, sizeof(track_abs_path));
258 #ifdef MEDIA_CUE_PARSE_DEBUG
259 printf("using abs path %s\n", track_abs_path);
262 return media_detect_cd_info(track_abs_path, data_track_pregap_bytes, info);
268 /* Fill in "info" with detected CD info. Use this when you want to open a specific track file directly, and the pregap is known. */
269 bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_cd_info_t *info)
273 if (string_is_empty(path) || !info)
276 file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0);
280 #ifdef MEDIA_CUE_PARSE_DEBUG
281 printf("[MEDIA] Could not open path for reading: %s\n", path);
289 unsigned sector_size = 0;
290 unsigned buf_size = 17 * 2352;
291 char *buf = (char*)calloc(1, buf_size);
292 int64_t read_bytes = 0;
298 filestream_seek(file, pregap_bytes, RETRO_VFS_SEEK_POSITION_START);
300 read_bytes = filestream_read(file, buf, buf_size);
302 if (read_bytes != buf_size)
304 #ifdef MEDIA_CUE_PARSE_DEBUG
305 printf("[MEDIA] Could not read from media: got %" PRId64 " bytes instead of %d.\n", read_bytes, buf_size);
308 filestream_close(file);
313 /* 12-byte sync field at the start of every sector, common to both mode1 and mode2 data tracks
314 * (when at least sync data is requested). This is a CD-ROM standard feature and not specific to any game devices,
315 * and as such should not be part of any system-specific detection or "magic" bytes.
316 * Depending on what parts of a sector were requested from the disc, the user data might start at
317 * byte offset 0, 4, 8, 12, 16 or 24. Cue sheets only specify the total number of bytes requested from the sectors
318 * of a track (like 2048 or 2352) and it is then assumed based on the size/mode as to what fields are present. */
319 if (!memcmp(buf, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", 12))
321 /* Assume track data contains all fields. */
326 /* assume Mode 2 formed (formless is rarely used) */
337 /* Assume sectors only contain user data instead. */
342 if (!memcmp(buf + offset, "SEGADISCSYSTEM",
343 STRLEN_CONST("SEGADISCSYSTEM")))
345 const char *title_pos = NULL;
346 const char *serial_pos = NULL;
348 /* All discs currently in Redump for MCD start with SEGADISCSYSTEM. There are other strings mentioned elsewhere online,
349 * but I have not seen any real examples of them. */
350 info->system_id = MEDIA_CD_SYSTEM_MEGA_CD;
352 strcpy_literal(info->system, "Sega CD / Mega CD");
354 title_pos = buf + offset + 0x150;
356 if (media_skip_spaces(&title_pos, 48))
358 memcpy(info->title, title_pos, 48 - (title_pos - (buf + offset + 0x150)));
359 media_zero_trailing_spaces(info->title, sizeof(info->title));
363 info->title[0] = 'N';
364 info->title[1] = '/';
365 info->title[2] = 'A';
366 info->title[3] = '\0';
369 serial_pos = buf + offset + 0x183;
371 if (media_skip_spaces(&serial_pos, 8))
373 memcpy(info->serial, serial_pos, 8 - (serial_pos - (buf + offset + 0x183)));
374 media_zero_trailing_spaces(info->serial, sizeof(info->serial));
378 info->serial[0] = 'N';
379 info->serial[1] = '/';
380 info->serial[2] = 'A';
381 info->serial[3] = '\0';
384 else if (!memcmp(buf + offset, "SEGA SEGASATURN",
385 STRLEN_CONST("SEGA SEGASATURN")))
387 const char *title_pos = NULL;
388 const char *serial_pos = NULL;
389 const char *version_pos = NULL;
390 const char *release_date_pos = NULL;
392 info->system_id = MEDIA_CD_SYSTEM_SATURN;
394 strcpy_literal(info->system, "Sega Saturn");
396 title_pos = buf + offset + 0x60;
398 if (media_skip_spaces(&title_pos, 112))
400 memcpy(info->title, title_pos, 112 - (title_pos - (buf + offset + 0x60)));
401 media_zero_trailing_spaces(info->title, sizeof(info->title));
405 info->title [0] = 'N';
406 info->title [1] = '/';
407 info->title [2] = 'A';
408 info->title [3] = '\0';
411 serial_pos = buf + offset + 0x20;
413 if (media_skip_spaces(&serial_pos, 10))
415 memcpy(info->serial, serial_pos, 10 - (serial_pos - (buf + offset + 0x20)));
416 media_zero_trailing_spaces(info->serial, sizeof(info->serial));
420 info->serial[0] = 'N';
421 info->serial[1] = '/';
422 info->serial[2] = 'A';
423 info->serial[3] = '\0';
426 version_pos = buf + offset + 0x2a;
428 if (media_skip_spaces(&version_pos, 6))
430 memcpy(info->version, version_pos, 6 - (version_pos - (buf + offset + 0x2a)));
431 media_zero_trailing_spaces(info->version, sizeof(info->version));
435 info->version[0] = 'N';
436 info->version[1] = '/';
437 info->version[2] = 'A';
438 info->version[3] = '\0';
441 release_date_pos = buf + offset + 0x30;
443 if (media_skip_spaces(&release_date_pos, 8))
445 memcpy(info->release_date, release_date_pos, 8 - (release_date_pos - (buf + offset + 0x30)));
446 media_zero_trailing_spaces(info->release_date, sizeof(info->release_date));
450 info->release_date[0] = 'N';
451 info->release_date[1] = '/';
452 info->release_date[2] = 'A';
453 info->release_date[3] = '\0';
456 else if (!memcmp(buf + offset, "SEGA SEGAKATANA", STRLEN_CONST("SEGA SEGAKATANA")))
458 const char *title_pos = NULL;
459 const char *serial_pos = NULL;
460 const char *version_pos = NULL;
461 const char *release_date_pos = NULL;
463 info->system_id = MEDIA_CD_SYSTEM_DREAMCAST;
465 strcpy_literal(info->system, "Sega Dreamcast");
467 title_pos = buf + offset + 0x80;
469 if (media_skip_spaces(&title_pos, 96))
471 memcpy(info->title, title_pos, 96 - (title_pos - (buf + offset + 0x80)));
472 media_zero_trailing_spaces(info->title, sizeof(info->title));
476 info->title [0] = 'N';
477 info->title [1] = '/';
478 info->title [2] = 'A';
479 info->title [3] = '\0';
482 serial_pos = buf + offset + 0x40;
484 if (media_skip_spaces(&serial_pos, 10))
486 memcpy(info->serial, serial_pos, 10 - (serial_pos - (buf + offset + 0x40)));
487 media_zero_trailing_spaces(info->serial, sizeof(info->serial));
491 info->serial [0] = 'N';
492 info->serial [1] = '/';
493 info->serial [2] = 'A';
494 info->serial [3] = '\0';
497 version_pos = buf + offset + 0x4a;
499 if (media_skip_spaces(&version_pos, 6))
501 memcpy(info->version, version_pos, 6 - (version_pos - (buf + offset + 0x4a)));
502 media_zero_trailing_spaces(info->version, sizeof(info->version));
506 info->version [0] = 'N';
507 info->version [1] = '/';
508 info->version [2] = 'A';
509 info->version [3] = '\0';
512 release_date_pos = buf + offset + 0x50;
514 if (media_skip_spaces(&release_date_pos, 8))
516 memcpy(info->release_date, release_date_pos, 8 - (release_date_pos - (buf + offset + 0x50)));
517 media_zero_trailing_spaces(info->release_date, sizeof(info->release_date));
521 info->release_date[0] = 'N';
522 info->release_date[1] = '/';
523 info->release_date[2] = 'A';
524 info->release_date[3] = '\0';
527 /* Primary Volume Descriptor fields of ISO9660 */
528 else if (!memcmp(buf + offset + (16 * sector_size), "\1CD001\1\0PLAYSTATION", 19))
530 const char *title_pos = NULL;
532 info->system_id = MEDIA_CD_SYSTEM_PSX;
534 strcpy_literal(info->system, "Sony PlayStation");
536 title_pos = buf + offset + (16 * sector_size) + 40;
538 if (media_skip_spaces(&title_pos, 32))
540 memcpy(info->title, title_pos, 32 - (title_pos - (buf + offset + (16 * sector_size) + 40)));
541 media_zero_trailing_spaces(info->title, sizeof(info->title));
545 info->title [0] = 'N';
546 info->title [1] = '/';
547 info->title [2] = 'A';
548 info->title [3] = '\0';
551 else if (!memcmp(buf + offset, "\x01\x5a\x5a\x5a\x5a\x5a\x01\x00\x00\x00\x00\x00", 12))
553 info->system_id = MEDIA_CD_SYSTEM_3DO;
555 strcpy_literal(info->system, "3DO");
557 else if (!memcmp(buf + offset + 0x950, "PC Engine CD-ROM SYSTEM", 23))
559 info->system_id = MEDIA_CD_SYSTEM_PC_ENGINE_CD;
561 strcpy_literal(info->system, "TurboGrafx-CD / PC-Engine CD");
567 filestream_close(file);