file.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "file.h"
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. #if HAVE_MMAP
  23. #include <sys/mman.h>
  24. #elif HAVE_MAPVIEWOFFILE
  25. #include <io.h>
  26. #include <windows.h>
  27. #endif
  28. typedef struct {
  29. const AVClass *class;
  30. int log_offset;
  31. void *log_ctx;
  32. } FileLogContext;
  33. static const AVClass file_log_ctx_class = {
  34. "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
  35. offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
  36. };
  37. int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
  38. int log_offset, void *log_ctx)
  39. {
  40. FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
  41. int err, fd = open(filename, O_RDONLY);
  42. struct stat st;
  43. av_unused void *ptr;
  44. off_t off_size;
  45. char errbuf[128];
  46. size_t max_size = HAVE_MMAP ? SIZE_MAX : FF_INTERNAL_MEM_TYPE_MAX_VALUE;
  47. *bufptr = NULL;
  48. if (fd < 0) {
  49. err = AVERROR(errno);
  50. av_strerror(err, errbuf, sizeof(errbuf));
  51. av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
  52. return err;
  53. }
  54. if (fstat(fd, &st) < 0) {
  55. err = AVERROR(errno);
  56. av_strerror(err, errbuf, sizeof(errbuf));
  57. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
  58. close(fd);
  59. return err;
  60. }
  61. off_size = st.st_size;
  62. if (off_size > max_size) {
  63. av_log(&file_log_ctx, AV_LOG_ERROR,
  64. "File size for file '%s' is too big\n", filename);
  65. close(fd);
  66. return AVERROR(EINVAL);
  67. }
  68. *size = off_size;
  69. #if HAVE_MMAP
  70. ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  71. if (ptr == MAP_FAILED) {
  72. err = AVERROR(errno);
  73. av_strerror(err, errbuf, sizeof(errbuf));
  74. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
  75. close(fd);
  76. return err;
  77. }
  78. *bufptr = ptr;
  79. #elif HAVE_MAPVIEWOFFILE
  80. {
  81. HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
  82. mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
  83. if (!mh) {
  84. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
  85. close(fd);
  86. return -1;
  87. }
  88. ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
  89. CloseHandle(mh);
  90. if (!ptr) {
  91. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
  92. close(fd);
  93. return -1;
  94. }
  95. *bufptr = ptr;
  96. }
  97. #else
  98. *bufptr = av_malloc(*size);
  99. if (!*bufptr) {
  100. av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
  101. close(fd);
  102. return AVERROR(ENOMEM);
  103. }
  104. read(fd, *bufptr, *size);
  105. #endif
  106. close(fd);
  107. return 0;
  108. }
  109. void av_file_unmap(uint8_t *bufptr, size_t size)
  110. {
  111. #if HAVE_MMAP
  112. munmap(bufptr, size);
  113. #elif HAVE_MAPVIEWOFFILE
  114. UnmapViewOfFile(bufptr);
  115. #else
  116. av_free(bufptr);
  117. #endif
  118. }
  119. #ifdef TEST
  120. #undef printf
  121. int main(void)
  122. {
  123. uint8_t *buf;
  124. size_t size;
  125. if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
  126. return 1;
  127. buf[0] = 's';
  128. printf("%s", buf);
  129. av_file_unmap(buf, size);
  130. return 0;
  131. }
  132. #endif