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.

164 lines
4.1KB

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