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.

124 lines
3.2KB

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