Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        internal DeflaterManaged()
        {
            deflateEncoder = new FastEncoder();
            copyEncoder    = new CopyEncoder();
            input          = new DeflateInput();
            output         = new OutputBuffer();

            processingState = DeflaterState.NotStarted;
        }
Beispiel #4
0
        private void WriteLenNLen(ushort len, OutputBuffer output)
        {
            // len
            output.WriteUInt16(len);

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

            output.WriteUInt16(onesComp);
        }
Beispiel #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
            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);
            }
        }
Beispiel #6
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;
            }
        }
Beispiel #7
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);
                }
            }
        }
Beispiel #8
0
 internal void GetBlockFooter(OutputBuffer output)
 {
     WriteEndOfBlock(output);
 }
Beispiel #9
0
 internal void GetBlockHeader(OutputBuffer output)
 {
     WriteDeflatePreamble(output);
 }
Beispiel #10
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);
 }
Beispiel #11
0
        static internal void WriteChar(byte b, OutputBuffer output)
        {
            uint code = FastEncoderStatics.FastEncoderLiteralCodeInfo[b];

            output.WriteBits((int)code & 31, code >> 5);
        }
Beispiel #12
0
 private bool SafeToWriteTo(OutputBuffer output)    // can we safely continue writing to output buffer
 {
     return(output.FreeBytes > FastEncoderStatics.MaxCodeLen);
 }