mkvreader.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. #include "mkvparser/mkvreader.h"
  9. #include <sys/types.h>
  10. #include <cassert>
  11. namespace mkvparser {
  12. MkvReader::MkvReader() : m_file(NULL), reader_owns_file_(true) {}
  13. MkvReader::MkvReader(FILE* fp) : m_file(fp), reader_owns_file_(false) {
  14. GetFileSize();
  15. }
  16. MkvReader::~MkvReader() {
  17. if (reader_owns_file_)
  18. Close();
  19. m_file = NULL;
  20. }
  21. int MkvReader::Open(const char* fileName) {
  22. if (fileName == NULL)
  23. return -1;
  24. if (m_file)
  25. return -1;
  26. #ifdef _MSC_VER
  27. const errno_t e = fopen_s(&m_file, fileName, "rb");
  28. if (e)
  29. return -1; // error
  30. #else
  31. m_file = fopen(fileName, "rb");
  32. if (m_file == NULL)
  33. return -1;
  34. #endif
  35. return !GetFileSize();
  36. }
  37. bool MkvReader::GetFileSize() {
  38. if (m_file == NULL)
  39. return false;
  40. #ifdef _MSC_VER
  41. int status = _fseeki64(m_file, 0L, SEEK_END);
  42. if (status)
  43. return false; // error
  44. m_length = _ftelli64(m_file);
  45. #else
  46. fseek(m_file, 0L, SEEK_END);
  47. m_length = ftell(m_file);
  48. #endif
  49. assert(m_length >= 0);
  50. if (m_length < 0)
  51. return false;
  52. #ifdef _MSC_VER
  53. status = _fseeki64(m_file, 0L, SEEK_SET);
  54. if (status)
  55. return false; // error
  56. #else
  57. fseek(m_file, 0L, SEEK_SET);
  58. #endif
  59. return true;
  60. }
  61. void MkvReader::Close() {
  62. if (m_file != NULL) {
  63. fclose(m_file);
  64. m_file = NULL;
  65. }
  66. }
  67. int MkvReader::Length(long long* total, long long* available) {
  68. if (m_file == NULL)
  69. return -1;
  70. if (total)
  71. *total = m_length;
  72. if (available)
  73. *available = m_length;
  74. return 0;
  75. }
  76. int MkvReader::Read(long long offset, long len, unsigned char* buffer) {
  77. if (m_file == NULL)
  78. return -1;
  79. if (offset < 0)
  80. return -1;
  81. if (len < 0)
  82. return -1;
  83. if (len == 0)
  84. return 0;
  85. if (offset >= m_length)
  86. return -1;
  87. #ifdef _MSC_VER
  88. const int status = _fseeki64(m_file, offset, SEEK_SET);
  89. if (status)
  90. return -1; // error
  91. #elif defined(_WIN32)
  92. fseeko64(m_file, static_cast<off_t>(offset), SEEK_SET);
  93. #else
  94. fseeko(m_file, static_cast<off_t>(offset), SEEK_SET);
  95. #endif
  96. const size_t size = fread(buffer, 1, len, m_file);
  97. if (size < size_t(len))
  98. return -1; // error
  99. return 0; // success
  100. }
  101. } // namespace mkvparser