ClickHouse/src/IO/LZMAInflatingReadBuffer.cpp

92 lines
2.4 KiB
C++
Raw Normal View History

#include <IO/LZMAInflatingReadBuffer.h>
2020-11-12 09:21:33 +00:00
#if !defined(ARCADIA_BUILD)
2020-11-04 16:39:26 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int LZMA_STREAM_DECODER_FAILED;
}
LZMAInflatingReadBuffer::LZMAInflatingReadBuffer(std::unique_ptr<ReadBuffer> in_, size_t buf_size, char * existing_memory, size_t alignment)
2020-11-04 12:45:37 +00:00
: BufferWithOwnMemory<ReadBuffer>(buf_size, existing_memory, alignment), in(std::move(in_)), eof(false)
{
lstr = LZMA_STREAM_INIT;
lstr.allocator = nullptr;
lstr.next_in = nullptr;
lstr.avail_in = 0;
lstr.next_out = nullptr;
lstr.avail_out = 0;
// 500 mb
uint64_t memlimit = 500ULL << 20;
lzma_ret ret = lzma_stream_decoder(&lstr, memlimit, LZMA_CONCATENATED);
// lzma does not provide api for converting error code to string unlike zlib
if (ret != LZMA_OK)
2020-11-04 16:39:26 +00:00
throw Exception(
ErrorCodes::LZMA_STREAM_DECODER_FAILED,
"lzma_stream_decoder initialization failed: error code: {}; lzma version: {}",
ret,
LZMA_VERSION_STRING);
}
LZMAInflatingReadBuffer::~LZMAInflatingReadBuffer()
{
lzma_end(&lstr);
}
bool LZMAInflatingReadBuffer::nextImpl()
{
if (eof)
return false;
lzma_action action = LZMA_RUN;
2020-11-04 16:39:26 +00:00
if (!lstr.avail_in)
{
in->nextIfAtEnd();
2020-11-04 16:39:26 +00:00
lstr.next_in = reinterpret_cast<unsigned char *>(in->position());
lstr.avail_in = in->buffer().end() - in->position();
}
if (in->eof())
{
action = LZMA_FINISH;
}
2020-11-04 16:39:26 +00:00
lstr.next_out = reinterpret_cast<unsigned char *>(internal_buffer.begin());
lstr.avail_out = internal_buffer.size();
lzma_ret ret = lzma_code(&lstr, action);
in->position() = in->buffer().end() - lstr.avail_in;
working_buffer.resize(internal_buffer.size() - lstr.avail_out);
2020-11-04 16:39:26 +00:00
if (ret == LZMA_STREAM_END)
{
if (in->eof())
{
eof = true;
2021-02-04 23:14:17 +00:00
return !working_buffer.empty();
2020-11-04 16:39:26 +00:00
}
else
{
throw Exception(
ErrorCodes::LZMA_STREAM_DECODER_FAILED,
"lzma decoder finished, but input stream has not exceeded: error code: {}; lzma version: {}",
2020-11-04 16:39:26 +00:00
ret,
LZMA_VERSION_STRING);
}
}
if (ret != LZMA_OK)
2020-11-04 16:39:26 +00:00
throw Exception(
ErrorCodes::LZMA_STREAM_DECODER_FAILED,
"lzma_stream_decoder failed: error code: error codeL {}; lzma version: {}",
ret,
LZMA_VERSION_STRING);
return true;
}
}
2020-11-12 09:21:33 +00:00
#endif