]> git.sesse.net Git - vlc/blobdiff - modules/access/file.c
- Parse seek offsets as unsigned (why they were signed in the first place??)
[vlc] / modules / access / file.c
index 9246bafe3889419211f4f98f77d446b1fba76f24..04d0dc513fa7e2c91fadc7319ac0a9ba06d7e8fe 100644 (file)
@@ -106,14 +106,17 @@ vlc_module_end();
  * Exported prototypes
  *****************************************************************************/
 static int  Seek( access_t *, int64_t );
-static int  Read( access_t *, uint8_t *, int );
+static ssize_t Read( access_t *, uint8_t *, size_t );
 static int  Control( access_t *, int, va_list );
+#ifdef HAVE_MMAP
 static block_t *mmapBlock( access_t * );
+#endif
 
 static int  open_file( access_t *, const char * );
 
 struct access_sys_t
 {
+    uint64_t     pagemask;
     unsigned int i_nb_reads;
     vlc_bool_t   b_kfir;
 
@@ -200,13 +203,15 @@ static int Open( vlc_object_t *p_this )
         p_sys->b_seekable = VLC_FALSE;
 
 # ifdef HAVE_MMAP
+    p_sys->pagemask = sysconf (_SC_PAGE_SIZE) - 1;
+
     /* Autodetect mmap() support */
     if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
     {
         void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
         if (addr != MAP_FAILED)
         {
-            /* Does the file system needs to support mmap? */
+            /* Does the file system support mmap? */
             munmap (addr, 1);
             p_access->pf_read = NULL;
             p_access->pf_block = mmapBlock;
@@ -249,10 +254,10 @@ static void Close (vlc_object_t * p_this)
 /*****************************************************************************
  * Read: standard read on a file descriptor.
  *****************************************************************************/
-static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
+static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
 {
     access_sys_t *p_sys = p_access->p_sys;
-    int i_ret;
+    ssize_t i_ret;
     int fd = p_sys->fd;
 
 #if !defined(WIN32) && !defined(UNDER_CE)
@@ -338,7 +343,7 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
 }
 
 #ifdef HAVE_MMAP
-# define MMAP_SIZE (1 << 18)
+# define MMAP_SIZE (1 << 20)
 
 struct block_sys_t
 {
@@ -362,15 +367,14 @@ static block_t *mmapBlock (access_t *p_access)
     access_sys_t *p_sys = p_access->p_sys;
 
     const int flags = MAP_SHARED;
-    const size_t pagesize = sysconf (_SC_PAGE_SIZE);
-    off_t offset = p_access->info.i_pos & ~(pagesize - 1);
-    size_t align = p_access->info.i_pos & (pagesize - 1);
-    size_t length = (MMAP_SIZE > pagesize) ? MMAP_SIZE : pagesize;
+    off_t offset = p_access->info.i_pos & ~p_sys->pagemask;
+    size_t align = p_access->info.i_pos & p_sys->pagemask;
+    size_t length = (MMAP_SIZE > p_sys->pagemask) ? MMAP_SIZE : (p_sys->pagemask + 1);
     void *addr;
 
     if (p_access->info.i_pos >= p_access->info.i_size)
     {
-        /* End of file - check that file size hasn't change... */
+        /* End of file - check if file size changed... */
         struct stat st;
 
         if ((fstat (p_sys->fd, &st) == 0)
@@ -390,7 +394,7 @@ static block_t *mmapBlock (access_t *p_access)
     }
 
     if (offset + length > p_access->info.i_size)
-        /* Don't mmap paste end of file */
+        /* Don't mmap beyond end of file */
         length = p_access->info.i_size - offset;
 
     assert (offset <= p_access->info.i_pos);               /* and */
@@ -410,7 +414,7 @@ static block_t *mmapBlock (access_t *p_access)
 
     p_access->info.i_pos = offset + length;
 
-    msg_Dbg (p_access, "mapped %lu bytes at %p from offset %lu",
+    msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
              (unsigned long)length, addr, (unsigned long)offset);
     block_sys_t *block = malloc (sizeof (*block));
     if (block == NULL)
@@ -434,24 +438,23 @@ static block_t *mmapBlock (access_t *p_access)
  *****************************************************************************/
 static int Seek (access_t *p_access, int64_t i_pos)
 {
-    if (i_pos > p_access->info.i_size)
+    /* FIXME: i_size should really be unsigned */
+    if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
     {
-        msg_Err (p_access, "seeking too far");
+        /* This should only happen with corrupted files.
+         * But it also seems to happen with buggy demuxes (ASF) */
+        msg_Err (p_access, "seeking too far (0x"I64Fx" / 0x"I64Fx")",
+                 i_pos, p_access->info.i_size);
         i_pos = p_access->info.i_size;
     }
-    else if (i_pos < 0)
-    {
-        msg_Err (p_access, "seeking too early");
-        i_pos = 0;
-    }
 
     p_access->info.i_pos = i_pos;
     p_access->info.b_eof = VLC_FALSE;
 
 #ifdef HAVE_MMAP
     if (p_access->pf_block == NULL)
-        lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
 #endif
+        lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
     return VLC_SUCCESS;
 }