2 * This file is part of FFmpeg.
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.
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.
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
22 #include "file_open.h"
36 #elif HAVE_MAPVIEWOFFILE
40 typedef struct FileLogContext
{
46 static const AVClass file_log_ctx_class
= {
48 .item_name
= av_default_item_name
,
50 .version
= LIBAVUTIL_VERSION_INT
,
51 .log_level_offset_offset
= offsetof(FileLogContext
, log_offset
),
52 .parent_log_context_offset
= offsetof(FileLogContext
, log_ctx
),
55 int av_file_map(const char *filename
, uint8_t **bufptr
, size_t *size
,
56 int log_offset
, void *log_ctx
)
58 FileLogContext file_log_ctx
= { &file_log_ctx_class
, log_offset
, log_ctx
};
59 int err
, fd
= avpriv_open(filename
, O_RDONLY
);
68 av_log(&file_log_ctx
, AV_LOG_ERROR
, "Cannot read file '%s': %s\n", filename
, av_err2str(err
));
72 if (fstat(fd
, &st
) < 0) {
74 av_log(&file_log_ctx
, AV_LOG_ERROR
, "Error occurred in fstat(): %s\n", av_err2str(err
));
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
);
84 return AVERROR(EINVAL
);
94 ptr
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
95 if (ptr
== MAP_FAILED
) {
97 av_log(&file_log_ctx
, AV_LOG_ERROR
, "Error occurred in mmap(): %s\n", av_err2str(err
));
103 #elif HAVE_MAPVIEWOFFILE
105 HANDLE mh
, fh
= (HANDLE
)_get_osfhandle(fd
);
107 mh
= CreateFileMapping(fh
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
109 av_log(&file_log_ctx
, AV_LOG_ERROR
, "Error occurred in CreateFileMapping()\n");
115 ptr
= MapViewOfFile(mh
, FILE_MAP_COPY
, 0, 0, *size
);
118 av_log(&file_log_ctx
, AV_LOG_ERROR
, "Error occurred in MapViewOfFile()\n");
127 *bufptr
= av_malloc(*size
);
129 av_log(&file_log_ctx
, AV_LOG_ERROR
, "Memory allocation error occurred\n");
132 return AVERROR(ENOMEM
);
134 read(fd
, *bufptr
, *size
);
142 void av_file_unmap(uint8_t *bufptr
, size_t size
)
144 if (!size
|| !bufptr
)
147 munmap(bufptr
, size
);
148 #elif HAVE_MAPVIEWOFFILE
149 UnmapViewOfFile(bufptr
);