Beispiel #1
0
        public void GetBlock(DeflateInput input, OutputBuffer output, bool isFinal)
        {
            Debug.Assert(output != null);
            Debug.Assert(output.FreeBytes >= 5);
            int num = 0;

            if (input != null)
            {
                num = Math.Min(input.Count, output.FreeBytes - 5 - output.BitsInBuffer);
                if (num > 65531)
                {
                    num = 65531;
                }
            }
            if (isFinal)
            {
                output.WriteBits(3, 1U);
            }
            else
            {
                output.WriteBits(3, 0U);
            }
            output.FlushBits();
            this.WriteLenNLen((ushort)num, output);
            if (input == null || num <= 0)
            {
                return;
            }
            output.WriteBytes(input.Buffer, input.StartIndex, num);
            input.ConsumeBytes(num);
        }
Beispiel #2
0
        private void GetCompressedOutput(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
        {
            int bytesWritten = output.BytesWritten;
            int num1         = 0;
            int num2         = this.BytesInHistory + input.Count;

            do
            {
                int num3 = input.Count < this.inputWindow.FreeWindowSpace ? input.Count : this.inputWindow.FreeWindowSpace;
                if (maxBytesToCopy >= 1)
                {
                    num3 = Math.Min(num3, maxBytesToCopy - num1);
                }
                if (num3 > 0)
                {
                    this.inputWindow.CopyBytes(input.Buffer, input.StartIndex, num3);
                    input.ConsumeBytes(num3);
                    num1 += num3;
                }
                this.GetCompressedOutput(output);
            }while (this.SafeToWriteTo(output) && this.InputAvailable(input) && (maxBytesToCopy < 1 || num1 < maxBytesToCopy));
            int num4 = output.BytesWritten - bytesWritten;
            int num5 = this.BytesInHistory + input.Count;
            int num6 = num2 - num5;

            if ((uint)num4 <= 0U)
            {
                return;
            }
            this.lastCompressionRatio = (double)num4 / (double)num6;
        }
Beispiel #3
0
 internal void GetBlock(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
 {
     Debug.Assert(this.InputAvailable(input), "call SetInput before trying to compress!");
     FastEncoder.WriteDeflatePreamble(output);
     this.GetCompressedOutput(input, output, maxBytesToCopy);
     this.WriteEndOfBlock(output);
 }
Beispiel #4
0
 internal DeflaterManaged()
 {
     this.deflateEncoder  = new FastEncoder();
     this.copyEncoder     = new CopyEncoder();
     this.input           = new DeflateInput();
     this.output          = new OutputBuffer();
     this.processingState = DeflaterManaged.DeflaterState.NotStarted;
 }
Beispiel #5
0
        int IDeflater.GetDeflateOutput(byte[] outputBuffer)
        {
            Debug.Assert(outputBuffer != null, "Can't pass in a null output buffer!");
            Debug.Assert(!this.NeedsInput(), "GetDeflateOutput should only be called after providing input");
            this.output.UpdateBuffer(outputBuffer);
            switch (this.processingState)
            {
            case DeflaterManaged.DeflaterState.NotStarted:
                Debug.Assert(this.deflateEncoder.BytesInHistory == 0, "have leftover bytes in window");
                DeflateInput.InputState  state1 = this.input.DumpState();
                OutputBuffer.BufferState state2 = this.output.DumpState();
                this.deflateEncoder.GetBlockHeader(this.output);
                this.deflateEncoder.GetCompressedData(this.input, this.output);
                if (!this.UseCompressed(this.deflateEncoder.LastCompressionRatio))
                {
                    this.input.RestoreState(state1);
                    this.output.RestoreState(state2);
                    this.copyEncoder.GetBlock(this.input, this.output, false);
                    this.FlushInputWindows();
                    this.processingState = DeflaterManaged.DeflaterState.CheckingForIncompressible;
                    break;
                }
                this.processingState = DeflaterManaged.DeflaterState.CompressThenCheck;
                break;

            case DeflaterManaged.DeflaterState.SlowDownForIncompressible1:
                this.deflateEncoder.GetBlockFooter(this.output);
                this.processingState = DeflaterManaged.DeflaterState.SlowDownForIncompressible2;
                goto case DeflaterManaged.DeflaterState.SlowDownForIncompressible2;

            case DeflaterManaged.DeflaterState.SlowDownForIncompressible2:
                if (this.inputFromHistory.Count > 0)
                {
                    this.copyEncoder.GetBlock(this.inputFromHistory, this.output, false);
                }
                if (this.inputFromHistory.Count == 0)
                {
                    this.deflateEncoder.FlushInput();
                    this.processingState = DeflaterManaged.DeflaterState.CheckingForIncompressible;
                    break;
                }
                break;

            case DeflaterManaged.DeflaterState.StartingSmallData:
                this.deflateEncoder.GetBlockHeader(this.output);
                this.processingState = DeflaterManaged.DeflaterState.HandlingSmallData;
                goto case DeflaterManaged.DeflaterState.HandlingSmallData;

            case DeflaterManaged.DeflaterState.CompressThenCheck:
                this.deflateEncoder.GetCompressedData(this.input, this.output);
                if (!this.UseCompressed(this.deflateEncoder.LastCompressionRatio))
                {
                    this.processingState  = DeflaterManaged.DeflaterState.SlowDownForIncompressible1;
                    this.inputFromHistory = this.deflateEncoder.UnprocessedInput;
                    break;
                }
                break;

            case DeflaterManaged.DeflaterState.CheckingForIncompressible:
                Debug.Assert(this.deflateEncoder.BytesInHistory == 0, "have leftover bytes in window");
                DeflateInput.InputState  state3 = this.input.DumpState();
                OutputBuffer.BufferState state4 = this.output.DumpState();
                this.deflateEncoder.GetBlock(this.input, this.output, 8072);
                if (!this.UseCompressed(this.deflateEncoder.LastCompressionRatio))
                {
                    this.input.RestoreState(state3);
                    this.output.RestoreState(state4);
                    this.copyEncoder.GetBlock(this.input, this.output, false);
                    this.FlushInputWindows();
                    break;
                }
                break;

            case DeflaterManaged.DeflaterState.HandlingSmallData:
                this.deflateEncoder.GetCompressedData(this.input, this.output);
                break;
            }
            return(this.output.BytesWritten);
        }
Beispiel #6
0
 internal void GetCompressedData(DeflateInput input, OutputBuffer output)
 {
     this.GetCompressedOutput(input, output, -1);
 }
Beispiel #7
0
 private bool InputAvailable(DeflateInput input)
 {
     return(input.Count > 0 || this.BytesInHistory > 0);
 }