2023-03-12 21:03:47 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: BrotliDecompressor.hpp
|
|
|
|
Date: 2023-3-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "brotli/decode.h"
|
|
|
|
|
|
|
|
namespace Aurora::Compression
|
|
|
|
{
|
|
|
|
struct BrotliInflate : BaseStream
|
|
|
|
{
|
|
|
|
DecompressInfo meta;
|
|
|
|
|
|
|
|
BrotliInflate(const DecompressInfo &meta) : meta(meta), BaseStream(meta.uInternalStreamSize)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~BrotliInflate()
|
|
|
|
{
|
|
|
|
if (this->pState)
|
|
|
|
{
|
|
|
|
BrotliDecoderDestroyInstance(this->pState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Init(const AuSPtr<IO::IStreamReader> &reader) override
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
this->pReader_ = reader;
|
|
|
|
|
|
|
|
if (!this->IsValid())
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->pState = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
|
|
|
|
if (!this->pState)
|
|
|
|
{
|
|
|
|
SysPushErrorMem("No brotli decoder");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-27 19:45:49 +00:00
|
|
|
for (const auto &[a, b] : meta.options)
|
|
|
|
{
|
|
|
|
if (!BrotliDecoderSetParameter(this->pState, (BrotliDecoderParameter)a, b))
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Decompressor argument assignment {} = {}", a, b);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 21:03:47 +00:00
|
|
|
this->SetArray(this->din_);
|
2023-06-05 13:55:40 +00:00
|
|
|
this->SetOutArray(this->dout_);
|
2023-03-12 21:03:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Ingest_s(AuUInt32 input) override
|
|
|
|
{
|
|
|
|
AuUInt32 done {}, read {};
|
|
|
|
|
|
|
|
if (!this->pReader_)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
while (read < input)
|
|
|
|
{
|
2023-06-10 19:03:29 +00:00
|
|
|
read += IngestForInPointer<uint8_t, size_t>(this->pReader_, this->pInBuffer, this->uAvailIn, input - read, this);
|
2023-03-12 21:03:47 +00:00
|
|
|
|
|
|
|
if (!this->uAvailIn)
|
|
|
|
{
|
|
|
|
return { read, done };
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t outNext {};
|
|
|
|
uint8_t *pOut {};
|
|
|
|
do
|
|
|
|
{
|
2023-06-05 13:55:40 +00:00
|
|
|
auto [pMainDOut, uMainDOutLength] = this->GetDOutPair();
|
|
|
|
|
|
|
|
outNext = uMainDOutLength;
|
|
|
|
pOut = (uint8_t *)pMainDOut;
|
2023-03-12 21:03:47 +00:00
|
|
|
|
|
|
|
auto ret = BrotliDecoderDecompressStream(this->pState, &this->uAvailIn, (const uint8_t **)&this->pInBuffer, &outNext, &pOut, nullptr);
|
|
|
|
if (ret == BROTLI_DECODER_RESULT_ERROR)
|
|
|
|
{
|
2023-10-17 08:27:59 +00:00
|
|
|
this->SetLastError(ret, BrotliDecoderErrorString((BrotliDecoderErrorCode)ret));
|
2023-03-12 21:03:47 +00:00
|
|
|
this->pReader_.reset();
|
|
|
|
return AuMakePair(read, 0);
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
auto have = uMainDOutLength - outNext;
|
2023-03-12 21:03:47 +00:00
|
|
|
done += have;
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
if (!Write2(pMainDOut, have))
|
2023-03-12 21:03:47 +00:00
|
|
|
{
|
|
|
|
this->pReader_.reset();
|
2024-05-30 00:15:37 +00:00
|
|
|
this->SetLastError(0x69, "OOM");
|
2023-03-12 21:03:47 +00:00
|
|
|
return AuMakePair(read, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (outNext == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { read, done };
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AuSPtr<IO::IStreamReader> pReader_;
|
|
|
|
unsigned char din_[kChunkSize];
|
|
|
|
unsigned char dout_[kChunkSize];
|
|
|
|
uint8_t *pInBuffer;
|
|
|
|
size_t uAvailIn {};
|
|
|
|
BrotliDecoderState *pState {};
|
|
|
|
};
|
|
|
|
}
|