2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CCompressedStream.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2012-08-01 15:02:54 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CCompressedStream.h"
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
static const int inflateBlockSize = 10000;
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
CBufferedStream::CBufferedStream():
|
|
|
|
position(0),
|
|
|
|
endOfFileReached(false)
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
si64 CBufferedStream::read(ui8 * data, si64 size)
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
2013-07-30 18:02:55 +03:00
|
|
|
ensureSize(position + size);
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
auto start = buffer.data() + position;
|
|
|
|
si64 toRead = std::min<si64>(size, buffer.size() - position);
|
2012-08-01 15:02:54 +03:00
|
|
|
|
|
|
|
std::copy(start, start + toRead, data);
|
|
|
|
position += toRead;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
si64 CBufferedStream::seek(si64 position)
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
2013-07-30 18:02:55 +03:00
|
|
|
ensureSize(position);
|
|
|
|
this->position = std::min<si64>(position, buffer.size());
|
2012-08-01 15:02:54 +03:00
|
|
|
return this->position;
|
|
|
|
}
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
si64 CBufferedStream::tell()
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
si64 CBufferedStream::skip(si64 delta)
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
2013-07-30 18:02:55 +03:00
|
|
|
return seek(position + delta) - delta;
|
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
si64 CBufferedStream::getSize()
|
|
|
|
{
|
|
|
|
si64 prevPos = tell();
|
|
|
|
seek(std::numeric_limits<si64>::max());
|
|
|
|
si64 size = tell();
|
|
|
|
seek(prevPos);
|
|
|
|
return size;
|
2012-08-01 15:02:54 +03:00
|
|
|
}
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
void CBufferedStream::ensureSize(si64 size)
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
2013-07-30 18:02:55 +03:00
|
|
|
while (buffer.size() < size && !endOfFileReached)
|
|
|
|
{
|
|
|
|
si64 initialSize = buffer.size();
|
|
|
|
si64 currentStep = std::min<si64>(size, buffer.size());
|
|
|
|
vstd::amax(currentStep, 1024); // to avoid large number of calls at start
|
|
|
|
|
|
|
|
buffer.resize(initialSize + currentStep);
|
|
|
|
|
|
|
|
si64 readSize = readMore(buffer.data() + initialSize, currentStep);
|
|
|
|
if (readSize != currentStep)
|
|
|
|
{
|
|
|
|
endOfFileReached = true;
|
|
|
|
buffer.resize(initialSize + readSize);
|
|
|
|
buffer.shrink_to_fit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
}
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
void CBufferedStream::reset()
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
2013-07-30 18:02:55 +03:00
|
|
|
buffer.clear();
|
|
|
|
position = 0;
|
|
|
|
endOfFileReached = false;
|
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
CCompressedStream::CCompressedStream(std::unique_ptr<CInputStream> stream, bool gzip, size_t decompressedSize):
|
|
|
|
gzipStream(std::move(stream)),
|
|
|
|
compressedBuffer(inflateBlockSize)
|
|
|
|
{
|
|
|
|
assert(gzipStream);
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
// Allocate inflate state
|
2017-07-16 11:58:05 +02:00
|
|
|
inflateState = new z_stream();
|
2013-07-30 18:02:55 +03:00
|
|
|
inflateState->zalloc = Z_NULL;
|
|
|
|
inflateState->zfree = Z_NULL;
|
|
|
|
inflateState->opaque = Z_NULL;
|
|
|
|
inflateState->avail_in = 0;
|
|
|
|
inflateState->next_in = Z_NULL;
|
|
|
|
|
|
|
|
int wbits = 15;
|
|
|
|
if (gzip)
|
|
|
|
wbits += 16;
|
|
|
|
|
|
|
|
int ret = inflateInit2(inflateState, wbits);
|
|
|
|
if (ret != Z_OK)
|
|
|
|
throw std::runtime_error("Failed to initialize inflate!\n");
|
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
CCompressedStream::~CCompressedStream()
|
|
|
|
{
|
2013-12-19 22:43:16 +03:00
|
|
|
inflateEnd(inflateState);
|
2016-08-22 11:56:05 +02:00
|
|
|
vstd::clear_pointer(inflateState);
|
2013-07-30 18:02:55 +03:00
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
si64 CCompressedStream::readMore(ui8 *data, si64 size)
|
|
|
|
{
|
|
|
|
if (inflateState == nullptr)
|
|
|
|
return 0; //file already decompressed
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2012-08-02 14:03:26 +03:00
|
|
|
bool fileEnded = false; //end of file reached
|
2013-07-30 18:02:55 +03:00
|
|
|
bool endLoop = false;
|
|
|
|
|
|
|
|
int decompressed = inflateState->total_out;
|
|
|
|
|
|
|
|
inflateState->avail_out = size;
|
|
|
|
inflateState->next_out = data;
|
2012-08-02 14:03:26 +03:00
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (inflateState->avail_in == 0)
|
|
|
|
{
|
|
|
|
//inflate ran out of available data or was not initialized yet
|
|
|
|
// get new input data and update state accordingly
|
2013-07-30 18:02:55 +03:00
|
|
|
si64 availSize = gzipStream->read(compressedBuffer.data(), compressedBuffer.size());
|
|
|
|
if (availSize != compressedBuffer.size())
|
|
|
|
gzipStream.reset();
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
inflateState->avail_in = availSize;
|
2012-08-01 15:02:54 +03:00
|
|
|
inflateState->next_in = compressedBuffer.data();
|
|
|
|
}
|
|
|
|
|
2012-08-02 14:03:26 +03:00
|
|
|
int ret = inflate(inflateState, Z_NO_FLUSH);
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
if (inflateState->avail_in == 0 && gzipStream == nullptr)
|
|
|
|
fileEnded = true;
|
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
switch (ret)
|
|
|
|
{
|
2013-07-30 18:02:55 +03:00
|
|
|
case Z_OK: //input data ended/ output buffer full
|
2012-08-01 15:02:54 +03:00
|
|
|
endLoop = false;
|
|
|
|
break;
|
2013-07-30 18:02:55 +03:00
|
|
|
case Z_STREAM_END: // stream ended. Note that campaign files consist from multiple such streams
|
|
|
|
endLoop = true;
|
|
|
|
break;
|
|
|
|
case Z_BUF_ERROR: // output buffer full. Can be triggered?
|
|
|
|
endLoop = true;
|
2012-08-01 15:02:54 +03:00
|
|
|
break;
|
|
|
|
default:
|
2013-11-10 16:43:55 +03:00
|
|
|
if (inflateState->msg == nullptr)
|
|
|
|
throw std::runtime_error("Decompression error. Return code was " + boost::lexical_cast<std::string>(ret));
|
|
|
|
else
|
|
|
|
throw std::runtime_error(std::string("Decompression error: ") + inflateState->msg);
|
2012-08-01 15:02:54 +03:00
|
|
|
}
|
|
|
|
}
|
2013-07-30 18:02:55 +03:00
|
|
|
while (endLoop == false && inflateState->avail_out != 0 );
|
|
|
|
|
|
|
|
decompressed = inflateState->total_out - decompressed;
|
2012-08-01 15:02:54 +03:00
|
|
|
|
|
|
|
// Clean up and return
|
2012-08-02 14:03:26 +03:00
|
|
|
if (fileEnded)
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
|
|
|
inflateEnd(inflateState);
|
2016-08-22 11:56:05 +02:00
|
|
|
vstd::clear_pointer(inflateState);
|
2012-08-01 15:02:54 +03:00
|
|
|
}
|
2013-07-30 18:02:55 +03:00
|
|
|
return decompressed;
|
2012-08-02 14:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCompressedStream::getNextBlock()
|
|
|
|
{
|
|
|
|
if (!inflateState)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (inflateReset(inflateState) < 0)
|
|
|
|
return false;
|
|
|
|
|
2013-07-30 18:02:55 +03:00
|
|
|
reset();
|
2012-08-02 14:03:26 +03:00
|
|
|
return true;
|
|
|
|
}
|