file.c 3.7 KB

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