[libdvdcss-devel] [PATCH 26/47] device: Refactor a number of block size calculations

Diego Biurrun diego at biurrun.de
Wed Oct 29 21:33:26 CET 2014


---
 src/device.c | 46 ++++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/src/device.c b/src/device.c
index 0c77b63..81152cb 100644
--- a/src/device.c
+++ b/src/device.c
@@ -539,7 +539,7 @@ static int os2_open ( dvdcss_t dvdcss, const char *psz_device )
  *****************************************************************************/
 static int libc_seek( dvdcss_t dvdcss, int i_blocks )
 {
-    off_t   i_seek;
+    off_t i_ret, i_seek = i_blocks * DVDCSS_BLOCK_SIZE;
 
     if( dvdcss->i_pos == i_blocks )
     {
@@ -547,17 +547,16 @@ static int libc_seek( dvdcss_t dvdcss, int i_blocks )
         return i_blocks;
     }
 
-    i_seek = i_blocks * DVDCSS_BLOCK_SIZE;
-    i_seek = lseek( dvdcss->i_read_fd, i_seek, SEEK_SET );
+    i_ret = lseek( dvdcss->i_read_fd, i_seek, SEEK_SET );
 
-    if( i_seek < 0 )
+    if( i_ret < 0 )
     {
         print_error( dvdcss, "seek error" );
         dvdcss->i_pos = -1;
-        return i_seek;
+        return i_ret;
     }
 
-    dvdcss->i_pos = i_seek / DVDCSS_BLOCK_SIZE;
+    dvdcss->i_pos = i_seek;
 
     return dvdcss->i_pos;
 }
@@ -596,7 +595,7 @@ static int win2k_seek( dvdcss_t dvdcss, int i_blocks )
  *****************************************************************************/
 static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
 {
-    off_t i_size, i_ret;
+    off_t i_size, i_ret, i_ret_blocks;
 
     i_size = i_blocks * DVDCSS_BLOCK_SIZE;
     i_ret = read( dvdcss->i_read_fd, p_buffer, i_size );
@@ -608,24 +607,26 @@ static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
         return i_ret;
     }
 
+    i_ret_blocks = i_ret / DVDCSS_BLOCK_SIZE;
+
     /* Handle partial reads */
     if( i_ret != i_size )
     {
         int i_seek;
 
         dvdcss->i_pos = -1;
-        i_seek = libc_seek( dvdcss, i_ret / DVDCSS_BLOCK_SIZE );
+        i_seek = libc_seek( dvdcss, i_ret_blocks );
         if( i_seek < 0 )
         {
             return i_seek;
         }
 
         /* We have to return now so that i_pos isn't clobbered */
-        return i_ret / DVDCSS_BLOCK_SIZE;
+        return i_ret_blocks;
     }
 
-    dvdcss->i_pos += i_ret / DVDCSS_BLOCK_SIZE;
-    return i_ret / DVDCSS_BLOCK_SIZE;
+    dvdcss->i_pos += i_ret_blocks;
+    return i_ret_blocks;
 }
 
 #if defined( WIN32 )
@@ -641,8 +642,9 @@ static int win2k_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
         return -1;
     }
 
-    dvdcss->i_pos += i_bytes / DVDCSS_BLOCK_SIZE;
-    return i_bytes / DVDCSS_BLOCK_SIZE;
+    i_bytes /= DVDCSS_BLOCK_SIZE;
+    dvdcss->i_pos += i_bytes;
+    return i_bytes;
 }
 #endif /* defined( WIN32 ) */
 
@@ -681,6 +683,7 @@ static int libc_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
         }
 
         i_total += i_bytes;
+        i_total /= DVDCSS_BLOCK_SIZE;
 
         if( i_bytes != i_len )
         {
@@ -689,19 +692,19 @@ static int libc_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
             int i_seek;
 
             dvdcss->i_pos = -1;
-            i_seek = libc_seek( dvdcss, i_total / DVDCSS_BLOCK_SIZE );
+            i_seek = libc_seek( dvdcss, i_total );
             if( i_seek < 0 )
             {
                 return i_seek;
             }
 
             /* We have to return now so that i_pos isn't clobbered */
-            return i_total / DVDCSS_BLOCK_SIZE;
+            return i_total;
         }
     }
 
-    dvdcss->i_pos += i_total / DVDCSS_BLOCK_SIZE;
-    return i_total / DVDCSS_BLOCK_SIZE;
+    dvdcss->i_pos += i_total;
+    return i_total;
 #else
     int i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks );
 
@@ -711,8 +714,9 @@ static int libc_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
         return i_read;
     }
 
-    dvdcss->i_pos += i_read / DVDCSS_BLOCK_SIZE;
-    return i_read / DVDCSS_BLOCK_SIZE;
+    i_read /= DVDCSS_BLOCK_SIZE;
+    dvdcss->i_pos += i_read;
+    return i_read;
 #endif
 }
 
@@ -752,10 +756,8 @@ static int win2k_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
 
     if( i_blocks_total <= 0 ) return 0;
 
-    i_blocks_total /= DVDCSS_BLOCK_SIZE;
-
     if( !ReadFile( (HANDLE)dvdcss->i_fd, dvdcss->p_readv_buffer,
-                   i_blocks_total * DVDCSS_BLOCK_SIZE, &i_bytes, NULL ) )
+                   i_blocks_total, &i_bytes, NULL ) )
     {
         /* The read failed... too bad.
          * As in the POSIX spec the file position is left
-- 
1.9.1



More information about the libdvdcss-devel mailing list