LCOV - code coverage report
Current view: top level - media/libav/libavutil - file.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 30 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 2 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * This file is part of Libav.
       3             :  *
       4             :  * Libav is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * Libav is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with Libav; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "config.h"
      20             : #include "file.h"
      21             : #include "internal.h"
      22             : #include "log.h"
      23             : #include "mem.h"
      24             : #include <fcntl.h>
      25             : #include <sys/stat.h>
      26             : #if HAVE_UNISTD_H
      27             : #include <unistd.h>
      28             : #endif
      29             : #if HAVE_IO_H
      30             : #include <io.h>
      31             : #endif
      32             : #if HAVE_MMAP
      33             : #include <sys/mman.h>
      34             : #elif HAVE_MAPVIEWOFFILE
      35             : #include <windows.h>
      36             : #endif
      37             : 
      38             : typedef struct {
      39             :     const AVClass *class;
      40             :     int   log_offset;
      41             :     void *log_ctx;
      42             : } FileLogContext;
      43             : 
      44             : static const AVClass file_log_ctx_class = {
      45             :     "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
      46             :     offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
      47             : };
      48             : 
      49           0 : int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
      50             :                 int log_offset, void *log_ctx)
      51             : {
      52           0 :     FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
      53           0 :     int err, fd = avpriv_open(filename, O_RDONLY);
      54             :     struct stat st;
      55             :     av_unused void *ptr;
      56             :     off_t off_size;
      57             :     char errbuf[128];
      58           0 :     *bufptr = NULL;
      59             : 
      60           0 :     if (fd < 0) {
      61           0 :         err = AVERROR(errno);
      62           0 :         av_strerror(err, errbuf, sizeof(errbuf));
      63           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
      64           0 :         return err;
      65             :     }
      66             : 
      67           0 :     if (fstat(fd, &st) < 0) {
      68           0 :         err = AVERROR(errno);
      69           0 :         av_strerror(err, errbuf, sizeof(errbuf));
      70           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
      71           0 :         close(fd);
      72           0 :         return err;
      73             :     }
      74             : 
      75           0 :     off_size = st.st_size;
      76             :     if (off_size > SIZE_MAX) {
      77             :         av_log(&file_log_ctx, AV_LOG_ERROR,
      78             :                "File size for file '%s' is too big\n", filename);
      79             :         close(fd);
      80             :         return AVERROR(EINVAL);
      81             :     }
      82           0 :     *size = off_size;
      83             : 
      84             : #if HAVE_MMAP
      85           0 :     ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
      86           0 :     if (ptr == MAP_FAILED) {
      87           0 :         err = AVERROR(errno);
      88           0 :         av_strerror(err, errbuf, sizeof(errbuf));
      89           0 :         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
      90           0 :         close(fd);
      91           0 :         return err;
      92             :     }
      93           0 :     *bufptr = ptr;
      94             : #elif HAVE_MAPVIEWOFFILE
      95             :     {
      96             :         HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
      97             : 
      98             :         mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
      99             :         if (!mh) {
     100             :             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
     101             :             close(fd);
     102             :             return -1;
     103             :         }
     104             : 
     105             :         ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
     106             :         CloseHandle(mh);
     107             :         if (!ptr) {
     108             :             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
     109             :             close(fd);
     110             :             return -1;
     111             :         }
     112             : 
     113             :         *bufptr = ptr;
     114             :     }
     115             : #else
     116             :     *bufptr = av_malloc(*size);
     117             :     if (!*bufptr) {
     118             :         av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
     119             :         close(fd);
     120             :         return AVERROR(ENOMEM);
     121             :     }
     122             :     read(fd, *bufptr, *size);
     123             : #endif
     124             : 
     125           0 :     close(fd);
     126           0 :     return 0;
     127             : }
     128             : 
     129           0 : void av_file_unmap(uint8_t *bufptr, size_t size)
     130             : {
     131             : #if HAVE_MMAP
     132           0 :     munmap(bufptr, size);
     133             : #elif HAVE_MAPVIEWOFFILE
     134             :     UnmapViewOfFile(bufptr);
     135             : #else
     136             :     av_free(bufptr);
     137             : #endif
     138           0 : }

Generated by: LCOV version 1.13