Mercurial > mplayer.hg
changeset 8637:0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
patch by Andreas Hess <jaska@gmx.net>
author | arpi |
---|---|
date | Sun, 29 Dec 2002 15:32:56 +0000 |
parents | db4a1b1254cb |
children | a2fa276b8ec7 |
files | libmpdvdkit2/css.c libmpdvdkit2/device.c libmpdvdkit2/device.h libmpdvdkit2/ioctl.c libmpdvdkit2/ioctl.h libmpdvdkit2/libdvdcss.c libmpdvdkit2/libdvdcss.h |
diffstat | 7 files changed, 491 insertions(+), 373 deletions(-) [+] |
line wrap: on
line diff
--- a/libmpdvdkit2/css.c Sun Dec 29 15:10:02 2002 +0000 +++ b/libmpdvdkit2/css.c Sun Dec 29 15:32:56 2002 +0000 @@ -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 @@ 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 @@ /***************************************************************************** * _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 @@ 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 @@ 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 @@ _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 @@ 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 @@ /* 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 @@ do { - i_ret = _dvdcss_seek( dvdcss, i_pos ); + i_ret = dvdcss->pf_seek( dvdcss, i_pos ); if( i_ret != i_pos ) {
--- a/libmpdvdkit2/device.c Sun Dec 29 15:10:02 2002 +0000 +++ b/libmpdvdkit2/device.c Sun Dec 29 15:32:56 2002 +0000 @@ -54,67 +54,34 @@ #include "device.h" /***************************************************************************** - * Device reading prototypes, win32 specific - *****************************************************************************/ -#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 ); -#endif - -/***************************************************************************** - * readv_*: readv() replacements for iovec-impaired C libraries + * Device reading prototypes *****************************************************************************/ -#if defined( WIN32 ) -static inline int readv( int i_fd, struct iovec * p_iovec, int i_count ) -{ - int i_index, i_len, i_total = 0; - unsigned char *p_base; - int i_bytes; - - for( i_index = i_count; i_index; i_index-- ) - { - i_len = p_iovec->iov_len; - p_base = p_iovec->iov_base; - - /* Loop is unrolled one time to spare the (i_bytes <= 0) test */ +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 ); - 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; +#ifdef WIN32 +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 ); - if( i_bytes != i_len ) - { - /* we reached the end of the file or a signal interrupted - the read */ - return i_total; - } - } - - p_iovec++; - } - - return i_total; -} -#endif /* WIN32 */ +static int aspi_read_internal ( int, void *, int ); +#endif int _dvdcss_use_ioctls( dvdcss_t dvdcss ) { #if defined( WIN32 ) - /* Some one need to implement this for Windows */ + if( dvdcss->b_file ) + { + return 0; + } + + /* FIXME: implement this for Windows */ if( WIN2K ) { return 1; @@ -161,68 +128,50 @@ int _dvdcss_open ( dvdcss_t dvdcss ) { - char *psz_device = dvdcss->psz_device; + char psz_debug[200]; + char const *psz_device = dvdcss->psz_device; + + snprintf( psz_debug, 199, "opening target `%s'", psz_device ); + psz_debug[199] = '\0'; + _dvdcss_debug( dvdcss, psz_debug ); #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 ); + /* If device is not "X:", we are actually opening a file. */ + dvdcss->b_file = !psz_device[0] || psz_device[1] != ':' || psz_device[3]; - 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( (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; - } - } - - /* 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_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_error( dvdcss, "failed opening device" ); - return -1; + _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_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 @@ dvdcss->i_readv_buf_size = 0; } + return 0; #else close( dvdcss->i_fd ); @@ -269,198 +227,68 @@ dvdcss->i_raw_fd = -1; } + return 0; #endif +} + +/* 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 + dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, O_BINARY ); +#endif + + if( dvdcss->i_fd == -1 ) + { + _dvdcss_error( dvdcss, "failed opening device" ); + return -1; + } return 0; } -int _dvdcss_seek ( dvdcss_t dvdcss, int i_blocks ) +#if defined( WIN32 ) +static int win2k_open ( dvdcss_t dvdcss, char const *psz_device ) { -#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; + char psz_dvd[7]; + _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_device[0] ); - 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; - } + /* 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 ); - li_read.QuadPart /= DVDCSS_BLOCK_SIZE; - return (int)li_read.QuadPart; - } - else + 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( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) { - 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" ); + _dvdcss_error( dvdcss, "failed opening device" ); + return -1; } - 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 ); - } - -#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; -#endif - + return 0; } -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 ) - { - 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; - } - } - - 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 -} - - -#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 ) -{ - 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; - - i_blocks_total /= DVDCSS_BLOCK_SIZE; - - 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 */ - { - i_blocks = _win32_dvdcss_aread( i_fd, p_tmp_buffer, i_blocks_total ); - if( i_blocks < 0 ) - { - return -1; /* idem */ - } - } - - /* 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; -} - -/***************************************************************************** - * _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 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 @@ 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 @@ 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 @@ i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) ); if( i_hostadapters == 0 ) { + _dvdcss_error( dvdcss, "no ASPI adapters ready" ); FreeLibrary( hASPI ); return -1; } @@ -512,6 +343,7 @@ fd = malloc( sizeof( struct w32_aspidev ) ); if( fd == NULL ) { + _dvdcss_error( dvdcss, "not enough memory" ); FreeLibrary( hASPI ); return -1; } @@ -540,47 +372,97 @@ 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; + + dvdcss->i_seekpos = i_blocks; + + i_read = lseek( dvdcss->i_read_fd, + (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET ); - FreeLibrary( (HMODULE) fd->hASPI ); - free( (void*) i_fd ); + if( i_read < 0 ) + { + _dvdcss_error( dvdcss, "seek error" ); + } - return 0; + 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 @@ return fd->i_blocks; } +#endif + +/***************************************************************************** + * Read commands. + *****************************************************************************/ +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 /***************************************************************************** - * _win32_dvdcss_aread: aspi version of _dvdcss_read - * - * returns the number of blocks read. + * 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 *****************************************************************************/ -int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks ) +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 @@ 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 @@ return i_blocks; } - #endif
--- a/libmpdvdkit2/device.h Sun Dec 29 15:10:02 2002 +0000 +++ b/libmpdvdkit2/device.h Sun Dec 29 15:32:56 2002 +0000 @@ -46,12 +46,11 @@ 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
--- a/libmpdvdkit2/ioctl.c Sun Dec 29 15:10:02 2002 +0000 +++ b/libmpdvdkit2/ioctl.c Sun Dec 29 15:32:56 2002 +0000 @@ -11,6 +11,7 @@ * Eugenio Jarosiewicz <ej0@cise.ufl.edu> * David Siebörger <drs-videolan@rucus.ru.ac.za> * Alex Strelnikov <lelik@os2.ru> + * Gildas Bazin <gbazin@netcourrier.com> * * 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 @@ -214,18 +215,20 @@ *pi_copyright = dvdbs.copyrightProtectionSystemType; #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 p_buffer[ 8 ]; SCSI_PASS_THROUGH_DIRECT sptd; + /* When using IOCTL_DVD_READ_STRUCTURE and + DVD_COPYRIGHT_DESCRIPTOR, CopyrightProtectionType + seems to be always 6 ??? + To work around this M$ bug we try to send a raw scsi command + instead (if we've got enough privileges to do so). */ + memset( &sptd, 0, sizeof( sptd ) ); memset( &p_buffer, 0, sizeof( p_buffer ) ); - - /* When using IOCTL_DVD_READ_STRUCTURE and - DVD_COPYRIGHT_DESCRIPTOR, CopyrightProtectionType - is always 6. So we send a raw scsi command instead. */ sptd.Length = sizeof( SCSI_PASS_THROUGH_DIRECT ); sptd.CdbLength = 12; @@ -245,8 +248,11 @@ &sptd, sizeof( SCSI_PASS_THROUGH_DIRECT ), &tmp, NULL ) ? 0 : -1; + if( i_ret == 0 ) + { *pi_copyright = p_buffer[ 4 ]; } + } else { INIT_SSC( GPCMD_READ_DVD_STRUCTURE, 8 ); @@ -387,7 +393,7 @@ memcpy( p_key, dvdbs.discKeyStructures, DVD_DISCKEY_SIZE ); #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[DVD_DISK_KEY_LENGTH]; @@ -555,7 +561,7 @@ memcpy( p_key, dvdbs.titleKeyValue, DVD_KEY_SIZE ); #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[DVD_BUS_KEY_LENGTH]; @@ -567,7 +573,8 @@ key->SessionId = *pi_agid; key->KeyType = DvdTitleKey; key->KeyFlags = 0; - key->Parameters.TitleOffset.QuadPart = (LONGLONG) i_pos; + key->Parameters.TitleOffset.QuadPart = (LONGLONG) i_pos * + 2048 /*DVDCSS_BLOCK_SIZE*/; i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key, key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1; @@ -700,7 +707,7 @@ *pi_agid = dvdbs.grantID; #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { ULONG id; DWORD tmp; @@ -821,7 +828,7 @@ memcpy( p_challenge, dvdbs.challengeKeyValue, DVD_CHALLENGE_SIZE ); #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[DVD_CHALLENGE_KEY_LENGTH]; @@ -953,7 +960,7 @@ *pi_asf = dvdbs.successFlag; #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[DVD_ASF_LENGTH]; @@ -1088,7 +1095,7 @@ memcpy( p_key, dvdbs.key1Value, DVD_KEY_SIZE ); #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[DVD_BUS_KEY_LENGTH]; @@ -1205,7 +1212,7 @@ i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd ); #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; @@ -1333,7 +1340,7 @@ i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd ); #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[DVD_CHALLENGE_KEY_LENGTH]; @@ -1470,7 +1477,7 @@ i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd ); #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[DVD_BUS_KEY_LENGTH]; @@ -1616,7 +1623,7 @@ *p_scheme = dvdbs.rpcScheme; #elif defined( WIN32 ) - if( WIN2K ) /* NT/Win2000/Whistler */ + if( WIN2K ) /* NT/2k/XP */ { DWORD tmp; u8 buffer[ DVD_REGION_LENGTH ]; @@ -1628,19 +1635,19 @@ region, DVD_REGION_LENGTH, &tmp, NULL ) ? 0 : -1; /* Someone who has the headers should correct all this. */ - /* Use the IOCTL_SCSI_PASS_THROUGH_DIRECT so we get the real - * values of theses entities? */ + /* Use the IOCTL_SCSI_PASS_THROUGH_DIRECT so we get the real + * values of theses entities? */ if(region->SystemRegion != 0) { - *p_type = region->ResetCount > 1 ? 1 : 3 - region->ResetCount; - *p_mask = 0xff ^ (1 << (region->SystemRegion - 1)); - *p_scheme = 1; - } - else - { - *p_type = 0; /* ?? */ - *p_mask = 0xff; - *p_scheme = 1; /* ?? */ - } + *p_type = region->ResetCount > 1 ? 1 : 3 - region->ResetCount; + *p_mask = 0xff ^ (1 << (region->SystemRegion - 1)); + *p_scheme = 1; + } + else + { + *p_type = 0; /* ?? */ + *p_mask = 0xff; + *p_scheme = 1; /* ?? */ + } } else { @@ -1905,10 +1912,10 @@ #if defined( __QNXNTO__ ) /***************************************************************************** - * QNXInitCPT: initialize a ssc structure for the win32 aspi layer + * QNXInitCPT: initialize a CPT structure for QNX Neutrino ***************************************************************************** - * This function initializes a ssc raw device command structure for future - * use, either a read command or a write command. + * This function initializes a cpt command structure for future use, + * either a read command or a write command. *****************************************************************************/ static void QNXInitCPT( CAM_PASS_THRU * p_cpt, int i_type ) { @@ -1930,7 +1937,7 @@ p_cpt->cam_cdb[ 9 ] = p_cpt->cam_dxfer_len & 0xff; p_cpt->cam_cdb_len = 12; - p_cpt->cam_timeout = CAM_TIME_DEFAULT; + p_cpt->cam_timeout = CAM_TIME_DEFAULT; } #endif
--- a/libmpdvdkit2/ioctl.h Sun Dec 29 15:10:02 2002 +0000 +++ b/libmpdvdkit2/ioctl.h Sun Dec 29 15:32:56 2002 +0000 @@ -188,7 +188,7 @@ #define IOCTL_DVD_END_SESSION CTL_CODE(FILE_DEVICE_DVD, 0x0403, METHOD_BUFFERED, FILE_READ_ACCESS) #define IOCTL_DVD_GET_REGION CTL_CODE(FILE_DEVICE_DVD, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS) #define IOCTL_DVD_SEND_KEY2 CTL_CODE(FILE_DEVICE_DVD, 0x0406, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) - +#define IOCTL_DVD_READ_STRUCTURE CTL_CODE(FILE_DEVICE_DVD, 0x0450, METHOD_BUFFERED, FILE_READ_ACCESS) #define IOCTL_SCSI_PASS_THROUGH_DIRECT CTL_CODE(FILE_DEVICE_CONTROLLER, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) #define DVD_CHALLENGE_KEY_LENGTH (12 + sizeof(DVD_COPY_PROTECT_KEY)) @@ -198,11 +198,41 @@ #define DVD_ASF_LENGTH (sizeof(DVD_ASF) + sizeof(DVD_COPY_PROTECT_KEY)) #define DVD_REGION_LENGTH (sizeof(DVD_REGION)) +#define DVD_COPYRIGHT_MASK 0x00000040 +#define DVD_NOT_COPYRIGHTED 0x00000000 +#define DVD_COPYRIGHTED 0x00000040 + +#define DVD_SECTOR_PROTECT_MASK 0x00000020 +#define DVD_SECTOR_NOT_PROTECTED 0x00000000 +#define DVD_SECTOR_PROTECTED 0x00000020 + #define SCSI_IOCTL_DATA_OUT 0 #define SCSI_IOCTL_DATA_IN 1 typedef ULONG DVD_SESSION_ID, *PDVD_SESSION_ID; +typedef enum DVD_STRUCTURE_FORMAT { + DvdPhysicalDescriptor, + DvdCopyrightDescriptor, + DvdDiskKeyDescriptor, + DvdBCADescriptor, + DvdManufacturerDescriptor, + DvdMaxDescriptor +} DVD_STRUCTURE_FORMAT, *PDVD_STRUCTURE_FORMAT; + +typedef struct DVD_READ_STRUCTURE { + LARGE_INTEGER BlockByteOffset; + DVD_STRUCTURE_FORMAT Format; + DVD_SESSION_ID SessionId; + UCHAR LayerNumber; +} DVD_READ_STRUCTURE, *PDVD_READ_STRUCTURE; + +typedef struct _DVD_COPYRIGHT_DESCRIPTOR { + UCHAR CopyrightProtectionType; + UCHAR RegionManagementInformation; + USHORT Reserved; +} DVD_COPYRIGHT_DESCRIPTOR, *PDVD_COPYRIGHT_DESCRIPTOR; + typedef enum { DvdChallengeKey = 0x01, @@ -272,8 +302,10 @@ #define WIN2K ( 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 @@ -309,6 +341,19 @@ 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;
--- a/libmpdvdkit2/libdvdcss.c Sun Dec 29 15:10:02 2002 +0000 +++ b/libmpdvdkit2/libdvdcss.c Sun Dec 29 15:32:56 2002 +0000 @@ -273,8 +273,8 @@ char sector[DVDCSS_BLOCK_SIZE]; // 32768+40 -> disc title (32 uppercase chars) // 32768+813 -> disc manufacturing date + serial no (16 digit number) - _dvdcss_seek( dvdcss, 32768/DVDCSS_BLOCK_SIZE); - if(_dvdcss_read( dvdcss, sector, 1) == 1){ + dvdcss->pf_seek( dvdcss, 32768/DVDCSS_BLOCK_SIZE); + if(dvdcss->pf_read( dvdcss, sector, 1) == 1){ // check disc title first: char* title_name=§or[40]; int i=31; @@ -363,7 +363,7 @@ } } - return _dvdcss_seek( dvdcss, i_blocks ); + return dvdcss->pf_seek( dvdcss, i_blocks ); } /** @@ -395,7 +395,7 @@ { int i_ret, i_index; - i_ret = _dvdcss_read( dvdcss, p_buffer, i_blocks ); + i_ret = dvdcss->pf_read( dvdcss, p_buffer, i_blocks ); if( i_ret <= 0 || !dvdcss->b_scrambled @@ -471,7 +471,7 @@ void *iov_base; size_t iov_len; - i_ret = _dvdcss_readv( dvdcss, _p_iovec, i_blocks ); + i_ret = dvdcss->pf_readv( dvdcss, _p_iovec, i_blocks ); if( i_ret <= 0 || !dvdcss->b_scrambled
--- a/libmpdvdkit2/libdvdcss.h Sun Dec 29 15:10:02 2002 +0000 +++ b/libmpdvdkit2/libdvdcss.h Sun Dec 29 15:32:56 2002 +0000 @@ -22,6 +22,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/ +struct iovec; + /***************************************************************************** * The libdvdcss structure *****************************************************************************/ @@ -30,8 +32,14 @@ /* File descriptor */ char * psz_device; int i_fd; + int i_read_fd; int i_seekpos; + /* File handling */ + int ( * pf_seek ) ( dvdcss_t, int ); + int ( * pf_read ) ( dvdcss_t, void *, int ); + int ( * pf_readv ) ( dvdcss_t, struct iovec *, int ); + /* Decryption stuff */ int i_method; css_t css; @@ -46,13 +54,13 @@ int b_debug; #ifdef WIN32 + int b_file; char * p_readv_buffer; int i_readv_buf_size; #endif #ifndef WIN32 int i_raw_fd; - int i_read_fd; #endif }; @@ -66,9 +74,6 @@ /***************************************************************************** * Functions used across the library *****************************************************************************/ -int _dvdcss_seek ( dvdcss_t, int ); -int _dvdcss_read ( dvdcss_t, void *, int ); - void _dvdcss_error ( dvdcss_t, char * ); void _dvdcss_debug ( dvdcss_t, char * );