file.c 3.9 KB

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