diff --git a/Source/Compression/BlockDecompressor.cpp b/Source/Compression/BlockDecompressor.cpp index 1e41c490..4f94f840 100644 --- a/Source/Compression/BlockDecompressor.cpp +++ b/Source/Compression/BlockDecompressor.cpp @@ -300,8 +300,10 @@ namespace Aurora::Compression std::pair Ingest(AuUInt32 input) override { - AuUInt32 done {}, read {}; - std::shared_ptr pinShared; + AuUInt32 done {}, read {}, lastFrameSize {}; + std::shared_ptr inFrame; + std::shared_ptr outFrames[2]; + bool outFrame {}; while (read != input) { @@ -314,26 +316,36 @@ namespace Aurora::Compression } read += request; - std::shared_ptr inFrame(new char[frameSize], std::default_delete()); - std::shared_ptr outFrame(new char[frameSize], std::default_delete()); + if ((lastFrameSize < frameSize) || (!outFrames[outFrame])) + { + inFrame = std::shared_ptr(new char[frameSize], std::default_delete()); + outFrames[outFrame] = std::shared_ptr(new char[frameSize], std::default_delete()); + 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(outFrame.get()), - reinterpret_cast(outFrame.get()) + bytes); + reinterpret_cast(outPtr), + reinterpret_cast(outPtr) + bytes); + + outFrames[!outFrame] = std::move(outFrames[outFrame]); + outFrame = !outFrame; } return std::make_pair(read, done); diff --git a/Source/Compression/StreamCompression.cpp b/Source/Compression/StreamCompression.cpp index 9416f0c7..740e1399 100644 --- a/Source/Compression/StreamCompression.cpp +++ b/Source/Compression/StreamCompression.cpp @@ -470,57 +470,62 @@ 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 inFrame; + std::shared_ptr 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(new char[frameSize], std::default_delete()); + outFrames[outFrame] = std::shared_ptr(new char[frameSize], std::default_delete()); + 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; - } - - //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); + pipe.writePipe(outPtr, bytes); + inputStat += bytes; - inputStat += bufferedBytes; - outputStat += read; + outFrames[!outFrame] = std::move(outFrames[outFrame]); + outFrame = !outFrame; - if (!pipe.reportProgress(inputStat, outputStat)) - { - break; - } + if (!pipe.reportProgress(inputStat, outputStat)) + { + break; } } - out: + out: LZ4_freeStreamDecode(lz4Stream); return ret; }