CCompressedStream.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 ((si64)buffer.size() < size && !endOfFileReached)
  54. {
  55. si64 initialSize = buffer.size();
  56. si64 currentStep = std::min<si64>(size, buffer.size());
  57. vstd::amax(currentStep, 1024); // to avoid large number of calls at start
  58. buffer.resize(initialSize + currentStep);
  59. si64 readSize = readMore(buffer.data() + initialSize, currentStep);
  60. if (readSize != currentStep)
  61. {
  62. endOfFileReached = true;
  63. buffer.resize(initialSize + readSize);
  64. buffer.shrink_to_fit();
  65. return;
  66. }
  67. }
  68. }
  69. void CBufferedStream::reset()
  70. {
  71. buffer.clear();
  72. position = 0;
  73. endOfFileReached = false;
  74. }
  75. CCompressedStream::CCompressedStream(std::unique_ptr<CInputStream> stream, bool gzip, size_t decompressedSize):
  76. gzipStream(std::move(stream)),
  77. compressedBuffer(inflateBlockSize)
  78. {
  79. assert(gzipStream);
  80. // Allocate inflate state
  81. inflateState = new z_stream();
  82. inflateState->zalloc = Z_NULL;
  83. inflateState->zfree = Z_NULL;
  84. inflateState->opaque = Z_NULL;
  85. inflateState->avail_in = 0;
  86. inflateState->next_in = Z_NULL;
  87. int wbits = 15;
  88. if (gzip)
  89. wbits += 16;
  90. int ret = inflateInit2(inflateState, wbits);
  91. if (ret != Z_OK)
  92. throw std::runtime_error("Failed to initialize inflate!\n");
  93. }
  94. CCompressedStream::~CCompressedStream()
  95. {
  96. inflateEnd(inflateState);
  97. vstd::clear_pointer(inflateState);
  98. }
  99. si64 CCompressedStream::readMore(ui8 *data, si64 size)
  100. {
  101. if (inflateState == nullptr)
  102. return 0; //file already decompressed
  103. bool fileEnded = false; //end of file reached
  104. bool endLoop = false;
  105. int decompressed = inflateState->total_out;
  106. inflateState->avail_out = (uInt)size;
  107. inflateState->next_out = data;
  108. do
  109. {
  110. if (inflateState->avail_in == 0)
  111. {
  112. //inflate ran out of available data or was not initialized yet
  113. // get new input data and update state accordingly
  114. si64 availSize = gzipStream->read(compressedBuffer.data(), compressedBuffer.size());
  115. if (availSize != compressedBuffer.size())
  116. gzipStream.reset();
  117. inflateState->avail_in = (uInt)availSize;
  118. inflateState->next_in = compressedBuffer.data();
  119. }
  120. int ret = inflate(inflateState, Z_NO_FLUSH);
  121. if (inflateState->avail_in == 0 && gzipStream == nullptr)
  122. fileEnded = true;
  123. switch (ret)
  124. {
  125. case Z_OK: //input data ended/ output buffer full
  126. endLoop = false;
  127. break;
  128. case Z_STREAM_END: // stream ended. Note that campaign files consist from multiple such streams
  129. endLoop = true;
  130. break;
  131. case Z_BUF_ERROR: // output buffer full. Can be triggered?
  132. endLoop = true;
  133. break;
  134. default:
  135. if (inflateState->msg == nullptr)
  136. throw std::runtime_error("Decompression error. Return code was " + boost::lexical_cast<std::string>(ret));
  137. else
  138. throw std::runtime_error(std::string("Decompression error: ") + inflateState->msg);
  139. }
  140. }
  141. while (!endLoop && inflateState->avail_out != 0 );
  142. decompressed = inflateState->total_out - decompressed;
  143. // Clean up and return
  144. if (fileEnded)
  145. {
  146. inflateEnd(inflateState);
  147. vstd::clear_pointer(inflateState);
  148. }
  149. return decompressed;
  150. }
  151. bool CCompressedStream::getNextBlock()
  152. {
  153. if (!inflateState)
  154. return false;
  155. if (inflateReset(inflateState) < 0)
  156. return false;
  157. reset();
  158. return true;
  159. }
  160. VCMI_LIB_NAMESPACE_END