File: media_detect_cd.c

package info (click to toggle)
retroarch 1.20.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 75,736 kB
  • sloc: ansic: 1,207,646; cpp: 104,166; objc: 8,567; asm: 6,624; python: 3,776; makefile: 2,838; sh: 2,786; xml: 1,408; perl: 393; javascript: 10
file content (550 lines) | stat: -rw-r--r-- 18,208 bytes parent folder | download | duplicates (2)
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
/* Copyright  (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (media_detect_cd.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <media/media_detect_cd.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#include <file/file_path.h>
#include <retro_miscellaneous.h>

/*#define MEDIA_CUE_PARSE_DEBUG*/

static void media_zero_trailing_spaces(char *buf, size_t len)
{
   int i;

   for (i = len - 1; i >= 0; i--)
   {
      if (buf[i] == ' ')
         buf[i] = '\0';
      else if (buf[i] != '\0')
         break;
   }
}

static bool media_skip_spaces(const char **buf, size_t len)
{
   if (buf && *buf && **buf)
   {
      size_t i;
      for (i = 0; i < len; i++)
      {
         if ((*buf)[i] == ' ' || (*buf)[i] == '\t')
            continue;

         *buf += i;
         return true;
      }
   }

   return false;
}

/* 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. */
bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
{
   RFILE *file                          = NULL;
   char *line                           = NULL;
   char track_path[PATH_MAX_LENGTH]     = {0};
   char track_abs_path[PATH_MAX_LENGTH] = {0};
   char track_mode[11]                  = {0};
   bool found_file                      = false;
   bool found_track                     = false;
   unsigned first_data_track            = 0;
   uint64_t data_track_pregap_bytes     = 0;

   if (string_is_empty(path) || !info)
      return false;

   if (!(file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0)))
   {
#ifdef MEDIA_CUE_PARSE_DEBUG
      printf("[MEDIA] Could not open cue path for reading: %s\n", path);
      fflush(stdout);
#endif
      return false;
   }

   while (!filestream_eof(file) && (line = filestream_getline(file)))
   {
      size_t len = 0;
      const char *command = NULL;

      if (string_is_empty(line))
      {
         free(line);
         continue;
      }

      len     = strlen(line);
      command = line;

      media_skip_spaces(&command, len);

      if (!found_file && !strncasecmp(command, "FILE", 4))
      {
         const char *file = command + 4;
         media_skip_spaces(&file, len - 4);

         if (!string_is_empty(file))
         {
            const char *file_end = NULL;
            size_t file_len      = 0;
            bool quoted          = false;

            if (file[0] == '"')
            {
               quoted = true;
               file++;
            }

            if (quoted)
               file_end = strchr(file, '\"');
            else
               file_end = strchr(file, ' ');

            if (file_end)
            {
               file_len = file_end - file;
               memcpy(track_path, file, file_len);
               found_file = true;
#ifdef MEDIA_CUE_PARSE_DEBUG
               printf("Found file: %s\n", track_path);
               fflush(stdout);
#endif
            }
         }
      }
      else if (found_file && !found_track && !strncasecmp(command, "TRACK", 5))
      {
         const char *track = command + 5;
         media_skip_spaces(&track, len - 5);

         if (!string_is_empty(track))
         {
            char *ptr             = NULL;
            unsigned track_number = (unsigned)strtol(track, &ptr, 10);
#ifdef MEDIA_CUE_PARSE_DEBUG
            printf("Found track: %d\n", track_number);
            fflush(stdout);
#endif
            track++;

            if (track[0] && track[0] != ' ' && track[0] != '\t')
               track++;

            if (!string_is_empty(track))
            {
               media_skip_spaces(&track, strlen(track));
#ifdef MEDIA_CUE_PARSE_DEBUG
               printf("Found track type: %s\n", track);
               fflush(stdout);
#endif
               if (!strncasecmp(track, "MODE", 4))
               {
                  first_data_track = track_number;
                  found_track = true;
                  strlcpy(track_mode, track, sizeof(track_mode));
               }
               else
                  found_file = false;
            }
         }
      }
      else if (found_file && found_track && first_data_track && !strncasecmp(command, "INDEX", 5))
      {
         const char *index = command + 5;
         media_skip_spaces(&index, len - 5);

         if (!string_is_empty(index))
         {
            char *ptr             = NULL;
            unsigned index_number = (unsigned)strtol(index, &ptr, 10);

            if (index_number == 1)
            {
               const char *pregap = index + 1;

               if (pregap[0] && pregap[0] != ' ' && pregap[0] != '\t')
                  pregap++;

               if (!string_is_empty(pregap))
               {
                  media_skip_spaces(&pregap, strlen(pregap));
                  found_file  = false;
                  found_track = false;

                  if (first_data_track && !string_is_empty(track_mode))
                  {
                     unsigned track_sector_size = 0;
                     unsigned track_mode_number = 0;

                     if (strlen(track_mode) == 10)
                     {
                        sscanf(track_mode, "MODE%d/%d", (int*)&track_mode_number, (int*)&track_sector_size);
#ifdef MEDIA_CUE_PARSE_DEBUG
                        printf("Found track mode %d with sector size %d\n", track_mode_number, track_sector_size);
                        fflush(stdout);
#endif
                        if ((track_mode_number == 1 || track_mode_number == 2) && track_sector_size)
                        {
                           unsigned min = 0;
                           unsigned sec = 0;
                           unsigned frame = 0;
                           sscanf(pregap, "%02d:%02d:%02d", (int*)&min, (int*)&sec, (int*)&frame);

                           if (min || sec || frame || strstr(pregap, "00:00:00"))
                           {
                              data_track_pregap_bytes = ((min * 60 + sec) * 75 + frame) * track_sector_size;
#ifdef MEDIA_CUE_PARSE_DEBUG
                              printf("Found pregap of %02d:%02d:%02d (bytes: %" PRIu64 ")\n", min, sec, frame, data_track_pregap_bytes);
                              fflush(stdout);
#endif
                              break;
                           }
                        }
                     }
                  }
               }
            }
         }
      }

      free(line);
   }

   filestream_close(file);

   if (!string_is_empty(track_path))
   {
      size_t _len;
      if (strstr(track_path, "/") || strstr(track_path, "\\"))
      {
#ifdef MEDIA_CUE_PARSE_DEBUG
         printf("using path %s\n", track_path);
         fflush(stdout);
#endif
         return media_detect_cd_info(track_path, data_track_pregap_bytes, info);
      }

      _len = fill_pathname_basedir(track_abs_path, path, sizeof(track_abs_path));
      strlcpy(track_abs_path + _len, track_path, sizeof(track_abs_path) - _len);
#ifdef MEDIA_CUE_PARSE_DEBUG
      printf("using abs path %s\n", track_abs_path);
      fflush(stdout);
#endif
      return media_detect_cd_info(track_abs_path, data_track_pregap_bytes, info);
   }

   return true;
}

/* Fill in "info" with detected CD info. Use this when you want to open a specific track file directly, and the pregap is known. */
bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_cd_info_t *info)
{
   RFILE *file;

   if (string_is_empty(path) || !info)
      return false;

   if (!(file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0)))
   {
#ifdef MEDIA_CUE_PARSE_DEBUG
      printf("[MEDIA] Could not open path for reading: %s\n", path);
      fflush(stdout);
#endif
      return false;
   }

   {
      unsigned offset      = 0;
      unsigned sector_size = 0;
      unsigned buf_size    = 17 * 2352;
      char *buf            = (char*)calloc(1, buf_size);
      int64_t read_bytes   = 0;

      if (!buf)
         return false;

      if (pregap_bytes)
         filestream_seek(file, pregap_bytes, RETRO_VFS_SEEK_POSITION_START);

      read_bytes = filestream_read(file, buf, buf_size);

      if (read_bytes != buf_size)
      {
#ifdef MEDIA_CUE_PARSE_DEBUG
         printf("[MEDIA] Could not read from media: got %" PRId64 " bytes instead of %d.\n", read_bytes, buf_size);
         fflush(stdout);
#endif
         filestream_close(file);
         free(buf);
         return false;
      }

      /* 12-byte sync field at the start of every sector, common to both mode1 and mode2 data tracks
       * (when at least sync data is requested). This is a CD-ROM standard feature and not specific to any game devices,
       * and as such should not be part of any system-specific detection or "magic" bytes.
       * Depending on what parts of a sector were requested from the disc, the user data might start at
       * byte offset 0, 4, 8, 12, 16 or 24. Cue sheets only specify the total number of bytes requested from the sectors
       * of a track (like 2048 or 2352) and it is then assumed based on the size/mode as to what fields are present. */
      if (!memcmp(buf, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", 12))
      {
         /* Assume track data contains all fields. */
         sector_size = 2352;

         /* Assume Mode 2 formed (formless is rarely used) */
         if (buf[15] == 2)
            offset   = 24;
         else /* Assume Mode 1 */
            offset   = 16;
      }
      else /* Assume sectors only contain user data instead. */
         sector_size = 2048;

      if (!memcmp(buf + offset, "SEGADISCSYSTEM",
               STRLEN_CONST("SEGADISCSYSTEM")))
      {
         const char *title_pos  = NULL;
         const char *serial_pos = NULL;

         /* All discs currently in Redump for MCD start with SEGADISCSYSTEM. There are other strings mentioned elsewhere online,
          * but I have not seen any real examples of them. */
         info->system_id = MEDIA_CD_SYSTEM_MEGA_CD;

         strcpy_literal(info->system, "Sega CD / Mega CD");

         title_pos = buf + offset + 0x150;

         if (media_skip_spaces(&title_pos, 48))
         {
            memcpy(info->title, title_pos, 48 - (title_pos - (buf + offset + 0x150)));
            media_zero_trailing_spaces(info->title, sizeof(info->title));
         }
         else
         {
            info->title[0] = 'N';
            info->title[1] = '/';
            info->title[2] = 'A';
            info->title[3] = '\0';
         }

         serial_pos = buf + offset + 0x183;

         if (media_skip_spaces(&serial_pos, 8))
         {
            memcpy(info->serial, serial_pos, 8 - (serial_pos - (buf + offset + 0x183)));
            media_zero_trailing_spaces(info->serial, sizeof(info->serial));
         }
         else
         {
            info->serial[0] = 'N';
            info->serial[1] = '/';
            info->serial[2] = 'A';
            info->serial[3] = '\0';
         }
      }
      else if (!memcmp(buf + offset, "SEGA SEGASATURN",
               STRLEN_CONST("SEGA SEGASATURN")))
      {
         const char *title_pos        = NULL;
         const char *serial_pos       = NULL;
         const char *version_pos      = NULL;
         const char *release_date_pos = NULL;

         info->system_id = MEDIA_CD_SYSTEM_SATURN;

         strcpy_literal(info->system, "Sega Saturn");

         title_pos = buf + offset + 0x60;

         if (media_skip_spaces(&title_pos, 112))
         {
            memcpy(info->title, title_pos, 112 - (title_pos - (buf + offset + 0x60)));
            media_zero_trailing_spaces(info->title, sizeof(info->title));
         }
         else
         {
            info->title [0] = 'N';
            info->title [1] = '/';
            info->title [2] = 'A';
            info->title [3] = '\0';
         }

         serial_pos = buf + offset + 0x20;

         if (media_skip_spaces(&serial_pos, 10))
         {
            memcpy(info->serial, serial_pos, 10 - (serial_pos - (buf + offset + 0x20)));
            media_zero_trailing_spaces(info->serial, sizeof(info->serial));
         }
         else
         {
            info->serial[0] = 'N';
            info->serial[1] = '/';
            info->serial[2] = 'A';
            info->serial[3] = '\0';
         }

         version_pos = buf + offset + 0x2a;

         if (media_skip_spaces(&version_pos, 6))
         {
            memcpy(info->version, version_pos, 6 - (version_pos - (buf + offset + 0x2a)));
            media_zero_trailing_spaces(info->version, sizeof(info->version));
         }
         else
         {
            info->version[0] = 'N';
            info->version[1] = '/';
            info->version[2] = 'A';
            info->version[3] = '\0';
         }

         release_date_pos = buf + offset + 0x30;

         if (media_skip_spaces(&release_date_pos, 8))
         {
            memcpy(info->release_date, release_date_pos, 8 - (release_date_pos - (buf + offset + 0x30)));
            media_zero_trailing_spaces(info->release_date, sizeof(info->release_date));
         }
         else
         {
            info->release_date[0] = 'N';
            info->release_date[1] = '/';
            info->release_date[2] = 'A';
            info->release_date[3] = '\0';
         }
      }
      else if (!memcmp(buf + offset, "SEGA SEGAKATANA", STRLEN_CONST("SEGA SEGAKATANA")))
      {
         const char *title_pos        = NULL;
         const char *serial_pos       = NULL;
         const char *version_pos      = NULL;
         const char *release_date_pos = NULL;

         info->system_id = MEDIA_CD_SYSTEM_DREAMCAST;

         strcpy_literal(info->system, "Sega Dreamcast");

         title_pos = buf + offset + 0x80;

         if (media_skip_spaces(&title_pos, 96))
         {
            memcpy(info->title, title_pos, 96 - (title_pos - (buf + offset + 0x80)));
            media_zero_trailing_spaces(info->title, sizeof(info->title));
         }
         else
         {
            info->title       [0] = 'N';
            info->title       [1] = '/';
            info->title       [2] = 'A';
            info->title       [3] = '\0';
         }

         serial_pos = buf + offset + 0x40;

         if (media_skip_spaces(&serial_pos, 10))
         {
            memcpy(info->serial, serial_pos, 10 - (serial_pos - (buf + offset + 0x40)));
            media_zero_trailing_spaces(info->serial, sizeof(info->serial));
         }
         else
         {
            info->serial      [0] = 'N';
            info->serial      [1] = '/';
            info->serial      [2] = 'A';
            info->serial      [3] = '\0';
         }

         version_pos = buf + offset + 0x4a;

         if (media_skip_spaces(&version_pos, 6))
         {
            memcpy(info->version, version_pos, 6 - (version_pos - (buf + offset + 0x4a)));
            media_zero_trailing_spaces(info->version, sizeof(info->version));
         }
         else
         {
            info->version     [0] = 'N';
            info->version     [1] = '/';
            info->version     [2] = 'A';
            info->version     [3] = '\0';
         }

         release_date_pos = buf + offset + 0x50;

         if (media_skip_spaces(&release_date_pos, 8))
         {
            memcpy(info->release_date, release_date_pos, 8 - (release_date_pos - (buf + offset + 0x50)));
            media_zero_trailing_spaces(info->release_date, sizeof(info->release_date));
         }
         else
         {
            info->release_date[0] = 'N';
            info->release_date[1] = '/';
            info->release_date[2] = 'A';
            info->release_date[3] = '\0';
         }
      }
      /* Primary Volume Descriptor fields of ISO9660 */
      else if (!memcmp(buf + offset + (16 * sector_size), "\1CD001\1\0PLAYSTATION", 19))
      {
         const char *title_pos = NULL;

         info->system_id = MEDIA_CD_SYSTEM_PSX;

         strcpy_literal(info->system, "Sony PlayStation");

         title_pos = buf + offset + (16 * sector_size) + 40;

         if (media_skip_spaces(&title_pos, 32))
         {
            memcpy(info->title, title_pos, 32 - (title_pos - (buf + offset + (16 * sector_size) + 40)));
            media_zero_trailing_spaces(info->title, sizeof(info->title));
         }
         else
         {
            info->title       [0] = 'N';
            info->title       [1] = '/';
            info->title       [2] = 'A';
            info->title       [3] = '\0';
         }
      }
      else if (!memcmp(buf + offset, "\x01\x5a\x5a\x5a\x5a\x5a\x01\x00\x00\x00\x00\x00", 12))
      {
         info->system_id = MEDIA_CD_SYSTEM_3DO;
         strcpy_literal(info->system, "3DO");
      }
      else if (!memcmp(buf + offset + 0x950, "PC Engine CD-ROM SYSTEM", 23))
      {
         info->system_id = MEDIA_CD_SYSTEM_PC_ENGINE_CD;
         strcpy_literal(info->system, "TurboGrafx-CD / PC-Engine CD");
      }

      free(buf);
   }

   filestream_close(file);

   return true;
}