Jamie Reece Wilson
3db5554673
[+] brotli now respects CompressInfo::uBlockSize [*] refactored CompressInfo::uLz4BlockSize -> CompressInfo::uBlockSize [*] zstd decompressor now respects user arguments [*] zstd compressor now respects user arguments [*] brotli decompressor now respects user arguments [*] brotli compressor now respects user arguments
125 lines
3.6 KiB
C++
125 lines
3.6 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: ZSTDDecompressor.hpp
|
|
Date: 2022-2-15
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
#include "zstd.h"
|
|
|
|
namespace Aurora::Compression
|
|
{
|
|
struct ZSTDInflate : BaseStream
|
|
{
|
|
DecompressInfo meta;
|
|
|
|
ZSTDInflate(const DecompressInfo &meta) : meta(meta), BaseStream(meta.uInternalStreamSize)
|
|
{}
|
|
|
|
~ZSTDInflate()
|
|
{
|
|
if (auto dctx = AuExchange(this->dctx_, {}))
|
|
{
|
|
ZSTD_freeDCtx(dctx);
|
|
}
|
|
}
|
|
|
|
bool Init(const AuSPtr<IO::IStreamReader> &pReader) override
|
|
{
|
|
this->pReader_ = pReader;
|
|
this->dctx_ = ZSTD_createDCtx();
|
|
|
|
if (!this->IsValid())
|
|
{
|
|
SysPushErrorMem();
|
|
return false;
|
|
}
|
|
|
|
if (!this->dctx_)
|
|
{
|
|
SysPushErrorGen("Couldn't create decompressor");
|
|
return false;
|
|
}
|
|
|
|
for (const auto &[a, b] : meta.options)
|
|
{
|
|
auto uRet = ZSTD_DCtx_setParameter(this->dctx_, (ZSTD_dParameter)a, b);
|
|
if (ZSTD_isError(uRet))
|
|
{
|
|
SysPushErrorArg("Decompressor argument assignment {} = {}", a, b);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
this->pIterator_ = this->din_;
|
|
this->uAvailableIn_ = 0;
|
|
this->SetArray(this->din_);
|
|
this->SetOutArray(this->dout_);
|
|
return true;
|
|
}
|
|
|
|
AuStreamReadWrittenPair_t Ingest_s(AuUInt32 input) override
|
|
{
|
|
AuUInt32 uLength = AuUInt32(ZSTD_DStreamInSize());
|
|
AuUInt32 uOutFrameLength = AuUInt32(ZSTD_DStreamOutSize());
|
|
AuUInt32 done {}, read {};
|
|
|
|
if (!this->pReader_)
|
|
{
|
|
return {};
|
|
}
|
|
|
|
while (read < input)
|
|
{
|
|
read += IngestForInPointer<char, AuUInt32>(this->pReader_, this->pIterator_, this->uAvailableIn_, input - read, this);
|
|
|
|
if (!this->uAvailableIn_)
|
|
{
|
|
return {read, done};
|
|
}
|
|
|
|
this->input_ = ZSTD_inBuffer {this->pIterator_, this->uAvailableIn_, 0};
|
|
|
|
while (this->input_.pos < this->input_.size)
|
|
{
|
|
auto [pMainDOut, uMainDOutLength] = this->GetDOutPair();
|
|
ZSTD_outBuffer output = { pMainDOut, uMainDOutLength, 0 };
|
|
|
|
auto ret = ZSTD_decompressStream(this->dctx_, &output, &this->input_);
|
|
if (ZSTD_isError(ret))
|
|
{
|
|
SysPushErrorIO("Decompression error: {}", ZSTD_getErrorName(ret));
|
|
this->pReader_.reset();
|
|
return AuMakePair(read, 0);
|
|
}
|
|
|
|
done += AuUInt32(output.pos);
|
|
|
|
if (!Write2(reinterpret_cast<const AuUInt8 *>(pMainDOut),
|
|
AuUInt32(output.pos)))
|
|
{
|
|
SysPushErrorIO("Compression Out of Overhead");
|
|
this->pReader_.reset();
|
|
return AuMakePair(read, 0);
|
|
}
|
|
}
|
|
|
|
this->uAvailableIn_ = 0;
|
|
}
|
|
|
|
return {read, done};
|
|
}
|
|
|
|
private:
|
|
|
|
AuSPtr<IO::IStreamReader> pReader_;
|
|
ZSTD_DCtx *dctx_ {};
|
|
char din_[ZSTD_BLOCKSIZE_MAX + 3 /*ZSTD_BLOCKHEADERSIZE*/];
|
|
char dout_[ZSTD_BLOCKSIZE_MAX];
|
|
char *pIterator_ {};
|
|
AuUInt32 uAvailableIn_ {};
|
|
ZSTD_inBuffer input_;
|
|
};
|
|
} |