2020-11-09 22:52:22 +00:00
|
|
|
#include <IO/LZMAInflatingReadBuffer.h>
|
2023-04-27 02:36:50 +00:00
|
|
|
#include <IO/WithFileName.h>
|
2020-10-31 23:56:41 +00:00
|
|
|
|
2020-11-04 16:39:26 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-10-31 23:56:41 +00:00
|
|
|
extern const int LZMA_STREAM_DECODER_FAILED;
|
|
|
|
}
|
2022-03-15 13:00:53 +00:00
|
|
|
|
2020-11-09 22:52:22 +00:00
|
|
|
LZMAInflatingReadBuffer::LZMAInflatingReadBuffer(std::unique_ptr<ReadBuffer> in_, size_t buf_size, char * existing_memory, size_t alignment)
|
2022-04-15 23:56:45 +00:00
|
|
|
: CompressedReadBufferWrapper(std::move(in_), buf_size, existing_memory, alignment), eof_flag(false)
|
2020-10-31 23:56:41 +00:00
|
|
|
{
|
2020-11-01 00:14:40 +00:00
|
|
|
lstr = LZMA_STREAM_INIT;
|
2020-10-31 23:56:41 +00:00
|
|
|
lstr.allocator = nullptr;
|
|
|
|
lstr.next_in = nullptr;
|
|
|
|
lstr.avail_in = 0;
|
|
|
|
lstr.next_out = nullptr;
|
|
|
|
lstr.avail_out = 0;
|
|
|
|
|
|
|
|
// 500 mb
|
2020-11-11 01:50:56 +00:00
|
|
|
uint64_t memlimit = 500ULL << 20;
|
2020-10-31 23:56:41 +00:00
|
|
|
|
2020-11-01 23:52:34 +00:00
|
|
|
lzma_ret ret = lzma_stream_decoder(&lstr, memlimit, LZMA_CONCATENATED);
|
2020-10-31 23:56:41 +00:00
|
|
|
// 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(
|
2020-11-11 01:50:56 +00:00
|
|
|
ErrorCodes::LZMA_STREAM_DECODER_FAILED,
|
|
|
|
"lzma_stream_decoder initialization failed: error code: {}; lzma version: {}",
|
|
|
|
ret,
|
|
|
|
LZMA_VERSION_STRING);
|
2020-10-31 23:56:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 22:52:22 +00:00
|
|
|
LZMAInflatingReadBuffer::~LZMAInflatingReadBuffer()
|
2020-10-31 23:56:41 +00:00
|
|
|
{
|
2020-11-11 01:50:56 +00:00
|
|
|
lzma_end(&lstr);
|
2020-10-31 23:56:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 22:52:22 +00:00
|
|
|
bool LZMAInflatingReadBuffer::nextImpl()
|
2020-10-31 23:56:41 +00:00
|
|
|
{
|
2021-12-30 04:47:34 +00:00
|
|
|
if (eof_flag)
|
2020-10-31 23:56:41 +00:00
|
|
|
return false;
|
2020-11-02 20:04:49 +00:00
|
|
|
|
2020-11-11 01:50:56 +00:00
|
|
|
lzma_action action = LZMA_RUN;
|
2022-03-15 13:00:53 +00:00
|
|
|
lzma_ret ret;
|
2020-11-11 01:50:56 +00:00
|
|
|
|
2022-03-15 13:00:53 +00:00
|
|
|
do
|
2020-11-04 16:39:26 +00:00
|
|
|
{
|
2022-03-15 13:00:53 +00:00
|
|
|
if (!lstr.avail_in)
|
|
|
|
{
|
|
|
|
in->nextIfAtEnd();
|
|
|
|
lstr.next_in = reinterpret_cast<unsigned char *>(in->position());
|
|
|
|
lstr.avail_in = in->buffer().end() - in->position();
|
|
|
|
}
|
2020-11-11 01:50:56 +00:00
|
|
|
|
2022-03-15 13:00:53 +00:00
|
|
|
if (in->eof())
|
|
|
|
{
|
|
|
|
action = LZMA_FINISH;
|
|
|
|
}
|
|
|
|
|
|
|
|
lstr.next_out = reinterpret_cast<unsigned char *>(internal_buffer.begin());
|
|
|
|
lstr.avail_out = internal_buffer.size();
|
2020-11-11 01:50:56 +00:00
|
|
|
|
2022-03-15 13:00:53 +00:00
|
|
|
ret = lzma_code(&lstr, action);
|
|
|
|
in->position() = in->buffer().end() - lstr.avail_in;
|
|
|
|
}
|
|
|
|
while (ret == LZMA_OK && lstr.avail_out == internal_buffer.size());
|
2020-10-31 23:56:41 +00:00
|
|
|
|
2020-11-01 23:52:34 +00:00
|
|
|
working_buffer.resize(internal_buffer.size() - lstr.avail_out);
|
2020-10-31 23:56:41 +00:00
|
|
|
|
2020-11-04 16:39:26 +00:00
|
|
|
if (ret == LZMA_STREAM_END)
|
|
|
|
{
|
|
|
|
if (in->eof())
|
|
|
|
{
|
2021-12-30 04:47:34 +00:00
|
|
|
eof_flag = 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,
|
2023-04-27 02:36:50 +00:00
|
|
|
"lzma decoder finished, but input stream has not exceeded: error code: {}; lzma version: {}{}",
|
2020-11-04 16:39:26 +00:00
|
|
|
ret,
|
2023-04-27 02:36:50 +00:00
|
|
|
LZMA_VERSION_STRING,
|
|
|
|
getExceptionEntryWithFileName(*in));
|
2020-10-31 23:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != LZMA_OK)
|
2020-11-04 16:39:26 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::LZMA_STREAM_DECODER_FAILED,
|
2023-04-27 02:36:50 +00:00
|
|
|
"lzma_stream_decoder failed: error code: error code {}; lzma version: {}{}",
|
2020-11-04 16:39:26 +00:00
|
|
|
ret,
|
2023-04-27 02:36:50 +00:00
|
|
|
LZMA_VERSION_STRING,
|
|
|
|
getExceptionEntryWithFileName(*in));
|
2020-10-31 23:56:41 +00:00
|
|
|
|
2020-11-01 18:40:05 +00:00
|
|
|
return true;
|
2020-10-31 23:56:41 +00:00
|
|
|
}
|
2020-11-02 20:04:49 +00:00
|
|
|
}
|