Exemple #1
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
            int bytesWrittenPre = output.BytesWritten;
            int bytesConsumedFromInput = 0;
            int inputBytesPre = BytesInHistory + input.Count;

            do {
                // read more input data into the window if there is space available
                int 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;
                }

                GetCompressedOutput(output);

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

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

        }
Exemple #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
        internal 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);
        }
Exemple #3
0
        private void WriteLenNLen(UInt16 len, OutputBuffer output)
        {
            output.WriteUInt16(len);
            UInt16 value = (UInt16)(~len);

            output.WriteUInt16(value);
        }
        private void WriteLenNLen(ushort len, OutputBuffer output)
        {
            output.WriteUInt16(len);
            ushort value = ~len;

            output.WriteUInt16(value);
        }
        public void GetBlock(DeflateInput input, OutputBuffer output, bool isFinal)
        {
            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)
            {
                output.WriteBytes(input.Buffer, input.StartIndex, num);
                input.ConsumeBytes(num);
            }
        }
Exemple #6
0
        private void WriteEndOfBlock(OutputBuffer output)
        {
            uint num = FastEncoderStatics.FastEncoderLiteralCodeInfo[256];
            int  n   = (int)(num & 31u);

            output.WriteBits(n, num >> 5);
        }
Exemple #7
0
        internal static void WriteMatch(int matchLen, int matchPos, OutputBuffer output)
        {
            Debug.Assert(matchLen >= FastEncoderWindow.MinMatch && matchLen <= FastEncoderWindow.MaxMatch,
                         "Illegal currentMatch length!");

            // Get the code information for a match code
            var codeInfo =
                FastEncoderStatics.FastEncoderLiteralCodeInfo[
                    FastEncoderStatics.NumChars + 1 - FastEncoderWindow.MinMatch + matchLen];
            var codeLen = (int)codeInfo & 31;

            Debug.Assert(codeLen != 0, "Invalid Match Length!");
            if (codeLen <= 16)
            {
                output.WriteBits(codeLen, codeInfo >> 5);
            }
            else
            {
                output.WriteBits(16, (codeInfo >> 5) & 65535);
                output.WriteBits(codeLen - 16, codeInfo >> (5 + 16));
            }

            // Get the code information for a distance code
            codeInfo = FastEncoderStatics.FastEncoderDistanceCodeInfo[FastEncoderStatics.GetSlot(matchPos)];
            output.WriteBits((int)(codeInfo & 15), codeInfo >> 8);
            var extraBits = (int)(codeInfo >> 4) & 15;

            if (extraBits != 0)
            {
                output.WriteBits(extraBits, (uint)matchPos & FastEncoderStatics.BitMask[extraBits]);
            }
        }
Exemple #8
0
        private void GetCompressedOutput(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
        {
            int bytesWritten = output.BytesWritten;
            int num          = 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 - num);
                }
                if (num3 > 0)
                {
                    this.inputWindow.CopyBytes(input.Buffer, input.StartIndex, num3);
                    input.ConsumeBytes(num3);
                    num += num3;
                }
                this.GetCompressedOutput(output);
            }while (this.SafeToWriteTo(output) && this.InputAvailable(input) && (maxBytesToCopy < 1 || num < maxBytesToCopy));
            int num4 = output.BytesWritten - bytesWritten;
            int num5 = this.BytesInHistory + input.Count;
            int num6 = num2 - num5;

            if (num4 != 0)
            {
                this.lastCompressionRatio = (double)num4 / (double)num6;
            }
        }
        // Output the block type and tree structure for our hard-coded trees.
        // Contains following data:
        //  "final" block flag 1 bit
        //  BLOCKTYPE_DYNAMIC 2 bits
        //  FastEncoderLiteralTreeLength
        //  FastEncoderDistanceTreeLength
        //
        static internal void WriteDeflatePreamble(OutputBuffer output)
        {
            //Debug.Assert( bitCount == 0, "bitCount must be zero before writing tree bit!");

            output.WriteBytes(FastEncoderStatics.FastEncoderTreeStructureData, 0, FastEncoderStatics.FastEncoderTreeStructureData.Length);
            output.WriteBits(FastEncoderStatics.FastEncoderPostTreeBitCount, FastEncoderStatics.FastEncoderPostTreeBitBuf);
        }
Exemple #10
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
        internal 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);
        }
Exemple #11
0
 internal DeflaterManaged()
 {
     this.deflateEncoder  = new FastEncoder();
     this.copyEncoder     = new CopyEncoder();
     this.input           = new DeflateInput();
     this.output          = new OutputBuffer();
     this.processingState = DeflaterManaged.DeflaterState.NotStarted;
 }
        internal DeflaterManaged() {
            deflateEncoder = new FastEncoder();
            copyEncoder = new CopyEncoder();
            input = new DeflateInput();
            output = new OutputBuffer();

            processingState = DeflaterState.NotStarted;
        }
        internal DeflaterManaged()
        {
            deflateEncoder = new FastEncoder();
            copyEncoder    = new CopyEncoder();
            input          = new DeflateInput();
            output         = new OutputBuffer();

            processingState = DeflaterState.NotStarted;
        }
Exemple #14
0
        private void WriteEndOfBlock(OutputBuffer output)
        {
            // The fast encoder outputs one long block, so it just needs to terminate this block
            const int EndOfBlockCode = 256;
            var       code_info      = FastEncoderStatics.FastEncoderLiteralCodeInfo[EndOfBlockCode];
            var       code_len       = (int)(code_info & 31);

            output.WriteBits(code_len, code_info >> 5);
        }
Exemple #15
0
        private void WriteLenNLen(ushort len, OutputBuffer output) {

            // len
            output.WriteUInt16(len);

            // nlen
            ushort onesComp = (ushort)(~(ushort)len);
            output.WriteUInt16(onesComp);
        }
Exemple #16
0
        private void WriteLenNLen(ushort len, OutputBuffer output)
        {
            // len
            output.WriteUInt16(len);

            // nlen
            ushort onesComp = (ushort)(~(ushort)len);

            output.WriteUInt16(onesComp);
        }
Exemple #17
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
            int 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);

            // write uncompressed bytes
            if (input != null && count > 0)
            {
                output.WriteBytes(input.Buffer, input.StartIndex, count);
                input.ConsumeBytes(count);
            }
        }
Exemple #18
0
 private void GetCompressedOutput(OutputBuffer output)
 {
     while (this.inputWindow.BytesAvailable > 0 && this.SafeToWriteTo(output))
     {
         this.inputWindow.GetNextSymbolOrMatch(this.currentMatch);
         if (this.currentMatch.State == MatchState.HasSymbol)
         {
             FastEncoder.WriteChar(this.currentMatch.Symbol, output);
         }
         else if (this.currentMatch.State == MatchState.HasMatch)
         {
             FastEncoder.WriteMatch(this.currentMatch.Length, this.currentMatch.Position, output);
         }
         else
         {
             FastEncoder.WriteChar(this.currentMatch.Symbol, output);
             FastEncoder.WriteMatch(this.currentMatch.Length, this.currentMatch.Position, output);
         }
     }
 }
Exemple #19
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
            int 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);

            // write uncompressed bytes
            if (input != null && count > 0) {
                output.WriteBytes(input.Buffer, input.StartIndex, count);
                input.ConsumeBytes(count);
            }

        }
Exemple #20
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;
                }

                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)
            {
                LastCompressionRatio = bytesWritten / (double)totalBytesConsumed;
            }
        }
Exemple #21
0
        internal static void WriteMatch(int matchLen, int matchPos, OutputBuffer output)
        {
            uint num  = FastEncoderStatics.FastEncoderLiteralCodeInfo[254 + matchLen];
            int  num2 = (int)(num & 31u);

            if (num2 <= 16)
            {
                output.WriteBits(num2, num >> 5);
            }
            else
            {
                output.WriteBits(16, num >> 5 & 65535u);
                output.WriteBits(num2 - 16, num >> 21);
            }
            num = FastEncoderStatics.FastEncoderDistanceCodeInfo[FastEncoderStatics.GetSlot(matchPos)];
            output.WriteBits((int)(num & 15u), num >> 8);
            int num3 = (int)(num >> 4 & 15u);

            if (num3 != 0)
            {
                output.WriteBits(num3, (uint)(matchPos & (int)FastEncoderStatics.BitMask[num3]));
            }
        }
Exemple #22
0
        // compress the bytes in input history window
        private void GetCompressedOutput(OutputBuffer output)
        {
            while (inputWindow.BytesAvailable > 0 && SafeToWriteTo(output))
            {
                // Find next match. A match can be a symbol,
                // a distance/length pair, a symbol followed by a distance/Length pair
                inputWindow.GetNextSymbolOrMatch(currentMatch);

                if (currentMatch.State == MatchState.HasSymbol)
                {
                    WriteChar(currentMatch.Symbol, output);
                }
                else if (currentMatch.State == MatchState.HasMatch)
                {
                    WriteMatch(currentMatch.Length, currentMatch.Position, output);
                }
                else
                {
                    WriteChar(currentMatch.Symbol, output);
                    WriteMatch(currentMatch.Length, currentMatch.Position, output);
                }
            }
        }
Exemple #23
0
 private void WriteEndOfBlock(OutputBuffer output) {
     // The fast encoder outputs one long block, so it just needs to terminate this block
     const int EndOfBlockCode = 256;
     uint code_info = FastEncoderStatics.FastEncoderLiteralCodeInfo[EndOfBlockCode];
     int code_len = (int)(code_info & 31);
     output.WriteBits(code_len, code_info >> 5);
 }
Exemple #24
0
        static internal void WriteMatch(int matchLen, int matchPos, OutputBuffer output) {
            Debug.Assert(matchLen >= FastEncoderWindow.MinMatch && matchLen <= FastEncoderWindow.MaxMatch, "Illegal currentMatch length!");

            // Get the code information for a match code
            uint codeInfo = FastEncoderStatics.FastEncoderLiteralCodeInfo[(FastEncoderStatics.NumChars + 1 - FastEncoderWindow.MinMatch) + matchLen];
            int codeLen = (int)codeInfo & 31;
            Debug.Assert(codeLen != 0, "Invalid Match Length!");
            if (codeLen <= 16) {
                output.WriteBits(codeLen, codeInfo >> 5);
            }
            else {
                output.WriteBits(16, (codeInfo >> 5) & 65535);
                output.WriteBits(codeLen - 16, codeInfo >> (5 + 16));
            }

            // Get the code information for a distance code
            codeInfo = FastEncoderStatics.FastEncoderDistanceCodeInfo[FastEncoderStatics.GetSlot(matchPos)];
            output.WriteBits((int)(codeInfo & 15), codeInfo >> 8);
            int extraBits = (int)(codeInfo >> 4) & 15;
            if (extraBits != 0) {
                output.WriteBits(extraBits, (uint)matchPos & FastEncoderStatics.BitMask[extraBits]);
            }
        }
Exemple #25
0
 private bool SafeToWriteTo(OutputBuffer output)
 {
     // can we safely continue writing to output buffer
     return(output.FreeBytes > FastEncoderStatics.MaxCodeLen);
 }
Exemple #26
0
 internal void RestoreState(OutputBuffer.BufferState state) {
     pos = state.pos;
     bitBuf = state.bitBuf;
     bitCount = state.bitCount;
 }
Exemple #27
0
 internal void GetBlockFooter(OutputBuffer output)
 {
     WriteEndOfBlock(output);
 }
Exemple #28
0
 // Compress data but don't format as block (doesn't have header and footer)
 internal void GetCompressedData(DeflateInput input, OutputBuffer output)
 {
     GetCompressedOutput(input, output, -1);
 }
Exemple #29
0
 internal void GetBlockHeader(OutputBuffer output) {
     WriteDeflatePreamble(output);
 }
Exemple #30
0
 internal void GetBlockFooter(OutputBuffer output) {
     WriteEndOfBlock(output);
 }
Exemple #31
0
 internal void GetBlock(DeflateInput input, OutputBuffer output, int maxBytesToCopy)
 {
     FastEncoder.WriteDeflatePreamble(output);
     this.GetCompressedOutput(input, output, maxBytesToCopy);
     this.WriteEndOfBlock(output);
 }
Exemple #32
0
 internal static void WriteDeflatePreamble(OutputBuffer output)
 {
     output.WriteBytes(FastEncoderStatics.FastEncoderTreeStructureData, 0, FastEncoderStatics.FastEncoderTreeStructureData.Length);
     output.WriteBits(9, 34u);
 }
Exemple #33
0
        internal static void WriteChar(byte b, OutputBuffer output)
        {
            uint num = FastEncoderStatics.FastEncoderLiteralCodeInfo[(int)b];

            output.WriteBits((int)(num & 31u), num >> 5);
        }
Exemple #34
0
        // compress the bytes in input history window
        private void GetCompressedOutput(OutputBuffer output) {

            while (inputWindow.BytesAvailable > 0 && SafeToWriteTo(output)) {

                // Find next match. A match can be a symbol, 
                // a distance/length pair, a symbol followed by a distance/Length pair
                inputWindow.GetNextSymbolOrMatch(currentMatch);

                if (currentMatch.State == MatchState.HasSymbol) {
                    WriteChar(currentMatch.Symbol, output);
                }
                else if (currentMatch.State == MatchState.HasMatch) {
                    WriteMatch(currentMatch.Length, currentMatch.Position, output);
                }
                else {
                    WriteChar(currentMatch.Symbol, output);
                    WriteMatch(currentMatch.Length, currentMatch.Position, output);
                }
            }
        }
Exemple #35
0
        internal static void WriteChar(byte b, OutputBuffer output)
        {
            var code = FastEncoderStatics.FastEncoderLiteralCodeInfo[b];

            output.WriteBits((int)code & 31, code >> 5);
        }
Exemple #36
0
 // Compress data but don't format as block (doesn't have header and footer)
 internal void GetCompressedData(DeflateInput input, OutputBuffer output) {
     GetCompressedOutput(input, output, -1);
 }
Exemple #37
0
 private bool SafeToWriteTo(OutputBuffer output) {  // can we safely continue writing to output buffer
     return output.FreeBytes > FastEncoderStatics.MaxCodeLen;
 }
Exemple #38
0
        // Output the block type and tree structure for our hard-coded trees.
        // Contains following data:
        //  "final" block flag 1 bit
        //  BLOCKTYPE_DYNAMIC 2 bits
        //  FastEncoderLiteralTreeLength
        //  FastEncoderDistanceTreeLength
        //
        static internal void WriteDeflatePreamble(OutputBuffer output) {
            //Debug.Assert( bitCount == 0, "bitCount must be zero before writing tree bit!");

            output.WriteBytes(FastEncoderStatics.FastEncoderTreeStructureData, 0, FastEncoderStatics.FastEncoderTreeStructureData.Length);
            output.WriteBits(FastEncoderStatics.FastEncoderPostTreeBitCount, FastEncoderStatics.FastEncoderPostTreeBitBuf);
        }
Exemple #39
0
 internal void GetBlockHeader(OutputBuffer output)
 {
     WriteDeflatePreamble(output);
 }
Exemple #40
0
 static internal void WriteChar(byte b, OutputBuffer output) {
     uint code = FastEncoderStatics.FastEncoderLiteralCodeInfo[b];
     output.WriteBits((int)code & 31, code >> 5);
 }
Exemple #41
0
 private bool SafeToWriteTo(OutputBuffer output)
 {
     return(output.FreeBytes > 16);
 }
Exemple #42
0
        internal static void WriteChar(Byte b, OutputBuffer output)
        {
            UInt32 num = FastEncoderStatics.FastEncoderLiteralCodeInfo[(Int32)b];

            output.WriteBits((Int32)(num & 31u), num >> 5);
        }