Esempio n. 1
0
        public static void Decompress <T>(T data, BitSource bits, DeflateOutput <T> output)
        {
            while (true)
            {
                using var currentBlock = new DeflateBlock(bits);

                if (currentBlock.Type == BlockType.NoCompression)
                {
                    // skip to next byte
                    bits.SkipToNextByte();

                    // Read length and nlength?
                    var length = bits.ReadBitsAsUshort(16);
                    bits.ConsumeBytes(2);

                    // copy to output
                    output.Write(data, length);
                    bits.ConsumeBytes(length);
                }
                else
                {
                    while (true)
                    {
                        // decode literal/length value from input stream
                        var value = currentBlock.GetNextValue();

                        // end of block
                        if ((value & 0x1FF) == DeflateConstants.EndOfBlock)
                        {
                            break;
                        }

                        if (value < DeflateConstants.EndOfBlock)
                        {
                            //copy value(literal byte) to output stream
                            output.WriteByte((byte)value);
                        }
                        else // value = 257..285
                        {
                            currentBlock.GetLengthAndDistance(value, out var length, out var distance);

                            output.WriteWindow(length, distance);
                        }
                    }
                }

                if (currentBlock.IsFinal)
                {
                    break;
                }
            }
        }