00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "file.h"
00020 #include <fcntl.h>
00021 #include <sys/stat.h>
00022 #include <unistd.h>
00023 #if HAVE_MMAP
00024 #include <sys/mman.h>
00025 #elif HAVE_MAPVIEWOFFILE
00026 #include <io.h>
00027 #include <windows.h>
00028 #endif
00029
00030 typedef struct {
00031 const AVClass *class;
00032 int log_offset;
00033 void *log_ctx;
00034 } FileLogContext;
00035
00036 static const AVClass file_log_ctx_class = {
00037 "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
00038 offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
00039 };
00040
00041 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
00042 int log_offset, void *log_ctx)
00043 {
00044 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
00045 int err, fd = open(filename, O_RDONLY);
00046 struct stat st;
00047 av_unused void *ptr;
00048 off_t off_size;
00049 char errbuf[128];
00050 size_t max_size = HAVE_MMAP ? SIZE_MAX : FF_INTERNAL_MEM_TYPE_MAX_VALUE;
00051 *bufptr = NULL;
00052
00053 if (fd < 0) {
00054 err = AVERROR(errno);
00055 av_strerror(err, errbuf, sizeof(errbuf));
00056 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
00057 return err;
00058 }
00059
00060 if (fstat(fd, &st) < 0) {
00061 err = AVERROR(errno);
00062 av_strerror(err, errbuf, sizeof(errbuf));
00063 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
00064 close(fd);
00065 return err;
00066 }
00067
00068 off_size = st.st_size;
00069 if (off_size > max_size) {
00070 av_log(&file_log_ctx, AV_LOG_ERROR,
00071 "File size for file '%s' is too big\n", filename);
00072 close(fd);
00073 return AVERROR(EINVAL);
00074 }
00075 *size = off_size;
00076
00077 #if HAVE_MMAP
00078 ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
00079 if (ptr == MAP_FAILED) {
00080 err = AVERROR(errno);
00081 av_strerror(err, errbuf, sizeof(errbuf));
00082 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
00083 close(fd);
00084 return err;
00085 }
00086 *bufptr = ptr;
00087 #elif HAVE_MAPVIEWOFFILE
00088 {
00089 HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
00090
00091 mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
00092 if (!mh) {
00093 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
00094 close(fd);
00095 return -1;
00096 }
00097
00098 ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
00099 CloseHandle(mh);
00100 if (!ptr) {
00101 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
00102 close(fd);
00103 return -1;
00104 }
00105
00106 *bufptr = ptr;
00107 }
00108 #else
00109 *bufptr = av_malloc(*size);
00110 if (!*bufptr) {
00111 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
00112 close(fd);
00113 return AVERROR(ENOMEM);
00114 }
00115 read(fd, *bufptr, *size);
00116 #endif
00117
00118 close(fd);
00119 return 0;
00120 }
00121
00122 void av_file_unmap(uint8_t *bufptr, size_t size)
00123 {
00124 #if HAVE_MMAP
00125 munmap(bufptr, size);
00126 #elif HAVE_MAPVIEWOFFILE
00127 UnmapViewOfFile(bufptr);
00128 #else
00129 av_free(bufptr);
00130 #endif
00131 }
00132
00133 #ifdef TEST
00134
00135 #undef printf
00136
00137 int main(void)
00138 {
00139 uint8_t *buf;
00140 size_t size;
00141 if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
00142 return 1;
00143
00144 buf[0] = 's';
00145 printf("%s", buf);
00146 av_file_unmap(buf, size);
00147 return 0;
00148 }
00149 #endif