static int CDDAReader_init(cdio_CDDAReader *self, PyObject *args, PyObject *kwds) { char *device = NULL; struct stat buf; self->is_cd_image = 0; self->is_logging = 0; self->dealloc = NULL; self->closed = 0; self->audiotools_pcm = NULL; cddareader_reset_log(&(self->log)); if (!PyArg_ParseTuple(args, "s|i", &device, &(self->is_logging))) return -1; if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL) return -1; /*identify whether drive is physical or a CD image*/ if (stat(device, &buf)) { PyErr_SetFromErrno(PyExc_IOError); return -1; } if (S_ISREG(buf.st_mode)) { if (cdio_is_cuefile(device) || cdio_is_binfile(device) || cdio_is_tocfile(device) || cdio_is_nrg(device)) { /*open CD image and set function pointers*/ self->is_cd_image = 1; self->is_logging = 0; return CDDAReader_init_image(self, device); } else { /*unsupported file*/ PyErr_SetString(PyExc_ValueError, "unsupported CD image type"); return -1; } } else if (S_ISBLK(buf.st_mode)) { if (cdio_is_device(device, DRIVER_LINUX)) { /*open CD device and set function pointers*/ self->is_cd_image = 0; return CDDAReader_init_device(self, device); } else { /*unsupported block device*/ PyErr_SetString(PyExc_ValueError, "unsupported block device"); return -1; } } else { /*unsupported file type*/ PyErr_SetString(PyExc_ValueError, "unsupported file type"); return -1; } }
static PyObject* cdio_identify_cdrom(PyObject *dummy, PyObject *args) { const char* device; struct stat buf; if (!PyArg_ParseTuple(args, "s", &device)) return NULL; if (stat(device, &buf)) { PyErr_SetFromErrno(PyExc_IOError); return NULL; } if (S_ISREG(buf.st_mode)) { if (cdio_is_cuefile(device)) { return Py_BuildValue("i", CD_IMAGE | CUE_FILE); } else if (cdio_is_binfile(device)) { return Py_BuildValue("i", CD_IMAGE | BIN_FILE); } else if (cdio_is_tocfile(device)) { return Py_BuildValue("i", CD_IMAGE | TOC_FILE); } else if (cdio_is_nrg(device)) { return Py_BuildValue("i", CD_IMAGE | NRG_FILE); } else { PyErr_SetString(PyExc_ValueError, "unknown image file"); return NULL; } } else if (S_ISBLK(buf.st_mode)) { if (cdio_is_device(device, DRIVER_LINUX)) { return Py_BuildValue("i", DEVICE_FILE); } else { PyErr_SetString(PyExc_ValueError, "unknown CD device"); return NULL; } } else { PyErr_SetString(PyExc_ValueError, "unknown device"); return NULL; } }
/*! Determine if psz_source refers to a real hardware CD-ROM. @param psz_source location name of object @param driver_id driver for reading object. Use DRIVER_UNKNOWN if you don't know what driver to use. @return true if psz_source is a device; If false is returned we could have a CD disk image. */ bool isDevice(const char *psz_source, driver_id_t driver_id) { return cdio_is_device(psz_source, driver_id); }
int main(int argc, char *argv[]) { uint8_t buffer[CDIO_CD_FRAMESIZE_RAW] = { 0, }; unsigned int blocklen=CDIO_CD_FRAMESIZE_RAW; CdIo *p_cdio=NULL; int output_fd=-1; FILE *output_stream; init(); /* Parse our arguments; every option seen by `parse_opt' will be reflected in `arguments'. */ parse_options(argc, argv); print_version(program_name, VERSION, opts.no_header, opts.version_only); p_cdio = open_input(source_name, opts.source_image, opts.access_mode); if (opts.output_file!=NULL) { /* If hexdump not explicitly set, then don't produce hexdump when writing to a file. */ if (opts.hexdump == 2) opts.hexdump = 0; output_fd = open(opts.output_file, O_WRONLY|O_CREAT|O_TRUNC, 0644); if (-1 == output_fd) { err_exit("Error opening output file %s: %s\n", opts.output_file, strerror(errno)); } } else /* If we are writing to stdout, then the default is to produce a hexdump. */ if (opts.hexdump == 2) opts.hexdump = 1; for ( ; opts.start_lsn <= opts.end_lsn; opts.start_lsn++ ) { switch (opts.read_mode) { case READ_AUDIO: case READ_M1F1: case READ_M1F2: case READ_M2F1: case READ_M2F2: if (DRIVER_OP_SUCCESS != cdio_read_sector(p_cdio, &buffer, opts.start_lsn, (cdio_read_mode_t) opts.read_mode)) { report( stderr, "error reading block %u\n", (unsigned int) opts.start_lsn ); blocklen = 0; } else { switch (opts.read_mode) { case READ_M1F1: blocklen=CDIO_CD_FRAMESIZE; break; case READ_M1F2: blocklen=M2RAW_SECTOR_SIZE; case READ_M2F1: blocklen=CDIO_CD_FRAMESIZE; case READ_M2F2: blocklen=M2F2_SECTOR_SIZE; default: ; } } break; case READ_ANY: { driver_id_t driver_id = cdio_get_driver_id(p_cdio); if (cdio_is_device(source_name, driver_id)) { if (DRIVER_OP_SUCCESS != mmc_read_sectors(p_cdio, &buffer, opts.start_lsn, CDIO_MMC_READ_TYPE_ANY, 1)) { report( stderr, "error reading block %u\n", (unsigned int) opts.start_lsn ); blocklen = 0; } } else { err_exit( "%s: mode 'any' must be used with a real CD-ROM, not an image file.\n", program_name); } } break; case READ_MODE_UNINIT: err_exit("%s: Reading mode not set\n", program_name); break; } if (!opts.output_file) { output_stream = stdout; } else { output_stream = fdopen(output_fd, "w"); } if (opts.hexdump) hexdump(output_stream, buffer, blocklen, opts.just_hex); else if (opts.output_file) write(output_fd, buffer, blocklen); else { unsigned int i; for (i=0; i<blocklen; i++) printf("%c", buffer[i]); } } if (opts.output_file) close(output_fd); myexit(p_cdio, EXIT_SUCCESS); /* Not reached:*/ return(EXIT_SUCCESS); }
int main(int argc, const char *argv[]) { cdrom_drive_t *d = NULL; /* Place to store handle given by cd-parapnioa. */ driver_id_t driver_id; char **ppsz_cd_drives; /* List of all drives with a loaded CDDA in it. */ int i_rc=0; /* See if we can find a device with a loaded CD-DA in it. If successful drive_id will be set. */ ppsz_cd_drives = cdio_get_devices_with_cap_ret(NULL, CDIO_FS_AUDIO, false, &driver_id); if (ppsz_cd_drives && *ppsz_cd_drives) { /* Found such a CD-ROM with a CD-DA loaded. Use the first drive in the list. */ d=cdda_identify(*ppsz_cd_drives, 1, NULL); } else { printf("Unable find or access a CD-ROM drive with an audio CD in it.\n"); exit(SKIP_TEST_RC); } /** We had a bug in is_device when driver_id == DRIVER_UNKNOWN or DRIVER_DEVICE. Let's make sure we've fixed that problem. **/ if (!cdio_is_device(*ppsz_cd_drives, DRIVER_UNKNOWN) || !cdio_is_device(*ppsz_cd_drives, DRIVER_DEVICE)) exit(99); /* Don't need a list of CD's with CD-DA's any more. */ cdio_free_device_list(ppsz_cd_drives); /* We'll set for verbose paranoia messages. */ cdda_verbose_set(d, CDDA_MESSAGE_PRINTIT, CDDA_MESSAGE_PRINTIT); if ( 0 != cdio_cddap_open(d) ) { printf("Unable to open disc.\n"); exit(SKIP_TEST_RC); } /* Okay now set up to read up to the first 300 frames of the first audio track of the Audio CD. */ { cdrom_paranoia_t *p = paranoia_init(d); lsn_t i_first_lsn = cdda_disc_firstsector(d); if ( -1 == i_first_lsn ) { printf("Trouble getting starting LSN\n"); } else { lsn_t i_lsn; /* Current LSN to read */ lsn_t i_last_lsn = cdda_disc_lastsector(d); unsigned int i_sectors = i_last_lsn - i_first_lsn + 1; unsigned int j; unsigned int i_good = 0; unsigned int i_bad = 0; /* Set reading mode for full paranoia, but allow skipping sectors. */ paranoia_modeset(p, PARANOIA_MODE_FULL^PARANOIA_MODE_NEVERSKIP); for ( j=0; j<10; j++ ) { /* Pick a place to start reading. */ i_lsn = i_first_lsn + (rand() % i_sectors); paranoia_seek(p, i_lsn, SEEK_SET); printf("Testing %d sectors starting at %ld\n", MAX_SECTORS, (long int) i_lsn); for ( i = 0; i < MAX_SECTORS && i_lsn <= i_last_lsn; i++, i_lsn++ ) { /* read a sector */ int16_t *p_readbuf = paranoia_read(p, callback); char *psz_err=cdio_cddap_errors(d); char *psz_mes=cdio_cddap_messages(d); memcpy(audio_buf[i], p_readbuf, CDIO_CD_FRAMESIZE_RAW); if (psz_mes || psz_err) printf("%s%s\n", psz_mes ? psz_mes: "", psz_err ? psz_err: ""); if (psz_err) free(psz_err); if (psz_mes) free(psz_mes); if( !p_readbuf ) { printf("paranoia read error. Stopping.\n"); goto out; } } /* Compare with the sectors from paranoia. */ i_lsn -= MAX_SECTORS; for ( i = 0; i < MAX_SECTORS; i++, i_lsn++ ) { uint8_t readbuf[CDIO_CD_FRAMESIZE_RAW] = {0,}; if ( PARANOIA_CB_READ == audio_status[i] || PARANOIA_CB_VERIFY == audio_status[i] ) { /* We read the block via paranoia without an error. */ if ( 0 == cdio_read_audio_sector(d->p_cdio, readbuf, i_lsn) ) { if ( BIGENDIAN != d->bigendianp ) { /* We will compare in the slow, pedantic way*/ int j; for (j=0; j < CDIO_CD_FRAMESIZE_RAW ; j +=2) { if (audio_buf[i][j] != readbuf[j+1] && audio_buf[i][j+1] != readbuf[j] ) { printf("LSN %ld doesn't match\n", (long int) i_lsn); i_bad++; } else { i_good++; } } } else { if ( 0 != memcmp(audio_buf[i], readbuf, CDIO_CD_FRAMESIZE_RAW) ) { printf("LSN %ld doesn't match\n", (long int) i_lsn); i_bad++; } else { i_good++; } } } } else { printf("Skipping LSN %ld because of status: %s\n", (long int) i_lsn, paranoia_cb_mode2str[audio_status[i]]); } } } printf("%u sectors compared okay %u sectors were different\n", i_good, i_bad); if (i_bad > i_good) i_rc = 1; } out: paranoia_free(p); } cdio_cddap_close(d); exit(i_rc); }
/*! Sets up to read from place specified by source_name and driver_id. This should be called before using any other routine, except cdio_init. This will call cdio_init, if that hasn't been done previously. NULL is returned on error. */ CdIo * cdio_open (const char *orig_source_name, driver_id_t driver_id) { char *source_name; if (CdIo_last_driver == -1) cdio_init(); if (NULL == orig_source_name || strlen(orig_source_name)==0) source_name = cdio_get_default_device(NULL); else source_name = strdup(orig_source_name); retry: switch (driver_id) { case DRIVER_UNKNOWN: { CdIo *cdio=scan_for_driver(CDIO_MIN_DRIVER, CDIO_MAX_DRIVER, source_name); if (cdio != NULL && cdio_is_device(source_name, cdio->driver_id)) { driver_id = cdio->driver_id; } else { struct stat buf; if (0 != stat(source_name, &buf)) { return NULL; } if (S_ISREG(buf.st_mode)) { /* FIXME: check to see if is a text file. If so, then set SOURCE_CUE. */ int i=strlen(source_name)-strlen("bin"); if (i > 0 && ( (source_name)[i] =='n' || (source_name)[i] =='N' ) && ( (source_name)[i+1] =='r' || (source_name)[i+1] =='R' ) && ( (source_name)[i+2] =='g' || (source_name)[i+2] =='G' ) ) driver_id = DRIVER_NRG; else if (i > 0 && ( (source_name)[i] =='c' || (source_name)[i] =='C') && ( (source_name)[i+1] =='u' || (source_name)[i+1] =='U') && ( (source_name)[i+2] =='e' || (source_name)[i+2] =='E') ) driver_id = DRIVER_BINCUE; else driver_id = DRIVER_BINCUE; } else { cdio_destroy(cdio); return NULL; } } cdio_destroy(cdio); goto retry; } case DRIVER_DEVICE: { /* Scan for a driver. */ CdIo *ret = cdio_open_cd(source_name); free(source_name); return ret; } break; case DRIVER_BSDI: case DRIVER_FREEBSD: case DRIVER_LINUX: case DRIVER_SOLARIS: case DRIVER_WIN32: case DRIVER_OSX: case DRIVER_NRG: case DRIVER_BINCUE: if ((*CdIo_all_drivers[driver_id].have_driver)()) { CdIo *ret = (*CdIo_all_drivers[driver_id].driver_open)(source_name); if (ret) ret->driver_id = driver_id; return ret; } } free(source_name); return NULL; }