shithub: libdvdcss

Download patch

ref: 06626375c5f96a98eabddcd928ba1c269665498f
parent: 2b99a49bad03ee880d1a8a2ff65d9080be934f45
author: Sam Hocevar <sam@videolan.org>
date: Fri Oct 18 14:48:59 EDT 2002

* ./src/device.c: split the open, read and seek functions so that we can
    use function pointers instead of doing the if(WIN2K) test.
  * ./src/device.c: if the target is not a drive name such as F:, we open
    it with the standard libc functions, even under Win32.


--- a/src/css.c
+++ b/src/css.c
@@ -2,7 +2,7 @@
  * css.c: Functions for DVD authentication and descrambling
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: css.c,v 1.17 2002/10/10 21:40:41 massiot Exp $
+ * $Id: css.c,v 1.18 2002/10/18 18:48:58 sam Exp $
  *
  * Author: St�phane Borel <stef@via.ecp.fr>
  *         H�kan Hjort <d95hjort@dtek.chalmers.se>
@@ -255,7 +255,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 )
@@ -519,9 +519,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 */
@@ -1352,7 +1352,7 @@
 
     do
     {
-        i_ret = _dvdcss_seek( dvdcss, i_pos );
+        i_ret = dvdcss->pf_seek( dvdcss, i_pos );
 
         if( i_ret != i_pos )
         {
--- a/src/device.c
+++ b/src/device.c
@@ -2,7 +2,7 @@
  * device.h: DVD device access
  *****************************************************************************
  * Copyright (C) 1998-2002 VideoLAN
- * $Id: device.c,v 1.4 2002/10/10 12:44:28 gbazin Exp $
+ * $Id: device.c,v 1.5 2002/10/18 18:48:59 sam Exp $
  *
  * Authors: St�phane Borel <stef@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -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,70 +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] );
+    /* If device is not "X:", we are actually opening a file. */
+    dvdcss->b_file = !psz_device[0] || psz_device[1] != ':' || psz_device[3];
 
-        /* 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 );
-
-        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_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 );
 
@@ -245,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 */
@@ -262,6 +217,7 @@
         dvdcss->i_readv_buf_size = 0;
     }
 
+    return 0;
 #else
     close( dvdcss->i_fd );
 
@@ -271,198 +227,68 @@
         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;
+/* Following functions are local */
 
-        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 );
-    }
-
+/*****************************************************************************
+ * 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;
+    char psz_dvd[7];
+    _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_device[0] );
 
-    for( i_index = i_num_buffers; i_index; i_index-- )
-    {
-        i_blocks_total += p_iovec[i_index-1].iov_len; 
-    }
+    /* 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 );
 
-    if( i_blocks_total <= 0 ) return 0;
+    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 );
 
-    i_blocks_total /= DVDCSS_BLOCK_SIZE;
-
-    if( WIN2K )
+    if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
     {
-        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;
+        _dvdcss_error( dvdcss, "failed opening device" );
+        return -1;
     }
-    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;
+    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;
@@ -470,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 )
     {
@@ -483,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;
     }
@@ -492,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;
     }
@@ -507,6 +335,7 @@
     i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
     if( i_hostadapters == 0 )
     {
+        _dvdcss_error( dvdcss, "no ASPI adapters ready" );
         FreeLibrary( hASPI );
         return -1;
     }
@@ -514,6 +343,7 @@
     fd = malloc( sizeof( struct w32_aspidev ) );
     if( fd == NULL )
     {
+        _dvdcss_error( dvdcss, "not enough memory" );
         FreeLibrary( hASPI );
         return -1;
     }
@@ -543,7 +373,8 @@
                 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
             {
                 fd->i_sid = MAKEWORD( i, j );
-                return (int) fd;
+                dvdcss->i_fd = (int) fd;
+                return 0;
             }
         }
     }
@@ -550,39 +381,67 @@
 
     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;
@@ -592,14 +451,180 @@
 
     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;
     struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
@@ -640,9 +665,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;
@@ -676,6 +701,5 @@
 
     return i_blocks;
 }
-
 #endif
 
--- a/src/device.h
+++ b/src/device.h
@@ -2,7 +2,7 @@
  * device.h: DVD device access
  *****************************************************************************
  * Copyright (C) 1998-2002 VideoLAN
- * $Id: device.h,v 1.3 2002/08/10 17:42:09 sam Exp $
+ * $Id: device.h,v 1.4 2002/10/18 18:48:59 sam Exp $
  *
  * Authors: St�phane Borel <stef@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -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/src/libdvdcss.c
+++ b/src/libdvdcss.c
@@ -5,7 +5,7 @@
  *          H�kan Hjort <d95hjort@dtek.chalmers.se>
  *
  * Copyright (C) 1998-2002 VideoLAN
- * $Id: libdvdcss.c,v 1.18 2002/08/10 20:21:54 sam Exp $
+ * $Id: libdvdcss.c,v 1.19 2002/10/18 18:48:59 sam Exp $
  *
  * 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
@@ -318,7 +318,7 @@
         }
     }
 
-    return _dvdcss_seek( dvdcss, i_blocks );
+    return dvdcss->pf_seek( dvdcss, i_blocks );
 }
 
 /**
@@ -350,7 +350,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
@@ -426,7 +426,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/src/libdvdcss.h
+++ b/src/libdvdcss.h
@@ -2,7 +2,7 @@
  * private.h: private DVD reading library data
  *****************************************************************************
  * Copyright (C) 1998-2001 VideoLAN
- * $Id: libdvdcss.h,v 1.4 2002/08/10 14:27:26 sam Exp $
+ * $Id: libdvdcss.h,v 1.5 2002/10/18 18:48:59 sam Exp $
  *
  * Authors: St�phane Borel <stef@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -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;
@@ -45,6 +53,7 @@
     int    b_debug;
 
 #ifdef WIN32
+    int    b_file;
     char * p_readv_buffer;
     int    i_readv_buf_size;
 #endif
@@ -51,7 +60,6 @@
 
 #ifndef WIN32
     int    i_raw_fd;
-    int    i_read_fd;
 #endif
 };
 
@@ -65,9 +73,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 * );