diff options
Diffstat (limited to 'contrib/libcdio/image')
-rw-r--r-- | contrib/libcdio/image/Makefile.am | 3 | ||||
-rw-r--r-- | contrib/libcdio/image/bincue.c | 1214 | ||||
-rw-r--r-- | contrib/libcdio/image/cdrdao.c | 1198 | ||||
-rw-r--r-- | contrib/libcdio/image/nrg.c | 1274 | ||||
-rw-r--r-- | contrib/libcdio/image/nrg.h | 115 |
5 files changed, 3804 insertions, 0 deletions
diff --git a/contrib/libcdio/image/Makefile.am b/contrib/libcdio/image/Makefile.am new file mode 100644 index 000000000..e7a09e980 --- /dev/null +++ b/contrib/libcdio/image/Makefile.am @@ -0,0 +1,3 @@ +include $(top_srcdir)/misc/Makefile.common + +EXTRA_DIST = bincue.c nrg.c diff --git a/contrib/libcdio/image/bincue.c b/contrib/libcdio/image/bincue.c new file mode 100644 index 000000000..56f0e151c --- /dev/null +++ b/contrib/libcdio/image/bincue.c @@ -0,0 +1,1214 @@ +/* + $Id: bincue.c,v 1.2 2005/01/01 02:43:58 rockyb Exp $ + + Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com> + Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org> + cue parsing routine adapted from cuetools + Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* This code implements low-level access functions for a CD images + residing inside a disk file (*.bin) and its associated cue sheet. + (*.cue). +*/ + +static const char _rcsid[] = "$Id: bincue.c,v 1.2 2005/01/01 02:43:58 rockyb Exp $"; + +#include "image.h" +#include "cdio_assert.h" +#include "cdio_private.h" +#include "_cdio_stdio.h" + +#include <cdio/logging.h> +#include <cdio/util.h> +#include <cdio/version.h> + +#ifdef HAVE_STDIO_H +#include <stdio.h> +#endif +#ifdef HAVE_STDLIB_H +#include <stdlib.h> +#endif +#ifdef HAVE_STRING_H +#include <string.h> +#endif +#ifdef HAVE_STRINGS_H +#include <strings.h> +#endif +#ifdef HAVE_ERRNO_H +#include <errno.h> +#endif +#ifdef HAVE_GLOB_H +#include <glob.h> +#endif +#include <ctype.h> + +#include "portable.h" +/* reader */ + +#define DEFAULT_CDIO_DEVICE "videocd.bin" +#define DEFAULT_CDIO_CUE "videocd.cue" + +typedef struct { + /* Things common to all drivers like this. + This must be first. */ + generic_img_private_t gen; + internal_position_t pos; + + char *psz_cue_name; + char *psz_mcn; /* Media Catalog Number (5.22.3) + exactly 13 bytes */ + track_info_t tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track + add 1 for leadout. */ + discmode_t disc_mode; +} _img_private_t; + +static uint32_t _stat_size_bincue (void *user_data); +static bool parse_cuefile (_img_private_t *cd, const char *toc_name); + +#define NEED_MEDIA_EJECT_IMAGE +#include "image_common.h" + +/*! + Initialize image structures. + */ +static bool +_init_bincue (_img_private_t *env) +{ + lsn_t lead_lsn; + + if (env->gen.init) + return false; + + if (!(env->gen.data_source = cdio_stdio_new (env->gen.source_name))) { + cdio_warn ("init failed"); + return false; + } + + /* Have to set init before calling _stat_size_bincue() or we will + get into infinite recursion calling passing right here. + */ + env->gen.init = true; + env->gen.i_first_track = 1; + env->psz_mcn = NULL; + env->disc_mode = CDIO_DISC_MODE_NO_INFO; + + cdtext_init (&(env->gen.cdtext)); + + lead_lsn = _stat_size_bincue( (_img_private_t *) env); + + if (-1 == lead_lsn) return false; + + if ((env->psz_cue_name == NULL)) return false; + + /* Read in CUE sheet. */ + if ( !parse_cuefile(env, env->psz_cue_name) ) return false; + + /* Fake out leadout track and sector count for last track*/ + cdio_lsn_to_msf (lead_lsn, &env->tocent[env->gen.i_tracks].start_msf); + env->tocent[env->gen.i_tracks].start_lba = cdio_lsn_to_lba(lead_lsn); + env->tocent[env->gen.i_tracks - env->gen.i_first_track].sec_count = + cdio_lsn_to_lba(lead_lsn - + env->tocent[env->gen.i_tracks - env->gen.i_first_track].start_lba); + + return true; +} + +/*! + Reads into buf the next size bytes. + Returns -1 on error. + Would be libc's seek() but we have to adjust for the extra track header + information in each sector. +*/ +static off_t +_lseek_bincue (void *user_data, off_t offset, int whence) +{ + _img_private_t *env = user_data; + + /* real_offset is the real byte offset inside the disk image + The number below was determined empirically. I'm guessing + the 1st 24 bytes of a bin file are used for something. + */ + off_t real_offset=0; + + unsigned int i; + + env->pos.lba = 0; + for (i=0; i<env->gen.i_tracks; i++) { + track_info_t *this_track=&(env->tocent[i]); + env->pos.index = i; + if ( (this_track->sec_count*this_track->datasize) >= offset) { + int blocks = offset / this_track->datasize; + int rem = offset % this_track->datasize; + int block_offset = blocks * this_track->blocksize; + real_offset += block_offset + rem; + env->pos.buff_offset = rem; + env->pos.lba += blocks; + break; + } + real_offset += this_track->sec_count*this_track->blocksize; + offset -= this_track->sec_count*this_track->datasize; + env->pos.lba += this_track->sec_count; + } + + if (i==env->gen.i_tracks) { + cdio_warn ("seeking outside range of disk image"); + return -1; + } else { + real_offset += env->tocent[i].datastart; + return cdio_stream_seek(env->gen.data_source, real_offset, whence); + } +} + +/*! + Reads into buf the next size bytes. + Returns -1 on error. + FIXME: + At present we assume a read doesn't cross sector or track + boundaries. +*/ +static ssize_t +_read_bincue (void *user_data, void *data, size_t size) +{ + _img_private_t *env = user_data; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + char *p = data; + ssize_t final_size=0; + ssize_t this_size; + track_info_t *this_track=&(env->tocent[env->pos.index]); + ssize_t skip_size = this_track->datastart + this_track->endsize; + + while (size > 0) { + long int rem = this_track->datasize - env->pos.buff_offset; + if ((long int) size <= rem) { + this_size = cdio_stream_read(env->gen.data_source, buf, size, 1); + final_size += this_size; + memcpy (p, buf, this_size); + break; + } + + /* Finish off reading this sector. */ + cdio_warn ("Reading across block boundaries not finished"); + + size -= rem; + this_size = cdio_stream_read(env->gen.data_source, buf, rem, 1); + final_size += this_size; + memcpy (p, buf, this_size); + p += this_size; + this_size = cdio_stream_read(env->gen.data_source, buf, rem, 1); + + /* Skip over stuff at end of this sector and the beginning of the next. + */ + cdio_stream_read(env->gen.data_source, buf, skip_size, 1); + + /* Get ready to read another sector. */ + env->pos.buff_offset=0; + env->pos.lba++; + + /* Have gone into next track. */ + if (env->pos.lba >= env->tocent[env->pos.index+1].start_lba) { + env->pos.index++; + this_track=&(env->tocent[env->pos.index]); + skip_size = this_track->datastart + this_track->endsize; + } + } + return final_size; +} + +/*! + Return the size of the CD in logical block address (LBA) units. + */ +static uint32_t +_stat_size_bincue (void *user_data) +{ + _img_private_t *env = user_data; + long size; + + size = cdio_stream_stat (env->gen.data_source); + + if (size % CDIO_CD_FRAMESIZE_RAW) + { + cdio_warn ("image %s size (%ld) not multiple of blocksize (%d)", + env->gen.source_name, size, CDIO_CD_FRAMESIZE_RAW); + if (size % M2RAW_SECTOR_SIZE == 0) + cdio_warn ("this may be a 2336-type disc image"); + else if (size % CDIO_CD_FRAMESIZE_RAW == 0) + cdio_warn ("this may be a 2352-type disc image"); + /* exit (EXIT_FAILURE); */ + } + + size /= CDIO_CD_FRAMESIZE_RAW; + + return size; +} + +#define MAXLINE 4096 /* maximum line length + 1 */ + +static bool +parse_cuefile (_img_private_t *cd, const char *psz_cue_name) +{ + /* The below declarations may be common in other image-parse routines. */ + FILE *fp; + char psz_line[MAXLINE]; /* text of current line read in file fp. */ + unsigned int i_line=0; /* line number in file of psz_line. */ + int i = -1; /* Position in tocent. Same as + cd->gen.i_tracks - 1 */ + char *psz_keyword, *psz_field; + cdio_log_level_t log_level = (NULL == cd) ? CDIO_LOG_INFO : CDIO_LOG_WARN; + cdtext_field_t cdtext_key; + + /* The below declarations may be unique to this image-parse routine. */ + int start_index; + bool b_first_index_for_track=false; + + if (NULL == psz_cue_name) + return false; + + fp = fopen (psz_cue_name, "r"); + if (fp == NULL) { + cdio_log(log_level, "error opening %s for reading: %s", + psz_cue_name, strerror(errno)); + return false; + } + + if (cd) { + cd->gen.i_tracks=0; + cd->gen.i_first_track=1; + cd->gen.b_cdtext_init = true; + cd->gen.b_cdtext_error = false; + cd->psz_mcn=NULL; + } + + while ((fgets(psz_line, MAXLINE, fp)) != NULL) { + + i_line++; + + if (NULL != (psz_keyword = strtok (psz_line, " \t\n\r"))) { + /* REM remarks ... */ + if (0 == strcmp ("REM", psz_keyword)) { + ; + + /* global section */ + /* CATALOG ddddddddddddd */ + } else if (0 == strcmp ("CATALOG", psz_keyword)) { + if (-1 == i) { + if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) { + cdio_log(log_level, + "%s line %d after word CATALOG: ", + psz_cue_name, i_line); + cdio_log(log_level, + "expecting 13-digit media catalog number, got nothing."); + goto err_exit; + } + if (strlen(psz_field) != 13) { + cdio_log(log_level, + "%s line %d after word CATALOG: ", + psz_cue_name, i_line); + cdio_log(log_level, + "Token %s has length %ld. Should be 13 digits.", + psz_field, (long int) strlen(psz_field)); + goto err_exit; + } else { + /* Check that we have all digits*/ + unsigned int i; + for (i=0; i<13; i++) { + if (!isdigit(psz_field[i])) { + cdio_log(log_level, + "%s line %d after word CATALOG:", + psz_cue_name, i_line); + cdio_log(log_level, + "Character \"%c\" at postition %i of token \"%s\" " + "is not all digits.", + psz_field[i], i+1, psz_field); + goto err_exit; + } + } + } + + if (cd) cd->psz_mcn = strdup (psz_field); + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + } else { + goto not_in_global_section; + } + + /* FILE "<filename>" <BINARY|WAVE|other?> */ + } else if (0 == strcmp ("FILE", psz_keyword)) { + if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) { + if (cd) cd->tocent[i + 1].filename = strdup (psz_field); + } else { + goto format_error; + } + + /* TRACK N <mode> */ + } else if (0 == strcmp ("TRACK", psz_keyword)) { + int i_track; + + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + if (1!=sscanf(psz_field, "%d", &i_track)) { + cdio_log(log_level, + "%s line %d after word TRACK:", + psz_cue_name, i_line); + cdio_log(log_level, + "Expecting a track number, got %s", psz_field); + goto err_exit; + } + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + track_info_t *this_track=NULL; + + if (cd) { + this_track = &(cd->tocent[cd->gen.i_tracks]); + this_track->track_num = cd->gen.i_tracks; + this_track->num_indices = 0; + b_first_index_for_track = false; + cdtext_init (&(cd->gen.cdtext_track[cd->gen.i_tracks])); + cd->gen.i_tracks++; + } + i++; + + if (0 == strcmp ("AUDIO", psz_field)) { + if (cd) { + this_track->mode = AUDIO; + this_track->blocksize = CDIO_CD_FRAMESIZE_RAW; + this_track->datasize = CDIO_CD_FRAMESIZE_RAW; + this_track->datastart = 0; + this_track->endsize = 0; + this_track->track_format = TRACK_FORMAT_AUDIO; + this_track->track_green = false; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_DA; + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DATA: + case CDIO_DISC_MODE_CD_XA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE1/2048", psz_field)) { + if (cd) { + this_track->mode = MODE1; + this_track->blocksize = 2048; + this_track->track_format= TRACK_FORMAT_DATA; + this_track->track_green = false; + /* Is the below correct? */ + this_track->datastart = 0; + this_track->datasize = CDIO_CD_FRAMESIZE; + this_track->endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_DATA; + break; + case CDIO_DISC_MODE_CD_DATA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_XA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE1/2352", psz_field)) { + if (cd) { + this_track->blocksize = 2352; + this_track->track_format= TRACK_FORMAT_DATA; + this_track->track_green = false; + this_track->datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + this_track->datasize = CDIO_CD_FRAMESIZE; + this_track->endsize = CDIO_CD_EDC_SIZE + + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE; + this_track->mode = MODE1_RAW; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_DATA; + break; + case CDIO_DISC_MODE_CD_DATA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_XA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2/2336", psz_field)) { + if (cd) { + this_track->blocksize = 2336; + this_track->track_format= TRACK_FORMAT_XA; + this_track->track_green = true; + this_track->mode = MODE2; + this_track->datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + this_track->datasize = M2RAW_SECTOR_SIZE; + this_track->endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_DATA; + break; + case CDIO_DISC_MODE_CD_DATA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_XA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2/2048", psz_field)) { + if (cd) { + this_track->blocksize = 2048; + this_track->track_format= TRACK_FORMAT_XA; + this_track->track_green = true; + this_track->mode = MODE2_FORM1; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2/2324", psz_field)) { + if (cd) { + this_track->blocksize = 2324; + this_track->track_format= TRACK_FORMAT_XA; + this_track->track_green = true; + this_track->mode = MODE2_FORM2; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2/2336", psz_field)) { + if (cd) { + this_track->blocksize = 2336; + this_track->track_format= TRACK_FORMAT_XA; + this_track->track_green = true; + this_track->mode = MODE2_FORM_MIX; + this_track->datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + this_track->datasize = M2RAW_SECTOR_SIZE; + this_track->endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2/2352", psz_field)) { + if (cd) { + this_track->blocksize = 2352; + this_track->track_format= TRACK_FORMAT_XA; + this_track->track_green = true; + this_track->mode = MODE2_RAW; + this_track->datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE; + this_track->datasize = CDIO_CD_FRAMESIZE; + this_track->endsize = CDIO_CD_SYNC_SIZE + CDIO_CD_ECC_SIZE; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else { + cdio_log(log_level, + "%s line %d after word TRACK:", + psz_cue_name, i_line); + cdio_log(log_level, + "Unknown track mode %s", psz_field); + goto err_exit; + } + } else { + goto format_error; + } + + /* FLAGS flag1 flag2 ... */ + } else if (0 == strcmp ("FLAGS", psz_keyword)) { + if (0 <= i) { + while (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + if (0 == strcmp ("PRE", psz_field)) { + if (cd) cd->tocent[i].flags |= PRE_EMPHASIS; + } else if (0 == strcmp ("DCP", psz_field)) { + if (cd) cd->tocent[i].flags |= COPY_PERMITTED; + } else if (0 == strcmp ("4CH", psz_field)) { + if (cd) cd->tocent[i].flags |= FOUR_CHANNEL_AUDIO; + } else if (0 == strcmp ("SCMS", psz_field)) { + if (cd) cd->tocent[i].flags |= SCMS; + } else { + goto format_error; + } + } + } else { + goto format_error; + } + + /* ISRC CCOOOYYSSSSS */ + } else if (0 == strcmp ("ISRC", psz_keyword)) { + if (0 <= i) { + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + if (cd) cd->tocent[i].isrc = strdup (psz_field); + } else { + goto format_error; + } + } else { + goto in_global_section; + } + + /* PREGAP MM:SS:FF */ + } else if (0 == strcmp ("PREGAP", psz_keyword)) { + if (0 <= i) { + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + lba_t lba = cdio_lsn_to_lba(cdio_mmssff_to_lba (psz_field)); + if (CDIO_INVALID_LBA == lba) { + cdio_log(log_level, "%s line %d: after word PREGAP:", + psz_cue_name, i_line); + cdio_log(log_level, "Invalid MSF string %s", + psz_field); + goto err_exit; + } + if (cd) { + cd->tocent[i].pregap = lba; + } + } else { + goto format_error; + } if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + } else { + goto in_global_section; + } + + /* INDEX [##] MM:SS:FF */ + } else if (0 == strcmp ("INDEX", psz_keyword)) { + if (0 <= i) { + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) + if (1!=sscanf(psz_field, "%d", &start_index)) { + cdio_log(log_level, + "%s line %d after word INDEX:", + psz_cue_name, i_line); + cdio_log(log_level, + "expecting an index number, got %s", + psz_field); + goto err_exit; + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + lba_t lba = cdio_mmssff_to_lba (psz_field); + if (CDIO_INVALID_LBA == lba) { + cdio_log(log_level, "%s line %d: after word INDEX:", + psz_cue_name, i_line); + cdio_log(log_level, "Invalid MSF string %s", + psz_field); + goto err_exit; + } + if (cd) { +#if FIXED_ME + cd->tocent[i].indexes[cd->tocent[i].nindex++] = lba; +#else + track_info_t *this_track= + &(cd->tocent[cd->gen.i_tracks - cd->gen.i_first_track]); + + if (start_index != 0) { + if (!b_first_index_for_track) { + lba += CDIO_PREGAP_SECTORS; + cdio_lba_to_msf(lba, &(this_track->start_msf)); + b_first_index_for_track = true; + this_track->start_lba = lba; + } + + if (cd->gen.i_tracks > 1) { + /* Figure out number of sectors for previous track */ + track_info_t *prev_track=&(cd->tocent[cd->gen.i_tracks-2]); + if ( this_track->start_lba < prev_track->start_lba ) { + cdio_log (log_level, + "track %d at LBA %lu starts before track %d at LBA %lu", + cd->gen.i_tracks, + (unsigned long int) this_track->start_lba, + cd->gen.i_tracks, + (unsigned long int) prev_track->start_lba); + prev_track->sec_count = 0; + } else if ( this_track->start_lba >= prev_track->start_lba + + CDIO_PREGAP_SECTORS ) { + prev_track->sec_count = this_track->start_lba - + prev_track->start_lba - CDIO_PREGAP_SECTORS ; + } else { + cdio_log (log_level, + "%lu fewer than pregap (%d) sectors in track %d", + (long unsigned int) + this_track->start_lba - prev_track->start_lba, + CDIO_PREGAP_SECTORS, + cd->gen.i_tracks); + /* Include pregap portion in sec_count. Maybe the pregap + was omitted. */ + prev_track->sec_count = this_track->start_lba - + prev_track->start_lba; + } + } + this_track->num_indices++; + } + } +#endif + } else { + goto format_error; + } + } else { + goto in_global_section; + } + + /* CD-TEXT */ + } else if ( CDTEXT_INVALID != + (cdtext_key = cdtext_is_keyword (psz_keyword)) ) { + if (-1 == i) { + if (cd) { + cdtext_set (cdtext_key, + strtok (NULL, "\"\t\n\r"), + &(cd->gen.cdtext)); + } + } else { + if (cd) { + cdtext_set (cdtext_key, strtok (NULL, "\"\t\n\r"), + &(cd->gen.cdtext_track[i])); + } + } + + /* unrecognized line */ + } else { + cdio_log(log_level, "%s line %d: warning: unrecognized keyword: %s", + psz_cue_name, i_line, psz_keyword); + goto err_exit; + } + } + } + + if (NULL != cd) { + cd->gen.toc_init = true; + } + + fclose (fp); + return true; + + format_error: + cdio_log(log_level, "%s line %d after word %s", + psz_cue_name, i_line, psz_keyword); + goto err_exit; + + in_global_section: + cdio_log(log_level, "%s line %d: word %s not allowed in global section", + psz_cue_name, i_line, psz_keyword); + goto err_exit; + + not_in_global_section: + cdio_log(log_level, "%s line %d: word %s only allowed in global section", + psz_cue_name, i_line, psz_keyword); + + err_exit: + fclose (fp); + return false; + +} + +/*! + Reads a single audio sector from CD device into data starting + from lsn. Returns 0 if no error. + */ +static int +_read_audio_sectors_bincue (void *user_data, void *data, lsn_t lsn, + unsigned int nblocks) +{ + _img_private_t *env = user_data; + int ret; + + /* Why the adjustment of 272, I don't know. It seems to work though */ + if (lsn != 0) { + ret = cdio_stream_seek (env->gen.data_source, + (lsn * CDIO_CD_FRAMESIZE_RAW) - 272, SEEK_SET); + if (ret!=0) return ret; + + ret = cdio_stream_read (env->gen.data_source, data, + CDIO_CD_FRAMESIZE_RAW, nblocks); + } else { + /* We need to pad out the first 272 bytes with 0's */ + BZERO(data, 272); + + ret = cdio_stream_seek (env->gen.data_source, 0, SEEK_SET); + + if (ret!=0) return ret; + + ret = cdio_stream_read (env->gen.data_source, (uint8_t *) data+272, + CDIO_CD_FRAMESIZE_RAW - 272, nblocks); + } + + /* ret is number of bytes if okay, but we need to return 0 okay. */ + return ret == 0; +} + +/*! + Reads a single mode2 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_read_mode1_sector_bincue (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *p_env = user_data; + int ret; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + int blocksize = CDIO_CD_FRAMESIZE_RAW; + + ret = cdio_stream_seek (p_env->gen.data_source, lsn * blocksize, SEEK_SET); + if (ret!=0) return ret; + + /* FIXME: Not completely sure the below is correct. */ + ret = cdio_stream_read (p_env->gen.data_source, buf, CDIO_CD_FRAMESIZE_RAW, 1); + if (ret==0) return ret; + + memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE, + b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); + + return 0; +} + +/*! + Reads nblocks of mode1 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_read_mode1_sectors_bincue (void *user_data, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *p_env = user_data; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _read_mode1_sector_bincue (p_env, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +/*! + Reads a single mode1 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_read_mode2_sector_bincue (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *p_env = user_data; + int ret; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + + /* NOTE: The logic below seems a bit wrong and convoluted + to me, but passes the regression tests. (Perhaps it is why we get + valgrind errors in vcdxrip). Leave it the way it was for now. + Review this sector 2336 stuff later. + */ + + int blocksize = CDIO_CD_FRAMESIZE_RAW; + + ret = cdio_stream_seek (p_env->gen.data_source, lsn * blocksize, SEEK_SET); + if (ret!=0) return ret; + + ret = cdio_stream_read (p_env->gen.data_source, buf, CDIO_CD_FRAMESIZE_RAW, 1); + if (ret==0) return ret; + + + /* See NOTE above. */ + if (b_form2) + memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE, + M2RAW_SECTOR_SIZE); + else + memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE); + + return 0; +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_read_mode2_sectors_bincue (void *user_data, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *p_env = user_data; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _read_mode2_sector_bincue (p_env, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +/*! + Return an array of strings giving possible BIN/CUE disk images. + */ +char ** +cdio_get_devices_bincue (void) +{ + char **drives = NULL; + unsigned int num_files=0; +#ifdef HAVE_GLOB_H + unsigned int i; + glob_t globbuf; + globbuf.gl_offs = 0; + glob("*.cue", GLOB_DOOFFS, NULL, &globbuf); + for (i=0; i<globbuf.gl_pathc; i++) { + cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files); + } + globfree(&globbuf); +#else + cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files); +#endif /*HAVE_GLOB_H*/ + cdio_add_device_list(&drives, NULL, &num_files); + return drives; +} + +/*! + Return a string containing the default CD device. + */ +char * +cdio_get_default_device_bincue(void) +{ + char **drives = cdio_get_devices_nrg(); + char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]); + cdio_free_device_list(drives); + return drive; +} + +static bool +get_hwinfo_bincue ( const CdIo *p_cdio, /*out*/ cdio_hwinfo_t *hw_info) +{ + strcpy(hw_info->psz_vendor, "libcdio"); + strcpy(hw_info->psz_model, "CDRWIN"); + strcpy(hw_info->psz_revision, CDIO_VERSION); + return true; + +} + +/*! + Return the number of tracks in the current medium. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_format_t +_get_track_format_bincue(void *user_data, track_t i_track) +{ + _img_private_t *p_env = user_data; + + if (i_track > p_env->gen.i_tracks || i_track == 0) + return TRACK_FORMAT_ERROR; + + return p_env->tocent[i_track-p_env->gen.i_first_track].track_format; +} + +/*! + Return true if we have XA data (green, mode2 form1) or + XA data (green, mode2 form2). That is track begins: + sync - header - subheader + 12 4 - 8 + + FIXME: there's gotta be a better design for this and get_track_format? +*/ +static bool +_get_track_green_bincue(void *user_data, track_t i_track) +{ + _img_private_t *p_env = user_data; + + if ( NULL == p_env || + ( i_track < p_env->gen.i_first_track + || i_track >= p_env->gen.i_tracks + p_env->gen.i_first_track ) ) + return false; + + return p_env->tocent[i_track-p_env->gen.i_first_track].track_green; +} + +/*! + Return the starting LSN track number + i_track in obj. Track numbers start at 1. + The "leadout" track is specified either by + using i_track LEADOUT_TRACK or the total tracks+1. + False is returned if there is no track entry. +*/ +static lba_t +_get_lba_track_bincue(void *user_data, track_t i_track) +{ + _img_private_t *p_env = user_data; + + if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = p_env->gen.i_tracks+1; + + if (i_track <= p_env->gen.i_tracks + p_env->gen.i_first_track && i_track != 0) { + return p_env->tocent[i_track-p_env->gen.i_first_track].start_lba; + } else + return CDIO_INVALID_LBA; +} + +/*! + Return corresponding BIN file if psz_cue_name is a cue file or NULL + if not a CUE file. +*/ +char * +cdio_is_cuefile(const char *psz_cue_name) +{ + int i; + char *psz_bin_name; + + if (psz_cue_name == NULL) return NULL; + + /* FIXME? Now that we have cue parsing, should we really force + the filename extension requirement or is it enough just to + parse the cuefile? + */ + + psz_bin_name=strdup(psz_cue_name); + i=strlen(psz_bin_name)-strlen("cue"); + + if (i>0) { + if (psz_cue_name[i]=='c' && psz_cue_name[i+1]=='u' && psz_cue_name[i+2]=='e') { + psz_bin_name[i++]='b'; psz_bin_name[i++]='i'; psz_bin_name[i++]='n'; + if (parse_cuefile(NULL, psz_cue_name)) + return psz_bin_name; + else + goto error; + } + else if (psz_cue_name[i]=='C' && psz_cue_name[i+1]=='U' && psz_cue_name[i+2]=='E') { + psz_bin_name[i++]='B'; psz_bin_name[i++]='I'; psz_bin_name[i++]='N'; + if (parse_cuefile(NULL, psz_cue_name)) + return psz_bin_name; + else + goto error; + } + } + error: + free(psz_bin_name); + return NULL; +} + +/*! + Return corresponding CUE file if psz_bin_name is a bin file or NULL + if not a BIN file. +*/ +char * +cdio_is_binfile(const char *psz_bin_name) +{ + int i; + char *psz_cue_name; + + if (psz_bin_name == NULL) return NULL; + + psz_cue_name=strdup(psz_bin_name); + i=strlen(psz_bin_name)-strlen("bin"); + + if (i>0) { + if (psz_bin_name[i]=='b' && psz_bin_name[i+1]=='i' && psz_bin_name[i+2]=='n') { + psz_cue_name[i++]='c'; psz_cue_name[i++]='u'; psz_cue_name[i++]='e'; + return psz_cue_name; + } + else if (psz_bin_name[i]=='B' && psz_bin_name[i+1]=='I' && psz_bin_name[i+2]=='N') { + psz_cue_name[i++]='C'; psz_cue_name[i++]='U'; psz_cue_name[i++]='E'; + return psz_cue_name; + } + } + free(psz_cue_name); + return NULL; +} + +/*! + Initialization routine. This is the only thing that doesn't + get called via a function pointer. In fact *we* are the + ones to set that up. + */ +CdIo * +cdio_open_am_bincue (const char *psz_source_name, const char *psz_access_mode) +{ + if (psz_access_mode != NULL) + cdio_warn ("there is only one access mode for bincue. Arg %s ignored", + psz_access_mode); + return cdio_open_bincue(psz_source_name); +} + +/*! + Initialization routine. This is the only thing that doesn't + get called via a function pointer. In fact *we* are the + ones to set that up. + */ +CdIo * +cdio_open_bincue (const char *source_name) +{ + char *psz_bin_name = cdio_is_cuefile(source_name); + + if (NULL != psz_bin_name) { + free(psz_bin_name); + return cdio_open_cue(source_name); + } else { + char *psz_cue_name = cdio_is_binfile(source_name); + CdIo *cdio = cdio_open_cue(psz_cue_name); + free(psz_cue_name); + return cdio; + } +} + +CdIo * +cdio_open_cue (const char *psz_cue_name) +{ + CdIo *ret; + _img_private_t *_data; + char *psz_bin_name; + + cdio_funcs _funcs; + + memset( &_funcs, 0, sizeof(_funcs) ); + + _funcs.eject_media = _eject_media_image; + _funcs.free = _free_image; + _funcs.get_arg = _get_arg_image; + _funcs.get_cdtext = get_cdtext_generic; + _funcs.get_devices = cdio_get_devices_bincue; + _funcs.get_default_device = cdio_get_default_device_bincue; + _funcs.get_discmode = _get_discmode_image; + _funcs.get_drive_cap = _get_drive_cap_image; + _funcs.get_first_track_num= _get_first_track_num_image; + _funcs.get_hwinfo = get_hwinfo_bincue; + _funcs.get_mcn = _get_mcn_image; + _funcs.get_num_tracks = _get_num_tracks_image; + _funcs.get_track_format = _get_track_format_bincue; + _funcs.get_track_green = _get_track_green_bincue; + _funcs.get_track_lba = _get_lba_track_bincue; + _funcs.get_track_msf = _get_track_msf_image; + _funcs.lseek = _lseek_bincue; + _funcs.read = _read_bincue; + _funcs.read_audio_sectors = _read_audio_sectors_bincue; + _funcs.read_mode1_sector = _read_mode1_sector_bincue; + _funcs.read_mode1_sectors = _read_mode1_sectors_bincue; + _funcs.read_mode2_sector = _read_mode2_sector_bincue; + _funcs.read_mode2_sectors = _read_mode2_sectors_bincue; + _funcs.set_arg = _set_arg_image; + _funcs.stat_size = _stat_size_bincue; + + if (NULL == psz_cue_name) return NULL; + + _data = _cdio_malloc (sizeof (_img_private_t)); + _data->gen.init = false; + _data->psz_cue_name = NULL; + + ret = cdio_new ((void *)_data, &_funcs); + + if (ret == NULL) { + free(_data); + return NULL; + } + + psz_bin_name = cdio_is_cuefile(psz_cue_name); + + if (NULL == psz_bin_name) { + cdio_error ("source name %s is not recognized as a CUE file", + psz_cue_name); + } + + _set_arg_image (_data, "cue", psz_cue_name); + _set_arg_image (_data, "source", psz_bin_name); + free(psz_bin_name); + + if (_init_bincue(_data)) { + return ret; + } else { + _free_image(_data); + free(ret); + return NULL; + } +} + +bool +cdio_have_bincue (void) +{ + return true; +} diff --git a/contrib/libcdio/image/cdrdao.c b/contrib/libcdio/image/cdrdao.c new file mode 100644 index 000000000..828172721 --- /dev/null +++ b/contrib/libcdio/image/cdrdao.c @@ -0,0 +1,1198 @@ +/* + $Id: cdrdao.c,v 1.1 2005/01/01 02:43:58 rockyb Exp $ + + Copyright (C) 2004 Rocky Bernstein <rocky@panix.com> + toc reading routine adapted from cuetools + Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* This code implements low-level access functions for a CD images + residing inside a disk file (*.bin) and its associated cue sheet. + (*.cue). +*/ + +static const char _rcsid[] = "$Id: cdrdao.c,v 1.1 2005/01/01 02:43:58 rockyb Exp $"; + +#include "image.h" +#include "cdio_assert.h" +#include "_cdio_stdio.h" + +#include <cdio/logging.h> +#include <cdio/sector.h> +#include <cdio/util.h> +#include <cdio/version.h> + +#ifdef HAVE_STDIO_H +#include <stdio.h> +#endif +#ifdef HAVE_STDLIB_H +#include <stdlib.h> +#endif +#ifdef HAVE_STRING_H +#include <string.h> +#endif +#ifdef HAVE_STRINGS_H +#include <strings.h> +#endif +#ifdef HAVE_GLOB_H +#include <glob.h> +#endif +#ifdef HAVE_ERRNO_H +#include <errno.h> +#endif + +#include <ctype.h> + +#include "portable.h" + +/* reader */ + +#define DEFAULT_CDIO_DEVICE "videocd.bin" +#define DEFAULT_CDIO_CDRDAO "videocd.toc" + +typedef struct { + /* Things common to all drivers like this. + This must be first. */ + generic_img_private_t gen; + internal_position_t pos; + + char *psz_cue_name; + char *psz_mcn; /* Media Catalog Number (5.22.3) + exactly 13 bytes */ + track_info_t tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track + add 1 for leadout. */ + discmode_t disc_mode; +} _img_private_t; + +static uint32_t _stat_size_cdrdao (void *user_data); +static bool parse_tocfile (_img_private_t *cd, const char *toc_name); + +#define NEED_MEDIA_EJECT_IMAGE +#include "image_common.h" + +/*! + Initialize image structures. + */ +static bool +_init_cdrdao (_img_private_t *env) +{ + lsn_t lead_lsn; + + if (env->gen.init) + return false; + + /* Have to set init before calling _stat_size_cdrdao() or we will + get into infinite recursion calling passing right here. + */ + env->gen.init = true; + env->gen.i_first_track = 1; + env->psz_mcn = NULL; + env->disc_mode = CDIO_DISC_MODE_NO_INFO; + + cdtext_init (&(env->gen.cdtext)); + + /* Read in TOC sheet. */ + if ( !parse_tocfile(env, env->psz_cue_name) ) return false; + + lead_lsn = _stat_size_cdrdao( (_img_private_t *) env); + + if (-1 == lead_lsn) + return false; + + /* Fake out leadout track and sector count for last track*/ + cdio_lsn_to_msf (lead_lsn, &env->tocent[env->gen.i_tracks].start_msf); + env->tocent[env->gen.i_tracks].start_lba = cdio_lsn_to_lba(lead_lsn); + env->tocent[env->gen.i_tracks-env->gen.i_first_track].sec_count = + cdio_lsn_to_lba(lead_lsn - env->tocent[env->gen.i_tracks-1].start_lba); + + return true; +} + +/*! + Reads into buf the next size bytes. + Returns -1 on error. + Would be libc's seek() but we have to adjust for the extra track header + information in each sector. +*/ +static off_t +_lseek_cdrdao (void *user_data, off_t offset, int whence) +{ + _img_private_t *env = user_data; + + /* real_offset is the real byte offset inside the disk image + The number below was determined empirically. I'm guessing + the 1st 24 bytes of a bin file are used for something. + */ + off_t real_offset=0; + + unsigned int i; + + env->pos.lba = 0; + for (i=0; i<env->gen.i_tracks; i++) { + track_info_t *this_track=&(env->tocent[i]); + env->pos.index = i; + if ( (this_track->sec_count*this_track->datasize) >= offset) { + int blocks = offset / this_track->datasize; + int rem = offset % this_track->datasize; + int block_offset = blocks * this_track->blocksize; + real_offset += block_offset + rem; + env->pos.buff_offset = rem; + env->pos.lba += blocks; + break; + } + real_offset += this_track->sec_count*this_track->blocksize; + offset -= this_track->sec_count*this_track->datasize; + env->pos.lba += this_track->sec_count; + } + + if (i==env->gen.i_tracks) { + cdio_warn ("seeking outside range of disk image"); + return -1; + } else { + real_offset += env->tocent[i].datastart; + return cdio_stream_seek(env->tocent[i].data_source, real_offset, whence); + } +} + +/*! + Reads into buf the next size bytes. + Returns -1 on error. + FIXME: + At present we assume a read doesn't cross sector or track + boundaries. +*/ +static ssize_t +_read_cdrdao (void *user_data, void *data, size_t size) +{ + _img_private_t *env = user_data; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + char *p = data; + ssize_t final_size=0; + ssize_t this_size; + track_info_t *this_track=&(env->tocent[env->pos.index]); + ssize_t skip_size = this_track->datastart + this_track->endsize; + + while (size > 0) { + int rem = this_track->datasize - env->pos.buff_offset; + if (size <= rem) { + this_size = cdio_stream_read(this_track->data_source, buf, size, 1); + final_size += this_size; + memcpy (p, buf, this_size); + break; + } + + /* Finish off reading this sector. */ + cdio_warn ("Reading across block boundaries not finished"); + + size -= rem; + this_size = cdio_stream_read(this_track->data_source, buf, rem, 1); + final_size += this_size; + memcpy (p, buf, this_size); + p += this_size; + this_size = cdio_stream_read(this_track->data_source, buf, rem, 1); + + /* Skip over stuff at end of this sector and the beginning of the next. + */ + cdio_stream_read(this_track->data_source, buf, skip_size, 1); + + /* Get ready to read another sector. */ + env->pos.buff_offset=0; + env->pos.lba++; + + /* Have gone into next track. */ + if (env->pos.lba >= env->tocent[env->pos.index+1].start_lba) { + env->pos.index++; + this_track=&(env->tocent[env->pos.index]); + skip_size = this_track->datastart + this_track->endsize; + } + } + return final_size; +} + +/*! + Return the size of the CD in logical block address (LBA) units. + */ +static uint32_t +_stat_size_cdrdao (void *user_data) +{ + _img_private_t *env = user_data; + long size; + + size = cdio_stream_stat (env->tocent[0].data_source); + + if (size % CDIO_CD_FRAMESIZE_RAW) + { + cdio_warn ("image %s size (%ld) not multiple of blocksize (%d)", + env->tocent[0].filename, size, CDIO_CD_FRAMESIZE_RAW); + if (size % M2RAW_SECTOR_SIZE == 0) + cdio_warn ("this may be a 2336-type disc image"); + else if (size % CDIO_CD_FRAMESIZE_RAW == 0) + cdio_warn ("this may be a 2352-type disc image"); + /* exit (EXIT_FAILURE); */ + } + + size /= CDIO_CD_FRAMESIZE_RAW; + + return size; +} + +#define MAXLINE 512 +#define UNIMPLIMENTED_MSG \ + cdio_log(log_level, "%s line %d: unimplimented keyword: %s", \ + psz_cue_name, i_line, psz_keyword) + + +static bool +parse_tocfile (_img_private_t *cd, const char *psz_cue_name) +{ + /* The below declarations may be common in other image-parse routines. */ + FILE *fp; + char psz_line[MAXLINE]; /* text of current line read in file fp. */ + unsigned int i_line=0; /* line number in file of psz_line. */ + int i = -1; /* Position in tocent. Same as + cd->gen.i_tracks - 1 */ + char *psz_keyword, *psz_field; + cdio_log_level_t log_level = (NULL == cd) ? CDIO_LOG_INFO : CDIO_LOG_WARN; + cdtext_field_t cdtext_key; + + /* The below declaration(s) may be unique to this image-parse routine. */ + unsigned int i_cdtext_nest = 0; + + if (NULL == psz_cue_name) + return false; + + fp = fopen (psz_cue_name, "r"); + if (fp == NULL) { + cdio_log(log_level, "error opening %s for reading: %s", + psz_cue_name, strerror(errno)); + return false; + } + + if (cd) { + cd->gen.b_cdtext_init = true; + cd->gen.b_cdtext_error = false; + } + + while ((fgets(psz_line, MAXLINE, fp)) != NULL) { + + i_line++; + + /* strip comment from line */ + /* todo: // in quoted strings? */ + /* //comment */ + if (NULL != (psz_field = strstr (psz_line, "//"))) + *psz_field = '\0'; + + if (NULL != (psz_keyword = strtok (psz_line, " \t\n\r"))) { + /* CATALOG "ddddddddddddd" */ + if (0 == strcmp ("CATALOG", psz_keyword)) { + if (-1 == i) { + if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) { + if (13 != strlen(psz_field)) { + cdio_log(log_level, + "%s line %d after word CATALOG:", + psz_cue_name, i_line); + cdio_log(log_level, + "Token %s has length %ld. Should be 13 digits.", + psz_field, (long int) strlen(psz_field)); + + goto err_exit; + } else { + /* Check that we have all digits*/ + unsigned int i; + for (i=0; i<13; i++) { + if (!isdigit(psz_field[i])) { + cdio_log(log_level, + "%s line %d after word CATALOG:", + psz_cue_name, i_line); + cdio_log(log_level, + "Character \"%c\" at postition %i of token \"%s\"" + " is not all digits.", + psz_field[i], i+1, psz_field); + goto err_exit; + } + } + if (NULL != cd) cd->psz_mcn = strdup (psz_field); + } + } else { + cdio_log(log_level, + "%s line %d after word CATALOG:", + psz_cue_name, i_line); + cdio_log(log_level, "Expecting 13 digits; nothing seen."); + goto err_exit; + } + } else { + goto err_exit; + } + + /* CD_DA | CD_ROM | CD_ROM_XA */ + } else if (0 == strcmp ("CD_DA", psz_keyword)) { + if (-1 == i) { + if (NULL != cd) + cd->disc_mode = CDIO_DISC_MODE_CD_DA; + } else { + goto not_in_global_section; + } + } else if (0 == strcmp ("CD_ROM", psz_keyword)) { + if (-1 == i) { + if (NULL != cd) + cd->disc_mode = CDIO_DISC_MODE_CD_DATA; + } else { + goto not_in_global_section; + } + + } else if (0 == strcmp ("CD_ROM_XA", psz_keyword)) { + if (-1 == i) { + if (NULL != cd) + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + } else { + goto not_in_global_section; + } + + /* TRACK <track-mode> [<sub-channel-mode>] */ + } else if (0 == strcmp ("TRACK", psz_keyword)) { + i++; + if (NULL != cd) cdtext_init (&(cd->gen.cdtext_track[i])); + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + if (0 == strcmp ("AUDIO", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_AUDIO; + cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW; + cd->tocent[i].datasize = CDIO_CD_FRAMESIZE_RAW; + cd->tocent[i].datastart = 0; + cd->tocent[i].endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_DA; + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DATA: + case CDIO_DISC_MODE_CD_XA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + + } + } else if (0 == strcmp ("MODE1", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_DATA; + cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW; + cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + cd->tocent[i].datasize = CDIO_CD_FRAMESIZE; + cd->tocent[i].endsize = CDIO_CD_EDC_SIZE + + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_DATA; + break; + case CDIO_DISC_MODE_CD_DATA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_XA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE1_RAW", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_DATA; + cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW; + cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + cd->tocent[i].datasize = CDIO_CD_FRAMESIZE; + cd->tocent[i].endsize = CDIO_CD_EDC_SIZE + + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_DATA; + break; + case CDIO_DISC_MODE_CD_DATA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_XA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_XA; + cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + cd->tocent[i].datasize = M2RAW_SECTOR_SIZE; + cd->tocent[i].endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2_FORM1", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_XA; + cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + cd->tocent[i].datasize = CDIO_CD_FRAMESIZE_RAW; + cd->tocent[i].endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2_FORM2", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_XA; + cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE; + cd->tocent[i].datasize = CDIO_CD_FRAMESIZE; + cd->tocent[i].endsize = CDIO_CD_SYNC_SIZE + + CDIO_CD_ECC_SIZE; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2_FORM_MIX", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_XA; + cd->tocent[i].datasize = M2RAW_SECTOR_SIZE; + cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW; + cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE; + cd->tocent[i].track_green = true; + cd->tocent[i].endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else if (0 == strcmp ("MODE2_RAW", psz_field)) { + if (NULL != cd) { + cd->tocent[i].track_format = TRACK_FORMAT_XA; + cd->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW; + cd->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE; + cd->tocent[i].datasize = CDIO_CD_FRAMESIZE; + cd->tocent[i].track_green = true; + cd->tocent[i].endsize = 0; + switch(cd->disc_mode) { + case CDIO_DISC_MODE_NO_INFO: + cd->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case CDIO_DISC_MODE_CD_XA: + case CDIO_DISC_MODE_CD_MIXED: + case CDIO_DISC_MODE_ERROR: + /* Disc type stays the same. */ + break; + case CDIO_DISC_MODE_CD_DA: + case CDIO_DISC_MODE_CD_DATA: + cd->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + default: + cd->disc_mode = CDIO_DISC_MODE_ERROR; + } + } + } else { + cdio_log(log_level, "%s line %d after TRACK:", + psz_cue_name, i_line); + cdio_log(log_level, "'%s' not a valid mode.", psz_field); + goto err_exit; + } + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + /* todo: set sub-channel-mode */ + if (0 == strcmp ("RW", psz_field)) + ; + else if (0 == strcmp ("RW_RAW", psz_field)) + ; + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + + /* track flags */ + /* [NO] COPY | [NO] PRE_EMPHASIS */ + } else if (0 == strcmp ("NO", psz_keyword)) { + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + if (0 == strcmp ("COPY", psz_field)) { + if (NULL != cd) + cd->tocent[i].flags &= ~CDIO_TRACK_FLAG_COPY_PERMITTED; + + } else if (0 == strcmp ("PRE_EMPHASIS", psz_field)) + if (NULL != cd) { + cd->tocent[i].flags &= ~CDIO_TRACK_FLAG_PRE_EMPHASIS; + goto err_exit; + } + } else { + goto format_error; + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + } else if (0 == strcmp ("COPY", psz_keyword)) { + if (NULL != cd) + cd->tocent[i].flags |= CDIO_TRACK_FLAG_COPY_PERMITTED; + } else if (0 == strcmp ("PRE_EMPHASIS", psz_keyword)) { + if (NULL != cd) + cd->tocent[i].flags |= CDIO_TRACK_FLAG_PRE_EMPHASIS; + /* TWO_CHANNEL_AUDIO */ + } else if (0 == strcmp ("TWO_CHANNEL_AUDIO", psz_keyword)) { + if (NULL != cd) + cd->tocent[i].flags &= ~CDIO_TRACK_FLAG_FOUR_CHANNEL_AUDIO; + /* FOUR_CHANNEL_AUDIO */ + } else if (0 == strcmp ("FOUR_CHANNEL_AUDIO", psz_keyword)) { + if (NULL != cd) + cd->tocent[i].flags |= CDIO_TRACK_FLAG_FOUR_CHANNEL_AUDIO; + + /* ISRC "CCOOOYYSSSSS" */ + } else if (0 == strcmp ("ISRC", psz_keyword)) { + if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) { + if (NULL != cd) + cd->tocent[i].isrc = strdup(psz_field); + } else { + goto format_error; + } + + /* SILENCE <length> */ + } else if (0 == strcmp ("SILENCE", psz_keyword)) { + UNIMPLIMENTED_MSG; + + /* ZERO <length> */ + } else if (0 == strcmp ("ZERO", psz_keyword)) { + UNIMPLIMENTED_MSG; + + /* [FILE|AUDIOFILE] "<filename>" <start> [<length>] */ + } else if (0 == strcmp ("FILE", psz_keyword) + || 0 == strcmp ("AUDIOFILE", psz_keyword)) { + if (0 <= i) { + if (NULL != (psz_field = strtok (NULL, "\"\t\n\r"))) { + if (NULL != cd) { + cd->tocent[i].filename = strdup (psz_field); + /* Todo: do something about reusing existing files. */ + if (!(cd->tocent[i].data_source = cdio_stdio_new (psz_field))) { + cdio_log (log_level, + "%s line %d: can't open file `%s' for reading", + psz_cue_name, i_line, psz_field); + goto err_exit; + } + } + } + + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + lba_t lba = cdio_lsn_to_lba(cdio_mmssff_to_lba (psz_field)); + if (CDIO_INVALID_LBA == lba) { + cdio_log(log_level, "%s line %d: invalid MSF string %s", + psz_cue_name, i_line, psz_field); + goto err_exit; + } + + if (NULL != cd) { + cd->tocent[i].start_lba = lba; + cdio_lba_to_msf(lba, &(cd->tocent[i].start_msf)); + } + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) + if (NULL != cd) + cd->tocent[i].length = cdio_mmssff_to_lba (psz_field); + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + } else { + goto not_in_global_section; + } + + /* DATAFILE "<filename>" <start> [<length>] */ + } else if (0 == strcmp ("DATAFILE", psz_keyword)) { + goto unimplimented_error; + + /* FIFO "<fifo path>" [<length>] */ + } else if (0 == strcmp ("FIFO", psz_keyword)) { + goto unimplimented_error; + + /* START MM:SS:FF */ + } else if (0 == strcmp ("START", psz_keyword)) { + if (0 <= i) { + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + /* todo: line is too long! */ + if (NULL != cd) { + cd->tocent[i].start_lba += cdio_mmssff_to_lba (psz_field); + cdio_lba_to_msf(cd->tocent[i].start_lba, + &(cd->tocent[i].start_msf)); + } + } + + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + } else { + goto not_in_global_section; + } + + /* PREGAP MM:SS:FF */ + } else if (0 == strcmp ("PREGAP", psz_keyword)) { + if (0 <= i) { + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + if (NULL != cd) + cd->tocent[i].pregap = cdio_mmssff_to_lba (psz_field); + } else { + goto format_error; + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + } else { + goto not_in_global_section; + } + + /* INDEX MM:SS:FF */ + } else if (0 == strcmp ("INDEX", psz_keyword)) { + if (0 <= i) { + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + if (NULL != cd) { +#if 0 + if (1 == cd->tocent[i].nindex) { + cd->tocent[i].indexes[1] = cd->tocent[i].indexes[0]; + cd->tocent[i].nindex++; + } + cd->tocent[i].indexes[cd->tocent[i].nindex++] = + cdio_mmssff_to_lba (psz_field) + cd->tocent[i].indexes[0]; +#else + ; + +#endif + } + } else { + goto format_error; + } + if (NULL != (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + } else { + goto not_in_global_section; + } + + /* CD_TEXT { ... } */ + /* todo: opening { must be on same line as CD_TEXT */ + } else if (0 == strcmp ("CD_TEXT", psz_keyword)) { + if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + if ( 0 == strcmp( "{", psz_field ) ) { + i_cdtext_nest++; + } else { + cdio_log (log_level, + "%s line %d: expecting '{'", psz_cue_name, i_line); + goto err_exit; + } + + } else if (0 == strcmp ("LANGUAGE_MAP", psz_keyword)) { + /* LANGUAGE d { ... } */ + } else if (0 == strcmp ("LANGUAGE", psz_keyword)) { + if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + /* Language number */ + if (NULL == (psz_field = strtok (NULL, " \t\n\r"))) { + goto format_error; + } + if ( 0 == strcmp( "{", psz_field ) ) { + i_cdtext_nest++; + } + } else if (0 == strcmp ("{", psz_keyword)) { + i_cdtext_nest++; + } else if (0 == strcmp ("}", psz_keyword)) { + if (i_cdtext_nest > 0) i_cdtext_nest--; + } else if ( CDTEXT_INVALID != + (cdtext_key = cdtext_is_keyword (psz_keyword)) ) { + if (-1 == i) { + if (NULL != cd) { + cdtext_set (cdtext_key, + strtok (NULL, "\"\t\n\r"), + &(cd->gen.cdtext)); + } + } else { + if (NULL != cd) { + cdtext_set (cdtext_key, + strtok (NULL, "\"\t\n\r"), + &(cd->gen.cdtext_track[i])); + } + } + + /* unrecognized line */ + } else { + cdio_log(log_level, "%s line %d: warning: unrecognized word: %s", + psz_cue_name, i_line, psz_keyword); + goto err_exit; + } + } + } + + if (NULL != cd) { + cd->gen.i_tracks = i+1; + cd->gen.toc_init = true; + } + + fclose (fp); + return true; + + unimplimented_error: + UNIMPLIMENTED_MSG; + goto err_exit; + + format_error: + cdio_log(log_level, "%s line %d after word %s", + psz_cue_name, i_line, psz_keyword); + goto err_exit; + + not_in_global_section: + cdio_log(log_level, "%s line %d: word %s only allowed in global section", + psz_cue_name, i_line, psz_keyword); + + err_exit: + fclose (fp); + return false; +} + +/*! + Reads a single audio sector from CD device into data starting + from lsn. Returns 0 if no error. + */ +static int +_read_audio_sectors_cdrdao (void *user_data, void *data, lsn_t lsn, + unsigned int nblocks) +{ + _img_private_t *env = user_data; + int ret; + + /* Why the adjustment of 272, I don't know. It seems to work though */ + if (lsn != 0) { + ret = cdio_stream_seek (env->tocent[0].data_source, + (lsn * CDIO_CD_FRAMESIZE_RAW) - 272, SEEK_SET); + if (ret!=0) return ret; + + ret = cdio_stream_read (env->tocent[0].data_source, data, + CDIO_CD_FRAMESIZE_RAW, nblocks); + } else { + /* We need to pad out the first 272 bytes with 0's */ + BZERO(data, 272); + + ret = cdio_stream_seek (env->tocent[0].data_source, 0, SEEK_SET); + + if (ret!=0) return ret; + + ret = cdio_stream_read (env->tocent[0].data_source, (uint8_t *) data+272, + CDIO_CD_FRAMESIZE_RAW - 272, nblocks); + } + + /* ret is number of bytes if okay, but we need to return 0 okay. */ + return ret == 0; +} + +/*! + Reads a single mode2 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_read_mode1_sector_cdrdao (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *env = user_data; + int ret; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + + ret = cdio_stream_seek (env->tocent[0].data_source, + lsn * CDIO_CD_FRAMESIZE_RAW, SEEK_SET); + if (ret!=0) return ret; + + /* FIXME: Not completely sure the below is correct. */ + ret = cdio_stream_read (env->tocent[0].data_source, buf, + CDIO_CD_FRAMESIZE_RAW, 1); + if (ret==0) return ret; + + memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE, + b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); + + return 0; +} + +/*! + Reads nblocks of mode1 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_read_mode1_sectors_cdrdao (void *user_data, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *env = user_data; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _read_mode1_sector_cdrdao (env, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +/*! + Reads a single mode1 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_read_mode2_sector_cdrdao (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *env = user_data; + int ret; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + + /* NOTE: The logic below seems a bit wrong and convoluted + to me, but passes the regression tests. (Perhaps it is why we get + valgrind errors in vcdxrip). Leave it the way it was for now. + Review this sector 2336 stuff later. + */ + + ret = cdio_stream_seek (env->tocent[0].data_source, + lsn * CDIO_CD_FRAMESIZE_RAW, SEEK_SET); + if (ret!=0) return ret; + + ret = cdio_stream_read (env->tocent[0].data_source, buf, + CDIO_CD_FRAMESIZE_RAW, 1); + if (ret==0) return ret; + + + /* See NOTE above. */ + if (b_form2) + memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE, + M2RAW_SECTOR_SIZE); + else + memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE); + + return 0; +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_read_mode2_sectors_cdrdao (void *user_data, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *env = user_data; + int i; + int retval; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _read_mode2_sector_cdrdao (env, + ((char *)data) + (CDIO_CD_FRAMESIZE * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +/*! + Return an array of strings giving possible TOC disk images. + */ +char ** +cdio_get_devices_cdrdao (void) +{ + char **drives = NULL; + unsigned int num_files=0; +#ifdef HAVE_GLOB_H + unsigned int i; + glob_t globbuf; + globbuf.gl_offs = 0; + glob("*.toc", GLOB_DOOFFS, NULL, &globbuf); + for (i=0; i<globbuf.gl_pathc; i++) { + cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files); + } + globfree(&globbuf); +#else + cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files); +#endif /*HAVE_GLOB_H*/ + cdio_add_device_list(&drives, NULL, &num_files); + return drives; +} + +/*! + Return a string containing the default CD device. + */ +char * +cdio_get_default_device_cdrdao(void) +{ + char **drives = cdio_get_devices_nrg(); + char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]); + cdio_free_device_list(drives); + return drive; +} + +static bool +get_hwinfo_cdrdao ( const CdIo *p_cdio, /*out*/ cdio_hwinfo_t *hw_info) +{ + strcpy(hw_info->psz_vendor, "libcdio"); + strcpy(hw_info->psz_model, "cdrdao"); + strcpy(hw_info->psz_revision, CDIO_VERSION); + return true; + +} + +/*! + Return the number of tracks in the current medium. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_format_t +_get_track_format_cdrdao(void *user_data, track_t i_track) +{ + _img_private_t *env = user_data; + + if (!env->gen.init) _init_cdrdao(env); + + if (i_track > env->gen.i_tracks || i_track == 0) + return TRACK_FORMAT_ERROR; + + return env->tocent[i_track-env->gen.i_first_track].track_format; +} + +/*! + Return true if we have XA data (green, mode2 form1) or + XA data (green, mode2 form2). That is track begins: + sync - header - subheader + 12 4 - 8 + + FIXME: there's gotta be a better design for this and get_track_format? +*/ +static bool +_get_track_green_cdrdao(void *user_data, track_t i_track) +{ + _img_private_t *env = user_data; + + if (!env->gen.init) _init_cdrdao(env); + + if (i_track > env->gen.i_tracks || i_track == 0) + return false; + + return env->tocent[i_track-env->gen.i_first_track].track_green; +} + +/*! + Return the starting LSN track number + i_track in obj. Track numbers start at 1. + The "leadout" track is specified either by + using i_track LEADOUT_TRACK or the total tracks+1. + False is returned if there is no track entry. +*/ +static lba_t +_get_lba_track_cdrdao(void *user_data, track_t i_track) +{ + _img_private_t *env = user_data; + _init_cdrdao (env); + + if (i_track == CDIO_CDROM_LEADOUT_TRACK) + i_track = env->gen.i_tracks+1; + + if (i_track <= env->gen.i_tracks+1 && i_track != 0) { + return env->tocent[i_track-1].start_lba; + } else + return CDIO_INVALID_LBA; +} + +/*! + Check that a TOC file is valid. We parse the entire file. + +*/ +bool +cdio_is_tocfile(const char *psz_cue_name) +{ + int i; + + if (psz_cue_name == NULL) return false; + + i=strlen(psz_cue_name)-strlen("toc"); + + if (i>0) { + if ( (psz_cue_name[i]=='t' && psz_cue_name[i+1]=='o' && psz_cue_name[i+2]=='c') + || (psz_cue_name[i]=='T' && psz_cue_name[i+1]=='O' && psz_cue_name[i+2]=='C') ) { + return parse_tocfile(NULL, psz_cue_name); + } + } + return false; +} + +/*! + Initialization routine. This is the only thing that doesn't + get called via a function pointer. In fact *we* are the + ones to set that up. + */ +CdIo * +cdio_open_am_cdrdao (const char *psz_source_name, const char *psz_access_mode) +{ + if (psz_access_mode != NULL && strcmp(psz_access_mode, "image")) + cdio_warn ("there is only one access mode, 'image' for cdrdao. Arg %s ignored", + psz_access_mode); + return cdio_open_cdrdao(psz_source_name); +} + +/*! + Initialization routine. This is the only thing that doesn't + get called via a function pointer. In fact *we* are the + ones to set that up. + */ +CdIo * +cdio_open_cdrdao (const char *psz_cue_name) +{ + CdIo *ret; + _img_private_t *_data; + + cdio_funcs _funcs; + + memset( &_funcs, 0, sizeof(_funcs) ); + + _funcs.eject_media = _eject_media_image; + _funcs.free = _free_image; + _funcs.get_arg = _get_arg_image; + _funcs.get_cdtext = get_cdtext_generic; + _funcs.get_devices = cdio_get_devices_cdrdao; + _funcs.get_default_device = cdio_get_default_device_cdrdao; + _funcs.get_discmode = _get_discmode_image; + _funcs.get_drive_cap = _get_drive_cap_image; + _funcs.get_first_track_num= _get_first_track_num_image; + _funcs.get_hwinfo = get_hwinfo_cdrdao; + _funcs.get_mcn = _get_mcn_image; + _funcs.get_num_tracks = _get_num_tracks_image; + _funcs.get_track_format = _get_track_format_cdrdao; + _funcs.get_track_green = _get_track_green_cdrdao; + _funcs.get_track_lba = _get_lba_track_cdrdao; + _funcs.get_track_msf = _get_track_msf_image; + _funcs.lseek = _lseek_cdrdao; + _funcs.read = _read_cdrdao; + _funcs.read_audio_sectors = _read_audio_sectors_cdrdao; + _funcs.read_mode1_sector = _read_mode1_sector_cdrdao; + _funcs.read_mode1_sectors = _read_mode1_sectors_cdrdao; + _funcs.read_mode2_sector = _read_mode2_sector_cdrdao; + _funcs.read_mode2_sectors = _read_mode2_sectors_cdrdao; + _funcs.set_arg = _set_arg_image; + _funcs.stat_size = _stat_size_cdrdao; + + if (NULL == psz_cue_name) return NULL; + + _data = _cdio_malloc (sizeof (_img_private_t)); + _data->gen.init = false; + _data->psz_cue_name = NULL; + _data->gen.data_source = NULL; + _data->gen.source_name = NULL; + + ret = cdio_new ((void *)_data, &_funcs); + + if (ret == NULL) { + free(_data); + return NULL; + } + + if (!cdio_is_tocfile(psz_cue_name)) { + cdio_debug ("source name %s is not recognized as a TOC file", + psz_cue_name); + return NULL; + } + + _set_arg_image (_data, "cue", psz_cue_name); + _set_arg_image (_data, "source", psz_cue_name); + + if (_init_cdrdao(_data)) { + return ret; + } else { + _free_image(_data); + free(ret); + return NULL; + } +} + +bool +cdio_have_cdrdao (void) +{ + return true; +} diff --git a/contrib/libcdio/image/nrg.c b/contrib/libcdio/image/nrg.c new file mode 100644 index 000000000..40e5bbbf7 --- /dev/null +++ b/contrib/libcdio/image/nrg.c @@ -0,0 +1,1274 @@ +/* + $Id: nrg.c,v 1.3 2005/05/07 22:07:27 rockyb Exp $ + + Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com> + Copyright (C) 2001, 2003 Herbert Valerio Riedel <hvr@gnu.org> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +/*! This code implements low-level access functions for the Nero native + CD-image format residing inside a disk file (*.nrg). +*/ + +#include "image.h" + +#ifdef HAVE_STDIO_H +#include <stdio.h> +#endif +#ifdef HAVE_STDLIB_H +#include <stdlib.h> +#endif +#ifdef HAVE_STRING_H +#include <string.h> +#endif +#ifdef HAVE_GLOB_H +#include <glob.h> +#endif + +#include <cdio/bytesex.h> +#include <cdio/ds.h> +#include <cdio/logging.h> +#include <cdio/util.h> +#include <cdio/version.h> +#include "cdio_assert.h" +#include "_cdio_stdio.h" +#include "nrg.h" + +static const char _rcsid[] = "$Id: nrg.c,v 1.3 2005/05/07 22:07:27 rockyb Exp $"; + + +/* reader */ + +#define DEFAULT_CDIO_DEVICE "image.nrg" + +/* + Link element of track structure as a linked list. + Possibly redundant with above track_info_t */ +typedef struct { + uint32_t start_lsn; + uint32_t sec_count; /* Number of sectors in track. Does not + include pregap before next entry. */ + uint64_t img_offset; /* Bytes offset from beginning of disk image file.*/ + uint32_t blocksize; /* Number of bytes in a block */ + int flags; /* don't copy, 4 channel, pre-emphasis */ +} _mapping_t; + + +typedef struct { + /* Things common to all drivers like this. + This must be first. */ + generic_img_private_t gen; + internal_position_t pos; + + /* This is common to all image drivers... */ + char *psz_cue_name; + char *psz_mcn; /* Media Catalog Number (5.22.3) */ + + track_info_t tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track + add 1 for leadout. */ + discmode_t disc_mode; + + /* Nero Specific stuff. Note: for the image_free to work, this *must* + be last. */ + bool is_dao; /* True if some of disk at once. False + if some sort of track at once. */ + uint32_t mtyp; /* Value of MTYP (media type?) tag */ + uint8_t dtyp; /* Value of DAOX media type tag */ + + /* This is a hack because I don't really understnad NERO better. */ + bool is_cues; + + CdioList *mapping; /* List of track information */ + uint32_t size; +} _img_private_t; + +static bool parse_nrg (_img_private_t *env, const char *psz_cue_name); +static uint32_t _stat_size_nrg (void *user_data); + +#include "image_common.h" + +/* Updates internal track TOC, so we can later + simulate ioctl(CDROMREADTOCENTRY). + */ +static void +_register_mapping (_img_private_t *env, lsn_t start_lsn, uint32_t sec_count, + uint64_t img_offset, uint32_t blocksize, + track_format_t track_format, bool track_green, + int flags) +{ + const int track_num=env->gen.i_tracks; + track_info_t *this_track=&(env->tocent[env->gen.i_tracks]); + _mapping_t *_map = _cdio_malloc (sizeof (_mapping_t)); + + _map->start_lsn = start_lsn; + _map->sec_count = sec_count; + _map->img_offset = img_offset; + _map->blocksize = blocksize; + _map->flags = flags; + + if (!env->mapping) env->mapping = _cdio_list_new (); + _cdio_list_append (env->mapping, _map); + + env->size = MAX (env->size, (start_lsn + sec_count)); + + /* Update *this_track and track_num. These structures are + in a sense redundant witht the obj->mapping list. Perhaps one + or the other can be eliminated. + */ + + cdio_lba_to_msf (cdio_lsn_to_lba(start_lsn), &(this_track->start_msf)); + this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf); + this_track->track_num = track_num+1; + this_track->blocksize = blocksize; + if (env->is_cues) + this_track->datastart = img_offset; + else + this_track->datastart = 0; + + if (track_green) + this_track->datastart += CDIO_CD_SUBHEADER_SIZE; + + this_track->sec_count = sec_count; + + this_track->track_format= track_format; + this_track->track_green = track_green; + + switch (this_track->track_format) { + case TRACK_FORMAT_AUDIO: + this_track->blocksize = CDIO_CD_FRAMESIZE_RAW; + this_track->datasize = CDIO_CD_FRAMESIZE_RAW; + /*this_track->datastart = 0;*/ + this_track->endsize = 0; + break; + case TRACK_FORMAT_CDI: + this_track->datasize=CDIO_CD_FRAMESIZE; + break; + case TRACK_FORMAT_XA: + if (track_green) { + this_track->blocksize = CDIO_CD_FRAMESIZE; + /*this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE;*/ + this_track->datasize = M2RAW_SECTOR_SIZE; + this_track->endsize = 0; + } else { + /*this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE + + CDIO_CD_SUBHEADER_SIZE;*/ + this_track->datasize = CDIO_CD_FRAMESIZE; + this_track->endsize = CDIO_CD_SYNC_SIZE + CDIO_CD_ECC_SIZE; + } + break; + case TRACK_FORMAT_DATA: + if (track_green) { + /*this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE;*/ + this_track->datasize = CDIO_CD_FRAMESIZE; + this_track->endsize = CDIO_CD_EDC_SIZE + CDIO_CD_M1F1_ZERO_SIZE + + CDIO_CD_ECC_SIZE; + } else { + /* Is the below correct? */ + /*this_track->datastart = 0;*/ + this_track->datasize = CDIO_CD_FRAMESIZE; + this_track->endsize = 0; + } + break; + default: + /*this_track->datasize=CDIO_CD_FRAMESIZE_RAW;*/ + cdio_warn ("track %d has unknown format %d", + env->gen.i_tracks, this_track->track_format); + } + + env->gen.i_tracks++; + + cdio_debug ("start lsn: %lu sector count: %0lu -> %8ld (%08lx)", + (long unsigned int) start_lsn, + (long unsigned int) sec_count, + (long unsigned int) img_offset, + (long unsigned int) img_offset); +} + + +/* + Disk and track information for a Nero file are located at the end + of the file. This routine extracts that information. + + FIXME: right now psz_nrg_name is not used. It will be in the future. + */ +static bool +parse_nrg (_img_private_t *env, const char *psz_nrg_name) +{ + long unsigned int footer_start; + long unsigned int size; + char *footer_buf = NULL; + cdio_log_level_t log_level = (NULL == env) ? CDIO_LOG_INFO : CDIO_LOG_WARN; + + size = cdio_stream_stat (env->gen.data_source); + if (-1 == size) return false; + + { + _footer_t buf; + cdio_assert (sizeof (buf) == 12); + + cdio_stream_seek (env->gen.data_source, size - sizeof (buf), SEEK_SET); + cdio_stream_read (env->gen.data_source, (void *) &buf, sizeof (buf), 1); + + if (buf.v50.ID == UINT32_TO_BE (NERO_ID)) { + cdio_info ("detected Nero version 5.0 (32-bit offsets) NRG magic"); + footer_start = uint32_to_be (buf.v50.footer_ofs); + } else if (buf.v55.ID == UINT32_TO_BE (NER5_ID)) { + cdio_info ("detected Nero version 5.5.x (64-bit offsets) NRG magic"); + footer_start = uint64_from_be (buf.v55.footer_ofs); + } else { + cdio_log (log_level, "Image not recognized as either version 5.0 or " + "version 5.5.x-6.x type NRG"); + return false; + } + + cdio_debug (".NRG footer start = %ld, length = %ld", + (long) footer_start, (long) (size - footer_start)); + + cdio_assert (IN ((size - footer_start), 0, 4096)); + + footer_buf = _cdio_malloc (size - footer_start); + + cdio_stream_seek (env->gen.data_source, footer_start, SEEK_SET); + cdio_stream_read (env->gen.data_source, footer_buf, + size - footer_start, 1); + } + { + int pos = 0; + + while (pos < size - footer_start) { + _chunk_t *chunk = (void *) (footer_buf + pos); + uint32_t opcode = UINT32_FROM_BE (chunk->id); + + bool break_out = false; + + switch (opcode) { + + case CUES_ID: /* "CUES" Seems to have sector size 2336 and 150 sector + pregap seems to be included at beginning of image. + */ + case CUEX_ID: /* "CUEX" */ + { + unsigned entries = UINT32_FROM_BE (chunk->len); + _cuex_array_t *_entries = (void *) chunk->data; + + cdio_assert (env->mapping == NULL); + + cdio_assert ( sizeof (_cuex_array_t) == 8 ); + cdio_assert ( UINT32_FROM_BE (chunk->len) % sizeof(_cuex_array_t) + == 0 ); + + entries /= sizeof (_cuex_array_t); + + if (CUES_ID == opcode) { + lsn_t lsn = UINT32_FROM_BE (_entries[0].lsn); + int idx; + + cdio_info ("CUES type image detected" ); + + /* CUES LSN has 150 pregap include at beginning? -/ + cdio_assert (lsn == 0?); + */ + + env->is_cues = true; /* HACK alert. */ + env->gen.i_tracks = 0; + env->gen.i_first_track = 1; + for (idx = 1; idx < entries-1; idx += 2) { + lsn_t sec_count; + int addrtype = _entries[idx].addr_ctrl / 16; + int control = _entries[idx].addr_ctrl % 16; + int flags = 0; + if ( 1 == control ) + flags &= ~CDIO_TRACK_FLAG_COPY_PERMITTED; + + cdio_assert (_entries[idx].track == _entries[idx + 1].track); + + /* lsn and sec_count*2 aren't correct, but it comes closer on the + single example I have: svcdgs.nrg + We are picking up the wrong fields and/or not interpreting + them correctly. + */ + + switch (addrtype) { + case 0: + lsn = UINT32_FROM_BE (_entries[idx].lsn); + break; + case 1: + { +#if 0 + msf_t msf = (msf_t) _entries[idx].lsn; + lsn = cdio_msf_to_lsn(&msf); +#else + lsn = CDIO_INVALID_LSN; +#endif + cdio_warn ("untested (i.e. probably wrong) CUE MSF code"); + break; + } + default: + lsn = CDIO_INVALID_LSN; + cdio_warn("unknown addrtype %d", addrtype); + } + + sec_count = UINT32_FROM_BE (_entries[idx + 1].lsn); + + _register_mapping (env, lsn, sec_count*2, + (lsn+CDIO_PREGAP_SECTORS) * M2RAW_SECTOR_SIZE, + M2RAW_SECTOR_SIZE, TRACK_FORMAT_XA, true, + flags); + } + } else { + lsn_t lsn = UINT32_FROM_BE (_entries[0].lsn); + int idx; + + cdio_info ("CUEX type image detected"); + + /* LSN must start at -150 (LBA 0)? */ + cdio_assert (lsn == -150); + + for (idx = 2; idx < entries; idx += 2) { + lsn_t sec_count; + int addrtype = _entries[idx].addr_ctrl >> 4; + int control = _entries[idx].addr_ctrl & 0xf; + int flags = 0; + if ( 1 == control ) + flags &= ~CDIO_TRACK_FLAG_COPY_PERMITTED; + + /* extractnrg.pl has addrtype for LBA's 0, and + for MSF 1. ??? + + FIXME: Should decode as appropriate for addrtype. + */ + cdio_assert ( addrtype == 0 || addrtype == 1 ); + + cdio_assert (_entries[idx].track != _entries[idx + 1].track); + + lsn = UINT32_FROM_BE (_entries[idx].lsn); + sec_count = UINT32_FROM_BE (_entries[idx + 1].lsn); + + _register_mapping (env, lsn, sec_count - lsn, + (lsn + CDIO_PREGAP_SECTORS)*M2RAW_SECTOR_SIZE, + M2RAW_SECTOR_SIZE, TRACK_FORMAT_XA, true, + flags); + } + } + break; + } + + case DAOX_ID: /* "DAOX" */ + case DAOI_ID: /* "DAOI" */ + { + track_format_t track_format; + int form2; + + /* We include an extra 0 byte so these can be used as C strings.*/ + env->psz_mcn = _cdio_malloc (CDIO_MCN_SIZE+1); + + if (DAOX_ID == opcode) { + _daox_array_t *_entries = (void *) chunk->data; + form2 = _entries->_unknown[1]; + env->dtyp = _entries->_unknown[19]; + memcpy(env->psz_mcn, &(_entries->psz_mcn), CDIO_MCN_SIZE); + env->psz_mcn[CDIO_MCN_SIZE] = '\0'; + } else { + _daoi_array_t *_entries = (void *) chunk->data; + form2 = _entries->_unknown[1]; + env->dtyp = _entries->_unknown[19]; + memcpy(env->psz_mcn, &(_entries->psz_mcn), CDIO_MCN_SIZE); + env->psz_mcn[CDIO_MCN_SIZE] = '\0'; + } + + env->is_dao = true; + cdio_debug ("DAO%c tag detected, track format %d, form %x\n", + opcode==DAOX_ID ? 'X': 'I', env->dtyp, form2); + switch (env->dtyp) { + case 0: + /* Mode 1 */ + track_format = TRACK_FORMAT_DATA; + env->disc_mode = CDIO_DISC_MODE_CD_DATA; + break; + case 2: + /* Mode 2 form 1 */ + form2 = 0; + track_format = TRACK_FORMAT_XA; + env->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case 3: + /* Mode 2 */ + track_format = TRACK_FORMAT_XA; + env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ?? */ + break; + case 0x6: + /* Mode2 form mix */ + track_format = TRACK_FORMAT_XA; + env->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + case 0x20: /* ??? Mode2 form 2, Mode2 raw?? */ + track_format = TRACK_FORMAT_XA; + env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ??. */ + break; + case 0x7: + track_format = TRACK_FORMAT_AUDIO; + env->disc_mode = CDIO_DISC_MODE_CD_DA; + break; + default: + cdio_log (log_level, "Unknown track format %x\n", + env->dtyp); + track_format = TRACK_FORMAT_AUDIO; + } + if (0 == form2) { + int i; + for (i=0; i<env->gen.i_tracks; i++) { + cdtext_init (&(env->gen.cdtext_track[i])); + env->tocent[i].track_format= track_format; + env->tocent[i].datastart = 0; + env->tocent[i].track_green = false; + if (TRACK_FORMAT_AUDIO == track_format) { + env->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW; + env->tocent[i].datasize = CDIO_CD_FRAMESIZE_RAW; + env->tocent[i].endsize = 0; + } else { + env->tocent[i].datasize = CDIO_CD_FRAMESIZE; + env->tocent[i].datastart = 0; + } + } + } else if (2 == form2) { + int i; + for (i=0; i<env->gen.i_tracks; i++) { + cdtext_init (&(env->gen.cdtext_track[i])); + env->tocent[i].track_green = true; + env->tocent[i].track_format= track_format; + env->tocent[i].datasize = CDIO_CD_FRAMESIZE; + if (TRACK_FORMAT_XA == track_format) { + env->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE; + env->tocent[i].endsize = CDIO_CD_SYNC_SIZE + + CDIO_CD_ECC_SIZE; + } else { + env->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + env->tocent[i].endsize = CDIO_CD_EDC_SIZE + + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE; + + } + } + } else { + cdio_log (log_level, "Don't know if form1 or form2 form2: %x\n", + form2); + } + break; + } + case NERO_ID: + case NER5_ID: + cdio_error ("unexpected nrg magic ID NER%c detected", + opcode==NERO_ID ? 'O': '5'); + free(footer_buf); + return false; + break; + + case END1_ID: /* "END!" */ + cdio_debug ("nrg end tag detected"); + break_out = true; + break; + + case ETNF_ID: /* "ETNF" */ { + unsigned entries = UINT32_FROM_BE (chunk->len); + _etnf_array_t *_entries = (void *) chunk->data; + + cdio_assert (env->mapping == NULL); + + cdio_assert ( sizeof (_etnf_array_t) == 20 ); + cdio_assert ( UINT32_FROM_BE(chunk->len) % sizeof(_etnf_array_t) + == 0 ); + + entries /= sizeof (_etnf_array_t); + + cdio_info ("SAO type image (ETNF) detected"); + + { + int idx; + for (idx = 0; idx < entries; idx++) { + uint32_t _len = UINT32_FROM_BE (_entries[idx].length); + uint32_t _start = UINT32_FROM_BE (_entries[idx].start_lsn); + uint32_t _start2 = UINT32_FROM_BE (_entries[idx].start); + uint32_t track_mode= uint32_from_be (_entries[idx].type); + bool track_green = true; + track_format_t track_format = TRACK_FORMAT_XA; + uint16_t blocksize; + + switch (track_mode) { + case 0: + /* Mode 1 */ + track_format = TRACK_FORMAT_DATA; + track_green = false; /* ?? */ + blocksize = CDIO_CD_FRAMESIZE; + env->disc_mode = CDIO_DISC_MODE_CD_DATA; + break; + case 2: + /* Mode 2 form 1 */ + track_format = TRACK_FORMAT_XA; + track_green = false; /* ?? */ + blocksize = CDIO_CD_FRAMESIZE; + env->disc_mode = CDIO_DISC_MODE_CD_XA; + break; + case 3: + /* Mode 2 */ + track_format = TRACK_FORMAT_XA; + track_green = true; + blocksize = M2RAW_SECTOR_SIZE; + env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ?? */ + break; + case 06: + /* Mode2 form mix */ + track_format = TRACK_FORMAT_XA; + track_green = true; + blocksize = M2RAW_SECTOR_SIZE; + env->disc_mode = CDIO_DISC_MODE_CD_MIXED; + break; + case 0x20: /* ??? Mode2 form 2, Mode2 raw?? */ + track_format = TRACK_FORMAT_XA; + track_green = true; + blocksize = M2RAW_SECTOR_SIZE; + env->disc_mode = CDIO_DISC_MODE_CD_XA; /* ??. */ + break; + case 7: + track_format = TRACK_FORMAT_AUDIO; + track_green = false; + blocksize = CDIO_CD_FRAMESIZE_RAW; + env->disc_mode = CDIO_DISC_MODE_CD_DA; + break; + default: + cdio_log (log_level, + "Don't know how to handle track mode (%lu)?", + (long unsigned int) track_mode); + free(footer_buf); + return false; + } + + cdio_assert (_len % blocksize == 0); + + _len /= blocksize; + + cdio_assert (_start * blocksize == _start2); + + _start += idx * CDIO_PREGAP_SECTORS; + _register_mapping (env, _start, _len, _start2, blocksize, + track_format, track_green, 0); + + } + } + break; + } + + case ETN2_ID: { /* "ETN2", same as above, but with 64bit stuff instead */ + unsigned entries = uint32_from_be (chunk->len); + _etn2_array_t *_entries = (void *) chunk->data; + + cdio_assert (env->mapping == NULL); + + cdio_assert (sizeof (_etn2_array_t) == 32); + cdio_assert (uint32_from_be (chunk->len) % sizeof (_etn2_array_t) == 0); + + entries /= sizeof (_etn2_array_t); + + cdio_info ("SAO type image (ETN2) detected"); + + { + int idx; + for (idx = 0; idx < entries; idx++) { + uint32_t _len = uint64_from_be (_entries[idx].length); + uint32_t _start = uint32_from_be (_entries[idx].start_lsn); + uint32_t _start2 = uint64_from_be (_entries[idx].start); + uint32_t track_mode= uint32_from_be (_entries[idx].type); + bool track_green = true; + track_format_t track_format = TRACK_FORMAT_XA; + uint16_t blocksize; + + + switch (track_mode) { + case 0: + track_format = TRACK_FORMAT_DATA; + track_green = false; /* ?? */ + blocksize = CDIO_CD_FRAMESIZE; + break; + case 2: + track_format = TRACK_FORMAT_XA; + track_green = false; /* ?? */ + blocksize = CDIO_CD_FRAMESIZE; + break; + case 3: + track_format = TRACK_FORMAT_XA; + track_green = true; + blocksize = M2RAW_SECTOR_SIZE; + break; + case 7: + track_format = TRACK_FORMAT_AUDIO; + track_green = false; + blocksize = CDIO_CD_FRAMESIZE_RAW; + break; + default: + cdio_log (log_level, + "Don't know how to handle track mode (%lu)?", + (long unsigned int) track_mode); + free(footer_buf); + return false; + } + + if (_len % blocksize != 0) { + cdio_log (log_level, + "length is not a multiple of blocksize " + "len %lu, size %d, rem %lu", + (long unsigned int) _len, blocksize, + (long unsigned int) _len % blocksize); + if (0 == _len % CDIO_CD_FRAMESIZE) { + cdio_log(log_level, "Adjusting blocksize to %d", + CDIO_CD_FRAMESIZE); + blocksize = CDIO_CD_FRAMESIZE; + } else if (0 == _len % M2RAW_SECTOR_SIZE) { + cdio_log(log_level, + "Adjusting blocksize to %d", M2RAW_SECTOR_SIZE); + blocksize = M2RAW_SECTOR_SIZE; + } else if (0 == _len % CDIO_CD_FRAMESIZE_RAW) { + cdio_log(log_level, + "Adjusting blocksize to %d", CDIO_CD_FRAMESIZE_RAW); + blocksize = CDIO_CD_FRAMESIZE_RAW; + } + } + + _len /= blocksize; + + if (_start * blocksize != _start2) { + cdio_log (log_level, + "%lu * %d != %lu", + (long unsigned int) _start, blocksize, + (long unsigned int) _start2); + if (_start * CDIO_CD_FRAMESIZE == _start2) { + cdio_log(log_level, + "Adjusting blocksize to %d", CDIO_CD_FRAMESIZE); + blocksize = CDIO_CD_FRAMESIZE; + } else if (_start * M2RAW_SECTOR_SIZE == _start2) { + cdio_log(log_level, + "Adjusting blocksize to %d", M2RAW_SECTOR_SIZE); + blocksize = M2RAW_SECTOR_SIZE; + } else if (_start * CDIO_CD_FRAMESIZE_RAW == _start2) { + cdio_log(log_level, + "Adjusting blocksize to %d", CDIO_CD_FRAMESIZE_RAW); + blocksize = CDIO_CD_FRAMESIZE_RAW; + } + } + + _start += idx * CDIO_PREGAP_SECTORS; + _register_mapping (env, _start, _len, _start2, blocksize, + track_format, track_green, 0); + } + } + break; + } + + case SINF_ID: { /* "SINF" */ + + uint32_t *_sessions = (void *) chunk->data; + + cdio_assert (UINT32_FROM_BE (chunk->len) == 4); + + cdio_debug ("SINF: %lu sessions", + (long unsigned int) UINT32_FROM_BE (*_sessions)); + } + break; + + case MTYP_ID: { /* "MTYP" */ + uint32_t *mtyp_p = (void *) chunk->data; + uint32_t mtyp = UINT32_FROM_BE (*mtyp_p); + + cdio_assert (UINT32_FROM_BE (chunk->len) == 4); + + cdio_debug ("MTYP: %lu", + (long unsigned int) UINT32_FROM_BE (*mtyp_p)); + + if (mtyp != MTYP_AUDIO_CD) { + cdio_log (log_level, + "Unknown MTYP value: %u", (unsigned int) mtyp); + } + env->mtyp = mtyp; + } + break; + + case CDTX_ID: { /* "CD TEXT" */ + + cdio_log (log_level, + "Don't know how to handle CD TEXT yet" ); + break; + } + + default: + cdio_log (log_level, + "unknown tag %8.8x seen", + (unsigned int) UINT32_FROM_BE (chunk->id)); + break; + } + + if (break_out) + break; + + pos += 8; + pos += UINT32_FROM_BE (chunk->len); + } + } + + /* Fake out leadout track. */ + /* Don't use _stat_size_nrg since that will lead to recursion since + we haven't fully initialized things yet. + */ + cdio_lsn_to_msf (env->size, &env->tocent[env->gen.i_tracks].start_msf); + env->tocent[env->gen.i_tracks].start_lba = cdio_lsn_to_lba(env->size); + env->tocent[env->gen.i_tracks-1].sec_count = + cdio_lsn_to_lba(env->size - env->tocent[env->gen.i_tracks-1].start_lba); + + env->gen.b_cdtext_init = true; + env->gen.b_cdtext_error = false; + env->gen.toc_init = true; + free(footer_buf); + return true; +} + +/*! + Initialize image structures. + */ +static bool +_init_nrg (_img_private_t *env) +{ + if (env->gen.init) { + cdio_error ("init called more than once"); + return false; + } + + if (!(env->gen.data_source = cdio_stdio_new (env->gen.source_name))) { + cdio_warn ("can't open nrg image file %s for reading", + env->gen.source_name); + return false; + } + + env->psz_mcn = NULL; + env->disc_mode = CDIO_DISC_MODE_NO_INFO; + + cdtext_init (&(env->gen.cdtext)); + + if ( !parse_nrg (env, env->gen.source_name) ) { + cdio_warn ("image file %s is not a Nero image", + env->gen.source_name); + return false; + } + + env->gen.init = true; + return true; + +} + +/*! + Reads into buf the next size bytes. + Returns -1 on error. + Would be libc's seek() but we have to adjust for the extra track header + information in each sector. +*/ +static off_t +_lseek_nrg (void *user_data, off_t offset, int whence) +{ + _img_private_t *env = user_data; + + /* real_offset is the real byte offset inside the disk image + The number below was determined empirically. + */ + off_t real_offset= env->is_dao ? 0x4b000 : 0; + + unsigned int i; + + for (i=0; i<env->gen.i_tracks; i++) { + track_info_t *this_track=&(env->tocent[i]); + env->pos.index = i; + if ( (this_track->sec_count*this_track->datasize) >= offset) { + int blocks = offset / this_track->datasize; + int rem = offset % this_track->datasize; + int block_offset = blocks * this_track->blocksize; + real_offset += block_offset + rem; + env->pos.buff_offset = rem; + env->pos.lba += blocks; + break; + } + real_offset += this_track->sec_count*this_track->blocksize; + offset -= this_track->sec_count*this_track->datasize; + env->pos.lba += this_track->sec_count; + } + + if (i==env->gen.i_tracks) { + cdio_warn ("seeking outside range of disk image"); + return -1; + } else + real_offset += env->tocent[i].datastart; + return cdio_stream_seek(env->gen.data_source, real_offset, whence); +} + +/*! + Reads into buf the next size bytes. + Returns -1 on error. + FIXME: + At present we assume a read doesn't cross sector or track + boundaries. +*/ +static ssize_t +_read_nrg (void *user_data, void *buf, size_t size) +{ + _img_private_t *env = user_data; + return cdio_stream_read(env->gen.data_source, buf, size, 1); +} + +static uint32_t +_stat_size_nrg (void *user_data) +{ + _img_private_t *env = user_data; + + return env->size; +} + +/*! + Reads a single audio sector from CD device into data starting + from LSN. Returns 0 if no error. + */ +static int +_read_audio_sectors_nrg (void *user_data, void *data, lsn_t lsn, + unsigned int nblocks) +{ + _img_private_t *env = user_data; + + CdioListNode *node; + + if (lsn >= env->size) + { + cdio_warn ("trying to read beyond image size (%lu >= %lu)", + (long unsigned int) lsn, (long unsigned int) env->size); + return -1; + } + + _CDIO_LIST_FOREACH (node, env->mapping) { + _mapping_t *_map = _cdio_list_node_data (node); + + if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) { + int ret; + long int img_offset = _map->img_offset; + + img_offset += (lsn - _map->start_lsn) * CDIO_CD_FRAMESIZE_RAW; + + ret = cdio_stream_seek (env->gen.data_source, img_offset, + SEEK_SET); + if (ret!=0) return ret; + ret = cdio_stream_read (env->gen.data_source, data, + CDIO_CD_FRAMESIZE_RAW, nblocks); + if (ret==0) return ret; + break; + } + } + + if (!node) cdio_warn ("reading into pre gap (lsn %lu)", + (long unsigned int) lsn); + + return 0; +} + +static int +_read_mode1_sector_nrg (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *env = user_data; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + + CdioListNode *node; + + if (lsn >= env->size) + { + cdio_warn ("trying to read beyond image size (%lu >= %lu)", + (long unsigned int) lsn, (long unsigned int) env->size); + return -1; + } + + _CDIO_LIST_FOREACH (node, env->mapping) { + _mapping_t *_map = _cdio_list_node_data (node); + + if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) { + int ret; + long int img_offset = _map->img_offset; + + img_offset += (lsn - _map->start_lsn) * _map->blocksize; + + ret = cdio_stream_seek (env->gen.data_source, img_offset, + SEEK_SET); + if (ret!=0) return ret; + + /* FIXME: Not completely sure the below is correct. */ + ret = cdio_stream_read (env->gen.data_source, + (M2RAW_SECTOR_SIZE == _map->blocksize) + ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE) + : buf, + _map->blocksize, 1); + if (ret==0) return ret; + break; + } + } + + if (!node) + cdio_warn ("reading into pre gap (lsn %lu)", (long unsigned int) lsn); + + memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE, + b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); + + return 0; +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_read_mode1_sectors_nrg (void *user_data, void *data, lsn_t lsn, + bool b_form2, unsigned nblocks) +{ + _img_private_t *env = user_data; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _read_mode1_sector_nrg (env, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +static int +_read_mode2_sector_nrg (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *env = user_data; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + + CdioListNode *node; + + if (lsn >= env->size) + { + cdio_warn ("trying to read beyond image size (%lu >= %lu)", + (long unsigned int) lsn, (long unsigned int) env->size); + return -1; + } + + _CDIO_LIST_FOREACH (node, env->mapping) { + _mapping_t *_map = _cdio_list_node_data (node); + + if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1))) { + int ret; + long int img_offset = _map->img_offset; + + img_offset += (lsn - _map->start_lsn) * _map->blocksize; + + ret = cdio_stream_seek (env->gen.data_source, img_offset, + SEEK_SET); + if (ret!=0) return ret; + ret = cdio_stream_read (env->gen.data_source, + (M2RAW_SECTOR_SIZE == _map->blocksize) + ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE) + : buf, + _map->blocksize, 1); + if (ret==0) return ret; + break; + } + } + + if (!node) + cdio_warn ("reading into pre gap (lsn %lu)", (long unsigned int) lsn); + + if (b_form2) + memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE, + M2RAW_SECTOR_SIZE); + else + memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE); + + return 0; +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_read_mode2_sectors_nrg (void *user_data, void *data, lsn_t lsn, + bool b_form2, unsigned nblocks) +{ + _img_private_t *env = user_data; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _read_mode2_sector_nrg (env, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +/* + Free memory resources associated with NRG object. +*/ +static void +_free_nrg (void *user_data) +{ + _img_private_t *env = user_data; + + if (NULL == env) return; + if (NULL != env->mapping) + _cdio_list_free (env->mapping, true); + + /* The remaining part of the image is like the other image drivers, + so free that in the same way. */ + _free_image(user_data); +} + +/*! + Eject media -- there's nothing to do here except free resources. + We always return 2. + */ +static int +_eject_media_nrg(void *obj) +{ + _free_nrg (obj); + return 2; +} + +/*! + Return an array of strings giving possible NRG disk images. + */ +char ** +cdio_get_devices_nrg (void) +{ + char **drives = NULL; + unsigned int num_files=0; +#ifdef HAVE_GLOB_H + unsigned int i; + glob_t globbuf; + globbuf.gl_offs = 0; + glob("*.nrg", GLOB_DOOFFS, NULL, &globbuf); + for (i=0; i<globbuf.gl_pathc; i++) { + cdio_add_device_list(&drives, globbuf.gl_pathv[i], &num_files); + } + globfree(&globbuf); +#else + cdio_add_device_list(&drives, DEFAULT_CDIO_DEVICE, &num_files); +#endif /*HAVE_GLOB_H*/ + cdio_add_device_list(&drives, NULL, &num_files); + return drives; +} + +/*! + Return a string containing the default CD device. + */ +char * +cdio_get_default_device_nrg(void) +{ + char **drives = cdio_get_devices_nrg(); + char *drive = (drives[0] == NULL) ? NULL : strdup(drives[0]); + cdio_free_device_list(drives); + return drive; +} + +static bool +get_hwinfo_nrg ( const CdIo *p_cdio, /*out*/ cdio_hwinfo_t *hw_info) +{ + strcpy(hw_info->psz_vendor, "libcdio"); + strcpy(hw_info->psz_model, "Nero"); + strcpy(hw_info->psz_revision, CDIO_VERSION); + return true; + +} + +/*! + Return the number of tracks in the current medium. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_format_t +get_track_format_nrg(void *user_data, track_t track_num) +{ + _img_private_t *env = user_data; + + if (track_num > env->gen.i_tracks || track_num == 0) + return TRACK_FORMAT_ERROR; + + if ( env->dtyp != DTYP_INVALID) { + switch (env->dtyp) { + case DTYP_MODE2_XA: + return TRACK_FORMAT_XA; + case DTYP_MODE1: + return TRACK_FORMAT_DATA; + default: ; + } + } + + /*if ( MTYP_AUDIO_CD == env->mtyp) return TRACK_FORMAT_AUDIO; */ + return env->tocent[track_num-1].track_format; +} + +/*! + Return true if we have XA data (green, mode2 form1) or + XA data (green, mode2 form2). That is track begins: + sync - header - subheader + 12 4 - 8 + + FIXME: there's gotta be a better design for this and get_track_format? +*/ +static bool +_get_track_green_nrg(void *user_data, track_t track_num) +{ + _img_private_t *env = user_data; + + if (track_num > env->gen.i_tracks || track_num == 0) + return false; + + if ( MTYP_AUDIO_CD == env->mtyp) return false; + return env->tocent[track_num-1].track_green; +} + +/*! + Check that a NRG file is valid. + +*/ +/* Later we'll probably do better. For now though, this gets us + started for now. +*/ +bool +cdio_is_nrg(const char *psz_nrg) +{ + unsigned int i; + + if (psz_nrg == NULL) return false; + + i=strlen(psz_nrg)-strlen("nrg"); + + if (i>0) { + if (psz_nrg[i]=='n' && psz_nrg[i+1]=='r' && psz_nrg[i+2]=='g') { + return true; + } + else if (psz_nrg[i]=='N' && psz_nrg[i+1]=='R' && psz_nrg[i+2]=='G') { + return true; + } + } + return false; +} + +/*! + Initialization routine. This is the only thing that doesn't + get called via a function pointer. In fact *we* are the + ones to set that up. + */ +CdIo * +cdio_open_am_nrg (const char *psz_source_name, const char *psz_access_mode) +{ + if (psz_access_mode != NULL && strcmp(psz_access_mode, "image")) + cdio_warn ("there is only one access mode for nrg. Arg %s ignored", + psz_access_mode); + return cdio_open_nrg(psz_source_name); +} + + +CdIo * +cdio_open_nrg (const char *psz_source) +{ + CdIo *ret; + _img_private_t *_data; + + cdio_funcs _funcs; + + memset( &_funcs, 0, sizeof(_funcs) ); + + _funcs.eject_media = _eject_media_nrg; + _funcs.free = _free_nrg; + _funcs.get_arg = _get_arg_image; + _funcs.get_cdtext = get_cdtext_generic; + _funcs.get_devices = cdio_get_devices_nrg; + _funcs.get_default_device = cdio_get_default_device_nrg; + _funcs.get_discmode = _get_discmode_image; + _funcs.get_drive_cap = _get_drive_cap_image; + _funcs.get_first_track_num= _get_first_track_num_image; + _funcs.get_hwinfo = get_hwinfo_nrg; + _funcs.get_mcn = _get_mcn_image; + _funcs.get_num_tracks = _get_num_tracks_image; + _funcs.get_track_format = get_track_format_nrg; + _funcs.get_track_green = _get_track_green_nrg; + _funcs.get_track_lba = NULL; /* Will use generic routine via msf */ + _funcs.get_track_msf = _get_track_msf_image; + _funcs.lseek = _lseek_nrg; + _funcs.read = _read_nrg; + _funcs.read_audio_sectors = _read_audio_sectors_nrg; + _funcs.read_mode1_sector = _read_mode1_sector_nrg; + _funcs.read_mode1_sectors = _read_mode1_sectors_nrg; + _funcs.read_mode2_sector = _read_mode2_sector_nrg; + _funcs.read_mode2_sectors = _read_mode2_sectors_nrg; + _funcs.set_arg = _set_arg_image; + _funcs.stat_size = _stat_size_nrg; + + _data = _cdio_malloc (sizeof (_img_private_t)); + _data->gen.init = false; + + _data->gen.i_tracks = 0; + _data->mtyp = 0; + _data->dtyp = DTYP_INVALID; + _data->gen.i_first_track= 1; + _data->is_dao = false; + _data->is_cues = false; /* FIXME: remove is_cues. */ + + ret = cdio_new ((void *)_data, &_funcs); + + if (ret == NULL) { + free(_data); + return NULL; + } + + _set_arg_image(_data, "source", (NULL == psz_source) + ? DEFAULT_CDIO_DEVICE: psz_source); + + _data->psz_cue_name = strdup(_get_arg_image(_data, "source")); + + if (!cdio_is_nrg(_data->psz_cue_name)) { + cdio_debug ("source name %s is not recognized as a NRG image", + _data->psz_cue_name); + _free_nrg(_data); + return NULL; + } + + _set_arg_image (_data, "cue", _data->psz_cue_name); + + if (_init_nrg(_data)) + return ret; + else { + _free_nrg(_data); + free(ret); + return NULL; + } + +} + +bool +cdio_have_nrg (void) +{ + return true; +} diff --git a/contrib/libcdio/image/nrg.h b/contrib/libcdio/image/nrg.h new file mode 100644 index 000000000..2923c78f1 --- /dev/null +++ b/contrib/libcdio/image/nrg.h @@ -0,0 +1,115 @@ +/* + $Id: nrg.h,v 1.1 2005/01/01 02:43:59 rockyb Exp $ + + Copyright (C) 2004 Rocky Bernstein <rocky@panix.com> + Copyright (C) 2001, 2003 Herbert Valerio Riedel <hvr@gnu.org> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* NERO (NRG) file format structures. */ + +/* this ugly image format is typical for lazy win32 programmers... at + least structure were set big endian, so at reverse + engineering wasn't such a big headache... */ + +PRAGMA_BEGIN_PACKED +typedef union { + struct { + uint32_t __x GNUC_PACKED; + uint32_t ID GNUC_PACKED; + uint32_t footer_ofs GNUC_PACKED; + } v50; + struct { + uint32_t ID GNUC_PACKED; + uint64_t footer_ofs GNUC_PACKED; + } v55; +} _footer_t; + +typedef struct { + uint32_t start GNUC_PACKED; + uint32_t length GNUC_PACKED; + uint32_t type GNUC_PACKED; /* 0x0 -> MODE1, 0x2 -> MODE2 form1, + 0x3 -> MIXED_MODE2 2336 blocksize + */ + uint32_t start_lsn GNUC_PACKED; /* does not include any pre-gaps! */ + uint32_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */ +} _etnf_array_t; + +/* Finally they realized that 32-bit offsets are a bit outdated for + IA64 *eg* */ +typedef struct { + uint64_t start GNUC_PACKED; + uint64_t length GNUC_PACKED; + uint32_t type GNUC_PACKED; /* 0x0 -> MODE1, 0x2 -> MODE2 form1, + 0x3 -> MIXED_MODE2 2336 blocksize + */ + uint32_t start_lsn GNUC_PACKED; + uint64_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */ +} _etn2_array_t; + +typedef struct { + uint8_t type GNUC_PACKED; /* has track copy bit and whether audiofile + or datafile. Is often 0x41 == 'A' */ + uint8_t track GNUC_PACKED; /* binary or BCD?? */ + uint8_t addr_ctrl GNUC_PACKED; /* addresstype: MSF or LBA in lower 4 bits + control in upper 4 bits. + makes 0->1 transitions */ + uint8_t res GNUC_PACKED; /* ?? */ + uint32_t lsn GNUC_PACKED; +} _cuex_array_t; + +typedef struct { + uint32_t _unknown1 GNUC_PACKED; + char psz_mcn[CDIO_MCN_SIZE] GNUC_PACKED; + uint8_t _unknown[64-CDIO_MCN_SIZE-sizeof(uint32_t)] GNUC_PACKED; +} _daox_array_t; + +typedef struct { + uint32_t _unknown1 GNUC_PACKED; + char psz_mcn[CDIO_MCN_SIZE] GNUC_PACKED; + uint8_t _unknown[64-CDIO_MCN_SIZE-sizeof(uint32_t)] GNUC_PACKED; +} _daoi_array_t; + +typedef struct { + uint32_t id GNUC_PACKED; + uint32_t len GNUC_PACKED; + char data[EMPTY_ARRAY_SIZE] GNUC_PACKED; +} _chunk_t; + +PRAGMA_END_PACKED + +/* Nero images are Big Endian. */ +#define CDTX_ID 0x43445458 /* CD TEXT */ +#define CUEX_ID 0x43554558 /* Nero version 5.5.x-6.x */ +#define CUES_ID 0x43554553 /* Nero pre version 5.5.x-6.x */ +#define DAOX_ID 0x44414f58 /* Nero version 5.5.x-6.x */ +#define DAOI_ID 0x44414f49 +#define END1_ID 0x454e4421 +#define ETN2_ID 0x45544e32 +#define ETNF_ID 0x45544e46 +#define NER5_ID 0x4e455235 /* Nero version 5.5.x */ +#define NERO_ID 0x4e45524f /* Nero pre 5.5.x */ +#define SINF_ID 0x53494e46 /* Session information */ +#define MTYP_ID 0x4d545950 /* Disc Media type? */ + +#define MTYP_AUDIO_CD 1 /* This isn't correct. But I don't know the + the right thing is and it sometimes works (and + sometimes is wrong). */ + +/* Disk track type Values gleaned from DAOX */ +#define DTYP_MODE1 0 +#define DTYP_MODE2_XA 2 +#define DTYP_INVALID 255 |