You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.3KB

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