private void DeflateInit(ZLibNative.CompressionLevel compressionLevel, int windowBits, int memLevel, ZLibNative.CompressionStrategy strategy) { ZErrorCode errC; try { errC = ZLibNative.CreateZLibStreamForDeflate(out _zlibStream, compressionLevel, windowBits, memLevel, strategy); } catch (Exception cause) { throw new ZLibException(SR.ZLibErrorDLLLoadError, cause); } switch (errC) { case ZErrorCode.Ok: return; case ZErrorCode.MemError: throw new ZLibException(SR.ZLibErrorNotEnoughMemory, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); case ZErrorCode.VersionError: throw new ZLibException(SR.ZLibErrorVersionMismatch, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); case ZErrorCode.StreamError: throw new ZLibException(SR.ZLibErrorIncorrectInitParameters, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); default: throw new ZLibException(SR.ZLibErrorUnexpected, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); } }
private ZErrorCode Deflate(ZFlushCode flushCode) { ZErrorCode errC; try { errC = _zlibStream.Deflate(flushCode); } catch (Exception cause) { throw new ZLibException(SR.ZLibErrorDLLLoadError, cause); } switch (errC) { case ZErrorCode.Ok: case ZErrorCode.StreamEnd: return(errC); case ZErrorCode.BufError: return(errC); // This is a recoverable error case ZErrorCode.StreamError: throw new ZLibException(SR.ZLibErrorInconsistentStream, "deflate", (int)errC, _zlibStream.GetErrorMessage()); default: throw new ZLibException(SR.ZLibErrorUnexpected, "deflate", (int)errC, _zlibStream.GetErrorMessage()); } }
internal Deflater(CompressionLevel compressionLevel, int windowBits) { Debug.Assert(windowBits >= minWindowBits && windowBits <= maxWindowBits); ZLibNative.CompressionLevel zlibCompressionLevel; int memLevel; switch (compressionLevel) { // See the note in ZLibNative.CompressionLevel for the recommended combinations. case CompressionLevel.Optimal: zlibCompressionLevel = ZLibNative.CompressionLevel.DefaultCompression; memLevel = ZLibNative.Deflate_DefaultMemLevel; break; case CompressionLevel.Fastest: zlibCompressionLevel = ZLibNative.CompressionLevel.BestSpeed; memLevel = ZLibNative.Deflate_DefaultMemLevel; break; case CompressionLevel.NoCompression: zlibCompressionLevel = ZLibNative.CompressionLevel.NoCompression; memLevel = ZLibNative.Deflate_NoCompressionMemLevel; break; case CompressionLevel.SmallestSize: zlibCompressionLevel = ZLibNative.CompressionLevel.BestCompression; memLevel = ZLibNative.Deflate_DefaultMemLevel; break; default: throw new ArgumentOutOfRangeException(nameof(compressionLevel)); } ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy; ZLibNative.ZLibStreamHandle?zlibStream = null; ZErrorCode errC; try { errC = ZLibNative.CreateZLibStreamForDeflate(out zlibStream, zlibCompressionLevel, windowBits, memLevel, strategy); } catch (Exception cause) { zlibStream?.Dispose(); throw new ZLibException(SR.ZLibErrorDLLLoadError, cause); } _zlibStream = zlibStream; switch (errC) { case ZErrorCode.Ok: return; case ZErrorCode.MemError: throw new ZLibException(SR.ZLibErrorNotEnoughMemory, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); case ZErrorCode.VersionError: throw new ZLibException(SR.ZLibErrorVersionMismatch, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); case ZErrorCode.StreamError: throw new ZLibException(SR.ZLibErrorIncorrectInitParameters, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); default: throw new ZLibException(SR.ZLibErrorUnexpected, "deflateInit2_", (int)errC, _zlibStream.GetErrorMessage()); } }
private void InflateInit(int windowBits) { ZLibNative.ErrorCode error; try { error = ZLibNative.CreateZLibStreamForInflate(out _zlibStream, windowBits); } catch (Exception exception) // could not load the ZLib dll { throw new ZLibException(SR.ZLibErrorDLLLoadError, exception); } switch (error) { case ZLibNative.ErrorCode.Ok: // Successful initialization return; case ZLibNative.ErrorCode.MemError: // Not enough memory throw new ZLibException(SR.ZLibErrorNotEnoughMemory, "inflateInit2_", (int)error, _zlibStream.GetErrorMessage()); case ZLibNative.ErrorCode.VersionError: //zlib library is incompatible with the version assumed throw new ZLibException(SR.ZLibErrorVersionMismatch, "inflateInit2_", (int)error, _zlibStream.GetErrorMessage()); case ZLibNative.ErrorCode.StreamError: // Parameters are invalid throw new ZLibException(SR.ZLibErrorIncorrectInitParameters, "inflateInit2_", (int)error, _zlibStream.GetErrorMessage()); default: throw new ZLibException(SR.ZLibErrorUnexpected, "inflateInit2_", (int)error, _zlibStream.GetErrorMessage()); } }