Previous 15994 Revisions Next

r12670 Saturday 20th August, 2011 at 06:49:00 UTC by Miodrag Milanović
Added support for loading other image types to cdrom device, with use of chdman part of code (no whatsnew)
[src/emu/imagedev]chd_cd.c
[src/lib]lib.mak
[src/lib/util]cdrom.c cdrom.h chdcd.c* chdcd.h*
[src/tools]chdcd.c chdcd.h tools.mak

src/tools/chdcd.c
r12669r12670
1/***************************************************************************
2
3    TOC parser for CHD compression frontend
4    Handles CDRDAO .toc, CDRWIN .cue, and Sega GDROM .gdi
5
6    Copyright Nicola Salmoria and the MAME Team.
7    Visit http://mamedev.org for licensing and usage restrictions.
8
9***************************************************************************/
10
11#include <ctype.h>
12#include <stdlib.h>
13#include "osdcore.h"
14#include "chd.h"
15#include "chdcd.h"
16#include "corefile.h"
17
18
19
20/***************************************************************************
21    CONSTANTS & DEFINES
22***************************************************************************/
23
24#define TOKENIZE i = tokenize( linebuffer, i, sizeof(linebuffer), token, sizeof(token) );
25
26
27
28/***************************************************************************
29    GLOBAL VARIABLES
30***************************************************************************/
31
32static char linebuffer[512];
33
34
35
36/***************************************************************************
37    IMPLEMENTATION
38***************************************************************************/
39
40static astring get_file_path(astring &path)
41{   
42   int pos = path.rchr( 0, '\\');
43   if (pos!=-1) {
44      path = path.substr(0,pos+1);
45   } else {
46      pos = path.rchr( 0, '/');
47      path = path.substr(0,pos+1);
48   }
49   return path;
50}
51/*-------------------------------------------------
52    get_file_size - get the size of a file
53-------------------------------------------------*/
54
55static UINT64 get_file_size(const char *filename)
56{
57   osd_file *file;
58   UINT64 filesize = 0;
59   file_error filerr;
60
61   filerr = osd_open(filename, OPEN_FLAG_READ, &file, &filesize);
62   if (filerr == FILERR_NONE)
63      osd_close(file);
64
65   return filesize;
66}
67
68
69/*-------------------------------------------------
70    tokenize - get a token from the line buffer
71-------------------------------------------------*/
72
73static int tokenize( const char *linebuffer, int i, int linebuffersize, char *token, int tokensize )
74{
75   int j = 0;
76   int singlequote = 0;
77   int doublequote = 0;
78
79   while ((i < linebuffersize) && isspace((UINT8)linebuffer[i]))
80   {
81      i++;
82   }
83
84   while ((i < linebuffersize) && (j < tokensize))
85   {
86      if (!singlequote && linebuffer[i] == '"' )
87      {
88         doublequote = !doublequote;
89      }
90      else if (!doublequote && linebuffer[i] == '\'')
91      {
92         singlequote = !singlequote;
93      }
94      else if (!singlequote && !doublequote && isspace((UINT8)linebuffer[i]))
95      {
96         break;
97      }
98      else
99      {
100         token[j] = linebuffer[i];
101         j++;
102      }
103
104      i++;
105   }
106
107   token[j] = '\0';
108
109   return i;
110}
111
112
113/*-------------------------------------------------
114    msf_to_frames - convert m:s:f into a number of frames
115-------------------------------------------------*/
116
117static int msf_to_frames( char *token )
118{
119   int m = 0;
120   int s = 0;
121   int f = 0;
122
123   if( sscanf( token, "%d:%d:%d", &m, &s, &f ) == 1 )
124   {
125      f = m;
126   }
127   else
128   {
129      /* convert to just frames */
130      s += (m * 60);
131      f += (s * 75);
132   }
133
134   return f;
135}
136
137/*-------------------------------------------------
138    parse_wav_sample - takes a .WAV file, verifies
139    that the file is 16 bits, and returns the
140    length in bytes of the data and the offset in
141    bytes to where the data starts in the file.
142-------------------------------------------------*/
143static UINT32 parse_wav_sample(char *filename, UINT32 *dataoffs)
144{
145   unsigned long offset = 0;
146   UINT32 length, rate, filesize;
147   UINT16 bits, temp16;
148   char buf[32];
149   osd_file *file;
150   file_error filerr;
151   UINT64 fsize = 0;
152   UINT32 actual;
153
154   filerr = osd_open(filename, OPEN_FLAG_READ, &file, &fsize);
155   if (filerr != FILERR_NONE)
156   {
157      osd_close(file);
158      return 0;
159   }
160
161   /* read the core header and make sure it's a WAVE file */
162   osd_read(file, buf, 0, 4, &actual);
163   offset += actual;
164   if (offset < 4)
165      return 0;
166   if (memcmp(&buf[0], "RIFF", 4) != 0)
167      return 0;
168
169   /* get the total size */
170   osd_read(file, &filesize, offset, 4, &actual);
171   offset += actual;
172   if (offset < 8)
173      return 0;
174   filesize = LITTLE_ENDIANIZE_INT32(filesize);
175
176   /* read the RIFF file type and make sure it's a WAVE file */
177   osd_read(file, buf, offset, 4, &actual);
178   offset += actual;
179   if (offset < 12)
180      return 0;
181   if (memcmp(&buf[0], "WAVE", 4) != 0)
182      return 0;
183
184   /* seek until we find a format tag */
185   while (1)
186   {
187      osd_read(file, buf, offset, 4, &actual);
188      offset += actual;
189      osd_read(file, &length, offset, 4, &actual);
190      offset += actual;
191      length = LITTLE_ENDIANIZE_INT32(length);
192      if (memcmp(&buf[0], "fmt ", 4) == 0)
193         break;
194
195      /* seek to the next block */
196      offset += length;
197      if (offset >= filesize)
198         return 0;
199   }
200
201   /* read the format -- make sure it is PCM */
202   osd_read(file, &temp16, offset, 2, &actual);
203   offset += actual;
204   temp16 = LITTLE_ENDIANIZE_INT16(temp16);
205   if (temp16 != 1)
206      return 0;
207
208   /* number of channels -- only mono is supported */
209   osd_read(file, &temp16, offset, 2, &actual);
210   offset += actual;
211   temp16 = LITTLE_ENDIANIZE_INT16(temp16);
212   if (temp16 != 2)
213      return 0;
214
215   /* sample rate */
216   osd_read(file, &rate, offset, 4, &actual);
217   offset += actual;
218   rate = LITTLE_ENDIANIZE_INT32(rate);
219   if (rate != 44100)
220      return 0;
221
222   /* bytes/second and block alignment are ignored */
223   osd_read(file, buf, offset, 6, &actual);
224   offset += actual;
225
226   /* bits/sample */
227   osd_read(file, &bits, offset, 2, &actual);
228   offset += actual;
229   if (bits != 16)
230      return 0;
231
232   /* seek past any extra data */
233   offset += length - 16;
234
235   /* seek until we find a data tag */
236   while (1)
237   {
238      osd_read(file, buf, offset, 4, &actual);
239      offset += actual;
240      osd_read(file, &length, offset, 4, &actual);
241      offset += actual;
242      length = LITTLE_ENDIANIZE_INT32(length);
243      if (memcmp(&buf[0], "data", 4) == 0)
244         break;
245
246      /* seek to the next block */
247      offset += length;
248      if (offset >= filesize)
249         return 0;
250   }
251
252   /* if there was a 0 length data block, we're done */
253   if (length == 0)
254      return 0;
255
256   osd_close(file);
257
258   *dataoffs = offset;
259
260   return length;
261}
262
263UINT16 read_uint16(FILE *infile)
264{
265   UINT16 res = 0;
266   unsigned char buffer[2];
267
268   fread(buffer, 2, 1, infile);
269
270   res = buffer[1] | buffer[0]<<8;
271
272   return res;
273}
274
275UINT32 read_uint32(FILE *infile)
276{
277   UINT32 res = 0;
278   unsigned char buffer[4];
279
280   fread(buffer, 4, 1, infile);
281
282   res = buffer[3] | buffer[2]<<8 | buffer[1]<<16 | buffer[0]<<24;
283
284   return res;
285}
286
287UINT64 read_uint64(FILE *infile)
288{
289   UINT64 res0 = U64(0), res1 = U64(0);
290   UINT64 res;
291   unsigned char buffer[8];
292
293   fread(buffer, 8, 1, infile);
294
295   res0 = buffer[3] | buffer[2]<<8 | buffer[1]<<16 | buffer[0]<<24;
296   res1 = buffer[7] | buffer[6]<<8 | buffer[5]<<16 | buffer[4]<<24;
297
298   res = res0<<32 | res1;
299
300   return res;
301}
302
303/*-------------------------------------------------
304    chdcd_parse_toc - parse a CDRWin format CUE file
305-------------------------------------------------*/
306
307chd_error chdcd_parse_nero(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
308{
309   FILE *infile;
310   unsigned char buffer[12];
311   UINT32 chain_offs, chunk_size;
312   int done = 0;
313
314   astring path = astring(tocfname);
315
316   infile = fopen(tocfname, "rt");   
317   path = get_file_path(path);
318
319   if (infile == (FILE *)NULL)
320   {
321      return CHDERR_FILE_NOT_FOUND;
322   }
323
324   /* clear structures */
325   memset(outtoc, 0, sizeof(cdrom_toc));
326   memset(outinfo, 0, sizeof(chdcd_track_input_info));
327
328   // seek to 12 bytes before the end
329   fseek(infile, -12, SEEK_END);
330   fread(buffer, 12, 1, infile);
331
332   if (memcmp(buffer, "NER5", 4))
333   {
334      printf("ERROR: Not a Nero 5.5 or later image!\n");
335      fclose(infile);
336      return CHDERR_FILE_NOT_FOUND;
337   }
338
339   chain_offs = buffer[11] | (buffer[10]<<8) | (buffer[9]<<16) | (buffer[8]<<24);
340
341   if ((buffer[7] != 0) || (buffer[6] != 0) || (buffer[5] != 0) || (buffer[4] != 0))
342   {
343      printf("ERROR: File size is > 4GB, this version of CHDMAN cannot handle it.");
344      fclose(infile);
345      return CHDERR_FILE_NOT_FOUND;
346   }
347
348//  printf("NER5 detected, chain offset: %x\n", chain_offs);
349
350   while (!done)
351   {
352      UINT32 offset;
353      UINT8 start, end;
354      int track;
355
356      fseek(infile, chain_offs, SEEK_SET);
357      fread(buffer, 8, 1, infile);
358
359      chunk_size = (buffer[7] | buffer[6]<<8 | buffer[5]<<16 | buffer[4]<<24);
360
361//      printf("Chunk type: %c%c%c%c, size %x\n", buffer[0], buffer[1], buffer[2], buffer[3], chunk_size);
362
363      // we want the DAOX chunk, which has the TOC information
364      if (!memcmp(buffer, "DAOX", 4))
365      {
366         // skip second chunk size and UPC code
367         fseek(infile, 16, SEEK_CUR);
368
369         read_uint32(infile);
370         fread(&start, 1, 1, infile);
371         fread(&end, 1, 1, infile);
372
373//          printf("TOC type: %08x.  Start track %d  End track: %d\n", toc_type, start, end);
374
375         outtoc->numtrks = (end-start) + 1;
376
377         offset = 0;
378         for (track = start; track <= end; track++)
379         {
380            UINT32 size, mode;
381            UINT64 index0, index1, index2;
382
383            fseek(infile, 12, SEEK_CUR);   // skip ISRC code
384            size = read_uint16(infile);
385            mode = read_uint32(infile);
386            index0 = read_uint64(infile);
387            index1 = read_uint64(infile);
388            index2 = read_uint64(infile);
389
390//              printf("Track %d: sector size %d mode %x index0 %llx index1 %llx index2 %llx (pregap %d sectors, length %d sectors)\n", track, size, mode, index0, index1, index2, (UINT32)(index1-index0)/size, (UINT32)(index2-index1)/size);
391            strncpy(outinfo->fname[track-1], path.cstr(), 256);
392            strncat(outinfo->fname[track-1], tocfname, 256);
393            outinfo->offset[track-1] = offset + (UINT32)(index1-index0);
394            outinfo->idx0offs[track-1] = 0;
395            outinfo->idx1offs[track-1] = 0;
396
397            switch (mode>>24)
398            {
399               case 0x00:   // 2048 byte data
400                  outtoc->tracks[track-1].trktype = CD_TRACK_MODE1;
401                  outinfo->swap[track-1] = 0;
402                  break;
403
404               case 0x06:   // 2352 byte mode 2 raw
405                  outtoc->tracks[track-1].trktype = CD_TRACK_MODE2_RAW;
406                  outinfo->swap[track-1] = 0;
407                  break;
408
409               case 0x07:   // 2352 byte audio
410                  outtoc->tracks[track-1].trktype = CD_TRACK_AUDIO;
411                  outinfo->swap[track-1] = 1;
412                  break;
413
414               default:
415                  printf("ERROR: Unknown track type %x, contact MAMEDEV!\n", mode>>24);
416                  break;
417            }
418
419            outtoc->tracks[track-1].datasize = size;
420
421            outtoc->tracks[track-1].subtype = CD_SUB_NONE;
422            outtoc->tracks[track-1].subsize = 0;
423
424            outtoc->tracks[track-1].pregap = (UINT32)(index1-index0)/size;
425            outtoc->tracks[track-1].frames = (UINT32)(index2-index1)/size;
426            outtoc->tracks[track-1].postgap = 0;
427            outtoc->tracks[track-1].pgtype = 0;
428            outtoc->tracks[track-1].pgsub = CD_SUB_NONE;
429            outtoc->tracks[track-1].pgdatasize = 0;
430            outtoc->tracks[track-1].pgsubsize = 0;
431
432            offset += (UINT32)index2-index1;
433         }
434      }
435
436      if (!memcmp(buffer, "END!", 4))
437      {
438         done = 1;
439      }
440      else
441      {
442         chain_offs += chunk_size + 8;
443      }
444   }
445
446   fclose(infile);
447
448   return CHDERR_NONE;
449}
450
451/*-------------------------------------------------
452    chdcd_parse_gdi - parse a Sega GD-ROM rip
453-------------------------------------------------*/
454
455static chd_error chdcd_parse_gdi(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
456{
457   FILE *infile;
458   int i, numtracks;
459   //int chdpos=0;
460
461   astring path = astring(tocfname);
462
463   infile = fopen(tocfname, "rt");   
464   path = get_file_path(path);
465
466   if (infile == (FILE *)NULL)
467   {
468      return CHDERR_FILE_NOT_FOUND;
469   }
470
471   /* clear structures */
472   memset(outtoc, 0, sizeof(cdrom_toc));
473   memset(outinfo, 0, sizeof(chdcd_track_input_info));
474
475
476   fgets(linebuffer,511,infile);
477   numtracks=atoi(linebuffer);
478
479   for(i=0;i<numtracks;++i)
480   {
481      char *tok;
482      int trknum;
483      int trksize,trktype;
484      int sz;
485      int hunks;
486
487
488      fgets(linebuffer,511,infile);
489
490      tok=strtok(linebuffer," ");
491
492      trknum=atoi(tok)-1;
493
494      outinfo->swap[trknum]=0;
495      outinfo->offset[trknum]=0;
496
497      //outtoc->tracks[trknum].trktype = CD_TRACK_MODE1;
498      outtoc->tracks[trknum].datasize = 0;
499      outtoc->tracks[trknum].subtype = CD_SUB_NONE;
500      outtoc->tracks[trknum].subsize = 0;
501
502      tok=strtok(NULL," ");
503      outtoc->tracks[trknum].physframeofs=atoi(tok);
504
505      tok=strtok(NULL," ");
506      trktype=atoi(tok);
507
508      tok=strtok(NULL," ");
509      trksize=atoi(tok);
510
511      if(trktype==4 && trksize==2352)
512      {
513         outtoc->tracks[trknum].trktype=CD_TRACK_MODE1_RAW;
514         outtoc->tracks[trknum].datasize=2352;
515      }
516      if(trktype==4 && trksize==2048)
517      {
518         outtoc->tracks[trknum].trktype=CD_TRACK_MODE1;
519         outtoc->tracks[trknum].datasize=2048;
520      }
521      if(trktype==0)
522      {
523         //assert(trksize==2352);
524         outtoc->tracks[trknum].trktype=CD_TRACK_AUDIO;
525         outtoc->tracks[trknum].datasize=2352;
526      }
527
528      astring name;
529     
530      tok=strtok(NULL," ");     
531      name = tok;
532      if (tok[0]=='"') {
533         do {
534            tok=strtok(NULL," ");           
535            if (tok!=NULL) {
536               name += " ";
537               name += tok;
538            }
539         } while(tok!=NULL && (strrchr(tok,'"')-tok !=(strlen(tok)-1)));
540         name = name.delchr('"');
541      }
542      strncpy(outinfo->fname[trknum], path.cstr(), 256);
543      strncat(outinfo->fname[trknum], name, 256);
544     
545      sz=get_file_size(outinfo->fname[trknum]);
546
547      outtoc->tracks[trknum].frames=sz/trksize;
548      outtoc->tracks[trknum].extraframes=0;
549
550      if(trknum!=0)
551      {
552         int dif=outtoc->tracks[trknum].physframeofs-(outtoc->tracks[trknum-1].frames+outtoc->tracks[trknum-1].physframeofs);
553         outtoc->tracks[trknum-1].frames+=dif;
554      }
555
556/*
557        if(trknum!=0)
558        {
559            outtoc->tracks[trknum-1].extraframes=outtoc->tracks[trknum].physframeofs-(outtoc->tracks[trknum-1].frames+outtoc->tracks[trknum-1].physframeofs);
560        }
561*/
562      hunks = (outtoc->tracks[trknum].frames+CD_FRAMES_PER_HUNK - 1) / CD_FRAMES_PER_HUNK;
563      outtoc->tracks[trknum].extraframes = hunks * CD_FRAMES_PER_HUNK - outtoc->tracks[trknum].frames;
564
565      //chdpos+=outtoc->tracks[trknum].frames+outtoc->tracks[trknum].extraframes;
566
567   }
568   /*
569    for(i=0;i<numtracks;++i)
570    {
571        printf("%s %d %d %d\n",outinfo->fname[i],outtoc->tracks[i].frames,outtoc->tracks[i].extraframes,outtoc->tracks[i].physframeofs);
572    }
573    */
574   /* close the input TOC */
575   fclose(infile);
576
577   /* store the number of tracks found */
578   outtoc->numtrks = numtracks;
579
580   return CHDERR_NONE;
581}
582
583/*-------------------------------------------------
584    chdcd_parse_toc - parse a CDRWin format CUE file
585-------------------------------------------------*/
586
587chd_error chdcd_parse_cue(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
588{
589   FILE *infile;
590   int i, trknum;
591   static char token[128];
592   static char lastfname[256];
593   UINT32 wavlen, wavoffs;
594   astring path = astring(tocfname);
595
596   infile = fopen(tocfname, "rt");   
597   path = get_file_path(path);
598   if (infile == (FILE *)NULL)
599   {
600      return CHDERR_FILE_NOT_FOUND;
601   }
602
603   /* clear structures */
604   memset(outtoc, 0, sizeof(cdrom_toc));
605   memset(outinfo, 0, sizeof(chdcd_track_input_info));
606
607   trknum = -1;
608   wavoffs = wavlen = 0;
609
610   while (!feof(infile))
611   {
612      /* get the next line */
613      fgets(linebuffer, 511, infile);
614
615      /* if EOF didn't hit, keep going */
616      if (!feof(infile))
617      {
618         i = 0;
619
620         TOKENIZE
621
622         if (!strcmp(token, "FILE"))
623         {
624            /* found the data file for a track */
625            TOKENIZE
626
627            /* keep the filename */
628            strncpy(lastfname, path.cstr(), 256);
629            strncat(lastfname, token, 256);
630
631            /* get the file type */
632            TOKENIZE
633
634            if (!strcmp(token, "BINARY"))
635            {
636               outinfo->swap[trknum] = 0;
637            }
638            else if (!strcmp(token, "MOTOROLA"))
639            {
640               outinfo->swap[trknum] = 1;
641            }
642            else if (!strcmp(token, "WAVE"))
643            {
644               wavlen = parse_wav_sample(lastfname, &wavoffs);
645               if (!wavlen)
646               {
647                  file_error err;
648                  core_file *fhand;
649
650                  err = core_fopen(lastfname, OPEN_FLAG_READ, &fhand);
651                  if (err != FILERR_NONE) printf("holy moley!\n");
652                  else core_fclose(fhand);
653
654                  printf("ERROR: couldn't read [%s] or not a valid .WAV\n", lastfname);
655                  return CHDERR_FILE_NOT_FOUND;
656               }
657            }
658            else
659            {
660               printf("ERROR: Unhandled track type %s\n", token);
661               return CHDERR_FILE_NOT_FOUND;
662            }
663         }
664         else if (!strcmp(token, "TRACK"))
665         {
666            /* get the track number */
667            TOKENIZE
668            trknum = strtoul(token, NULL, 10) - 1;
669
670            /* next token on the line is the track type */
671            TOKENIZE
672
673            if (wavlen != 0)
674            {
675               outtoc->tracks[trknum].trktype = CD_TRACK_AUDIO;
676               outtoc->tracks[trknum].frames = wavlen/2352;
677               outinfo->offset[trknum] = wavoffs;
678               wavoffs = wavlen = 0;
679            }
680            else
681            {
682               outtoc->tracks[trknum].trktype = CD_TRACK_MODE1;
683               outtoc->tracks[trknum].datasize = 0;
684               outinfo->offset[trknum] = 0;
685            }
686            outtoc->tracks[trknum].subtype = CD_SUB_NONE;
687            outtoc->tracks[trknum].subsize = 0;
688            outtoc->tracks[trknum].pregap = 0;
689            outinfo->idx0offs[trknum] = -1;
690            outinfo->idx1offs[trknum] = 0;
691
692            strncpy(outinfo->fname[trknum], lastfname, 256); // default filename to the last one
693//              printf("trk %d: fname %s offset %d\n", trknum, &outinfo->fname[trknum][0], outinfo->offset[trknum]);
694
695            cdrom_convert_type_string_to_track_info(token, &outtoc->tracks[trknum]);
696            if (outtoc->tracks[trknum].datasize == 0)
697            {
698               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
699               return CHDERR_FILE_NOT_FOUND;
700            }
701
702            /* next (optional) token on the line is the subcode type */
703            TOKENIZE
704
705            cdrom_convert_subtype_string_to_track_info(token, &outtoc->tracks[trknum]);
706         }
707         else if (!strcmp(token, "INDEX"))   /* only in bin/cue files */
708         {
709            int idx, frames;
710
711            /* get index number */
712            TOKENIZE
713            idx = strtoul(token, NULL, 10);
714
715            /* get index */
716            TOKENIZE
717            frames = msf_to_frames( token );
718
719            if (idx == 0)
720            {
721               outinfo->idx0offs[trknum] = frames;
722            }
723            else if (idx == 1)
724            {
725               outinfo->idx1offs[trknum] = frames;
726               if ((outtoc->tracks[trknum].pregap == 0) && (outinfo->idx0offs[trknum] != -1))
727               {
728                  outtoc->tracks[trknum].pregap = frames - outinfo->idx0offs[trknum];
729               }
730            }
731         }
732         else if (!strcmp(token, "PREGAP"))
733         {
734            int frames;
735
736            /* get index */
737            TOKENIZE
738            frames = msf_to_frames( token );
739
740            outtoc->tracks[trknum].pregap = frames;
741         }
742         else if (!strcmp(token, "POSTGAP"))
743         {
744            int frames;
745
746            /* get index */
747            TOKENIZE
748            frames = msf_to_frames( token );
749
750            outtoc->tracks[trknum].postgap = frames;
751         }
752      }
753   }
754
755   /* close the input CUE */
756   fclose(infile);
757
758   /* store the number of tracks found */
759   outtoc->numtrks = trknum + 1;
760
761   /* now go over the files again and set the lengths */
762   for (trknum = 0; trknum < outtoc->numtrks; trknum++)
763   {
764      UINT64 tlen = 0;
765
766      // this is true for cue/bin and cue/iso, and we need it for cue/wav since .WAV is little-endian
767      if (outtoc->tracks[trknum].trktype == CD_TRACK_AUDIO)
768      {
769         outinfo->swap[trknum] = 1;
770      }
771
772      // don't do this for .WAV tracks, we already have their length and offset filled out
773      if (outinfo->offset[trknum] == 0)
774      {
775         // is this the last track?
776         if (trknum == (outtoc->numtrks-1))
777         {
778            /* if we have the same filename as the last track, do it that way */
779            if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum-1][0]))
780            {
781               tlen = get_file_size(outinfo->fname[trknum]);
782               if (tlen == 0)
783               {
784                  printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]);
785                  return CHDERR_FILE_NOT_FOUND;
786               }
787               outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize);
788               outtoc->tracks[trknum].frames = (tlen - outinfo->offset[trknum]) / (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
789            }
790            else   /* data files are different */
791            {
792               tlen = get_file_size(outinfo->fname[trknum]);
793               if (tlen == 0)
794               {
795                  printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]);
796                  return CHDERR_FILE_NOT_FOUND;
797               }
798               tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
799               outtoc->tracks[trknum].frames = tlen;
800               outinfo->offset[trknum] = 0;
801            }
802         }
803         else
804         {
805            /* if we have the same filename as the next track, do it that way */
806            if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum+1][0]))
807            {
808               outtoc->tracks[trknum].frames = outinfo->idx1offs[trknum+1] - outinfo->idx1offs[trknum];
809
810               if (trknum == 0)   // track 0 offset is 0
811               {
812                  outinfo->offset[trknum] = 0;
813               }
814               else
815               {
816                  outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize);
817               }
818
819               if (!outtoc->tracks[trknum].frames)
820               {
821                  printf("ERROR: unable to determine size of track %d, missing INDEX 01 markers?\n", trknum+1);
822                  return CHDERR_FILE_NOT_FOUND;
823               }
824            }
825            else   /* data files are different */
826            {
827               tlen = get_file_size(outinfo->fname[trknum]);
828               if (tlen == 0)
829               {
830                  printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum]);
831                  return CHDERR_FILE_NOT_FOUND;
832               }
833               tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
834               outtoc->tracks[trknum].frames = tlen;
835               outinfo->offset[trknum] = 0;
836            }
837         }
838      }
839      printf("trk %d: %d frames @ offset %d\n", trknum+1, outtoc->tracks[trknum].frames, outinfo->offset[trknum]);
840   }
841
842   return CHDERR_NONE;
843}
844
845/*-------------------------------------------------
846    chdcd_parse_toc - parse a CDRDAO format TOC file
847-------------------------------------------------*/
848
849chd_error chdcd_parse_toc(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
850{
851   FILE *infile;
852   int i, trknum;
853   static char token[128];
854
855   if (strstr(tocfname,".gdi"))
856   {
857      return chdcd_parse_gdi(tocfname, outtoc, outinfo);
858   }
859
860   if (strstr(tocfname,".cue"))
861   {
862      return chdcd_parse_cue(tocfname, outtoc, outinfo);
863   }
864
865   if (strstr(tocfname,".nrg"))
866   {
867      return chdcd_parse_nero(tocfname, outtoc, outinfo);
868   }
869
870   astring path = astring(tocfname);
871
872   infile = fopen(tocfname, "rt");   
873   path = get_file_path(path);
874
875   if (infile == (FILE *)NULL)
876   {
877      return CHDERR_FILE_NOT_FOUND;
878   }
879
880   /* clear structures */
881   memset(outtoc, 0, sizeof(cdrom_toc));
882   memset(outinfo, 0, sizeof(chdcd_track_input_info));
883
884   trknum = -1;
885
886   while (!feof(infile))
887   {
888      /* get the next line */
889      fgets(linebuffer, 511, infile);
890
891      /* if EOF didn't hit, keep going */
892      if (!feof(infile))
893      {
894         i = 0;
895
896         TOKENIZE
897
898         if ((!strcmp(token, "DATAFILE")) || (!strcmp(token, "AUDIOFILE")) || (!strcmp(token, "FILE")))
899         {
900            int f;
901
902            /* found the data file for a track */
903            TOKENIZE
904
905            /* keep the filename */
906            strncpy(outinfo->fname[trknum], path.cstr(), 256);
907            strncat(outinfo->fname[trknum], token, 256);
908
909            /* get either the offset or the length */
910            TOKENIZE
911
912            if (!strcmp(token, "SWAP"))
913            {
914               TOKENIZE
915
916               outinfo->swap[trknum] = 1;
917            }
918            else
919            {
920               outinfo->swap[trknum] = 0;
921            }
922
923            if (token[0] == '#')
924            {
925               /* it's a decimal offset, use it */
926               f = strtoul(&token[1], NULL, 10);
927            }
928            else if (isdigit((UINT8)token[0]))
929            {
930               /* convert the time to an offset */
931               f = msf_to_frames( token );
932
933               f *= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
934            }
935            else
936            {
937               f = 0;
938            }
939
940            outinfo->offset[trknum] = f;
941
942            TOKENIZE
943
944            if (isdigit((UINT8)token[0]))
945            {
946               // this could be the length or an offset from the previous field.
947               f = msf_to_frames( token );
948
949               TOKENIZE
950
951               if (isdigit((UINT8)token[0]))
952               {
953                  // it was an offset.
954                  f *= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
955
956                  outinfo->offset[trknum] += f;
957
958                  // this is the length.
959                  f = msf_to_frames( token );
960               }
961            }
962            else if( trknum == 0 && outinfo->offset[trknum] != 0 )
963            {
964               /* the 1st track might have a length with no offset */
965               f = outinfo->offset[trknum] / (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
966               outinfo->offset[trknum] = 0;
967            }
968            else
969            {
970               /* guesstimate the track length? */
971               f = 0;
972            }
973
974            outtoc->tracks[trknum].frames = f;
975         }
976         else if (!strcmp(token, "TRACK"))
977         {
978            trknum++;
979
980            /* next token on the line is the track type */
981            TOKENIZE
982
983            outtoc->tracks[trknum].trktype = CD_TRACK_MODE1;
984            outtoc->tracks[trknum].datasize = 0;
985            outtoc->tracks[trknum].subtype = CD_SUB_NONE;
986            outtoc->tracks[trknum].subsize = 0;
987
988            cdrom_convert_type_string_to_track_info(token, &outtoc->tracks[trknum]);
989            if (outtoc->tracks[trknum].datasize == 0)
990            {
991               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
992               return CHDERR_FILE_NOT_FOUND;
993            }
994
995            /* next (optional) token on the line is the subcode type */
996            TOKENIZE
997
998            cdrom_convert_subtype_string_to_track_info(token, &outtoc->tracks[trknum]);
999         }
1000         else if (!strcmp(token, "START"))
1001         {
1002            int frames;
1003
1004            /* get index */
1005            TOKENIZE
1006            frames = msf_to_frames( token );
1007
1008            outtoc->tracks[trknum].pregap = frames;
1009         }
1010      }
1011   }
1012
1013   /* close the input TOC */
1014   fclose(infile);
1015
1016   /* store the number of tracks found */
1017   outtoc->numtrks = trknum + 1;
1018
1019   return CHDERR_NONE;
1020}
src/tools/chdcd.h
r12669r12670
1/***************************************************************************
2
3    CDRDAO TOC parser for CHD compression frontend
4
5    Copyright Nicola Salmoria and the MAME Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8***************************************************************************/
9
10#pragma once
11
12#ifndef __CHDCD_H__
13#define __CHDCD_H__
14
15#include "cdrom.h"
16
17typedef struct _chdcd_track_input_info chdcd_track_input_info;
18struct _chdcd_track_input_info   /* used only at compression time */
19{
20   char fname[CD_MAX_TRACKS][256];   /* filename for each track */
21   UINT32 offset[CD_MAX_TRACKS];   /* offset in the data file for each track */
22   int swap[CD_MAX_TRACKS];   /* data needs to be byte swapped */
23   UINT32 idx0offs[CD_MAX_TRACKS];
24   UINT32 idx1offs[CD_MAX_TRACKS];
25};
26
27
28chd_error chdcd_parse_toc(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo);
29
30#endif   /* __CHDCD_H__ */
src/tools/tools.mak
r12669r12670
8383
8484CHDMANOBJS = \
8585   $(TOOLSOBJ)/chdman.o \
86   $(TOOLSOBJ)/chdcd.o \
8786
8887chdman$(EXE): $(VERSIONOBJ) $(CHDMANOBJS) $(LIBUTIL) $(ZLIB) $(EXPAT) $(LIBOCORE)
8988   @echo Linking $@...
src/emu/imagedev/chd_cd.c
r12669r12670
9696   image_device_format *format = global_alloc_clear(image_device_format);;
9797   format->m_index      = 0;
9898   format->m_name        = "chdcd";
99   format->m_description = "CHD CD-ROM drive";
100   format->m_extensions  = "chd";
99   format->m_description = "CD-ROM drive";
100   format->m_extensions  = "chd,cue,toc,nrg,gdi";
101101   format->m_optspec     = cd_option_spec;
102102   format->m_next        = NULL;
103103
r12669r12670
129129
130130   if (software_entry() == NULL)
131131   {
132      err = chd_open_file( image_core_file(), CHD_OPEN_READ, NULL, &chd );   /* CDs are never writeable */
133      if ( err )
134         goto error;
132      if (strstr(m_image_name,".chd")) {
133         err = chd_open_file( image_core_file(), CHD_OPEN_READ, NULL, &chd );   /* CDs are never writeable */
134         if ( err )
135            goto error;
136      }
135137   } else {
136138      chd  = get_disk_handle(device().machine(), device().subtag(tempstring,"cdrom"));
137139   }
138140
139141   /* open the CHD file */
140   m_cdrom_handle = cdrom_open( chd );
142   if (chd) {
143      m_cdrom_handle = cdrom_open( chd );
144   } else {
145      m_cdrom_handle = cdrom_open( m_image_name );
146   }
141147   if ( ! m_cdrom_handle )
142148      goto error;
143149
src/lib/lib.mak
r12669r12670
3434   $(LIBOBJ)/util/bitmap.o \
3535   $(LIBOBJ)/util/cdrom.o \
3636   $(LIBOBJ)/util/chd.o \
37   $(LIBOBJ)/util/chdcd.o \
3738   $(LIBOBJ)/util/corefile.o \
3839   $(LIBOBJ)/util/corestr.o \
3940   $(LIBOBJ)/util/coreutil.o \
src/lib/util/cdrom.c
r12669r12670
4848#include "cdrom.h"
4949
5050#include <stdlib.h>
51#include "chdcd.h"
5152
5253
53
5454/***************************************************************************
5555    DEBUGGING
5656***************************************************************************/
r12669r12670
7474{
7575   chd_file *         chd;            /* CHD file */
7676   cdrom_toc         cdtoc;            /* TOC for the CD */
77   chdcd_track_input_info track_info;      /* track info */
7778   UINT32            hunksectors;      /* sectors per hunk */
7879   UINT32            cachehunk;         /* which hunk is cached */
7980   UINT8 *            cache;            /* cache of the current hunk */
81   core_file *         fhandle[CD_MAX_TRACKS];/* file handle */
8082};
8183
8284
r12669r12670
122124    BASE FUNCTIONALITY
123125***************************************************************************/
124126
127cdrom_file *cdrom_open(const char *inputfile)
128{   
129   int i;
130   cdrom_file *file;
131   UINT32 physofs, chdofs;   
132   
133   /* allocate memory for the CD-ROM file */
134   file = (cdrom_file *)malloc(sizeof(cdrom_file));
135   if (file == NULL)
136      return NULL;
137
138   /* setup the CDROM module and get the disc info */
139   chd_error err = chdcd_parse_toc(inputfile, &file->cdtoc, &file->track_info);
140   if (err != CHDERR_NONE)
141   {
142      fprintf(stderr, "Error reading input file: %s\n", chd_error_string(err));
143      return NULL;
144   }
145
146   /* fill in the data */
147   file->chd = NULL;
148   file->hunksectors = 1;
149   file->cachehunk = -1;
150
151   LOG(("CD has %d tracks\n", file->cdtoc.numtrks));
152
153   for (i = 0; i < file->cdtoc.numtrks; i++)
154   {
155      file_error filerr = core_fopen(file->track_info.fname[i], OPEN_FLAG_READ, &file->fhandle[i]);
156      if (filerr != FILERR_NONE)
157      {
158         fprintf(stderr, "Unable to open file: %s\n", file->track_info.fname[i]);
159         return NULL;
160      }
161   }
162   /* calculate the starting frame for each track, keeping in mind that CHDMAN
163       pads tracks out with extra frames to fit hunk size boundries
164    */
165   physofs = chdofs = 0;
166   for (i = 0; i < file->cdtoc.numtrks; i++)
167   {
168      file->cdtoc.tracks[i].physframeofs = physofs;
169      file->cdtoc.tracks[i].chdframeofs = chdofs;
170
171      physofs += file->cdtoc.tracks[i].frames;
172      chdofs  += file->cdtoc.tracks[i].frames;
173      chdofs  += file->cdtoc.tracks[i].extraframes;
174
175      LOG(("Track %02d is format %d subtype %d datasize %d subsize %d frames %d extraframes %d physofs %d chdofs %d\n", i+1,
176         file->cdtoc.tracks[i].trktype,
177         file->cdtoc.tracks[i].subtype,
178         file->cdtoc.tracks[i].datasize,
179         file->cdtoc.tracks[i].subsize,
180         file->cdtoc.tracks[i].frames,
181         file->cdtoc.tracks[i].extraframes,
182         file->cdtoc.tracks[i].physframeofs,
183         file->cdtoc.tracks[i].chdframeofs));
184   }
185
186   /* fill out dummy entries for the last track to help our search */
187   file->cdtoc.tracks[i].physframeofs = physofs;
188   file->cdtoc.tracks[i].chdframeofs = chdofs;
189
190   /* allocate a cache */
191   file->cache = (UINT8 *)malloc(CD_FRAME_SIZE);
192   if (file->cache == NULL)
193   {
194      free(file);
195      return NULL;
196   }
197
198   return file;   
199}
200
125201/*-------------------------------------------------
126202    cdrom_open - "open" a CD-ROM file from an
127203    already-opened CHD file
r12669r12670
215291   /* free the cache */
216292   if (file->cache)
217293      free(file->cache);
294     
295   for (int i = 0; i < file->cdtoc.numtrks; i++)
296   {
297      core_fclose(file->fhandle[i]);
298   }
299     
218300   free(file);
219301}
220302
r12669r12670
639721   /* if we haven't cached this hunk, read it now */
640722   if (file->cachehunk != hunknum)
641723   {
642      err = chd_read(file->chd, hunknum, file->cache);
643      if (err != CHDERR_NONE)
644         return err;
724      if (file->chd) {
725         err = chd_read(file->chd, hunknum, file->cache);
726         if (err != CHDERR_NONE)
727            return err;
728      } else {     
729         core_file *srcfile = file->fhandle[*tracknum];
730
731         UINT64 sourcefileoffset = file->track_info.offset[*tracknum];
732         int bytespersector = file->cdtoc.tracks[*tracknum].datasize + file->cdtoc.tracks[*tracknum].subsize;         
733
734         sourcefileoffset += chdsector * bytespersector;
735
736         core_fseek(srcfile, sourcefileoffset, SEEK_SET);
737         core_fread(srcfile, file->cache, bytespersector);
738
739         if (file->track_info.swap[*tracknum])               
740         {
741            for (int swapindex = 0; swapindex < 2352; swapindex += 2 )
742            {
743               int swaptemp = file->cache[ swapindex ];
744               file->cache[ swapindex ] = file->cache[ swapindex + 1 ];
745               file->cache[ swapindex + 1 ] = swaptemp;
746            }
747         }
748      }
749     
645750      file->cachehunk = hunknum;
646751   }
647752   return CHDERR_NONE;
src/lib/util/cdrom.h
r12669r12670
130130cdrom_file *cdrom_open(chd_file *chd);
131131void cdrom_close(cdrom_file *file);
132132
133cdrom_file *cdrom_open(const char *inputfile);
134
133135/* core read access */
134136UINT32 cdrom_read_data(cdrom_file *file, UINT32 lbasector, void *buffer, UINT32 datatype);
135137UINT32 cdrom_read_subcode(cdrom_file *file, UINT32 lbasector, void *buffer);
src/lib/util/chdcd.c
r0r12670
1/***************************************************************************
2
3    TOC parser for CHD compression frontend
4    Handles CDRDAO .toc, CDRWIN .cue, and Sega GDROM .gdi
5
6    Copyright Nicola Salmoria and the MAME Team.
7    Visit http://mamedev.org for licensing and usage restrictions.
8
9***************************************************************************/
10
11#include <ctype.h>
12#include <stdlib.h>
13#include "osdcore.h"
14#include "chd.h"
15#include "chdcd.h"
16#include "corefile.h"
17
18
19
20/***************************************************************************
21    CONSTANTS & DEFINES
22***************************************************************************/
23
24#define TOKENIZE i = tokenize( linebuffer, i, sizeof(linebuffer), token, sizeof(token) );
25
26
27
28/***************************************************************************
29    GLOBAL VARIABLES
30***************************************************************************/
31
32static char linebuffer[512];
33
34
35
36/***************************************************************************
37    IMPLEMENTATION
38***************************************************************************/
39
40static astring get_file_path(astring &path)
41{   
42   int pos = path.rchr( 0, '\\');
43   if (pos!=-1) {
44      path = path.substr(0,pos+1);
45   } else {
46      pos = path.rchr( 0, '/');
47      path = path.substr(0,pos+1);
48   }
49   return path;
50}
51/*-------------------------------------------------
52    get_file_size - get the size of a file
53-------------------------------------------------*/
54
55static UINT64 get_file_size(const char *filename)
56{
57   osd_file *file;
58   UINT64 filesize = 0;
59   file_error filerr;
60
61   filerr = osd_open(filename, OPEN_FLAG_READ, &file, &filesize);
62   if (filerr == FILERR_NONE)
63      osd_close(file);
64
65   return filesize;
66}
67
68
69/*-------------------------------------------------
70    tokenize - get a token from the line buffer
71-------------------------------------------------*/
72
73static int tokenize( const char *linebuffer, int i, int linebuffersize, char *token, int tokensize )
74{
75   int j = 0;
76   int singlequote = 0;
77   int doublequote = 0;
78
79   while ((i < linebuffersize) && isspace((UINT8)linebuffer[i]))
80   {
81      i++;
82   }
83
84   while ((i < linebuffersize) && (j < tokensize))
85   {
86      if (!singlequote && linebuffer[i] == '"' )
87      {
88         doublequote = !doublequote;
89      }
90      else if (!doublequote && linebuffer[i] == '\'')
91      {
92         singlequote = !singlequote;
93      }
94      else if (!singlequote && !doublequote && isspace((UINT8)linebuffer[i]))
95      {
96         break;
97      }
98      else
99      {
100         token[j] = linebuffer[i];
101         j++;
102      }
103
104      i++;
105   }
106
107   token[j] = '\0';
108
109   return i;
110}
111
112
113/*-------------------------------------------------
114    msf_to_frames - convert m:s:f into a number of frames
115-------------------------------------------------*/
116
117static int msf_to_frames( char *token )
118{
119   int m = 0;
120   int s = 0;
121   int f = 0;
122
123   if( sscanf( token, "%d:%d:%d", &m, &s, &f ) == 1 )
124   {
125      f = m;
126   }
127   else
128   {
129      /* convert to just frames */
130      s += (m * 60);
131      f += (s * 75);
132   }
133
134   return f;
135}
136
137/*-------------------------------------------------
138    parse_wav_sample - takes a .WAV file, verifies
139    that the file is 16 bits, and returns the
140    length in bytes of the data and the offset in
141    bytes to where the data starts in the file.
142-------------------------------------------------*/
143static UINT32 parse_wav_sample(char *filename, UINT32 *dataoffs)
144{
145   unsigned long offset = 0;
146   UINT32 length, rate, filesize;
147   UINT16 bits, temp16;
148   char buf[32];
149   osd_file *file;
150   file_error filerr;
151   UINT64 fsize = 0;
152   UINT32 actual;
153
154   filerr = osd_open(filename, OPEN_FLAG_READ, &file, &fsize);
155   if (filerr != FILERR_NONE)
156   {
157      osd_close(file);
158      return 0;
159   }
160
161   /* read the core header and make sure it's a WAVE file */
162   osd_read(file, buf, 0, 4, &actual);
163   offset += actual;
164   if (offset < 4)
165      return 0;
166   if (memcmp(&buf[0], "RIFF", 4) != 0)
167      return 0;
168
169   /* get the total size */
170   osd_read(file, &filesize, offset, 4, &actual);
171   offset += actual;
172   if (offset < 8)
173      return 0;
174   filesize = LITTLE_ENDIANIZE_INT32(filesize);
175
176   /* read the RIFF file type and make sure it's a WAVE file */
177   osd_read(file, buf, offset, 4, &actual);
178   offset += actual;
179   if (offset < 12)
180      return 0;
181   if (memcmp(&buf[0], "WAVE", 4) != 0)
182      return 0;
183
184   /* seek until we find a format tag */
185   while (1)
186   {
187      osd_read(file, buf, offset, 4, &actual);
188      offset += actual;
189      osd_read(file, &length, offset, 4, &actual);
190      offset += actual;
191      length = LITTLE_ENDIANIZE_INT32(length);
192      if (memcmp(&buf[0], "fmt ", 4) == 0)
193         break;
194
195      /* seek to the next block */
196      offset += length;
197      if (offset >= filesize)
198         return 0;
199   }
200
201   /* read the format -- make sure it is PCM */
202   osd_read(file, &temp16, offset, 2, &actual);
203   offset += actual;
204   temp16 = LITTLE_ENDIANIZE_INT16(temp16);
205   if (temp16 != 1)
206      return 0;
207
208   /* number of channels -- only mono is supported */
209   osd_read(file, &temp16, offset, 2, &actual);
210   offset += actual;
211   temp16 = LITTLE_ENDIANIZE_INT16(temp16);
212   if (temp16 != 2)
213      return 0;
214
215   /* sample rate */
216   osd_read(file, &rate, offset, 4, &actual);
217   offset += actual;
218   rate = LITTLE_ENDIANIZE_INT32(rate);
219   if (rate != 44100)
220      return 0;
221
222   /* bytes/second and block alignment are ignored */
223   osd_read(file, buf, offset, 6, &actual);
224   offset += actual;
225
226   /* bits/sample */
227   osd_read(file, &bits, offset, 2, &actual);
228   offset += actual;
229   if (bits != 16)
230      return 0;
231
232   /* seek past any extra data */
233   offset += length - 16;
234
235   /* seek until we find a data tag */
236   while (1)
237   {
238      osd_read(file, buf, offset, 4, &actual);
239      offset += actual;
240      osd_read(file, &length, offset, 4, &actual);
241      offset += actual;
242      length = LITTLE_ENDIANIZE_INT32(length);
243      if (memcmp(&buf[0], "data", 4) == 0)
244         break;
245
246      /* seek to the next block */
247      offset += length;
248      if (offset >= filesize)
249         return 0;
250   }
251
252   /* if there was a 0 length data block, we're done */
253   if (length == 0)
254      return 0;
255
256   osd_close(file);
257
258   *dataoffs = offset;
259
260   return length;
261}
262
263UINT16 read_uint16(FILE *infile)
264{
265   UINT16 res = 0;
266   unsigned char buffer[2];
267
268   fread(buffer, 2, 1, infile);
269
270   res = buffer[1] | buffer[0]<<8;
271
272   return res;
273}
274
275UINT32 read_uint32(FILE *infile)
276{
277   UINT32 res = 0;
278   unsigned char buffer[4];
279
280   fread(buffer, 4, 1, infile);
281
282   res = buffer[3] | buffer[2]<<8 | buffer[1]<<16 | buffer[0]<<24;
283
284   return res;
285}
286
287UINT64 read_uint64(FILE *infile)
288{
289   UINT64 res0 = U64(0), res1 = U64(0);
290   UINT64 res;
291   unsigned char buffer[8];
292
293   fread(buffer, 8, 1, infile);
294
295   res0 = buffer[3] | buffer[2]<<8 | buffer[1]<<16 | buffer[0]<<24;
296   res1 = buffer[7] | buffer[6]<<8 | buffer[5]<<16 | buffer[4]<<24;
297
298   res = res0<<32 | res1;
299
300   return res;
301}
302
303/*-------------------------------------------------
304    chdcd_parse_toc - parse a CDRWin format CUE file
305-------------------------------------------------*/
306
307chd_error chdcd_parse_nero(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
308{
309   FILE *infile;
310   unsigned char buffer[12];
311   UINT32 chain_offs, chunk_size;
312   int done = 0;
313
314   astring path = astring(tocfname);
315
316   infile = fopen(tocfname, "rt");   
317   path = get_file_path(path);
318
319   if (infile == (FILE *)NULL)
320   {
321      return CHDERR_FILE_NOT_FOUND;
322   }
323
324   /* clear structures */
325   memset(outtoc, 0, sizeof(cdrom_toc));
326   memset(outinfo, 0, sizeof(chdcd_track_input_info));
327
328   // seek to 12 bytes before the end
329   fseek(infile, -12, SEEK_END);
330   fread(buffer, 12, 1, infile);
331
332   if (memcmp(buffer, "NER5", 4))
333   {
334      printf("ERROR: Not a Nero 5.5 or later image!\n");
335      fclose(infile);
336      return CHDERR_FILE_NOT_FOUND;
337   }
338
339   chain_offs = buffer[11] | (buffer[10]<<8) | (buffer[9]<<16) | (buffer[8]<<24);
340
341   if ((buffer[7] != 0) || (buffer[6] != 0) || (buffer[5] != 0) || (buffer[4] != 0))
342   {
343      printf("ERROR: File size is > 4GB, this version of CHDMAN cannot handle it.");
344      fclose(infile);
345      return CHDERR_FILE_NOT_FOUND;
346   }
347
348//  printf("NER5 detected, chain offset: %x\n", chain_offs);
349
350   while (!done)
351   {
352      UINT32 offset;
353      UINT8 start, end;
354      int track;
355
356      fseek(infile, chain_offs, SEEK_SET);
357      fread(buffer, 8, 1, infile);
358
359      chunk_size = (buffer[7] | buffer[6]<<8 | buffer[5]<<16 | buffer[4]<<24);
360
361//      printf("Chunk type: %c%c%c%c, size %x\n", buffer[0], buffer[1], buffer[2], buffer[3], chunk_size);
362
363      // we want the DAOX chunk, which has the TOC information
364      if (!memcmp(buffer, "DAOX", 4))
365      {
366         // skip second chunk size and UPC code
367         fseek(infile, 16, SEEK_CUR);
368
369         read_uint32(infile);
370         fread(&start, 1, 1, infile);
371         fread(&end, 1, 1, infile);
372
373//          printf("TOC type: %08x.  Start track %d  End track: %d\n", toc_type, start, end);
374
375         outtoc->numtrks = (end-start) + 1;
376
377         offset = 0;
378         for (track = start; track <= end; track++)
379         {
380            UINT32 size, mode;
381            UINT64 index0, index1, index2;
382
383            fseek(infile, 12, SEEK_CUR);   // skip ISRC code
384            size = read_uint16(infile);
385            mode = read_uint32(infile);
386            index0 = read_uint64(infile);
387            index1 = read_uint64(infile);
388            index2 = read_uint64(infile);
389
390//              printf("Track %d: sector size %d mode %x index0 %llx index1 %llx index2 %llx (pregap %d sectors, length %d sectors)\n", track, size, mode, index0, index1, index2, (UINT32)(index1-index0)/size, (UINT32)(index2-index1)/size);
391            strncpy(outinfo->fname[track-1], path.cstr(), 256);
392            strncat(outinfo->fname[track-1], tocfname, 256);
393            outinfo->offset[track-1] = offset + (UINT32)(index1-index0);
394            outinfo->idx0offs[track-1] = 0;
395            outinfo->idx1offs[track-1] = 0;
396
397            switch (mode>>24)
398            {
399               case 0x00:   // 2048 byte data
400                  outtoc->tracks[track-1].trktype = CD_TRACK_MODE1;
401                  outinfo->swap[track-1] = 0;
402                  break;
403
404               case 0x06:   // 2352 byte mode 2 raw
405                  outtoc->tracks[track-1].trktype = CD_TRACK_MODE2_RAW;
406                  outinfo->swap[track-1] = 0;
407                  break;
408
409               case 0x07:   // 2352 byte audio
410                  outtoc->tracks[track-1].trktype = CD_TRACK_AUDIO;
411                  outinfo->swap[track-1] = 1;
412                  break;
413
414               default:
415                  printf("ERROR: Unknown track type %x, contact MAMEDEV!\n", mode>>24);
416                  break;
417            }
418
419            outtoc->tracks[track-1].datasize = size;
420
421            outtoc->tracks[track-1].subtype = CD_SUB_NONE;
422            outtoc->tracks[track-1].subsize = 0;
423
424            outtoc->tracks[track-1].pregap = (UINT32)(index1-index0)/size;
425            outtoc->tracks[track-1].frames = (UINT32)(index2-index1)/size;
426            outtoc->tracks[track-1].postgap = 0;
427            outtoc->tracks[track-1].pgtype = 0;
428            outtoc->tracks[track-1].pgsub = CD_SUB_NONE;
429            outtoc->tracks[track-1].pgdatasize = 0;
430            outtoc->tracks[track-1].pgsubsize = 0;
431
432            offset += (UINT32)index2-index1;
433         }
434      }
435
436      if (!memcmp(buffer, "END!", 4))
437      {
438         done = 1;
439      }
440      else
441      {
442         chain_offs += chunk_size + 8;
443      }
444   }
445
446   fclose(infile);
447
448   return CHDERR_NONE;
449}
450
451/*-------------------------------------------------
452    chdcd_parse_gdi - parse a Sega GD-ROM rip
453-------------------------------------------------*/
454
455static chd_error chdcd_parse_gdi(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
456{
457   FILE *infile;
458   int i, numtracks;
459   //int chdpos=0;
460
461   astring path = astring(tocfname);
462
463   infile = fopen(tocfname, "rt");   
464   path = get_file_path(path);
465
466   if (infile == (FILE *)NULL)
467   {
468      return CHDERR_FILE_NOT_FOUND;
469   }
470
471   /* clear structures */
472   memset(outtoc, 0, sizeof(cdrom_toc));
473   memset(outinfo, 0, sizeof(chdcd_track_input_info));
474
475
476   fgets(linebuffer,511,infile);
477   numtracks=atoi(linebuffer);
478
479   for(i=0;i<numtracks;++i)
480   {
481      char *tok;
482      int trknum;
483      int trksize,trktype;
484      int sz;
485      int hunks;
486
487
488      fgets(linebuffer,511,infile);
489
490      tok=strtok(linebuffer," ");
491
492      trknum=atoi(tok)-1;
493
494      outinfo->swap[trknum]=0;
495      outinfo->offset[trknum]=0;
496
497      //outtoc->tracks[trknum].trktype = CD_TRACK_MODE1;
498      outtoc->tracks[trknum].datasize = 0;
499      outtoc->tracks[trknum].subtype = CD_SUB_NONE;
500      outtoc->tracks[trknum].subsize = 0;
501
502      tok=strtok(NULL," ");
503      outtoc->tracks[trknum].physframeofs=atoi(tok);
504
505      tok=strtok(NULL," ");
506      trktype=atoi(tok);
507
508      tok=strtok(NULL," ");
509      trksize=atoi(tok);
510
511      if(trktype==4 && trksize==2352)
512      {
513         outtoc->tracks[trknum].trktype=CD_TRACK_MODE1_RAW;
514         outtoc->tracks[trknum].datasize=2352;
515      }
516      if(trktype==4 && trksize==2048)
517      {
518         outtoc->tracks[trknum].trktype=CD_TRACK_MODE1;
519         outtoc->tracks[trknum].datasize=2048;
520      }
521      if(trktype==0)
522      {
523         //assert(trksize==2352);
524         outtoc->tracks[trknum].trktype=CD_TRACK_AUDIO;
525         outtoc->tracks[trknum].datasize=2352;
526      }
527
528      astring name;
529     
530      tok=strtok(NULL," ");     
531      name = tok;
532      if (tok[0]=='"') {
533         do {
534            tok=strtok(NULL," ");           
535            if (tok!=NULL) {
536               name += " ";
537               name += tok;
538            }
539         } while(tok!=NULL && (strrchr(tok,'"')-tok !=(strlen(tok)-1)));
540         name = name.delchr('"');
541      }
542      strncpy(outinfo->fname[trknum], path.cstr(), 256);
543      strncat(outinfo->fname[trknum], name, 256);
544     
545      sz=get_file_size(outinfo->fname[trknum]);
546
547      outtoc->tracks[trknum].frames=sz/trksize;
548      outtoc->tracks[trknum].extraframes=0;
549
550      if(trknum!=0)
551      {
552         int dif=outtoc->tracks[trknum].physframeofs-(outtoc->tracks[trknum-1].frames+outtoc->tracks[trknum-1].physframeofs);
553         outtoc->tracks[trknum-1].frames+=dif;
554      }
555
556/*
557        if(trknum!=0)
558        {
559            outtoc->tracks[trknum-1].extraframes=outtoc->tracks[trknum].physframeofs-(outtoc->tracks[trknum-1].frames+outtoc->tracks[trknum-1].physframeofs);
560        }
561*/
562      hunks = (outtoc->tracks[trknum].frames+CD_FRAMES_PER_HUNK - 1) / CD_FRAMES_PER_HUNK;
563      outtoc->tracks[trknum].extraframes = hunks * CD_FRAMES_PER_HUNK - outtoc->tracks[trknum].frames;
564
565      //chdpos+=outtoc->tracks[trknum].frames+outtoc->tracks[trknum].extraframes;
566
567   }
568   /*
569    for(i=0;i<numtracks;++i)
570    {
571        printf("%s %d %d %d\n",outinfo->fname[i],outtoc->tracks[i].frames,outtoc->tracks[i].extraframes,outtoc->tracks[i].physframeofs);
572    }
573    */
574   /* close the input TOC */
575   fclose(infile);
576
577   /* store the number of tracks found */
578   outtoc->numtrks = numtracks;
579
580   return CHDERR_NONE;
581}
582
583/*-------------------------------------------------
584    chdcd_parse_toc - parse a CDRWin format CUE file
585-------------------------------------------------*/
586
587chd_error chdcd_parse_cue(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
588{
589   FILE *infile;
590   int i, trknum;
591   static char token[128];
592   static char lastfname[256];
593   UINT32 wavlen, wavoffs;
594   astring path = astring(tocfname);
595
596   infile = fopen(tocfname, "rt");   
597   path = get_file_path(path);
598   if (infile == (FILE *)NULL)
599   {
600      return CHDERR_FILE_NOT_FOUND;
601   }
602
603   /* clear structures */
604   memset(outtoc, 0, sizeof(cdrom_toc));
605   memset(outinfo, 0, sizeof(chdcd_track_input_info));
606
607   trknum = -1;
608   wavoffs = wavlen = 0;
609
610   while (!feof(infile))
611   {
612      /* get the next line */
613      fgets(linebuffer, 511, infile);
614
615      /* if EOF didn't hit, keep going */
616      if (!feof(infile))
617      {
618         i = 0;
619
620         TOKENIZE
621
622         if (!strcmp(token, "FILE"))
623         {
624            /* found the data file for a track */
625            TOKENIZE
626
627            /* keep the filename */
628            strncpy(lastfname, path.cstr(), 256);
629            strncat(lastfname, token, 256);
630
631            /* get the file type */
632            TOKENIZE
633
634            if (!strcmp(token, "BINARY"))
635            {
636               outinfo->swap[trknum] = 0;
637            }
638            else if (!strcmp(token, "MOTOROLA"))
639            {
640               outinfo->swap[trknum] = 1;
641            }
642            else if (!strcmp(token, "WAVE"))
643            {
644               wavlen = parse_wav_sample(lastfname, &wavoffs);
645               if (!wavlen)
646               {
647                  file_error err;
648                  core_file *fhand;
649
650                  err = core_fopen(lastfname, OPEN_FLAG_READ, &fhand);
651                  if (err != FILERR_NONE) printf("holy moley!\n");
652                  else core_fclose(fhand);
653
654                  printf("ERROR: couldn't read [%s] or not a valid .WAV\n", lastfname);
655                  return CHDERR_FILE_NOT_FOUND;
656               }
657            }
658            else
659            {
660               printf("ERROR: Unhandled track type %s\n", token);
661               return CHDERR_FILE_NOT_FOUND;
662            }
663         }
664         else if (!strcmp(token, "TRACK"))
665         {
666            /* get the track number */
667            TOKENIZE
668            trknum = strtoul(token, NULL, 10) - 1;
669
670            /* next token on the line is the track type */
671            TOKENIZE
672
673            if (wavlen != 0)
674            {
675               outtoc->tracks[trknum].trktype = CD_TRACK_AUDIO;
676               outtoc->tracks[trknum].frames = wavlen/2352;
677               outinfo->offset[trknum] = wavoffs;
678               wavoffs = wavlen = 0;
679            }
680            else
681            {
682               outtoc->tracks[trknum].trktype = CD_TRACK_MODE1;
683               outtoc->tracks[trknum].datasize = 0;
684               outinfo->offset[trknum] = 0;
685            }
686            outtoc->tracks[trknum].subtype = CD_SUB_NONE;
687            outtoc->tracks[trknum].subsize = 0;
688            outtoc->tracks[trknum].pregap = 0;
689            outinfo->idx0offs[trknum] = -1;
690            outinfo->idx1offs[trknum] = 0;
691
692            strncpy(outinfo->fname[trknum], lastfname, 256); // default filename to the last one
693//              printf("trk %d: fname %s offset %d\n", trknum, &outinfo->fname[trknum][0], outinfo->offset[trknum]);
694
695            cdrom_convert_type_string_to_track_info(token, &outtoc->tracks[trknum]);
696            if (outtoc->tracks[trknum].datasize == 0)
697            {
698               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
699               return CHDERR_FILE_NOT_FOUND;
700            }
701
702            /* next (optional) token on the line is the subcode type */
703            TOKENIZE
704
705            cdrom_convert_subtype_string_to_track_info(token, &outtoc->tracks[trknum]);
706         }
707         else if (!strcmp(token, "INDEX"))   /* only in bin/cue files */
708         {
709            int idx, frames;
710
711            /* get index number */
712            TOKENIZE
713            idx = strtoul(token, NULL, 10);
714
715            /* get index */
716            TOKENIZE
717            frames = msf_to_frames( token );
718
719            if (idx == 0)
720            {
721               outinfo->idx0offs[trknum] = frames;
722            }
723            else if (idx == 1)
724            {
725               outinfo->idx1offs[trknum] = frames;
726               if ((outtoc->tracks[trknum].pregap == 0) && (outinfo->idx0offs[trknum] != -1))
727               {
728                  outtoc->tracks[trknum].pregap = frames - outinfo->idx0offs[trknum];
729               }
730            }
731         }
732         else if (!strcmp(token, "PREGAP"))
733         {
734            int frames;
735
736            /* get index */
737            TOKENIZE
738            frames = msf_to_frames( token );
739
740            outtoc->tracks[trknum].pregap = frames;
741         }
742         else if (!strcmp(token, "POSTGAP"))
743         {
744            int frames;
745
746            /* get index */
747            TOKENIZE
748            frames = msf_to_frames( token );
749
750            outtoc->tracks[trknum].postgap = frames;
751         }
752      }
753   }
754
755   /* close the input CUE */
756   fclose(infile);
757
758   /* store the number of tracks found */
759   outtoc->numtrks = trknum + 1;
760
761   /* now go over the files again and set the lengths */
762   for (trknum = 0; trknum < outtoc->numtrks; trknum++)
763   {
764      UINT64 tlen = 0;
765
766      // this is true for cue/bin and cue/iso, and we need it for cue/wav since .WAV is little-endian
767      if (outtoc->tracks[trknum].trktype == CD_TRACK_AUDIO)
768      {
769         outinfo->swap[trknum] = 1;
770      }
771
772      // don't do this for .WAV tracks, we already have their length and offset filled out
773      if (outinfo->offset[trknum] == 0)
774      {
775         // is this the last track?
776         if (trknum == (outtoc->numtrks-1))
777         {
778            /* if we have the same filename as the last track, do it that way */
779            if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum-1][0]))
780            {
781               tlen = get_file_size(outinfo->fname[trknum]);
782               if (tlen == 0)
783               {
784                  printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]);
785                  return CHDERR_FILE_NOT_FOUND;
786               }
787               outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize);
788               outtoc->tracks[trknum].frames = (tlen - outinfo->offset[trknum]) / (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
789            }
790            else   /* data files are different */
791            {
792               tlen = get_file_size(outinfo->fname[trknum]);
793               if (tlen == 0)
794               {
795                  printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]);
796                  return CHDERR_FILE_NOT_FOUND;
797               }
798               tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
799               outtoc->tracks[trknum].frames = tlen;
800               outinfo->offset[trknum] = 0;
801            }
802         }
803         else
804         {
805            /* if we have the same filename as the next track, do it that way */
806            if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum+1][0]))
807            {
808               outtoc->tracks[trknum].frames = outinfo->idx1offs[trknum+1] - outinfo->idx1offs[trknum];
809
810               if (trknum == 0)   // track 0 offset is 0
811               {
812                  outinfo->offset[trknum] = 0;
813               }
814               else
815               {
816                  outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize);
817               }
818
819               if (!outtoc->tracks[trknum].frames)
820               {
821                  printf("ERROR: unable to determine size of track %d, missing INDEX 01 markers?\n", trknum+1);
822                  return CHDERR_FILE_NOT_FOUND;
823               }
824            }
825            else   /* data files are different */
826            {
827               tlen = get_file_size(outinfo->fname[trknum]);
828               if (tlen == 0)
829               {
830                  printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum]);
831                  return CHDERR_FILE_NOT_FOUND;
832               }
833               tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
834               outtoc->tracks[trknum].frames = tlen;
835               outinfo->offset[trknum] = 0;
836            }
837         }
838      }
839      printf("trk %d: %d frames @ offset %d\n", trknum+1, outtoc->tracks[trknum].frames, outinfo->offset[trknum]);
840   }
841
842   return CHDERR_NONE;
843}
844
845/*-------------------------------------------------
846    chdcd_parse_toc - parse a CDRDAO format TOC file
847-------------------------------------------------*/
848
849chd_error chdcd_parse_toc(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
850{
851   FILE *infile;
852   int i, trknum;
853   static char token[128];
854
855   if (strstr(tocfname,".gdi"))
856   {
857      return chdcd_parse_gdi(tocfname, outtoc, outinfo);
858   }
859
860   if (strstr(tocfname,".cue"))
861   {
862      return chdcd_parse_cue(tocfname, outtoc, outinfo);
863   }
864
865   if (strstr(tocfname,".nrg"))
866   {
867      return chdcd_parse_nero(tocfname, outtoc, outinfo);
868   }
869
870   astring path = astring(tocfname);
871
872   infile = fopen(tocfname, "rt");   
873   path = get_file_path(path);
874
875   if (infile == (FILE *)NULL)
876   {
877      return CHDERR_FILE_NOT_FOUND;
878   }
879
880   /* clear structures */
881   memset(outtoc, 0, sizeof(cdrom_toc));
882   memset(outinfo, 0, sizeof(chdcd_track_input_info));
883
884   trknum = -1;
885
886   while (!feof(infile))
887   {
888      /* get the next line */
889      fgets(linebuffer, 511, infile);
890
891      /* if EOF didn't hit, keep going */
892      if (!feof(infile))
893      {
894         i = 0;
895
896         TOKENIZE
897
898         if ((!strcmp(token, "DATAFILE")) || (!strcmp(token, "AUDIOFILE")) || (!strcmp(token, "FILE")))
899         {
900            int f;
901
902            /* found the data file for a track */
903            TOKENIZE
904
905            /* keep the filename */
906            strncpy(outinfo->fname[trknum], path.cstr(), 256);
907            strncat(outinfo->fname[trknum], token, 256);
908
909            /* get either the offset or the length */
910            TOKENIZE
911
912            if (!strcmp(token, "SWAP"))
913            {
914               TOKENIZE
915
916               outinfo->swap[trknum] = 1;
917            }
918            else
919            {
920               outinfo->swap[trknum] = 0;
921            }
922
923            if (token[0] == '#')
924            {
925               /* it's a decimal offset, use it */
926               f = strtoul(&token[1], NULL, 10);
927            }
928            else if (isdigit((UINT8)token[0]))
929            {
930               /* convert the time to an offset */
931               f = msf_to_frames( token );
932
933               f *= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
934            }
935            else
936            {
937               f = 0;
938            }
939
940            outinfo->offset[trknum] = f;
941
942            TOKENIZE
943
944            if (isdigit((UINT8)token[0]))
945            {
946               // this could be the length or an offset from the previous field.
947               f = msf_to_frames( token );
948
949               TOKENIZE
950
951               if (isdigit((UINT8)token[0]))
952               {
953                  // it was an offset.
954                  f *= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
955
956                  outinfo->offset[trknum] += f;
957
958                  // this is the length.
959                  f = msf_to_frames( token );
960               }
961            }
962            else if( trknum == 0 && outinfo->offset[trknum] != 0 )
963            {
964               /* the 1st track might have a length with no offset */
965               f = outinfo->offset[trknum] / (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
966               outinfo->offset[trknum] = 0;
967            }
968            else
969            {
970               /* guesstimate the track length? */
971               f = 0;
972            }
973
974            outtoc->tracks[trknum].frames = f;
975         }
976         else if (!strcmp(token, "TRACK"))
977         {
978            trknum++;
979
980            /* next token on the line is the track type */
981            TOKENIZE
982
983            outtoc->tracks[trknum].trktype = CD_TRACK_MODE1;
984            outtoc->tracks[trknum].datasize = 0;
985            outtoc->tracks[trknum].subtype = CD_SUB_NONE;
986            outtoc->tracks[trknum].subsize = 0;
987
988            cdrom_convert_type_string_to_track_info(token, &outtoc->tracks[trknum]);
989            if (outtoc->tracks[trknum].datasize == 0)
990            {
991               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
992               return CHDERR_FILE_NOT_FOUND;
993            }
994
995            /* next (optional) token on the line is the subcode type */
996            TOKENIZE
997
998            cdrom_convert_subtype_string_to_track_info(token, &outtoc->tracks[trknum]);
999         }
1000         else if (!strcmp(token, "START"))
1001         {
1002            int frames;
1003
1004            /* get index */
1005            TOKENIZE
1006            frames = msf_to_frames( token );
1007
1008            outtoc->tracks[trknum].pregap = frames;
1009         }
1010      }
1011   }
1012
1013   /* close the input TOC */
1014   fclose(infile);
1015
1016   /* store the number of tracks found */
1017   outtoc->numtrks = trknum + 1;
1018
1019   return CHDERR_NONE;
1020}
src/lib/util/chdcd.h
r0r12670
1/***************************************************************************
2
3    CDRDAO TOC parser for CHD compression frontend
4
5    Copyright Nicola Salmoria and the MAME Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8***************************************************************************/
9
10#pragma once
11
12#ifndef __CHDCD_H__
13#define __CHDCD_H__
14
15#include "cdrom.h"
16
17typedef struct _chdcd_track_input_info chdcd_track_input_info;
18struct _chdcd_track_input_info   /* used only at compression time */
19{
20   char fname[CD_MAX_TRACKS][256];   /* filename for each track */
21   UINT32 offset[CD_MAX_TRACKS];   /* offset in the data file for each track */
22   int swap[CD_MAX_TRACKS];   /* data needs to be byte swapped */
23   UINT32 idx0offs[CD_MAX_TRACKS];
24   UINT32 idx1offs[CD_MAX_TRACKS];
25};
26
27
28chd_error chdcd_parse_toc(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo);
29
30#endif   /* __CHDCD_H__ */

Previous 15994 Revisions Next


© 1998-2018 The MESS Team