📄 cdio.c
字号:
}/*! Return the number of the first track. CDIO_INVALID_TRACK is returned on error.*/track_tcdio_get_first_track_num (const CdIo *p_cdio){ if (NULL == p_cdio) return CDIO_INVALID_TRACK; if (p_cdio->op.get_first_track_num) { return p_cdio->op.get_first_track_num (p_cdio->env); } else { return CDIO_INVALID_TRACK; }}/*! Return a string containing the name of the driver in use. if CdIo is NULL (we haven't initialized a specific device driver), then return NULL.*/boolcdio_get_hwinfo (const CdIo *p_cdio, cdio_hwinfo_t *hw_info) { if (!p_cdio) return false; if (p_cdio->op.get_hwinfo) { return p_cdio->op.get_hwinfo (p_cdio, hw_info); } else { /* Perhaps driver forgot to initialize. We are no worse off Using scsi_mmc than returning false here. */ return scsi_mmc_get_hwinfo(p_cdio, hw_info); }}/*! Return a string containing the name of the driver in use. if CdIo is NULL (we haven't initialized a specific device driver), then return NULL.*/char *cdio_get_mcn (const CdIo *p_cdio) { if (p_cdio->op.get_mcn) { return p_cdio->op.get_mcn (p_cdio->env); } else { return NULL; }}/*! Return the number of tracks in the current medium. CDIO_INVALID_TRACK is returned on error.*/track_tcdio_get_num_tracks (const CdIo *p_cdio){ if (p_cdio == NULL) return CDIO_INVALID_TRACK; if (p_cdio->op.get_num_tracks) { return p_cdio->op.get_num_tracks (p_cdio->env); } else { return CDIO_INVALID_TRACK; }}/*! Get format of track. */track_format_tcdio_get_track_format(const CdIo *p_cdio, track_t i_track){ cdio_assert (p_cdio != NULL); if (p_cdio->op.get_track_format) { return p_cdio->op.get_track_format (p_cdio->env, i_track); } else { return TRACK_FORMAT_ERROR; }}/*! 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?*/boolcdio_get_track_green(const CdIo *cdio, track_t track_num){ cdio_assert (cdio != NULL); if (cdio->op.get_track_green) { return cdio->op.get_track_green (cdio->env, track_num); } else { return false; }}/*! Return the starting LBA for track number track_num in cdio. Tracks numbers start at 1. The "leadout" track is specified either by using track_num LEADOUT_TRACK or the total tracks+1. CDIO_INVALID_LBA is returned on error.*/lba_tcdio_get_track_lba(const CdIo *cdio, track_t track_num){ if (cdio == NULL) return CDIO_INVALID_LBA; if (cdio->op.get_track_lba) { return cdio->op.get_track_lba (cdio->env, track_num); } else { msf_t msf; if (cdio->op.get_track_msf) if (cdio_get_track_msf(cdio, track_num, &msf)) return cdio_msf_to_lba(&msf); return CDIO_INVALID_LBA; }}/*! Return the starting LSN for track number track_num in cdio. Tracks numbers start at 1. The "leadout" track is specified either by using track_num LEADOUT_TRACK or the total tracks+1. CDIO_INVALID_LBA is returned on error.*/lsn_tcdio_get_track_lsn(const CdIo *cdio, track_t track_num){ if (cdio == NULL) return CDIO_INVALID_LBA; if (cdio->op.get_track_lba) { return cdio_lba_to_lsn(cdio->op.get_track_lba (cdio->env, track_num)); } else { msf_t msf; if (cdio_get_track_msf(cdio, track_num, &msf)) return cdio_msf_to_lsn(&msf); return CDIO_INVALID_LSN; }}/*! Return the starting MSF (minutes/secs/frames) for track number track_num in cdio. Track numbers start at 1. The "leadout" track is specified either by using track_num LEADOUT_TRACK or the total tracks+1. False is returned if there is no track entry.*/boolcdio_get_track_msf(const CdIo *cdio, track_t track_num, /*out*/ msf_t *msf){ cdio_assert (cdio != NULL); if (cdio->op.get_track_msf) { return cdio->op.get_track_msf (cdio->env, track_num, msf); } else if (cdio->op.get_track_lba) { lba_t lba = cdio->op.get_track_lba (cdio->env, track_num); if (lba == CDIO_INVALID_LBA) return false; cdio_lba_to_msf(lba, msf); return true; } else { return false; }}/*! Return the number of sectors between this track an the next. This includes any pregap sectors before the start of the next track. Tracks start at 1. 0 is returned if there is an error.*/unsigned intcdio_get_track_sec_count(const CdIo *cdio, track_t track_num){ track_t num_tracks = cdio_get_num_tracks(cdio); if (track_num >=1 && track_num <= num_tracks) return ( cdio_get_track_lba(cdio, track_num+1) - cdio_get_track_lba(cdio, track_num) ); return 0;}boolcdio_have_driver(driver_id_t driver_id){ return (*CdIo_all_drivers[driver_id].have_driver)();}/*! Return the Joliet level recognized for p_cdio.*/uint8_t cdio_get_joliet_level(const CdIo *p_cdio){ if (!p_cdio) return 0; { const generic_img_private_t *p_env = (generic_img_private_t *) (p_cdio->env); return p_env->i_joliet_level; }}boolcdio_is_device(const char *psz_source, driver_id_t driver_id){ if (CdIo_all_drivers[driver_id].is_device == NULL) return false; return (*CdIo_all_drivers[driver_id].is_device)(psz_source);}/*! Initialize CD Reading and control routines. Should be called first. May be implicitly called by other routines if not called first.*/boolcdio_init(void){ CdIo_driver_t *all_dp; CdIo_driver_t *dp = CdIo_driver; driver_id_t driver_id; if (CdIo_last_driver != CDIO_DRIVER_UNINIT) { cdio_warn ("Init routine called more than once."); return false; } for (driver_id=DRIVER_UNKNOWN; driver_id<=CDIO_MAX_DRIVER; driver_id++) { all_dp = &CdIo_all_drivers[driver_id]; if ((*CdIo_all_drivers[driver_id].have_driver)()) { *dp++ = *all_dp; CdIo_last_driver++; } } return true;}CdIo *cdio_new (generic_img_private_t *p_env, cdio_funcs *p_funcs){ CdIo *p_new_cdio = _cdio_malloc (sizeof (CdIo)); if (NULL == p_new_cdio) return NULL; p_new_cdio->env = p_env; /* This is the private "environment" that driver-dependent routines use. */ p_new_cdio->op = *p_funcs; p_env->cdio = p_new_cdio; /* A way for the driver-dependent routines to access the higher-level general cdio object. */ return p_new_cdio;}/*! Free any resources associated with cdio.*/voidcdio_destroy (CdIo *cdio){ CdIo_last_driver = CDIO_DRIVER_UNINIT; if (cdio == NULL) return; if (cdio->op.free != NULL) cdio->op.free (cdio->env); free (cdio);}/*! lseek - reposition read/write file offset Returns (off_t) -1 on error. Similar to (if not the same as) libc's lseek()*/off_tcdio_lseek (const CdIo *cdio, off_t offset, int whence){ if (cdio == NULL) return -1; if (cdio->op.lseek) return cdio->op.lseek (cdio->env, offset, whence); return -1;}/*! Reads into buf the next size bytes. Returns -1 on error. Similar to (if not the same as) libc's read()*/ssize_tcdio_read (const CdIo *p_cdio, void *buf, size_t size){ if (p_cdio == NULL) return -1; if (p_cdio->op.read) return p_cdio->op.read (p_cdio->env, buf, size); return -1;}/*! Reads an audio sector from cd device into data starting from lsn. Returns 0 if no error. */intcdio_read_audio_sector (const CdIo *p_cdio, void *buf, lsn_t lsn) { if (NULL == p_cdio || NULL == buf || CDIO_INVALID_LSN == lsn ) return 0; if (p_cdio->op.read_audio_sectors != NULL) return p_cdio->op.read_audio_sectors (p_cdio->env, buf, lsn, 1); return -1;}/*! Reads audio sectors from cd device into data starting from lsn. Returns 0 if no error. */intcdio_read_audio_sectors (const CdIo *p_cdio, void *buf, lsn_t lsn, unsigned int nblocks) { if ( NULL == p_cdio || NULL == buf || CDIO_INVALID_LSN == lsn ) return 0; if (p_cdio->op.read_audio_sectors != NULL) return p_cdio->op.read_audio_sectors (p_cdio->env, buf, lsn, nblocks); return -1;}#ifndef SEEK_SET#define SEEK_SET 0#endif /*! Reads a single mode1 form1 or form2 sector from cd device into data starting from lsn. Returns 0 if no error. */intcdio_read_mode1_sector (const CdIo *p_cdio, void *data, lsn_t lsn, bool b_form2){ uint32_t size = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE ; if (NULL == p_cdio || NULL == data || CDIO_INVALID_LSN == lsn ) return 0; if (p_cdio->op.read_mode1_sector) { return p_cdio->op.read_mode1_sector(p_cdio->env, data, lsn, b_form2); } else if (p_cdio->op.lseek && p_cdio->op.read) { char buf[CDIO_CD_FRAMESIZE] = { 0, }; if (0 > cdio_lseek(p_cdio, CDIO_CD_FRAMESIZE*lsn, SEEK_SET)) return -1; if (0 > cdio_read(p_cdio, buf, CDIO_CD_FRAMESIZE)) return -1; memcpy (data, buf, size); return 0; } return 1;}intcdio_read_mode1_sectors (const CdIo *cdio, void *buf, lsn_t lsn, bool b_form2, unsigned int num_sectors){ if (NULL == cdio || NULL == buf || CDIO_INVALID_LSN == lsn ) return 0; cdio_assert (cdio->op.read_mode1_sectors != NULL); return cdio->op.read_mode1_sectors (cdio->env, buf, lsn, b_form2, num_sectors);}/*! Reads a single mode2 sector from cd device into data starting from lsn. Returns 0 if no error. */intcdio_read_mode2_sector (const CdIo *cdio, void *buf, lsn_t lsn, bool b_form2){ if (NULL == cdio || NULL == buf || CDIO_INVALID_LSN == lsn ) return 0; cdio_assert (cdio->op.read_mode2_sector != NULL || cdio->op.read_mode2_sectors != NULL); if (cdio->op.read_mode2_sector) return cdio->op.read_mode2_sector (cdio->env, buf, lsn, b_form2); /* fallback */ if (cdio->op.read_mode2_sectors != NULL) return cdio_read_mode2_sectors (cdio, buf, lsn, b_form2, 1); return 1;}intcdio_read_mode2_sectors (const CdIo *cdio, void *buf, lsn_t lsn, bool b_form2, unsigned int num_sectors){ if (NULL == cdio || NULL == buf || CDIO_INVALID_LSN == lsn ) return 0; cdio_assert (cdio->op.read_mode2_sectors != NULL); return cdio->op.read_mode2_sectors (cdio->env, buf, lsn, b_form2, num_sectors);}uint32_tcdio_stat_size (const CdIo *cdio){ cdio_assert (cdio != NULL); return cdio->op.stat_size (cdio->env);}/*! Set the arg "key" with "value" in the source device.*/intcdio_set_arg (CdIo *cdio, const char key[], const char value[]){ cdio_assert (cdio != NULL); cdio_assert (cdio->op.set_arg != NULL); cdio_assert (key != NULL); return cdio->op.set_arg (cdio->env, key, value);}/*! 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){ return cdio_open_am(orig_source_name, driver_id, NULL);}/*! 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_am (const char *psz_orig_source, driver_id_t driver_id, const char *psz_access_mode){ char *psz_source; if (CdIo_last_driver == -1) cdio_init(); if (NULL == psz_orig_source || strlen(psz_orig_source)==0) psz_source = cdio_get_default_device(NULL); else psz_source = strdup(psz_orig_source); switch (driver_id) { case DRIVER_UNKNOWN: { CdIo *cdio=scan_for_driver(CDIO_MIN_DRIVER, CDIO_MAX_DRIVER, psz_source, psz_access_mode); free(psz_source); return cdio; } case DRIVER_DEVICE: { /* Scan for a driver. */ CdIo *ret = cdio_open_am_cd(psz_source, psz_access_mode); free(psz_source); 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: case DRIVER_CDRDAO: if ((*CdIo_all_drivers[driver_id].have_driver)()) { CdIo *ret = (*CdIo_all_drivers[driver_id].driver_open_am)(psz_source, psz_access_mode); if (ret) ret->driver_id = driver_id; free(psz_source); return ret; } } free(psz_source); return NULL;}/*! Set up CD-ROM for reading. The device_name is the some sort of device name. @return the cdio object for subsequent operations. NULL on error or there is no driver for a some sort of hardware CD-ROM.*/CdIo *cdio_open_cd (const char *psz_source){ return cdio_open_am_cd(psz_source, NULL);}/*! Set up CD-ROM for reading. The device_name is the some sort of device name. @return the cdio object for subsequent operations. NULL on error or there is no driver for a some sort of hardware CD-ROM.*//* In the future we'll have more complicated code to allow selection of an I/O routine as well as code to find an appropriate default routine among the "registered" routines. Possibly classes too disk-based, SCSI-based, native-based, vendor (e.g. Sony, or Plextor) based For now though, we'll start more simply...*/CdIo *cdio_open_am_cd (const char *psz_source, const char *psz_access_mode){ if (CdIo_last_driver == -1) cdio_init(); /* Scan for a driver. */ return scan_for_driver(CDIO_MIN_DEVICE_DRIVER, CDIO_MAX_DEVICE_DRIVER, psz_source, psz_access_mode);}/* * Local variables: * c-file-style: "gnu" * tab-width: 8 * indent-tabs-mode: nil * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -