// this function tries to get the .cue file of the given .bin // the necessary data is put into the ti (trackinformation)-array static int parsecue(const char *isofile) { char cuename[MAXPATHLEN]; char filepath[MAXPATHLEN]; char *incue_fname; FILE *fi; char *token; char time[20]; char *tmp; char linebuf[256], tmpb[256], dummy[256]; unsigned int incue_max_len; unsigned int t, file_len, mode, sector_offs; unsigned int sector_size = 2352; numtracks = 0; // copy name of the iso and change extension from .bin to .cue strncpy(cuename, isofile, sizeof(cuename)); cuename[MAXPATHLEN - 1] = '\0'; if (strlen(cuename) >= 4) { strcpy(cuename + strlen(cuename) - 4, ".cue"); } else { return -1; } if ((fi = fopen(cuename, "r")) == NULL) { return -1; } // Some stupid tutorials wrongly tell users to use cdrdao to rip a // "bin/cue" image, which is in fact a "bin/toc" image. So let's check // that... if (fgets(linebuf, sizeof(linebuf), fi) != NULL) { if (!strncmp(linebuf, "CD_ROM_XA", 9)) { // Don't proceed further, as this is actually a .toc file rather // than a .cue file. fclose(fi); return parsetoc(isofile); } fseek(fi, 0, SEEK_SET); } // build a path for files referenced in .cue strncpy(filepath, cuename, sizeof(filepath)); tmp = strrchr(filepath, '/'); if (tmp == NULL) tmp = strrchr(filepath, '\\'); if (tmp != NULL) tmp++; else tmp = filepath; *tmp = 0; filepath[sizeof(filepath) - 1] = 0; incue_fname = tmp; incue_max_len = sizeof(filepath) - (tmp - filepath) - 1; memset(&ti, 0, sizeof(ti)); file_len = 0; sector_offs = 2 * 75; while (fgets(linebuf, sizeof(linebuf), fi) != NULL) { strncpy(dummy, linebuf, sizeof(linebuf)); token = strtok(dummy, " "); if (token == NULL) { continue; } if (!strcmp(token, "TRACK")) { numtracks++; sector_size = 0; if (strstr(linebuf, "AUDIO") != NULL) { ti[numtracks].type = CDDA; sector_size = 2352; } else if (sscanf(linebuf, " TRACK %u MODE%u/%u", &t, &mode, §or_size) == 3) ti[numtracks].type = DATA; else { SysPrintf(".cue: failed to parse TRACK\n"); ti[numtracks].type = numtracks == 1 ? DATA : CDDA; } if (sector_size == 0) sector_size = 2352; } else if (!strcmp(token, "INDEX")) { if (sscanf(linebuf, " INDEX %02d %8s", &t, time) != 2) SysPrintf(".cue: failed to parse INDEX\n"); tok2msf(time, (char *)&ti[numtracks].start); t = msf2sec(ti[numtracks].start); ti[numtracks].start_offset = t * sector_size; t += sector_offs; sec2msf(t, ti[numtracks].start); // default track length to file length t = file_len - ti[numtracks].start_offset / sector_size; sec2msf(t, ti[numtracks].length); if (numtracks > 1 && ti[numtracks].handle == NULL) { // this track uses the same file as the last, // start of this track is last track's end t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start); sec2msf(t, ti[numtracks - 1].length); } if (numtracks > 1 && pregapOffset == -1) pregapOffset = ti[numtracks].start_offset / sector_size; } else if (!strcmp(token, "PREGAP")) { if (sscanf(linebuf, " PREGAP %8s", time) == 1) { tok2msf(time, dummy); sector_offs += msf2sec(dummy); } pregapOffset = -1; // mark to fill track start_offset } else if (!strcmp(token, "FILE")) { t = sscanf(linebuf, " FILE \"%256[^\"]\"", tmpb); if (t != 1) sscanf(linebuf, " FILE %256s", tmpb); // absolute path? ti[numtracks + 1].handle = fopen(tmpb, "rb"); if (ti[numtracks + 1].handle == NULL) { // relative to .cue? tmp = strrchr(tmpb, '\\'); if (tmp == NULL) tmp = strrchr(tmpb, '/'); if (tmp != NULL) tmp++; else tmp = tmpb; strncpy(incue_fname, tmp, incue_max_len); ti[numtracks + 1].handle = fopen(filepath, "rb"); } // update global offset if this is not first file in this .cue if (numtracks + 1 > 1) { multifile = 1; sector_offs += file_len; } file_len = 0; if (ti[numtracks + 1].handle == NULL) { SysPrintf(_("\ncould not open: %s\n"), filepath); continue; } fseek(ti[numtracks + 1].handle, 0, SEEK_END); file_len = ftell(ti[numtracks + 1].handle) / 2352; if (numtracks == 0 && strlen(isofile) >= 4 && strcmp(isofile + strlen(isofile) - 4, ".cue") == 0) { // user selected .cue as image file, use it's data track instead fclose(cdHandle); cdHandle = fopen(filepath, "rb"); } } } fclose(fi); return 0; }
// this function tries to get the .cue file of the given .bin // the necessary data is put into the ti (trackinformation)-array static int parsecue(const char *isofile) { char cuename[MAXPATHLEN]; FILE *fi; char *token; char time[20]; char *tmp; char linebuf[256], dummy[256]; unsigned int t; numtracks = 0; // copy name of the iso and change extension from .bin to .cue strncpy(cuename, isofile, sizeof(cuename)); cuename[MAXPATHLEN - 1] = '\0'; if (strlen(cuename) >= 4) { strcpy(cuename + strlen(cuename) - 4, ".cue"); } else { return -1; } if ((fi = fopen(cuename, "r")) == NULL) { return -1; } // Some stupid tutorials wrongly tell users to use cdrdao to rip a // "bin/cue" image, which is in fact a "bin/toc" image. So let's check // that... if (fgets(linebuf, sizeof(linebuf), fi) != NULL) { if (!strncmp(linebuf, "CD_ROM_XA", 9)) { // Don't proceed further, as this is actually a .toc file rather // than a .cue file. fclose(fi); return parsetoc(isofile); } fseek(fi, 0, SEEK_SET); } memset(&ti, 0, sizeof(ti)); while (fgets(linebuf, sizeof(linebuf), fi) != NULL) { strncpy(dummy, linebuf, sizeof(linebuf)); token = strtok(dummy, " "); if (token == NULL) { continue; } if (!strcmp(token, "TRACK")){ numtracks++; if (strstr(linebuf, "AUDIO") != NULL) { ti[numtracks].type = CDDA; } else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) { ti[numtracks].type = DATA; } } else if (!strcmp(token, "INDEX")) { tmp = strstr(linebuf, "INDEX"); if (tmp != NULL) { tmp += strlen("INDEX") + 3; // 3 - space + numeric index while (*tmp == ' ') tmp++; if (*tmp != '\n') sscanf(tmp, "%8s", time); } tok2msf((char *)&time, (char *)&ti[numtracks].start); t = msf2sec(ti[numtracks].start) + 2 * 75; sec2msf(t, ti[numtracks].start); // If we've already seen another track, this is its end if (numtracks > 1) { t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start); sec2msf(t, ti[numtracks - 1].length); } } } fclose(fi); // Fill out the last track's end based on size if (numtracks >= 1) { fseek(cdHandle, 0, SEEK_END); t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75; sec2msf(t, ti[numtracks].length); } return 0; }
// this function tries to get the .toc file of the given .bin // the necessary data is put into the ti (trackinformation)-array static int parsetoc(const char *isofile) { char tocname[MAXPATHLEN]; FILE *fi; char linebuf[256], tmp[256], name[256]; char *token; char time[20], time2[20]; unsigned int t, sector_offs, sector_size; unsigned int current_zero_gap = 0; numtracks = 0; // copy name of the iso and change extension from .bin to .toc strncpy(tocname, isofile, sizeof(tocname)); tocname[MAXPATHLEN - 1] = '\0'; if (strlen(tocname) >= 4) { strcpy(tocname + strlen(tocname) - 4, ".toc"); } else { return -1; } if ((fi = fopen(tocname, "r")) == NULL) { // try changing extension to .cue (to satisfy some stupid tutorials) strcpy(tocname + strlen(tocname) - 4, ".cue"); if ((fi = fopen(tocname, "r")) == NULL) { // if filename is image.toc.bin, try removing .bin (for Brasero) strcpy(tocname, isofile); t = strlen(tocname); if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) { tocname[t - 4] = '\0'; if ((fi = fopen(tocname, "r")) == NULL) { return -1; } } else { return -1; } } // check if it's really a TOC named as a .cue fgets(linebuf, sizeof(linebuf), fi); token = strtok(linebuf, " "); if (token && strncmp(token, "CD", 2) != 0 && strcmp(token, "CATALOG") != 0) { fclose(fi); return -1; } fseek(fi, 0, SEEK_SET); } memset(&ti, 0, sizeof(ti)); cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio sector_size = CD_FRAMESIZE_RAW; sector_offs = 2 * 75; // parse the .toc file while (fgets(linebuf, sizeof(linebuf), fi) != NULL) { // search for tracks strncpy(tmp, linebuf, sizeof(linebuf)); token = strtok(tmp, " "); if (token == NULL) continue; if (!strcmp(token, "TRACK")) { sector_offs += current_zero_gap; current_zero_gap = 0; // get type of track token = strtok(NULL, " "); numtracks++; if (!strncmp(token, "MODE2_RAW", 9)) { ti[numtracks].type = DATA; sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0 // check if this image contains mixed subchannel data token = strtok(NULL, " "); if (token != NULL && !strncmp(token, "RW", 2)) { sector_size = CD_FRAMESIZE_RAW + SUB_FRAMESIZE; subChanMixed = TRUE; if (!strncmp(token, "RW_RAW", 6)) subChanRaw = TRUE; } } else if (!strncmp(token, "AUDIO", 5)) { ti[numtracks].type = CDDA; } } else if (!strcmp(token, "DATAFILE")) { if (ti[numtracks].type == CDDA) { sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2); ti[numtracks].start_offset = t; t = t / sector_size + sector_offs; sec2msf(t, (char *)&ti[numtracks].start); tok2msf((char *)&time2, (char *)&ti[numtracks].length); } else { sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time); tok2msf((char *)&time, (char *)&ti[numtracks].length); } } else if (!strcmp(token, "FILE")) { sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2); tok2msf((char *)&time, (char *)&ti[numtracks].start); t += msf2sec(ti[numtracks].start) * sector_size; ti[numtracks].start_offset = t; t = t / sector_size + sector_offs; sec2msf(t, (char *)&ti[numtracks].start); tok2msf((char *)&time2, (char *)&ti[numtracks].length); } else if (!strcmp(token, "ZERO") || !strcmp(token, "SILENCE")) { // skip unneeded optional fields while (token != NULL) { token = strtok(NULL, " "); if (strchr(token, ':') != NULL) break; } if (token != NULL) { tok2msf(token, tmp); current_zero_gap = msf2sec(tmp); } if (numtracks > 1) { t = ti[numtracks - 1].start_offset; t /= sector_size; pregapOffset = t + msf2sec(ti[numtracks - 1].length); } } else if (!strcmp(token, "START")) { token = strtok(NULL, " "); if (token != NULL && strchr(token, ':')) { tok2msf(token, tmp); t = msf2sec(tmp); ti[numtracks].start_offset += (t - current_zero_gap) * sector_size; t = msf2sec(ti[numtracks].start) + t; sec2msf(t, (char *)&ti[numtracks].start); } } } fclose(fi); return 0; }
// this function tries to get the .toc file of the given .bin // the necessary data is put into the ti (trackinformation)-array static int parsetoc(const char *isofile) { char tocname[MAXPATHLEN]; FILE *fi; char linebuf[256], dummy[256], name[256]; char *token; char time[20], time2[20]; unsigned int t; numtracks = 0; // copy name of the iso and change extension from .bin to .toc strncpy(tocname, isofile, sizeof(tocname)); tocname[MAXPATHLEN - 1] = '\0'; if (strlen(tocname) >= 4) { strcpy(tocname + strlen(tocname) - 4, ".toc"); } else { return -1; } if ((fi = fopen(tocname, "r")) == NULL) { // try changing extension to .cue (to satisfy some stupid tutorials) strcpy(tocname + strlen(tocname) - 4, ".cue"); if ((fi = fopen(tocname, "r")) == NULL) { // if filename is image.toc.bin, try removing .bin (for Brasero) strcpy(tocname, isofile); t = strlen(tocname); if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) { tocname[t - 4] = '\0'; if ((fi = fopen(tocname, "r")) == NULL) { return -1; } } else { return -1; } } } memset(&ti, 0, sizeof(ti)); cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio // parse the .toc file while (fgets(linebuf, sizeof(linebuf), fi) != NULL) { // search for tracks strncpy(dummy, linebuf, sizeof(linebuf)); token = strtok(dummy, " "); if (token == NULL) continue; if (!strcmp(token, "TRACK")) { // get type of track token = strtok(NULL, " "); numtracks++; if (!strncmp(token, "MODE2_RAW", 9)) { ti[numtracks].type = DATA; sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0 // check if this image contains mixed subchannel data token = strtok(NULL, " "); if (token != NULL && !strncmp(token, "RW_RAW", 6)) { subChanMixed = TRUE; subChanRaw = TRUE; } } else if (!strncmp(token, "AUDIO", 5)) { ti[numtracks].type = CDDA; } } else if (!strcmp(token, "DATAFILE")) { if (ti[numtracks].type == CDDA) { sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2); t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0); t += 2 * 75; sec2msf(t, (char *)&ti[numtracks].start); tok2msf((char *)&time2, (char *)&ti[numtracks].length); } else { sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time); tok2msf((char *)&time, (char *)&ti[numtracks].length); } } else if (!strcmp(token, "FILE")) { sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2); tok2msf((char *)&time, (char *)&ti[numtracks].start); t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0); t += msf2sec(ti[numtracks].start) + 2 * 75; sec2msf(t, (char *)&ti[numtracks].start); tok2msf((char *)&time2, (char *)&ti[numtracks].length); } } fclose(fi); return 0; }