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.

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