2
0

Compression.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "Compression.h"
  34. #include <assimp/ai_assert.h>
  35. #include <assimp/Exceptional.h>
  36. namespace Assimp {
  37. struct Compression::impl {
  38. bool mOpen;
  39. z_stream mZSstream;
  40. FlushMode mFlushMode;
  41. impl() :
  42. mOpen(false),
  43. mZSstream(),
  44. mFlushMode(Compression::FlushMode::NoFlush) {
  45. // empty
  46. }
  47. };
  48. Compression::Compression() :
  49. mImpl(new impl) {
  50. // empty
  51. }
  52. Compression::~Compression() {
  53. ai_assert(mImpl != nullptr);
  54. if (mImpl->mOpen) {
  55. close();
  56. }
  57. delete mImpl;
  58. }
  59. bool Compression::open(Format format, FlushMode flush, int windowBits) {
  60. ai_assert(mImpl != nullptr);
  61. if (mImpl->mOpen) {
  62. return false;
  63. }
  64. // build a zlib stream
  65. mImpl->mZSstream.opaque = Z_NULL;
  66. mImpl->mZSstream.zalloc = Z_NULL;
  67. mImpl->mZSstream.zfree = Z_NULL;
  68. mImpl->mFlushMode = flush;
  69. if (format == Format::Binary) {
  70. mImpl->mZSstream.data_type = Z_BINARY;
  71. } else {
  72. mImpl->mZSstream.data_type = Z_ASCII;
  73. }
  74. // raw decompression without a zlib or gzip header
  75. if (windowBits == 0) {
  76. inflateInit(&mImpl->mZSstream);
  77. } else {
  78. inflateInit2(&mImpl->mZSstream, windowBits);
  79. }
  80. mImpl->mOpen = true;
  81. return mImpl->mOpen;
  82. }
  83. static int getFlushMode(Compression::FlushMode flush) {
  84. int z_flush = 0;
  85. switch (flush) {
  86. case Compression::FlushMode::NoFlush:
  87. z_flush = Z_NO_FLUSH;
  88. break;
  89. case Compression::FlushMode::Block:
  90. z_flush = Z_BLOCK;
  91. break;
  92. case Compression::FlushMode::Tree:
  93. z_flush = Z_TREES;
  94. break;
  95. case Compression::FlushMode::SyncFlush:
  96. z_flush = Z_SYNC_FLUSH;
  97. break;
  98. case Compression::FlushMode::Finish:
  99. z_flush = Z_FINISH;
  100. break;
  101. default:
  102. ai_assert(false);
  103. break;
  104. }
  105. return z_flush;
  106. }
  107. static constexpr size_t MYBLOCK = 32786;
  108. size_t Compression::decompress(const void *data, size_t in, std::vector<char> &uncompressed) {
  109. ai_assert(mImpl != nullptr);
  110. if (data == nullptr || in == 0) {
  111. return 0l;
  112. }
  113. mImpl->mZSstream.next_in = (Bytef*)(data);
  114. mImpl->mZSstream.avail_in = (uInt)in;
  115. int ret = 0;
  116. size_t total = 0l;
  117. const int flushMode = getFlushMode(mImpl->mFlushMode);
  118. if (flushMode == Z_FINISH) {
  119. mImpl->mZSstream.avail_out = static_cast<uInt>(uncompressed.size());
  120. mImpl->mZSstream.next_out = reinterpret_cast<Bytef *>(&*uncompressed.begin());
  121. ret = inflate(&mImpl->mZSstream, Z_FINISH);
  122. if (ret != Z_STREAM_END && ret != Z_OK) {
  123. throw DeadlyImportError("Compression", "Failure decompressing this file using gzip.");
  124. }
  125. total = mImpl->mZSstream.avail_out;
  126. } else {
  127. do {
  128. Bytef block[MYBLOCK] = {};
  129. mImpl->mZSstream.avail_out = MYBLOCK;
  130. mImpl->mZSstream.next_out = block;
  131. ret = inflate(&mImpl->mZSstream, flushMode);
  132. if (ret != Z_STREAM_END && ret != Z_OK) {
  133. throw DeadlyImportError("Compression", "Failure decompressing this file using gzip.");
  134. }
  135. const size_t have = MYBLOCK - mImpl->mZSstream.avail_out;
  136. total += have;
  137. uncompressed.resize(total);
  138. ::memcpy(uncompressed.data() + total - have, block, have);
  139. } while (ret != Z_STREAM_END);
  140. }
  141. return total;
  142. }
  143. size_t Compression::decompressBlock(const void *data, size_t in, char *out, size_t availableOut) {
  144. ai_assert(mImpl != nullptr);
  145. if (data == nullptr || in == 0 || out == nullptr || availableOut == 0) {
  146. return 0l;
  147. }
  148. // push data to the stream
  149. mImpl->mZSstream.next_in = (Bytef *)data;
  150. mImpl->mZSstream.avail_in = (uInt)in;
  151. mImpl->mZSstream.next_out = (Bytef *)out;
  152. mImpl->mZSstream.avail_out = (uInt)availableOut;
  153. // and decompress the data ....
  154. int ret = ::inflate(&mImpl->mZSstream, Z_SYNC_FLUSH);
  155. if (ret != Z_OK && ret != Z_STREAM_END) {
  156. throw DeadlyImportError("X: Failed to decompress MSZIP-compressed data");
  157. }
  158. ::inflateReset(&mImpl->mZSstream);
  159. ::inflateSetDictionary(&mImpl->mZSstream, (const Bytef *)out, (uInt)availableOut - mImpl->mZSstream.avail_out);
  160. return availableOut - (size_t)mImpl->mZSstream.avail_out;
  161. }
  162. bool Compression::isOpen() const {
  163. ai_assert(mImpl != nullptr);
  164. return mImpl->mOpen;
  165. }
  166. bool Compression::close() {
  167. ai_assert(mImpl != nullptr);
  168. if (!mImpl->mOpen) {
  169. return false;
  170. }
  171. inflateEnd(&mImpl->mZSstream);
  172. mImpl->mOpen = false;
  173. return true;
  174. }
  175. } // namespace Assimp