From 95643d6e19974b25597abc1989d6a0a87feaf0ed Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Tue, 17 Oct 2023 09:27:59 +0100 Subject: [PATCH] [+] ICompressionStream::GetLastErrorString [+] ICompressionStream::GetLastError --- .../Aurora/Compression/ICompressionStream.hpp | 12 ++++++++++ Source/Compression/AuBaseStream.cpp | 23 +++++++++++++++++++ Source/Compression/AuBaseStream.hpp | 8 +++++++ .../Compressors/BZip2Compressor.hpp | 6 ++--- .../Compressors/BZip2Decompressor.hpp | 4 ++-- .../Compressors/BrotliDecompressor.hpp | 3 +-- .../Compressors/DeflateCompressor.hpp | 8 +++---- .../Compression/Compressors/LZ4Compressor.hpp | 2 ++ .../Compressors/LZ4Decompressor.hpp | 1 + .../Compressors/LZMACompressor.hpp | 6 ++--- .../Compressors/LZMADecompressor.hpp | 6 ++--- .../Compressors/ZSTDCompressor.hpp | 14 +++++++---- .../Compressors/ZSTDDecompressor.hpp | 3 ++- 13 files changed, 73 insertions(+), 23 deletions(-) diff --git a/Include/Aurora/Compression/ICompressionStream.hpp b/Include/Aurora/Compression/ICompressionStream.hpp index 0504a1a8..d4059e4f 100644 --- a/Include/Aurora/Compression/ICompressionStream.hpp +++ b/Include/Aurora/Compression/ICompressionStream.hpp @@ -70,5 +70,17 @@ namespace Aurora::Compression /// Compression only virtual bool Finish() = 0; + + /** + * @brief + * @return + */ + virtual AuOptional GetLastError() = 0; + + /** + * @brief + * @return + */ + virtual AuOptional GetLastErrorString() = 0; }; } \ No newline at end of file diff --git a/Source/Compression/AuBaseStream.cpp b/Source/Compression/AuBaseStream.cpp index 9a653429..e6f3893d 100644 --- a/Source/Compression/AuBaseStream.cpp +++ b/Source/Compression/AuBaseStream.cpp @@ -195,6 +195,29 @@ namespace Aurora::Compression return this->pOutputBuffer_->allocSize; } + AuOptional BaseStream::GetLastError() + { + return this->optLastError_; + } + + AuOptional BaseStream::GetLastErrorString() + { + return this->optLastErrorString_; + } + + void BaseStream::SetLastError(int i, const AuString &str) + { + this->optLastError_ = i; + this->optLastErrorString_ = str; + SysPushErrorMalformedData("Compression Error: {} {}", i, str); + } + + void BaseStream::SetLastError(int i) + { + this->optLastError_ = i; + SysPushErrorMalformedData("Compression Error: {}", i); + } + bool BaseStream::Flush() { return false; diff --git a/Source/Compression/AuBaseStream.hpp b/Source/Compression/AuBaseStream.hpp index 8c2ced42..e1219d45 100644 --- a/Source/Compression/AuBaseStream.hpp +++ b/Source/Compression/AuBaseStream.hpp @@ -32,6 +32,12 @@ namespace Aurora::Compression virtual AuUInt32 GetInternalBufferSize() override; + virtual AuOptional GetLastError() override; + virtual AuOptional GetLastErrorString() override; + + void SetLastError(int i, const AuString &str); + void SetLastError(int i); + bool Write(const void *pDest, AuUInt32 dwLength); bool Write2(const void *pDest, AuUInt32 dwLength); @@ -63,6 +69,8 @@ namespace Aurora::Compression Memory::ByteBuffer _outbufferOwned; AuThreadPrimitives::SpinLock _spinlock; AuUInt32 uBufferSize_; + AuOptional optLastError_; + AuString optLastErrorString_; }; } diff --git a/Source/Compression/Compressors/BZip2Compressor.hpp b/Source/Compression/Compressors/BZip2Compressor.hpp index 0b2a1690..cc63f3d4 100644 --- a/Source/Compression/Compressors/BZip2Compressor.hpp +++ b/Source/Compression/Compressors/BZip2Compressor.hpp @@ -58,7 +58,7 @@ namespace Aurora::Compression auto ret = BZ2_bzCompressInit(&this->ctx_, meta.uCompressionLevel, 0, 0); if (ret < BZ_OK) { - SysPushErrorMem("Error: {}", BShitToString(ret)); + this->SetLastError(ret, BShitToString(ret)); return false; } this->bInit_ = true; @@ -147,7 +147,7 @@ namespace Aurora::Compression auto ret = BZ2_bzCompress(&this->ctx_, type); if (ret < BZ_OK) { - SysPushErrorIO("Error: {}", BShitToString(ret)); + this->SetLastError(ret, BShitToString(ret)); this->pReader_.reset(); return false; } @@ -177,7 +177,7 @@ namespace Aurora::Compression auto ret = BZ2_bzCompress(&this->ctx_, type); if (ret < BZ_OK) { - SysPushErrorIO("Error: {}", BShitToString(ret)); + this->SetLastError(ret, BShitToString(ret)); this->pReader_.reset(); return false; } diff --git a/Source/Compression/Compressors/BZip2Decompressor.hpp b/Source/Compression/Compressors/BZip2Decompressor.hpp index d38f8f63..ffb105f2 100644 --- a/Source/Compression/Compressors/BZip2Decompressor.hpp +++ b/Source/Compression/Compressors/BZip2Decompressor.hpp @@ -58,7 +58,7 @@ namespace Aurora::Compression auto ret = BZ2_bzDecompressInit(&this->ctx_, 0, 0); if (ret < BZ_OK) { - SysPushErrorMem("Error: {}", BShitToString(ret)); + this->SetLastError(ret, BShitToString(ret)); return false; } @@ -97,7 +97,7 @@ namespace Aurora::Compression auto ret = BZ2_bzDecompress(&this->ctx_); if (ret < BZ_OK) { - SysPushErrorIO("Error: {}", BShitToString(ret)); + this->SetLastError(ret, BShitToString(ret)); this->pReader_.reset(); return AuMakePair(read, 0); } diff --git a/Source/Compression/Compressors/BrotliDecompressor.hpp b/Source/Compression/Compressors/BrotliDecompressor.hpp index 43e932eb..85e9eb7f 100644 --- a/Source/Compression/Compressors/BrotliDecompressor.hpp +++ b/Source/Compression/Compressors/BrotliDecompressor.hpp @@ -53,7 +53,6 @@ namespace Aurora::Compression } } - this->SetArray(this->din_); this->SetOutArray(this->dout_); return true; @@ -89,7 +88,7 @@ namespace Aurora::Compression auto ret = BrotliDecoderDecompressStream(this->pState, &this->uAvailIn, (const uint8_t **)&this->pInBuffer, &outNext, &pOut, nullptr); if (ret == BROTLI_DECODER_RESULT_ERROR) { - SysPushErrorIO("Brotli Error"); + this->SetLastError(ret, BrotliDecoderErrorString((BrotliDecoderErrorCode)ret)); this->pReader_.reset(); return AuMakePair(read, 0); } diff --git a/Source/Compression/Compressors/DeflateCompressor.hpp b/Source/Compression/Compressors/DeflateCompressor.hpp index 763decc2..7fd82263 100644 --- a/Source/Compression/Compressors/DeflateCompressor.hpp +++ b/Source/Compression/Compressors/DeflateCompressor.hpp @@ -42,7 +42,7 @@ namespace Aurora::Compression auto ret = deflateInit2(&this->ctx_, meta.uCompressionLevel, Z_DEFLATED, this->bits_, 8, Z_DEFAULT_STRATEGY); if (ret < Z_OK) { - SysPushErrorMem("Error: {}", ret); + this->SetLastError(ret, zError(ret)); return false; } this->init_ = true; @@ -84,7 +84,7 @@ namespace Aurora::Compression auto ret = deflate(&this->ctx_, Z_NO_FLUSH); if (ret < Z_OK) { - SysPushErrorIO("Error: {}", zError(ret)); + this->SetLastError(ret, zError(ret)); this->pReader_.reset(); return AuMakePair(read, 0); } @@ -135,7 +135,7 @@ namespace Aurora::Compression auto ret = deflate(&this->ctx_, Z_FULL_FLUSH); if (ret < Z_OK) { - SysPushErrorIO("Error: {}", zError(ret)); + this->SetLastError(ret, zError(ret)); this->pReader_.reset(); return false; } @@ -163,7 +163,7 @@ namespace Aurora::Compression auto ret = deflate(&this->ctx_, type); if (ret < Z_OK) { - SysPushErrorIO("Error: {}", zError(ret)); + this->SetLastError(ret, zError(ret)); this->pReader_.reset(); return false; } diff --git a/Source/Compression/Compressors/LZ4Compressor.hpp b/Source/Compression/Compressors/LZ4Compressor.hpp index c154f328..3da40e82 100644 --- a/Source/Compression/Compressors/LZ4Compressor.hpp +++ b/Source/Compression/Compressors/LZ4Compressor.hpp @@ -130,6 +130,7 @@ namespace Aurora::Compression AuUInt32 bufferedBytes = LZ4F_compressEnd(cctxPtr, startPtr, meta.uInternalStreamSize - bytesRemInFrame, &options); if (LZ4F_isError(bufferedBytes)) { + this->SetLastError(bufferedBytes); this->bDead = true; return false; } @@ -180,6 +181,7 @@ namespace Aurora::Compression if (LZ4F_isError(temp)) { SysPushErrorGeneric("LZ4 internal stream size was too small. ingested too much data. must reset stream now"); + this->SetLastError(temp); bDead = true; return {}; } diff --git a/Source/Compression/Compressors/LZ4Decompressor.hpp b/Source/Compression/Compressors/LZ4Decompressor.hpp index 6721a08f..4f4a01a8 100644 --- a/Source/Compression/Compressors/LZ4Decompressor.hpp +++ b/Source/Compression/Compressors/LZ4Decompressor.hpp @@ -95,6 +95,7 @@ namespace Aurora::Compression bytesRemInFrame = LZ4F_decompress(this->lz4Stream_, this->pBufferOut_.get(), &frameS2Ptr, this->pReadPtr_, &frameSPtr, &opts); if (LZ4F_isError(bytesRemInFrame)) { + this->SetLastError(bytesRemInFrame); return {}; } diff --git a/Source/Compression/Compressors/LZMACompressor.hpp b/Source/Compression/Compressors/LZMACompressor.hpp index 36464b1a..8e5d2cd9 100644 --- a/Source/Compression/Compressors/LZMACompressor.hpp +++ b/Source/Compression/Compressors/LZMACompressor.hpp @@ -74,7 +74,7 @@ namespace Aurora::Compression if (ret != LZMA_OK) { - SysPushErrorGeneric("LZMA Coder failure: {}", (int)ret); + this->SetLastError(ret); AuResetMember(this->pReader_); return false; } @@ -133,7 +133,7 @@ namespace Aurora::Compression if (ret != LZMA_OK && ret != LZMA_STREAM_END) { - SysPushErrorIO("Error: {}", int(ret)); + this->SetLastError(ret); this->pReader_.reset(); return AuMakePair(read, 0); } @@ -142,7 +142,7 @@ namespace Aurora::Compression done += have; if (!Write2(reinterpret_cast(pMainDOut), - have)) + have)) { this->pReader_.reset(); SysPushErrorIO("Compression Out of Overhead"); diff --git a/Source/Compression/Compressors/LZMADecompressor.hpp b/Source/Compression/Compressors/LZMADecompressor.hpp index 1f791dfa..3b24efff 100644 --- a/Source/Compression/Compressors/LZMADecompressor.hpp +++ b/Source/Compression/Compressors/LZMADecompressor.hpp @@ -60,7 +60,7 @@ namespace Aurora::Compression if (ret != LZMA_OK) { - SysPushErrorGeneric("LZMA Decoder failure: {}", (int)ret); + this->SetLastError(ret); AuResetMember(this->pReader_); return false; } @@ -71,7 +71,7 @@ namespace Aurora::Compression if (ret != LZMA_OK) { - SysPushErrorGeneric("LZMA Decoder failure: {}", (int)ret); + this->SetLastError(ret); AuResetMember(this->pReader_); return false; } @@ -119,7 +119,7 @@ namespace Aurora::Compression if (ret != LZMA_OK && ret != LZMA_STREAM_END) { - SysPushErrorIO("Error: {}", int(ret)); + this->SetLastError(ret); this->pReader_.reset(); return AuMakePair(read, 0); } diff --git a/Source/Compression/Compressors/ZSTDCompressor.hpp b/Source/Compression/Compressors/ZSTDCompressor.hpp index 8af66049..af64e51e 100644 --- a/Source/Compression/Compressors/ZSTDCompressor.hpp +++ b/Source/Compression/Compressors/ZSTDCompressor.hpp @@ -49,6 +49,7 @@ namespace Aurora::Compression if (ZSTD_isError(uRet)) { SysPushErrorArg("Invalid compression level"); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); return false; } @@ -56,6 +57,7 @@ namespace Aurora::Compression if (ZSTD_isError(uRet)) { SysPushErrorArg("Invalid option"); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); return false; } @@ -65,6 +67,7 @@ namespace Aurora::Compression if (ZSTD_isError(uRet)) { SysPushErrorArg("ZSTD_c_strategy"); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); return false; } } @@ -79,7 +82,7 @@ namespace Aurora::Compression uRet = ZSTD_CCtx_setParameter(this->cctx_, ZSTD_c_nbWorkers, AuMax(meta.uThreads, AuUInt8(1u))); if (ZSTD_isError(uRet)) { - SysPushErrorArg(); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); return false; } } @@ -90,6 +93,7 @@ namespace Aurora::Compression if (ZSTD_isError(uRet)) { SysPushErrorArg("Compressor argument assignment {} = {}", a, b); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); return false; } } @@ -139,7 +143,7 @@ namespace Aurora::Compression uRet = ZSTD_compressStream2(this->cctx_, &output, &this->input_, mode); if (ZSTD_isError(uRet)) { - SysPushErrorIO("Compression error: {}", ZSTD_getErrorName(uRet)); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); this->uAvailableIn_ -= AuUInt32(output.pos); this->pReader_.reset(); return AuMakePair(read, 0); @@ -192,7 +196,7 @@ namespace Aurora::Compression uRet = ZSTD_compressStream2(this->cctx_, &output, &this->input_, ZSTD_e_flush); if (ZSTD_isError(uRet)) { - SysPushErrorIO("Compression error: {}", ZSTD_getErrorName(uRet)); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); this->uAvailableIn_ -= AuUInt32(output.pos); return {}; } @@ -240,12 +244,12 @@ namespace Aurora::Compression uRet = ZSTD_endStream(this->cctx_, &output); if (ZSTD_isError(uRet)) { - SysPushErrorIO("Compression error: {}", ZSTD_getErrorName(uRet)); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); return {}; } if (!Write(reinterpret_cast(this->dout_), - AuUInt32(output.pos))) + AuUInt32(output.pos))) { SysPushErrorIO("Compression Out of Overhead"); return false; diff --git a/Source/Compression/Compressors/ZSTDDecompressor.hpp b/Source/Compression/Compressors/ZSTDDecompressor.hpp index 72e4353a..67a9f9b1 100644 --- a/Source/Compression/Compressors/ZSTDDecompressor.hpp +++ b/Source/Compression/Compressors/ZSTDDecompressor.hpp @@ -49,6 +49,7 @@ namespace Aurora::Compression if (ZSTD_isError(uRet)) { SysPushErrorArg("Decompressor argument assignment {} = {}", a, b); + this->SetLastError(uRet, ZSTD_getErrorName(uRet)); return false; } } @@ -90,7 +91,7 @@ namespace Aurora::Compression auto ret = ZSTD_decompressStream(this->dctx_, &output, &this->input_); if (ZSTD_isError(ret)) { - SysPushErrorIO("Decompression error: {}", ZSTD_getErrorName(ret)); + this->SetLastError(ret, ZSTD_getErrorName(ret)); this->pReader_.reset(); return AuMakePair(read, 0); }