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.

136 lines
3.7KB

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