CCompressedStream.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "StdInc.h"
  2. #include "CCompressedStream.h"
  3. #include <zlib.h>
  4. static const int inflateBlockSize = 10000;
  5. CCompressedStream::CCompressedStream(std::unique_ptr<CInputStream> stream, bool gzip, size_t decompressedSize):
  6. gzipStream(std::move(stream)),
  7. buffer(decompressedSize),
  8. decompressedSize(0),
  9. compressedBuffer(inflateBlockSize),
  10. position(0)
  11. {
  12. assert(gzipStream);
  13. // Allocate inflate state
  14. inflateState = new z_stream;
  15. inflateState->zalloc = Z_NULL;
  16. inflateState->zfree = Z_NULL;
  17. inflateState->opaque = Z_NULL;
  18. inflateState->avail_in = 0;
  19. inflateState->next_in = Z_NULL;
  20. int wbits = 15;
  21. if (gzip)
  22. wbits += 16;
  23. int ret = inflateInit2(inflateState, wbits);
  24. if (ret != Z_OK)
  25. throw std::runtime_error("Failed to initialize inflate!\n");
  26. }
  27. CCompressedStream::~CCompressedStream()
  28. {
  29. if (inflateState)
  30. {
  31. inflateEnd(inflateState);
  32. delete inflateState;
  33. }
  34. }
  35. si64 CCompressedStream::read(ui8 * data, si64 size)
  36. {
  37. decompressTill(position + size);
  38. auto start = buffer.begin() + position;
  39. si64 toRead = std::min<si64>(size, decompressedSize - position);
  40. std::copy(start, start + toRead, data);
  41. position += toRead;
  42. return size;
  43. }
  44. si64 CCompressedStream::seek(si64 position)
  45. {
  46. decompressTill(position);
  47. this->position = std::min<si64>(position, decompressedSize);
  48. return this->position;
  49. }
  50. si64 CCompressedStream::tell()
  51. {
  52. return position;
  53. }
  54. si64 CCompressedStream::skip(si64 delta)
  55. {
  56. decompressTill(position + delta);
  57. si64 oldPosition = position;
  58. position = std::min<si64>(position + delta , decompressedSize);
  59. return position - oldPosition;
  60. }
  61. si64 CCompressedStream::getSize()
  62. {
  63. decompressTill(-1);
  64. return decompressedSize;
  65. }
  66. void CCompressedStream::decompressTill(si64 newSize)
  67. {
  68. assert(newSize < 100 * 1024 * 1024); //just in case
  69. if (inflateState == nullptr)
  70. return; //file already decompressed
  71. if (newSize >= 0 && newSize <= inflateState->total_out)
  72. return; //no need to decompress anything
  73. bool toEnd = newSize < 0; //if true - we've got request to read whole file
  74. if (toEnd && buffer.empty())
  75. buffer.resize(16 * 1024); //some space for initial decompression
  76. if (!toEnd && buffer.size() < newSize)
  77. buffer.resize(newSize);
  78. bool fileEnded = false; //end of file reached
  79. int endLoop = false;
  80. do
  81. {
  82. if (inflateState->avail_in == 0)
  83. {
  84. //inflate ran out of available data or was not initialized yet
  85. // get new input data and update state accordingly
  86. si64 size = gzipStream->read(compressedBuffer.data(), compressedBuffer.size());
  87. if (size != compressedBuffer.size())
  88. fileEnded = true; //end of file reached
  89. inflateState->avail_in = size;
  90. inflateState->next_in = compressedBuffer.data();
  91. }
  92. inflateState->avail_out = buffer.size() - inflateState->total_out;
  93. inflateState->next_out = buffer.data() + inflateState->total_out;
  94. int ret = inflate(inflateState, Z_NO_FLUSH);
  95. switch (ret)
  96. {
  97. case Z_STREAM_END: //end decompression
  98. endLoop = true;
  99. break;
  100. case Z_OK: //decompress next chunk
  101. endLoop = false;
  102. break;
  103. case Z_BUF_ERROR:
  104. {
  105. if (toEnd)
  106. {
  107. //not enough memory. Allocate bigger buffer and try again
  108. buffer.resize(buffer.size() * 2);
  109. endLoop = false;
  110. }
  111. else
  112. {
  113. //specific amount of data was requested. inflate extracted all requested data
  114. // but returned "error". Ensure that enough data was extracted and return
  115. assert(inflateState->total_out == newSize);
  116. endLoop = true;
  117. }
  118. }
  119. break;
  120. default:
  121. throw std::runtime_error("Decompression error!\n");
  122. }
  123. }
  124. while (!endLoop);
  125. // Clean up and return
  126. if (fileEnded)
  127. {
  128. inflateEnd(inflateState);
  129. buffer.resize(inflateState->total_out);
  130. decompressedSize = inflateState->total_out;
  131. vstd::clear_pointer(inflateState);
  132. }
  133. else
  134. decompressedSize = inflateState->total_out;
  135. }
  136. bool CCompressedStream::getNextBlock()
  137. {
  138. if (!inflateState)
  139. return false;
  140. if (inflateReset(inflateState) < 0)
  141. return false;
  142. decompressedSize = 0;
  143. position = 0;
  144. buffer.clear();
  145. return true;
  146. }