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.

134 lines
3.6KB

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