// Copy the compressed bytes to output buffer as a block. maxBytesToCopy limits the number of 
        // bytes we can copy from input. Set to any value < 1 if no limit
        public void GetBlock(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
        {
            Debug.Assert(InputAvailable(input), "call SetInput before trying to compress!");

            WriteDeflatePreamble(output);
            GetCompressedOutput(input, output, maxBytesToCopy);
            WriteEndOfBlock(output);
        }
Пример #2
0
        // Copy the compressed bytes to output buffer as a block. maxBytesToCopy limits the number of
        // bytes we can copy from input. Set to any value < 1 if no limit
        public void GetBlock(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
        {
            Debug.Assert(InputAvailable(input), "call SetInput before trying to compress!");

            WriteDeflatePreamble(output);
            GetCompressedOutput(input, output, maxBytesToCopy);
            WriteEndOfBlock(output);
        }
Пример #3
0
        internal Deflater()
        {
            _deflateEncoder = new FastEncoder();
            _copyEncoder    = new CopyEncoder();
            _input          = new DeflateInput();
            _output         = new OutputBuffer();

            _processingState = DeflaterState.NotStarted;
        }
Пример #4
0
        internal Deflater()
        {
            _deflateEncoder = new FastEncoder();
            _copyEncoder = new CopyEncoder();
            _input = new DeflateInput();
            _output = new OutputBuffer();

            _processingState = DeflaterState.NotStarted;
        }
Пример #5
0
        // null input means write an empty payload with formatting info. This is needed for the final block.
        public void GetBlock(DeflateInput input, OutputBuffer output, bool isFinal)
        {
            Debug.Assert(output != null);
            Debug.Assert(output.FreeBytes >= PaddingSize);

            // determine number of bytes to write
            var count = 0;

            if (input != null)
            {
                // allow space for padding and bits not yet flushed to buffer
                count = Math.Min(input.Count, output.FreeBytes - PaddingSize - output.BitsInBuffer);

                // we don't expect the output buffer to ever be this big (currently 4K), but we'll check this
                // just in case that changes.
                if (count > MaxUncompressedBlockSize - PaddingSize)
                {
                    count = MaxUncompressedBlockSize - PaddingSize;
                }
            }

            // write header and flush bits
            if (isFinal)
            {
                output.WriteBits(FastEncoderStatics.BFinalNoCompressionHeaderBitCount, FastEncoderStatics.BFinalNoCompressionHeader);
            }
            else
            {
                output.WriteBits(FastEncoderStatics.NoCompressionHeaderBitCount, FastEncoderStatics.NoCompressionHeader);
            }

            // now we're aligned
            output.FlushBits();

            // write len, nlen
            WriteLenNLen((ushort)count, output);
            if (input == null || count <= 0)
            {
                return;
            }

            // write uncompressed bytes
            output.WriteBytes(input.Buffer, input.StartIndex, count);
            input.ConsumeBytes(count);
        }
        // null input means write an empty payload with formatting info. This is needed for the final block.
        public void GetBlock(DeflateInput input, OutputBuffer output, bool isFinal)
        {
            Debug.Assert(output != null);
            Debug.Assert(output.FreeBytes >= PaddingSize);

            // determine number of bytes to write
            var count = 0;
            if (input != null)
            {

                // allow space for padding and bits not yet flushed to buffer
                count = Math.Min(input.Count, output.FreeBytes - PaddingSize - output.BitsInBuffer);

                // we don't expect the output buffer to ever be this big (currently 4K), but we'll check this
                // just in case that changes.
                if (count > MaxUncompressedBlockSize - PaddingSize)
                {
                    count = MaxUncompressedBlockSize - PaddingSize;
                }
            }

            // write header and flush bits
            if (isFinal)
            {
                output.WriteBits(FastEncoderStatics.BFinalNoCompressionHeaderBitCount, FastEncoderStatics.BFinalNoCompressionHeader);
            }
            else
            {
                output.WriteBits(FastEncoderStatics.NoCompressionHeaderBitCount, FastEncoderStatics.NoCompressionHeader);
            }

            // now we're aligned
            output.FlushBits();

            // write len, nlen
            WriteLenNLen((ushort) count, output);
            if (input == null || count <= 0) 
                return;

            // write uncompressed bytes            
            output.WriteBytes(input.Buffer, input.StartIndex, count);
            input.ConsumeBytes(count);
        }
Пример #7
0
        // maxBytesToCopy limits the number of bytes we can copy from input. Set to any value < 1 if no limit
        private void GetCompressedOutput(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
        {
            // snapshot for compression ratio stats
            var bytesWrittenPre        = output.BytesWritten;
            var bytesConsumedFromInput = 0;
            var inputBytesPre          = BytesInHistory + input.Count;

            do
            {
                // read more input data into the window if there is space available
                var bytesToCopy = (input.Count < _inputWindow.FreeWindowSpace)
                    ? input.Count
                    : _inputWindow.FreeWindowSpace;
                if (maxBytesToCopy >= 1)
                {
                    bytesToCopy = Math.Min(bytesToCopy, maxBytesToCopy - bytesConsumedFromInput);
                }
                if (bytesToCopy > 0)
                {
                    // copy data into history window
                    _inputWindow.CopyBytes(input.Buffer, input.StartIndex, bytesToCopy);
                    input.ConsumeBytes(bytesToCopy);
                    bytesConsumedFromInput += bytesToCopy;
                }

                this.GetCompressedOutput(output);
            }while (SafeToWriteTo(output) && InputAvailable(input) && (maxBytesToCopy < 1 || bytesConsumedFromInput < maxBytesToCopy));

            // determine compression ratio, save
            var bytesWrittenPost   = output.BytesWritten;
            var bytesWritten       = bytesWrittenPost - bytesWrittenPre;
            var inputBytesPost     = BytesInHistory + input.Count;
            var totalBytesConsumed = inputBytesPre - inputBytesPost;

            if (bytesWritten != 0)
            {
                this.LastCompressionRatio = bytesWritten / (double)totalBytesConsumed;
            }
        }
        // maxBytesToCopy limits the number of bytes we can copy from input. Set to any value < 1 if no limit
        private void GetCompressedOutput(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
        {
            // snapshot for compression ratio stats
            var bytesWrittenPre = output.BytesWritten;
            var bytesConsumedFromInput = 0;
            var inputBytesPre = BytesInHistory + input.Count;

            do
            {
                // read more input data into the window if there is space available
                var bytesToCopy = (input.Count < _inputWindow.FreeWindowSpace)
                    ? input.Count
                    : _inputWindow.FreeWindowSpace;
                if (maxBytesToCopy >= 1)
                {
                    bytesToCopy = Math.Min(bytesToCopy, maxBytesToCopy - bytesConsumedFromInput);
                }
                if (bytesToCopy > 0)
                {
                    // copy data into history window
                    _inputWindow.CopyBytes(input.Buffer, input.StartIndex, bytesToCopy);
                    input.ConsumeBytes(bytesToCopy);
                    bytesConsumedFromInput += bytesToCopy;
                }

                this.GetCompressedOutput(output);

            } 
            while (SafeToWriteTo(output) && InputAvailable(input) && (maxBytesToCopy < 1 || bytesConsumedFromInput < maxBytesToCopy));

            // determine compression ratio, save
            var bytesWrittenPost = output.BytesWritten;
            var bytesWritten = bytesWrittenPost - bytesWrittenPre;
            var inputBytesPost = BytesInHistory + input.Count;
            var totalBytesConsumed = inputBytesPre - inputBytesPost;
            if (bytesWritten != 0)
                this.LastCompressionRatio = bytesWritten / (double)totalBytesConsumed;            

        }
Пример #9
0
 // Compress data but don't format as block (doesn't have header and footer)
 public void GetCompressedData(DeflateInput input, OutputBuffer output)
 {
     this.GetCompressedOutput(input, output, -1);
 }
Пример #10
0
 private bool InputAvailable(DeflateInput input)
 {
     return(input.Count > 0 || BytesInHistory > 0);
 }
Пример #11
0
        public int GetDeflateOutput(byte[] outputBuffer)
        {
            Debug.Assert(outputBuffer != null, "Can't pass in a null output buffer!");
            Debug.Assert(!NeedsInput(), "GetDeflateOutput should only be called after providing input");

            _output.UpdateBuffer(outputBuffer);

            switch (_processingState)
            {
            case DeflaterState.NotStarted:
            {
                // first call. Try to compress but if we get bad compression ratio, switch to uncompressed blocks.
                Debug.Assert(_deflateEncoder.BytesInHistory == 0, "have leftover bytes in window");

                // save these in case we need to switch to uncompressed format
                var initialInputState  = _input.DumpState();
                var initialOutputState = _output.DumpState();

                _deflateEncoder.GetBlockHeader(_output);
                _deflateEncoder.GetCompressedData(_input, _output);

                if (!UseCompressed(_deflateEncoder.LastCompressionRatio))
                {
                    // we're expanding; restore state and switch to uncompressed
                    _input.RestoreState(initialInputState);
                    _output.RestoreState(initialOutputState);
                    _copyEncoder.GetBlock(_input, _output, false);
                    this.FlushInputWindows();
                    _processingState = DeflaterState.CheckingForIncompressible;
                }
                else
                {
                    _processingState = DeflaterState.CompressThenCheck;
                }

                break;
            }

            case DeflaterState.CompressThenCheck:
            {
                // continue assuming data is compressible. If we reach data that indicates otherwise
                // finish off remaining data in history and decide whether to compress on a
                // block-by-block basis
                _deflateEncoder.GetCompressedData(_input, _output);

                if (!UseCompressed(_deflateEncoder.LastCompressionRatio))
                {
                    _processingState  = DeflaterState.SlowDownForIncompressible1;
                    _inputFromHistory = _deflateEncoder.UnprocessedInput;
                }
                break;
            }

            case DeflaterState.SlowDownForIncompressible1:
            {
                // finish off previous compressed block
                _deflateEncoder.GetBlockFooter(_output);

                _processingState = DeflaterState.SlowDownForIncompressible2;
                goto case DeflaterState.SlowDownForIncompressible2;         // yeah I know, but there's no fallthrough
            }

            case DeflaterState.SlowDownForIncompressible2:
            {
                // clear out data from history, but add them as uncompressed blocks
                if (_inputFromHistory.Count > 0)
                {
                    _copyEncoder.GetBlock(_inputFromHistory, _output, false);
                }

                if (_inputFromHistory.Count == 0)
                {
                    // now we're clean
                    _deflateEncoder.FlushInput();
                    _processingState = DeflaterState.CheckingForIncompressible;
                }
                break;
            }

            case DeflaterState.CheckingForIncompressible:
            {
                // decide whether to compress on a block-by-block basis
                Debug.Assert(_deflateEncoder.BytesInHistory == 0, "have leftover bytes in window");

                // save these in case we need to store as uncompressed
                var initialInputState  = _input.DumpState();
                var initialOutputState = _output.DumpState();

                // enforce max so we can ensure state between calls
                _deflateEncoder.GetBlock(_input, _output, CleanCopySize);

                if (!UseCompressed(_deflateEncoder.LastCompressionRatio))
                {
                    // we're expanding; restore state and switch to uncompressed
                    _input.RestoreState(initialInputState);
                    _output.RestoreState(initialOutputState);
                    _copyEncoder.GetBlock(_input, _output, false);
                    this.FlushInputWindows();
                }

                break;
            }

            case DeflaterState.StartingSmallData:
            {
                // add compressed header and data, but not footer. Subsequent calls will keep
                // adding compressed data (no header and no footer). We're doing this to
                // avoid overhead of header and footer size relative to compressed payload.
                _deflateEncoder.GetBlockHeader(_output);

                _processingState = DeflaterState.HandlingSmallData;
                goto case DeflaterState.HandlingSmallData;         // yeah I know, but there's no fallthrough
            }

            case DeflaterState.HandlingSmallData:
            {
                // continue adding compressed data
                _deflateEncoder.GetCompressedData(_input, _output);
                break;
            }
            }

            return(_output.BytesWritten);
        }
 // Compress data but don't format as block (doesn't have header and footer)
 public void GetCompressedData(DeflateInput input, OutputBuffer output)
 {
     this.GetCompressedOutput(input, output, -1);
 }
 private bool InputAvailable(DeflateInput input)
 {
     return input.Count > 0 || BytesInHistory > 0;
 }
Пример #14
0
        public int GetDeflateOutput(byte[] outputBuffer)
        {
            Debug.Assert(outputBuffer != null, "Can't pass in a null output buffer!");
            Debug.Assert(!NeedsInput(), "GetDeflateOutput should only be called after providing input");

            _output.UpdateBuffer(outputBuffer);

            switch (_processingState)
            {
                case DeflaterState.NotStarted:
                    {
                        // first call. Try to compress but if we get bad compression ratio, switch to uncompressed blocks. 
                        Debug.Assert(_deflateEncoder.BytesInHistory == 0, "have leftover bytes in window");

                        // save these in case we need to switch to uncompressed format
                        var initialInputState = _input.DumpState();
                        var initialOutputState = _output.DumpState();

                        _deflateEncoder.GetBlockHeader(_output);
                        _deflateEncoder.GetCompressedData(_input, _output);

                        if (!UseCompressed(_deflateEncoder.LastCompressionRatio))
                        {
                            // we're expanding; restore state and switch to uncompressed
                            _input.RestoreState(initialInputState);
                            _output.RestoreState(initialOutputState);
                            _copyEncoder.GetBlock(_input, _output, false);
                            this.FlushInputWindows();
                            _processingState = DeflaterState.CheckingForIncompressible;
                        }
                        else
                        {
                            _processingState = DeflaterState.CompressThenCheck;
                        }

                        break;
                    }
                case DeflaterState.CompressThenCheck:
                    {
                        // continue assuming data is compressible. If we reach data that indicates otherwise
                        // finish off remaining data in history and decide whether to compress on a 
                        // block-by-block basis
                        _deflateEncoder.GetCompressedData(_input, _output);

                        if (!UseCompressed(_deflateEncoder.LastCompressionRatio))
                        {
                            _processingState = DeflaterState.SlowDownForIncompressible1;
                            _inputFromHistory = _deflateEncoder.UnprocessedInput;
                        }
                        break;
                    }
                case DeflaterState.SlowDownForIncompressible1:
                    {
                        // finish off previous compressed block
                        _deflateEncoder.GetBlockFooter(_output);

                        _processingState = DeflaterState.SlowDownForIncompressible2;
                        goto case DeflaterState.SlowDownForIncompressible2; // yeah I know, but there's no fallthrough
                    }

                case DeflaterState.SlowDownForIncompressible2:
                    {
                        // clear out data from history, but add them as uncompressed blocks
                        if (_inputFromHistory.Count > 0)
                        {
                            _copyEncoder.GetBlock(_inputFromHistory, _output, false);
                        }

                        if (_inputFromHistory.Count == 0)
                        {
                            // now we're clean
                            _deflateEncoder.FlushInput();
                            _processingState = DeflaterState.CheckingForIncompressible;
                        }
                        break;
                    }

                case DeflaterState.CheckingForIncompressible:
                    {
                        // decide whether to compress on a block-by-block basis
                        Debug.Assert(_deflateEncoder.BytesInHistory == 0, "have leftover bytes in window");

                        // save these in case we need to store as uncompressed
                        var initialInputState = _input.DumpState();
                        var initialOutputState = _output.DumpState();

                        // enforce max so we can ensure state between calls
                        _deflateEncoder.GetBlock(_input, _output, CleanCopySize);

                        if (!UseCompressed(_deflateEncoder.LastCompressionRatio))
                        {
                            // we're expanding; restore state and switch to uncompressed
                            _input.RestoreState(initialInputState);
                            _output.RestoreState(initialOutputState);
                            _copyEncoder.GetBlock(_input, _output, false);
                            this.FlushInputWindows();
                        }

                        break;
                    }

                case DeflaterState.StartingSmallData:
                    {
                        // add compressed header and data, but not footer. Subsequent calls will keep 
                        // adding compressed data (no header and no footer). We're doing this to 
                        // avoid overhead of header and footer size relative to compressed payload.
                        _deflateEncoder.GetBlockHeader(_output);

                        _processingState = DeflaterState.HandlingSmallData;
                        goto case DeflaterState.HandlingSmallData; // yeah I know, but there's no fallthrough
                    }

                case DeflaterState.HandlingSmallData:
                    {
                        // continue adding compressed data
                        _deflateEncoder.GetCompressedData(_input, _output);
                        break;
                    }
            }

            return _output.BytesWritten;
        }