From c4e423537d77267ff859f20cefcbeaa4817d7199 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sun, 11 Apr 2004 12:20:31 +0000 Subject: libvcd 0.7.20, libcdio 0.68 CVS patchset: 6388 CVS date: 2004/04/11 12:20:31 --- src/input/vcd/libcdio/MSWindows/aspi32.c | 588 ++++++++++++++ src/input/vcd/libcdio/MSWindows/aspi32.h | 146 ++++ src/input/vcd/libcdio/MSWindows/ioctl.c | 472 ++++++++++++ src/input/vcd/libcdio/MSWindows/win32.c | 672 ++++++++++++++++ src/input/vcd/libcdio/MSWindows/win32.h | 90 +++ src/input/vcd/libcdio/Makefile.am | 12 +- src/input/vcd/libcdio/_cdio_bincue.c | 962 ----------------------- src/input/vcd/libcdio/_cdio_bsdi.c | 63 +- src/input/vcd/libcdio/_cdio_freebsd.c | 4 +- src/input/vcd/libcdio/_cdio_generic.c | 30 +- src/input/vcd/libcdio/_cdio_linux.c | 142 +++- src/input/vcd/libcdio/_cdio_nrg.c | 938 ----------------------- src/input/vcd/libcdio/_cdio_osx.c | 193 +++-- src/input/vcd/libcdio/_cdio_stdio.c | 20 +- src/input/vcd/libcdio/_cdio_stdio.h | 18 +- src/input/vcd/libcdio/_cdio_stream.c | 8 +- src/input/vcd/libcdio/_cdio_stream.h | 11 +- src/input/vcd/libcdio/_cdio_sunos.c | 258 +++---- src/input/vcd/libcdio/_cdio_win32.c | 1168 ---------------------------- src/input/vcd/libcdio/bytesex.h | 2 +- src/input/vcd/libcdio/bytesex_asm.h | 2 +- src/input/vcd/libcdio/cd_types.c | 10 +- src/input/vcd/libcdio/cdio.c | 85 ++- src/input/vcd/libcdio/cdio/cd_types.h | 127 +-- src/input/vcd/libcdio/cdio/cdio.h | 299 +++++--- src/input/vcd/libcdio/cdio/iso9660.h | 395 ++++++---- src/input/vcd/libcdio/cdio/logging.h | 105 ++- src/input/vcd/libcdio/cdio/sector.h | 152 ++-- src/input/vcd/libcdio/cdio/types.h | 39 +- src/input/vcd/libcdio/cdio/util.h | 8 +- src/input/vcd/libcdio/cdio/version.h | 11 +- src/input/vcd/libcdio/cdio/xa.h | 72 +- src/input/vcd/libcdio/cdio_assert.h | 2 +- src/input/vcd/libcdio/cdio_private.h | 25 +- src/input/vcd/libcdio/ds.c | 4 +- src/input/vcd/libcdio/ds.h | 2 +- src/input/vcd/libcdio/image/bincue.c | 1019 +++++++++++++++++++++++++ src/input/vcd/libcdio/image/nrg.c | 1229 ++++++++++++++++++++++++++++++ src/input/vcd/libcdio/iso9660.c | 4 +- src/input/vcd/libcdio/iso9660_fs.c | 567 +++++++++++--- src/input/vcd/libcdio/iso9660_private.h | 2 +- src/input/vcd/libcdio/logging.c | 8 +- src/input/vcd/libcdio/scsi_mmc.h | 2 +- src/input/vcd/libcdio/sector.c | 4 +- src/input/vcd/libcdio/util.c | 4 +- src/input/vcd/libcdio/xa.c | 2 +- 46 files changed, 6059 insertions(+), 3917 deletions(-) create mode 100644 src/input/vcd/libcdio/MSWindows/aspi32.c create mode 100644 src/input/vcd/libcdio/MSWindows/aspi32.h create mode 100644 src/input/vcd/libcdio/MSWindows/ioctl.c create mode 100644 src/input/vcd/libcdio/MSWindows/win32.c create mode 100644 src/input/vcd/libcdio/MSWindows/win32.h delete mode 100644 src/input/vcd/libcdio/_cdio_bincue.c delete mode 100644 src/input/vcd/libcdio/_cdio_nrg.c delete mode 100644 src/input/vcd/libcdio/_cdio_win32.c create mode 100644 src/input/vcd/libcdio/image/bincue.c create mode 100644 src/input/vcd/libcdio/image/nrg.c (limited to 'src/input/vcd/libcdio') diff --git a/src/input/vcd/libcdio/MSWindows/aspi32.c b/src/input/vcd/libcdio/MSWindows/aspi32.c new file mode 100644 index 000000000..bb568fcc7 --- /dev/null +++ b/src/input/vcd/libcdio/MSWindows/aspi32.c @@ -0,0 +1,588 @@ +/* + $Id: aspi32.c,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $ + + Copyright (C) 2004 Rocky Bernstein + + 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 file contains Win32-specific code and implements low-level + control of the CD drive via the ASPI API. + Inspired by vlc's cdrom.h code +*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +static const char _rcsid[] = "$Id: aspi32.c,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $"; + +#include +#include +#include +#include "cdio_assert.h" +#include "scsi_mmc.h" + +#include + +#ifdef HAVE_WIN32_CDROM + +#include +#include +#include +#include +#include +#include + +#include +#include "win32.h" + +#include +#include +#include "aspi32.h" + +/* General ioctl() CD-ROM command function */ +static bool +wnaspi32_mciSendCommand(int id, UINT msg, DWORD flags, void *arg) +{ + MCIERROR mci_error; + + mci_error = mciSendCommand(id, msg, flags, (DWORD)arg); + if ( mci_error ) { + char error[256]; + + mciGetErrorString(mci_error, error, 256); + cdio_warn("mciSendCommand() error: %s", error); + } + return(mci_error == 0); +} + +const char * +wnaspi32_is_cdrom(const char drive_letter) +{ + static char psz_win32_drive[7]; + HMODULE hASPI = NULL; + long (*lpGetSupport)( void ) = NULL; + long (*lpSendCommand)( void* ) = NULL; + DWORD dwSupportInfo; + int i_adapter, i_num_adapters; + char c_drive; + + hASPI = LoadLibrary( "wnaspi32.dll" ); + if( hASPI != NULL ) { + (FARPROC) lpGetSupport = GetProcAddress( hASPI, + "GetASPI32SupportInfo" ); + (FARPROC) lpSendCommand = GetProcAddress( hASPI, + "SendASPI32Command" ); + } + + if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL ) { + cdio_debug("Unable to load ASPI or get ASPI function pointers"); + if( hASPI ) FreeLibrary( hASPI ); + return NULL; + } + + /* ASPI support seems to be there */ + + dwSupportInfo = lpGetSupport(); + + if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) { + cdio_debug("no host adapters found (ASPI)"); + FreeLibrary( hASPI ); + return NULL; + } + + if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP ) { + cdio_debug("Unable to initalize ASPI layer"); + FreeLibrary( hASPI ); + return NULL; + } + + i_num_adapters = LOBYTE( LOWORD( dwSupportInfo ) ); + if( i_num_adapters == 0 ) { + FreeLibrary( hASPI ); + return NULL; + } + + c_drive = toupper(drive_letter) - 'A'; + + for( i_adapter = 0; i_adapter < i_num_adapters; i_adapter++ ) { + struct SRB_GetDiskInfo srbDiskInfo; + int i_target; + SRB_HAInquiry srbInquiry; + + srbInquiry.SRB_Cmd = SC_HA_INQUIRY; + srbInquiry.SRB_HaId = i_adapter; + + lpSendCommand( (void*) &srbInquiry ); + + if( srbInquiry.SRB_Status != SS_COMP ) continue; + if( !srbInquiry.HA_Unique[3]) srbInquiry.HA_Unique[3]=8; + + for(i_target=0; i_target < srbInquiry.HA_Unique[3]; i_target++) + { + int i_lun; + for( i_lun=0; i_lun<8; i_lun++) + { + srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO; + srbDiskInfo.SRB_Flags = 0; + srbDiskInfo.SRB_Hdr_Rsvd = 0; + srbDiskInfo.SRB_HaId = i_adapter; + srbDiskInfo.SRB_Target = i_target; + srbDiskInfo.SRB_Lun = i_lun; + + lpSendCommand( (void*) &srbDiskInfo ); + + if( (srbDiskInfo.SRB_Status == SS_COMP) && + (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) ) { + /* Make sure this is a cdrom device */ + struct SRB_GDEVBlock srbGDEVBlock; + + memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) ); + srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE; + srbDiskInfo.SRB_HaId = i_adapter; + srbGDEVBlock.SRB_Target = i_target; + + lpSendCommand( (void*) &srbGDEVBlock ); + + if( ( srbGDEVBlock.SRB_Status == SS_COMP ) && + ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) { + sprintf( psz_win32_drive, "%c:", drive_letter ); + FreeLibrary( hASPI ); + return(psz_win32_drive); + } + } + } + } + } + FreeLibrary( hASPI ); + return NULL; +} + +/*! + Initialize CD device. + */ +bool +wnaspi32_init_win32 (_img_private_t *env) +{ + HMODULE hASPI = NULL; + long (*lpGetSupport)( void ) = NULL; + long (*lpSendCommand)( void* ) = NULL; + DWORD dwSupportInfo; + int i, j, i_num_adapters; + char c_drive = env->gen.source_name[0]; + + hASPI = LoadLibrary( "wnaspi32.dll" ); + if( hASPI != NULL ) { + (FARPROC) lpGetSupport = GetProcAddress( hASPI, + "GetASPI32SupportInfo" ); + (FARPROC) lpSendCommand = GetProcAddress( hASPI, + "SendASPI32Command" ); + } + + if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL ) { + cdio_debug("Unable to load ASPI or get ASPI function pointers"); + if( hASPI ) FreeLibrary( hASPI ); + return false; + } + + /* ASPI support seems to be there */ + + dwSupportInfo = lpGetSupport(); + + if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) { + cdio_debug("no host adapters found (ASPI)"); + FreeLibrary( hASPI ); + return -1; + } + + if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP ) { + cdio_debug("unable to initalize ASPI layer"); + FreeLibrary( hASPI ); + return -1; + } + + i_num_adapters = LOBYTE( LOWORD( dwSupportInfo ) ); + if( i_num_adapters == 0 ) { + FreeLibrary( hASPI ); + return -1; + } + + c_drive = toupper(c_drive) - 'A'; + + for( i = 0; i < i_num_adapters; i++ ) { + for( j = 0; j < 15; j++ ) { + struct SRB_GetDiskInfo srbDiskInfo; + int lun; + + for (lun = 0; lun < 8; lun++ ) { + srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO; + srbDiskInfo.SRB_HaId = i; + srbDiskInfo.SRB_Flags = 0; + srbDiskInfo.SRB_Hdr_Rsvd = 0; + srbDiskInfo.SRB_Target = j; + srbDiskInfo.SRB_Lun = lun; + + lpSendCommand( (void*) &srbDiskInfo ); + + if( (srbDiskInfo.SRB_Status == SS_COMP) ) { + + if (srbDiskInfo.SRB_Int13HDriveInfo != c_drive) + { + continue; + } else { + /* Make sure this is a cdrom device */ + struct SRB_GDEVBlock srbGDEVBlock; + + memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) ); + srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE; + srbGDEVBlock.SRB_HaId = i; + srbGDEVBlock.SRB_Target = j; + + lpSendCommand( (void*) &srbGDEVBlock ); + + if( ( srbGDEVBlock.SRB_Status == SS_COMP ) && + ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) { + env->i_sid = MAKEWORD( i, j ); + env->hASPI = (long)hASPI; + env->lpSendCommand = lpSendCommand; + cdio_debug("Using ASPI layer"); + + return true; + } else { + FreeLibrary( hASPI ); + cdio_debug( "%c: is not a CD-ROM drive", + env->gen.source_name[0] ); + return false; + } + } + } + } + } + } + + FreeLibrary( hASPI ); + cdio_debug( "Unable to get HaId and target (ASPI)" ); + return false; +} + +/*! + Reads a single mode2 sector from cd device into data starting from lsn. + Returns 0 if no error. + */ +static int +wnaspi32_mmc_read_sectors (_img_private_t *env, void *data, lsn_t lsn, + int sector_type, unsigned int nblocks) +{ + unsigned char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + HANDLE hEvent; + struct SRB_ExecSCSICmd ssc; + +#if 1 + sector_type = 0; /*all types */ + int sync = 0; + int header_code = 2; + int i_user_data = 1; + int edc_ecc = 0; + int error_field = 0; +#endif + + + /* Create the transfer completion event */ + hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); + if( hEvent == NULL ) { + return 1; + } + + /* Data selection */ + + memset( &ssc, 0, sizeof( ssc ) ); + + ssc.SRB_Cmd = SC_EXEC_SCSI_CMD; + ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY; + ssc.SRB_HaId = LOBYTE( env->i_sid ); + ssc.SRB_Target = HIBYTE( env->i_sid ); + ssc.SRB_SenseLen = SENSE_LEN; + + ssc.SRB_PostProc = (LPVOID) hEvent; + ssc.SRB_CDBLen = 12; + + /* Operation code */ + ssc.CDBByte[ 0 ] = CDIO_MMC_GPCMD_READ_CD; + + CDIO_MMC_SET_READ_TYPE(ssc.CDBByte, sector_type); + CDIO_MMC_SET_READ_LBA(ssc.CDBByte, lsn); + CDIO_MMC_SET_READ_LENGTH(ssc.CDBByte, nblocks); + +#if 1 + ssc.CDBByte[ 9 ] = (sync << 7) | + (header_code << 5) | + (i_user_data << 4) | + (edc_ecc << 3) | + (error_field << 1); + /* ssc.CDBByte[ 9 ] = READ_CD_USERDATA_MODE2; */ +#else + CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(ssc.CDBByte, + CDIO_MMC_MCSB_ALL_HEADERS); +#endif + + /* Result buffer */ + ssc.SRB_BufPointer = buf; + ssc.SRB_BufLen = CDIO_CD_FRAMESIZE_RAW; + + /* Initiate transfer */ + ResetEvent( hEvent ); + env->lpSendCommand( (void*) &ssc ); + + /* If the command has still not been processed, wait until it's + * finished */ + if( ssc.SRB_Status == SS_PENDING ) { + WaitForSingleObject( hEvent, INFINITE ); + } + CloseHandle( hEvent ); + + /* check that the transfer went as planned */ + if( ssc.SRB_Status != SS_COMP ) { + return 1; + } + + /* FIXME! remove the 8 (SUBHEADER size) below... */ + memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW); + + return 0; +} + +/*! + Reads an audio device into data starting from lsn. + Returns 0 if no error. + */ +int +wnaspi32_read_audio_sectors (_img_private_t *env, void *data, lsn_t lsn, + unsigned int nblocks) +{ + return wnaspi32_mmc_read_sectors( env, data, lsn, CDIO_MMC_READ_TYPE_CDDA, + nblocks ); +} + +/*! + Reads a single mode2 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +int +wnaspi32_read_mode2_sector (_img_private_t *env, void *data, lsn_t lsn) +{ + return wnaspi32_mmc_read_sectors(env, data, lsn, CDIO_MMC_READ_TYPE_ANY, + 1); +} + +/*! + Read and cache the CD's Track Table of Contents and track info. + Return true if successful or false if an error. +*/ +bool +wnaspi32_read_toc (_img_private_t *env) +{ + HANDLE hEvent; + struct SRB_ExecSCSICmd ssc; + unsigned char p_tocheader[ 4 ]; + + /* Create the transfer completion event */ + hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); + if( hEvent == NULL ) { + return false; + } + + memset( &ssc, 0, sizeof( ssc ) ); + + ssc.SRB_Cmd = SC_EXEC_SCSI_CMD; + ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY; + ssc.SRB_HaId = LOBYTE( env->i_sid ); + ssc.SRB_Target = HIBYTE( env->i_sid ); + ssc.SRB_SenseLen = SENSE_LEN; + + ssc.SRB_PostProc = (LPVOID) hEvent; + ssc.SRB_CDBLen = 10; + + /* Operation code */ + ssc.CDBByte[ 0 ] = READ_TOC; + + /* Format */ + ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC; + + /* Starting track */ + ssc.CDBByte[ 6 ] = 0; + + /* Allocation length and buffer */ + ssc.SRB_BufLen = sizeof( p_tocheader ); + ssc.SRB_BufPointer = p_tocheader; + ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff; + ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff; + + /* Initiate transfer */ + ResetEvent( hEvent ); + env->lpSendCommand( (void*) &ssc ); + + /* If the command has still not been processed, wait until it's + * finished */ + if( ssc.SRB_Status == SS_PENDING ) + WaitForSingleObject( hEvent, INFINITE ); + + /* check that the transfer went as planned */ + if( ssc.SRB_Status != SS_COMP ) { + CloseHandle( hEvent ); + return false; + } + + env->first_track_num = p_tocheader[2]; + env->total_tracks = p_tocheader[3] - p_tocheader[2] + 1; + + { + int i, i_toclength; + unsigned char *p_fulltoc; + + i_toclength = 4 /* header */ + p_tocheader[0] + + ((unsigned int)p_tocheader[1] << 8); + + p_fulltoc = malloc( i_toclength ); + + if( p_fulltoc == NULL ) { + cdio_error( "out of memory" ); + CloseHandle( hEvent ); + return false; + } + + /* Allocation length and buffer */ + ssc.SRB_BufLen = i_toclength; + ssc.SRB_BufPointer = p_fulltoc; + ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff; + ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff; + + /* Initiate transfer */ + ResetEvent( hEvent ); + env->lpSendCommand( (void*) &ssc ); + + /* If the command has still not been processed, wait until it's + * finished */ + if( ssc.SRB_Status == SS_PENDING ) + WaitForSingleObject( hEvent, INFINITE ); + + /* check that the transfer went as planned */ + if( ssc.SRB_Status != SS_COMP ) + env->total_tracks = 0; + + for( i = 0 ; i <= env->total_tracks ; i++ ) { + int i_index = 8 + 8 * i; + env->tocent[ i ].start_lsn = ((int)p_fulltoc[ i_index ] << 24) + + ((int)p_fulltoc[ i_index+1 ] << 16) + + ((int)p_fulltoc[ i_index+2 ] << 8) + + (int)p_fulltoc[ i_index+3 ]; + env->tocent[ i ].Control = (UCHAR)p_fulltoc[ 1 + 8 * i ]; + + cdio_debug( "p_sectors: %i %lu", + i, (unsigned long int) env->tocent[i].start_lsn ); + } + + free( p_fulltoc ); + } + + CloseHandle( hEvent ); + env->gen.toc_init = true; + return true; +} + +/* Eject media will eventually get removed from _cdio_win32.c */ +#if 0 +/*! + Eject media. Return 1 if successful, 0 otherwise. + */ +int +wnaspi32_eject_media (void *user_data) { + + _img_private_t *env = user_data; + + + MCI_OPEN_PARMS op; + MCI_STATUS_PARMS st; + DWORD i_flags; + char psz_drive[4]; + int ret; + + memset( &op, 0, sizeof(MCI_OPEN_PARMS) ); + op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO; + strcpy( psz_drive, "X:" ); + psz_drive[0] = env->gen.source_name[0]; + op.lpstrElementName = psz_drive; + + /* Set the flags for the device type */ + i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | + MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE; + + if( wnaspi32_mciSendCommand( 0, MCI_OPEN, i_flags, &op ) ) { + st.dwItem = MCI_STATUS_READY; + /* Eject disc */ + ret = wnaspi32_mciSendCommand( op.wDeviceID, MCI_SET, + MCI_SET_DOOR_OPEN, 0 ) != 0; + /* Release access to the device */ + wnaspi32_mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 ); + } else + ret = 0; + + return ret; +} +#endif + +/*! + Get format of track. +*/ +track_format_t +wnaspi32_get_track_format(_img_private_t *env, track_t track_num) +{ + MCI_OPEN_PARMS op; + MCI_STATUS_PARMS st; + DWORD i_flags; + int ret; + + memset( &op, 0, sizeof(MCI_OPEN_PARMS) ); + op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO; + op.lpstrElementName = env->gen.source_name; + + /* Set the flags for the device type */ + i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | + MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE; + + if( wnaspi32_mciSendCommand( 0, MCI_OPEN, i_flags, &op ) ) { + st.dwItem = MCI_CDA_STATUS_TYPE_TRACK; + st.dwTrack = track_num; + i_flags = MCI_TRACK | MCI_STATUS_ITEM ; + ret = wnaspi32_mciSendCommand( op.wDeviceID, MCI_STATUS, i_flags, &st ); + + /* Release access to the device */ + wnaspi32_mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 ); + + switch(st.dwReturn) { + case MCI_CDA_TRACK_AUDIO: + return TRACK_FORMAT_AUDIO; + case MCI_CDA_TRACK_OTHER: + return TRACK_FORMAT_DATA; + default: + return TRACK_FORMAT_XA; + } + } + return TRACK_FORMAT_ERROR; +} + +#endif /* HAVE_WIN32_CDROM */ + + diff --git a/src/input/vcd/libcdio/MSWindows/aspi32.h b/src/input/vcd/libcdio/MSWindows/aspi32.h new file mode 100644 index 000000000..381d6c154 --- /dev/null +++ b/src/input/vcd/libcdio/MSWindows/aspi32.h @@ -0,0 +1,146 @@ +/* Win32 aspi specific */ +/* + $Id: aspi32.h,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $ + + Copyright (C) 2003, 2004 Rocky Bernstein + + 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 +*/ + +#define ASPI_HAID 0 +#define ASPI_TARGET 0 +#define DTYPE_CDROM 0x05 + +#define SENSE_LEN 0x0E +#define SC_HA_INQUIRY 0x00 +#define SC_GET_DEV_TYPE 0x01 +#define SC_EXEC_SCSI_CMD 0x02 +#define SC_GET_DISK_INFO 0x06 +#define SS_COMP 0x01 +#define SS_PENDING 0x00 +#define SS_NO_ADAPTERS 0xE8 +#define SRB_DIR_IN 0x08 +#define SRB_DIR_OUT 0x10 +#define SRB_EVENT_NOTIFY 0x40 + +#define SECTOR_TYPE_MODE2 0x14 +#define READ_CD_USERDATA_MODE2 0x10 + +#define READ_TOC 0x43 +#define READ_TOC_FORMAT_TOC 0x0 + +#pragma pack(1) + +struct SRB_GetDiskInfo +{ + unsigned char SRB_Cmd; + unsigned char SRB_Status; + unsigned char SRB_HaId; + unsigned char SRB_Flags; + unsigned long SRB_Hdr_Rsvd; + unsigned char SRB_Target; + unsigned char SRB_Lun; + unsigned char SRB_DriveFlags; + unsigned char SRB_Int13HDriveInfo; + unsigned char SRB_Heads; + unsigned char SRB_Sectors; + unsigned char SRB_Rsvd1[22]; +}; + +struct SRB_GDEVBlock +{ + unsigned char SRB_Cmd; + unsigned char SRB_Status; + unsigned char SRB_HaId; + unsigned char SRB_Flags; + unsigned long SRB_Hdr_Rsvd; + unsigned char SRB_Target; + unsigned char SRB_Lun; + unsigned char SRB_DeviceType; + unsigned char SRB_Rsvd1; +}; + +struct SRB_ExecSCSICmd +{ + unsigned char SRB_Cmd; + unsigned char SRB_Status; + unsigned char SRB_HaId; + unsigned char SRB_Flags; + unsigned long SRB_Hdr_Rsvd; + unsigned char SRB_Target; + unsigned char SRB_Lun; + unsigned short SRB_Rsvd1; + unsigned long SRB_BufLen; + unsigned char *SRB_BufPointer; + unsigned char SRB_SenseLen; + unsigned char SRB_CDBLen; + unsigned char SRB_HaStat; + unsigned char SRB_TargStat; + unsigned long *SRB_PostProc; + unsigned char SRB_Rsvd2[20]; + unsigned char CDBByte[16]; + unsigned char SenseArea[SENSE_LEN+2]; +}; + +/***************************************************************************** + %%% SRB - HOST ADAPTER INQUIRY - SC_HA_INQUIRY (0) %%% +*****************************************************************************/ + +typedef struct // Offset +{ // HX/DEC + BYTE SRB_Cmd; // 00/000 ASPI command code = SC_HA_INQUIRY + BYTE SRB_Status; // 01/001 ASPI command status byte + BYTE SRB_HaId; // 02/002 ASPI host adapter number + BYTE SRB_Flags; // 03/003 ASPI request flags + DWORD SRB_Hdr_Rsvd; // 04/004 Reserved, MUST = 0 + BYTE HA_Count; // 08/008 Number of host adapters present + BYTE HA_SCSI_ID; // 09/009 SCSI ID of host adapter + BYTE HA_ManagerId[16]; // 0A/010 String describing the manager + BYTE HA_Identifier[16]; // 1A/026 String describing the host adapter + BYTE HA_Unique[16]; // 2A/042 Host Adapter Unique parameters + WORD HA_Rsvd1; // 3A/058 Reserved, MUST = 0 +} +SRB_HAInquiry; + +const char * wnaspi32_is_cdrom(const char drive_letter); + +/*! + Initialize CD device. + */ +bool wnaspi32_init_win32 (_img_private_t *env); + +/*! + Reads an audio device into data starting from lsn. + Returns 0 if no error. + */ +int wnaspi32_read_audio_sectors (_img_private_t *env, void *data, lsn_t lsn, + unsigned int nblocks); +/*! + Reads a single mode2 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +int wnaspi32_read_mode2_sector (_img_private_t *env, void *data, lsn_t lsn); + +/*! + Read and cache the CD's Track Table of Contents and track info. + Return true if successful or false if an error. +*/ +bool wnaspi32_read_toc (_img_private_t *env); + +/*! + Get format of track. +*/ +track_format_t wnaspi32_get_track_format(_img_private_t *env, + track_t track_num); diff --git a/src/input/vcd/libcdio/MSWindows/ioctl.c b/src/input/vcd/libcdio/MSWindows/ioctl.c new file mode 100644 index 000000000..1d2f99420 --- /dev/null +++ b/src/input/vcd/libcdio/MSWindows/ioctl.c @@ -0,0 +1,472 @@ +/* + $Id: ioctl.c,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $ + + Copyright (C) 2004 Rocky Bernstein + + 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 file contains Win32-specific code using the DeviceIoControl + access method. +*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +static const char _rcsid[] = "$Id: ioctl.c,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $"; + +#include +#include +#include "cdio_assert.h" + +#ifdef HAVE_WIN32_CDROM + +#include +#include + +#include +#include +#include + +#include "scsi_mmc.h" + +/* Win32 DeviceIoControl specifics */ +/***** FIXME: #include ntddcdrm.h from Wine, but probably need to + modify it a little. +*/ + +#define SCSI_IOCTL_DATA_OUT 0 //Give data to SCSI device (e.g. for writing) +#define SCSI_IOCTL_DATA_IN 1 //Get data from SCSI device (e.g. for reading) +#define SCSI_IOCTL_DATA_UNSPECIFIED 2 //No data (e.g. for ejecting) + +#define IOCTL_SCSI_PASS_THROUGH 0x4D004 +typedef struct ScsiPassThrough { + unsigned short Length; + unsigned char ScsiStatus; + unsigned char PathId; + unsigned char TargetId; + unsigned char Lun; + unsigned char CdbLength; + unsigned char SenseInfoLength; + unsigned char DataIn; + unsigned int DataTransferLength; + unsigned int TimeOutValue; + unsigned int DataBufferOffset; + unsigned int SenseInfoOffset; + unsigned char Cdb[16]; +} SCSI_PASS_THROUGH; + +#define IOCTL_SCSI_PASS_THROUGH_DIRECT 0x4D014 +typedef struct _SCSI_PASS_THROUGH_DIRECT { + USHORT Length; + UCHAR ScsiStatus; + UCHAR PathId; + UCHAR TargetId; + UCHAR Lun; + UCHAR CdbLength; + UCHAR SenseInfoLength; + UCHAR DataIn; + ULONG DataTransferLength; + ULONG TimeOutValue; + PVOID DataBuffer; + ULONG SenseInfoOffset; + UCHAR Cdb[16]; +} SCSI_PASS_THROUGH_DIRECT, *PSCSI_PASS_THROUGH_DIRECT; + +#ifndef IOCTL_CDROM_BASE +# define IOCTL_CDROM_BASE FILE_DEVICE_CD_ROM +#endif +#ifndef IOCTL_CDROM_RAW_READ +#define IOCTL_CDROM_RAW_READ CTL_CODE(IOCTL_CDROM_BASE, 0x000F, \ + METHOD_OUT_DIRECT, FILE_READ_ACCESS) +#endif + +#ifndef IOCTL_CDROM_BASE +# define IOCTL_CDROM_BASE FILE_DEVICE_CD_ROM +#endif +#ifndef IOCTL_CDROM_READ_TOC +#define IOCTL_CDROM_READ_TOC \ + CTL_CODE(IOCTL_CDROM_BASE, 0x0000, METHOD_BUFFERED, FILE_READ_ACCESS) +#endif +#ifndef IOCTL_CDROM_RAW_READ +#define IOCTL_CDROM_RAW_READ \ + CTL_CODE(IOCTL_CDROM_BASE, 0x000F, METHOD_OUT_DIRECT, FILE_READ_ACCESS) +#endif + +#ifndef IOCTL_CDROM_READ_Q_CHANNEL +#define IOCTL_CDROM_READ_Q_CHANNEL \ + CTL_CODE(IOCTL_CDROM_BASE, 0x000B, METHOD_BUFFERED, FILE_READ_ACCESS) +#endif + +typedef struct _TRACK_DATA { + UCHAR Format; + UCHAR Control : 4; + UCHAR Adr : 4; + UCHAR TrackNumber; + UCHAR Reserved1; + UCHAR Address[4]; +} TRACK_DATA, *PTRACK_DATA; + +typedef struct _CDROM_TOC { + UCHAR Length[2]; + UCHAR FirstTrack; + UCHAR LastTrack; + TRACK_DATA TrackData[CDIO_CD_MAX_TRACKS+1]; +} CDROM_TOC, *PCDROM_TOC; + +#define IOCTL_CDROM_SUB_Q_CHANNEL 0x00 +#define IOCTL_CDROM_CURRENT_POSITION 0x01 +#define IOCTL_CDROM_MEDIA_CATALOG 0x02 +#define IOCTL_CDROM_TRACK_ISRC 0x03 + +typedef enum _TRACK_MODE_TYPE { + YellowMode2, + XAForm2, + CDDA +} TRACK_MODE_TYPE, *PTRACK_MODE_TYPE; + +typedef struct __RAW_READ_INFO { + LARGE_INTEGER DiskOffset; + ULONG SectorCount; + TRACK_MODE_TYPE TrackMode; +} RAW_READ_INFO, *PRAW_READ_INFO; + +typedef struct _CDROM_SUB_Q_DATA_FORMAT { + UCHAR Format; + UCHAR Track; +} CDROM_SUB_Q_DATA_FORMAT, *PCDROM_SUB_Q_DATA_FORMAT; + +typedef struct _SUB_Q_HEADER { + UCHAR Reserved; + UCHAR AudioStatus; + UCHAR DataLength[2]; +} SUB_Q_HEADER, *PSUB_Q_HEADER; + +typedef struct _SUB_Q_MEDIA_CATALOG_NUMBER { + SUB_Q_HEADER Header; + UCHAR FormatCode; + UCHAR Reserved[3]; + UCHAR Reserved1 : 7; + UCHAR Mcval :1; + UCHAR MediaCatalog[15]; +} SUB_Q_MEDIA_CATALOG_NUMBER, *PSUB_Q_MEDIA_CATALOG_NUMBER; + +#include "win32.h" + +/* + Returns a string that can be used in a CreateFile call if + c_drive letter is a character. If not NULL is returned. + */ + +const char * +win32ioctl_is_cdrom(const char c_drive_letter) +{ + + UINT uDriveType; + char sz_win32_drive[4]; + + sz_win32_drive[0]= c_drive_letter; + sz_win32_drive[1]=':'; + sz_win32_drive[2]='\\'; + sz_win32_drive[3]='\0'; + + uDriveType = GetDriveType(sz_win32_drive); + + switch(uDriveType) { + case DRIVE_CDROM: { + char sz_win32_drive_full[] = "\\\\.\\X:"; + sz_win32_drive_full[4] = c_drive_letter; + return strdup(sz_win32_drive_full); + } + default: + cdio_debug("Drive %c is not a CD-ROM", c_drive_letter); + return NULL; + } +} + +/*! + Reads an audio device using the DeviceIoControl method into data + starting from lsn. Returns 0 if no error. + */ +int +win32ioctl_read_audio_sectors (_img_private_t *env, void *data, lsn_t lsn, + unsigned int nblocks) +{ + DWORD dwBytesReturned; + RAW_READ_INFO cdrom_raw; + + /* Initialize CDROM_RAW_READ structure */ + cdrom_raw.DiskOffset.QuadPart = CDIO_CD_FRAMESIZE_RAW * lsn; + cdrom_raw.SectorCount = nblocks; + cdrom_raw.TrackMode = CDDA; + + if( DeviceIoControl( env->h_device_handle, + IOCTL_CDROM_RAW_READ, &cdrom_raw, + sizeof(RAW_READ_INFO), data, + CDIO_CD_FRAMESIZE_RAW * nblocks, + &dwBytesReturned, NULL ) == 0 ) { + cdio_info("Error reading audio-mode %lu (%ld)\n", + (long unsigned int) lsn, GetLastError()); + return 1; + } + return 0; +} + +/*! + Reads a single raw sector using the DeviceIoControl method into + data starting from lsn. Returns 0 if no error. + */ +static int +win32ioctl_read_raw_sector (_img_private_t *env, void *buf, lsn_t lsn) +{ + SCSI_PASS_THROUGH_DIRECT sptd; + BOOL success; + DWORD dwBytesReturned; + + sptd.Length=sizeof(sptd); + sptd.PathId=0; /* SCSI card ID will be filled in automatically */ + sptd.TargetId=0; /* SCSI target ID will also be filled in */ + sptd.Lun=0; /* SCSI lun ID will also be filled in */ + sptd.CdbLength=12; /* CDB size is 12 for ReadCD MMC1 command */ + sptd.SenseInfoLength=0; /* Don't return any sense data */ + sptd.DataIn=SCSI_IOCTL_DATA_IN; //There will be data from drive + sptd.DataTransferLength=CDIO_CD_FRAMESIZE_RAW; + sptd.TimeOutValue=60; /*SCSI timeout value (60 seconds - + maybe it should be longer) */ + sptd.DataBuffer= (PVOID) buf; + sptd.SenseInfoOffset=0; + + /* ReadCD CDB12 command. The values were taken from MMC1 draft paper. */ + sptd.Cdb[0]=CDIO_MMC_GPCMD_READ_CD; + sptd.Cdb[1]=0; + + CDIO_MMC_SET_READ_LBA(sptd.Cdb, lsn); + + CDIO_MMC_SET_READ_LENGTH(sptd.Cdb, 1); + + sptd.Cdb[9]=0xF8; /* Raw read, 2352 bytes per sector */ + sptd.Cdb[10]=0; + sptd.Cdb[11]=0; + sptd.Cdb[12]=0; + sptd.Cdb[13]=0; + sptd.Cdb[14]=0; + sptd.Cdb[15]=0; + + /* Send the command to drive */ + success=DeviceIoControl(env->h_device_handle, + IOCTL_SCSI_PASS_THROUGH_DIRECT, + (PVOID)&sptd, + (DWORD)sizeof(SCSI_PASS_THROUGH_DIRECT), + NULL, 0, + &dwBytesReturned, + NULL); + + if(! success) { + cdio_info("Error reading %lu (%ld)\n", (long unsigned) lsn, + GetLastError()); + return 1; + } + + return 0; +} + +/*! + Reads a single mode2 sector using the DeviceIoControl method into + data starting from lsn. Returns 0 if no error. + */ +int +win32ioctl_read_mode2_sector (_img_private_t *env, void *data, lsn_t lsn, + bool mode2_form2) +{ + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + int ret = win32ioctl_read_raw_sector (env, buf, lsn); + + if ( 0 != ret) return ret; + + memcpy (data, + buf + CDIO_CD_SYNC_SIZE + CDIO_CD_XA_HEADER, + mode2_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); + + return 0; + +} + +/*! + Reads a single mode2 sector using the DeviceIoControl method into + data starting from lsn. Returns 0 if no error. + */ +int +win32ioctl_read_mode1_sector (_img_private_t *env, void *data, lsn_t lsn, + bool mode2_form2) +{ + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + int ret = win32ioctl_read_raw_sector (env, buf, lsn); + + if ( 0 != ret) return ret; + + memcpy (data, + buf + CDIO_CD_SYNC_SIZE+CDIO_CD_HEADER_SIZE, + mode2_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); + + return 0; + +} + +/*! + Initialize internal structures for CD device. + */ +bool +win32ioctl_init_win32 (_img_private_t *env) +{ + char psz_win32_drive[7]; + unsigned int len=strlen(env->gen.source_name); + OSVERSIONINFO ov; + DWORD dw_access_flags; + + cdio_debug("using winNT/2K/XP ioctl layer"); + + memset(&ov,0,sizeof(OSVERSIONINFO)); + ov.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); + GetVersionEx(&ov); + + if((ov.dwPlatformId==VER_PLATFORM_WIN32_NT) && + (ov.dwMajorVersion>4)) + dw_access_flags = GENERIC_READ|GENERIC_WRITE; /* add gen write on W2k/XP */ + else dw_access_flags = GENERIC_READ; + + if (cdio_is_device_win32(env->gen.source_name)) { + sprintf( psz_win32_drive, "\\\\.\\%c:", env->gen.source_name[len-2] ); + + env->h_device_handle = CreateFile( psz_win32_drive, + dw_access_flags, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL ); + if( env->h_device_handle == INVALID_HANDLE_VALUE ) + { + /* No good. try toggle write. */ + dw_access_flags ^= GENERIC_WRITE; + env->h_device_handle = CreateFile( psz_win32_drive, + dw_access_flags, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL ); + return (env->h_device_handle == NULL) ? false : true; + } + return true; + } + return false; +} + +#define MSF_TO_LBA2(min, sec, frame) \ + ((int) frame + CDIO_CD_FRAMES_PER_SEC * (CDIO_CD_SECS_PER_MIN*min + sec) \ + - CDIO_PREGAP_SECTORS ) + +/*! + Read and cache the CD's Track Table of Contents and track info. + Return true if successful or false if an error. +*/ +bool +win32ioctl_read_toc (_img_private_t *env) +{ + + DWORD dwBytesReturned; + CDROM_TOC cdrom_toc; + int i; + + if( DeviceIoControl( env->h_device_handle, + IOCTL_CDROM_READ_TOC, + NULL, 0, &cdrom_toc, sizeof(CDROM_TOC), + &dwBytesReturned, NULL ) == 0 ) { + cdio_warn( "could not read TOCHDR: %ld" , (long int) GetLastError()); + return false; + } + + env->first_track_num = cdrom_toc.FirstTrack; + env->total_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1; + + + for( i = 0 ; i <= env->total_tracks ; i++ ) { + env->tocent[ i ].start_lsn = MSF_TO_LBA2( + cdrom_toc.TrackData[i].Address[1], + cdrom_toc.TrackData[i].Address[2], + cdrom_toc.TrackData[i].Address[3] ); + env->tocent[ i ].Control = cdrom_toc.TrackData[i].Control; + env->tocent[ i ].Format = cdrom_toc.TrackData[i].Format; + cdio_debug("p_sectors: %i, %lu", i, + (unsigned long int) (env->tocent[i].start_lsn)); + } + env->gen.toc_init = true; + return true; +} + +/*! + Return the media catalog number MCN. + + Note: string is malloc'd so caller should free() then returned + string when done with it. + + */ +char * +win32ioctl_get_mcn (_img_private_t *env) { + + DWORD dwBytesReturned; + SUB_Q_MEDIA_CATALOG_NUMBER mcn; + CDROM_SUB_Q_DATA_FORMAT q_data_format; + + memset( &mcn, 0, sizeof(mcn) ); + + q_data_format.Format = IOCTL_CDROM_MEDIA_CATALOG; + + q_data_format.Track=1; + + if( DeviceIoControl( env->h_device_handle, + IOCTL_CDROM_READ_Q_CHANNEL, + &q_data_format, sizeof(q_data_format), + &mcn, sizeof(mcn), + &dwBytesReturned, NULL ) == 0 ) { + cdio_warn( "could not read Q Channel at track %d", 1); + } else if (mcn.Mcval) + return strdup(mcn.MediaCatalog); + return NULL; +} + +/*! + Get the format (XA, DATA, AUDIO) of a track. +*/ +track_format_t +win32ioctl_get_track_format(_img_private_t *env, track_t track_num) +{ + /* This is pretty much copied from the "badly broken" cdrom_count_tracks + in linux/cdrom.c. + */ + if (env->tocent[track_num-1].Control & 0x04) { + if (env->tocent[track_num-1].Format == 0x10) + return TRACK_FORMAT_CDI; + else if (env->tocent[track_num-1].Format == 0x20) + return TRACK_FORMAT_XA; + else + return TRACK_FORMAT_DATA; + } else + return TRACK_FORMAT_AUDIO; +} + + +#endif /*HAVE_WIN32_CDROM*/ diff --git a/src/input/vcd/libcdio/MSWindows/win32.c b/src/input/vcd/libcdio/MSWindows/win32.c new file mode 100644 index 000000000..13ccb9131 --- /dev/null +++ b/src/input/vcd/libcdio/MSWindows/win32.c @@ -0,0 +1,672 @@ +/* + $Id: win32.c,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $ + + Copyright (C) 2003, 2004 Rocky Bernstein + + 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 file contains Win32-specific code and implements low-level + control of the CD drive. Inspired by vlc's cdrom.h code +*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +static const char _rcsid[] = "$Id: win32.c,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $"; + +#include +#include +#include +#include "cdio_assert.h" +#include "cdio_private.h" /* protoype for cdio_is_device_win32 */ +#include "scsi_mmc.h" + +#include + +#ifdef HAVE_WIN32_CDROM + +#include +#include +#include +#include +#include +#include + +#include +#include +#include "win32.h" + +#include +#include +#include "aspi32.h" + +#define WIN_NT ( GetVersion() < 0x80000000 ) + +/* General ioctl() CD-ROM command function */ +static bool +_cdio_mciSendCommand(int id, UINT msg, DWORD flags, void *arg) +{ + MCIERROR mci_error; + + mci_error = mciSendCommand(id, msg, flags, (DWORD)arg); + if ( mci_error ) { + char error[256]; + + mciGetErrorString(mci_error, error, 256); + cdio_warn("mciSendCommand() error: %s", error); + } + return(mci_error == 0); +} + +static const char * +cdio_is_cdrom(const char drive_letter) { + if ( WIN_NT ) { + return win32ioctl_is_cdrom(drive_letter); + } else { + return wnaspi32_is_cdrom(drive_letter); + } +} + +/*! + Initialize CD device. + */ +static bool +_cdio_init_win32 (void *user_data) +{ + _img_private_t *env = user_data; + if (env->gen.init) { + cdio_error ("init called more than once"); + return false; + } + + env->gen.init = true; + env->toc_init = false; + + + /* Initializations */ + env->h_device_handle = NULL; + env->i_sid = 0; + env->hASPI = 0; + env->lpSendCommand = 0; + + if ( WIN_NT ) { + return win32ioctl_init_win32(env); + } else { + return wnaspi32_init_win32(env); + } +} + +/*! + Release and free resources associated with cd. + */ +static void +_cdio_win32_free (void *user_data) +{ + _img_private_t *env = user_data; + + if (NULL == env) return; + free (env->gen.source_name); + + if( env->h_device_handle ) + CloseHandle( env->h_device_handle ); + if( env->hASPI ) + FreeLibrary( (HMODULE)env->hASPI ); + + free (env); +} + +/*! + Reads an audio device into data starting from lsn. + Returns 0 if no error. + */ +static int +_cdio_read_audio_sectors (void *user_data, void *data, lsn_t lsn, + unsigned int nblocks) +{ + _img_private_t *env = user_data; + if ( env->hASPI ) { + return wnaspi32_read_audio_sectors( env, data, lsn, nblocks ); + } else { + return win32ioctl_read_audio_sectors( env, data, lsn, nblocks ); + } +} + +/*! + Reads a single mode1 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_mode1_sector (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *env = user_data; + + if (env->gen.ioctls_debugged == 75) + cdio_debug ("only displaying every 75th ioctl from now on"); + + if (env->gen.ioctls_debugged == 30 * 75) + cdio_debug ("only displaying every 30*75th ioctl from now on"); + + if (env->gen.ioctls_debugged < 75 + || (env->gen.ioctls_debugged < (30 * 75) + && env->gen.ioctls_debugged % 75 == 0) + || env->gen.ioctls_debugged % (30 * 75) == 0) + cdio_debug ("reading %lu", (unsigned long int) lsn); + + env->gen.ioctls_debugged++; + + if ( env->hASPI ) { + return 1; + } else { + return win32ioctl_read_mode1_sector( env, data, lsn, b_form2 ); + } +} + +/*! + Reads nblocks of mode1 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_cdio_read_mode1_sectors (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 (b_form2) { + if ( (retval = _cdio_read_mode1_sector (env, + ((char *)data) + (M2RAW_SECTOR_SIZE * i), + lsn + i, true)) ) + return retval; + } else { + char buf[M2RAW_SECTOR_SIZE] = { 0, }; + if ( (retval = _cdio_read_mode1_sector (env, buf, lsn + i, false)) ) + return retval; + + memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), + buf, CDIO_CD_FRAMESIZE); + } + } + return 0; +} + +/*! + Reads a single mode2 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_mode2_sector (void *user_data, void *data, lsn_t lsn, + bool b_form2) +{ + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + _img_private_t *env = user_data; + + if (env->gen.ioctls_debugged == 75) + cdio_debug ("only displaying every 75th ioctl from now on"); + + if (env->gen.ioctls_debugged == 30 * 75) + cdio_debug ("only displaying every 30*75th ioctl from now on"); + + if (env->gen.ioctls_debugged < 75 + || (env->gen.ioctls_debugged < (30 * 75) + && env->gen.ioctls_debugged % 75 == 0) + || env->gen.ioctls_debugged % (30 * 75) == 0) + cdio_debug ("reading %lu", (unsigned long int) lsn); + + env->gen.ioctls_debugged++; + + if ( env->hASPI ) { + int ret; + ret = wnaspi32_read_mode2_sector(user_data, buf, lsn); + if( ret != 0 ) return ret; + if (b_form2) + memcpy (data, buf, M2RAW_SECTOR_SIZE); + else + memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); + return 0; + } else { + return win32ioctl_read_mode2_sector( env, data, lsn, b_form2 ); + } +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_cdio_read_mode2_sectors (void *user_data, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode2_sector (user_data, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +/*! + Return the size of the CD in logical block address (LBA) units. + */ +static uint32_t +_cdio_stat_size (void *user_data) +{ + _img_private_t *env = user_data; + + return env->tocent[env->total_tracks].start_lsn; +} + +/*! + Set the key "arg" to "value" in source device. +*/ +static int +_cdio_set_arg (void *user_data, const char key[], const char value[]) +{ + _img_private_t *env = user_data; + + if (!strcmp (key, "source")) + { + if (!value) + return -2; + + free (env->gen.source_name); + + env->gen.source_name = strdup (value); + } + else + return -1; + + return 0; +} + +/*! + Read and cache the CD's Track Table of Contents and track info. + Return true if successful or false if an error. +*/ +static bool +_cdio_read_toc (_img_private_t *env) +{ + bool ret; + if( env->hASPI ) { + ret = wnaspi32_read_toc( env ); + } else { + ret =win32ioctl_read_toc(env); + } + if (ret) env->gen.toc_init = true ; + return true; +} + +/*! + Eject media. Return 1 if successful, 0 otherwise. + */ +static int +_cdio_eject_media (void *user_data) { + + _img_private_t *env = user_data; + + + MCI_OPEN_PARMS op; + MCI_STATUS_PARMS st; + DWORD i_flags; + char psz_drive[4]; + int ret; + + memset( &op, 0, sizeof(MCI_OPEN_PARMS) ); + op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO; + strcpy( psz_drive, "X:" ); + psz_drive[0] = env->gen.source_name[0]; + op.lpstrElementName = psz_drive; + + /* Set the flags for the device type */ + i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | + MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE; + + if( _cdio_mciSendCommand( 0, MCI_OPEN, i_flags, &op ) ) { + st.dwItem = MCI_STATUS_READY; + /* Eject disc */ + ret = _cdio_mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 ) != 0; + /* Release access to the device */ + _cdio_mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 ); + } else + ret = 0; + + return ret; +} + +/*! + Return the value associated with the key "arg". +*/ +static const char * +_cdio_get_arg (void *user_data, const char key[]) +{ + _img_private_t *env = user_data; + + if (!strcmp (key, "source")) { + return env->gen.source_name; + } else if (!strcmp (key, "access-mode")) { + if (env->hASPI) + return "ASPI"; + else if ( WIN_NT ) + return "winNT/2K/XP ioctl"; + else + return "undefined WIN32"; + } + return NULL; +} + +/*! + Return the number of of the first track. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_t +_cdio_get_first_track_num(void *user_data) +{ + _img_private_t *env = user_data; + + if (!env->toc_init) _cdio_read_toc (env) ; + + return env->first_track_num; +} + +/*! + Return the media catalog number MCN. + + Note: string is malloc'd so caller should free() then returned + string when done with it. + + */ +static char * +_cdio_get_mcn (void *env) { + + _img_private_t *_env = env; + + if( ! _env->hASPI ) { + win32ioctl_get_mcn(_env); + } + return NULL; +} + +/*! + Return the number of tracks in the current medium. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_t +_cdio_get_num_tracks(void *user_data) +{ + _img_private_t *env = user_data; + + if (!env->toc_init) _cdio_read_toc (env) ; + + return env->total_tracks; +} + +/*! + Get format of track. +*/ +static track_format_t +_cdio_get_track_format(void *obj, track_t track_num) +{ + _img_private_t *env = obj; + + if (!env->gen.toc_init) _cdio_read_toc (env) ; + + if (track_num > env->total_tracks || track_num == 0) + return TRACK_FORMAT_ERROR; + + if( env->hASPI ) { + return wnaspi32_get_track_format(env, track_num); + } else { + return win32ioctl_get_track_format(env, track_num); + } +} + +/*! + 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 +_cdio_get_track_green(void *obj, track_t track_num) +{ + _img_private_t *env = obj; + + if (!env->toc_init) _cdio_read_toc (env) ; + + if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = env->total_tracks+1; + + if (track_num > env->total_tracks+1 || track_num == 0) + return false; + + switch (_cdio_get_track_format(env, track_num)) { + case TRACK_FORMAT_ERROR: + case TRACK_FORMAT_CDI: + case TRACK_FORMAT_AUDIO: + return false; + default: + break; + } + + /* FIXME: Dunno if this is the right way, but it's what + I was using in cd-info for a while. + */ + return ((env->tocent[track_num-1].Control & 2) != 0); +} + +/*! + Return the starting MSF (minutes/secs/frames) for track number + track_num in obj. 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. +*/ +static bool +_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf) +{ + _img_private_t *_obj = env; + + if (NULL == msf) return false; + + if (!_obj->toc_init) _cdio_read_toc (_obj) ; + + if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; + + if (track_num > _obj->total_tracks+1 || track_num == 0) { + return false; + } else { + cdio_lsn_to_msf(_obj->tocent[track_num-1].start_lsn, msf); + return true; + } +} + +#endif /* HAVE_WIN32_CDROM */ + +/*! + Return an array of strings giving possible CD devices. + */ +char ** +cdio_get_devices_win32 (void) +{ +#ifndef HAVE_WIN32_CDROM + return NULL; +#else + char **drives = NULL; + unsigned int num_drives=0; + char drive_letter; + + /* Scan the system for CD-ROM drives. + */ + +#if FINISHED + /* Now check the currently mounted CD drives */ + if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) { + cdio_add_device_list(&drives, drive, &num_drives); + } + + /* Finally check possible mountable drives in /etc/fstab */ + if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) { + cdio_add_device_list(&drives, drive, &num_drives); + } +#endif + + /* Scan the system for CD-ROM drives. + Not always 100% reliable, so use the USE_MNTENT code above first. + */ + for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) { + const char *drive_str=cdio_is_cdrom(drive_letter); + if (drive_str != NULL) { + cdio_add_device_list(&drives, drive_str, &num_drives); + } + } + cdio_add_device_list(&drives, NULL, &num_drives); + return drives; +#endif /*HAVE_WIN32_CDROM*/ +} + +/*! + Return a string containing the default CD device if none is specified. + if CdIo is NULL (we haven't initialized a specific device driver), + then find a suitable one and return the default device for that. + + NULL is returned if we couldn't get a default device. +*/ +char * +cdio_get_default_device_win32(void) +{ + +#ifdef HAVE_WIN32_CDROM + char drive_letter; + + for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) { + const char *drive_str=cdio_is_cdrom(drive_letter); + if (drive_str != NULL) { + return strdup(drive_str); + } + } +#endif + return NULL; +} + +/*! + Return true if source_name could be a device containing a CD-ROM. +*/ +bool +cdio_is_device_win32(const char *source_name) +{ + unsigned int len; + len = strlen(source_name); + + if (NULL == source_name) return false; + +#ifdef HAVE_WIN32_CDROM + if ((len == 2) && isalpha(source_name[0]) + && (source_name[len-1] == ':')) + return true; + + if ( ! WIN_NT ) return false; + + /* Test to see if of form: \\.\x: */ + return ( (len == 6) + && source_name[0] == '\\' && source_name[1] == '\\' + && source_name[2] == '.' && source_name[3] == '\\' + && isalpha(source_name[len-2]) + && (source_name[len-1] == ':') ); +#else + return false; +#endif +} + +/*! + 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_win32 (const char *source_name) +{ + +#ifdef HAVE_WIN32_CDROM + CdIo *ret; + _img_private_t *_data; + + cdio_funcs _funcs = { + .eject_media = _cdio_eject_media, + .free = _cdio_win32_free, + .get_arg = _cdio_get_arg, + .get_default_device = cdio_get_default_device_win32, + .get_devices = cdio_get_devices_win32, + .get_first_track_num= _cdio_get_first_track_num, + .get_mcn = _cdio_get_mcn, + .get_num_tracks = _cdio_get_num_tracks, + .get_track_format = _cdio_get_track_format, + .get_track_green = _cdio_get_track_green, + .get_track_lba = NULL, /* This could be implemented if need be. */ + .get_track_msf = _cdio_get_track_msf, + .lseek = NULL, + .read = NULL, + .read_audio_sectors = _cdio_read_audio_sectors, + .read_mode1_sector = _cdio_read_mode1_sector, + .read_mode1_sectors = _cdio_read_mode1_sectors, + .read_mode2_sector = _cdio_read_mode2_sector, + .read_mode2_sectors = _cdio_read_mode2_sectors, + .set_arg = _cdio_set_arg, + .stat_size = _cdio_stat_size + }; + + _data = _cdio_malloc (sizeof (_img_private_t)); + _data->gen.init = false; + _data->gen.fd = -1; + + _cdio_set_arg(_data, "source", (NULL == source_name) + ? cdio_get_default_device_win32(): source_name); + + ret = cdio_new (_data, &_funcs); + if (ret == NULL) return NULL; + + if (_cdio_init_win32(_data)) + return ret; + else { + _cdio_win32_free (_data); + return NULL; + } + +#else + return NULL; +#endif /* HAVE_WIN32_CDROM */ + +} + +bool +cdio_have_win32 (void) +{ +#ifdef HAVE_WIN32_CDROM + return true; +#else + return false; +#endif /* HAVE_WIN32_CDROM */ +} diff --git a/src/input/vcd/libcdio/MSWindows/win32.h b/src/input/vcd/libcdio/MSWindows/win32.h new file mode 100644 index 000000000..d70797c08 --- /dev/null +++ b/src/input/vcd/libcdio/MSWindows/win32.h @@ -0,0 +1,90 @@ +/* + $Id: win32.h,v 1.1 2004/04/11 12:20:31 miguelfreitas Exp $ + + Copyright (C) 2004 Rocky Bernstein + + 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 +*/ + +#include "cdio_private.h" + +#pragma pack() + +typedef struct { + lsn_t start_lsn; + UCHAR Control : 4; + UCHAR Format; +} track_info_t; + +typedef struct { + /* Things common to all drivers like this. + This must be first. */ + generic_img_private_t gen; + + HANDLE h_device_handle; /* device descriptor */ + long hASPI; + short i_sid; + long (*lpSendCommand)( void* ); + + /* Track information */ + bool toc_init; /* if true, info below is valid. */ + track_info_t tocent[100]; /* entry info for each track */ + track_t total_tracks; /* number of tracks in image */ + track_t first_track_num; /* track number of first track */ + +} _img_private_t; + +/*! + Reads an audio device using the DeviceIoControl method into data + starting from lsn. Returns 0 if no error. + */ +int win32ioctl_read_audio_sectors (_img_private_t *obj, void *data, lsn_t lsn, + unsigned int nblocks); +/*! + Reads a single mode2 sector using the DeviceIoControl method into + data starting from lsn. Returns 0 if no error. + */ +int +win32ioctl_read_mode2_sector (_img_private_t *env, void *data, lsn_t lsn, + bool mode2_form2); + +/*! + Reads a single mode1 sector using the DeviceIoControl method into + data starting from lsn. Returns 0 if no error. + */ +int +win32ioctl_read_mode1_sector (_img_private_t *env, void *data, lsn_t lsn, + bool mode2_form2); + +const char *win32ioctl_is_cdrom(const char drive_letter); + +/*! + Initialize internal structures for CD device. + */ +bool win32ioctl_init_win32 (_img_private_t *env); + +/*! + Read and cache the CD's Track Table of Contents and track info. + Return true if successful or false if an error. +*/ +bool win32ioctl_read_toc (_img_private_t *env); + +char *win32ioctl_get_mcn (_img_private_t *env); + +/*! + Get the format (XA, DATA, AUDIO) of a track. +*/ +track_format_t win32ioctl_get_track_format(_img_private_t *env, + track_t track_num); diff --git a/src/input/vcd/libcdio/Makefile.am b/src/input/vcd/libcdio/Makefile.am index 636c734f7..799d4585f 100644 --- a/src/input/vcd/libcdio/Makefile.am +++ b/src/input/vcd/libcdio/Makefile.am @@ -1,21 +1,25 @@ include $(top_srcdir)/misc/Makefile.common -SUBDIRS = cdio +SUBDIRS = cdio MSWindows image INCLUDES = $(LIBCDIO_CFLAGS) libcdio_SRCS = \ - _cdio_bincue.c \ + image/bincue.c \ + image/nrg.c \ + MSWindows/aspi32.c \ + MSWindows/aspi32.h \ + MSWindows/ioctl.c \ + MSWindows/win32.c \ + MSWindows/win32.h \ _cdio_bsdi.c \ _cdio_freebsd.c \ _cdio_generic.c \ _cdio_linux.c \ _cdio_osx.c \ - _cdio_nrg.c \ _cdio_stdio.c \ _cdio_stream.c \ _cdio_sunos.c \ - _cdio_win32.c \ cdio.c \ cd_types.c \ ds.c \ diff --git a/src/input/vcd/libcdio/_cdio_bincue.c b/src/input/vcd/libcdio/_cdio_bincue.c deleted file mode 100644 index 04293c483..000000000 --- a/src/input/vcd/libcdio/_cdio_bincue.c +++ /dev/null @@ -1,962 +0,0 @@ -/* - $Id: _cdio_bincue.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ - - Copyright (C) 2001 Herbert Valerio Riedel - Copyright (C) 2002,2003 Rocky Bernstein - - 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: _cdio_bincue.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; - -#include "cdio_assert.h" -#include "cdio_private.h" -#include "_cdio_stdio.h" - -#include -#include -#include - -#ifdef HAVE_STDIO_H -#include -#endif -#ifdef HAVE_STDLIB_H -#include -#endif -#ifdef HAVE_STRING_H -#include -#endif -#ifdef HAVE_STRINGS_H -#include -#endif -#ifdef HAVE_GLOB_H -#include -#endif -#include - -/* FIXME: should put in a common definition somewhere. */ -#ifdef HAVE_BZERO -#define BZERO(ptr, size) bzero(ptr, size) -#elif HAVE_MEMSET -#define BZERO(ptr, size) memset(ptr, 0, size) -#else - Error -- you need either memset or bzero -#endif - -/* reader */ - -#define DEFAULT_CDIO_DEVICE "videocd.bin" -#define DEFAULT_CDIO_CUE "videocd.cue" - -typedef struct { - track_t track_num; /* Probably is index+1 */ - msf_t start_msf; - lba_t start_lba; - int start_index; - int sec_count; /* Number of sectors in this track. Does not - include pregap */ - int num_indices; - int flags; /* "DCP", "4CH", "PRE" */ - track_format_t track_format; - bool track_green; - uint16_t datasize; /* How much is in the portion we return back? */ - uint16_t datastart; /* Offset from begining that data starts */ - uint16_t endsize; /* How much stuff at the end to skip over. This - stuff may have error correction (EDC, or ECC).*/ - uint16_t blocksize; /* total block size = start + size + end */ - - -} track_info_t; - -typedef struct { - /* Things common to all drivers like this. - This must be first. */ - generic_img_private_t gen; - internal_position_t pos; - - bool sector_2336; /* Playstation (PSX) uses 2336-byte sectors */ - - char *cue_name; - char *mcn; /* Media catalog number. */ - track_info_t tocent[100]; /* entry info for each track */ - track_t total_tracks; /* number of tracks in image */ - track_t first_track_num; /* track number of first track */ - bool have_cue; -} _img_private_t; - -static bool _cdio_image_read_cue (_img_private_t *_obj); -static uint32_t _cdio_stat_size (void *env); - -/*! - Initialize image structures. - */ -static bool -_cdio_init (_img_private_t *_obj) -{ - lsn_t lead_lsn; - - if (_obj->gen.init) - return false; - - if (!(_obj->gen.data_source = cdio_stdio_new (_obj->gen.source_name))) { - cdio_error ("init failed"); - return false; - } - - /* Have to set init before calling _cdio_stat_size() or we will - get into infinite recursion calling passing right here. - */ - _obj->gen.init = true; - - lead_lsn = _cdio_stat_size( (_img_private_t *) _obj); - - if (-1 == lead_lsn) - return false; - - /* Read in CUE sheet. */ - if ((_obj->cue_name != NULL)) { - _obj->have_cue = _cdio_image_read_cue(_obj); - } - - if (!_obj->have_cue ) { - /* Time to fake things... - Make one big track, track 0 and 1 are the same. - We are guessing stuff starts at msf 00:04:00 - 2 for the 150 - sector pregap and 2 for the cue information. - */ - track_info_t *this_track=&(_obj->tocent[0]); - int blocksize = _obj->sector_2336 - ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; - - _obj->total_tracks = 2; - _obj->first_track_num = 1; - this_track->start_msf.m = to_bcd8(0); - this_track->start_msf.s = to_bcd8(4); - this_track->start_msf.f = to_bcd8(0); - this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf); - this_track->blocksize = blocksize; - this_track->track_format= TRACK_FORMAT_XA; - this_track->track_green = true; - - - _obj->tocent[1] = _obj->tocent[0]; - } - - /* Fake out leadout track and sector count for last track*/ - cdio_lsn_to_msf (lead_lsn, &_obj->tocent[_obj->total_tracks].start_msf); - _obj->tocent[_obj->total_tracks].start_lba = cdio_lsn_to_lba(lead_lsn); - _obj->tocent[_obj->total_tracks-1].sec_count = - cdio_lsn_to_lba(lead_lsn - _obj->tocent[_obj->total_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 -_cdio_lseek (void *env, off_t offset, int whence) -{ - _img_private_t *_obj = env; - - /* 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; - - _obj->pos.lba = 0; - for (i=0; i<_obj->total_tracks; i++) { - track_info_t *this_track=&(_obj->tocent[i]); - _obj->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; - _obj->pos.buff_offset = rem; - _obj->pos.lba += blocks; - break; - } - real_offset += this_track->sec_count*this_track->blocksize; - offset -= this_track->sec_count*this_track->datasize; - _obj->pos.lba += this_track->sec_count; - } - - if (i==_obj->total_tracks) { - cdio_warn ("seeking outside range of disk image"); - return -1; - } else { - real_offset += _obj->tocent[i].datastart; - return cdio_stream_seek(_obj->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 -_cdio_read (void *env, void *data, size_t size) -{ - _img_private_t *_obj = env; - char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; - char *p = data; - ssize_t final_size=0; - ssize_t this_size; - track_info_t *this_track=&(_obj->tocent[_obj->pos.index]); - ssize_t skip_size = this_track->datastart + this_track->endsize; - - while (size > 0) { - int rem = this_track->datasize - _obj->pos.buff_offset; - if (size <= rem) { - this_size = cdio_stream_read(_obj->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(_obj->gen.data_source, buf, rem, 1); - final_size += this_size; - memcpy (p, buf, this_size); - p += this_size; - this_size = cdio_stream_read(_obj->gen.data_source, buf, rem, 1); - - /* Skip over stuff at end of this sector and the beginning of the next. - */ - cdio_stream_read(_obj->gen.data_source, buf, skip_size, 1); - - /* Get ready to read another sector. */ - _obj->pos.buff_offset=0; - _obj->pos.lba++; - - /* Have gone into next track. */ - if (_obj->pos.lba >= _obj->tocent[_obj->pos.index+1].start_lba) { - _obj->pos.index++; - this_track=&(_obj->tocent[_obj->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 -_cdio_stat_size (void *env) -{ - _img_private_t *_obj = env; - long size; - int blocksize = _obj->sector_2336 - ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; - - _cdio_init (_obj); - - size = cdio_stream_stat (_obj->gen.data_source); - - if (size % blocksize) - { - cdio_warn ("image %s size (%ld) not multiple of blocksize (%d)", - _obj->gen.source_name, size, blocksize); - 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 /= blocksize; - - return size; -} - -#define MAXLINE 512 - -static bool -_cdio_image_read_cue (_img_private_t *_obj) -{ - FILE *fp; - char line[MAXLINE]; - - int track_num; - int min,sec,frame; - int blocksize; - int start_index; - bool seen_first_index_for_track=false; - - if ( _obj == NULL || _obj->cue_name == NULL ) return false; - - fp = fopen (_obj->cue_name, "r"); - if (fp == NULL) return false; - - _obj->total_tracks=0; - _obj->first_track_num=1; - _obj->mcn=NULL; - - while ((fgets(line, MAXLINE, fp)) != NULL) { - char s[80]; - char *p; - /*printf("Retrieved line of length %zu :\n", read); - printf("%s", line); */ - for (p=line; isspace(*p); p++) ; - if (1==sscanf(p, "FILE \"%80s[^\"]", s)) { - /* Should expand file name based on cue file basename. - free(_obj->bin_file); - _obj->bin_file = strdup(s); - */ - /* printf("Found file name %s\n", s); */ - } else if (1==sscanf(p, "CATALOG %80s", s)) { - _obj->mcn = strdup(s); - } else if (2==sscanf(p, "TRACK %d MODE2/%d", &track_num, &blocksize)) { - track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]); - this_track->track_num = track_num; - this_track->num_indices = 0; - this_track->track_format= TRACK_FORMAT_XA; - this_track->track_green = true; - _obj->total_tracks++; - seen_first_index_for_track=false; - /*printf("Added track %d with blocksize %d\n", track_num, blocksize);*/ - - this_track->blocksize = blocksize; - switch(blocksize) { - case 2336: - this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE; - this_track->datasize = M2RAW_SECTOR_SIZE; - this_track->endsize = 0; - break; - default: - cdio_warn ("Unknown MODE2 size %d. Assuming 2352", blocksize); - case 2352: - if (_obj->sector_2336) { - this_track->datastart = 0; - this_track->datasize = M2RAW_SECTOR_SIZE; - this_track->endsize = blocksize - 2336; - } 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; - } - - } else if (2==sscanf(p, "TRACK %d MODE1/%d", &track_num, &blocksize)) { - track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]); - this_track->blocksize = blocksize; - this_track->blocksize = blocksize; - switch(blocksize) { - case 2048: - /* Is the below correct? */ - this_track->datastart = 0; - this_track->datasize = CDIO_CD_FRAMESIZE; - this_track->endsize = 0; - break; - default: - cdio_warn ("Unknown MODE1 size %d. Assuming 2352", blocksize); - case 2352: - 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->track_num = track_num; - this_track->num_indices = 0; - this_track->track_format = TRACK_FORMAT_DATA; - this_track->track_green = false; - _obj->total_tracks++; - seen_first_index_for_track=false; - /*printf("Added track %d with blocksize %d\n", track_num, blocksize);*/ - - } else if (1==sscanf(p, "TRACK %d AUDIO", &track_num)) { - track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]); - 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_num = track_num; - this_track->num_indices = 0; - this_track->track_format = TRACK_FORMAT_AUDIO; - this_track->track_green = false; - _obj->total_tracks++; - seen_first_index_for_track=false; - - } else if (4==sscanf(p, "INDEX %d %d:%d:%d", - &start_index, &min, &sec, &frame)) { - track_info_t *this_track=&(_obj->tocent[_obj->total_tracks-1]); - /* FIXME! all of this is a big hack. - If start_index == 0, then this is the "last_cue" information. - The +2 below seconds is to adjust for the 150 pregap. - */ - if (start_index != 0) { - if (!seen_first_index_for_track) { - this_track->start_index = start_index; - sec += 2; - if (sec >= 60) { - min++; - sec -= 60; - } - this_track->start_msf.m = to_bcd8 (min); - this_track->start_msf.s = to_bcd8 (sec); - this_track->start_msf.f = to_bcd8 (frame); - this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf); - seen_first_index_for_track=true; - } - - if (_obj->total_tracks > 1) { - /* Figure out number of sectors for previous track */ - track_info_t *prev_track=&(_obj->tocent[_obj->total_tracks-2]); - if ( this_track->start_lba < prev_track->start_lba ) { - cdio_warn("track %d at LBA %lu starts before track %d at LBA %lu", - _obj->total_tracks, - (unsigned long int) this_track->start_lba, - _obj->total_tracks-1, - (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_warn ("%lu fewer than pregap (%d) sectors in track %d", - (long unsigned int) - this_track->start_lba - prev_track->start_lba, - CDIO_PREGAP_SECTORS, - _obj->total_tracks-1); - /* 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++; - } - } - } - _obj->have_cue = _obj->total_tracks != 0; - - fclose (fp); - return true; -} - -/*! - Reads a single audio sector from CD device into data starting - from lsn. Returns 0 if no error. - */ -static int -_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, - unsigned int nblocks) -{ - _img_private_t *_obj = env; - int ret; - - _cdio_init (_obj); - - /* Why the adjustment of 272, I don't know. It seems to work though */ - if (lsn != 0) { - ret = cdio_stream_seek (_obj->gen.data_source, - (lsn * CDIO_CD_FRAMESIZE_RAW) - 272, SEEK_SET); - if (ret!=0) return ret; - - ret = cdio_stream_read (_obj->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 (_obj->gen.data_source, 0, SEEK_SET); - - if (ret!=0) return ret; - - ret = cdio_stream_read (_obj->gen.data_source, (uint8_t *) data+272, - CDIO_CD_FRAMESIZE_RAW - 272, nblocks); - } - - return ret; -} - -/*! - Reads a single mode2 sector from cd device into data starting - from lsn. Returns 0 if no error. - */ -static int -_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, - bool mode2_form2) -{ - _img_private_t *_obj = env; - int ret; - char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; - int blocksize = _obj->sector_2336 - ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; - - _cdio_init (_obj); - - ret = cdio_stream_seek (_obj->gen.data_source, lsn * blocksize, SEEK_SET); - if (ret!=0) return ret; - - ret = cdio_stream_read (_obj->gen.data_source, - _obj->sector_2336 - ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE) - : buf, - blocksize, 1); - if (ret==0) return ret; - - if (mode2_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 -_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn, - bool mode2_form2, unsigned int nblocks) -{ - _img_private_t *_obj = env; - int i; - int retval; - - for (i = 0; i < nblocks; i++) { - if (mode2_form2) { - if ( (retval = _cdio_read_mode2_sector (_obj, - ((char *)data) + (M2RAW_SECTOR_SIZE * i), - lsn + i, true)) ) - return retval; - } else { - char buf[M2RAW_SECTOR_SIZE] = { 0, }; - if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) ) - return retval; - - memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), - buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); - } - } - return 0; -} - -#define free_if_notnull(obj) \ - if (NULL != obj) free(obj); - -static void -_cdio_bincue_free (void *env) { - _img_private_t *_obj = env; - - if (NULL == _obj) return; - free_if_notnull(_obj->mcn); - cdio_generic_stream_free(_obj); -} - -/*! - Set the arg "key" with "value" in the source device. - Currently "source" to set the source device in I/O operations - is the only valid key. - - 0 is returned if no error was found, and nonzero if there as an error. -*/ -/*! - Set the device to use in I/O operations. -*/ -static int -_cdio_set_arg (void *env, const char key[], const char value[]) -{ - _img_private_t *_obj = env; - - if (!strcmp (key, "source")) - { - free_if_notnull (_obj->gen.source_name); - - if (!value) - return -2; - - _obj->gen.source_name = strdup (value); - } - else if (!strcmp (key, "sector")) - { - if (!strcmp (value, "2336")) - _obj->sector_2336 = true; - else if (!strcmp (value, "2352")) - _obj->sector_2336 = false; - else - return -2; - } - else if (!strcmp (key, "cue")) - { - free_if_notnull (_obj->cue_name); - - if (!value) - return -2; - - _obj->cue_name = strdup (value); - } - else - return -1; - - return 0; -} - -/*! - Return the value associated with the key "arg". -*/ -static const char * -_cdio_get_arg (void *env, const char key[]) -{ - _img_private_t *_obj = env; - - if (!strcmp (key, "source")) { - return _obj->gen.source_name; - } else if (!strcmp (key, "cue")) { - return _obj->cue_name; - } - return NULL; -} - -/*! - Return an array of strings giving possible NRG 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; ifirst_track_num; -} - -/*! - Return the media catalog number (MCN) from the CD or NULL if there - is none or we don't have the ability to get it. - - Note: string is malloc'd so caller has to free() the returned - string when done with it. - */ -static char * -_cdio_get_mcn(void *env) -{ - _img_private_t *_obj = env; - - _cdio_init (_obj); - - if (NULL == _obj->mcn) return NULL; - return strdup(_obj->mcn); -} - -/*! - Return the number of tracks in the current medium. - If no cuesheet is available, We fake it an just say there's - one big track. - CDIO_INVALID_TRACK is returned on error. -*/ -static track_t -_cdio_get_num_tracks(void *env) -{ - _img_private_t *_obj = env; - _cdio_init (_obj); - - return _obj->have_cue && _obj->total_tracks > 0 ? _obj->total_tracks : 1; -} - -/*! - Return the number of tracks in the current medium. - CDIO_INVALID_TRACK is returned on error. -*/ -static track_format_t -_cdio_get_track_format(void *env, track_t track_num) -{ - _img_private_t *_obj = env; - - if (!_obj->gen.init) _cdio_init(_obj); - - if (track_num > _obj->total_tracks || track_num == 0) - return TRACK_FORMAT_ERROR; - - return _obj->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 -_cdio_get_track_green(void *env, track_t track_num) -{ - _img_private_t *_obj = env; - - if (!_obj->gen.init) _cdio_init(_obj); - - if (track_num > _obj->total_tracks || track_num == 0) - return false; - - return _obj->tocent[track_num-1].track_green; -} - -/*! - Return the starting LSN track number - track_num in obj. 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. -*/ -static lba_t -_cdio_get_track_lba(void *env, track_t track_num) -{ - _img_private_t *_obj = env; - _cdio_init (_obj); - - if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; - - if (track_num <= _obj->total_tracks+1 && track_num != 0) { - return _obj->tocent[track_num-1].start_lba; - } else - return CDIO_INVALID_LBA; -} - -/*! - Return the starting MSF (minutes/secs/frames) for the track number - track_num in obj. Tracks numbers start at 1. - The "leadout" track is specified either by - using track_num LEADOUT_TRACK or the total tracks+1. - -*/ -static bool -_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf) -{ - _img_private_t *_obj = env; - _cdio_init (_obj); - - if (NULL == msf) return false; - - if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; - - if (track_num <= _obj->total_tracks+1 && track_num != 0) { - *msf = _obj->tocent[track_num-1].start_msf; - return true; - } else - return false; -} - -/*! - Return corresponding BIN file if cue_name is a cue file or NULL - if not a CUE file. - -*/ -/* Later we'll probably parse the entire file. For now though, this gets us - started for now. -*/ -char * -cdio_is_cuefile(const char *cue_name) -{ - int i; - char *bin_name; - - if (cue_name == NULL) return false; - - bin_name=strdup(cue_name); - i=strlen(bin_name)-strlen("cue"); - - if (i>0) { - if (cue_name[i]=='c' && cue_name[i+1]=='u' && cue_name[i+2]=='e') { - bin_name[i++]='b'; bin_name[i++]='i'; bin_name[i++]='n'; - return bin_name; - } - else if (cue_name[i]=='C' && cue_name[i+1]=='U' && cue_name[i+2]=='E') { - bin_name[i++]='B'; bin_name[i++]='I'; bin_name[i++]='N'; - return bin_name; - } - } - free(bin_name); - return NULL; -} - -/*! - Return corresponding CUE file if bin_name is a bin file or NULL - if not a BIN file. - -*/ -/* Later we'll probably do better. For now though, this gets us - started for now. -*/ -char * -cdio_is_binfile(const char *bin_name) -{ - int i; - char *cue_name; - - if (bin_name == NULL) return false; - - cue_name=strdup(bin_name); - i=strlen(bin_name)-strlen("bin"); - - if (i>0) { - if (bin_name[i]=='b' && bin_name[i+1]=='i' && bin_name[i+2]=='n') { - cue_name[i++]='c'; cue_name[i++]='u'; cue_name[i++]='e'; - return cue_name; - } - else if (bin_name[i]=='B' && bin_name[i+1]=='I' && bin_name[i+2]=='N') { - cue_name[i++]='C'; cue_name[i++]='U'; cue_name[i++]='E'; - return cue_name; - } - } - free(cue_name); - return NULL; -} - -static CdIo * -cdio_open_common (_img_private_t **_data) -{ - cdio_funcs _funcs = { - .eject_media = cdio_generic_bogus_eject_media, - .free = _cdio_bincue_free, - .get_arg = _cdio_get_arg, - .get_default_device = cdio_get_default_device_bincue, - .get_first_track_num= _cdio_get_first_track_num, - .get_mcn = _cdio_get_mcn, - .get_num_tracks = _cdio_get_num_tracks, - .get_track_format = _cdio_get_track_format, - .get_track_green = _cdio_get_track_green, - .get_track_lba = _cdio_get_track_lba, - .get_track_msf = _cdio_get_track_msf, - .lseek = _cdio_lseek, - .read = _cdio_read, - .read_audio_sectors = _cdio_read_audio_sectors, - .read_mode2_sector = _cdio_read_mode2_sector, - .read_mode2_sectors = _cdio_read_mode2_sectors, - .set_arg = _cdio_set_arg, - .stat_size = _cdio_stat_size - }; - - *_data = _cdio_malloc (sizeof (_img_private_t)); - (*_data)->gen.init = false; - (*_data)->sector_2336 = false; - (*_data)->cue_name = NULL; - - return cdio_new (*_data, &_funcs); -} - -CdIo * -cdio_open_bincue (const char *source_name) -{ - char *bin_name = cdio_is_cuefile(source_name); - - if (NULL != bin_name) { - return cdio_open_cue(source_name); - } else { - char *cue_name = cdio_is_binfile(source_name); - CdIo *cdio = cdio_open_cue(cue_name); - free(cue_name); - return cdio; - } -} - -CdIo * -cdio_open_cue (const char *cue_name) -{ - CdIo *ret; - _img_private_t *_data; - char *bin_name; - - if (NULL == cue_name) return NULL; - ret = cdio_open_common(&_data); - if (ret == NULL) return NULL; - - bin_name = cdio_is_cuefile(cue_name); - - if (NULL == bin_name) { - cdio_error ("source name %s is not recognized as a CUE file", cue_name); - } - - _cdio_set_arg (_data, "cue", cue_name); - _cdio_set_arg (_data, "source", bin_name); - free(bin_name); - - if (_cdio_init(_data)) { - return ret; - } else { - cdio_generic_stream_free (_data); - return NULL; - } -} - -bool -cdio_have_bincue (void) -{ - return true; -} diff --git a/src/input/vcd/libcdio/_cdio_bsdi.c b/src/input/vcd/libcdio/_cdio_bsdi.c index c3770b592..7fd60a9d9 100644 --- a/src/input/vcd/libcdio/_cdio_bsdi.c +++ b/src/input/vcd/libcdio/_cdio_bsdi.c @@ -1,8 +1,8 @@ /* - $Id: _cdio_bsdi.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_bsdi.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2001 Herbert Valerio Riedel - Copyright (C) 2002,2003 Rocky Bernstein + Copyright (C) 2002, 2003, 2004 Rocky Bernstein 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 @@ -27,7 +27,7 @@ # include "config.h" #endif -static const char _rcsid[] = "$Id: _cdio_bsdi.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_bsdi.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; #include #include @@ -195,13 +195,58 @@ _read_audio_sectors (void *env, void *data, lsn_t lsn, return 0; } +/*! + Reads a single mode1 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_mode1_sector (void *env, void *data, lsn_t lsn, + bool b_form2) +{ + + char buf[M2RAW_SECTOR_SIZE] = { 0, }; +#if FIXED + do something here. +#else + if (0 > cdio_generic_lseek(env, CDIO_CD_FRAMESIZE*lsn, SEEK_SET)) + return -1; + if (0 > cdio_generic_read(env, buf, CDIO_CD_FRAMESIZE)) + return -1; + memcpy (data, buf, b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); +#endif + return 0; +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_cdio_read_mode1_sectors (void *env, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *_obj = env; + unsigned int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode1_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + /*! Reads a single mode2 sector from cd device into data starting from lsn. Returns 0 if no error. */ static int _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, - bool mode2_form2) + bool b_form2) { char buf[M2RAW_SECTOR_SIZE] = { 0, }; struct cdrom_msf *msf = (struct cdrom_msf *) &buf; @@ -246,7 +291,7 @@ _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, break; } - if (mode2_form2) + if (b_form2) memcpy (data, buf, M2RAW_SECTOR_SIZE); else memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); @@ -261,14 +306,14 @@ _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, */ static int _cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn, - bool mode2_form2, unsigned int nblocks) + bool b_form2, unsigned int nblocks) { _img_private_t *_obj = env; int i; int retval; for (i = 0; i < nblocks; i++) { - if (mode2_form2) { + if (b_form2) { if ( (retval = _cdio_read_mode2_sector (_obj, ((char *)data) + (M2RAW_SECTOR_SIZE * i), lsn + i, true)) ) @@ -667,8 +712,10 @@ cdio_open_bsdi (const char *source_name) .get_track_msf = _cdio_get_track_msf, .lseek = cdio_generic_lseek, .read = cdio_generic_read, - .read_mode2_sector = _cdio_read_mode2_sector, .read_audio_sectors = _read_audio_sectors, + .read_mode1_sector = _cdio_read_mode1_sector, + .read_mode1_sectors = _cdio_read_mode1_sectors, + .read_mode2_sector = _cdio_read_mode2_sector, .read_mode2_sectors = _cdio_read_mode2_sectors, .set_arg = _cdio_set_arg, .stat_size = _cdio_stat_size diff --git a/src/input/vcd/libcdio/_cdio_freebsd.c b/src/input/vcd/libcdio/_cdio_freebsd.c index 1eaf227ef..0aae6a6a8 100644 --- a/src/input/vcd/libcdio/_cdio_freebsd.c +++ b/src/input/vcd/libcdio/_cdio_freebsd.c @@ -1,5 +1,5 @@ /* - $Id: _cdio_freebsd.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_freebsd.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2003 Rocky Bernstein @@ -27,7 +27,7 @@ # include "config.h" #endif -static const char _rcsid[] = "$Id: _cdio_freebsd.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_freebsd.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; #include #include diff --git a/src/input/vcd/libcdio/_cdio_generic.c b/src/input/vcd/libcdio/_cdio_generic.c index 180de1086..1328fb2a7 100644 --- a/src/input/vcd/libcdio/_cdio_generic.c +++ b/src/input/vcd/libcdio/_cdio_generic.c @@ -1,8 +1,8 @@ /* - $Id: _cdio_generic.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_generic.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2001 Herbert Valerio Riedel - Copyright (C) 2002,2003 Rocky Bernstein + Copyright (C) 2002, 2003, 2004 Rocky Bernstein 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 @@ -27,7 +27,7 @@ # include "config.h" #endif -static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; #include #include @@ -126,7 +126,7 @@ cdio_generic_read (void *user_data, void *buf, size_t size) Release and free resources associated with stream or disk image. */ void -cdio_generic_stream_free (void *user_data) +cdio_generic_stdio_free (void *user_data) { generic_img_private_t *_obj = user_data; @@ -134,9 +134,7 @@ cdio_generic_stream_free (void *user_data) free (_obj->source_name); if (_obj->data_source) - cdio_stream_destroy (_obj->data_source); - - free (_obj); + cdio_stdio_destroy (_obj->data_source); } @@ -177,17 +175,31 @@ cdio_add_device_list(char **device_list[], const char *drive, int *num_drives) { if (NULL != drive) { unsigned int j; + + /* Check if drive is already in list. */ for (j=0; j<*num_drives; j++) { if (strcmp((*device_list)[j], drive) == 0) break; } + if (j==*num_drives) { + /* Drive not in list. Add it. */ (*num_drives)++; - *device_list = realloc(*device_list, (*num_drives) * sizeof(char *)); + if (*device_list) { + *device_list = realloc(*device_list, (*num_drives) * sizeof(char *)); + } else { + /* num_drives should be 0. Add assert? */ + *device_list = malloc((*num_drives) * sizeof(char *)); + } + (*device_list)[*num_drives-1] = strdup(drive); } } else { (*num_drives)++; - *device_list = realloc(*device_list, (*num_drives) * sizeof(char *)); + if (*device_list) { + *device_list = realloc(*device_list, (*num_drives) * sizeof(char *)); + } else { + *device_list = malloc((*num_drives) * sizeof(char *)); + } (*device_list)[*num_drives-1] = NULL; } } diff --git a/src/input/vcd/libcdio/_cdio_linux.c b/src/input/vcd/libcdio/_cdio_linux.c index 51afb728e..e5dd47551 100644 --- a/src/input/vcd/libcdio/_cdio_linux.c +++ b/src/input/vcd/libcdio/_cdio_linux.c @@ -1,8 +1,8 @@ /* - $Id: _cdio_linux.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_linux.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2001 Herbert Valerio Riedel - Copyright (C) 2002,2003 Rocky Bernstein + Copyright (C) 2002, 2003, 2004 Rocky Bernstein 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 @@ -27,7 +27,7 @@ # include "config.h" #endif -static const char _rcsid[] = "$Id: _cdio_linux.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_linux.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; #include @@ -110,7 +110,7 @@ cdio_is_cdrom(char *drive, char *mnttype) } /* If it does exist, verify that it's an available CD-ROM */ - cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0); + cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0); if ( cdfd >= 0 ) { if ( ioctl(cdfd, CDROMREADTOCHDR, &tochdr) != -1 ) { is_cd = true; @@ -177,10 +177,9 @@ cdio_check_mounts(const char *mtab) } if ( strcmp(mnt_type, "iso9660") == 0 ) { if (cdio_is_cdrom(mnt_dev, mnt_type) > 0) { - free(mnt_dev); free(mnt_type); endmntent(mntfp); - return strdup(mnt_dev); + return mnt_dev; } } free(mnt_dev); @@ -353,13 +352,110 @@ _read_packet_mode2_sectors (int fd, void *buf, lba_t lba, return retval; } +/*! + Reads a single mode1 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_mode1_sector (void *env, void *data, lsn_t lsn, + bool b_form2) +{ + + char buf[M2RAW_SECTOR_SIZE] = { 0, }; +#if FIXED + struct cdrom_msf *msf = (struct cdrom_msf *) &buf; + msf_t _msf; + + _img_private_t *_obj = env; + + cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf); + msf->cdmsf_min0 = from_bcd8(_msf.m); + msf->cdmsf_sec0 = from_bcd8(_msf.s); + msf->cdmsf_frame0 = from_bcd8(_msf.f); + + retry: + switch (_obj->access_mode) + { + case _AM_NONE: + cdio_error ("no way to read mode1"); + return 1; + break; + + case _AM_IOCTL: + if (ioctl (_obj->gen.fd, CDROMREADMODE1, &buf) == -1) + { + perror ("ioctl()"); + return 1; + /* exit (EXIT_FAILURE); */ + } + break; + + case _AM_READ_CD: + case _AM_READ_10: + if (_read_packet_mode2_sectors (_obj->gen.fd, buf, lsn, 1, + (_obj->access_mode == _AM_READ_10))) + { + perror ("ioctl()"); + if (_obj->access_mode == _AM_READ_CD) + { + cdio_info ("READ_CD failed; switching to READ_10 mode..."); + _obj->access_mode = _AM_READ_10; + goto retry; + } + else + { + cdio_info ("READ_10 failed; switching to ioctl(CDROMREADMODE2) mode..."); + _obj->access_mode = _AM_IOCTL; + goto retry; + } + return 1; + } + break; + } + + memcpy (data, buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE, + b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); + +#else + if (0 > cdio_generic_lseek(env, CDIO_CD_FRAMESIZE*lsn, SEEK_SET)) + return -1; + if (0 > cdio_generic_read(env, buf, CDIO_CD_FRAMESIZE)) + return -1; + memcpy (data, buf, b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); +#endif + return 0; +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ +static int +_cdio_read_mode1_sectors (void *env, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *_obj = env; + unsigned int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode1_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + /*! Reads a single mode2 sector from cd device into data starting from lsn. Returns 0 if no error. */ static int _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, - bool mode2_form2) + bool b_form2) { char buf[M2RAW_SECTOR_SIZE] = { 0, }; struct cdrom_msf *msf = (struct cdrom_msf *) &buf; @@ -412,7 +508,7 @@ _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, break; } - if (mode2_form2) + if (b_form2) memcpy (data, buf, M2RAW_SECTOR_SIZE); else memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); @@ -427,27 +523,18 @@ _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, */ static int _cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn, - bool mode2_form2, unsigned int nblocks) + bool b_form2, unsigned int nblocks) { _img_private_t *_obj = env; unsigned int i; int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; for (i = 0; i < nblocks; i++) { - if (mode2_form2) { - if ( (retval = _cdio_read_mode2_sector (_obj, - ((char *)data) - + (M2RAW_SECTOR_SIZE * i), - lsn + i, true)) ) - return retval; - } else { - char buf[M2RAW_SECTOR_SIZE] = { 0, }; - if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) ) - return retval; - - memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), - buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); - } + if ( (retval = _cdio_read_mode2_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; } return 0; } @@ -727,6 +814,7 @@ _cdio_get_mcn (void *env) { struct cdrom_mcn mcn; _img_private_t *_obj = env; + memset(&mcn, 0, sizeof(mcn)); if (ioctl(_obj->gen.fd, CDROM_GET_MCN, &mcn) != 0) return NULL; return strdup(mcn.medium_catalog_number); @@ -866,12 +954,14 @@ cdio_get_devices_linux (void) /* Now check the currently mounted CD drives */ if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) { - cdio_add_device_list(&drives, drive, &num_drives); + cdio_add_device_list(&drives, ret_drive, &num_drives); + free(ret_drive); } /* Finally check possible mountable drives in /etc/fstab */ if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) { - cdio_add_device_list(&drives, drive, &num_drives); + cdio_add_device_list(&drives, ret_drive, &num_drives); + free(ret_drive); } /* Scan the system for CD-ROM drives. @@ -980,6 +1070,8 @@ cdio_open_linux (const char *orig_source_name) .lseek = cdio_generic_lseek, .read = cdio_generic_read, .read_audio_sectors = _cdio_read_audio_sectors, + .read_mode1_sector = _cdio_read_mode1_sector, + .read_mode1_sectors = _cdio_read_mode1_sectors, .read_mode2_sector = _cdio_read_mode2_sector, .read_mode2_sectors = _cdio_read_mode2_sectors, .set_arg = _cdio_set_arg, diff --git a/src/input/vcd/libcdio/_cdio_nrg.c b/src/input/vcd/libcdio/_cdio_nrg.c deleted file mode 100644 index 4b92f105f..000000000 --- a/src/input/vcd/libcdio/_cdio_nrg.c +++ /dev/null @@ -1,938 +0,0 @@ -/* - $Id: _cdio_nrg.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ - - Copyright (C) 2001,2003 Herbert Valerio Riedel - - 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). -*/ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#ifdef HAVE_STDIO_H -#include -#endif -#ifdef HAVE_STDLIB_H -#include -#endif -#ifdef HAVE_STRING_H -#include -#endif -#ifdef HAVE_GLOB_H -#include -#endif - -#include -#include -#include -#include "cdio_assert.h" -#include "bytesex.h" -#include "ds.h" -#include "cdio_private.h" -#include "_cdio_stdio.h" - -static const char _rcsid[] = "$Id: _cdio_nrg.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; - -/* structures used */ - -/* 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 struct { - uint32_t start GNUC_PACKED; - uint32_t length GNUC_PACKED; - uint32_t type GNUC_PACKED; /* only 0x3 seen so far... - -> 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 32bit 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; - uint32_t start_lsn GNUC_PACKED; - uint64_t _unknown GNUC_PACKED; /* wtf is this for? -- always zero... */ -} _etn2_array_t; - -typedef struct { - uint8_t _unknown1 GNUC_PACKED; /* 0x41 == 'A' */ - uint8_t track GNUC_PACKED; /* binary or BCD?? */ - uint8_t index GNUC_PACKED; /* makes 0->1 transitions */ - uint8_t _unknown2 GNUC_PACKED; /* ?? */ - uint32_t lsn GNUC_PACKED; -} _cuex_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 - -/* to be converted into BE */ -#define CUEX_ID 0x43554558 -#define CUES_ID 0x43554553 -#define DAOX_ID 0x44414f58 -#define DAOI_ID 0x44414f49 -#define END1_ID 0x454e4421 -#define ETN2_ID 0x45544e32 -#define ETNF_ID 0x45544e46 -#define NER5_ID 0x4e455235 -#define NERO_ID 0x4e45524f -#define SINF_ID 0x53494e46 - -/* reader */ - -#define DEFAULT_CDIO_DEVICE "image.nrg" - -typedef struct { - int track_num; /* Probably is index+1 */ - msf_t start_msf; - lba_t start_lba; - int start_index; - int sec_count; /* Number of sectors in track. Does not - include pregap before next entry. */ - int flags; /* "DCP", "4CH", "PRE" */ - track_format_t track_format; - bool track_green; - uint16_t datasize; /* How much is in the portion we return back? */ - long int datastart; /* Offset from begining that data starts */ - uint16_t endsize; /* How much stuff at the end to skip over. This - stuff may have error correction (EDC, or ECC).*/ - uint16_t blocksize; /* total block size = start + size + end */ -} track_info_t; - -/* - 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.*/ -} _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 a hack because I don't really understnad NERO better. */ - bool is_cues; - - bool sector_2336; - track_info_t tocent[100]; /* entry info for each track */ - track_t total_tracks; /* number of tracks in image */ - track_t first_track_num; /* track number of first track */ - CdioList *mapping; /* List of track information */ - uint32_t size; -} _img_private_t; - -static bool _cdio_parse_nero_footer (_img_private_t *_obj); -static uint32_t _cdio_stat_size (void *env); - -/* Updates internal track TOC, so we can later - simulate ioctl(CDROMREADTOCENTRY). - */ -static void -_register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t sec_count, - uint64_t img_offset, uint32_t blocksize) -{ - const int track_num=_obj->total_tracks; - track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]); - _mapping_t *_map = _cdio_malloc (sizeof (_mapping_t)); - - if (!_obj->mapping) - _obj->mapping = _cdio_list_new (); - - _cdio_list_append (_obj->mapping, _map); - _map->start_lsn = start_lsn; - _map->sec_count = sec_count; - - _map->img_offset = img_offset; - - _obj->size = MAX (_obj->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 (_obj->is_cues) - this_track->datastart = img_offset + 8; - else - this_track->datastart = 8; - - this_track->sec_count = sec_count; - - _obj->total_tracks++; - - /* FIXME: These are probably not right. Probably we need to look at - "type." But this hasn't been reverse engineered yet. - */ - this_track->track_format= TRACK_FORMAT_XA; - this_track->track_green = true; - - /* cdio_debug ("map: %d +%d -> %ld", start_lsn, sec_count, img_offset); */ -} - - -/* - Disk and track information for a Nero file are located at the end - of the file. This routine extracts that information. - */ -static bool -_cdio_parse_nero_footer (_img_private_t *_obj) -{ - long unsigned int footer_start; - long unsigned int size; - char *footer_buf = NULL; - - if (_obj->size) - return 0; - - size = cdio_stream_stat (_obj->gen.data_source); - if (-1 == size) return false; - - { -PRAGMA_BEGIN_PACKED - 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; - } buf; -PRAGMA_END_PACKED - - cdio_assert (sizeof (buf) == 12); - - cdio_stream_seek (_obj->gen.data_source, size - sizeof (buf), SEEK_SET); - cdio_stream_read (_obj->gen.data_source, (void *) &buf, sizeof (buf), 1); - - if (buf.v50.ID == UINT32_TO_BE (0x4e45524f)) /* "NERO" */ - { - cdio_info ("detected v50 (32bit offsets) NRG magic"); - footer_start = uint32_to_be (buf.v50.footer_ofs); - } - else if (buf.v55.ID == UINT32_TO_BE (0x4e455235)) /* "NER5" */ - { - cdio_info ("detected v55 (64bit offsets) NRG magic"); - footer_start = uint64_from_be (buf.v55.footer_ofs); - } - else - { - cdio_warn ("Image not recognized as either v50 or v55 type NRG"); - return -1; - } - - 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 (_obj->gen.data_source, footer_start, SEEK_SET); - cdio_stream_read (_obj->gen.data_source, footer_buf, size - footer_start, 1); - } - - { - int pos = 0; - - while (pos < size - footer_start) { - _chunk_t *chunk = (void *) (footer_buf + pos); - - bool break_out = false; - - switch (UINT32_FROM_BE (chunk->id)) { - case CUES_ID: { /* "CUES" Seems to have sector size 2336 and 150 sector - pregap seems to be included at beginning of image. - */ - - - unsigned entries = UINT32_FROM_BE (chunk->len); - _cuex_array_t *_entries = (void *) chunk->data; - - cdio_assert (_obj->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); - - cdio_info ("CUES type image detected"); - - { - lsn_t lsn = UINT32_FROM_BE (_entries[0].lsn); - int idx; - - /*cdio_assert (lsn == 0?);*/ - - _obj->is_cues = true; /* HACK alert. */ - _obj->sector_2336 = true; - _obj->total_tracks = 0; - _obj->first_track_num = 1; - for (idx = 1; idx < entries-1; idx += 2) { - lsn_t sec_count; - - cdio_assert (_entries[idx].index == 0); - 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. - */ - - lsn = UINT32_FROM_BE (_entries[idx].lsn); - sec_count = UINT32_FROM_BE (_entries[idx + 1].lsn); - - _register_mapping (_obj, lsn, sec_count*2, - (lsn+CDIO_PREGAP_SECTORS) * M2RAW_SECTOR_SIZE, - M2RAW_SECTOR_SIZE); - } - } - break; - } - - case CUEX_ID: /* "CUEX" */ { - unsigned entries = UINT32_FROM_BE (chunk->len); - _cuex_array_t *_entries = (void *) chunk->data; - - cdio_assert (_obj->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); - - cdio_info ("DAO type image detected"); - - { - uint32_t lsn = UINT32_FROM_BE (_entries[0].lsn); - int idx; - - cdio_assert (lsn == 0xffffff6a); - - for (idx = 2; idx < entries; idx += 2) { - lsn_t lsn2; - - cdio_assert (_entries[idx].index == 1); - cdio_assert (_entries[idx].track != _entries[idx + 1].track); - - lsn = UINT32_FROM_BE (_entries[idx].lsn); - lsn2 = UINT32_FROM_BE (_entries[idx + 1].lsn); - - _register_mapping (_obj, lsn, lsn2 - lsn, - (lsn + CDIO_PREGAP_SECTORS)*M2RAW_SECTOR_SIZE, - M2RAW_SECTOR_SIZE); - } - } - break; - } - - case DAOI_ID: /* "DAOI" */ - cdio_debug ("DAOI tag detected..."); - break; - case DAOX_ID: /* "DAOX" */ - cdio_debug ("DAOX tag detected..."); - break; - - case NER5_ID: /* "NER5" */ - cdio_error ("unexpected nrg magic ID NER5 detected"); - return -1; - break; - - case NERO_ID: /* "NER0" */ - cdio_error ("unexpected nrg magic ID NER0 detected"); - return -1; - 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 (_obj->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"); - - _obj->sector_2336 = true; - - { - 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); - - cdio_assert (UINT32_FROM_BE (_entries[idx].type) == 3); - cdio_assert (_len % M2RAW_SECTOR_SIZE == 0); - - _len /= M2RAW_SECTOR_SIZE; - - cdio_assert (_start * M2RAW_SECTOR_SIZE == _start2); - - _start += idx * CDIO_PREGAP_SECTORS; - _register_mapping (_obj, _start, _len, _start2, M2RAW_SECTOR_SIZE); - - } - } - 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 (_obj->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"); - - _obj->sector_2336 = true; - - { - 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); - - cdio_assert (uint32_from_be (_entries[idx].type) == 3); - cdio_assert (_len % M2RAW_SECTOR_SIZE == 0); - - _len /= M2RAW_SECTOR_SIZE; - - cdio_assert (_start * M2RAW_SECTOR_SIZE == _start2); - - _start += idx * CDIO_PREGAP_SECTORS; - _register_mapping (_obj, _start, _len, _start2, M2RAW_SECTOR_SIZE); - } - } - 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; - - default: - cdio_warn ("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 _cdio_stat_size since that will lead to recursion since - we haven't fully initialized things yet. - */ - cdio_lsn_to_msf (_obj->size, &_obj->tocent[_obj->total_tracks].start_msf); - _obj->tocent[_obj->total_tracks].start_lba = cdio_lsn_to_lba(_obj->size); - _obj->tocent[_obj->total_tracks-1].sec_count = - cdio_lsn_to_lba(_obj->size - _obj->tocent[_obj->total_tracks-1].start_lba); - - return 0; -} - -/*! - Initialize image structures. - */ -static bool -_cdio_init (_img_private_t *_obj) -{ - if (_obj->gen.init) { - cdio_error ("init called more than once"); - return false; - } - - if (!(_obj->gen.data_source = cdio_stdio_new (_obj->gen.source_name))) { - cdio_error ("init failed"); - return false; - } - - _cdio_parse_nero_footer (_obj); - _obj->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 -_cdio_lseek (void *env, off_t offset, int whence) -{ - _img_private_t *_obj = env; - - /* 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; - unsigned int user_datasize; - - for (i=0; i<_obj->total_tracks; i++) { - track_info_t *this_track=&(_obj->tocent[i]); - switch (this_track->track_format) { - case TRACK_FORMAT_AUDIO: - user_datasize=CDIO_CD_FRAMESIZE_RAW; - break; - case TRACK_FORMAT_CDI: - user_datasize=CDIO_CD_FRAMESIZE; - break; - case TRACK_FORMAT_XA: - user_datasize=CDIO_CD_FRAMESIZE; - break; - default: - user_datasize=CDIO_CD_FRAMESIZE_RAW; - cdio_warn ("track %d has unknown format %d", - i+1, this_track->track_format); - } - - if ( (this_track->sec_count*user_datasize) >= offset) { - int blocks = offset / user_datasize; - int rem = offset % user_datasize; - int block_offset = blocks * (M2RAW_SECTOR_SIZE); - real_offset += block_offset + rem; - break; - } - real_offset += this_track->sec_count*(M2RAW_SECTOR_SIZE); - - offset -= this_track->sec_count*user_datasize; - } - - if (i==_obj->total_tracks) { - cdio_warn ("seeking outside range of disk image"); - return -1; - } else - real_offset += _obj->tocent[i].datastart; - return cdio_stream_seek(_obj->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 -_cdio_read (void *env, void *buf, size_t size) -{ - _img_private_t *_obj = env; - return cdio_stream_read(_obj->gen.data_source, buf, size, 1); -} - -static uint32_t -_cdio_stat_size (void *env) -{ - _img_private_t *_obj = env; - - return _obj->size; -} - -/*! - Reads a single audio sector from CD device into data starting - from LSN. Returns 0 if no error. - */ -static int -_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, - unsigned int nblocks) -{ - _img_private_t *_obj = env; - - CdioListNode *node; - - if (lsn >= _obj->size) - { - cdio_warn ("trying to read beyond image size (%lu >= %lu)", - (long unsigned int) lsn, (long unsigned int) _obj->size); - return -1; - } - - _CDIO_LIST_FOREACH (node, _obj->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 (_obj->gen.data_source, img_offset, - SEEK_SET); - if (ret!=0) return ret; - ret = cdio_stream_read (_obj->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 -_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, - bool mode2_form2) -{ - _img_private_t *_obj = env; - char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; - int blocksize = _obj->sector_2336 - ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; - - CdioListNode *node; - - if (lsn >= _obj->size) - { - cdio_warn ("trying to read beyond image size (%lu >= %lu)", - (long unsigned int) lsn, (long unsigned int) _obj->size); - return -1; - } - - _CDIO_LIST_FOREACH (node, _obj->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) * blocksize; - - ret = cdio_stream_seek (_obj->gen.data_source, img_offset, - SEEK_SET); - if (ret!=0) return ret; - ret = cdio_stream_read (_obj->gen.data_source, _obj->sector_2336 - ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE) - : buf, - blocksize, 1); - if (ret==0) return ret; - break; - } - } - - if (!node) - cdio_warn ("reading into pre gap (lsn %lu)", (long unsigned int) lsn); - - if (mode2_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 -_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn, - bool mode2_form2, unsigned nblocks) -{ - _img_private_t *_obj = env; - int i; - int retval; - - for (i = 0; i < nblocks; i++) { - if (mode2_form2) { - if ( (retval = _cdio_read_mode2_sector (_obj, - ((char *)data) + (M2RAW_SECTOR_SIZE * i), - lsn + i, true)) ) - return retval; - } else { - char buf[M2RAW_SECTOR_SIZE] = { 0, }; - if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) ) - return retval; - - memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), - buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); - } - } - return 0; -} - -/*! - Set the device to use in I/O operations. -*/ -static int -_cdio_set_arg (void *env, const char key[], const char value[]) -{ - _img_private_t *_obj = env; - - if (!strcmp (key, "source")) - { - free (_obj->gen.source_name); - - if (!value) - return -2; - - _obj->gen.source_name = strdup (value); - } - else - return -1; - - return 0; -} - -/*! - Return the value associated with the key "arg". -*/ -static const char * -_cdio_get_arg (void *env, const char key[]) -{ - _img_private_t *_obj = env; - - if (!strcmp (key, "source")) { - return _obj->gen.source_name; - } - return NULL; -} - -/*! - 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; ifirst_track_num; -} - -/*! - Return the number of tracks. We fake it an just say there's - one big track. -*/ -static track_t -_cdio_get_num_tracks(void *env) -{ - _img_private_t *_obj = env; - - return _obj->total_tracks; -} - -/*! - Return the number of tracks in the current medium. - CDIO_INVALID_TRACK is returned on error. -*/ -static track_format_t -_cdio_get_track_format(void *env, track_t track_num) -{ - _img_private_t *_obj = env; - - if (track_num > _obj->total_tracks || track_num == 0) - return TRACK_FORMAT_ERROR; - - return _obj->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 -_cdio_get_track_green(void *env, track_t track_num) -{ - _img_private_t *_obj = env; - - if (track_num > _obj->total_tracks || track_num == 0) - return false; - - return _obj->tocent[track_num-1].track_green; -} - -/*! - Return the starting MSF (minutes/secs/frames) for the track number - track_num in obj. Tracks numbers start at 1. - The "leadout" track is specified either by - using track_num LEADOUT_TRACK or the total tracks+1. -*/ -static bool -_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf) -{ - _img_private_t *_obj = env; - - if (NULL == msf) return 1; - - if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; - - if (track_num <= _obj->total_tracks+1 && track_num != 0) { - *msf = _obj->tocent[track_num-1].start_msf; - return true; - } else - return false; -} - -CdIo * -cdio_open_nrg (const char *source_name) -{ - CdIo *ret; - _img_private_t *_data; - - cdio_funcs _funcs = { - .eject_media = cdio_generic_bogus_eject_media, - .free = cdio_generic_stream_free, - .get_arg = _cdio_get_arg, - .get_devices = cdio_get_devices_nrg, - .get_default_device = cdio_get_default_device_nrg, - .get_first_track_num= _cdio_get_first_track_num, - .get_num_tracks = _cdio_get_num_tracks, - .get_track_format = _cdio_get_track_format, - .get_track_green = _cdio_get_track_green, - .get_track_lba = NULL, /* Will use generic routine via msf */ - .get_track_msf = _cdio_get_track_msf, - .lseek = _cdio_lseek, - .read = _cdio_read, - .read_audio_sectors = _cdio_read_audio_sectors, - .read_mode2_sector = _cdio_read_mode2_sector, - .read_mode2_sectors = _cdio_read_mode2_sectors, - .set_arg = _cdio_set_arg, - .stat_size = _cdio_stat_size, - }; - - _data = _cdio_malloc (sizeof (_img_private_t)); - _data->gen.init = false; - - - _data->total_tracks = 0; - _data->first_track_num= 1; - _data->sector_2336 = false; /* FIXME: remove sector_2336 */ - _data->is_cues = false; /* FIXME: remove is_cues. */ - - - _cdio_set_arg(_data, "source", (NULL == source_name) - ? DEFAULT_CDIO_DEVICE: source_name); - - ret = cdio_new (_data, &_funcs); - if (ret == NULL) return NULL; - - if (_cdio_init(_data)) - return ret; - else { - cdio_generic_stream_free (_data); - return NULL; - } - -} - -bool -cdio_have_nrg (void) -{ - return true; -} diff --git a/src/input/vcd/libcdio/_cdio_osx.c b/src/input/vcd/libcdio/_cdio_osx.c index b02151f30..887eb00bf 100644 --- a/src/input/vcd/libcdio/_cdio_osx.c +++ b/src/input/vcd/libcdio/_cdio_osx.c @@ -1,12 +1,14 @@ /* - $Id: _cdio_osx.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_osx.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ - Copyright (C) 2003 Rocky Bernstein from vcdimager code + Copyright (C) 2003, 2004 Rocky Bernstein + from vcdimager code: Copyright (C) 2001 Herbert Valerio Riedel and VideoLAN code Copyright (C) 1998-2001 VideoLAN Authors: Johan Bilien Gildas Bazin Jon Lech Johansen + Derk-Jan Hartman 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 @@ -31,16 +33,13 @@ # include "config.h" #endif -static const char _rcsid[] = "$Id: _cdio_osx.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_osx.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; #include #include #include "cdio_assert.h" #include "cdio_private.h" -/* Is this the right default? */ -#define DEFAULT_CDIO_DEVICE "/dev/rdisk2" - #include #ifdef HAVE_DARWIN_CDROM @@ -55,9 +54,14 @@ static const char _rcsid[] = "$Id: _cdio_osx.c,v 1.1 2003/10/13 11:47:11 f1rmb E #include #include +#include #include +#include +#include #include +#include #include +#include #include #include @@ -124,61 +128,71 @@ _cdio_getNumberOfTracks( CDTOC *pTOC, int i_descriptors ) Returns 0 if no error. */ static int -_cdio_read_mode2_form2_sectors (int device_handle, void *data, lsn_t lsn, - bool mode2_form2, unsigned int nblocks) +_cdio_read_mode1_sectors (void *env, void *data, lsn_t lsn, + bool is_form2, unsigned int nblocks) { + _img_private_t *_obj = env; dk_cd_read_t cd_read; memset( &cd_read, 0, sizeof(cd_read) ); - cd_read.offset = lsn * CDIO_CD_FRAMESIZE_RAW; - cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | - kCDSectorAreaSubHeader | kCDSectorAreaUser | - kCDSectorAreaAuxiliary; - cd_read.sectorType = kCDSectorTypeUnknown; + cd_read.sectorArea = kCDSectorAreaUser; + cd_read.buffer = data; + cd_read.sectorType = kCDSectorTypeMode1; - cd_read.buffer = data; - cd_read.bufferLength = CDIO_CD_FRAMESIZE_RAW * nblocks; + if (is_form2) { + cd_read.offset = lsn * kCDSectorSizeMode2; + cd_read.bufferLength = kCDSectorSizeMode2 * nblocks; + } else { + cd_read.offset = lsn * kCDSectorSizeMode1; + cd_read.bufferLength = kCDSectorSizeMode1 * nblocks; + } - if( ioctl( device_handle, DKIOCCDREAD, &cd_read ) == -1 ) - { - cdio_error( "could not read block %d", lsn ); - return -1; - } + if( ioctl( _obj->gen.fd, DKIOCCDREAD, &cd_read ) == -1 ) + { + cdio_error( "could not read block %d, %s", lsn, strerror(errno) ); + return -1; + } return 0; } /*! - Reads nblocks of mode2 sectors from cd device into data starting + Reads nblocks of mode2 form2 sectors from cd device into data starting from lsn. Returns 0 if no error. */ static int _cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn, - bool mode2_form2, unsigned int nblocks) + bool is_form2, unsigned int nblocks) { _img_private_t *_obj = env; - int i; - int retval; - - if (mode2_form2) { - return _cdio_read_mode2_form2_sectors(_obj->gen.fd, data, lsn, - mode2_form2, nblocks); + dk_cd_read_t cd_read; + + memset( &cd_read, 0, sizeof(cd_read) ); + + cd_read.sectorArea = kCDSectorAreaUser; + cd_read.buffer = data; + + if (is_form2) { + cd_read.offset = lsn * kCDSectorSizeMode2Form2; + cd_read.sectorType = kCDSectorTypeMode2Form2; + cd_read.bufferLength = kCDSectorSizeMode2Form2 * nblocks; + } else { + cd_read.offset = lsn * kCDSectorSizeMode2Form1; + cd_read.sectorType = kCDSectorTypeMode2Form1; + cd_read.bufferLength = kCDSectorSizeMode2Form1 * nblocks; } - for (i = 0; i < nblocks; i++) { - char buf[M2RAW_SECTOR_SIZE] = { 0, }; - retval = _cdio_read_mode2_form2_sectors (_obj->gen.fd, buf, lsn + i, - mode2_form2, 1); - if ( retval ) return retval; - - memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), - buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); + if( ioctl( _obj->gen.fd, DKIOCCDREAD, &cd_read ) == -1 ) + { + cdio_error( "could not read block %d, %s", lsn, strerror(errno) ); + return -1; } return 0; } + /*! Reads a single audio sector from CD device into data starting from lsn. Returns 0 if no error. @@ -187,7 +201,35 @@ static int _cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, unsigned int nblocks) { - return _cdio_read_mode2_sectors(env, data, lsn, true, nblocks); + _img_private_t *_obj = env; + dk_cd_read_t cd_read; + + memset( &cd_read, 0, sizeof(cd_read) ); + + cd_read.offset = lsn * kCDSectorSizeCDDA; + cd_read.sectorArea = kCDSectorAreaUser; + cd_read.sectorType = kCDSectorTypeCDDA; + + cd_read.buffer = data; + cd_read.bufferLength = kCDSectorSizeCDDA * nblocks; + + if( ioctl( _obj->gen.fd, DKIOCCDREAD, &cd_read ) == -1 ) + { + cdio_error( "could not read block %d", lsn ); + return -1; + } + return 0; +} + +/*! + Reads a single mode2 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_mode1_sector (void *env, void *data, lsn_t lsn, + bool is_form2) +{ + return _cdio_read_mode1_sectors(env, data, lsn, is_form2, 1); } /*! @@ -196,9 +238,9 @@ _cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, */ static int _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, - bool mode2_form2) + bool is_form2) { - return _cdio_read_mode2_sectors(env, data, lsn, mode2_form2, 1); + return _cdio_read_mode2_sectors(env, data, lsn, is_form2, 1); } /*! @@ -534,23 +576,24 @@ _cdio_get_first_track_num(void *env) } /*! - Return the media catalog number MCN. - */ + Return the media catalog number MCN. + */ static char * _cdio_get_mcn (void *env) { - _img_private_t *_obj = env; - dk_cd_read_mcn_t cd_read; + _img_private_t *_obj = env; + dk_cd_read_mcn_t cd_read; - memset( &cd_read, 0, sizeof(cd_read) ); + memset( &cd_read, 0, sizeof(cd_read) ); - if( ioctl( _obj->gen.fd, DKIOCCDREADMCN, &cd_read ) < 0 ) - { - cdio_error( "could not read MCN, %s", strerror(errno) ); - return -1; - } - return strdup((char*)cd_read.mcn); + if( ioctl( _obj->gen.fd, DKIOCCDREADMCN, &cd_read ) < 0 ) + { + cdio_error( "could not read MCN, %s", strerror(errno) ); + return NULL; + } + return strdup((char*)cd_read.mcn); } + /*! Return the number of tracks in the current medium. CDIO_INVALID_TRACK is returned on error. @@ -572,27 +615,31 @@ static track_format_t _cdio_get_track_format(void *env, track_t track_num) { _img_private_t *_obj = env; + CDTrackInfo a_track; if (!_obj->toc_init) _cdio_read_toc (_obj) ; if (track_num > TOTAL_TRACKS || track_num == 0) return TRACK_FORMAT_ERROR; + + dk_cd_read_track_info_t cd_read; + memset( &cd_read, 0, sizeof(cd_read) ); -#if 0 - if (_obj->tocent[track_num-1].entry.control & CDROM_DATA_TRACK) { - if (_obj->tocent[track_num-1].cdte_format == 0x10) - return TRACK_FORMAT_CDI; - else if (_obj->tocent[track_num-1].cdte_format == 0x20) - return TRACK_FORMAT_XA; - else - return TRACK_FORMAT_DATA; - } else - return TRACK_FORMAT_AUDIO; -#else - /* FIXME! Figure out how to do. */ - return TRACK_FORMAT_DATA; -#endif + cd_read.address = track_num; + cd_read.addressType = kCDTrackInfoAddressTypeTrackNumber; + + cd_read.buffer = &a_track; + cd_read.bufferLength = sizeof(CDTrackInfo); + if( ioctl( _obj->gen.fd, DKIOCCDREADTRACKINFO, &cd_read ) == -1 ) + { + cdio_error( "could not read trackinfo for track %d", track_num ); + return -1; + } + + cdio_warn( "trackinfo trackMode: %x dataMode: %x", a_track.trackMode, a_track.dataMode ); + + return TRACK_FORMAT_AUDIO; } /*! @@ -798,12 +845,13 @@ cdio_get_default_device_osx(void) ones to set that up. */ CdIo * -cdio_open_osx (const char *source_name) +cdio_open_osx (const char *orig_source_name) { #ifdef HAVE_DARWIN_CDROM CdIo *ret; _img_private_t *_data; + char *source_name; cdio_funcs _funcs = { .eject_media = _cdio_eject_media, @@ -812,7 +860,7 @@ cdio_open_osx (const char *source_name) .get_default_device = cdio_get_default_device_osx, .get_devices = cdio_get_devices_osx, .get_first_track_num= _cdio_get_first_track_num, - .get_mcn = _cdio_get_mcn, + .get_mcn = _cdio_get_mcn, .get_num_tracks = _cdio_get_num_tracks, .get_track_format = _cdio_get_track_format, .get_track_green = _cdio_get_track_green, @@ -821,6 +869,8 @@ cdio_open_osx (const char *source_name) .lseek = cdio_generic_lseek, .read = cdio_generic_read, .read_audio_sectors = _cdio_read_audio_sectors, + .read_mode1_sector = _cdio_read_mode1_sector, + .read_mode1_sectors = _cdio_read_mode1_sectors, .read_mode2_sector = _cdio_read_mode2_sector, .read_mode2_sectors = _cdio_read_mode2_sectors, .set_arg = _cdio_set_arg, @@ -832,8 +882,13 @@ cdio_open_osx (const char *source_name) _data->gen.init = false; _data->gen.fd = -1; - _cdio_set_arg(_data, "source", (NULL == source_name) - ? DEFAULT_CDIO_DEVICE: source_name); + if (NULL == orig_source_name) { + source_name=cdio_get_default_device_linux(); + if (NULL == source_name) return NULL; + _cdio_set_arg(_data, "source", source_name); + free(source_name); + } else + _cdio_set_arg(_data, "source", orig_source_name); ret = cdio_new (_data, &_funcs); if (ret == NULL) return NULL; @@ -860,5 +915,3 @@ cdio_have_osx (void) return false; #endif /* HAVE_DARWIN_CDROM */ } - - diff --git a/src/input/vcd/libcdio/_cdio_stdio.c b/src/input/vcd/libcdio/_cdio_stdio.c index b8d8daa67..0083b2194 100644 --- a/src/input/vcd/libcdio/_cdio_stdio.c +++ b/src/input/vcd/libcdio/_cdio_stdio.c @@ -1,8 +1,8 @@ /* - $Id: _cdio_stdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_stdio.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein 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 @@ -35,7 +35,7 @@ #include "_cdio_stream.h" #include "_cdio_stdio.h" -static const char _rcsid[] = "$Id: _cdio_stdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_stdio.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; #define CDIO_STDIO_BUFSIZE (128*1024) @@ -145,7 +145,7 @@ _stdio_stat(void *user_data) must use feof(3) and ferror(3) to determine which occurred. */ static long -_stdio_read(void *user_data, void *buf, long count) +_stdio_read(void *user_data, void *buf, long int count) { _UserData *const ud = user_data; long read; @@ -171,6 +171,15 @@ _stdio_read(void *user_data, void *buf, long count) return read; } +/*! + Deallocate resources assocaited with obj. After this obj is unusable. +*/ +void +cdio_stdio_destroy(CdioDataSource *obj) +{ + cdio_stream_destroy(obj); +} + CdioDataSource* cdio_stdio_new(const char pathname[]) { @@ -181,7 +190,8 @@ cdio_stdio_new(const char pathname[]) if (stat (pathname, &statbuf) == -1) { - cdio_error ("could not stat() file `%s': %s", pathname, strerror (errno)); + cdio_warn ("could not retrieve file info for `%s': %s", + pathname, strerror (errno)); return NULL; } diff --git a/src/input/vcd/libcdio/_cdio_stdio.h b/src/input/vcd/libcdio/_cdio_stdio.h index 013dff58b..f5e79c41c 100644 --- a/src/input/vcd/libcdio/_cdio_stdio.h +++ b/src/input/vcd/libcdio/_cdio_stdio.h @@ -1,5 +1,5 @@ /* - $Id: _cdio_stdio.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_stdio.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel Copyright (C) 2003 Rocky Bernstein @@ -25,8 +25,20 @@ #include "_cdio_stream.h" -CdioDataSource* -cdio_stdio_new(const char pathname[]); +/*! + Initialize a new stdio stream reading from pathname. + A pointer to the stream is returned or NULL if there was an error. + + cdio_stream_free should be called on the returned value when you + don't need the stream any more. No other finalization is needed. + */ +CdioDataSource* cdio_stdio_new(const char pathname[]); + +/*! + Deallocate resources assocaited with obj. After this obj is unusable. +*/ +void cdio_stdio_destroy(CdioDataSource *obj); + #endif /* __CDIO_STREAM_STDIO_H__ */ diff --git a/src/input/vcd/libcdio/_cdio_stream.c b/src/input/vcd/libcdio/_cdio_stream.c index ee2f2b086..fc1f7fce7 100644 --- a/src/input/vcd/libcdio/_cdio_stream.c +++ b/src/input/vcd/libcdio/_cdio_stream.c @@ -1,7 +1,7 @@ /* - $Id: _cdio_stream.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_stream.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ - Copyright (C) 2000 Herbert Valerio Riedel + Copyright (C) 2000, 2004 Herbert Valerio Riedel 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 @@ -34,7 +34,7 @@ #include #include "_cdio_stream.h" -static const char _rcsid[] = "$Id: _cdio_stream.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_stream.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; /* * DataSource implementations @@ -186,6 +186,8 @@ cdio_stream_destroy(CdioDataSource* obj) cdio_stream_close(obj); obj->op.free(obj->user_data); + + free(obj); } diff --git a/src/input/vcd/libcdio/_cdio_stream.h b/src/input/vcd/libcdio/_cdio_stream.h index 562c18bf8..ffbb4098e 100644 --- a/src/input/vcd/libcdio/_cdio_stream.h +++ b/src/input/vcd/libcdio/_cdio_stream.h @@ -1,8 +1,8 @@ /* - $Id: _cdio_stream.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_stream.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein 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 @@ -96,14 +96,17 @@ extern "C" { Otherwise, -1 is returned and the global variable errno is set to indi- cate the error. */ - long cdio_stream_seek(CdioDataSource* obj, long offset, int whence); + long int cdio_stream_seek(CdioDataSource* obj, long offset, int whence); /*! Return whatever size of stream reports, I guess unit size is bytes. On error return -1; */ - long cdio_stream_stat(CdioDataSource* obj); + long int cdio_stream_stat(CdioDataSource* obj); + /*! + Deallocate resources assocaited with obj. After this obj is unusable. + */ void cdio_stream_destroy(CdioDataSource* obj); void cdio_stream_close(CdioDataSource* obj); diff --git a/src/input/vcd/libcdio/_cdio_sunos.c b/src/input/vcd/libcdio/_cdio_sunos.c index e7faf57cd..795ba6184 100644 --- a/src/input/vcd/libcdio/_cdio_sunos.c +++ b/src/input/vcd/libcdio/_cdio_sunos.c @@ -1,8 +1,8 @@ /* - $Id: _cdio_sunos.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: _cdio_sunos.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2001 Herbert Valerio Riedel - Copyright (C) 2002,2003 Rocky Bernstein + Copyright (C) 2002, 2003, 2004 Rocky Bernstein 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 @@ -30,7 +30,6 @@ #include #endif - #include #include #include @@ -42,7 +41,7 @@ #ifdef HAVE_SOLARIS_CDROM -static const char _rcsid[] = "$Id: _cdio_sunos.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: _cdio_sunos.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; #include #include @@ -98,80 +97,35 @@ static bool _cdio_init (_img_private_t *_obj) { - struct dk_cinfo cinfo; - if (!cdio_generic_init(_obj)) return false; - /* - * CDROMCDXA/CDROMREADMODE2 are broken on IDE/ATAPI devices. - * Try to send MMC3 SCSI commands via the uscsi interface on - * ATAPI devices. - */ - if ( ioctl(_obj->gen.fd, DKIOCINFO, &cinfo) == 0 - && ((strcmp(cinfo.dki_cname, "ide") == 0) - || (strncmp(cinfo.dki_cname, "pci", 3) == 0)) ) { - _obj->access_mode = _AM_SUN_CTRL_ATAPI; - } else { - _obj->access_mode = _AM_SUN_CTRL_SCSI; - } + _obj->access_mode = _AM_SUN_CTRL_SCSI; return true; } -static int -_cdio_mmc_read_sectors (int fd, void *buf, lsn_t lsn, int sector_type, - unsigned int nblocks) -{ - struct uscsi_cmd sc; - union scsi_cdb cdb; - int sub_channel = 0; - - memset(&cdb, 0, sizeof(cdb)); - memset(&sc, 0, sizeof(sc)); - - cdb.scc_cmd = CDIO_MMC_GPCMD_READ_CD; - CDIO_MMC_SET_READ_TYPE(cdb.cdb_opaque, sector_type); - CDIO_MMC_SET_READ_LBA(cdb.cdb_opaque, lsn); - CDIO_MMC_SET_READ_LENGTH(cdb.cdb_opaque, nblocks); - CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(cdb.cdb_opaque, - CDIO_MMC_MCSB_ALL_HEADERS); - cdb.cdb_opaque[10] = sub_channel; - - sc.uscsi_cdb = (caddr_t)&cdb; - sc.uscsi_cdblen = 12; - sc.uscsi_bufaddr = (caddr_t) buf; - sc.uscsi_buflen = CDIO_CD_FRAMESIZE_RAW; - sc.uscsi_flags = USCSI_ISOLATE | USCSI_READ; - sc.uscsi_timeout = 20; - if (ioctl(fd, USCSICMD, &sc)) { - perror("USCSICMD: READ CD"); - return 1; - } - if (sc.uscsi_status) { - cdio_error("SCSI command failed with status %d\n", - sc.uscsi_status); - } - - return sc.uscsi_status; -} - /*! - Reads a single mode2 sector from cd device into data starting from lsn. + Reads audio sectors from CD device into data starting from lsn. Returns 0 if no error. - */ + + May have to check size of nblocks. There may be a limit that + can be read in one go, e.g. 25 blocks. +*/ + static int -_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, - bool mode2_form2) +_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, + unsigned int nblocks) { - char buf[M2RAW_SECTOR_SIZE] = { 0, }; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; struct cdrom_msf *msf = (struct cdrom_msf *) &buf; msf_t _msf; + struct cdrom_cdda cdda; _img_private_t *_obj = env; cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf); - msf->cdmsf_min0 = from_bcd8(_msf.m); - msf->cdmsf_sec0 = from_bcd8(_msf.s); + msf->cdmsf_min0 = from_bcd8(_msf.m); + msf->cdmsf_sec0 = from_bcd8(_msf.s); msf->cdmsf_frame0 = from_bcd8(_msf.f); if (_obj->gen.ioctls_debugged == 75) @@ -184,58 +138,89 @@ _cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, || (_obj->gen.ioctls_debugged < (30 * 75) && _obj->gen.ioctls_debugged % 75 == 0) || _obj->gen.ioctls_debugged % (30 * 75) == 0) - cdio_debug ("reading %2.2d:%2.2d:%2.2d", - msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0); + cdio_debug ("reading %d", lsn); _obj->gen.ioctls_debugged++; - switch (_obj->access_mode) - { - case _AM_NONE: - cdio_error ("No way to read CD mode2."); - return 1; - break; - - case _AM_SUN_CTRL_SCSI: - if (ioctl (_obj->gen.fd, CDROMREADMODE2, buf) == -1) { - perror ("ioctl(..,CDROMREADMODE2,..)"); + cdda.cdda_addr = lsn; + cdda.cdda_length = nblocks; + cdda.cdda_data = (caddr_t) data; + if (ioctl (_obj->gen.fd, CDROMCDDA, &cdda) == -1) { + perror ("ioctl(..,CDROMCDDA,..)"); return 1; /* exit (EXIT_FAILURE); */ - } - break; - - case _AM_SUN_CTRL_ATAPI: - { - if (_cdio_mmc_read_sectors(_obj->gen.fd, data, lsn, - CDIO_MMC_READ_TYPE_MODE2, 1)) { - return 1; - } - break; - } - } - - if (mode2_form2) - memcpy (data, buf, M2RAW_SECTOR_SIZE); - else - memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); + } + memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW); return 0; } /*! - Reads single audio sectors from CD device into data starting from lsn. + Reads a single mode1 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_mode1_sector (void *env, void *data, lsn_t lsn, + bool b_form2) +{ + + char buf[M2RAW_SECTOR_SIZE] = { 0, }; +#if FIXED + do something here. +#else + if (0 > cdio_generic_lseek(env, CDIO_CD_FRAMESIZE*lsn, SEEK_SET)) + return -1; + if (0 > cdio_generic_read(env, buf, CDIO_CD_FRAMESIZE)) + return -1; + memcpy (data, buf, b_form2 ? M2RAW_SECTOR_SIZE: CDIO_CD_FRAMESIZE); +#endif + return 0; +} + +/*! + Reads nblocks of mode2 sectors from cd device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_mode1_sectors (void *env, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *_obj = env; + unsigned int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; - May have to check size of nblocks. There may be a limit that - can be read in one go, e.g. 25 blocks. -*/ + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode1_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} +/*! + Reads a single mode2 sector from cd device into data starting from lsn. + Returns 0 if no error. + */ static int -_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, - unsigned int nblocks) +_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, + bool b_form2) { + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + struct cdrom_msf *msf = (struct cdrom_msf *) &buf; + msf_t _msf; + int offset = 0; + struct cdrom_cdxa cd_read; + _img_private_t *_obj = env; + cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf); + msf->cdmsf_min0 = from_bcd8(_msf.m); + msf->cdmsf_sec0 = from_bcd8(_msf.s); + msf->cdmsf_frame0 = from_bcd8(_msf.f); + if (_obj->gen.ioctls_debugged == 75) cdio_debug ("only displaying every 75th ioctl from now on"); @@ -246,40 +231,29 @@ _cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, || (_obj->gen.ioctls_debugged < (30 * 75) && _obj->gen.ioctls_debugged % 75 == 0) || _obj->gen.ioctls_debugged % (30 * 75) == 0) - cdio_debug ("reading %d", lsn); + cdio_debug ("reading %2.2d:%2.2d:%2.2d", + msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0); _obj->gen.ioctls_debugged++; - switch (_obj->access_mode) - { - case _AM_NONE: - cdio_error ("No way to read CD audio"); - return 1; - break; - - case _AM_SUN_CTRL_SCSI: - { - struct cdrom_cdda cdda; - cdda.cdda_addr = lsn; - cdda.cdda_length = nblocks; - cdda.cdda_data = (caddr_t) data; - if (ioctl (_obj->gen.fd, CDROMCDDA, &cdda) == -1) { - perror ("ioctl(..,CDROMCDDA,..)"); - return 1; - /* exit (EXIT_FAILURE); */ - } - } - break; - - case _AM_SUN_CTRL_ATAPI: - { - if (_cdio_mmc_read_sectors(_obj->gen.fd, data, lsn, - CDIO_MMC_READ_TYPE_CDDA, nblocks)) { - return 1; - } - break; - } - } + /* Using CDROMXA ioctl will actually use the same uscsi command + * as ATAPI, except we don't need to be root + */ + offset = CDIO_CD_XA_SYNC_HEADER; + cd_read.cdxa_addr = lsn; + cd_read.cdxa_data = buf; + cd_read.cdxa_length = 1; + cd_read.cdxa_format = CDROM_XA_SECTOR_DATA; + if (ioctl (_obj->gen.fd, CDROMCDXA, &cd_read) == -1) { + perror ("ioctl(..,CDROMCDXA,..)"); + return 1; + /* exit (EXIT_FAILURE); */ + } + + if (b_form2) + memcpy (data, buf + (offset-CDIO_CD_SUBHEADER_SIZE), M2RAW_SECTOR_SIZE); + else + memcpy (((char *)data), buf + offset, CDIO_CD_FRAMESIZE); return 0; } @@ -290,31 +264,24 @@ _cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, Returns 0 if no error. */ static int -_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn, - bool mode2_form2, unsigned int nblocks) +_cdio_read_mode2_sectors (void *env, void *data, lsn_t lsn, + bool b_form2, unsigned int nblocks) { _img_private_t *_obj = env; unsigned int i; int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; for (i = 0; i < nblocks; i++) { - if (mode2_form2) { - if ( (retval = _cdio_read_mode2_sector (_obj, - ((char *)data) + (M2RAW_SECTOR_SIZE * i), - lsn + i, true)) ) - return retval; - } else { - char buf[M2RAW_SECTOR_SIZE] = { 0, }; - if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) ) - return retval; - - memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), - buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); - } + if ( (retval = _cdio_read_mode2_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; } return 0; } + /*! Return the size of the CD in logical block address (LBA) units. */ @@ -364,7 +331,7 @@ _cdio_set_arg (void *env, const char key[], const char value[]) else if (!strcmp (key, "access-mode")) { if (!strcmp(value, "ATAPI")) - _obj->access_mode = _AM_SUN_CTRL_ATAPI; + _obj->access_mode = _AM_SUN_CTRL_SCSI; /* force ATAPI to be SCSI */ else if (!strcmp(value, "SCSI")) _obj->access_mode = _AM_SUN_CTRL_SCSI; else @@ -686,6 +653,7 @@ cdio_open_solaris (const char *source_name) .get_devices = cdio_get_devices_solaris, .get_default_device = cdio_get_default_device_solaris, .get_first_track_num= _cdio_get_first_track_num, + .get_mcn = NULL, .get_num_tracks = _cdio_get_num_tracks, .get_track_format = _cdio_get_track_format, .get_track_green = _cdio_get_track_green, @@ -694,6 +662,8 @@ cdio_open_solaris (const char *source_name) .lseek = cdio_generic_lseek, .read = cdio_generic_read, .read_audio_sectors = _cdio_read_audio_sectors, + .read_mode1_sector = _cdio_read_mode1_sector, + .read_mode1_sectors = _cdio_read_mode1_sectors, .read_mode2_sector = _cdio_read_mode2_sector, .read_mode2_sectors = _cdio_read_mode2_sectors, .stat_size = _cdio_stat_size, diff --git a/src/input/vcd/libcdio/_cdio_win32.c b/src/input/vcd/libcdio/_cdio_win32.c deleted file mode 100644 index 418beb86c..000000000 --- a/src/input/vcd/libcdio/_cdio_win32.c +++ /dev/null @@ -1,1168 +0,0 @@ -/* - $Id: _cdio_win32.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ - - Copyright (C) 2003 Rocky Bernstein - - 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 file contains Win32-specific code and implements low-level - control of the CD drive. Inspired by vlc's cdrom.h code -*/ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -static const char _rcsid[] = "$Id: _cdio_win32.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; - -#include -#include -#include -#include "cdio_assert.h" -#include "cdio_private.h" -#include "scsi_mmc.h" - -/* LBA = msf.frame + 75 * ( msf.second - 2 + 60 * msf.minute ) */ -#define MSF_TO_LBA2(min, sec, frame) ((int)frame + 75 * (sec -2 + 60 * min)) - -#include - -#ifdef HAVE_WIN32_CDROM - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -/* Win32 DeviceIoControl specifics */ -#ifndef MAXIMUM_NUMBER_TRACKS -# define MAXIMUM_NUMBER_TRACKS 100 -#endif -typedef struct _TRACK_DATA { - UCHAR Reserved; - UCHAR Control : 4; - UCHAR Adr : 4; - UCHAR TrackNumber; - UCHAR Reserved1; - UCHAR Address[4]; -} TRACK_DATA, *PTRACK_DATA; -typedef struct _CDROM_TOC { - UCHAR Length[2]; - UCHAR FirstTrack; - UCHAR LastTrack; - TRACK_DATA TrackData[MAXIMUM_NUMBER_TRACKS]; -} CDROM_TOC, *PCDROM_TOC; -typedef enum _TRACK_MODE_TYPE { - YellowMode2, - XAForm2, - CDDA -} TRACK_MODE_TYPE, *PTRACK_MODE_TYPE; -typedef struct __RAW_READ_INFO { - LARGE_INTEGER DiskOffset; - ULONG SectorCount; - TRACK_MODE_TYPE TrackMode; -} RAW_READ_INFO, *PRAW_READ_INFO; - -#ifndef IOCTL_CDROM_BASE -# define IOCTL_CDROM_BASE FILE_DEVICE_CD_ROM -#endif -#ifndef IOCTL_CDROM_READ_TOC -# define IOCTL_CDROM_READ_TOC CTL_CODE(IOCTL_CDROM_BASE, 0x0000, \ - METHOD_BUFFERED, FILE_READ_ACCESS) -#endif -#ifndef IOCTL_CDROM_RAW_READ -#define IOCTL_CDROM_RAW_READ CTL_CODE(IOCTL_CDROM_BASE, 0x000F, \ - METHOD_OUT_DIRECT, FILE_READ_ACCESS) -#endif - -/* Win32 aspi specific */ -#define WIN_NT ( GetVersion() < 0x80000000 ) -#define ASPI_HAID 0 -#define ASPI_TARGET 0 -#define DTYPE_CDROM 0x05 - -#define SENSE_LEN 0x0E -#define SC_GET_DEV_TYPE 0x01 -#define SC_EXEC_SCSI_CMD 0x02 -#define SC_GET_DISK_INFO 0x06 -#define SS_COMP 0x01 -#define SS_PENDING 0x00 -#define SS_NO_ADAPTERS 0xE8 -#define SRB_DIR_IN 0x08 -#define SRB_DIR_OUT 0x10 -#define SRB_EVENT_NOTIFY 0x40 - -#define SECTOR_TYPE_MODE2 0x14 -#define READ_CD_USERDATA_MODE2 0x10 - -#define READ_TOC 0x43 -#define READ_TOC_FORMAT_TOC 0x0 - -#pragma pack(1) - -struct SRB_GetDiskInfo -{ - unsigned char SRB_Cmd; - unsigned char SRB_Status; - unsigned char SRB_HaId; - unsigned char SRB_Flags; - unsigned long SRB_Hdr_Rsvd; - unsigned char SRB_Target; - unsigned char SRB_Lun; - unsigned char SRB_DriveFlags; - unsigned char SRB_Int13HDriveInfo; - unsigned char SRB_Heads; - unsigned char SRB_Sectors; - unsigned char SRB_Rsvd1[22]; -}; - -struct SRB_GDEVBlock -{ - unsigned char SRB_Cmd; - unsigned char SRB_Status; - unsigned char SRB_HaId; - unsigned char SRB_Flags; - unsigned long SRB_Hdr_Rsvd; - unsigned char SRB_Target; - unsigned char SRB_Lun; - unsigned char SRB_DeviceType; - unsigned char SRB_Rsvd1; -}; - -struct SRB_ExecSCSICmd -{ - unsigned char SRB_Cmd; - unsigned char SRB_Status; - unsigned char SRB_HaId; - unsigned char SRB_Flags; - unsigned long SRB_Hdr_Rsvd; - unsigned char SRB_Target; - unsigned char SRB_Lun; - unsigned short SRB_Rsvd1; - unsigned long SRB_BufLen; - unsigned char *SRB_BufPointer; - unsigned char SRB_SenseLen; - unsigned char SRB_CDBLen; - unsigned char SRB_HaStat; - unsigned char SRB_TargStat; - unsigned long *SRB_PostProc; - unsigned char SRB_Rsvd2[20]; - unsigned char CDBByte[16]; - unsigned char SenseArea[SENSE_LEN+2]; -}; - -#pragma pack() - -typedef struct { - lsn_t start_lsn; -} track_info_t; - -typedef struct { - /* Things common to all drivers like this. - This must be first. */ - generic_img_private_t gen; - - HANDLE h_device_handle; /* device descriptor */ - long hASPI; - short i_sid; - long (*lpSendCommand)( void* ); - - /* Track information */ - bool toc_init; /* if true, info below is valid. */ - track_info_t tocent[100]; /* entry info for each track */ - track_t total_tracks; /* number of tracks in image */ - track_t first_track_num; /* track number of first track */ - -} _img_private_t; - -/* General ioctl() CD-ROM command function */ -static bool -_cdio_mciSendCommand(int id, UINT msg, DWORD flags, void *arg) -{ - MCIERROR mci_error; - - mci_error = mciSendCommand(id, msg, flags, (DWORD)arg); - if ( mci_error ) { - char error[256]; - - mciGetErrorString(mci_error, error, 256); - cdio_error("mciSendCommand() error: %s", error); - } - return(mci_error == 0); -} - -static const char * -cdio_is_cdrom(const char drive_letter) { - static char psz_win32_drive[7]; - static char root_path_name[8]; - _img_private_t obj; - - /* Initializations */ - obj.h_device_handle = NULL; - obj.i_sid = 0; - obj.hASPI = 0; - obj.lpSendCommand = 0; - - if ( WIN_NT ) { - sprintf( psz_win32_drive, "\\\\.\\%c:", drive_letter ); - sprintf( root_path_name, "\\\\.\\%c:\\", drive_letter ); - - obj.h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, - FILE_FLAG_NO_BUFFERING | - FILE_FLAG_RANDOM_ACCESS, NULL ); - if (obj.h_device_handle != NULL - && (DRIVE_CDROM == GetDriveType(root_path_name))) { - CloseHandle(obj.h_device_handle); - return strdup(psz_win32_drive); - } else { - CloseHandle(obj.h_device_handle); - return NULL; - } - } else { - HMODULE hASPI = NULL; - long (*lpGetSupport)( void ) = NULL; - long (*lpSendCommand)( void* ) = NULL; - DWORD dwSupportInfo; - int j, i_hostadapters; - char c_drive; - - hASPI = LoadLibrary( "wnaspi32.dll" ); - if( hASPI != NULL ) { - (FARPROC) lpGetSupport = GetProcAddress( hASPI, - "GetASPI32SupportInfo" ); - (FARPROC) lpSendCommand = GetProcAddress( hASPI, - "SendASPI32Command" ); - } - - if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL ) { - cdio_debug("Unable to load ASPI or get ASPI function pointers"); - if( hASPI ) FreeLibrary( hASPI ); - return NULL; - } - - /* ASPI support seems to be there */ - - dwSupportInfo = lpGetSupport(); - - if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) { - cdio_debug("no host adapters found (ASPI)"); - FreeLibrary( hASPI ); - return NULL; - } - - if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP ) { - cdio_debug("Unable to initalize ASPI layer"); - FreeLibrary( hASPI ); - return NULL; - } - - i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) ); - if( i_hostadapters == 0 ) { - FreeLibrary( hASPI ); - return NULL; - } - - c_drive = toupper(drive_letter) - 'A'; - - for( j = 0; j < 15; j++ ) { - struct SRB_GetDiskInfo srbDiskInfo; - - srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO; - srbDiskInfo.SRB_HaId = 0; - srbDiskInfo.SRB_Flags = 0; - srbDiskInfo.SRB_Hdr_Rsvd = 0; - srbDiskInfo.SRB_Target = j; - srbDiskInfo.SRB_Lun = 0; - - lpSendCommand( (void*) &srbDiskInfo ); - - if( (srbDiskInfo.SRB_Status == SS_COMP) && - (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) ) { - /* Make sure this is a cdrom device */ - struct SRB_GDEVBlock srbGDEVBlock; - - memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) ); - srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE; - srbGDEVBlock.SRB_HaId = 0; - srbGDEVBlock.SRB_Target = j; - - lpSendCommand( (void*) &srbGDEVBlock ); - - if( ( srbGDEVBlock.SRB_Status == SS_COMP ) && - ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) { - sprintf( psz_win32_drive, "%c:", drive_letter ); - FreeLibrary( hASPI ); - return(psz_win32_drive); - } - } - } - FreeLibrary( hASPI ); - } - return NULL; - -} - -/*! - Initialize CD device. - */ -static bool -_cdio_init_win32 (void *user_data) -{ - _img_private_t *_obj = user_data; - if (_obj->gen.init) { - cdio_error ("init called more than once"); - return false; - } - - _obj->gen.init = true; - _obj->toc_init = false; - - - /* Initializations */ - _obj->h_device_handle = NULL; - _obj->i_sid = 0; - _obj->hASPI = 0; - _obj->lpSendCommand = 0; - - if ( WIN_NT ) { - char psz_win32_drive[7]; - unsigned int len=strlen(_obj->gen.source_name); - - cdio_debug("using winNT/2K/XP ioctl layer"); - - if (cdio_is_device_win32(_obj->gen.source_name)) { - sprintf( psz_win32_drive, "\\\\.\\%c:", _obj->gen.source_name[len-2] ); - - _obj->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, - FILE_FLAG_NO_BUFFERING | - FILE_FLAG_RANDOM_ACCESS, NULL ); - return (_obj->h_device_handle == NULL) ? false : true; - } else - return false; - } else { - HMODULE hASPI = NULL; - long (*lpGetSupport)( void ) = NULL; - long (*lpSendCommand)( void* ) = NULL; - DWORD dwSupportInfo; - int i, j, i_hostadapters; - char c_drive = _obj->gen.source_name[0]; - - hASPI = LoadLibrary( "wnaspi32.dll" ); - if( hASPI != NULL ) { - (FARPROC) lpGetSupport = GetProcAddress( hASPI, - "GetASPI32SupportInfo" ); - (FARPROC) lpSendCommand = GetProcAddress( hASPI, - "SendASPI32Command" ); - } - - if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL ) { - cdio_debug("Unable to load ASPI or get ASPI function pointers"); - if( hASPI ) FreeLibrary( hASPI ); - return false; - } - - /* ASPI support seems to be there */ - - dwSupportInfo = lpGetSupport(); - - if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) { - cdio_debug("no host adapters found (ASPI)"); - FreeLibrary( hASPI ); - return -1; - } - - if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP ) { - cdio_debug("unable to initalize ASPI layer"); - FreeLibrary( hASPI ); - return -1; - } - - i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) ); - if( i_hostadapters == 0 ) { - FreeLibrary( hASPI ); - return -1; - } - - c_drive = toupper(c_drive) - 'A'; - - for( i = 0; i < i_hostadapters; i++ ) { - for( j = 0; j < 15; j++ ) { - struct SRB_GetDiskInfo srbDiskInfo; - - srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO; - srbDiskInfo.SRB_HaId = i; - srbDiskInfo.SRB_Flags = 0; - srbDiskInfo.SRB_Hdr_Rsvd = 0; - srbDiskInfo.SRB_Target = j; - srbDiskInfo.SRB_Lun = 0; - - lpSendCommand( (void*) &srbDiskInfo ); - - if( (srbDiskInfo.SRB_Status == SS_COMP) && - (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) ) { - /* Make sure this is a cdrom device */ - struct SRB_GDEVBlock srbGDEVBlock; - - memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) ); - srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE; - srbGDEVBlock.SRB_HaId = i; - srbGDEVBlock.SRB_Target = j; - - lpSendCommand( (void*) &srbGDEVBlock ); - - if( ( srbGDEVBlock.SRB_Status == SS_COMP ) && - ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) ) { - _obj->i_sid = MAKEWORD( i, j ); - _obj->hASPI = (long)hASPI; - _obj->lpSendCommand = lpSendCommand; - cdio_debug("Using ASPI layer"); - - return true; - } else { - FreeLibrary( hASPI ); - cdio_debug( "%c: is not a CD-ROM drive", - _obj->gen.source_name[0] ); - return false; - } - } - } - } - - FreeLibrary( hASPI ); - cdio_debug( "Unable to get HaId and target (ASPI)" ); - - } - - return false; -} - -/*! - Release and free resources associated with cd. - */ -static void -_cdio_win32_free (void *user_data) -{ - _img_private_t *_obj = user_data; - - if (NULL == _obj) return; - free (_obj->gen.source_name); - - if( _obj->h_device_handle ) - CloseHandle( _obj->h_device_handle ); - if( _obj->hASPI ) - FreeLibrary( (HMODULE)_obj->hASPI ); - - free (_obj); -} - -/*! - Reads a single mode2 sector from cd device into data starting from lsn. - Returns 0 if no error. - */ -static int -_cdio_mmc_read_sectors (void *user_data, void *data, lsn_t lsn, - int sector_type, unsigned int nblocks) -{ - _img_private_t *_obj = user_data; - unsigned char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; - - if( _obj->hASPI ) { - HANDLE hEvent; - struct SRB_ExecSCSICmd ssc; - - /* Create the transfer completion event */ - hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); - if( hEvent == NULL ) { - return 1; - } - - /* Data selection */ - - memset( &ssc, 0, sizeof( ssc ) ); - - ssc.SRB_Cmd = SC_EXEC_SCSI_CMD; - ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY; - ssc.SRB_HaId = LOBYTE( _obj->i_sid ); - ssc.SRB_Target = HIBYTE( _obj->i_sid ); - ssc.SRB_SenseLen = SENSE_LEN; - - ssc.SRB_PostProc = (LPVOID) hEvent; - ssc.SRB_CDBLen = 12; - - /* Operation code */ - ssc.CDBByte[ 0 ] = CDIO_MMC_GPCMD_READ_CD; - - CDIO_MMC_SET_READ_TYPE(ssc.CDBByte, sector_type); - CDIO_MMC_SET_READ_LBA(ssc.CDBByte, lsn); - CDIO_MMC_SET_READ_LENGTH(ssc.CDBByte, nblocks); - CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(ssc.CDBByte, - CDIO_MMC_MCSB_ALL_HEADERS); - - /* Result buffer */ - ssc.SRB_BufPointer = buf; - ssc.SRB_BufLen = CDIO_CD_FRAMESIZE_RAW; - - /* Initiate transfer */ - ResetEvent( hEvent ); - _obj->lpSendCommand( (void*) &ssc ); - - /* If the command has still not been processed, wait until it's - * finished */ - if( ssc.SRB_Status == SS_PENDING ) { - WaitForSingleObject( hEvent, INFINITE ); - } - CloseHandle( hEvent ); - - /* check that the transfer went as planned */ - if( ssc.SRB_Status != SS_COMP ) { - return 1; - } - - } else { - DWORD dwBytesReturned; - RAW_READ_INFO cdrom_raw; - - /* Initialize CDROM_RAW_READ structure */ - cdrom_raw.DiskOffset.QuadPart = CDIO_CD_FRAMESIZE * lsn; - cdrom_raw.SectorCount = 1; - cdrom_raw.TrackMode = XAForm2; - - if( DeviceIoControl( _obj->h_device_handle, - IOCTL_CDROM_RAW_READ, &cdrom_raw, - sizeof(RAW_READ_INFO), buf, - sizeof(buf), &dwBytesReturned, NULL ) - == 0 ) { - return 1; - } - } - - /* FIXME! remove the 8 (SUBHEADER size) below... */ - memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW); - - return 0; -} - -/*! - Reads an audio device into data starting from lsn. - Returns 0 if no error. - */ -static int -_cdio_read_audio_sectors (void *user_data, void *data, lsn_t lsn, - unsigned int nblocks) -{ - return _cdio_mmc_read_sectors( user_data, data, lsn, - CDIO_MMC_READ_TYPE_CDDA, nblocks ); -} - -/*! - Reads a single mode2 sector from cd device into data starting - from lsn. Returns 0 if no error. - */ -static int -_cdio_read_mode2_sector (void *user_data, void *data, lsn_t lsn, - bool mode2_form2) -{ - char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; - _img_private_t *_obj = user_data; - int ret; - - if (_obj->gen.ioctls_debugged == 75) - cdio_debug ("only displaying every 75th ioctl from now on"); - - if (_obj->gen.ioctls_debugged == 30 * 75) - cdio_debug ("only displaying every 30*75th ioctl from now on"); - - if (_obj->gen.ioctls_debugged < 75 - || (_obj->gen.ioctls_debugged < (30 * 75) - && _obj->gen.ioctls_debugged % 75 == 0) - || _obj->gen.ioctls_debugged % (30 * 75) == 0) - cdio_debug ("reading %lu", (unsigned long int) lsn); - - _obj->gen.ioctls_debugged++; - - ret = _cdio_mmc_read_sectors(user_data, buf, lsn, CDIO_MMC_READ_TYPE_ANY, 1); - - if( ret != 0 ) return ret; - - if (mode2_form2) - memcpy (data, buf, M2RAW_SECTOR_SIZE); - else - memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_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 -_cdio_read_mode2_sectors (void *user_data, void *data, lsn_t lsn, - bool mode2_form2, unsigned int nblocks) -{ - _img_private_t *_obj = user_data; - int i; - int retval; - - for (i = 0; i < nblocks; i++) { - if (mode2_form2) { - if ( (retval = _cdio_read_mode2_sector (_obj, - ((char *)data) + (M2RAW_SECTOR_SIZE * i), - lsn + i, true)) ) - return retval; - } else { - char buf[M2RAW_SECTOR_SIZE] = { 0, }; - if ( (retval = _cdio_read_mode2_sector (_obj, buf, lsn + i, true)) ) - return retval; - - memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), - buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); - } - } - return 0; -} - -/*! - Return the size of the CD in logical block address (LBA) units. - */ -static uint32_t -_cdio_stat_size (void *user_data) -{ - _img_private_t *_obj = user_data; - - return _obj->tocent[_obj->total_tracks].start_lsn; -} - -/*! - Set the key "arg" to "value" in source device. -*/ -static int -_cdio_set_arg (void *user_data, const char key[], const char value[]) -{ - _img_private_t *_obj = user_data; - - if (!strcmp (key, "source")) - { - if (!value) - return -2; - - free (_obj->gen.source_name); - - _obj->gen.source_name = strdup (value); - } - else - return -1; - - return 0; -} - -/*! - Read and cache the CD's Track Table of Contents and track info. - Return true if successful or false if an error. -*/ -static bool -_cdio_read_toc (_img_private_t *_obj) -{ - - if( _obj->hASPI ) { - HANDLE hEvent; - struct SRB_ExecSCSICmd ssc; - unsigned char p_tocheader[ 4 ]; - - /* Create the transfer completion event */ - hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); - if( hEvent == NULL ) { - return false; - } - - memset( &ssc, 0, sizeof( ssc ) ); - - ssc.SRB_Cmd = SC_EXEC_SCSI_CMD; - ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY; - ssc.SRB_HaId = LOBYTE( _obj->i_sid ); - ssc.SRB_Target = HIBYTE( _obj->i_sid ); - ssc.SRB_SenseLen = SENSE_LEN; - - ssc.SRB_PostProc = (LPVOID) hEvent; - ssc.SRB_CDBLen = 10; - - /* Operation code */ - ssc.CDBByte[ 0 ] = READ_TOC; - - /* Format */ - ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC; - - /* Starting track */ - ssc.CDBByte[ 6 ] = 0; - - /* Allocation length and buffer */ - ssc.SRB_BufLen = sizeof( p_tocheader ); - ssc.SRB_BufPointer = p_tocheader; - ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff; - ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff; - - /* Initiate transfer */ - ResetEvent( hEvent ); - _obj->lpSendCommand( (void*) &ssc ); - - /* If the command has still not been processed, wait until it's - * finished */ - if( ssc.SRB_Status == SS_PENDING ) - WaitForSingleObject( hEvent, INFINITE ); - - /* check that the transfer went as planned */ - if( ssc.SRB_Status != SS_COMP ) { - CloseHandle( hEvent ); - return false; - } - - _obj->first_track_num = p_tocheader[2]; - _obj->total_tracks = p_tocheader[3] - p_tocheader[2] + 1; - - { - int i, i_toclength; - unsigned char *p_fulltoc; - - i_toclength = 4 /* header */ + p_tocheader[0] + - ((unsigned int)p_tocheader[1] << 8); - - p_fulltoc = malloc( i_toclength ); - - if( p_fulltoc == NULL ) { - cdio_error( "out of memory" ); - CloseHandle( hEvent ); - return false; - } - - /* Allocation length and buffer */ - ssc.SRB_BufLen = i_toclength; - ssc.SRB_BufPointer = p_fulltoc; - ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff; - ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff; - - /* Initiate transfer */ - ResetEvent( hEvent ); - _obj->lpSendCommand( (void*) &ssc ); - - /* If the command has still not been processed, wait until it's - * finished */ - if( ssc.SRB_Status == SS_PENDING ) - WaitForSingleObject( hEvent, INFINITE ); - - /* check that the transfer went as planned */ - if( ssc.SRB_Status != SS_COMP ) - _obj->total_tracks = 0; - - for( i = 0 ; i <= _obj->total_tracks ; i++ ) { - int i_index = 8 + 8 * i; - _obj->tocent[ i ].start_lsn = ((int)p_fulltoc[ i_index ] << 24) + - ((int)p_fulltoc[ i_index+1 ] << 16) + - ((int)p_fulltoc[ i_index+2 ] << 8) + - (int)p_fulltoc[ i_index+3 ]; - - cdio_debug( "p_sectors: %i %lu", - i, (unsigned long int) _obj->tocent[i].start_lsn ); - } - - free( p_fulltoc ); - } - - CloseHandle( hEvent ); - return true; - - } else { - DWORD dwBytesReturned; - CDROM_TOC cdrom_toc; - int i; - - if( DeviceIoControl( _obj->h_device_handle, - IOCTL_CDROM_READ_TOC, - NULL, 0, &cdrom_toc, sizeof(CDROM_TOC), - &dwBytesReturned, NULL ) == 0 ) { - cdio_debug( "could not read TOCHDR" ); - return false; - } - - _obj->first_track_num = cdrom_toc.FirstTrack; - _obj->total_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1; - - - for( i = 0 ; i <= _obj->total_tracks ; i++ ) { - _obj->tocent[ i ].start_lsn = MSF_TO_LBA2( - cdrom_toc.TrackData[i].Address[1], - cdrom_toc.TrackData[i].Address[2], - cdrom_toc.TrackData[i].Address[3] ); - cdio_debug("p_sectors: %i, %lu", i, - (unsigned long int) (_obj->tocent[i].start_lsn)); - } - } - return true; -} - -/*! - Eject media. Return 1 if successful, 0 otherwise. - */ -static int -_cdio_eject_media (void *user_data) { - - _img_private_t *_obj = user_data; - - - MCI_OPEN_PARMS op; - MCI_STATUS_PARMS st; - DWORD i_flags; - char psz_drive[4]; - int ret; - - memset( &op, 0, sizeof(MCI_OPEN_PARMS) ); - op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO; - strcpy( psz_drive, "X:" ); - psz_drive[0] = _obj->gen.source_name[0]; - op.lpstrElementName = psz_drive; - - /* Set the flags for the device type */ - i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | - MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE; - - if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) ) { - st.dwItem = MCI_STATUS_READY; - /* Eject disc */ - ret = mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 ) != 0; - /* Release access to the device */ - mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 ); - } else - ret = 0; - - return ret; -} - -/*! - Return the value associated with the key "arg". -*/ -static const char * -_cdio_get_arg (void *user_data, const char key[]) -{ - _img_private_t *_obj = user_data; - - if (!strcmp (key, "source")) { - return _obj->gen.source_name; - } else if (!strcmp (key, "access-mode")) { - if ( WIN_NT ) - return "winNT/2K/XP ioctl"; - else if (_obj->hASPI) - return "ASPI"; - else - return "undefined WIN32"; - } - return NULL; -} - -/*! - Return the number of of the first track. - CDIO_INVALID_TRACK is returned on error. -*/ -static track_t -_cdio_get_first_track_num(void *user_data) -{ - _img_private_t *_obj = user_data; - - if (!_obj->toc_init) _cdio_read_toc (_obj) ; - - return _obj->first_track_num; -} - -/*! - Return the number of tracks in the current medium. - CDIO_INVALID_TRACK is returned on error. -*/ -static track_t -_cdio_get_num_tracks(void *user_data) -{ - _img_private_t *_obj = user_data; - - if (!_obj->toc_init) _cdio_read_toc (_obj) ; - - return _obj->total_tracks; -} - -/*! - Get format of track. -*/ -static track_format_t -_cdio_get_track_format(void *user_data, track_t track_num) -{ - _img_private_t *_obj = user_data; - - MCI_OPEN_PARMS op; - MCI_STATUS_PARMS st; - DWORD i_flags; - int ret; - - memset( &op, 0, sizeof(MCI_OPEN_PARMS) ); - op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO; - op.lpstrElementName = _obj->gen.source_name; - - /* Set the flags for the device type */ - i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | - MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE; - - if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) ) { - st.dwItem = MCI_CDA_STATUS_TYPE_TRACK; - st.dwTrack = track_num; - i_flags = MCI_TRACK | MCI_STATUS_ITEM ; - ret = mciSendCommand( op.wDeviceID, MCI_STATUS, i_flags, - (unsigned long) &st ); - - /* Release access to the device */ - mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 ); - - switch(st.dwReturn) { - case MCI_CDA_TRACK_AUDIO: - return TRACK_FORMAT_AUDIO; - case MCI_CDA_TRACK_OTHER: - return TRACK_FORMAT_DATA; - default: - return TRACK_FORMAT_XA; - } - } - - 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? -*/ -static bool -_cdio_get_track_green(void *user_data, track_t track_num) -{ - _img_private_t *_obj = user_data; - - if (!_obj->toc_init) _cdio_read_toc (_obj) ; - - if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; - - if (track_num > _obj->total_tracks+1 || track_num == 0) - return false; - - /* FIXME! */ - return true; -} - -/*! - Return the starting MSF (minutes/secs/frames) for track number - track_num in obj. 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. -*/ -static bool -_cdio_get_track_msf(void *user_data, track_t track_num, msf_t *msf) -{ - _img_private_t *_obj = user_data; - - if (NULL == msf) return false; - - if (!_obj->toc_init) _cdio_read_toc (_obj) ; - - if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; - - if (track_num > _obj->total_tracks+1 || track_num == 0) { - return false; - } else { - cdio_lsn_to_msf(_obj->tocent[track_num-1].start_lsn, msf); - return true; - } -} - -#endif /* HAVE_WIN32_CDROM */ - -/*! - Return an array of strings giving possible CD devices. - */ -char ** -cdio_get_devices_win32 (void) -{ -#ifndef HAVE_WIN32_CDROM - return NULL; -#else - char **drives = NULL; - unsigned int num_drives=0; - char drive_letter; - - /* Scan the system for CD-ROM drives. - */ - -#if FINISHED - /* Now check the currently mounted CD drives */ - if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) { - cdio_add_device_list(&drives, drive, &num_drives); - } - - /* Finally check possible mountable drives in /etc/fstab */ - if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) { - cdio_add_device_list(&drives, drive, &num_drives); - } -#endif - - /* Scan the system for CD-ROM drives. - Not always 100% reliable, so use the USE_MNTENT code above first. - */ - for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) { - const char *drive_str=cdio_is_cdrom(drive_letter); - if (drive_str != NULL) { - cdio_add_device_list(&drives, drive_str, &num_drives); - } - } - cdio_add_device_list(&drives, NULL, &num_drives); - return drives; -#endif /*HAVE_WIN32_CDROM*/ -} - -/*! - Return a string containing the default CD device if none is specified. - if CdIo is NULL (we haven't initialized a specific device driver), - then find a suitable one and return the default device for that. - - NULL is returned if we couldn't get a default device. -*/ -char * -cdio_get_default_device_win32(void) -{ - -#ifdef HAVE_WIN32_CDROM - char drive_letter; - - for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) { - const char *drive_str=cdio_is_cdrom(drive_letter); - if (drive_str != NULL) { - return strdup(drive_str); - } - } -#endif - return NULL; -} - -/*! - Return true if source_name could be a device containing a CD-ROM. -*/ -bool -cdio_is_device_win32(const char *source_name) -{ - unsigned int len; - len = strlen(source_name); - - if (NULL == source_name) return false; - -#ifdef HAVE_WIN32_CDROM - if ( WIN_NT ) - /* Really should test to see if of form: \\.\x: */ - return ((len == 6) && isalpha(source_name[len-2]) - && (source_name[len-1] == ':')); - else - /* See if is of form: x: */ - return ((len == 2) && isalpha(source_name[0]) - && (source_name[len-1] == ':')); -#else - return false; -#endif -} - -/*! - 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_win32 (const char *source_name) -{ - -#ifdef HAVE_WIN32_CDROM - CdIo *ret; - _img_private_t *_data; - - cdio_funcs _funcs = { - .eject_media = _cdio_eject_media, - .free = _cdio_win32_free, - .get_arg = _cdio_get_arg, - .get_default_device = cdio_get_default_device_win32, - .get_devices = cdio_get_devices_win32, - .get_first_track_num= _cdio_get_first_track_num, - .get_mcn = NULL, - .get_num_tracks = _cdio_get_num_tracks, - .get_track_format = _cdio_get_track_format, - .get_track_green = _cdio_get_track_green, - .get_track_lba = NULL, /* This could be implemented if need be. */ - .get_track_msf = _cdio_get_track_msf, - .lseek = NULL, - .read = NULL, - .read_audio_sectors = _cdio_read_audio_sectors, - .read_mode2_sector = _cdio_read_mode2_sector, - .read_mode2_sectors = _cdio_read_mode2_sectors, - .set_arg = _cdio_set_arg, - .stat_size = _cdio_stat_size - }; - - _data = _cdio_malloc (sizeof (_img_private_t)); - _data->gen.init = false; - _data->gen.fd = -1; - - _cdio_set_arg(_data, "source", (NULL == source_name) - ? cdio_get_default_device_win32(): source_name); - - ret = cdio_new (_data, &_funcs); - if (ret == NULL) return NULL; - - if (_cdio_init_win32(_data)) - return ret; - else { - _cdio_win32_free (_data); - return NULL; - } - -#else - return NULL; -#endif /* HAVE_WIN32_CDROM */ - -} - -bool -cdio_have_win32 (void) -{ -#ifdef HAVE_WIN32_CDROM - return true; -#else - return false; -#endif /* HAVE_WIN32_CDROM */ -} diff --git a/src/input/vcd/libcdio/bytesex.h b/src/input/vcd/libcdio/bytesex.h index 431de9799..a94a8129e 100644 --- a/src/input/vcd/libcdio/bytesex.h +++ b/src/input/vcd/libcdio/bytesex.h @@ -1,5 +1,5 @@ /* - $Id: bytesex.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: bytesex.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel diff --git a/src/input/vcd/libcdio/bytesex_asm.h b/src/input/vcd/libcdio/bytesex_asm.h index 34ef5e605..03cb3cd98 100644 --- a/src/input/vcd/libcdio/bytesex_asm.h +++ b/src/input/vcd/libcdio/bytesex_asm.h @@ -1,5 +1,5 @@ /* - $Id: bytesex_asm.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: bytesex_asm.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2001 Sven Ottemann 2001 Herbert Valerio Riedel diff --git a/src/input/vcd/libcdio/cd_types.c b/src/input/vcd/libcdio/cd_types.c index 0dd6c8410..047c8bb63 100644 --- a/src/input/vcd/libcdio/cd_types.c +++ b/src/input/vcd/libcdio/cd_types.c @@ -1,5 +1,5 @@ /* - $Id: cd_types.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: cd_types.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2003 Rocky Bernstein @@ -208,7 +208,7 @@ _cdio_get_joliet_level( void ) */ cdio_fs_anal_t cdio_guess_cd_type(const CdIo *cdio, int start_session, track_t track_num, - /*out*/ cdio_analysis_t *cdio_analysis) + /*out*/ cdio_iso_analysis_t *iso_analysis) { int ret = 0; bool sector0_read_ok; @@ -239,8 +239,8 @@ cdio_guess_cd_type(const CdIo *cdio, int start_session, track_t track_num, ret = CDIO_FS_ISO_HFS; else ret = CDIO_FS_ISO_9660; - cdio_analysis->isofs_size = _cdio_get_iso9660_fs_sec_count(); - sprintf(cdio_analysis->iso_label, buffer[0]+40); + iso_analysis->isofs_size = _cdio_get_iso9660_fs_sec_count(); + sprintf(iso_analysis->iso_label, buffer[0]+40); #if 0 if (_cdio_is_rockridge()) @@ -251,7 +251,7 @@ cdio_guess_cd_type(const CdIo *cdio, int start_session, track_t track_num, return ret; if (_cdio_is_joliet()) { - cdio_analysis->joliet_level = _cdio_get_joliet_level(); + iso_analysis->joliet_level = _cdio_get_joliet_level(); ret |= CDIO_FS_ANAL_JOLIET; } if (_cdio_is_it(INDEX_BOOTABLE)) diff --git a/src/input/vcd/libcdio/cdio.c b/src/input/vcd/libcdio/cdio.c index 8d6c39bed..2a814141b 100644 --- a/src/input/vcd/libcdio/cdio.c +++ b/src/input/vcd/libcdio/cdio.c @@ -1,7 +1,7 @@ /* - $Id: cdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: cdio.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein Copyright (C) 2001 Herbert Valerio Riedel This program is free software; you can redistribute it and/or modify @@ -37,7 +37,7 @@ #include #include "cdio_private.h" -static const char _rcsid[] = "$Id: cdio.c,v 1.1 2003/10/13 11:47:11 f1rmb Exp $"; +static const char _rcsid[] = "$Id: cdio.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; const char *track_format2str[6] = @@ -299,7 +299,9 @@ cdio_get_devices (driver_id_t driver_id) if (cdio == NULL) return NULL; if (cdio->op.get_devices) { - return cdio->op.get_devices (); + char **devices = cdio->op.get_devices (); + cdio_destroy(cdio); + return devices; } else { return NULL; } @@ -318,6 +320,8 @@ cdio_get_devices (driver_id_t driver_id) To find a CD-drive of any type, use the mask CDIO_FS_MATCH_ALL. NULL is returned if we couldn't get a default device. + It is also possible to return a non NULL but after dereferencing the + the value is NULL. This also means nothing was found. */ char ** cdio_get_devices_with_cap (char* search_devices[], @@ -332,23 +336,26 @@ cdio_get_devices_with_cap (char* search_devices[], if (capabilities == CDIO_FS_MATCH_ALL) { /* Duplicate drives into drives_ret. */ - for( ; *drives != NULL; drives++ ) { - cdio_add_device_list(&drives_ret, *drives, &num_drives); + char **d = drives; + + for( ; *d != NULL; d++ ) { + cdio_add_device_list(&drives_ret, *d, &num_drives); } } else { cdio_fs_anal_t got_fs=0; cdio_fs_anal_t need_fs = CDIO_FSTYPE(capabilities); cdio_fs_anal_t need_fs_ext; + char **d = drives; need_fs_ext = capabilities & ~CDIO_FS_MASK; - for( ; *drives != NULL; drives++ ) { - CdIo *cdio = cdio_open(*drives, DRIVER_UNKNOWN); + for( ; *d != NULL; d++ ) { + CdIo *cdio = cdio_open(*d, DRIVER_UNKNOWN); if (NULL != cdio) { track_t first_track = cdio_get_first_track_num(cdio); - cdio_analysis_t cdio_analysis; + cdio_iso_analysis_t cdio_iso_analysis; got_fs = cdio_guess_cd_type(cdio, 0, first_track, - &cdio_analysis); + &cdio_iso_analysis); /* Match on fs and add */ if ( (CDIO_FS_UNKNOWN == need_fs || CDIO_FSTYPE(got_fs) == need_fs) ) { @@ -362,8 +369,10 @@ cdio_get_devices_with_cap (char* search_devices[], cdio_destroy(cdio); } } - cdio_add_device_list(&drives_ret, NULL, &num_drives); } + cdio_add_device_list(&drives_ret, NULL, &num_drives); + cdio_free_device_list(drives); + free(drives); return drives_ret; } @@ -669,53 +678,48 @@ cdio_read_audio_sectors (const CdIo *cdio, void *buf, lsn_t lsn, 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. */ int -cdio_read_mode1_sector (const CdIo *cdio, void *data, lsn_t lsn, bool is_form2) +cdio_read_mode1_sector (const CdIo *cdio, void *data, lsn_t lsn, bool b_form2) { - uint32_t size = is_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE ; + uint32_t size = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE ; char buf[M2RAW_SECTOR_SIZE] = { 0, }; - int ret; cdio_assert (cdio != NULL); cdio_assert (data != NULL); - if (cdio->op.lseek && cdio->op.read) { + if (cdio->op.read_mode1_sector && cdio->op.read_mode1_sector) { + return cdio->op.read_mode1_sector(cdio->env, data, lsn, b_form2); + } else if (cdio->op.lseek && cdio->op.read) { if (0 > cdio_lseek(cdio, CDIO_CD_FRAMESIZE*lsn, SEEK_SET)) return -1; if (0 > cdio_read(cdio, buf, CDIO_CD_FRAMESIZE)) return -1; memcpy (data, buf, size); return 0; - } else { - ret = cdio_read_mode2_sector(cdio, data, lsn, is_form2); - if (ret == 0) - memcpy (data, buf+CDIO_CD_SUBHEADER_SIZE, size); - } - return ret; + } + + return 1; } int -cdio_read_mode1_sectors (const CdIo *cdio, void *data, lsn_t lsn, - bool is_form2, unsigned int num_sectors) +cdio_read_mode1_sectors (const CdIo *cdio, void *buf, lsn_t lsn, + bool b_form2, unsigned int num_sectors) { - uint32_t size = is_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE ; - int retval; - int i; - cdio_assert (cdio != NULL); + cdio_assert (buf != NULL); + cdio_assert (cdio->op.read_mode1_sectors != NULL); - for (i = 0; i < num_sectors; i++) { - if ( (retval = cdio_read_mode1_sector (cdio, - ((char *)data) + (size * i), - lsn + i, is_form2)) ) - return retval; - } - return 0; + return cdio->op.read_mode1_sectors (cdio->env, buf, lsn, b_form2, + num_sectors); } /*! @@ -724,7 +728,7 @@ cdio_read_mode1_sectors (const CdIo *cdio, void *data, lsn_t lsn, */ int cdio_read_mode2_sector (const CdIo *cdio, void *buf, lsn_t lsn, - bool is_form2) + bool b_form2) { cdio_assert (cdio != NULL); cdio_assert (buf != NULL); @@ -732,24 +736,24 @@ cdio_read_mode2_sector (const CdIo *cdio, void *buf, lsn_t lsn, || cdio->op.read_mode2_sectors != NULL); if (cdio->op.read_mode2_sector) - return cdio->op.read_mode2_sector (cdio->env, buf, lsn, is_form2); + 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, is_form2, 1); + return cdio_read_mode2_sectors (cdio, buf, lsn, b_form2, 1); return 1; } int -cdio_read_mode2_sectors (const CdIo *cdio, void *buf, lsn_t lsn, bool mode2raw, - unsigned num_sectors) +cdio_read_mode2_sectors (const CdIo *cdio, void *buf, lsn_t lsn, + bool b_form2, unsigned int num_sectors) { cdio_assert (cdio != NULL); cdio_assert (buf != NULL); cdio_assert (cdio->op.read_mode2_sectors != NULL); return cdio->op.read_mode2_sectors (cdio->env, buf, lsn, - mode2raw, num_sectors); + b_form2, num_sectors); } uint32_t @@ -849,6 +853,7 @@ cdio_open (const char *orig_source_name, driver_id_t driver_id) 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; + free(source_name); return ret; } } diff --git a/src/input/vcd/libcdio/cdio/cd_types.h b/src/input/vcd/libcdio/cdio/cd_types.h index 91b36453c..1117171ae 100644 --- a/src/input/vcd/libcdio/cdio/cd_types.h +++ b/src/input/vcd/libcdio/cdio/cd_types.h @@ -1,5 +1,5 @@ /* - $Id: cd_types.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: cd_types.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2003 Rocky Bernstein Copyright (C) 1996,1997,1998 Gerd Knorr @@ -20,10 +20,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* - Filesystem types we understand. The highest-numbered fs type should - be less than CDIO_FS_MASK defined below. -*/ +/** \file cd_types.h + * \brief Header for routines which automatically determine the Compact Disc + * format and possibly filesystem on the CD. + * + */ #ifndef __CDIO_CD_TYPES_H__ #define __CDIO_CD_TYPES_H__ @@ -32,72 +33,100 @@ extern "C" { #endif /* __cplusplus */ -#define CDIO_FS_AUDIO 1 /* audio only - not really a fs */ -#define CDIO_FS_HIGH_SIERRA 2 -#define CDIO_FS_ISO_9660 3 +/** + * Filesystem types we understand. The highest-numbered fs type should + * be less than CDIO_FS_MASK defined below. + */ +#define CDIO_FS_AUDIO 1 /**< audio only - not really a + filesystem */ +#define CDIO_FS_HIGH_SIERRA 2 +#define CDIO_FS_ISO_9660 3 /**< ISO 9660 filesystem */ #define CDIO_FS_INTERACTIVE 4 -#define CDIO_FS_HFS 5 -#define CDIO_FS_UFS 6 - -/* EXT2 was the GNU/Linux native filesystem for early kernels. Newer - GNU/Linux OS's may use EXT3 which EXT2 with a journal. */ +#define CDIO_FS_HFS 5 /**< file system used on the Macintosh + system in MacOS 6 through MacOS 9 + and depricated in OSX. */ +#define CDIO_FS_UFS 6 /**< Generic Unix file system derived + from the Berkeley fast file + system. */ + +/** + * EXT2 was the GNU/Linux native filesystem for early kernels. Newer + * GNU/Linux OS's may use EXT3 which EXT2 with a journal. + */ #define CDIO_FS_EXT2 7 -#define CDIO_FS_ISO_HFS 8 /* both hfs & isofs filesystem */ -#define CDIO_FS_ISO_9660_INTERACTIVE 9 /* both CD-RTOS and isofs filesystem */ +#define CDIO_FS_ISO_HFS 8 /**< both HFS & ISO-9660 filesystem */ +#define CDIO_FS_ISO_9660_INTERACTIVE 9 /**< both CD-RTOS and ISO filesystem */ -/* The 3DO is, technically, a set of specifications created by the 3DO -company. These specs are for making a 3DO Interactive Multiplayer -which uses a CD-player. Panasonic in the early 90's was the first -company to manufacture and market a 3DO player. */ +/** + * The 3DO is, technically, a set of specifications created by the 3DO + * company. These specs are for making a 3DO Interactive Multiplayer + * which uses a CD-player. Panasonic in the early 90's was the first + * company to manufacture and market a 3DO player. + */ #define CDIO_FS_3DO 10 -#define CDIO_FS_MASK 15 /* Should be 2*n-1 and > above */ +#define CDIO_FS_MASK 15 /**< Note: this should be 2**n-1 and + and greater than the highest + CDIO_FS number above */ #define CDIO_FS_UNKNOWN CDIO_FS_MASK -/* Macro to extract just the FS type portion defined above */ +/** + * Macro to extract just the FS type portion defined above +*/ #define CDIO_FSTYPE(fs) (fs & CDIO_FS_MASK) -/* - Bit masks for the classes of CD-images. These are generally - higher-level than the fs-type information above and may be determined - based of the fs type information. +/** + * Bit masks for the classes of CD-images. These are generally + * higher-level than the fs-type information above and may be determined + * based of the fs type information. */ -#define CDIO_FS_ANAL_XA 16 -#define CDIO_FS_ANAL_MULTISESSION 32 -#define CDIO_FS_ANAL_PHOTO_CD 64 -#define CDIO_FS_ANAL_HIDDEN_TRACK 128 +#define CDIO_FS_ANAL_XA 16 /**< eXtended Architecture format */ +#define CDIO_FS_ANAL_MULTISESSION 32 /**< CD has multisesion */ +#define CDIO_FS_ANAL_PHOTO_CD 64 /**< Is a Kodak Photo CD */ +#define CDIO_FS_ANAL_HIDDEN_TRACK 128 /**< Hidden track at the + beginning of the CD */ #define CDIO_FS_ANAL_CDTV 256 -#define CDIO_FS_ANAL_BOOTABLE 512 -#define CDIO_FS_ANAL_VIDEOCD 1024 /* VCD 1.1 */ -#define CDIO_FS_ANAL_ROCKRIDGE 2048 -#define CDIO_FS_ANAL_JOLIET 4096 -#define CDIO_FS_ANAL_SVCD 8192 /* Super VCD or Choiji Video CD */ -#define CDIO_FS_ANAL_CVD 16384 /* Choiji Video CD */ - -/* Pattern which can be used by cdio_get_devices to specify matching - any sort of CD. -*/ +#define CDIO_FS_ANAL_BOOTABLE 512 /**< CD is bootable */ +#define CDIO_FS_ANAL_VIDEOCD 1024 /**< VCD 1.1 */ +#define CDIO_FS_ANAL_ROCKRIDGE 2048 /**< Has Rock Ridge Extensions to + ISO 9660 */ +#define CDIO_FS_ANAL_JOLIET 4096 /**< Microsoft Joliet extensions + to ISO 9660 */ +#define CDIO_FS_ANAL_SVCD 8192 /**< Super VCD or Choiji Video CD */ +#define CDIO_FS_ANAL_CVD 16384 /**< Choiji Video CD */ + +/** + * Pattern which can be used by cdio_get_devices to specify matching + * any sort of CD. + */ #define CDIO_FS_MATCH_ALL (cdio_fs_anal_t) (~CDIO_FS_MASK) +/*! + \brief The type used to return analysis information from + cdio_guess_cd_type. + + These fields make sense only for when an ISO-9660 filesystem is used. + */ typedef struct { - unsigned int joliet_level; - char iso_label[33]; /* 32 + 1 for null byte at the end in - formatting the string */ + unsigned int joliet_level; /**< If has Joliet extensions, this is the + associated level number (i.e. 1, 2, or 3). */ + char iso_label[33]; /**< This is 32 + 1 for null byte at the end in + formatting the string */ unsigned int isofs_size; -} cdio_analysis_t; +} cdio_iso_analysis_t; -/* - Try to determine what kind of CD-image and/or filesystem we - have at track track_num. Return information about the CD image - is returned in cdio_analysis and the return value. -*/ +/** + * Try to determine what kind of CD-image and/or filesystem we + * have at track track_num. Return information about the CD image + * is returned in iso_analysis and the return value. + */ cdio_fs_anal_t cdio_guess_cd_type(const CdIo *cdio, int start_session, track_t track_num, - /*out*/ cdio_analysis_t *cdio_analysis); + /*out*/ cdio_iso_analysis_t *iso_analysis); #ifdef __cplusplus } diff --git a/src/input/vcd/libcdio/cdio/cdio.h b/src/input/vcd/libcdio/cdio/cdio.h index d0b304b38..a9fa4b02e 100644 --- a/src/input/vcd/libcdio/cdio/cdio.h +++ b/src/input/vcd/libcdio/cdio/cdio.h @@ -1,8 +1,8 @@ /* -*- c -*- - $Id: cdio.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: cdio.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2001 Herbert Valerio Riedel - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein 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 @@ -19,14 +19,16 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Public CD Input and Control Interface . */ +/** \file cdio.h + * \brief The top-level header for libcdio: the CD Input and Control library. + */ #ifndef __CDIO_H__ #define __CDIO_H__ -/* Application Interface or Protocol version number. If the public - interface changes, we increase this number. +/** Application Interface or Protocol version number. If the public + * interface changes, we increase this number. */ #define CDIO_API_VERSION 1 @@ -43,64 +45,68 @@ #include /* Flags specifying the category of device to open or is opened. */ -#define CDIO_SRC_IS_DISK_IMAGE_MASK 0x0001 -#define CDIO_SRC_IS_DEVICE_MASK 0x0002 -#define CDIO_SRC_IS_SCSI_MASK 0x0004 + +#define CDIO_SRC_IS_DISK_IMAGE_MASK 0x0001 /**< Read source is a CD image. */ +#define CDIO_SRC_IS_DEVICE_MASK 0x0002 /**< Read source is a CD device. */ +#define CDIO_SRC_IS_SCSI_MASK 0x0004 #define CDIO_SRC_IS_NATIVE_MASK 0x0008 #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ - /* opaque structure */ + /** This is an opaque structure. */ typedef struct _CdIo CdIo; - /* The below enumerations may be used to tag a specific driver - that is opened or is desired to be opened. Note that this is - different than what is available on a given host. - - Order is a little significant since the order is used in scans. - We have to start with UNKNOWN and devices should come before - disk-image readers. By putting something towards the top (a lower - enumeration number), in an iterative scan we prefer that to something - with a higher enumeration number. - - NOTE: IF YOU MODIFY ENUM MAKE SURE INITIALIZATION IN CDIO.C AGREES. - - */ + /** The driver_id_t enumerations may be used to tag a specific driver + * that is opened or is desired to be opened. Note that this is + * different than what is available on a given host. + * + * Order is a little significant since the order is used in scans. + * We have to start with UNKNOWN and devices should come before + * disk-image readers. By putting something towards the top (a lower + * enumeration number), in an iterative scan we prefer that to + * something with a higher enumeration number. + * + * NOTE: IF YOU MODIFY ENUM MAKE SURE INITIALIZATION IN CDIO.C AGREES. + * + */ typedef enum { DRIVER_UNKNOWN, - DRIVER_BSDI, - DRIVER_FREEBSD, - DRIVER_LINUX, - DRIVER_SOLARIS, - DRIVER_OSX, - DRIVER_WIN32, - DRIVER_BINCUE, /* Prefer bincue over nrg when both exist */ - DRIVER_NRG, - DRIVER_DEVICE /* Is really a set of the above; should come last */ + DRIVER_BSDI, /**< BSDI driver */ + DRIVER_FREEBSD, /**< FreeBSD driver */ + DRIVER_LINUX, /**< GNU/Linux Driver */ + DRIVER_SOLARIS, /**< Sun Solaris Driver */ + DRIVER_OSX, /**< Apple OSX Driver */ + DRIVER_WIN32, /**< Microsoft Windows Driver */ + DRIVER_BINCUE, /**< BIN/CUE format CD image. This is listed before NRG, + to make the code prefer BINCUE over NRG when both + exist. */ + DRIVER_NRG, /**< Nero NRG format CD image. */ + DRIVER_DEVICE /**< Is really a set of the above; should come last */ } driver_id_t; - /* Make sure what's listed below is the last one above. Since we have - a bogus (but useful) 0th entry above we don't have to add one below. - */ +/** Make sure what's listed for CDIO_MIN_DRIVER is the last + enumeration in driver_id_t. Since we have a bogus (but useful) 0th + entry above we don't have to add one. +*/ #define CDIO_MIN_DRIVER DRIVER_BSDI #define CDIO_MIN_DEVICE_DRIVER CDIO_MIN_DRIVER #define CDIO_MAX_DRIVER DRIVER_NRG #define CDIO_MAX_DEVICE_DRIVER DRIVER_WIN32 typedef enum { - TRACK_FORMAT_AUDIO, /* Audio track, e.g. CD-DA */ - TRACK_FORMAT_CDI, /* CD-i. How this is different from DATA below? */ - TRACK_FORMAT_XA, /* Mode2 of some sort */ - TRACK_FORMAT_DATA, /* Mode1 of some sort */ - TRACK_FORMAT_PSX, /* Playstation CD. Like audio but only 2336 bytes - of user data. + TRACK_FORMAT_AUDIO, /**< Audio track, e.g. CD-DA */ + TRACK_FORMAT_CDI, /**< CD-i. How this is different from DATA below? */ + TRACK_FORMAT_XA, /**< Mode2 of some sort */ + TRACK_FORMAT_DATA, /**< Mode1 of some sort */ + TRACK_FORMAT_PSX, /**< Playstation CD. Like audio but only 2336 bytes + * of user data. */ - TRACK_FORMAT_ERROR /* Dunno what is or some other error. */ + TRACK_FORMAT_ERROR /**< Dunno what is, or some other error. */ } track_format_t; - /* Printable tags for above enumeration. */ + /*! Printable tags for track_format_t enumeration. */ extern const char *track_format2str[6]; /*! @@ -140,12 +146,14 @@ extern "C" { To find a CD-drive of any type, use the mask CDIO_FS_MATCH_ALL. NULL is returned if we couldn't get a default device. + It is also possible to return a non NULL but after dereferencing the + the value is NULL. This also means nothing was found. */ char ** cdio_get_devices_with_cap (char* search_devices[], cdio_fs_anal_t capabilities, bool any); - /*!Return an array of device names. If you want a specific - devices, dor a driver give that device, if you want hardware + /*! Return an array of device names. If you want a specific + devices for a driver, give that device. If you want hardware devices, give DRIVER_DEVICE and if you want all possible devices, image drivers and hardware drivers give DRIVER_UNKNOWN. @@ -190,7 +198,7 @@ extern "C" { track_t cdio_get_num_tracks (const CdIo *obj); /*! - Get format of track. + Get the format (audio, mode2, mode1) of track. */ track_format_t cdio_get_track_format(const CdIo *obj, track_t track_num); @@ -255,46 +263,46 @@ extern "C" { ssize_t cdio_read(const CdIo *obj, void *buf, size_t size); /*! - Reads a audio sector from cd device into data starting - from lsn. Returns 0 if no error. - */ + Reads a audio sector from cd device into data starting + from lsn. Returns 0 if no error. + */ int cdio_read_audio_sector (const CdIo *obj, void *buf, lsn_t lsn); /*! - Reads a audio sector from cd device into data starting - from lsn. Returns 0 if no error. - */ + Reads a audio sector from cd device into data starting + from lsn. Returns 0 if no error. + */ int cdio_read_audio_sectors (const CdIo *obj, void *buf, lsn_t lsn, unsigned int nblocks); /*! Reads a single mode1 sector from cd device into data starting from lsn. Returns 0 if no error. - */ + */ int cdio_read_mode1_sector (const CdIo *obj, void *buf, lsn_t lsn, - bool is_form2); - + bool b_form2); + /*! - Reads nblocks of mode1 sectors from cd device into data starting - from lsn. Returns 0 if no error. - */ + Reads nblocks of mode1 sectors from cd device into data starting + from lsn. Returns 0 if no error. + */ int cdio_read_mode1_sectors (const CdIo *obj, void *buf, lsn_t lsn, - bool is_form2, unsigned int num_sectors); - + bool b_form2, unsigned int num_sectors); + /*! - Reads a single mode2 sector from cd device into data starting - from lsn. Returns 0 if no error. - */ + Reads a single mode2 sector from cd device into data starting + from lsn. Returns 0 if no error. + */ int cdio_read_mode2_sector (const CdIo *obj, void *buf, lsn_t lsn, - bool is_form2); - + bool b_form2); + /*! Reads nblocks of mode2 sectors from cd device into data starting from lsn. Returns 0 if no error. */ int cdio_read_mode2_sectors (const CdIo *obj, void *buf, lsn_t lsn, - bool is_form2, unsigned int num_sectors); + bool b_form2, unsigned int num_sectors); /*! Set the arg "key" with "value" in the source device. @@ -306,49 +314,73 @@ extern "C" { Return the size of the CD in logical block address (LBA) units. */ uint32_t cdio_stat_size (const CdIo *obj); - + /*! Initialize CD Reading and control routines. Should be called first. - */ + */ bool cdio_init(void); - + /* True if xxx driver is available. where xxx=linux, solaris, nrg, ... */ + + /*! True if BSDI driver is available. */ bool cdio_have_bsdi (void); + + /*! True if FreeBSD driver is available. */ bool cdio_have_freebsd (void); + + /*! True if GNU/Linux driver is available. */ bool cdio_have_linux (void); + + /*! True if Sun Solaris driver is available. */ bool cdio_have_solaris (void); + + /*! True if Apple OSX driver is available. */ bool cdio_have_osx (void); + + /*! True if Microsoft Windows driver is available. */ bool cdio_have_win32 (void); + + /*! True if Nero driver is available. */ bool cdio_have_nrg (void); + + /*! True if BIN/CUE driver is available. */ bool cdio_have_bincue (void); - /* Like above but uses the enumeration instead. */ + /*! Like cdio_have_xxx but uses an enumeration instead. */ bool cdio_have_driver (driver_id_t driver_id); - /* Return a string decribing driver_id. */ + /*! Return a string decribing driver_id. */ const char *cdio_driver_describe (driver_id_t driver_id); /*! 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. to call one of the specific routines below. + done previously. to call one of the specific cdio_open_xxx + routines. NULL is returned on error. */ CdIo * cdio_open (const char *source_name, driver_id_t driver_id); - /*! cdrao BIN/CUE CD disk-image routines. Source is the .bin file + /*! Set up BIN/CUE CD disk-image for reading. Source is the .bin or + .cue file NULL is returned on error. */ CdIo * cdio_open_bincue (const char *bin_name); + /*! Return a string containing the default CUE file that would + be used when none is specified. + + NULL is returned on error or there is no device. + */ char * cdio_get_default_device_bincue(void); char **cdio_get_devices_bincue(void); - /*! CD routines. Source is the some sort of device. + /*! Set up CD-ROM for reading. The device_name is + the some sort of device name. NULL is returned on error. */ @@ -360,65 +392,154 @@ extern "C" { */ CdIo * cdio_open_cue (const char *cue_name); - /*! BSDI CD-reading routines. - NULL is returned on error. - */ + /*! Set up CD-ROM for reading using the BSDI driver. The device_name is + the some sort of device name. + + NULL is returned on error or there is no BSDI driver. + + @see cdio_open + */ CdIo * cdio_open_bsdi (const char *source_name); + /*! Return a string containing the default device name that the + BSDI driver would use when none is specified. + + NULL is returned on error or there is no CD-ROM device. + + @see cdio_open_cd + @see cdio_open + */ char * cdio_get_default_device_bsdi(void); + /*! Return a list of all of the CD-ROM devices that the BSDI driver + can find. + */ char **cdio_get_devices_bsdi(void); - /*! BSDI CD-reading routines. - NULL is returned on error. - */ + /*! Set up CD-ROM for reading using the FreeBSD driver. The device_name is + the some sort of device name. + + NULL is returned on error or there is no FreeBSD driver. + + @see cdio_open_cd + @see cdio_open + */ CdIo * cdio_open_freebsd (const char *source_name); + /*! Return a string containing the default device name that the + FreeBSD driver would use when none is specified. + + NULL is returned on error or there is no CD-ROM device. + */ char * cdio_get_default_device_freebsd(void); + /*! Return a list of all of the CD-ROM devices that the FreeBSD driver + can find. + */ char **cdio_get_devices_freebsd(void); - /*! Linux CD-reading routines. - NULL is returned on error. + /*! Set up CD-ROM for reading using the GNU/Linux driver. The device_name is + the some sort of device name. + + NULL is returned on error or there is no GNU/Linux driver. */ CdIo * cdio_open_linux (const char *source_name); + /*! Return a string containing the default device name that the + GNU/Linux driver would use when none is specified. A scan is made + for CD-ROM drives with CDs in them. + + NULL is returned on error or there is no CD-ROM device. + + @see cdio_open_cd + @see cdio_open + */ char * cdio_get_default_device_linux(void); + /*! Return a list of all of the CD-ROM devices that the GNU/Linux driver + can find. + */ char **cdio_get_devices_linux(void); - /*! Solaris CD-reading routines. - NULL is returned on error. + /*! Set up CD-ROM for reading using the Sun Solaris driver. The + device_name is the some sort of device name. + + NULL is returned on error or there is no Solaris driver. */ CdIo * cdio_open_solaris (const char *source_name); + /*! Return a string containing the default device name that the + Solaris driver would use when none is specified. A scan is made + for CD-ROM drives with CDs in them. + + NULL is returned on error or there is no CD-ROM device. + + @see cdio_open_cd + @see cdio_open + */ char * cdio_get_default_device_solaris(void); + /*! Return a list of all of the CD-ROM devices that the Solaris driver + can find. + */ char **cdio_get_devices_solaris(void); - /*! Darwin OS X CD-reading routines. - NULL is returned on error. + /*! Set up CD-ROM for reading using the Apple OSX driver. The + device_name is the some sort of device name. + + NULL is returned on error or there is no OSX driver. + + @see cdio_open_cd + @see cdio_open */ CdIo * cdio_open_osx (const char *source_name); + /*! Return a string containing the default device name that the + OSX driver would use when none is specified. A scan is made + for CD-ROM drives with CDs in them. + + NULL is returned on error or there is no CD-ROM device + */ char * cdio_get_default_device_osx(void); + /*! Return a list of all of the CD-ROM devices that the OSX driver + can find. + */ char **cdio_get_devices_osx(void); - /*! Win32 CD-reading routines. - NULL is returned on error. + /*! Set up CD-ROM for reading using the Microsoft Windows driver. The + device_name is the some sort of device name. + + NULL is returned on error or there is no Microsof Windows driver. */ CdIo * cdio_open_win32 (const char *source_name); + /*! Return a string containing the default device name that the + Win32 driver would use when none is specified. A scan is made + for CD-ROM drives with CDs in them. + + NULL is returned on error or there is no CD-ROM device. + + @see cdio_open_cd + @see cdio_open + */ char * cdio_get_default_device_win32(void); char **cdio_get_devices_win32(void); - /*! Nero CD disk-image routines. - NULL is returned on error. + /*! Set up CD-ROM for reading using the Nero driver. The + device_name is the some sort of device name. + + NULL is returned on error or there is no Nero driver. */ CdIo * cdio_open_nrg (const char *source_name); + /*! Return a string containing the default device name that the + NRG driver would use when none is specified. A scan is made + for NRG disk images in the current directory.. + + NULL is returned on error or there is no CD-ROM device. + */ char * cdio_get_default_device_nrg(void); char **cdio_get_devices_nrg(void); diff --git a/src/input/vcd/libcdio/cdio/iso9660.h b/src/input/vcd/libcdio/cdio/iso9660.h index c2cabe5fc..78f78ef81 100644 --- a/src/input/vcd/libcdio/cdio/iso9660.h +++ b/src/input/vcd/libcdio/cdio/iso9660.h @@ -1,8 +1,8 @@ /* - $Id: iso9660.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: iso9660.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein See also iso9660.h by Eric Youngdale (1993). @@ -23,9 +23,9 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* - * Header file iso9660.h - assorted structure definitions and typecasts. - specific to iso9660 filesystem. +/*! + * \file iso9660.h + * \brief Header for libiso9660: the ISO-9660 filesystem library. */ @@ -42,16 +42,19 @@ #define MIN_TRACK_SIZE 4*75 #define MIN_ISO_SIZE MIN_TRACK_SIZE -/* - A ISO filename is: "abcde.eee;1" -> '.' ';' - - The maximum needed string length is: +/*! + An ISO filename is: "abcde.eee;1" -> '.' ';' + + For ISO-9660 Level 1, the maximum needed string length is: + +\verbatim 30 chars (filename + ext) + 2 chars ('.' + ';') - + strlen("32767") - + null byte + + 5 chars (strlen("32767")) + + 1 null byte ================================ - = 38 chars + = 38 chars +\endverbatim */ #define LEN_ISONAME 31 #define MAX_ISONAME 37 @@ -61,26 +64,28 @@ /* * ISO 9660 directory flags. */ -#define ISO_FILE 0 /* Not really a flag... */ -#define ISO_EXISTENCE 1 /* Do not make existence known (hidden) */ -#define ISO_DIRECTORY 2 /* This file is a directory */ -#define ISO_ASSOCIATED 4 /* This file is an assiciated file */ -#define ISO_RECORD 8 /* Record format in extended attr. != 0 */ -#define ISO_PROTECTION 16 /* No read/execute perm. in ext. attr. */ -#define ISO_DRESERVED1 32 /* Reserved bit 5 */ -#define ISO_DRESERVED2 64 /* Reserved bit 6 */ -#define ISO_MULTIEXTENT 128 /* Not final entry of a mult. ext. file */ +#define ISO_FILE 0 /**< Not really a flag... */ +#define ISO_EXISTENCE 1 /**< Do not make existence known (hidden) */ +#define ISO_DIRECTORY 2 /**< This file is a directory */ +#define ISO_ASSOCIATED 4 /**< This file is an associated file */ +#define ISO_RECORD 8 /**< Record format in extended attr. != 0 */ +#define ISO_PROTECTION 16 /**< No read/execute perm. in ext. attr. */ +#define ISO_DRESERVED1 32 /**< Reserved bit 5 */ +#define ISO_DRESERVED2 64 /**< Reserved bit 6 */ +#define ISO_MULTIEXTENT 128 /**< Not final entry of a mult. ext. file */ /* Volume descriptor types */ #define ISO_VD_PRIMARY 1 -#define ISO_VD_SUPPLEMENTARY 2 /* Used by Joliet */ +#define ISO_VD_SUPPLEMENTARY 2 /**< Used by Joliet */ #define ISO_VD_END 255 -#define ISO_PVD_SECTOR 16 -#define ISO_EVD_SECTOR 17 +#define ISO_PVD_SECTOR 16 /**< Sector of Primary Volume Descriptor */ +#define ISO_EVD_SECTOR 17 /**< Sector of End Volume Descriptor */ -#define ISO_STANDARD_ID "CD001" -#define ISO_BLOCKSIZE 2048 +#define ISO_STANDARD_ID "CD001" /**< String inside track identifying an + ISO 9660 filesystem. */ +#define ISO_BLOCKSIZE 2048 /**< Number of bytes in an ISO + 9660 block */ #ifdef __cplusplus extern "C" { @@ -95,64 +100,95 @@ enum strncpy_pad_check { PRAGMA_BEGIN_PACKED +/*! + \brief ISO-9660 shorter-format time structure. + + @see iso9660_dtime + */ struct iso9660_dtime { uint8_t dt_year; - uint8_t dt_month; /* 1..12. Note 1 origin not 0, like a tm struct. */ + uint8_t dt_month; /**< Has value in range 1..12. Note starts + at 1, not 0 like a tm struct. */ uint8_t dt_day; uint8_t dt_hour; uint8_t dt_minute; uint8_t dt_second; - int8_t dt_gmtoff; /* GMT values -48 .. + 52 in 15 min intervals */ + int8_t dt_gmtoff; /**< GMT values -48 .. + 52 in 15 minute + intervals */ } GNUC_PACKED; typedef struct iso9660_dtime iso9660_dtime_t; +/*! + \brief ISO-9660 longer-format time structure. + + @see iso9660_ltime + */ struct iso9660_ltime { - char lt_year [_delta( 1, 4)]; /* Add 1900 for Julian year */ - char lt_month [_delta( 5, 6)]; /* 1..12. Note starts at 1. */ + char lt_year [_delta( 1, 4)]; /**< Add 1900 to value + for the Julian + year */ + char lt_month [_delta( 5, 6)]; /**< Has value in range + 1..12. Note starts + at 1, not 0 like a + tm struct. */ char lt_day [_delta( 7, 8)]; char lt_hour [_delta( 9, 10)]; char lt_minute [_delta( 11, 12)]; char lt_second [_delta( 13, 14)]; - char lt_hsecond [_delta( 15, 16)]; /* 1/100's of a second */ + char lt_hsecond [_delta( 15, 16)]; /** mydir/file.ext;1 for version + number. For example, mydir/file.ext -> MYDIR/FILE.EXT;1 for version 1. The resulting ISO-9660 pathname is returned. */ char *iso9660_pathname_isofy (const char pathname[], uint16_t version); @@ -336,45 +408,88 @@ iso9660_dir_calc_record_size (unsigned int namelen, unsigned int su_len); /*! Given a directory pointer, find the filesystem entry that contains - lsn and return information about it in stat. + lsn and return information about it. - Returns true if we found an entry with the lsn and false if not. + Returns stat_t of entry if we found lsn, or NULL otherwise. */ -bool iso9660_find_fs_lsn(const CdIo *cdio, lsn_t lsn, - /*out*/ iso9660_stat_t *stat); +iso9660_stat_t *iso9660_find_fs_lsn(const CdIo *cdio, lsn_t lsn); + /*! - Get file status for pathname into stat. As with libc's stat, 0 is returned - if no error and -1 on error. + Given a directory pointer, find the filesystem entry that contains + lsn and return information about it. + + Returns stat_t of entry if we found lsn, or NULL otherwise. + */ +iso9660_stat_t *iso9660_find_ifs_lsn(const iso9660_t *iso, lsn_t lsn); + + +/*! + Get file status for pathname into stat. NULL is returned on error. + */ +iso9660_stat_t *iso9660_fs_stat (const CdIo *obj, const char pathname[], + bool is_mode2); + +/*! + Get file status for pathname into stat. NULL is returned on error. + pathname version numbers in the ISO 9660 + name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names + are lowercased. */ -int iso9660_fs_stat (const CdIo *obj, const char pathname[], - /*out*/ iso9660_stat_t *stat, bool is_mode2); +iso9660_stat_t *iso9660_fs_stat_translate (const CdIo *obj, + const char pathname[], + bool is_mode2); -void * /* list of char* -- caller must free it */ -iso9660_fs_readdir (const CdIo *obj, const char pathname[], bool mode2); +/*! + Get file status for pathname into stat. NULL is returned on error. + */ +void *iso9660_ifs_stat (iso9660_t *iso, const char pathname[]); -uint8_t -iso9660_get_dir_len(const iso9660_dir_t *idr); + +/*! + Get file status for pathname into stat. NULL is returned on error. + pathname version numbers in the ISO 9660 + name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names + are lowercased. + */ +void *iso9660_ifs_stat_translate (iso9660_t *iso, const char pathname[]); + + + +/*! + Read pathname (a directory) and return a list of iso9660_stat_t + of the files inside that. The caller must free the returned result. +*/ +void * iso9660_fs_readdir (const CdIo *obj, const char pathname[], bool mode2); + +/*! + Read pathname (a directory) and return a list of iso9660_stat_t + of the files inside that. The caller must free the returned result. +*/ +void * iso9660_ifs_readdir (iso9660_t *iso, const char pathname[]); + +uint8_t iso9660_get_dir_len(const iso9660_dir_t *idr); #if FIXME -uint8_t -iso9660_get_dir_size(const iso9660_dir_t *idr); +uint8_t iso9660_get_dir_size(const iso9660_dir_t *idr); -lsn_t -iso9660_get_dir_extent(const iso9660_dir_t *idr); +lsn_t iso9660_get_dir_extent(const iso9660_dir_t *idr); #endif -uint8_t -iso9660_get_pvd_type(const iso9660_pvd_t *pvd); +/*! + Return the directory name stored in the iso9660_dir_t + + A string is allocated: the caller must deallocate. +*/ +char * iso9660_dir_to_name (const iso9660_dir_t *iso9660_dir); + +uint8_t iso9660_get_pvd_type(const iso9660_pvd_t *pvd); -const char * -iso9660_get_pvd_id(const iso9660_pvd_t *pvd); +const char * iso9660_get_pvd_id(const iso9660_pvd_t *pvd); -int -iso9660_get_pvd_space_size(const iso9660_pvd_t *pvd); +int iso9660_get_pvd_space_size(const iso9660_pvd_t *pvd); -int -iso9660_get_pvd_block_size(const iso9660_pvd_t *pvd) ; +int iso9660_get_pvd_block_size(const iso9660_pvd_t *pvd) ; /*! Return the primary volume id version number (of pvd). If there is an error 0 is returned. @@ -401,7 +516,9 @@ uint16_t iso9660_pathtable_m_add_entry (void *pt, const char name[], uint32_t extent, uint16_t parent); -/* volume descriptors */ +/*===================================================================== + Volume Descriptors +======================================================================*/ void iso9660_set_pvd (void *pd, const char volume_id[], const char application_id[], diff --git a/src/input/vcd/libcdio/cdio/logging.h b/src/input/vcd/libcdio/cdio/logging.h index 9a600b437..aec981ad4 100644 --- a/src/input/vcd/libcdio/cdio/logging.h +++ b/src/input/vcd/libcdio/cdio/logging.h @@ -1,7 +1,8 @@ /* - $Id: logging.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: logging.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ - Copyright (C) 2000 Herbert Valerio Riedel + Copyright (C) 2000, Herbert Valerio Riedel + Copyright (C) 2003, Rocky Bernstein 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 @@ -17,42 +18,104 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/** \file logging.h + * \brief Header to control logging and level of detail of output. + * + */ #ifndef __LOGGING_H__ #define __LOGGING_H__ #include +/** + * The different log levels supported. + */ typedef enum { - CDIO_LOG_DEBUG = 1, - CDIO_LOG_INFO, - CDIO_LOG_WARN, - CDIO_LOG_ERROR, - CDIO_LOG_ASSERT + CDIO_LOG_DEBUG = 1, /**< Debug-level messages - helps debug what's up. */ + CDIO_LOG_INFO, /**< Informational - indicates perhaps something of + interest. */ + CDIO_LOG_WARN, /**< Warning conditions - something that looks funny. */ + CDIO_LOG_ERROR, /**< Error conditions - may terminate program. */ + CDIO_LOG_ASSERT /**< Critical conditions - may abort program. */ } cdio_log_level_t; -extern int cdio_loglevel_default; +/** + * The place to save the preference concerning how much verbosity + * is desired. This is used by the internal default log handler, but + * it could be use by applications which provide their own log handler. + */ +extern cdio_log_level_t cdio_loglevel_default; -void -cdio_log (cdio_log_level_t level, const char format[], ...) GNUC_PRINTF(2, 3); - +/** + * This type defines the signature of a log handler. For every + * message being logged, the handler will receive the log level and + * the message string. + * + * @see cdio_log_set_handler + * @see cdio_log_level_t + * + * @param level The log level. + * @param message The log message. + */ typedef void (*cdio_log_handler_t) (cdio_log_level_t level, const char message[]); -cdio_log_handler_t -cdio_log_set_handler (cdio_log_handler_t new_handler); +/** + * Set a custom log handler for libcdio. The return value is the log + * handler being replaced. If the provided parameter is NULL, then + * the handler will be reset to the default handler. + * + * @see cdio_log_handler_t + * + * @param new_handler The new log handler. + * @return The previous log handler. + */ +cdio_log_handler_t cdio_log_set_handler (cdio_log_handler_t new_handler); + +/** + * Handle an message with the given log level + * + * @see cdio_debug + * @see cdio_info + * @see cdio_warn + * @see cdio_error -void -cdio_debug (const char format[], ...) GNUC_PRINTF(1,2); + * @param level The log level. + * @param format printf-style format string + * @param ... remaining arguments needed by format string + */ +void cdio_log (cdio_log_level_t level, + const char format[], ...) GNUC_PRINTF(2, 3); + +/** + * Handle a debugging message. + * + * @see cdio_log for a more generic routine + */ +void cdio_debug (const char format[], ...) GNUC_PRINTF(1,2); -void -cdio_info (const char format[], ...) GNUC_PRINTF(1,2); +/** + * Handle an informative message. + * + * @see cdio_log for a more generic routine + */ +void cdio_info (const char format[], ...) GNUC_PRINTF(1,2); -void -cdio_warn (const char format[], ...) GNUC_PRINTF(1,2); +/** + * Handle a warning message. + * + * @see cdio_log for a more generic routine + */ +void cdio_warn (const char format[], ...) GNUC_PRINTF(1,2); -void -cdio_error (const char format[], ...) GNUC_PRINTF(1,2); +/** + * Handle an error message. + * + * @see cdio_log for a more generic routine. + */ +void cdio_error (const char format[], ...) GNUC_PRINTF(1,2); #endif /* __LOGGING_H__ */ diff --git a/src/input/vcd/libcdio/cdio/sector.h b/src/input/vcd/libcdio/cdio/sector.h index 39dd0fefb..939515543 100644 --- a/src/input/vcd/libcdio/cdio/sector.h +++ b/src/input/vcd/libcdio/cdio/sector.h @@ -1,5 +1,5 @@ /* - $Id: sector.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: sector.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel Copyright (C) 2003 Rocky Bernstein @@ -18,8 +18,44 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* - Things related to CDROM layout. Sector sizes, MSFs, LBAs, +/*! + \file sector.h + \brief Things related to CD-ROM layout: tracks, sector sizes, MSFs, LBAs. + + A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336, + 2340, or 2352 bytes long. + + Sector types of the standard CD-ROM data formats: + +\verbatim + format sector type user data size (bytes) + ----------------------------------------------------------------------------- + 1 (Red Book) CD-DA 2352 (CDIO_CD_FRAMESIZE_RAW) + 2 (Yellow Book) Mode1 Form1 2048 (CDIO_CD_FRAMESIZE) + 3 (Yellow Book) Mode1 Form2 2336 (M2RAW_SECTOR_SIZE) + 4 (Green Book) Mode2 Form1 2048 (CDIO_CD_FRAMESIZE) + 5 (Green Book) Mode2 Form2 2328 (2324+4 spare bytes) + + + The layout of the standard CD-ROM data formats: + ----------------------------------------------------------------------------- + - audio (red): | audio_sample_bytes | + | 2352 | + + - data (yellow, mode1): | sync - head - data - EDC - zero - ECC | + | 12 - 4 - 2048 - 4 - 8 - 276 | + + - data (yellow, mode2): | sync - head - data | + | 12 - 4 - 2336 | + + - XA data (green, mode2 form1): | sync - head - sub - data - EDC - ECC | + | 12 - 4 - 8 - 2048 - 4 - 276 | + + - XA data (green, mode2 form2): | sync - head - sub - data - Spare | + | 12 - 4 - 8 - 2324 - 4 | +\endverbatim + + */ #ifndef _CDIO_SECTOR_H_ @@ -34,80 +70,50 @@ #define CDIO_PREGAP_SECTORS 150 #define CDIO_POSTGAP_SECTORS 150 -/* - * A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336, - * 2340, or 2352 bytes long. - -* Sector types of the standard CD-ROM data formats: - * - * format sector type user data size (bytes) - * ----------------------------------------------------------------------------- - * 1 (Red Book) CD-DA 2352 (CDIO_CD_FRAMESIZE_RAW) - * 2 (Yellow Book) Mode1 Form1 2048 (CDIO_CD_FRAMESIZE) - * 3 (Yellow Book) Mode1 Form2 2336 (M2RAW_SECTOR_SIZE) - * 4 (Green Book) Mode2 Form1 2048 (CDIO_CD_FRAMESIZE) - * 5 (Green Book) Mode2 Form2 2328 (2324+4 spare bytes) - * - * - * The layout of the standard CD-ROM data formats: - * ----------------------------------------------------------------------------- - * - audio (red): | audio_sample_bytes | - * | 2352 | - * - * - data (yellow, mode1): | sync - head - data - EDC - zero - ECC | - * | 12 - 4 - 2048 - 4 - 8 - 276 | - * - * - data (yellow, mode2): | sync - head - data | - * | 12 - 4 - 2336 | - * - * - XA data (green, mode2 form1): | sync - head - sub - data - EDC - ECC | - * | 12 - 4 - 8 - 2048 - 4 - 276 | - * - * - XA data (green, mode2 form2): | sync - head - sub - data - Spare | - * | 12 - 4 - 8 - 2324 - 4 | - * - */ - /* Some generally useful CD-ROM information -- mostly based on the above. This is from linux.h - not to slight other OS's. This was the first place I came across such useful stuff. */ -#define CDIO_CD_MINS 74 /* max. minutes per CD, not really a limit */ -#define CDIO_CD_SECS_PER_MIN 60 /* seconds per minute */ -#define CDIO_CD_FRAMES_PER_SEC 75 /* frames per second */ -#define CDIO_CD_SYNC_SIZE 12 /* 12 sync bytes per raw data frame */ -#define CDIO_CD_CHUNK_SIZE 24 /* lowest-level "data bytes piece" */ -#define CDIO_CD_NUM_OF_CHUNKS 98 /* chunks per frame */ -#define CDIO_CD_FRAMESIZE_SUB 96 /* subchannel data "frame" size */ -#define CDIO_CD_HEADER_SIZE 4 /* header (address) bytes per raw data - frame */ -#define CDIO_CD_SUBHEADER_SIZE 8 /* subheader bytes per raw XA data frame */ -#define CDIO_CD_EDC_SIZE 4 /* bytes EDC per most raw data frame types */ -#define CDIO_CD_M1F1_ZERO_SIZE 8 /* bytes zero per yellow book mode 1 frame */ -#define CDIO_CD_ECC_SIZE 276 /* bytes ECC per most raw data frame types */ -#define CDIO_CD_FRAMESIZE 2048 /* bytes per frame, "cooked" mode */ -#define CDIO_CD_FRAMESIZE_RAW 2352/* bytes per frame, "raw" mode */ -#define CDIO_CD_FRAMESIZE_RAWER 2646 /* The maximum possible returned bytes */ -#define CDIO_CD_FRAMESIZE_RAW1 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE) /*2340*/ -#define CDIO_CD_FRAMESIZE_RAW0 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE-CD_HEAD_SIZE) /*2336*/ - -/* "before data" part of raw XA (green, mode2) frame */ +#define CDIO_CD_MINS 74 /**< max. minutes per CD, not really + a limit */ +#define CDIO_CD_SECS_PER_MIN 60 /**< seconds per minute */ +#define CDIO_CD_FRAMES_PER_SEC 75 /**< frames per second */ +#define CDIO_CD_SYNC_SIZE 12 /**< 12 sync bytes per raw data frame */ +#define CDIO_CD_CHUNK_SIZE 24 /**< lowest-level "data bytes piece" */ +#define CDIO_CD_NUM_OF_CHUNKS 98 /**< chunks per frame */ +#define CDIO_CD_FRAMESIZE_SUB 96 /**< subchannel data "frame" size */ +#define CDIO_CD_HEADER_SIZE 4 /**< header (address) bytes per raw + data frame */ +#define CDIO_CD_SUBHEADER_SIZE 8 /**< subheader bytes per raw XA data frame */ +#define CDIO_CD_EDC_SIZE 4 /**< bytes EDC per most raw data + frame types */ +#define CDIO_CD_M1F1_ZERO_SIZE 8 /**< bytes zero per yellow book mode + 1 frame */ +#define CDIO_CD_ECC_SIZE 276 /**< bytes ECC per most raw data frame types */ +#define CDIO_CD_FRAMESIZE 2048 /**< bytes per frame, "cooked" mode */ +#define CDIO_CD_FRAMESIZE_RAW 2352/**< bytes per frame, "raw" mode */ +#define CDIO_CD_FRAMESIZE_RAWER 2646 /**< The maximum possible returned bytes */ +#define CDIO_CD_FRAMESIZE_RAW1 (CDIO_CD_CD_FRAMESIZE_RAW-CDIO_CD_SYNC_SIZE) /*2340*/ +#define CDIO_CD_FRAMESIZE_RAW0 (CDIO_CD_FRAMESIZE_RAW-CDIO_CD_SYNC_SIZE-CDIO_CD__HEAD_SIZE) /*2336*/ + +/*! "before data" part of raw XA (green, mode2) frame */ #define CDIO_CD_XA_HEADER (CDIO_CD_HEADER_SIZE+CDIO_CD_SUBHEADER_SIZE) -/* "after data" part of raw XA (green, mode2 form1) frame */ +/*! "after data" part of raw XA (green, mode2 form1) frame */ #define CDIO_CD_XA_TAIL (CDIO_CD_EDC_SIZE+CDIO_CD_ECC_SIZE) -/* "before data" sync bytes + header of XA (green, mode2) frame */ +/*! "before data" sync bytes + header of XA (green, mode2) frame */ #define CDIO_CD_XA_SYNC_HEADER (CDIO_CD_SYNC_SIZE+CDIO_CD_XA_HEADER) /* CD-ROM address types (Linux cdrom_tocentry.cdte_format) */ -#define CDIO_CDROM_LBA 0x01 /* "logical block": first frame is #0 */ -#define CDIO_CDROM_MSF 0x02 /* "minute-second-frame": binary, not bcd here! */ +#define CDIO_CDROM_LBA 0x01 /**< "logical block": first frame is #0 */ +#define CDIO_CDROM_MSF 0x02 /**< "minute-second-frame": binary, not + BCD here! */ #define CDIO_CDROM_DATA_TRACK 0x04 -/* The leadout track is always 0xAA, regardless of # of tracks on disc */ +/*! The leadout track is always 0xAA, regardless of # of tracks on disc */ #define CDIO_CDROM_LEADOUT_TRACK 0xAA #define M2F2_SECTOR_SIZE 2324 @@ -129,20 +135,40 @@ #define msf_t_SIZEOF 3 -/* warning, returns new allocated string */ +/*! + Convert an LBA into a string representation of the MSF. + \warning cdio_lba_to_msf_str returns new allocated string */ char *cdio_lba_to_msf_str (lba_t lba); +/*! + Convert an LBA into the corresponding LSN. +*/ lba_t cdio_lba_to_lsn (lba_t lba); +/*! + Convert an LBA into the corresponding MSF. +*/ void cdio_lba_to_msf(lba_t lba, msf_t *msf); +/*! + Convert an LSN into the corresponding LBA. +*/ lba_t cdio_lsn_to_lba (lsn_t lsn); +/*! + Convert an LSN into the corresponding MSF. +*/ void cdio_lsn_to_msf (lsn_t lsn, msf_t *msf); +/*! + Convert a MSF into the corresponding LBA. +*/ lba_t cdio_msf_to_lba (const msf_t *msf); +/*! + Convert a MSF into the corresponding LSN. +*/ lsn_t cdio_msf_to_lsn (const msf_t *msf); diff --git a/src/input/vcd/libcdio/cdio/types.h b/src/input/vcd/libcdio/cdio/types.h index e3c3ff3e5..c4b0b0934 100644 --- a/src/input/vcd/libcdio/cdio/types.h +++ b/src/input/vcd/libcdio/cdio/types.h @@ -1,8 +1,8 @@ /* - $Id: types.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: types.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel - Copyright (C) 2002,2003 Rocky Bernstein + Copyright (C) 2002, 2003, 2004 Rocky Bernstein 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 @@ -18,6 +18,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/** \file types.h + * \brief Common type definitions used pervasively in libcdio. + */ + #ifndef __CDIO_TYPES_H__ #define __CDIO_TYPES_H__ @@ -54,10 +59,6 @@ extern "C" { # undef INT64_C #endif -#if defined (MINGW32) - typedef int ssize_t; -#endif - /* if it's still not defined, take a good guess... should work for most 32bit and 64bit archs */ @@ -172,14 +173,23 @@ extern "C" { /* our own offsetof()-like macro */ #define __cd_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) - /* In many structures on the disk a sector address is stored as a - BCD-encoded mmssff in three bytes. */ + /*! + \brief MSF (minute/second/frame) structure + + One CD-ROMs addressing scheme especially used in audio formats + (Red Book) is an address by minute, sector and frame which + BCD-encoded in three bytes. An alternative format is an lba_t. + + @see lba_t + */ PRAGMA_BEGIN_PACKED - typedef struct { + struct msf_rec { uint8_t m, s, f; - } GNUC_PACKED msf_t; + } GNUC_PACKED; PRAGMA_END_PACKED + typedef struct msf_rec msf_t; + #define msf_t_SIZEOF 3 /* type used for bit-fields in structs (1 <= bits <= 8) */ @@ -194,13 +204,16 @@ extern "C" { typedef uint8_t bitfield_t; #endif - /* The type of a Logical Block Address. */ + /*! The type of a Logical Block Address. + + @see msf_t + */ typedef uint32_t lba_t; - /* The type of an Logical Sector Number. */ + /*! The type of an Logical Sector Number. */ typedef uint32_t lsn_t; - /* The type of an track number 0..99. */ + /*! The type of an track number 0..99. */ typedef uint8_t track_t; /*! diff --git a/src/input/vcd/libcdio/cdio/util.h b/src/input/vcd/libcdio/cdio/util.h index 147a32365..5142653ad 100644 --- a/src/input/vcd/libcdio/cdio/util.h +++ b/src/input/vcd/libcdio/cdio/util.h @@ -1,5 +1,5 @@ /* - $Id: util.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: util.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel @@ -21,6 +21,12 @@ #ifndef __CDIO_UTIL_H__ #define __CDIO_UTIL_H__ +/*! + \file util.h + \brief Miscellaneous utility functions. + + Warning: this will probably get removed/replaced by using glib.h +*/ #include #undef MAX diff --git a/src/input/vcd/libcdio/cdio/version.h b/src/input/vcd/libcdio/cdio/version.h index c8bbce698..345924cab 100644 --- a/src/input/vcd/libcdio/cdio/version.h +++ b/src/input/vcd/libcdio/cdio/version.h @@ -1 +1,10 @@ -#define CDIO_VERSION "0.64-cvs" +/* $Id: version.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ */ +/** \file version.h + * \brief A file simply containing the library version number. + */ + +/*! CDIO_VERSION can as a string in programs to show what version is used. */ +#define CDIO_VERSION "0.68" + +/*! LIBCDIO_VERSION_NUM can be used for testing in the C preprocessor */ +#define LIBCDIO_VERSION_NUM 68 diff --git a/src/input/vcd/libcdio/cdio/xa.h b/src/input/vcd/libcdio/cdio/xa.h index 06e5df250..5ff7addd4 100644 --- a/src/input/vcd/libcdio/cdio/xa.h +++ b/src/input/vcd/libcdio/cdio/xa.h @@ -1,8 +1,8 @@ /* - $Id: xa.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: xa.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein See also iso9660.h by Eric Youngdale (1993) and in cdrtools. These are @@ -24,6 +24,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +/*! + \file xa.h + \brief Things related to the ISO-9660 XA (Extended Attributes) format +*/ + #ifndef __CDIO_XA_H__ #define __CDIO_XA_H__ @@ -34,17 +39,17 @@ #define ISO_XA_MARKER_OFFSET 1024 /* XA attribute definitions */ -#define XA_PERM_RSYS 0x0001 /* System Group Read */ -#define XA_PERM_XSYS 0x0004 /* System Group Execute */ +#define XA_PERM_RSYS 0x0001 /**< System Group Read */ +#define XA_PERM_XSYS 0x0004 /**< System Group Execute */ -#define XA_PERM_RUSR 0x0010 /* User (owner) Read */ -#define XA_PERM_XUSR 0x0040 /* User (owner) Execute */ +#define XA_PERM_RUSR 0x0010 /**< User (owner) Read */ +#define XA_PERM_XUSR 0x0040 /**< User (owner) Execute */ -#define XA_PERM_RGRP 0x0100 /* Group Read */ -#define XA_PERM_XGRP 0x0400 /* Group Execute */ +#define XA_PERM_RGRP 0x0100 /**< Group Read */ +#define XA_PERM_XGRP 0x0400 /**< Group Execute */ -#define XA_PERM_ROTH 0x1000 /* Other (world) Read */ -#define XA_PERM_XOTH 0x4000 /* Other (world) Execute */ +#define XA_PERM_ROTH 0x1000 /**< Other (world) Read */ +#define XA_PERM_XOTH 0x4000 /**< Other (world) Execute */ #define XA_ATTR_MODE2FORM1 (1 << 11) #define XA_ATTR_MODE2FORM2 (1 << 12) @@ -61,18 +66,37 @@ #define XA_FORM1_FILE (XA_ATTR_MODE2FORM1 | XA_PERM_ALL_ALL) #define XA_FORM2_FILE (XA_ATTR_MODE2FORM2 | XA_PERM_ALL_ALL) -/* - * Extended Attributes record according to Yellow Book. - */ -typedef struct iso9660_xa /* big endian!! */ +/*! \brief "Extended Architecture according t the Philips Yellow Book. + +CD-ROM EXtended Architecture is a modification to the CD-ROM +specification that defines two new types of sectors. CD-ROM XA was +developed jointly by Sony, Philips, and Microsoft, and announced in +August 1988. Its specifications were published in an extension to the +Yellow Book. CD-i, Photo CD, Video CD and CD-EXTRA have all +subsequently been based on CD-ROM XA. + +CD-XA defines another way of formatting sectors on a CD-ROM, including +headers in the sectors that describe the type (audio, video, data) and +some additional info (markers, resolution in case of a video or audio +sector, file numbers, etc). + +The data written on a CD-XA is consistent with and can be in ISO-9660 +file system format and therefore be readable by ISO-9660 file system +translators. But also a CD-I player can also read CD-XA discs even if +its own `Green Book' file system only resembles ISO 9660 and isn't +fully compatible. + + Note structure is big-endian. +*/ +typedef struct iso9660_xa { - uint16_t group_id; /* 0 */ - uint16_t user_id; /* 0 */ - uint16_t attributes; /* XA_ATTR_ */ - uint8_t signature[2]; /* { 'X', 'A' } */ - uint8_t filenum; /* file number, see also XA subheader */ - uint8_t reserved[5]; /* zero */ -} iso9660_xa_t GNUC_PACKED; + uint16_t group_id; /**< 0 */ + uint16_t user_id; /**< 0 */ + uint16_t attributes; /**< XA_ATTR_ */ + uint8_t signature[2]; /**< { 'X', 'A' } */ + uint8_t filenum; /**< file number, see also XA subheader */ + uint8_t reserved[5]; /**< zero */ +} GNUC_PACKED iso9660_xa_t; /*! @@ -106,6 +130,12 @@ typedef struct iso9660_xa /* big endian!! */ const char * iso9660_get_xa_attr_str (uint16_t xa_attr); +/*! + Allocates and initalizes a new iso9600_xa_t variable and returns + it. The caller should free the returned result. + + @see iso9660_xa +*/ iso9660_xa_t * iso9660_xa_init (iso9660_xa_t *_xa, uint16_t uid, uint16_t gid, uint16_t attr, uint8_t filenum); diff --git a/src/input/vcd/libcdio/cdio_assert.h b/src/input/vcd/libcdio/cdio_assert.h index 03ea16763..2433bf0c7 100644 --- a/src/input/vcd/libcdio/cdio_assert.h +++ b/src/input/vcd/libcdio/cdio_assert.h @@ -1,5 +1,5 @@ /* - $Id: cdio_assert.h,v 1.1 2003/10/13 11:47:11 f1rmb Exp $ + $Id: cdio_assert.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel diff --git a/src/input/vcd/libcdio/cdio_private.h b/src/input/vcd/libcdio/cdio_private.h index 85c92946d..c1106e694 100644 --- a/src/input/vcd/libcdio/cdio_private.h +++ b/src/input/vcd/libcdio/cdio_private.h @@ -1,7 +1,7 @@ /* - $Id: cdio_private.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: cdio_private.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein 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 @@ -146,7 +146,7 @@ extern "C" { from lsn. Returns 0 if no error. */ int (*read_mode2_sector) (void *env, void *buf, lsn_t lsn, - bool mode2raw); + bool mode2_form2); /*! Reads nblocks of mode2 sectors from cd device into data starting @@ -154,7 +154,22 @@ extern "C" { Returns 0 if no error. */ int (*read_mode2_sectors) (void *env, void *buf, lsn_t lsn, - bool mode2raw, unsigned int nblocks); + bool mode2_form2, unsigned int nblocks); + + /*! + Reads a single mode1 sector from cd device into buf starting + from lsn. Returns 0 if no error. + */ + int (*read_mode1_sector) (void *env, void *buf, lsn_t lsn, + bool mode1_form2); + + /*! + Reads nblocks of mode1 sectors from cd device into data starting + from lsn. + Returns 0 if no error. + */ + int (*read_mode1_sectors) (void *env, void *buf, lsn_t lsn, + bool mode1_form2, unsigned int nblocks); /*! Set the arg "key" with "value" in the source device. @@ -275,7 +290,7 @@ extern "C" { /*! Release and free resources associated with stream or disk image. */ - void cdio_generic_stream_free (void *env); + void cdio_generic_stdio_free (void *env); /*! Return true if source_name could be a device containing a CD-ROM on diff --git a/src/input/vcd/libcdio/ds.c b/src/input/vcd/libcdio/ds.c index 7cc62a8f9..bc015d79f 100644 --- a/src/input/vcd/libcdio/ds.c +++ b/src/input/vcd/libcdio/ds.c @@ -1,5 +1,5 @@ /* - $Id: ds.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: ds.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel @@ -30,7 +30,7 @@ #include #include "cdio_assert.h" -static const char _rcsid[] = "$Id: ds.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $"; +static const char _rcsid[] = "$Id: ds.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; struct _CdioList { diff --git a/src/input/vcd/libcdio/ds.h b/src/input/vcd/libcdio/ds.h index 046a5b0b0..24805840e 100644 --- a/src/input/vcd/libcdio/ds.h +++ b/src/input/vcd/libcdio/ds.h @@ -1,5 +1,5 @@ /* - $Id: ds.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: ds.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel diff --git a/src/input/vcd/libcdio/image/bincue.c b/src/input/vcd/libcdio/image/bincue.c new file mode 100644 index 000000000..d0ca9b240 --- /dev/null +++ b/src/input/vcd/libcdio/image/bincue.c @@ -0,0 +1,1019 @@ +/* + $Id: bincue.c,v 1.1 2004/04/11 12:20:32 miguelfreitas Exp $ + + Copyright (C) 2001 Herbert Valerio Riedel + Copyright (C) 2002, 2003, 2004 Rocky Bernstein + + 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.1 2004/04/11 12:20:32 miguelfreitas Exp $"; + +#include "cdio_assert.h" +#include "cdio_private.h" +#include "_cdio_stdio.h" + +#include +#include +#include + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STRINGS_H +#include +#endif +#ifdef HAVE_GLOB_H +#include +#endif +#include + +/* FIXME: should put in a common definition somewhere. */ +#ifdef HAVE_MEMSET +#define BZERO(ptr, size) memset(ptr, 0, size) +#elif HAVE_BZERO +#define BZERO(ptr, size) bzero(ptr, size) +#else + Error -- you need either memset or bzero +#endif + +/* reader */ + +#define DEFAULT_CDIO_DEVICE "videocd.bin" +#define DEFAULT_CDIO_CUE "videocd.cue" + +typedef struct { + track_t track_num; /* Probably is index+1 */ + msf_t start_msf; + lba_t start_lba; + int start_index; + int sec_count; /* Number of sectors in this track. Does not + include pregap */ + int num_indices; + int flags; /* "DCP", "4CH", "PRE" */ + track_format_t track_format; + bool track_green; + uint16_t datasize; /* How much is in the portion we return back? */ + uint16_t datastart; /* Offset from begining that data starts */ + uint16_t endsize; /* How much stuff at the end to skip over. This + stuff may have error correction (EDC, or ECC).*/ + uint16_t blocksize; /* total block size = start + size + end */ + + +} track_info_t; + +typedef struct { + /* Things common to all drivers like this. + This must be first. */ + generic_img_private_t gen; + internal_position_t pos; + + bool sector_2336; /* Playstation (PSX) uses 2336-byte sectors */ + + char *cue_name; + char *mcn; /* Media catalog number. */ + track_info_t tocent[100]; /* entry info for each track */ + track_t total_tracks; /* number of tracks in image */ + track_t first_track_num; /* track number of first track */ + bool have_cue; +} _img_private_t; + +static bool _cdio_image_read_cue (_img_private_t *_obj); +static uint32_t _cdio_stat_size (void *env); + +/*! + Initialize image structures. + */ +static bool +_cdio_init (_img_private_t *_obj) +{ + lsn_t lead_lsn; + + if (_obj->gen.init) + return false; + + if (!(_obj->gen.data_source = cdio_stdio_new (_obj->gen.source_name))) { + cdio_warn ("init failed"); + return false; + } + + /* Have to set init before calling _cdio_stat_size() or we will + get into infinite recursion calling passing right here. + */ + _obj->gen.init = true; + + lead_lsn = _cdio_stat_size( (_img_private_t *) _obj); + + if (-1 == lead_lsn) + return false; + + /* Read in CUE sheet. */ + if ((_obj->cue_name != NULL)) { + _obj->have_cue = _cdio_image_read_cue(_obj); + } + + if (!_obj->have_cue ) { + /* Time to fake things... + Make one big track, track 0 and 1 are the same. + We are guessing stuff starts at msf 00:04:00 - 2 for the 150 + sector pregap and 2 for the cue information. + */ + track_info_t *this_track=&(_obj->tocent[0]); + int blocksize = _obj->sector_2336 + ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; + + _obj->total_tracks = 2; + _obj->first_track_num = 1; + this_track->start_msf.m = to_bcd8(0); + this_track->start_msf.s = to_bcd8(4); + this_track->start_msf.f = to_bcd8(0); + this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf); + this_track->blocksize = blocksize; + this_track->track_format= TRACK_FORMAT_XA; + this_track->track_green = true; + + + _obj->tocent[1] = _obj->tocent[0]; + } + + /* Fake out leadout track and sector count for last track*/ + cdio_lsn_to_msf (lead_lsn, &_obj->tocent[_obj->total_tracks].start_msf); + _obj->tocent[_obj->total_tracks].start_lba = cdio_lsn_to_lba(lead_lsn); + _obj->tocent[_obj->total_tracks-1].sec_count = + cdio_lsn_to_lba(lead_lsn - _obj->tocent[_obj->total_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 +_cdio_lseek (void *env, off_t offset, int whence) +{ + _img_private_t *_obj = env; + + /* 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; + + _obj->pos.lba = 0; + for (i=0; i<_obj->total_tracks; i++) { + track_info_t *this_track=&(_obj->tocent[i]); + _obj->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; + _obj->pos.buff_offset = rem; + _obj->pos.lba += blocks; + break; + } + real_offset += this_track->sec_count*this_track->blocksize; + offset -= this_track->sec_count*this_track->datasize; + _obj->pos.lba += this_track->sec_count; + } + + if (i==_obj->total_tracks) { + cdio_warn ("seeking outside range of disk image"); + return -1; + } else { + real_offset += _obj->tocent[i].datastart; + return cdio_stream_seek(_obj->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 +_cdio_read (void *env, void *data, size_t size) +{ + _img_private_t *_obj = env; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + char *p = data; + ssize_t final_size=0; + ssize_t this_size; + track_info_t *this_track=&(_obj->tocent[_obj->pos.index]); + ssize_t skip_size = this_track->datastart + this_track->endsize; + + while (size > 0) { + int rem = this_track->datasize - _obj->pos.buff_offset; + if (size <= rem) { + this_size = cdio_stream_read(_obj->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(_obj->gen.data_source, buf, rem, 1); + final_size += this_size; + memcpy (p, buf, this_size); + p += this_size; + this_size = cdio_stream_read(_obj->gen.data_source, buf, rem, 1); + + /* Skip over stuff at end of this sector and the beginning of the next. + */ + cdio_stream_read(_obj->gen.data_source, buf, skip_size, 1); + + /* Get ready to read another sector. */ + _obj->pos.buff_offset=0; + _obj->pos.lba++; + + /* Have gone into next track. */ + if (_obj->pos.lba >= _obj->tocent[_obj->pos.index+1].start_lba) { + _obj->pos.index++; + this_track=&(_obj->tocent[_obj->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 +_cdio_stat_size (void *env) +{ + _img_private_t *_obj = env; + long size; + int blocksize = _obj->sector_2336 + ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; + + _cdio_init (_obj); + + size = cdio_stream_stat (_obj->gen.data_source); + + if (size % blocksize) + { + cdio_warn ("image %s size (%ld) not multiple of blocksize (%d)", + _obj->gen.source_name, size, blocksize); + 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 /= blocksize; + + return size; +} + +#define MAXLINE 512 + +static bool +_cdio_image_read_cue (_img_private_t *_obj) +{ + FILE *fp; + char line[MAXLINE]; + + int track_num; + int min,sec,frame; + int blocksize; + int start_index; + bool seen_first_index_for_track=false; + + if ( _obj == NULL || _obj->cue_name == NULL ) return false; + + fp = fopen (_obj->cue_name, "r"); + if (fp == NULL) return false; + + _obj->total_tracks=0; + _obj->first_track_num=1; + _obj->mcn=NULL; + + while ((fgets(line, MAXLINE, fp)) != NULL) { + char s[80]; + char *p; + /*printf("Retrieved line of length %zu :\n", read); + printf("%s", line); */ + for (p=line; isspace(*p); p++) ; + if (1==sscanf(p, "FILE \"%80s[^\"]", s)) { + /* Should expand file name based on cue file basename. + free(_obj->bin_file); + _obj->bin_file = strdup(s); + */ + /* printf("Found file name %s\n", s); */ + } else if (1==sscanf(p, "CATALOG %80s", s)) { + _obj->mcn = strdup(s); + } else if (2==sscanf(p, "TRACK %d MODE2/%d", &track_num, &blocksize)) { + track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]); + this_track->track_num = track_num; + this_track->num_indices = 0; + this_track->track_format= TRACK_FORMAT_XA; + this_track->track_green = true; + _obj->total_tracks++; + seen_first_index_for_track=false; + /*printf("Added track %d with blocksize %d\n", track_num, blocksize);*/ + + this_track->blocksize = blocksize; + switch(blocksize) { + case 2336: + this_track->datastart = CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE; + this_track->datasize = M2RAW_SECTOR_SIZE; + this_track->endsize = 0; + break; + default: + cdio_warn ("Unknown MODE2 size %d. Assuming 2352", blocksize); + case 2352: + if (_obj->sector_2336) { + this_track->datastart = 0; + this_track->datasize = M2RAW_SECTOR_SIZE; + this_track->endsize = blocksize - 2336; + } 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; + } + + } else if (2==sscanf(p, "TRACK %d MODE1/%d", &track_num, &blocksize)) { + track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]); + this_track->blocksize = blocksize; + switch(blocksize) { + case 2048: + /* Is the below correct? */ + this_track->datastart = 0; + this_track->datasize = CDIO_CD_FRAMESIZE; + this_track->endsize = 0; + break; + default: + cdio_warn ("Unknown MODE1 size %d. Assuming 2352", blocksize); + case 2352: + 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->track_num = track_num; + this_track->num_indices = 0; + this_track->track_format = TRACK_FORMAT_DATA; + this_track->track_green = false; + _obj->total_tracks++; + seen_first_index_for_track=false; + /*printf("Added track %d with blocksize %d\n", track_num, blocksize);*/ + + } else if (1==sscanf(p, "TRACK %d AUDIO", &track_num)) { + track_info_t *this_track=&(_obj->tocent[_obj->total_tracks]); + 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_num = track_num; + this_track->num_indices = 0; + this_track->track_format = TRACK_FORMAT_AUDIO; + this_track->track_green = false; + _obj->total_tracks++; + seen_first_index_for_track=false; + + } else if (4==sscanf(p, "INDEX %d %d:%d:%d", + &start_index, &min, &sec, &frame)) { + track_info_t *this_track=&(_obj->tocent[_obj->total_tracks-1]); + /* FIXME! all of this is a big hack. + If start_index == 0, then this is the "last_cue" information. + The +2 below seconds is to adjust for the 150 pregap. + */ + if (start_index != 0) { + if (!seen_first_index_for_track) { + this_track->start_index = start_index; + sec += 2; + if (sec >= 60) { + min++; + sec -= 60; + } + this_track->start_msf.m = to_bcd8 (min); + this_track->start_msf.s = to_bcd8 (sec); + this_track->start_msf.f = to_bcd8 (frame); + this_track->start_lba = cdio_msf_to_lba(&this_track->start_msf); + seen_first_index_for_track=true; + } + + if (_obj->total_tracks > 1) { + /* Figure out number of sectors for previous track */ + track_info_t *prev_track=&(_obj->tocent[_obj->total_tracks-2]); + if ( this_track->start_lba < prev_track->start_lba ) { + cdio_warn("track %d at LBA %lu starts before track %d at LBA %lu", + _obj->total_tracks, + (unsigned long int) this_track->start_lba, + _obj->total_tracks-1, + (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_warn ("%lu fewer than pregap (%d) sectors in track %d", + (long unsigned int) + this_track->start_lba - prev_track->start_lba, + CDIO_PREGAP_SECTORS, + _obj->total_tracks-1); + /* 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++; + } + } + } + _obj->have_cue = _obj->total_tracks != 0; + + fclose (fp); + return true; +} + +/*! + Reads a single audio sector from CD device into data starting + from lsn. Returns 0 if no error. + */ +static int +_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, + unsigned int nblocks) +{ + _img_private_t *_obj = env; + int ret; + + _cdio_init (_obj); + + /* Why the adjustment of 272, I don't know. It seems to work though */ + if (lsn != 0) { + ret = cdio_stream_seek (_obj->gen.data_source, + (lsn * CDIO_CD_FRAMESIZE_RAW) - 272, SEEK_SET); + if (ret!=0) return ret; + + ret = cdio_stream_read (_obj->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 (_obj->gen.data_source, 0, SEEK_SET); + + if (ret!=0) return ret; + + ret = cdio_stream_read (_obj->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 +_cdio_read_mode1_sector (void *env, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *_obj = env; + int ret; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + int blocksize = _obj->sector_2336 + ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; + + _cdio_init (_obj); + + ret = cdio_stream_seek (_obj->gen.data_source, lsn * blocksize, SEEK_SET); + if (ret!=0) return ret; + + /* FIXME: Not completely sure the below is correct. */ + ret = cdio_stream_read (_obj->gen.data_source, + _obj->sector_2336 + ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE) + : buf, + blocksize, 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 +_cdio_read_mode1_sectors (void *env, void *data, uint32_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *_obj = env; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode1_sector (_obj, + ((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 +_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *_obj = env; + 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 = _obj->sector_2336 + ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE_RAW; + + _cdio_init (_obj); + + ret = cdio_stream_seek (_obj->gen.data_source, lsn * blocksize, SEEK_SET); + if (ret!=0) return ret; + + ret = cdio_stream_read (_obj->gen.data_source, + _obj->sector_2336 + ? (buf + CDIO_CD_SYNC_SIZE + CDIO_CD_HEADER_SIZE) + : buf, + blocksize, 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 +_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn, + bool b_form2, unsigned int nblocks) +{ + _img_private_t *_obj = env; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode2_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +#define free_if_notnull(obj) \ + if (NULL != obj) { free(obj); obj=NULL; }; + +static void +_cdio_bincue_destroy (void *obj) +{ + _img_private_t *env = obj; + + if (NULL == env) return; + free_if_notnull(env->mcn); + free_if_notnull(env->cue_name); + cdio_generic_stdio_free(env); + free(env); +} + +/*! + Set the arg "key" with "value" in the source device. + Currently "source" to set the source device in I/O operations + is the only valid key. + + 0 is returned if no error was found, and nonzero if there as an error. +*/ +static int +_cdio_set_arg (void *env, const char key[], const char value[]) +{ + _img_private_t *_obj = env; + + if (!strcmp (key, "source")) + { + free_if_notnull (_obj->gen.source_name); + + if (!value) + return -2; + + _obj->gen.source_name = strdup (value); + } + else if (!strcmp (key, "sector")) + { + if (!strcmp (value, "2336")) + _obj->sector_2336 = true; + else if (!strcmp (value, "2352")) + _obj->sector_2336 = false; + else + return -2; + } + else if (!strcmp (key, "cue")) + { + free_if_notnull (_obj->cue_name); + + if (!value) + return -2; + + _obj->cue_name = strdup (value); + } + else + return -1; + + return 0; +} + +/*! + Return the value associated with the key "arg". +*/ +static const char * +_cdio_get_arg (void *env, const char key[]) +{ + _img_private_t *_obj = env; + + if (!strcmp (key, "source")) { + return _obj->gen.source_name; + } else if (!strcmp (key, "cue")) { + return _obj->cue_name; + } + return NULL; +} + +/*! + Return an array of strings giving possible NRG 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; ifirst_track_num; +} + +/*! + Return the media catalog number (MCN) from the CD or NULL if there + is none or we don't have the ability to get it. + + Note: string is malloc'd so caller has to free() the returned + string when done with it. + */ +static char * +_cdio_get_mcn(void *env) +{ + _img_private_t *_obj = env; + + _cdio_init (_obj); + + if (NULL == _obj->mcn) return NULL; + return strdup(_obj->mcn); +} + +/*! + Return the number of tracks in the current medium. + If no cuesheet is available, We fake it an just say there's + one big track. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_t +_cdio_get_num_tracks(void *env) +{ + _img_private_t *_obj = env; + _cdio_init (_obj); + + return _obj->have_cue && _obj->total_tracks > 0 ? _obj->total_tracks : 1; +} + +/*! + Return the number of tracks in the current medium. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_format_t +_cdio_get_track_format(void *env, track_t track_num) +{ + _img_private_t *_obj = env; + + if (!_obj->gen.init) _cdio_init(_obj); + + if (track_num > _obj->total_tracks || track_num == 0) + return TRACK_FORMAT_ERROR; + + return _obj->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 +_cdio_get_track_green(void *env, track_t track_num) +{ + _img_private_t *_obj = env; + + if (!_obj->gen.init) _cdio_init(_obj); + + if (track_num > _obj->total_tracks || track_num == 0) + return false; + + return _obj->tocent[track_num-1].track_green; +} + +/*! + Return the starting LSN track number + track_num in obj. 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. +*/ +static lba_t +_cdio_get_track_lba(void *env, track_t track_num) +{ + _img_private_t *_obj = env; + _cdio_init (_obj); + + if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; + + if (track_num <= _obj->total_tracks+1 && track_num != 0) { + return _obj->tocent[track_num-1].start_lba; + } else + return CDIO_INVALID_LBA; +} + +/*! + Return the starting MSF (minutes/secs/frames) for the track number + track_num in obj. Tracks numbers start at 1. + The "leadout" track is specified either by + using track_num LEADOUT_TRACK or the total tracks+1. + +*/ +static bool +_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf) +{ + _img_private_t *_obj = env; + _cdio_init (_obj); + + if (NULL == msf) return false; + + if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; + + if (track_num <= _obj->total_tracks+1 && track_num != 0) { + *msf = _obj->tocent[track_num-1].start_msf; + return true; + } else + return false; +} + +/*! + Return corresponding BIN file if cue_name is a cue file or NULL + if not a CUE file. + +*/ +/* Later we'll probably parse the entire file. For now though, this gets us + started for now. +*/ +char * +cdio_is_cuefile(const char *cue_name) +{ + int i; + char *bin_name; + + if (cue_name == NULL) return false; + + bin_name=strdup(cue_name); + i=strlen(bin_name)-strlen("cue"); + + if (i>0) { + if (cue_name[i]=='c' && cue_name[i+1]=='u' && cue_name[i+2]=='e') { + bin_name[i++]='b'; bin_name[i++]='i'; bin_name[i++]='n'; + return bin_name; + } + else if (cue_name[i]=='C' && cue_name[i+1]=='U' && cue_name[i+2]=='E') { + bin_name[i++]='B'; bin_name[i++]='I'; bin_name[i++]='N'; + return bin_name; + } + } + free(bin_name); + return NULL; +} + +/*! + Return corresponding CUE file if bin_name is a bin file or NULL + if not a BIN file. + +*/ +/* Later we'll probably do better. For now though, this gets us + started for now. +*/ +char * +cdio_is_binfile(const char *bin_name) +{ + int i; + char *cue_name; + + if (bin_name == NULL) return false; + + cue_name=strdup(bin_name); + i=strlen(bin_name)-strlen("bin"); + + if (i>0) { + if (bin_name[i]=='b' && bin_name[i+1]=='i' && bin_name[i+2]=='n') { + cue_name[i++]='c'; cue_name[i++]='u'; cue_name[i++]='e'; + return cue_name; + } + else if (bin_name[i]=='B' && bin_name[i+1]=='I' && bin_name[i+2]=='N') { + cue_name[i++]='C'; cue_name[i++]='U'; cue_name[i++]='E'; + return cue_name; + } + } + free(cue_name); + return NULL; +} + +CdIo * +cdio_open_bincue (const char *source_name) +{ + char *bin_name = cdio_is_cuefile(source_name); + + if (NULL != bin_name) { + free(bin_name); + return cdio_open_cue(source_name); + } else { + char *cue_name = cdio_is_binfile(source_name); + CdIo *cdio = cdio_open_cue(cue_name); + free(cue_name); + return cdio; + } +} + +CdIo * +cdio_open_cue (const char *cue_name) +{ + CdIo *ret; + _img_private_t *_data; + char *bin_name; + + cdio_funcs _funcs = { + .eject_media = cdio_generic_bogus_eject_media, + .free = _cdio_bincue_destroy, + .get_arg = _cdio_get_arg, + .get_default_device = cdio_get_default_device_bincue, + .get_first_track_num= _cdio_get_first_track_num, + .get_mcn = _cdio_get_mcn, + .get_num_tracks = _cdio_get_num_tracks, + .get_track_format = _cdio_get_track_format, + .get_track_green = _cdio_get_track_green, + .get_track_lba = _cdio_get_track_lba, + .get_track_msf = _cdio_get_track_msf, + .lseek = _cdio_lseek, + .read = _cdio_read, + .read_audio_sectors = _cdio_read_audio_sectors, + .read_mode1_sector = _cdio_read_mode1_sector, + .read_mode1_sectors = _cdio_read_mode1_sectors, + .read_mode2_sector = _cdio_read_mode2_sector, + .read_mode2_sectors = _cdio_read_mode2_sectors, + .set_arg = _cdio_set_arg, + .stat_size = _cdio_stat_size + }; + + if (NULL == cue_name) return NULL; + + _data = _cdio_malloc (sizeof (_img_private_t)); + (_data)->gen.init = false; + (_data)->sector_2336 = false; + (_data)->cue_name = NULL; + + ret = cdio_new (_data, &_funcs); + + if (ret == NULL) return NULL; + + bin_name = cdio_is_cuefile(cue_name); + + if (NULL == bin_name) { + cdio_error ("source name %s is not recognized as a CUE file", cue_name); + } + + _cdio_set_arg (_data, "cue", cue_name); + _cdio_set_arg (_data, "source", bin_name); + free(bin_name); + + if (_cdio_init(_data)) { + return ret; + } else { + _cdio_bincue_destroy(_data); + free(ret); + return NULL; + } +} + +bool +cdio_have_bincue (void) +{ + return true; +} diff --git a/src/input/vcd/libcdio/image/nrg.c b/src/input/vcd/libcdio/image/nrg.c new file mode 100644 index 000000000..c4aa5e403 --- /dev/null +++ b/src/input/vcd/libcdio/image/nrg.c @@ -0,0 +1,1229 @@ +/* + $Id: nrg.c,v 1.1 2004/04/11 12:20:32 miguelfreitas Exp $ + + Copyright (C) 2001, 2003 Herbert Valerio Riedel + Copyright (C) 2003, 2004 Rocky Bernstein + + 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). +*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_GLOB_H +#include +#endif + +#include +#include +#include +#include "cdio_assert.h" +#include "bytesex.h" +#include "ds.h" +#include "cdio_private.h" +#include "_cdio_stdio.h" + +static const char _rcsid[] = "$Id: nrg.c,v 1.1 2004/04/11 12:20:32 miguelfreitas Exp $"; + +/* structures used */ + +/* 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 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 32bit 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 _unknown1 GNUC_PACKED; /* 0x41 == 'A' */ + uint8_t track GNUC_PACKED; /* binary or BCD?? */ + uint8_t index GNUC_PACKED; /* makes 0->1 transitions */ + uint8_t _unknown2 GNUC_PACKED; /* ?? */ + uint32_t lsn GNUC_PACKED; +} _cuex_array_t; + +typedef struct { + uint8_t _unknown[64] GNUC_PACKED; +} _daox_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 + +/* to be converted into BE */ +#define CUEX_ID 0x43554558 +#define CUES_ID 0x43554553 +#define DAOX_ID 0x44414f58 +#define DAOI_ID 0x44414f49 +#define END1_ID 0x454e4421 +#define ETN2_ID 0x45544e32 +#define ETNF_ID 0x45544e46 +#define NER5_ID 0x4e455235 +#define NERO_ID 0x4e45524f +#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 + +/* reader */ + +#define DEFAULT_CDIO_DEVICE "image.nrg" + +typedef struct { + int track_num; /* Probably is index+1 */ + msf_t start_msf; + lba_t start_lba; + int start_index; + int sec_count; /* Number of sectors in track. Does not + include pregap before next entry. */ + int flags; /* "DCP", "4CH", "PRE" */ + track_format_t track_format; + bool track_green; + uint16_t datasize; /* How much is in the portion we return back? */ + long int datastart; /* Offset from begining that data starts */ + uint16_t endsize; /* How much stuff at the end to skip over. This + stuff may have error correction (EDC, or ECC).*/ + uint16_t blocksize; /* total block size = start + size + end */ +} track_info_t; + +/* + 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 */ +} _mapping_t; + + +typedef struct { + /* Things common to all drivers like this. + This must be first. */ + generic_img_private_t gen; + internal_position_t pos; + 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; + + char *mcn; /* Media catalog number. */ + track_info_t tocent[100]; /* entry info for each track */ + track_t total_tracks; /* number of tracks in image */ + track_t first_track_num; /* track number of first track */ + CdioList *mapping; /* List of track information */ + uint32_t size; +} _img_private_t; + +static bool _cdio_parse_nero_footer (_img_private_t *_obj); +static uint32_t _cdio_stat_size (void *env); + +/* Updates internal track TOC, so we can later + simulate ioctl(CDROMREADTOCENTRY). + */ +static void +_register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t sec_count, + uint64_t img_offset, uint32_t blocksize, + track_format_t track_format, bool track_green) +{ + const int track_num=_obj->total_tracks; + track_info_t *this_track=&(_obj->tocent[_obj->total_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; + + if (!_obj->mapping) _obj->mapping = _cdio_list_new (); + _cdio_list_append (_obj->mapping, _map); + + _obj->size = MAX (_obj->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 (_obj->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", + _obj->total_tracks, this_track->track_format); + } + + _obj->total_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. + */ +static bool +_cdio_parse_nero_footer (_img_private_t *_obj) +{ + long unsigned int footer_start; + long unsigned int size; + char *footer_buf = NULL; + + if (_obj->size) return true; + + size = cdio_stream_stat (_obj->gen.data_source); + if (-1 == size) return false; + + { +PRAGMA_BEGIN_PACKED + 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; + } buf; +PRAGMA_END_PACKED + + cdio_assert (sizeof (buf) == 12); + + cdio_stream_seek (_obj->gen.data_source, size - sizeof (buf), SEEK_SET); + cdio_stream_read (_obj->gen.data_source, (void *) &buf, sizeof (buf), 1); + + if (buf.v50.ID == UINT32_TO_BE (0x4e45524f)) /* "NERO" */ + { + cdio_info ("detected v50 (32bit offsets) NRG magic"); + footer_start = uint32_to_be (buf.v50.footer_ofs); + } + else if (buf.v55.ID == UINT32_TO_BE (0x4e455235)) /* "NER5" */ + { + cdio_info ("detected v55 (64bit offsets) NRG magic"); + footer_start = uint64_from_be (buf.v55.footer_ofs); + } + else + { + cdio_warn ("Image not recognized as either v50 or v55 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 (_obj->gen.data_source, footer_start, SEEK_SET); + cdio_stream_read (_obj->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 (_obj->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" ); + /*cdio_assert (lsn == 0?);*/ + + _obj->is_cues = true; /* HACK alert. */ + _obj->total_tracks = 0; + _obj->first_track_num = 1; + for (idx = 1; idx < entries-1; idx += 2) { + lsn_t sec_count; + + cdio_assert (_entries[idx].index == 0); + 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. + */ + + lsn = UINT32_FROM_BE (_entries[idx].lsn); + sec_count = UINT32_FROM_BE (_entries[idx + 1].lsn); + + _register_mapping (_obj, lsn, sec_count*2, + (lsn+CDIO_PREGAP_SECTORS) * M2RAW_SECTOR_SIZE, + M2RAW_SECTOR_SIZE, TRACK_FORMAT_XA, true); + } + } else { + lsn_t lsn = UINT32_FROM_BE (_entries[0].lsn); + int idx; + + cdio_info ("CUEX type image detected"); + cdio_assert (lsn == 0xffffff6a); + + for (idx = 2; idx < entries; idx += 2) { + lsn_t sec_count; + + cdio_assert (_entries[idx].index == 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 (_obj, lsn, sec_count - lsn, + (lsn + CDIO_PREGAP_SECTORS)*M2RAW_SECTOR_SIZE, + M2RAW_SECTOR_SIZE, TRACK_FORMAT_XA, true); + } + } + break; + } + + case DAOI_ID: /* "DAOI" */ + case DAOX_ID: /* "DAOX" */ + { + _daox_array_t *_entries = (void *) chunk->data; + track_format_t track_format; + int form = _entries->_unknown[18]; + _obj->dtyp = _entries->_unknown[36]; + _obj->is_dao = true; + cdio_debug ("DAO%c tag detected, track format %d, form %x\n", + opcode==DAOX_ID ? 'X': 'I', _obj->dtyp, form); + switch (_obj->dtyp) { + case 0: + track_format = TRACK_FORMAT_DATA; + break; + case 0x6: + case 0x20: + track_format = TRACK_FORMAT_XA; + break; + case 0x7: + track_format = TRACK_FORMAT_AUDIO; + break; + default: + cdio_warn ("Unknown track format %x\n", + _obj->dtyp); + track_format = TRACK_FORMAT_AUDIO; + } + if (0 == form) { + int i; + for (i=0; i<_obj->total_tracks; i++) { + _obj->tocent[i].track_format= track_format; + _obj->tocent[i].datastart = 0; + _obj->tocent[i].track_green = false; + if (TRACK_FORMAT_AUDIO == track_format) { + _obj->tocent[i].blocksize = CDIO_CD_FRAMESIZE_RAW; + _obj->tocent[i].datasize = CDIO_CD_FRAMESIZE_RAW; + _obj->tocent[i].endsize = 0; + } else { + _obj->tocent[i].datasize = CDIO_CD_FRAMESIZE; + _obj->tocent[i].datastart = 0; + } + } + } else if (2 == form) { + int i; + for (i=0; i<_obj->total_tracks; i++) { + _obj->tocent[i].track_green = true; + _obj->tocent[i].track_format= track_format; + _obj->tocent[i].datasize = CDIO_CD_FRAMESIZE; + if (TRACK_FORMAT_XA == track_format) { + _obj->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE + CDIO_CD_SUBHEADER_SIZE; + _obj->tocent[i].endsize = CDIO_CD_SYNC_SIZE + + CDIO_CD_ECC_SIZE; + } else { + _obj->tocent[i].datastart = CDIO_CD_SYNC_SIZE + + CDIO_CD_HEADER_SIZE; + _obj->tocent[i].endsize = CDIO_CD_EDC_SIZE + + CDIO_CD_M1F1_ZERO_SIZE + CDIO_CD_ECC_SIZE; + + } + } + } + break; + } + case NERO_ID: /* "NER0" */ + case NER5_ID: /* "NER5" */ + 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 (_obj->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: + 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_warn ("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 (_obj, _start, _len, _start2, blocksize, + track_format, track_green); + + } + } + 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 (_obj->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_warn ("Don't know how to handle track mode (%lu)?", + (long unsigned int) track_mode); + free(footer_buf); + return false; + } + + if (_len % blocksize != 0) { + cdio_warn ("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_warn("Adjusting blocksize to %d", CDIO_CD_FRAMESIZE); + blocksize = CDIO_CD_FRAMESIZE; + } else if (0 == _len % M2RAW_SECTOR_SIZE) { + cdio_warn("Adjusting blocksize to %d", M2RAW_SECTOR_SIZE); + blocksize = M2RAW_SECTOR_SIZE; + } else if (0 == _len % CDIO_CD_FRAMESIZE_RAW) { + cdio_warn("Adjusting blocksize to %d", CDIO_CD_FRAMESIZE_RAW); + blocksize = CDIO_CD_FRAMESIZE_RAW; + } + } + + _len /= blocksize; + + if (_start * blocksize != _start2) { + cdio_warn ("%lu * %d != %lu", + (long unsigned int) _start, blocksize, + (long unsigned int) _start2); + if (_start * CDIO_CD_FRAMESIZE == _start2) { + cdio_warn("Adjusting blocksize to %d", CDIO_CD_FRAMESIZE); + blocksize = CDIO_CD_FRAMESIZE; + } else if (_start * M2RAW_SECTOR_SIZE == _start2) { + cdio_warn("Adjusting blocksize to %d", M2RAW_SECTOR_SIZE); + blocksize = M2RAW_SECTOR_SIZE; + } else if (0 == _start * CDIO_CD_FRAMESIZE_RAW == _start2) { + cdio_warn("Adjusting blocksize to %d", CDIO_CD_FRAMESIZE_RAW); + blocksize = CDIO_CD_FRAMESIZE_RAW; + } + } + + _start += idx * CDIO_PREGAP_SECTORS; + _register_mapping (_obj, _start, _len, _start2, blocksize, + track_format, track_green); + } + } + 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_warn ("Unknown MTYP value: %u", (unsigned int) mtyp); + } + _obj->mtyp = mtyp; + } + break; + + default: + cdio_warn ("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 _cdio_stat_size since that will lead to recursion since + we haven't fully initialized things yet. + */ + cdio_lsn_to_msf (_obj->size, &_obj->tocent[_obj->total_tracks].start_msf); + _obj->tocent[_obj->total_tracks].start_lba = cdio_lsn_to_lba(_obj->size); + _obj->tocent[_obj->total_tracks-1].sec_count = + cdio_lsn_to_lba(_obj->size - _obj->tocent[_obj->total_tracks-1].start_lba); + + free(footer_buf); + return true; +} + +/*! + Initialize image structures. + */ +static bool +_cdio_init (_img_private_t *_obj) +{ + if (_obj->gen.init) { + cdio_error ("init called more than once"); + return false; + } + + if (!(_obj->gen.data_source = cdio_stdio_new (_obj->gen.source_name))) { + cdio_warn ("init failed"); + return false; + } + + _cdio_parse_nero_footer (_obj); + _obj->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 +_cdio_lseek (void *env, off_t offset, int whence) +{ + _img_private_t *_obj = env; + + /* real_offset is the real byte offset inside the disk image + The number below was determined empirically. + */ + off_t real_offset= _obj->is_dao ? 0x4b000 : 0; + + unsigned int i; + + for (i=0; i<_obj->total_tracks; i++) { + track_info_t *this_track=&(_obj->tocent[i]); + _obj->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; + _obj->pos.buff_offset = rem; + _obj->pos.lba += blocks; + break; + } + real_offset += this_track->sec_count*this_track->blocksize; + offset -= this_track->sec_count*this_track->datasize; + _obj->pos.lba += this_track->sec_count; + } + + if (i==_obj->total_tracks) { + cdio_warn ("seeking outside range of disk image"); + return -1; + } else + real_offset += _obj->tocent[i].datastart; + return cdio_stream_seek(_obj->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 +_cdio_read (void *env, void *buf, size_t size) +{ + _img_private_t *_obj = env; + return cdio_stream_read(_obj->gen.data_source, buf, size, 1); +} + +static uint32_t +_cdio_stat_size (void *env) +{ + _img_private_t *_obj = env; + + return _obj->size; +} + +/*! + Reads a single audio sector from CD device into data starting + from LSN. Returns 0 if no error. + */ +static int +_cdio_read_audio_sectors (void *env, void *data, lsn_t lsn, + unsigned int nblocks) +{ + _img_private_t *_obj = env; + + CdioListNode *node; + + if (lsn >= _obj->size) + { + cdio_warn ("trying to read beyond image size (%lu >= %lu)", + (long unsigned int) lsn, (long unsigned int) _obj->size); + return -1; + } + + _CDIO_LIST_FOREACH (node, _obj->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 (_obj->gen.data_source, img_offset, + SEEK_SET); + if (ret!=0) return ret; + ret = cdio_stream_read (_obj->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 +_cdio_read_mode1_sector (void *env, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *_obj = env; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + + CdioListNode *node; + + if (lsn >= _obj->size) + { + cdio_warn ("trying to read beyond image size (%lu >= %lu)", + (long unsigned int) lsn, (long unsigned int) _obj->size); + return -1; + } + + _CDIO_LIST_FOREACH (node, _obj->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 (_obj->gen.data_source, img_offset, + SEEK_SET); + if (ret!=0) return ret; + + /* FIXME: Not completely sure the below is correct. */ + ret = cdio_stream_read (_obj->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 +_cdio_read_mode1_sectors (void *env, void *data, uint32_t lsn, + bool b_form2, unsigned nblocks) +{ + _img_private_t *_obj = env; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode1_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +static int +_cdio_read_mode2_sector (void *env, void *data, lsn_t lsn, + bool b_form2) +{ + _img_private_t *_obj = env; + char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; + + CdioListNode *node; + + if (lsn >= _obj->size) + { + cdio_warn ("trying to read beyond image size (%lu >= %lu)", + (long unsigned int) lsn, (long unsigned int) _obj->size); + return -1; + } + + _CDIO_LIST_FOREACH (node, _obj->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 (_obj->gen.data_source, img_offset, + SEEK_SET); + if (ret!=0) return ret; + ret = cdio_stream_read (_obj->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 +_cdio_read_mode2_sectors (void *env, void *data, uint32_t lsn, + bool b_form2, unsigned nblocks) +{ + _img_private_t *_obj = env; + int i; + int retval; + unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; + + for (i = 0; i < nblocks; i++) { + if ( (retval = _cdio_read_mode2_sector (_obj, + ((char *)data) + (blocksize * i), + lsn + i, b_form2)) ) + return retval; + } + return 0; +} + +/* + Free memory resources associated with NRG object. +*/ +static void +_cdio_nrg_destroy (void *obj) +{ + _img_private_t *env = obj; + + if (NULL == env) return; + if (NULL != env->mapping) + _cdio_list_free (env->mapping, true); + cdio_generic_stdio_free(env); + free(env); +} + +/* + Set the device to use in I/O operations. +*/ +static int +_cdio_set_arg (void *env, const char key[], const char value[]) +{ + _img_private_t *_obj = env; + + if (!strcmp (key, "source")) + { + free (_obj->gen.source_name); + + if (!value) + return -2; + + _obj->gen.source_name = strdup (value); + } + else + return -1; + + return 0; +} + +/*! + Return the value associated with the key "arg". +*/ +static const char * +_cdio_get_arg (void *env, const char key[]) +{ + _img_private_t *_obj = env; + + if (!strcmp (key, "source")) { + return _obj->gen.source_name; + } + return NULL; +} + +/*! + 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; ifirst_track_num; +} + +/*! + Return the number of tracks. We fake it an just say there's + one big track. +*/ +static track_t +_cdio_get_num_tracks(void *env) +{ + _img_private_t *_obj = env; + + return _obj->total_tracks; +} + +/*! + Return the number of tracks in the current medium. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_format_t +_cdio_get_track_format(void *env, track_t track_num) +{ + _img_private_t *_obj = env; + + if (track_num > _obj->total_tracks || track_num == 0) + return TRACK_FORMAT_ERROR; + + if ( _obj->dtyp != DTYP_INVALID) { + switch (_obj->dtyp) { + case DTYP_MODE2_XA: + return TRACK_FORMAT_XA; + case DTYP_MODE1: + return TRACK_FORMAT_DATA; + default: ; + } + } + + /*if ( MTYP_AUDIO_CD == _obj->mtyp) return TRACK_FORMAT_AUDIO; */ + return _obj->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 +_cdio_get_track_green(void *env, track_t track_num) +{ + _img_private_t *_obj = env; + + if (track_num > _obj->total_tracks || track_num == 0) + return false; + + if ( MTYP_AUDIO_CD == _obj->mtyp) return false; + return _obj->tocent[track_num-1].track_green; +} + +/*! + Return the starting MSF (minutes/secs/frames) for the track number + track_num in obj. Tracks numbers start at 1. + The "leadout" track is specified either by + using track_num LEADOUT_TRACK or the total tracks+1. +*/ +static bool +_cdio_get_track_msf(void *env, track_t track_num, msf_t *msf) +{ + _img_private_t *_obj = env; + + if (NULL == msf) return 1; + + if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = _obj->total_tracks+1; + + if (track_num <= _obj->total_tracks+1 && track_num != 0) { + *msf = _obj->tocent[track_num-1].start_msf; + return true; + } else + return false; +} + +CdIo * +cdio_open_nrg (const char *source_name) +{ + CdIo *ret; + _img_private_t *_data; + + cdio_funcs _funcs = { + .eject_media = cdio_generic_bogus_eject_media, + .free = _cdio_nrg_destroy, + .get_arg = _cdio_get_arg, + .get_devices = cdio_get_devices_nrg, + .get_default_device = cdio_get_default_device_nrg, + .get_first_track_num= _cdio_get_first_track_num, + .get_num_tracks = _cdio_get_num_tracks, + .get_track_format = _cdio_get_track_format, + .get_track_green = _cdio_get_track_green, + .get_track_lba = NULL, /* Will use generic routine via msf */ + .get_track_msf = _cdio_get_track_msf, + .lseek = _cdio_lseek, + .read = _cdio_read, + .read_audio_sectors = _cdio_read_audio_sectors, + .read_mode1_sector = _cdio_read_mode1_sector, + .read_mode1_sectors = _cdio_read_mode1_sectors, + .read_mode2_sector = _cdio_read_mode2_sector, + .read_mode2_sectors = _cdio_read_mode2_sectors, + .set_arg = _cdio_set_arg, + .stat_size = _cdio_stat_size, + }; + + _data = _cdio_malloc (sizeof (_img_private_t)); + _data->gen.init = false; + + _data->total_tracks = 0; + _data->mtyp = 0; + _data->dtyp = DTYP_INVALID; + _data->first_track_num= 1; + _data->is_dao = false; + _data->is_cues = false; /* FIXME: remove is_cues. */ + + + _cdio_set_arg(_data, "source", (NULL == source_name) + ? DEFAULT_CDIO_DEVICE: source_name); + + ret = cdio_new (_data, &_funcs); + if (ret == NULL) return NULL; + + if (_cdio_init(_data)) + return ret; + else { + cdio_generic_stdio_free (_data); + free(_data); + return NULL; + } + +} + +bool +cdio_have_nrg (void) +{ + return true; +} diff --git a/src/input/vcd/libcdio/iso9660.c b/src/input/vcd/libcdio/iso9660.c index c7d6f8a54..c9c9ccb5e 100644 --- a/src/input/vcd/libcdio/iso9660.c +++ b/src/input/vcd/libcdio/iso9660.c @@ -1,5 +1,5 @@ /* - $Id: iso9660.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: iso9660.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel Copyright (C) 2003 Rocky Bernstein @@ -37,7 +37,7 @@ #include #endif -static const char _rcsid[] = "$Id: iso9660.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $"; +static const char _rcsid[] = "$Id: iso9660.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; /* some parameters... */ #define SYSTEM_ID "CD-RTOS CD-BRIDGE" diff --git a/src/input/vcd/libcdio/iso9660_fs.c b/src/input/vcd/libcdio/iso9660_fs.c index b54cf2c0f..5841b7d87 100644 --- a/src/input/vcd/libcdio/iso9660_fs.c +++ b/src/input/vcd/libcdio/iso9660_fs.c @@ -1,8 +1,8 @@ /* - $Id: iso9660_fs.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: iso9660_fs.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2001 Herbert Valerio Riedel - Copyright (C) 2003 Rocky Bernstein + Copyright (C) 2003, 2004 Rocky Bernstein 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 @@ -35,88 +35,173 @@ #include "cdio_assert.h" #include "bytesex.h" #include "ds.h" +#include "_cdio_stdio.h" +#include "cdio_private.h" #include -static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $"; +static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; -static void -_idr2statbuf (const iso9660_dir_t *idr, iso9660_stat_t *stat, bool is_mode2) +/* Implementation of iso9660_t type */ +struct _iso9660 { + CdioDataSource *stream; /* Stream pointer */ + void *env; /* environment. */ +}; + +/*! + Open an ISO 9660 image for reading. Maybe in the future we will have + flags and mode. NULL is returned on error. +*/ +iso9660_t * +iso9660_open (const char *pathname /*flags, mode */) +{ + iso9660_t *iso = (iso9660_t *) _cdio_malloc(sizeof(struct _iso9660)) ; + + if (NULL == iso) return NULL; + + iso->stream = cdio_stdio_new( pathname ); + if (NULL == iso->stream) { + free(iso); + return NULL; + } + + return iso; +} + + + +/*! + Close previously opened ISO 9660 image. + True is unconditionally returned. If there was an error false would + be returned. +*/ +bool +iso9660_close (iso9660_t *iso) +{ + if (NULL != iso) { + cdio_stdio_destroy(iso->stream); + free(iso); + } + return true; +} + + + +/*! + Seek to a position and then read n blocks. Size read is returned. +*/ +long int +iso9660_iso_seek_read (iso9660_t *iso, void *ptr, lsn_t start, long int size) +{ + long int ret; + if (NULL == iso) return 0; + ret = cdio_stream_seek (iso->stream, start * ISO_BLOCKSIZE, SEEK_SET); + if (ret!=0) return 0; + return cdio_stream_read (iso->stream, ptr, ISO_BLOCKSIZE, size); +} + + +static iso9660_stat_t * +_iso9660_dir_to_statbuf (const iso9660_dir_t *iso9660_dir, bool is_mode2) { iso9660_xa_t *xa_data = NULL; - uint8_t dir_len= iso9660_get_dir_len(idr); + uint8_t dir_len= iso9660_get_dir_len(iso9660_dir); + unsigned int filename_len; + unsigned int stat_len; + iso9660_stat_t *stat; + + if (!dir_len) return NULL; - memset ((void *) stat, 0, sizeof (iso9660_stat_t)); + filename_len = from_711(iso9660_dir->filename_len); - if (!dir_len) return; + /* .. string in statbuf is one longer than in iso9660_dir's listing '\1' */ + stat_len = sizeof(iso9660_stat_t)+filename_len+2; - stat->type = (idr->file_flags & ISO_DIRECTORY) ? _STAT_DIR : _STAT_FILE; - stat->lsn = from_733 (idr->extent); - stat->size = from_733 (idr->size); + stat = _cdio_malloc(stat_len); + stat->type = (iso9660_dir->file_flags & ISO_DIRECTORY) + ? _STAT_DIR : _STAT_FILE; + stat->lsn = from_733 (iso9660_dir->extent); + stat->size = from_733 (iso9660_dir->size); stat->secsize = _cdio_len2blocks (stat->size, ISO_BLOCKSIZE); - iso9660_get_dtime(&(idr->recording_time), true, &(stat->tm)); + if (iso9660_dir->filename[0] == '\0') + strcpy (stat->filename, "."); + else if (iso9660_dir->filename[0] == '\1') + strcpy (stat->filename, ".."); + else + strncpy (stat->filename, iso9660_dir->filename, filename_len); + + iso9660_get_dtime(&(iso9660_dir->recording_time), true, &(stat->tm)); cdio_assert (dir_len >= sizeof (iso9660_dir_t)); if (is_mode2) { - int su_length = iso9660_get_dir_len(idr) - sizeof (iso9660_dir_t); - su_length -= idr->filename_len; + int su_length = iso9660_get_dir_len(iso9660_dir) - sizeof (iso9660_dir_t); + su_length -= filename_len; if (su_length % 2) su_length--; if (su_length < 0 || su_length < sizeof (iso9660_xa_t)) - return; + return stat; - xa_data = (void *) (((char *) idr) + (iso9660_get_dir_len(idr) - su_length)); + xa_data = (void *) (((char *) iso9660_dir) + + (iso9660_get_dir_len(iso9660_dir) - su_length)); if (xa_data->signature[0] != 'X' || xa_data->signature[1] != 'A') { cdio_warn ("XA signature not found in ISO9660's system use area;" " ignoring XA attributes for this file entry."); - cdio_debug ("%d %d %d, '%c%c' (%d, %d)", iso9660_get_dir_len(idr), - idr->filename_len, + cdio_debug ("%d %d %d, '%c%c' (%d, %d)", + iso9660_get_dir_len(iso9660_dir), + filename_len, su_length, xa_data->signature[0], xa_data->signature[1], xa_data->signature[0], xa_data->signature[1]); - return; + return stat; } stat->xa = *xa_data; } + return stat; } -static char * -_idr2name (const iso9660_dir_t *idr) +/*! + Return the directory name stored in the iso9660_dir_t + + A string is allocated: the caller must deallocate. + */ +char * +iso9660_dir_to_name (const iso9660_dir_t *iso9660_dir) { char namebuf[256] = { 0, }; - uint8_t len=iso9660_get_dir_len(idr); + uint8_t len=iso9660_get_dir_len(iso9660_dir); if (!len) return NULL; cdio_assert (len >= sizeof (iso9660_dir_t)); - /* (idr->file_flags & ISO_DIRECTORY) */ + /* (iso9660_dir->file_flags & ISO_DIRECTORY) */ - if (idr->filename[0] == '\0') + if (iso9660_dir->filename[0] == '\0') strcpy (namebuf, "."); - else if (idr->filename[0] == '\1') + else if (iso9660_dir->filename[0] == '\1') strcpy (namebuf, ".."); else - strncpy (namebuf, idr->filename, idr->filename_len); + strncpy (namebuf, iso9660_dir->filename, iso9660_dir->filename_len); return strdup (namebuf); } -static void -_fs_stat_root (const CdIo *cdio, iso9660_stat_t *stat, bool is_mode2) +static iso9660_stat_t * +_fs_stat_root (const CdIo *cdio, bool is_mode2) { char block[ISO_BLOCKSIZE] = { 0, }; const iso9660_pvd_t *pvd = (void *) █ - const iso9660_dir_t *idr = (void *) pvd->root_directory_record; + const iso9660_dir_t *iso9660_dir = (void *) pvd->root_directory_record; + iso9660_stat_t *stat; if (is_mode2) { if (cdio_read_mode2_sector (cdio, &block, ISO_PVD_SECTOR, false)) @@ -126,25 +211,44 @@ _fs_stat_root (const CdIo *cdio, iso9660_stat_t *stat, bool is_mode2) cdio_assert_not_reached (); } - _idr2statbuf (idr, stat, is_mode2); + stat = _iso9660_dir_to_statbuf (iso9660_dir, is_mode2); + return stat; +} + +static iso9660_stat_t * +_fs_stat_iso_root (iso9660_t *iso) +{ + char block[ISO_BLOCKSIZE] = { 0, }; + const iso9660_pvd_t *pvd = (void *) █ + const iso9660_dir_t *iso9660_dir = (void *) pvd->root_directory_record; + iso9660_stat_t *stat; + int ret; + + ret = iso9660_iso_seek_read (iso, block, ISO_PVD_SECTOR, 1); + if (ret!=ISO_BLOCKSIZE) return NULL; + + stat = _iso9660_dir_to_statbuf (iso9660_dir, true); + return stat; } -static int +static iso9660_stat_t * _fs_stat_traverse (const CdIo *cdio, const iso9660_stat_t *_root, - char **splitpath, /*out*/ iso9660_stat_t *buf, - bool is_mode2) + char **splitpath, bool is_mode2, bool translate) { unsigned offset = 0; uint8_t *_dirbuf = NULL; + iso9660_stat_t *stat; if (!splitpath[0]) { - *buf = *_root; - return 0; + unsigned int len=sizeof(iso9660_stat_t) + strlen(_root->filename)+1; + stat = _cdio_malloc(len); + memcpy(stat, _root, len); + return stat; } if (_root->type == _STAT_FILE) - return -1; + return NULL; cdio_assert (_root->type == _STAT_DIR); @@ -160,137 +264,386 @@ _fs_stat_traverse (const CdIo *cdio, const iso9660_stat_t *_root, if (is_mode2) { if (cdio_read_mode2_sectors (cdio, _dirbuf, _root->lsn, false, _root->secsize)) - cdio_assert_not_reached (); + return NULL; } else { if (cdio_read_mode1_sectors (cdio, _dirbuf, _root->lsn, false, _root->secsize)) - cdio_assert_not_reached (); + return NULL; } while (offset < (_root->secsize * ISO_BLOCKSIZE)) { - const iso9660_dir_t *idr = (void *) &_dirbuf[offset]; - iso9660_stat_t stat; - char *name; + const iso9660_dir_t *iso9660_dir = (void *) &_dirbuf[offset]; + iso9660_stat_t *stat; + int cmp; - if (!iso9660_get_dir_len(idr)) + if (!iso9660_get_dir_len(iso9660_dir)) { offset++; continue; } - name = _idr2name (idr); - _idr2statbuf (idr, &stat, is_mode2); + stat = _iso9660_dir_to_statbuf (iso9660_dir, is_mode2); + + if (translate) { + char *trans_fname = malloc(strlen(stat->filename)); + int trans_len; + + if (trans_fname == NULL) { + cdio_warn("can't allocate %lu bytes", + (long unsigned int) strlen(stat->filename)); + return NULL; + } + trans_len = iso9660_name_translate(stat->filename, trans_fname); + cmp = strcmp(splitpath[0], trans_fname); + free(trans_fname); + } else { + cmp = strcmp(splitpath[0], stat->filename); + } + + if (!cmp) { + iso9660_stat_t *ret_stat + = _fs_stat_traverse (cdio, stat, &splitpath[1], is_mode2, + translate); + free(stat); + free (_dirbuf); + return ret_stat; + } + + free(stat); + + offset += iso9660_get_dir_len(iso9660_dir); + } + + cdio_assert (offset == (_root->secsize * ISO_BLOCKSIZE)); + + /* not found */ + free (_dirbuf); + return NULL; +} + +static iso9660_stat_t * +_fs_iso_stat_traverse (iso9660_t *iso, const iso9660_stat_t *_root, + char **splitpath, bool translate) +{ + unsigned offset = 0; + uint8_t *_dirbuf = NULL; + iso9660_stat_t *stat; + int ret; + + if (!splitpath[0]) + { + unsigned int len=sizeof(iso9660_stat_t) + strlen(_root->filename)+1; + stat = _cdio_malloc(len); + memcpy(stat, _root, len); + return stat; + } + + if (_root->type == _STAT_FILE) + return NULL; + + cdio_assert (_root->type == _STAT_DIR); + + if (_root->size != ISO_BLOCKSIZE * _root->secsize) + { + cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!", + (unsigned) _root->size, + (unsigned long int) ISO_BLOCKSIZE * _root->secsize); + } + + _dirbuf = _cdio_malloc (_root->secsize * ISO_BLOCKSIZE); + + ret = iso9660_iso_seek_read (iso, _dirbuf, _root->lsn, _root->secsize); + if (ret!=ISO_BLOCKSIZE*_root->secsize) return NULL; + + while (offset < (_root->secsize * ISO_BLOCKSIZE)) + { + const iso9660_dir_t *iso9660_dir = (void *) &_dirbuf[offset]; + iso9660_stat_t *stat; + int cmp; - if (!strcmp (splitpath[0], name)) + if (!iso9660_get_dir_len(iso9660_dir)) { - int retval = _fs_stat_traverse (cdio, &stat, &splitpath[1], buf, - is_mode2); - free (name); - free (_dirbuf); - return retval; + offset++; + continue; } + + stat = _iso9660_dir_to_statbuf (iso9660_dir, true); + + if (translate) { + char *trans_fname = malloc(strlen(stat->filename)+1); + int trans_len; + + if (trans_fname == NULL) { + cdio_warn("can't allocate %lu bytes", + (long unsigned int) strlen(stat->filename)); + return NULL; + } + trans_len = iso9660_name_translate(stat->filename, trans_fname); + cmp = strcmp(splitpath[0], trans_fname); + free(trans_fname); + } else { + cmp = strcmp(splitpath[0], stat->filename); + } + + if (!cmp) { + iso9660_stat_t *ret_stat + = _fs_iso_stat_traverse (iso, stat, &splitpath[1], translate); + free(stat); + free (_dirbuf); + return ret_stat; + } - free (name); - - offset += iso9660_get_dir_len(idr); + free(stat); + + offset += iso9660_get_dir_len(iso9660_dir); } cdio_assert (offset == (_root->secsize * ISO_BLOCKSIZE)); /* not found */ free (_dirbuf); - return -1; + return NULL; +} + +/*! + Get file status for pathname into stat. NULL is returned on error. + */ +iso9660_stat_t * +iso9660_fs_stat (const CdIo *cdio, const char pathname[], bool is_mode2) +{ + iso9660_stat_t *root; + char **splitpath; + iso9660_stat_t *stat; + + if (cdio == NULL) return NULL; + if (pathname == NULL) return NULL; + + root = _fs_stat_root (cdio, is_mode2); + if (NULL == root) return NULL; + + splitpath = _cdio_strsplit (pathname, '/'); + stat = _fs_stat_traverse (cdio, root, splitpath, is_mode2, false); + free(root); + _cdio_strfreev (splitpath); + + return stat; +} + +/*! + Get file status for pathname into stat. NULL is returned on error. + pathname version numbers in the ISO 9660 + name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names + are lowercased. + */ +iso9660_stat_t * +iso9660_fs_stat_translate (const CdIo *cdio, const char pathname[], + bool is_mode2) +{ + iso9660_stat_t *root; + char **splitpath; + iso9660_stat_t *stat; + + if (cdio == NULL) return NULL; + if (pathname == NULL) return NULL; + + root = _fs_stat_root (cdio, is_mode2); + if (NULL == root) return NULL; + + splitpath = _cdio_strsplit (pathname, '/'); + stat = _fs_stat_traverse (cdio, root, splitpath, is_mode2, true); + free(root); + _cdio_strfreev (splitpath); + + return stat; +} + +/*! + Get file status for pathname into stat. NULL is returned on error. + */ +void * +iso9660_ifs_stat (iso9660_t *iso, const char pathname[]) +{ + iso9660_stat_t *root; + char **splitpath; + iso9660_stat_t *stat; + + if (iso == NULL) return NULL; + if (pathname == NULL) return NULL; + + root = _fs_stat_iso_root (iso); + if (NULL == root) return NULL; + + splitpath = _cdio_strsplit (pathname, '/'); + stat = _fs_iso_stat_traverse (iso, root, splitpath, false); + free(root); + _cdio_strfreev (splitpath); + + return stat; } /*! - Get file status for pathname into stat. As with libc's stat, 0 is returned - if no error and -1 on error. + Get file status for pathname into stat. NULL is returned on error. + pathname version numbers in the ISO 9660 + name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names + are lowercased. */ -int -iso9660_fs_stat (const CdIo *cdio, const char pathname[], - /*out*/ iso9660_stat_t *stat, bool is_mode2) +void * +iso9660_ifs_stat_translate (iso9660_t *iso, const char pathname[]) { - iso9660_stat_t _root; - int retval; + iso9660_stat_t *root; char **splitpath; + iso9660_stat_t *stat; - cdio_assert (cdio != NULL); - cdio_assert (pathname != NULL); - cdio_assert (stat != NULL); + if (iso == NULL) return NULL; + if (pathname == NULL) return NULL; - _fs_stat_root (cdio, &_root, is_mode2); + root = _fs_stat_iso_root (iso); + if (NULL == root) return NULL; splitpath = _cdio_strsplit (pathname, '/'); - retval = _fs_stat_traverse (cdio, &_root, splitpath, stat, is_mode2); + stat = _fs_iso_stat_traverse (iso, root, splitpath, true); + free(root); _cdio_strfreev (splitpath); - return retval; + return stat; } -void * /* list of char* -- caller must free it */ +/*! + Read pathname (a directory) and return a list of iso9660_stat_t + of the files inside that. The caller must free the returned result. +*/ +void * iso9660_fs_readdir (const CdIo *cdio, const char pathname[], bool is_mode2) { - iso9660_stat_t stat; + iso9660_stat_t *stat; - cdio_assert (cdio != NULL); - cdio_assert (pathname != NULL); + if (NULL == cdio) return NULL; + if (NULL == pathname) return NULL; - if (iso9660_fs_stat (cdio, pathname, &stat, is_mode2)) + stat = iso9660_fs_stat (cdio, pathname, is_mode2); + if (NULL == stat) return NULL; - if (stat.type != _STAT_DIR) + if (stat->type != _STAT_DIR) { + free(stat); return NULL; + } { unsigned offset = 0; uint8_t *_dirbuf = NULL; CdioList *retval = _cdio_list_new (); - if (stat.size != ISO_BLOCKSIZE * stat.secsize) + if (stat->size != ISO_BLOCKSIZE * stat->secsize) { cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!", - (unsigned) stat.size, - (unsigned long int) ISO_BLOCKSIZE * stat.secsize); + (unsigned) stat->size, + (unsigned long int) ISO_BLOCKSIZE * stat->secsize); } - _dirbuf = _cdio_malloc (stat.secsize * ISO_BLOCKSIZE); + _dirbuf = _cdio_malloc (stat->secsize * ISO_BLOCKSIZE); if (is_mode2) { - if (cdio_read_mode2_sectors (cdio, _dirbuf, stat.lsn, false, - stat.secsize)) + if (cdio_read_mode2_sectors (cdio, _dirbuf, stat->lsn, false, + stat->secsize)) cdio_assert_not_reached (); } else { - if (cdio_read_mode1_sectors (cdio, _dirbuf, stat.lsn, false, - stat.secsize)) + if (cdio_read_mode1_sectors (cdio, _dirbuf, stat->lsn, false, + stat->secsize)) cdio_assert_not_reached (); } - while (offset < (stat.secsize * ISO_BLOCKSIZE)) + while (offset < (stat->secsize * ISO_BLOCKSIZE)) + { + const iso9660_dir_t *iso9660_dir = (void *) &_dirbuf[offset]; + iso9660_stat_t *iso9660_stat; + + if (!iso9660_get_dir_len(iso9660_dir)) + { + offset++; + continue; + } + + iso9660_stat = _iso9660_dir_to_statbuf(iso9660_dir, is_mode2); + _cdio_list_append (retval, iso9660_stat); + + offset += iso9660_get_dir_len(iso9660_dir); + } + + cdio_assert (offset == (stat->secsize * ISO_BLOCKSIZE)); + + free (_dirbuf); + free (stat); + return retval; + } +} + +/*! + Read pathname (a directory) and return a list of iso9660_stat_t + of the files inside that. The caller must free the returned result. +*/ +void * +iso9660_ifs_readdir (iso9660_t *iso, const char pathname[]) +{ + iso9660_stat_t *stat; + + if (NULL == iso) return NULL; + if (NULL == pathname) return NULL; + + stat = iso9660_ifs_stat (iso, pathname); + if (NULL == stat) return NULL; + + if (stat->type != _STAT_DIR) { + free(stat); + return NULL; + } + + { + long int ret; + unsigned offset = 0; + uint8_t *_dirbuf = NULL; + CdioList *retval = _cdio_list_new (); + + if (stat->size != ISO_BLOCKSIZE * stat->secsize) { - const iso9660_dir_t *idr = (void *) &_dirbuf[offset]; + cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!", + (unsigned) stat->size, + (unsigned long int) ISO_BLOCKSIZE * stat->secsize); + } + + _dirbuf = _cdio_malloc (stat->secsize * ISO_BLOCKSIZE); - if (!iso9660_get_dir_len(idr)) + ret = iso9660_iso_seek_read (iso, _dirbuf, stat->lsn, stat->secsize); + if (ret != ISO_BLOCKSIZE*stat->secsize) return NULL; + + while (offset < (stat->secsize * ISO_BLOCKSIZE)) + { + const iso9660_dir_t *iso9660_dir = (void *) &_dirbuf[offset]; + iso9660_stat_t *iso9660_stat; + + if (!iso9660_get_dir_len(iso9660_dir)) { offset++; continue; } - _cdio_list_append (retval, _idr2name (idr)); + iso9660_stat = _iso9660_dir_to_statbuf(iso9660_dir, true); + _cdio_list_append (retval, iso9660_stat); - offset += iso9660_get_dir_len(idr); + offset += iso9660_get_dir_len(iso9660_dir); } - cdio_assert (offset == (stat.secsize * ISO_BLOCKSIZE)); + cdio_assert (offset == (stat->secsize * ISO_BLOCKSIZE)); free (_dirbuf); + free (stat); return retval; } } -static bool -find_fs_lsn_recurse (const CdIo *cdio, const char pathname[], - /*out*/ iso9660_stat_t *statbuf, lsn_t lsn) +static iso9660_stat_t * +find_fs_lsn_recurse (const CdIo *cdio, const char pathname[], lsn_t lsn) { CdioList *entlist = iso9660_fs_readdir (cdio, pathname, true); CdioList *dirlist = _cdio_list_new (); @@ -302,25 +655,26 @@ find_fs_lsn_recurse (const CdIo *cdio, const char pathname[], _CDIO_LIST_FOREACH (entnode, entlist) { - char *name = _cdio_list_node_data (entnode); + iso9660_stat_t *statbuf = _cdio_list_node_data (entnode); char _fullname[4096] = { 0, }; + char *filename = (char *) statbuf->filename; - snprintf (_fullname, sizeof (_fullname), "%s%s", pathname, name); + snprintf (_fullname, sizeof (_fullname), "%s%s", pathname, filename); - if (iso9660_fs_stat (cdio, _fullname, statbuf, true)) - cdio_assert_not_reached (); - strncat (_fullname, "/", sizeof (_fullname)); if (statbuf->type == _STAT_DIR - && strcmp (name, ".") - && strcmp (name, "..")) + && strcmp ((char *) statbuf->filename, ".") + && strcmp ((char *) statbuf->filename, "..")) _cdio_list_append (dirlist, strdup (_fullname)); if (statbuf->lsn == lsn) { + unsigned int len=sizeof(iso9660_stat_t)+strlen(statbuf->filename)+1; + iso9660_stat_t *ret_stat = _cdio_malloc(len); + memcpy(ret_stat, statbuf, len); _cdio_list_free (entlist, true); _cdio_list_free (dirlist, true); - return true; + return ret_stat; } } @@ -332,26 +686,27 @@ find_fs_lsn_recurse (const CdIo *cdio, const char pathname[], _CDIO_LIST_FOREACH (entnode, dirlist) { char *_fullname = _cdio_list_node_data (entnode); + iso9660_stat_t *ret_stat = find_fs_lsn_recurse (cdio, _fullname, lsn); - if (find_fs_lsn_recurse (cdio, _fullname, statbuf, lsn)) { + if (NULL != ret_stat) { _cdio_list_free (dirlist, true); - return true; + return ret_stat; } } _cdio_list_free (dirlist, true); - return false; + return NULL; } /*! Given a directory pointer, find the filesystem entry that contains - lsn and return information about it in stat. + lsn and return information about it. - Returns true if we found an entry with the lsn and false if not. + Returns stat_t of entry if we found lsn, or NULL otherwise. */ -bool -iso9660_find_fs_lsn(const CdIo *cdio, lsn_t lsn, /*out*/ iso9660_stat_t *stat) +iso9660_stat_t * +iso9660_find_fs_lsn(const CdIo *cdio, lsn_t lsn) { - return find_fs_lsn_recurse (cdio, "/", stat, lsn); + return find_fs_lsn_recurse (cdio, "/", lsn); } diff --git a/src/input/vcd/libcdio/iso9660_private.h b/src/input/vcd/libcdio/iso9660_private.h index 411664c38..9a10950aa 100644 --- a/src/input/vcd/libcdio/iso9660_private.h +++ b/src/input/vcd/libcdio/iso9660_private.h @@ -1,5 +1,5 @@ /* - $Id: iso9660_private.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: iso9660_private.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel Copyright (C) 2003 Rocky Bernstein diff --git a/src/input/vcd/libcdio/logging.c b/src/input/vcd/libcdio/logging.c index 522d98d9a..8d561debe 100644 --- a/src/input/vcd/libcdio/logging.c +++ b/src/input/vcd/libcdio/logging.c @@ -1,7 +1,8 @@ /* - $Id: logging.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: logging.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel + Copyright (C) 2003, 2004 Rocky Bernstein 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 @@ -29,10 +30,9 @@ #include #include "cdio_assert.h" +static const char _rcsid[] = "$Id: logging.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; -static const char _rcsid[] = "$Id: logging.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $"; - -int cdio_loglevel_default = CDIO_LOG_WARN; +cdio_log_level_t cdio_loglevel_default = CDIO_LOG_WARN; static void default_cdio_log_handler (cdio_log_level_t level, const char message[]) diff --git a/src/input/vcd/libcdio/scsi_mmc.h b/src/input/vcd/libcdio/scsi_mmc.h index a0c22a86f..b6ea05b7a 100644 --- a/src/input/vcd/libcdio/scsi_mmc.h +++ b/src/input/vcd/libcdio/scsi_mmc.h @@ -1,5 +1,5 @@ /* - $Id: scsi_mmc.h,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: scsi_mmc.h,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2003 Rocky Bernstein diff --git a/src/input/vcd/libcdio/sector.c b/src/input/vcd/libcdio/sector.c index 24082bc2d..8ae28fc03 100644 --- a/src/input/vcd/libcdio/sector.c +++ b/src/input/vcd/libcdio/sector.c @@ -1,5 +1,5 @@ /* - $Id: sector.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: sector.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel @@ -32,7 +32,7 @@ #endif -static const char _rcsid[] = "$Id: sector.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $"; +static const char _rcsid[] = "$Id: sector.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; lba_t cdio_lba_to_lsn (lba_t lba) diff --git a/src/input/vcd/libcdio/util.c b/src/input/vcd/libcdio/util.c index 3b5b832cc..55717a15f 100644 --- a/src/input/vcd/libcdio/util.c +++ b/src/input/vcd/libcdio/util.c @@ -1,5 +1,5 @@ /* - $Id: util.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: util.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel Copyright (C) 2003 Rocky Bernstein @@ -31,7 +31,7 @@ #include "cdio_assert.h" #include -static const char _rcsid[] = "$Id: util.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $"; +static const char _rcsid[] = "$Id: util.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $"; size_t _cdio_strlenv(char **str_array) diff --git a/src/input/vcd/libcdio/xa.c b/src/input/vcd/libcdio/xa.c index 31c5e110a..dbf8abd3c 100644 --- a/src/input/vcd/libcdio/xa.c +++ b/src/input/vcd/libcdio/xa.c @@ -1,5 +1,5 @@ /* - $Id: xa.c,v 1.1 2003/10/13 11:47:12 f1rmb Exp $ + $Id: xa.c,v 1.2 2004/04/11 12:20:31 miguelfreitas Exp $ Copyright (C) 2000 Herbert Valerio Riedel Copyright (C) 2003 Rocky Bernstein -- cgit v1.2.3