FFmpeg
Loading...
Searching...
No Matches
file.c
Go to the documentation of this file.
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
40typedef struct FileLogContext {
41 const AVClass *class;
43 void *log_ctx;
45
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
55int 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 *bufptr = NULL;
64 *size = 0;
65
66 if (fd < 0) {
67 err = AVERROR(errno);
68 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, av_err2str(err));
69 return err;
70 }
71
72 if (fstat(fd, &st) < 0) {
73 err = AVERROR(errno);
74 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", av_err2str(err));
75 close(fd);
76 return err;
77 }
78
79 off_size = st.st_size;
80 if (off_size > SIZE_MAX) {
81 av_log(&file_log_ctx, AV_LOG_ERROR,
82 "File size for file '%s' is too big\n", filename);
83 close(fd);
84 return AVERROR(EINVAL);
85 }
86 *size = off_size;
87
88 if (!*size) {
89 *bufptr = NULL;
90 goto out;
91 }
92
93#if HAVE_MMAP
94 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
95 if (ptr == MAP_FAILED) {
96 err = AVERROR(errno);
97 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", av_err2str(err));
98 close(fd);
99 *size = 0;
100 return err;
101 }
102 *bufptr = ptr;
103#elif HAVE_MAPVIEWOFFILE
104 {
105 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
106
107 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
108 if (!mh) {
109 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
110 close(fd);
111 *size = 0;
112 return -1;
113 }
114
115 ptr = MapViewOfFile(mh, FILE_MAP_COPY, 0, 0, *size);
116 CloseHandle(mh);
117 if (!ptr) {
118 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
119 close(fd);
120 *size = 0;
121 return -1;
122 }
123
124 *bufptr = ptr;
125 }
126#else
127 *bufptr = av_malloc(*size);
128 if (!*bufptr) {
129 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
130 close(fd);
131 *size = 0;
132 return AVERROR(ENOMEM);
133 }
134 read(fd, *bufptr, *size);
135#endif
136
137out:
138 close(fd);
139 return 0;
140}
141
142void av_file_unmap(uint8_t *bufptr, size_t size)
143{
144 if (!size || !bufptr)
145 return;
146#if HAVE_MMAP
147 munmap(bufptr, size);
148#elif HAVE_MAPVIEWOFFILE
149 UnmapViewOfFile(bufptr);
150#else
151 av_free(bufptr);
152#endif
153}
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define NULL
Definition coverity.c:32
error code definitions
Misc file utilities.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_unused
Definition attributes.h:164
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:142
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition file.c:55
static const AVClass file_log_ctx_class
Definition file.c:46
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition file_open.c:67
common internal API header
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
Describe the class of an AVClass context structure.
Definition log.h:76
void * log_ctx
Definition file.c:43
int log_offset
Definition file.c:42
#define av_free(p)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
int size
#define mh