[+] LZ4 required some more work

This commit is contained in:
Reece Wilson 2021-07-11 12:18:33 +01:00
parent dd06417d04
commit 955578cf8e
2 changed files with 64 additions and 47 deletions

View File

@ -300,8 +300,10 @@ namespace Aurora::Compression
std::pair<AuUInt32, AuUInt32> Ingest(AuUInt32 input) override
{
AuUInt32 done {}, read {};
std::shared_ptr<char> pinShared;
AuUInt32 done {}, read {}, lastFrameSize {};
std::shared_ptr<char> inFrame;
std::shared_ptr<char> outFrames[2];
bool outFrame {};
while (read != input)
{
@ -314,26 +316,36 @@ namespace Aurora::Compression
}
read += request;
std::shared_ptr<char> inFrame(new char[frameSize], std::default_delete<char[]>());
std::shared_ptr<char> outFrame(new char[frameSize], std::default_delete<char[]>());
if ((lastFrameSize < frameSize) || (!outFrames[outFrame]))
{
inFrame = std::shared_ptr<char>(new char[frameSize], std::default_delete<char[]>());
outFrames[outFrame] = std::shared_ptr<char>(new char[frameSize], std::default_delete<char[]>());
lastFrameSize = frameSize;
}
request = frameSize;
if (this->_reader->Read(inFrame.get(), request) != IO::EStreamError::eErrorNone)
{
return std::make_pair(read, done);
}
read += request;
auto bytes = LZ4_decompress_safe_continue(_lz4Stream, inFrame.get(), outFrame.get(), frameSize, frameSize);
auto outPtr = outFrames[outFrame].get();
auto bytes = LZ4_decompress_safe_continue(_lz4Stream, inFrame.get(), outPtr, frameSize, frameSize);
if (bytes <= 0)
{
return std::make_pair(read, 0);
}
done += bytes;
pinShared = std::move(inFrame);
this->_outbuffer.insert(this->_outbuffer.end(),
reinterpret_cast<const AuUInt8 *>(outFrame.get()),
reinterpret_cast<const AuUInt8 *>(outFrame.get()) + bytes);
reinterpret_cast<const AuUInt8 *>(outPtr),
reinterpret_cast<const AuUInt8 *>(outPtr) + bytes);
outFrames[!outFrame] = std::move(outFrames[outFrame]);
outFrame = !outFrame;
}
return std::make_pair(read, done);

View File

@ -470,55 +470,60 @@ namespace Aurora::Compression
{
bool ret = true;
LZ4_streamDecode_t* lz4Stream = LZ4_createStreamDecode();
char inBuf[kChunkSize];
char outBuf[kChunkSize];
AuUInt32 inputStat = 0, outputStat = 0;
AuUInt32 inputStat {}, outputStat {}, lastFrameSize {};
std::shared_ptr<char> inFrame;
std::shared_ptr<char> outFrames[2];
bool outFrame {};
while (true)
{
AuUInt16 bufferedBytes;
auto read = pipe.inPipe(&bufferedBytes, sizeof(bufferedBytes));
if (read == 0)
AuUInt16 frameSize;
AuUInt32 request = sizeof(frameSize);
auto read = pipe.inPipe(&frameSize, sizeof(frameSize));
if (read != request)
{
ret = false;
break;
}
inputStat += request;
if ((lastFrameSize < frameSize) || (!outFrames[outFrame]))
{
inFrame = std::shared_ptr<char>(new char[frameSize], std::default_delete<char[]>());
outFrames[outFrame] = std::shared_ptr<char>(new char[frameSize], std::default_delete<char[]>());
lastFrameSize = frameSize;
}
request = frameSize;
read = pipe.inPipe(inFrame.get(), request);
if (read != request)
{
ret = false;
break;
}
inputStat += request;
auto outPtr = outFrames[outFrame].get();
auto bytes = LZ4_decompress_safe_continue(lz4Stream, inFrame.get(), outPtr, frameSize, frameSize);
if (bytes <= 0)
{
ret = false;
break;
}
if (read != 2)
{
ret = false;
goto out;
}
pipe.writePipe(outPtr, bytes);
inputStat += bytes;
//while (bufferedBytes) TODO:
{
auto toRead = std::min(AuUInt16(ArraySize(inBuf)), bufferedBytes);
read = pipe.inPipe(inBuf, toRead);
if (read != toRead)
{
ret = false;
goto out;
}
bufferedBytes -= toRead;
auto err = LZ4_decompress_safe_continue(lz4Stream, inBuf, outBuf, read, ArraySize(outBuf));
if (err < 0)
{
ret = false;
goto out;
}
pipe.writePipe(outBuf, err);
inputStat += bufferedBytes;
outputStat += read;
outFrames[!outFrame] = std::move(outFrames[outFrame]);
outFrame = !outFrame;
if (!pipe.reportProgress(inputStat, outputStat))
{
break;
}
}
}
out:
LZ4_freeStreamDecode(lz4Stream);