FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/file.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 3 37 8.1%
Functions: 1 2 50.0%
Branches: 1 12 8.3%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "error.h"
21 #include "file.h"
22 #include "file_open.h"
23 #include "internal.h"
24 #include "log.h"
25 #include "mem.h"
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #if HAVE_IO_H
32 #include <io.h>
33 #endif
34 #if HAVE_MMAP
35 #include <sys/mman.h>
36 #elif HAVE_MAPVIEWOFFILE
37 #include <windows.h>
38 #endif
39
40 typedef struct FileLogContext {
41 const AVClass *class;
42 int log_offset;
43 void *log_ctx;
44 } FileLogContext;
45
46 static const AVClass file_log_ctx_class = {
47 .class_name = "FILE",
48 .item_name = av_default_item_name,
49 .option = NULL,
50 .version = LIBAVUTIL_VERSION_INT,
51 .log_level_offset_offset = offsetof(FileLogContext, log_offset),
52 .parent_log_context_offset = offsetof(FileLogContext, log_ctx),
53 };
54
55 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
56 int log_offset, void *log_ctx)
57 {
58 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
59 int err, fd = avpriv_open(filename, O_RDONLY);
60 struct stat st;
61 av_unused void *ptr;
62 off_t off_size;
63 char errbuf[128];
64 *bufptr = NULL;
65 *size = 0;
66
67 if (fd < 0) {
68 err = AVERROR(errno);
69 av_strerror(err, errbuf, sizeof(errbuf));
70 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
71 return err;
72 }
73
74 if (fstat(fd, &st) < 0) {
75 err = AVERROR(errno);
76 av_strerror(err, errbuf, sizeof(errbuf));
77 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
78 close(fd);
79 return err;
80 }
81
82 off_size = st.st_size;
83 if (off_size > SIZE_MAX) {
84 av_log(&file_log_ctx, AV_LOG_ERROR,
85 "File size for file '%s' is too big\n", filename);
86 close(fd);
87 return AVERROR(EINVAL);
88 }
89 *size = off_size;
90
91 if (!*size) {
92 *bufptr = NULL;
93 goto out;
94 }
95
96 #if HAVE_MMAP
97 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
98 if (ptr == MAP_FAILED) {
99 err = AVERROR(errno);
100 av_strerror(err, errbuf, sizeof(errbuf));
101 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
102 close(fd);
103 *size = 0;
104 return err;
105 }
106 *bufptr = ptr;
107 #elif HAVE_MAPVIEWOFFILE
108 {
109 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
110
111 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
112 if (!mh) {
113 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
114 close(fd);
115 *size = 0;
116 return -1;
117 }
118
119 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
120 CloseHandle(mh);
121 if (!ptr) {
122 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
123 close(fd);
124 *size = 0;
125 return -1;
126 }
127
128 *bufptr = ptr;
129 }
130 #else
131 *bufptr = av_malloc(*size);
132 if (!*bufptr) {
133 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
134 close(fd);
135 *size = 0;
136 return AVERROR(ENOMEM);
137 }
138 read(fd, *bufptr, *size);
139 #endif
140
141 out:
142 close(fd);
143 return 0;
144 }
145
146 1 void av_file_unmap(uint8_t *bufptr, size_t size)
147 {
148
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (!size || !bufptr)
149 1 return;
150 #if HAVE_MMAP
151 munmap(bufptr, size);
152 #elif HAVE_MAPVIEWOFFILE
153 UnmapViewOfFile(bufptr);
154 #else
155 av_free(bufptr);
156 #endif
157 }
158