file.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. *bufptr = NULL;
  47. if (fd < 0) {
  48. err = AVERROR(errno);
  49. av_strerror(err, errbuf, sizeof(errbuf));
  50. av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
  51. return err;
  52. }
  53. if (fstat(fd, &st) < 0) {
  54. err = AVERROR(errno);
  55. av_strerror(err, errbuf, sizeof(errbuf));
  56. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
  57. close(fd);
  58. return err;
  59. }
  60. off_size = st.st_size;
  61. if (off_size > SIZE_MAX) {
  62. av_log(&file_log_ctx, AV_LOG_ERROR,
  63. "File size for file '%s' is too big\n", filename);
  64. close(fd);
  65. return AVERROR(EINVAL);
  66. }
  67. *size = off_size;
  68. #if HAVE_MMAP
  69. ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  70. if (ptr == MAP_FAILED) {
  71. err = AVERROR(errno);
  72. av_strerror(err, errbuf, sizeof(errbuf));
  73. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
  74. close(fd);
  75. return err;
  76. }
  77. *bufptr = ptr;
  78. #elif HAVE_MAPVIEWOFFILE
  79. {
  80. HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
  81. mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
  82. if (!mh) {
  83. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
  84. close(fd);
  85. return -1;
  86. }
  87. ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
  88. CloseHandle(mh);
  89. if (!ptr) {
  90. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
  91. close(fd);
  92. return -1;
  93. }
  94. *bufptr = ptr;
  95. }
  96. #else
  97. *bufptr = av_malloc(*size);
  98. if (!*bufptr) {
  99. av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
  100. close(fd);
  101. return AVERROR(ENOMEM);
  102. }
  103. read(fd, *bufptr, *size);
  104. #endif
  105. close(fd);
  106. return 0;
  107. }
  108. void av_file_unmap(uint8_t *bufptr, size_t size)
  109. {
  110. #if HAVE_MMAP
  111. munmap(bufptr, size);
  112. #elif HAVE_MAPVIEWOFFILE
  113. UnmapViewOfFile(bufptr);
  114. #else
  115. av_free(bufptr);
  116. #endif
  117. }
  118. #ifdef TEST
  119. #undef printf
  120. int main(void)
  121. {
  122. uint8_t *buf;
  123. size_t size;
  124. if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
  125. return 1;
  126. buf[0] = 's';
  127. printf("%s", buf);
  128. av_file_unmap(buf, size);
  129. return 0;
  130. }
  131. #endif