src/tools/chdcd.c
| r12669 | r12670 | |
| 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 | | |
| 32 | | static char linebuffer[512]; |
| 33 | | |
| 34 | | |
| 35 | | |
| 36 | | /*************************************************************************** |
| 37 | | IMPLEMENTATION |
| 38 | | ***************************************************************************/ |
| 39 | | |
| 40 | | static 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 | | |
| 55 | | static 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 | | |
| 73 | | static 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 | | |
| 117 | | static 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 | | -------------------------------------------------*/ |
| 143 | | static 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 | | |
| 263 | | UINT16 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 | | |
| 275 | | UINT32 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 | | |
| 287 | | UINT64 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 | | |
| 307 | | chd_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 | | |
| 455 | | static 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 | | |
| 587 | | chd_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 | | |
| 849 | | chd_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.c
| r0 | r12670 | |
| 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 | |
| 32 | static char linebuffer[512]; |
| 33 | |
| 34 | |
| 35 | |
| 36 | /*************************************************************************** |
| 37 | IMPLEMENTATION |
| 38 | ***************************************************************************/ |
| 39 | |
| 40 | static 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 | |
| 55 | static 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 | |
| 73 | static 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 | |
| 117 | static 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 | -------------------------------------------------*/ |
| 143 | static 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 | |
| 263 | UINT16 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 | |
| 275 | UINT32 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 | |
| 287 | UINT64 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 | |
| 307 | chd_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 | |
| 455 | static 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 | |
| 587 | chd_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 | |
| 849 | chd_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 | } |