2
0

DefaultIOStream.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file DefaultIOStream.cpp
  35. * @brief Default File I/O implementation for #Importer
  36. */
  37. #include <assimp/DefaultIOStream.h>
  38. #include <assimp/ai_assert.h>
  39. #include <sys/stat.h>
  40. #include <sys/types.h>
  41. using namespace Assimp;
  42. namespace {
  43. template <size_t sizeOfPointer>
  44. size_t select_ftell(FILE *file) {
  45. return ::ftell(file);
  46. }
  47. template <size_t sizeOfPointer>
  48. int select_fseek(FILE *file, int64_t offset, int origin) {
  49. return ::fseek(file, static_cast<long>(offset), origin);
  50. }
  51. #if defined _WIN32 && (!defined __GNUC__ || __MSVCRT_VERSION__ >= 0x0601)
  52. template <>
  53. size_t select_ftell<8>(FILE *file) {
  54. return (size_t)::_ftelli64(file);
  55. }
  56. template <>
  57. int select_fseek<8>(FILE *file, int64_t offset, int origin) {
  58. return ::_fseeki64(file, offset, origin);
  59. }
  60. #endif
  61. } // namespace
  62. // ----------------------------------------------------------------------------------
  63. DefaultIOStream::~DefaultIOStream() {
  64. if (mFile) {
  65. ::fclose(mFile);
  66. mFile = nullptr;
  67. }
  68. }
  69. // ----------------------------------------------------------------------------------
  70. size_t DefaultIOStream::Read(void *pvBuffer,
  71. size_t pSize,
  72. size_t pCount) {
  73. ai_assert(nullptr != pvBuffer);
  74. ai_assert(0 != pSize);
  75. ai_assert(0 != pCount);
  76. return (mFile ? ::fread(pvBuffer, pSize, pCount, mFile) : 0);
  77. }
  78. // ----------------------------------------------------------------------------------
  79. size_t DefaultIOStream::Write(const void *pvBuffer,
  80. size_t pSize,
  81. size_t pCount) {
  82. ai_assert(nullptr != pvBuffer);
  83. ai_assert(0 != pSize);
  84. return (mFile ? ::fwrite(pvBuffer, pSize, pCount, mFile) : 0);
  85. }
  86. // ----------------------------------------------------------------------------------
  87. aiReturn DefaultIOStream::Seek(size_t pOffset,
  88. aiOrigin pOrigin) {
  89. if (!mFile) {
  90. return AI_FAILURE;
  91. }
  92. // Just to check whether our enum maps one to one with the CRT constants
  93. static_assert(aiOrigin_CUR == SEEK_CUR &&
  94. aiOrigin_END == SEEK_END && aiOrigin_SET == SEEK_SET,
  95. "aiOrigin_CUR == SEEK_CUR && \
  96. aiOrigin_END == SEEK_END && aiOrigin_SET == SEEK_SET");
  97. // do the seek
  98. return (0 == select_fseek<sizeof(void *)>(mFile, (int64_t)pOffset, (int)pOrigin) ? AI_SUCCESS : AI_FAILURE);
  99. }
  100. // ----------------------------------------------------------------------------------
  101. size_t DefaultIOStream::Tell() const {
  102. if (!mFile) {
  103. return 0;
  104. }
  105. return select_ftell<sizeof(void *)>(mFile);
  106. }
  107. // ----------------------------------------------------------------------------------
  108. size_t DefaultIOStream::FileSize() const {
  109. if (!mFile || mFilename.empty()) {
  110. return 0;
  111. }
  112. if (SIZE_MAX == mCachedSize) {
  113. // Although fseek/ftell would allow us to reuse the existing file handle here,
  114. // it is generally unsafe because:
  115. // - For binary streams, it is not technically well-defined
  116. // - For text files the results are meaningless
  117. // That's why we use the safer variant fstat here.
  118. //
  119. // See here for details:
  120. // https://www.securecoding.cert.org/confluence/display/seccode/FIO19-C.+Do+not+use+fseek()+and+ftell()+to+compute+the+size+of+a+regular+file
  121. #if defined _WIN32 && (!defined __GNUC__ || __MSVCRT_VERSION__ >= 0x0601)
  122. struct __stat64 fileStat;
  123. //using fileno + fstat avoids having to handle the filename
  124. int err = _fstat64(_fileno(mFile), &fileStat);
  125. if (0 != err)
  126. return 0;
  127. mCachedSize = (size_t)(fileStat.st_size);
  128. #elif defined __GNUC__ || defined __APPLE__ || defined __MACH__ || defined __FreeBSD__
  129. struct stat fileStat;
  130. int err = stat(mFilename.c_str(), &fileStat);
  131. if (0 != err)
  132. return 0;
  133. const unsigned long long cachedSize = fileStat.st_size;
  134. mCachedSize = static_cast<size_t>(cachedSize);
  135. #else
  136. #error "Unknown platform"
  137. #endif
  138. }
  139. return mCachedSize;
  140. }
  141. // ----------------------------------------------------------------------------------
  142. void DefaultIOStream::Flush() {
  143. if (mFile) {
  144. ::fflush(mFile);
  145. }
  146. }
  147. // ----------------------------------------------------------------------------------