diff options
author | arpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2002-12-29 15:32:56 +0000 |
---|---|---|
committer | arpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2002-12-29 15:32:56 +0000 |
commit | 36048f18e110f40a6d3571cefcd15882d54ef3ff (patch) | |
tree | 969adbed7e709cb6465c777146b8d7d6247f65cd | |
parent | 7f69df17784d0a7b09aefc08e6e89868565e8e0f (diff) | |
download | mpv-36048f18e110f40a6d3571cefcd15882d54ef3ff.tar.bz2 mpv-36048f18e110f40a6d3571cefcd15882d54ef3ff.tar.xz |
update libdvdcss in libmpdvdkit to latest version (1.2.4)
patch by Andreas Hess <jaska@gmx.net>
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@8638 b3059339-0415-0410-9bf9-f77b7e298cf2
-rw-r--r-- | libmpdvdkit2/css.c | 45 | ||||
-rw-r--r-- | libmpdvdkit2/device.c | 655 | ||||
-rw-r--r-- | libmpdvdkit2/device.h | 3 | ||||
-rw-r--r-- | libmpdvdkit2/ioctl.c | 71 | ||||
-rw-r--r-- | libmpdvdkit2/ioctl.h | 47 | ||||
-rw-r--r-- | libmpdvdkit2/libdvdcss.c | 10 | ||||
-rw-r--r-- | libmpdvdkit2/libdvdcss.h | 13 |
7 files changed, 481 insertions, 363 deletions
diff --git a/libmpdvdkit2/css.c b/libmpdvdkit2/css.c index 83fcec8317..c5ad40ab8d 100644 --- a/libmpdvdkit2/css.c +++ b/libmpdvdkit2/css.c @@ -41,6 +41,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <unistd.h> #include "dvdcss.h" @@ -81,18 +82,27 @@ int _dvdcss_test( dvdcss_t dvdcss ) i_ret = ioctl_ReadCopyright( dvdcss->i_fd, 0 /* i_layer */, &i_copyright ); +#ifdef WIN32 + if( i_ret < 0 ) + { + /* Maybe we didn't have enough priviledges to read the copyright + * (see ioctl_ReadCopyright comments). + * Apparently, on unencrypted DVDs _dvdcss_disckey() always fails, so + * we can check this as a work-around. */ + i_ret = 0; + if( _dvdcss_disckey( dvdcss ) < 0 ) + i_copyright = 0; + else + i_copyright = 1; + } +#endif + if( i_ret < 0 ) { /* Since it's the first ioctl we try to issue, we add a notice */ _dvdcss_error( dvdcss, "css error: ioctl_ReadCopyright failed, " "make sure there is a DVD in the drive, and that " - "you have used the correct device node." -#if defined( WIN32 ) - "\nAlso note that if you are using Windows NT/2000/XP " - "you need to have administrator priviledges to be able " - "to use ioctls." -#endif - ); + "you have used the correct device node." ); return i_ret; } @@ -264,7 +274,7 @@ static void PrintKey( dvdcss_t dvdcss, char *prefix, u8 const *data ) /***************************************************************************** * _dvdcss_title: crack or decrypt the current title key if needed ***************************************************************************** - * This function should only be called by dvdcss_seek and should eventually + * This function should only be called by dvdcss->pf_seek and should eventually * not be external if possible. *****************************************************************************/ int _dvdcss_title ( dvdcss_t dvdcss, int i_block ) @@ -427,9 +437,12 @@ int _dvdcss_disckey( dvdcss_t dvdcss ) break; } _dvdcss_debug( dvdcss, "failed to decrypt the disc key, " - "trying to crack it instead" ); + "faulty drive/kernel? " + "cracking title keys instead" ); - /* Fallback */ + /* Fallback, but not to DISC as the disc key might be faulty */ + dvdcss->i_method = DVDCSS_METHOD_TITLE; + break; case DVDCSS_METHOD_DISC: @@ -443,6 +456,7 @@ int _dvdcss_disckey( dvdcss_t dvdcss ) break; } _dvdcss_debug( dvdcss, "failed to crack the disc key" ); + memset( p_disc_key, 0, KEY_SIZE ); dvdcss->i_method = DVDCSS_METHOD_TITLE; break; @@ -551,9 +565,9 @@ int _dvdcss_titlekey( dvdcss_t dvdcss, int i_pos, dvd_key_t p_title_key ) _dvdcss_debug( dvdcss, "resetting drive and cracking title key" ); /* Read an unscrambled sector and reset the drive */ - _dvdcss_seek( dvdcss, 0 ); - _dvdcss_read( dvdcss, p_garbage, 1 ); - _dvdcss_seek( dvdcss, 0 ); + dvdcss->pf_seek( dvdcss, 0 ); + dvdcss->pf_read( dvdcss, p_garbage, 1 ); + dvdcss->pf_seek( dvdcss, 0 ); _dvdcss_disckey( dvdcss ); /* Fallback */ @@ -978,7 +992,7 @@ static int DecryptDiscKey( u8 const *p_struct_disckey, dvd_key_t p_disc_key ) DecryptKey( 0, p_disc_key, p_struct_disckey, p_verify ); /* If the position / player key pair worked then return. */ - if( memcmp( p_disc_key, p_verify, 5 ) == 0 ) + if( memcmp( p_disc_key, p_verify, KEY_SIZE ) == 0 ) { return 0; } @@ -988,6 +1002,7 @@ static int DecryptDiscKey( u8 const *p_struct_disckey, dvd_key_t p_disc_key ) /* Have tried all combinations of positions and keys, * and we still didn't succeed. */ + memset( p_disc_key, 0, KEY_SIZE ); return -1; } @@ -1383,7 +1398,7 @@ static int CrackTitleKey( dvdcss_t dvdcss, int i_pos, int i_len, do { - i_ret = _dvdcss_seek( dvdcss, i_pos ); + i_ret = dvdcss->pf_seek( dvdcss, i_pos ); if( i_ret != i_pos ) { diff --git a/libmpdvdkit2/device.c b/libmpdvdkit2/device.c index f3aedbee8a..a75035c18e 100644 --- a/libmpdvdkit2/device.c +++ b/libmpdvdkit2/device.c @@ -54,67 +54,34 @@ #include "device.h" /***************************************************************************** - * Device reading prototypes, win32 specific + * Device reading prototypes *****************************************************************************/ +static int libc_open ( dvdcss_t, char const * ); +static int libc_seek ( dvdcss_t, int ); +static int libc_read ( dvdcss_t, void *, int ); +static int libc_readv ( dvdcss_t, struct iovec *, int ); + #ifdef WIN32 -int _win32_dvdcss_readv ( int, struct iovec *, int, char * ); -int _win32_dvdcss_aopen ( dvdcss_t, char ); -int _win32_dvdcss_aclose ( int ); -int _win32_dvdcss_aseek ( int, int, int ); -int _win32_dvdcss_aread ( int, void *, int ); +static int win2k_open ( dvdcss_t, char const * ); +static int aspi_open ( dvdcss_t, char const * ); +static int win2k_seek ( dvdcss_t, int ); +static int aspi_seek ( dvdcss_t, int ); +static int win2k_read ( dvdcss_t, void *, int ); +static int aspi_read ( dvdcss_t, void *, int ); +static int win_readv ( dvdcss_t, struct iovec *, int ); + +static int aspi_read_internal ( int, void *, int ); #endif -/***************************************************************************** - * readv_*: readv() replacements for iovec-impaired C libraries - *****************************************************************************/ -#if defined( WIN32 ) -static inline int readv( int i_fd, struct iovec * p_iovec, int i_count ) +int _dvdcss_use_ioctls( dvdcss_t dvdcss ) { - int i_index, i_len, i_total = 0; - unsigned char *p_base; - int i_bytes; - - for( i_index = i_count; i_index; i_index-- ) +#if defined( WIN32 ) + if( dvdcss->b_file ) { - i_len = p_iovec->iov_len; - p_base = p_iovec->iov_base; - - /* Loop is unrolled one time to spare the (i_bytes <= 0) test */ - - if( i_len > 0 ) - { - i_bytes = read( i_fd, p_base, i_len ); - - if( i_bytes < 0 ) - { - /* One of the reads failed, too bad. - We won't even bother returning the reads that went ok, - and as in the posix spec the file postition is left - unspecified after a failure */ - return -1; - } - - i_total += i_bytes; - - if( i_bytes != i_len ) - { - /* we reached the end of the file or a signal interrupted - the read */ - return i_total; - } - } - - p_iovec++; + return 0; } - return i_total; -} -#endif /* WIN32 */ - -int _dvdcss_use_ioctls( dvdcss_t dvdcss ) -{ -#if defined( WIN32 ) - /* Some one need to implement this for Windows */ + /* FIXME: implement this for Windows */ if( WIN2K ) { return 1; @@ -161,68 +128,50 @@ int _dvdcss_use_ioctls( dvdcss_t dvdcss ) int _dvdcss_open ( dvdcss_t dvdcss ) { - char *psz_device = dvdcss->psz_device; - -#if defined( WIN32 ) - if( WIN2K ) - { - char psz_dvd[7]; - _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_device[0] ); - - /* To have access to ioctls, we need read and write access to the - * device. This is only allowed if you have administrator priviledges - * so we allow for a fallback method where ioctls are not available but - * we at least have read access to the device. - * (See Microsoft Q241374: Read and Write Access Required for SCSI - * Pass Through Requests) */ - (HANDLE) dvdcss->i_fd = - CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, - FILE_FLAG_RANDOM_ACCESS, NULL ); + char psz_debug[200]; + char const *psz_device = dvdcss->psz_device; - if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) - (HANDLE) dvdcss->i_fd = - CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, - FILE_FLAG_RANDOM_ACCESS, NULL ); + snprintf( psz_debug, 199, "opening target `%s'", psz_device ); + psz_debug[199] = '\0'; + _dvdcss_debug( dvdcss, psz_debug ); - if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) - { - _dvdcss_error( dvdcss, "failed opening device" ); - return -1; - } - } - else - { - dvdcss->i_fd = _win32_dvdcss_aopen( dvdcss, psz_device[0] ); - if( dvdcss->i_fd == -1 ) - { - _dvdcss_error( dvdcss, "failed opening device" ); - return -1; - } - } +#if defined( WIN32 ) + /* If device is not "X:", we are actually opening a file. */ + dvdcss->b_file = !psz_device[0] || psz_device[1] != ':' || psz_device[3]; - /* initialise readv temporary buffer */ + /* Initialize readv temporary buffer */ dvdcss->p_readv_buffer = NULL; dvdcss->i_readv_buf_size = 0; -#else - dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, 0 ); - - if( dvdcss->i_fd == -1 ) + if( !dvdcss->b_file && WIN2K ) { - _dvdcss_error( dvdcss, "failed opening device" ); - return -1; + _dvdcss_debug( dvdcss, "using Win2K API for access" ); + dvdcss->pf_seek = win2k_seek; + dvdcss->pf_read = win2k_read; + dvdcss->pf_readv = win_readv; + return win2k_open( dvdcss, psz_device ); } - + else if( !dvdcss->b_file ) + { + _dvdcss_debug( dvdcss, "using ASPI for access" ); + dvdcss->pf_seek = aspi_seek; + dvdcss->pf_read = aspi_read; + dvdcss->pf_readv = win_readv; + return aspi_open( dvdcss, psz_device ); + } + else #endif - - return 0; + { + _dvdcss_debug( dvdcss, "using libc for access" ); + dvdcss->pf_seek = libc_seek; + dvdcss->pf_read = libc_read; + dvdcss->pf_readv = libc_readv; + return libc_open( dvdcss, psz_device ); + } } #ifndef WIN32 -int _dvdcss_raw_open ( dvdcss_t dvdcss, char *psz_device ) +int _dvdcss_raw_open ( dvdcss_t dvdcss, char const *psz_device ) { dvdcss->i_raw_fd = open( psz_device, 0 ); @@ -243,13 +192,21 @@ int _dvdcss_raw_open ( dvdcss_t dvdcss, char *psz_device ) int _dvdcss_close ( dvdcss_t dvdcss ) { #if defined( WIN32 ) - if( WIN2K ) + if( dvdcss->b_file ) + { + close( dvdcss->i_fd ); + } + else if( WIN2K ) { CloseHandle( (HANDLE) dvdcss->i_fd ); } - else + else /* ASPI */ { - _win32_dvdcss_aclose( dvdcss->i_fd ); + struct w32_aspidev *fd = (struct w32_aspidev *) dvdcss->i_fd; + + /* Unload aspi and free w32_aspidev structure */ + FreeLibrary( (HMODULE) fd->hASPI ); + free( (void*) dvdcss->i_fd ); } /* Free readv temporary buffer */ @@ -260,6 +217,7 @@ int _dvdcss_close ( dvdcss_t dvdcss ) dvdcss->i_readv_buf_size = 0; } + return 0; #else close( dvdcss->i_fd ); @@ -269,198 +227,68 @@ int _dvdcss_close ( dvdcss_t dvdcss ) dvdcss->i_raw_fd = -1; } -#endif - return 0; -} - -int _dvdcss_seek ( dvdcss_t dvdcss, int i_blocks ) -{ -#if defined( WIN32 ) - dvdcss->i_seekpos = i_blocks; - - if( WIN2K ) - { - LARGE_INTEGER li_read; - -#ifndef INVALID_SET_FILE_POINTER -#define INVALID_SET_FILE_POINTER ((DWORD)-1) -#endif - - li_read.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE; - - li_read.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd, - li_read.LowPart, - &li_read.HighPart, FILE_BEGIN ); - if( (li_read.LowPart == INVALID_SET_FILE_POINTER) - && GetLastError() != NO_ERROR) - { - li_read.QuadPart = -DVDCSS_BLOCK_SIZE; - } - - li_read.QuadPart /= DVDCSS_BLOCK_SIZE; - return (int)li_read.QuadPart; - } - else - { - return ( _win32_dvdcss_aseek( dvdcss->i_fd, i_blocks, SEEK_SET ) ); - } -#else - off_t i_read; - - dvdcss->i_seekpos = i_blocks; - - i_read = lseek( dvdcss->i_read_fd, - (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET ); - - if( i_read < 0 ) - { - _dvdcss_error( dvdcss, "seek error" ); - } - - return i_read / DVDCSS_BLOCK_SIZE; #endif - } -int _dvdcss_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) -{ -#if defined( WIN32 ) - if( WIN2K ) - { - int i_bytes; - - if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer, - i_blocks * DVDCSS_BLOCK_SIZE, - (LPDWORD)&i_bytes, NULL ) ) - { - return -1; - } - return i_bytes / DVDCSS_BLOCK_SIZE; - } - else - { - return _win32_dvdcss_aread( dvdcss->i_fd, p_buffer, i_blocks ); - } +/* Following functions are local */ +/***************************************************************************** + * Open commands. + *****************************************************************************/ +static int libc_open ( dvdcss_t dvdcss, char const *psz_device ) +{ +#if !defined( WIN32 ) + dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, 0 ); #else - int i_ret; - /* TODO: partial reads are wrong,i.e 2200/2048 = 1 - * but the location has advanced 2200 bytes (lseek possition that is) */ - i_ret = read( dvdcss->i_read_fd, p_buffer, - (off_t)i_blocks * DVDCSS_BLOCK_SIZE ); - if( i_ret < 0 ) - { - _dvdcss_error( dvdcss, "read error" ); - return i_ret; - } - - return i_ret / DVDCSS_BLOCK_SIZE; + dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, O_BINARY ); #endif -} - -int _dvdcss_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks ) -{ - int i_read; - -#if defined( WIN32 ) - /* Check the size of the readv temp buffer, just in case we need to - * realloc something bigger */ - if( dvdcss->i_readv_buf_size < i_blocks * DVDCSS_BLOCK_SIZE ) + if( dvdcss->i_fd == -1 ) { - dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE; - - if( dvdcss->p_readv_buffer ) free( dvdcss->p_readv_buffer ); - - /* Allocate a buffer which will be used as a temporary storage - * for readv */ - dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size ); - if( !dvdcss->p_readv_buffer ) - { - _dvdcss_error( dvdcss, " failed (readv)" ); - return -1; - } + _dvdcss_error( dvdcss, "failed opening device" ); + return -1; } - i_read = _win32_dvdcss_readv( dvdcss->i_fd, p_iovec, i_blocks, - dvdcss->p_readv_buffer ); - return i_read; - -#else - i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks ); - return i_read / DVDCSS_BLOCK_SIZE; - -#endif + return 0; } - #if defined( WIN32 ) - -/***************************************************************************** - * _win32_dvdcss_readv: vectored read using ReadFile for Win2K and - * _win32_dvdcss_aread for win9x - *****************************************************************************/ -int _win32_dvdcss_readv( int i_fd, struct iovec *p_iovec, - int i_num_buffers, char *p_tmp_buffer ) +static int win2k_open ( dvdcss_t dvdcss, char const *psz_device ) { - int i_index; - int i_blocks, i_blocks_total = 0; - - for( i_index = i_num_buffers; i_index; i_index-- ) - { - i_blocks_total += p_iovec[i_index-1].iov_len; - } - - if( i_blocks_total <= 0 ) return 0; + char psz_dvd[7]; + _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_device[0] ); + + /* To work around an M$ bug in IOCTL_DVD_READ_STRUCTURE, we need read + * _and_ write access to the device (so we can make SCSI Pass Through + * Requests). Unfortunately this is only allowed if you have + * administrator priviledges so we allow for a fallback method with + * only read access to the device (in this case ioctl_ReadCopyright() + * won't send back the right result). + * (See Microsoft Q241374: Read and Write Access Required for SCSI + * Pass Through Requests) */ + (HANDLE) dvdcss->i_fd = + CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, + FILE_FLAG_RANDOM_ACCESS, NULL ); - i_blocks_total /= DVDCSS_BLOCK_SIZE; + if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) + (HANDLE) dvdcss->i_fd = + CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ, + NULL, OPEN_EXISTING, + FILE_FLAG_RANDOM_ACCESS, NULL ); - if( WIN2K ) - { - unsigned long int i_bytes; - if( !ReadFile( (HANDLE)i_fd, p_tmp_buffer, - i_blocks_total * DVDCSS_BLOCK_SIZE, &i_bytes, NULL ) ) - { - return -1; - /* The read failed... too bad. - As in the posix spec the file postition is left - unspecified after a failure */ - } - i_blocks = i_bytes / DVDCSS_BLOCK_SIZE; - } - else /* Win9x */ + if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) { - i_blocks = _win32_dvdcss_aread( i_fd, p_tmp_buffer, i_blocks_total ); - if( i_blocks < 0 ) - { - return -1; /* idem */ - } + _dvdcss_error( dvdcss, "failed opening device" ); + return -1; } - /* We just have to copy the content of the temp buffer into the iovecs */ - i_index = 0; - i_blocks_total = i_blocks; - while( i_blocks_total > 0 ) - { - memcpy( p_iovec[i_index].iov_base, - &p_tmp_buffer[(i_blocks - i_blocks_total) * DVDCSS_BLOCK_SIZE], - p_iovec[i_index].iov_len ); - /* if we read less blocks than asked, we'll just end up copying - garbage, this isn't an issue as we return the number of - blocks actually read */ - i_blocks_total -= ( p_iovec[i_index].iov_len / DVDCSS_BLOCK_SIZE ); - i_index++; - } - - return i_blocks; + return 0; } -/***************************************************************************** - * _win32_dvdcss_aopen: open dvd drive (load aspi and init w32_aspidev - * structure) - *****************************************************************************/ -int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive ) +static int aspi_open( dvdcss_t dvdcss, char const * psz_device ) { HMODULE hASPI; DWORD dwSupportInfo; @@ -468,7 +296,9 @@ int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive ) int i, j, i_hostadapters; long (*lpGetSupport)( void ); long (*lpSendCommand)( void* ); + char c_drive = psz_device[0]; + /* load aspi and init w32_aspidev structure */ hASPI = LoadLibrary( "wnaspi32.dll" ); if( hASPI == NULL ) { @@ -481,7 +311,7 @@ int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive ) if(lpGetSupport == NULL || lpSendCommand == NULL ) { - _dvdcss_debug( dvdcss, "unable to get aspi function pointers" ); + _dvdcss_error( dvdcss, "unable to get aspi function pointers" ); FreeLibrary( hASPI ); return -1; } @@ -490,7 +320,7 @@ int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive ) if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) { - _dvdcss_debug( dvdcss, "no host adapters found (aspi)" ); + _dvdcss_error( dvdcss, "no ASPI adapters found" ); FreeLibrary( hASPI ); return -1; } @@ -505,6 +335,7 @@ int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive ) i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) ); if( i_hostadapters == 0 ) { + _dvdcss_error( dvdcss, "no ASPI adapters ready" ); FreeLibrary( hASPI ); return -1; } @@ -512,6 +343,7 @@ int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive ) fd = malloc( sizeof( struct w32_aspidev ) ); if( fd == NULL ) { + _dvdcss_error( dvdcss, "not enough memory" ); FreeLibrary( hASPI ); return -1; } @@ -540,47 +372,97 @@ int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive ) if( (srbDiskInfo.SRB_Status == SS_COMP) && (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) ) { - fd->i_sid = MAKEWORD( i, j ); - return (int) fd; + /* 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 ) ) + { + fd->i_sid = MAKEWORD( i, j ); + dvdcss->i_fd = (int) fd; + return 0; + } + else + { + free( (void*) fd ); + FreeLibrary( hASPI ); + _dvdcss_error( dvdcss,"this is not a cdrom drive" ); + return -1; + } } } } free( (void*) fd ); FreeLibrary( hASPI ); - _dvdcss_debug( dvdcss, "unable to get haid and target (aspi)" ); - return( -1 ); + _dvdcss_error( dvdcss, "unable to get haid and target (aspi)" ); + return -1; } +#endif /***************************************************************************** - * _win32_dvdcss_aclose: close dvd drive (unload aspi and free w32_aspidev - * structure) + * Seek commands. *****************************************************************************/ -int _win32_dvdcss_aclose( int i_fd ) +static int libc_seek( dvdcss_t dvdcss, int i_blocks ) { - struct w32_aspidev *fd = (struct w32_aspidev *) i_fd; + off_t i_read; - FreeLibrary( (HMODULE) fd->hASPI ); - free( (void*) i_fd ); + dvdcss->i_seekpos = i_blocks; - return 0; + i_read = lseek( dvdcss->i_read_fd, + (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET ); + + if( i_read < 0 ) + { + _dvdcss_error( dvdcss, "seek error" ); + } + + return i_read / DVDCSS_BLOCK_SIZE; } -/***************************************************************************** - * _win32_dvdcss_aseek: aspi version of _dvdcss_seek - * - * returns the number of blocks read. - *****************************************************************************/ -int _win32_dvdcss_aseek( int i_fd, int i_blocks, int i_method ) +#if defined( WIN32 ) +static int win2k_seek( dvdcss_t dvdcss, int i_blocks ) +{ + LARGE_INTEGER li_read; + + dvdcss->i_seekpos = i_blocks; + +#ifndef INVALID_SET_FILE_POINTER +# define INVALID_SET_FILE_POINTER ((DWORD)-1) +#endif + + li_read.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE; + + li_read.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd, + li_read.LowPart, + &li_read.HighPart, FILE_BEGIN ); + if( (li_read.LowPart == INVALID_SET_FILE_POINTER) + && GetLastError() != NO_ERROR) + { + li_read.QuadPart = -DVDCSS_BLOCK_SIZE; + } + + li_read.QuadPart /= DVDCSS_BLOCK_SIZE; + return (int)li_read.QuadPart; +} + +static int aspi_seek( dvdcss_t dvdcss, int i_blocks ) { int i_old_blocks; char sz_buf[ DVDCSS_BLOCK_SIZE ]; - struct w32_aspidev *fd = (struct w32_aspidev *) i_fd; + struct w32_aspidev *fd = (struct w32_aspidev *) dvdcss->i_fd; i_old_blocks = fd->i_blocks; fd->i_blocks = i_blocks; - if( _win32_dvdcss_aread( i_fd, sz_buf, 1 ) == -1 ) + if( aspi_read_internal( dvdcss->i_fd, sz_buf, 1 ) == -1 ) { fd->i_blocks = i_old_blocks; return -1; @@ -590,13 +472,179 @@ int _win32_dvdcss_aseek( int i_fd, int i_blocks, int i_method ) return fd->i_blocks; } +#endif /***************************************************************************** - * _win32_dvdcss_aread: aspi version of _dvdcss_read - * - * returns the number of blocks read. + * Read commands. *****************************************************************************/ -int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks ) +static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) +{ + int i_ret; + /* TODO: partial reads are wrong,i.e 2200/2048 = 1 + * but the location has advanced 2200 bytes (lseek possition that is) */ + i_ret = read( dvdcss->i_read_fd, p_buffer, + (off_t)i_blocks * DVDCSS_BLOCK_SIZE ); + if( i_ret < 0 ) + { + _dvdcss_error( dvdcss, "read error" ); + return i_ret; + } + + return i_ret / DVDCSS_BLOCK_SIZE; +} + +#if defined( WIN32 ) +static int win2k_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) +{ + int i_bytes; + + if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer, + i_blocks * DVDCSS_BLOCK_SIZE, + (LPDWORD)&i_bytes, NULL ) ) + { + return -1; + } + + return i_bytes / DVDCSS_BLOCK_SIZE; +} + +static int aspi_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) +{ + return aspi_read_internal( dvdcss->i_fd, p_buffer, i_blocks ); +} +#endif + +/***************************************************************************** + * Readv commands. + *****************************************************************************/ +static int libc_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks ) +{ +#if defined( WIN32 ) + int i_index, i_len, i_total = 0; + unsigned char *p_base; + int i_bytes; + + for( i_index = i_blocks; + i_index; + i_index--, p_iovec++ ) + { + i_len = p_iovec->iov_len; + p_base = p_iovec->iov_base; + + if( i_len <= 0 ) + { + continue; + } + + i_bytes = read( dvdcss->i_fd, p_base, i_len ); + + if( i_bytes < 0 ) + { + /* One of the reads failed, too bad. + * We won't even bother returning the reads that went ok, + * and as in the posix spec the file postition is left + * unspecified after a failure */ + return -1; + } + + i_total += i_bytes; + + if( i_bytes != i_len ) + { + /* We reached the end of the file or a signal interrupted + * the read. Return a partial read. */ + return i_total / DVDCSS_BLOCK_SIZE; + } + } + + return i_total / DVDCSS_BLOCK_SIZE; +#else + int i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks ); + + return i_read / DVDCSS_BLOCK_SIZE; +#endif +} + +#if defined( WIN32 ) +/***************************************************************************** + * win_readv: vectored read using ReadFile for Win2K and ASPI for win9x + *****************************************************************************/ +static int win_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks ) +{ + int i_index; + int i_blocks_read, i_blocks_total = 0; + + /* Check the size of the readv temp buffer, just in case we need to + * realloc something bigger */ + if( dvdcss->i_readv_buf_size < i_blocks * DVDCSS_BLOCK_SIZE ) + { + dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE; + + if( dvdcss->p_readv_buffer ) free( dvdcss->p_readv_buffer ); + + /* Allocate a buffer which will be used as a temporary storage + * for readv */ + dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size ); + if( !dvdcss->p_readv_buffer ) + { + _dvdcss_error( dvdcss, " failed (readv)" ); + return -1; + } + } + + for( i_index = i_blocks; i_index; i_index-- ) + { + i_blocks_total += p_iovec[i_index-1].iov_len; + } + + if( i_blocks_total <= 0 ) return 0; + + i_blocks_total /= DVDCSS_BLOCK_SIZE; + + if( WIN2K ) + { + unsigned long int i_bytes; + if( !ReadFile( (HANDLE)dvdcss->i_fd, dvdcss->p_readv_buffer, + i_blocks_total * DVDCSS_BLOCK_SIZE, &i_bytes, NULL ) ) + { + /* The read failed... too bad. + * As in the posix spec the file postition is left + * unspecified after a failure */ + return -1; + } + i_blocks_read = i_bytes / DVDCSS_BLOCK_SIZE; + } + else /* Win9x */ + { + i_blocks_read = aspi_read_internal( dvdcss->i_fd, + dvdcss->p_readv_buffer, + i_blocks_total ); + if( i_blocks_read < 0 ) + { + /* See above */ + return -1; + } + } + + /* We just have to copy the content of the temp buffer into the iovecs */ + for( i_index = 0, i_blocks_total = i_blocks_read; + i_blocks_total > 0; + i_index++ ) + { + memcpy( p_iovec[i_index].iov_base, + dvdcss->p_readv_buffer + (i_blocks_read - i_blocks_total) + * DVDCSS_BLOCK_SIZE, + p_iovec[i_index].iov_len ); + /* if we read less blocks than asked, we'll just end up copying + * garbage, this isn't an issue as we return the number of + * blocks actually read */ + i_blocks_total -= ( p_iovec[i_index].iov_len / DVDCSS_BLOCK_SIZE ); + } + + return i_blocks_read; +} + +static int aspi_read_internal( int i_fd, void *p_data, int i_blocks ) { HANDLE hEvent; struct SRB_ExecSCSICmd ssc; @@ -638,9 +686,9 @@ int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks ) ResetEvent( hEvent ); fd->lpSendCommand( (void*) &ssc ); - /* transfer the next 64kb (_win32_dvdcss_aread is called recursively) + /* transfer the next 64kb (aspi_read_internal is called recursively) * We need to check the status of the read on return */ - if( _win32_dvdcss_aread( i_fd, (u8*) p_data + 32 * DVDCSS_BLOCK_SIZE, + if( aspi_read_internal( i_fd, (u8*) p_data + 32 * DVDCSS_BLOCK_SIZE, i_blocks - 32) < 0 ) { return -1; @@ -674,6 +722,5 @@ int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks ) return i_blocks; } - #endif diff --git a/libmpdvdkit2/device.h b/libmpdvdkit2/device.h index 72648a97bb..c50e2c00eb 100644 --- a/libmpdvdkit2/device.h +++ b/libmpdvdkit2/device.h @@ -46,12 +46,11 @@ struct iovec int _dvdcss_use_ioctls ( dvdcss_t ); int _dvdcss_open ( dvdcss_t ); int _dvdcss_close ( dvdcss_t ); -int _dvdcss_readv ( dvdcss_t, struct iovec *, int ); /***************************************************************************** * Device reading prototypes, raw-device specific *****************************************************************************/ #ifndef WIN32 -int _dvdcss_raw_open ( dvdcss_t, char * ); +int _dvdcss_raw_open ( dvdcss_t, char const * ); #endif |