2022-09-28 08:45:15 +00:00
|
|
|
#include "config.h"
|
2019-02-13 14:47:24 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if USE_BROTLI
|
|
|
|
# include <brotli/decode.h>
|
|
|
|
# include "BrotliReadBuffer.h"
|
2023-04-27 02:36:50 +00:00
|
|
|
# include <IO/WithFileName.h>
|
2019-02-04 22:15:08 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-02-11 20:42:46 +00:00
|
|
|
|
2019-04-10 16:12:31 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BROTLI_READ_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-11 20:42:46 +00:00
|
|
|
class BrotliReadBuffer::BrotliStateWrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BrotliStateWrapper()
|
|
|
|
: state(BrotliDecoderCreateInstance(nullptr, nullptr, nullptr))
|
|
|
|
, result(BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BrotliStateWrapper()
|
|
|
|
{
|
|
|
|
BrotliDecoderDestroyInstance(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
BrotliDecoderState * state;
|
|
|
|
BrotliDecoderResult result;
|
|
|
|
};
|
|
|
|
|
2019-11-19 13:57:54 +00:00
|
|
|
BrotliReadBuffer::BrotliReadBuffer(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)
|
|
|
|
, brotli(std::make_unique<BrotliStateWrapper>())
|
|
|
|
, in_available(0)
|
|
|
|
, in_data(nullptr)
|
|
|
|
, out_capacity(0)
|
|
|
|
, out_data(nullptr)
|
|
|
|
, eof_flag(false)
|
2019-02-04 22:15:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-08 21:18:53 +00:00
|
|
|
BrotliReadBuffer::~BrotliReadBuffer() = default;
|
2019-02-04 22:15:08 +00:00
|
|
|
|
|
|
|
bool BrotliReadBuffer::nextImpl()
|
|
|
|
{
|
2021-12-30 04:47:34 +00:00
|
|
|
if (eof_flag)
|
2019-02-04 22:15:08 +00:00
|
|
|
return false;
|
|
|
|
|
2022-03-14 18:58:05 +00:00
|
|
|
do
|
2019-02-05 23:12:31 +00:00
|
|
|
{
|
2022-03-14 18:58:05 +00:00
|
|
|
if (!in_available)
|
|
|
|
{
|
|
|
|
in->nextIfAtEnd();
|
|
|
|
in_available = in->buffer().end() - in->position();
|
|
|
|
in_data = reinterpret_cast<uint8_t *>(in->position());
|
|
|
|
}
|
2019-02-05 23:12:31 +00:00
|
|
|
|
2022-03-14 18:58:05 +00:00
|
|
|
if (brotli->result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT && (!in_available || in->eof()))
|
|
|
|
{
|
2023-04-27 02:36:50 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::BROTLI_READ_FAILED,
|
|
|
|
"brotli decode error{}",
|
|
|
|
getExceptionEntryWithFileName(*in));
|
2022-03-14 18:58:05 +00:00
|
|
|
}
|
2019-02-04 22:15:08 +00:00
|
|
|
|
2022-03-14 18:58:05 +00:00
|
|
|
out_capacity = internal_buffer.size();
|
|
|
|
out_data = reinterpret_cast<uint8_t *>(internal_buffer.begin());
|
2019-02-04 22:15:08 +00:00
|
|
|
|
2022-03-14 18:58:05 +00:00
|
|
|
brotli->result = BrotliDecoderDecompressStream(brotli->state, &in_available, &in_data, &out_capacity, &out_data, nullptr);
|
|
|
|
|
|
|
|
in->position() = in->buffer().end() - in_available;
|
|
|
|
}
|
|
|
|
while (brotli->result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT && out_capacity == internal_buffer.size());
|
2019-02-04 22:15:08 +00:00
|
|
|
|
2019-02-05 23:12:31 +00:00
|
|
|
working_buffer.resize(internal_buffer.size() - out_capacity);
|
|
|
|
|
2019-02-11 20:42:46 +00:00
|
|
|
if (brotli->result == BROTLI_DECODER_RESULT_SUCCESS)
|
2019-02-05 23:12:31 +00:00
|
|
|
{
|
2019-11-19 13:57:54 +00:00
|
|
|
if (in->eof())
|
2019-02-05 23:12:31 +00:00
|
|
|
{
|
2021-12-30 04:47:34 +00:00
|
|
|
eof_flag = true;
|
2021-02-04 23:14:17 +00:00
|
|
|
return !working_buffer.empty();
|
2019-02-05 23:12:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-27 02:36:50 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::BROTLI_READ_FAILED,
|
|
|
|
"brotli decode error{}",
|
|
|
|
getExceptionEntryWithFileName(*in));
|
2019-02-05 23:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-04 22:15:08 +00:00
|
|
|
|
2019-02-11 20:42:46 +00:00
|
|
|
if (brotli->result == BROTLI_DECODER_RESULT_ERROR)
|
2019-02-05 23:12:31 +00:00
|
|
|
{
|
2023-04-27 02:36:50 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::BROTLI_READ_FAILED,
|
|
|
|
"brotli decode error{}",
|
|
|
|
getExceptionEntryWithFileName(*in));
|
2019-02-04 22:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-06 06:05:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:47:24 +00:00
|
|
|
#endif
|