Пример #1
0
        /// <summary>
        /// If this stream has some input leftover that hasn't been processed then we should
        /// check if it is another GZip file concatenated with this one.
        ///
        /// Returns false if the leftover input is another GZip data stream.
        /// </summary>
        private unsafe bool ResetStreamForLeftoverInput()
        {
            Debug.Assert(!NeedsInput());
            Debug.Assert(IsGzipStream());
            Debug.Assert(IsInputBufferHandleAllocated);

            lock (SyncLock)
            {
                IntPtr nextInPtr     = _zlibStream.NextIn;
                byte * nextInPointer = (byte *)nextInPtr.ToPointer();
                uint   nextAvailIn   = _zlibStream.AvailIn;

                // Check the leftover bytes to see if they start with he gzip header ID bytes
                if (*nextInPointer != ZLibNative.GZip_Header_ID1 || (nextAvailIn > 1 && *(nextInPointer + 1) != ZLibNative.GZip_Header_ID2))
                {
                    return(true);
                }

                // Trash our existing zstream.
                _zlibStream.Dispose();

                // Create a new zstream
                InflateInit(_windowBits);

                // SetInput on the new stream to the bits remaining from the last stream
                _zlibStream.NextIn  = nextInPtr;
                _zlibStream.AvailIn = nextAvailIn;
                _finished           = false;
            }

            return(false);
        }
Пример #2
0
        private void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    _zlibStream.Dispose();
                }

                _isDisposed = true;
            }
        }
Пример #3
0
        private void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    _zlibStream.Dispose();
                }

                DeallocateInputBufferHandle();
                _isDisposed = true;
            }
        }
Пример #4
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !_isDisposed)
            {
                if (_inputBufferHandle.HasValue)
                {
                    DeallocateInputBufferHandle();
                }

                _zlibStream.Dispose();
                _isDisposed = true;
            }
        }
Пример #5
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    _zlibStream.Dispose();
                }

                if (_inputBufferHandle.IsAllocated)
                {
                    DeallocateInputBufferHandle();
                }
                _isDisposed = true;
            }
        }
Пример #6
0
        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());
            }
        }