2
0

CCompressedStream.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if (inflateState == nullptr)
  69. return; //file already decompressed
  70. if (newSize >= 0 && newSize <= inflateState->total_out)
  71. return; //no need to decompress anything
  72. bool toEnd = newSize < 0; //if true - we've got request to read whole file
  73. if (toEnd && buffer.empty())
  74. buffer.resize(16 * 1024); //some space for initial decompression
  75. if (!toEnd && buffer.size() < newSize)
  76. buffer.resize(newSize);
  77. bool fileEnded = false; //end of file reached
  78. int endLoop = false;
  79. do
  80. {
  81. if (inflateState->avail_in == 0)
  82. {
  83. //inflate ran out of available data or was not initialized yet
  84. // get new input data and update state accordingly
  85. si64 size = gzipStream->read(compressedBuffer.data(), compressedBuffer.size());
  86. if (size != compressedBuffer.size())
  87. fileEnded = true; //end of file reached
  88. inflateState->avail_in = size;
  89. inflateState->next_in = compressedBuffer.data();
  90. }
  91. inflateState->avail_out = buffer.size() - inflateState->total_out;
  92. inflateState->next_out = buffer.data() + inflateState->total_out;
  93. int ret = inflate(inflateState, Z_NO_FLUSH);
  94. switch (ret)
  95. {
  96. case Z_STREAM_END: //end decompression
  97. endLoop = true;
  98. break;
  99. case Z_OK: //decompress next chunk
  100. endLoop = false;
  101. break;
  102. case Z_BUF_ERROR:
  103. {
  104. if (toEnd)
  105. {
  106. //not enough memory. Allocate bigger buffer and try again
  107. buffer.resize(buffer.size() * 2);
  108. endLoop = false;
  109. }
  110. else
  111. {
  112. //specific amount of data was requested. inflate extracted all requested data
  113. // but returned "error". Ensure that enough data was extracted and return
  114. assert(inflateState->total_out == newSize);
  115. endLoop = true;
  116. }
  117. }
  118. break;
  119. default:
  120. throw std::runtime_error("Decompression error!\n");
  121. }
  122. }
  123. while (!endLoop);
  124. // Clean up and return
  125. if (fileEnded)
  126. {
  127. inflateEnd(inflateState);
  128. buffer.resize(inflateState->total_out);
  129. decompressedSize = inflateState->total_out;
  130. vstd::clear_pointer(inflateState);
  131. }
  132. else
  133. decompressedSize = inflateState->total_out;
  134. }
  135. bool CCompressedStream::getNextBlock()
  136. {
  137. if (!inflateState)
  138. return false;
  139. if (inflateReset(inflateState) < 0)
  140. return false;
  141. decompressedSize = 0;
  142. position = 0;
  143. buffer.clear();
  144. return true;
  145. }