Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Deflate" /> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        private Deflate(BitReader reader)
        {
            this.input = reader;
            this.outputStream = new MemoryStream();
            this.output = new BinaryWriter(this.outputStream);
            this.dictionary = new CircularDictionary(32 * 1024);

            // Process the stream of blocks
            while (true)
            {
                // Block header
                var isFinal = this.input.ReadNoEof() != -1; // bfinal
                var type = this.ReadInt(2); // btype

                // Decompress by type
                if (type == 0)
                {
                    this.DecompressUncompressedBlock();
                }
                else if (type == 1 || type == 2)
                {
                    CodeTree litLenCode, distCode;
                    if (type == 1)
                    {
                        litLenCode = FixedLiteralLengthCode;
                        distCode = FixedDistanceCode;
                    }
                    else
                    {
                        var temp = this.DecodeHuffmanCodes();
                        litLenCode = temp[0];
                        distCode = temp[1];
                    }

                    this.DecompressHuffmanBlock(litLenCode, distCode);
                }
                else if (type == 3)
                {
                    throw new FormatException("Invalid block type");
                }
                else
                {
                    throw new NotImplementedException();
                }

                if (isFinal)
                {
                    break;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Deflate" /> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        private Deflate(BitReader reader)
        {
            this.input        = reader;
            this.outputStream = new MemoryStream();
            this.output       = new BinaryWriter(this.outputStream);
            this.dictionary   = new CircularDictionary(32 * 1024);

            // Process the stream of blocks
            while (true)
            {
                // Block header
                var isFinal = this.input.ReadNoEof() != -1; // bfinal
                var type    = this.ReadInt(2);              // btype

                // Decompress by type
                if (type == 0)
                {
                    this.DecompressUncompressedBlock();
                }
                else if (type == 1 || type == 2)
                {
                    CodeTree litLenCode, distCode;
                    if (type == 1)
                    {
                        litLenCode = FixedLiteralLengthCode;
                        distCode   = FixedDistanceCode;
                    }
                    else
                    {
                        var temp = this.DecodeHuffmanCodes();
                        litLenCode = temp[0];
                        distCode   = temp[1];
                    }

                    this.DecompressHuffmanBlock(litLenCode, distCode);
                }
                else if (type == 3)
                {
                    throw new FormatException("Invalid block type");
                }
                else
                {
                    throw new NotImplementedException();
                }

                if (isFinal)
                {
                    break;
                }
            }
        }