CCompressedStream.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * CCompressedStream.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CCompressedStream.h"
  12. #include <zlib.h>
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. static const int inflateBlockSize = 10000;
  15. CBufferedStream::CBufferedStream():
  16. position(0),
  17. endOfFileReached(false)
  18. {
  19. }
  20. si64 CBufferedStream::read(ui8 * data, si64 size)
  21. {
  22. ensureSize(position + size);
  23. auto * start = buffer.data() + position;
  24. si64 toRead = std::min<si64>(size, buffer.size() - position);
  25. std::copy(start, start + toRead, data);
  26. position += toRead;
  27. return size;
  28. }
  29. si64 CBufferedStream::seek(si64 position)
  30. {
  31. ensureSize(position);
  32. this->position = std::min<si64>(position, buffer.size());
  33. return this->position;
  34. }
  35. si64 CBufferedStream::tell()
  36. {
  37. return position;
  38. }
  39. si64 CBufferedStream::skip(si64 delta)
  40. {
  41. return seek(position + delta) - delta;
  42. }
  43. si64 CBufferedStream::getSize()
  44. {
  45. si64 prevPos = tell();
  46. seek(std::numeric_limits<si64>::max());
  47. si64 size = tell();
  48. seek(prevPos);
  49. return size;
  50. }
  51. void CBufferedStream::ensureSize(si64 size)
  52. {
  53. while(static_cast<si64>(buffer.size()) < size && !endOfFileReached)
  54. {
  55. si64 initialSize = buffer.size();
  56. si64 currentStep = std::min<si64>(size, buffer.size());
  57. // to avoid large number of calls at start
  58. // this is often used to load h3m map headers, most of which are ~300 bytes in size
  59. vstd::amax(currentStep, 512);
  60. buffer.resize(initialSize + currentStep);
  61. si64 readSize = readMore(buffer.data() + initialSize, currentStep);
  62. if (readSize != currentStep)
  63. {
  64. endOfFileReached = true;
  65. buffer.resize(initialSize + readSize);
  66. buffer.shrink_to_fit();
  67. return;
  68. }
  69. }
  70. }
  71. void CBufferedStream::reset()
  72. {
  73. buffer.clear();
  74. position = 0;
  75. endOfFileReached = false;
  76. }
  77. CCompressedStream::CCompressedStream(std::unique_ptr<CInputStream> stream, bool gzip, size_t decompressedSize):
  78. gzipStream(std::move(stream)),
  79. compressedBuffer(inflateBlockSize)
  80. {
  81. assert(gzipStream);
  82. // Allocate inflate state
  83. inflateState = new z_stream();
  84. inflateState->zalloc = Z_NULL;
  85. inflateState->zfree = Z_NULL;
  86. inflateState->opaque = Z_NULL;
  87. inflateState->avail_in = 0;
  88. inflateState->next_in = Z_NULL;
  89. int wbits = 15;
  90. if (gzip)
  91. wbits += 16;
  92. int ret = inflateInit2(inflateState, wbits);
  93. if (ret != Z_OK)
  94. throw std::runtime_error("Failed to initialize inflate!\n");
  95. }
  96. CCompressedStream::~CCompressedStream()
  97. {
  98. inflateEnd(inflateState);
  99. vstd::clear_pointer(inflateState);
  100. }
  101. si64 CCompressedStream::readMore(ui8 *data, si64 size)
  102. {
  103. if (inflateState == nullptr)
  104. return 0; //file already decompressed
  105. bool fileEnded = false; //end of file reached
  106. bool endLoop = false;
  107. int decompressed = inflateState->total_out;
  108. inflateState->avail_out = static_cast<uInt>(size);
  109. inflateState->next_out = data;
  110. do
  111. {
  112. if (inflateState->avail_in == 0)
  113. {
  114. //inflate ran out of available data or was not initialized yet
  115. // get new input data and update state accordingly
  116. si64 availSize = gzipStream->read(compressedBuffer.data(), compressedBuffer.size());
  117. if (availSize != compressedBuffer.size())
  118. gzipStream.reset();
  119. inflateState->avail_in = static_cast<uInt>(availSize);
  120. inflateState->next_in = compressedBuffer.data();
  121. }
  122. int ret = inflate(inflateState, Z_NO_FLUSH);
  123. if (inflateState->avail_in == 0 && gzipStream == nullptr)
  124. fileEnded = true;
  125. switch (ret)
  126. {
  127. case Z_OK: //input data ended/ output buffer full
  128. endLoop = false;
  129. break;
  130. case Z_STREAM_END: // stream ended. Note that campaign files consist from multiple such streams
  131. endLoop = true;
  132. break;
  133. case Z_BUF_ERROR: // output buffer full. Can be triggered?
  134. endLoop = true;
  135. break;
  136. default:
  137. if (inflateState->msg == nullptr)
  138. throw DecompressionException("Error code " + std::to_string(ret));
  139. else
  140. throw DecompressionException(inflateState->msg);
  141. }
  142. }
  143. while (!endLoop && inflateState->avail_out != 0 );
  144. decompressed = inflateState->total_out - decompressed;
  145. // Clean up and return
  146. if (fileEnded)
  147. {
  148. inflateEnd(inflateState);
  149. vstd::clear_pointer(inflateState);
  150. }
  151. return decompressed;
  152. }
  153. bool CCompressedStream::getNextBlock()
  154. {
  155. if (!inflateState)
  156. return false;
  157. if (inflateReset(inflateState) < 0)
  158. return false;
  159. reset();
  160. return true;
  161. }
  162. VCMI_LIB_NAMESPACE_END