Ejemplo n.º 1
0
        ////////////////////////////////////////////////////////////////////
        // Decode dynamic Huffman header
        ////////////////////////////////////////////////////////////////////

        private void DecodeDynamicHuffamTrees()
        {
            // length of length/literal tree
            Int32 LiteralLength = GetBits(5) + 257;

            // length of distance tree
            Int32 DistanceLength = GetBits(5) + 1;

            // length of bit length tree
            Int32 BitLengthReceived = GetBits(4) + 4;

            // get bit length info from input stream
            // note: array length must be 19 and not length received
            Array.Clear(BitLengthArray, 0, BitLengthArray.Length);
            for (Int32 Index = 0; Index < BitLengthReceived; Index++)
            {
                BitLengthArray[BitLengthOrder[Index]] = (Byte)GetBits(3);
            }

            // create bit length hauffman tree
            BitLengthTree.BuildTree(BitLengthArray, 0, BitLengthArray.Length);

            // create a combined array of length/literal and distance
            Int32 TotalLength = LiteralLength + DistanceLength;

            Array.Clear(LiteralDistanceArray, 0, LiteralDistanceArray.Length);
            Byte LastCode = 0;

            for (Int32 Ptr = 0; Ptr < TotalLength;)
            {
                // get next symbol from input stream
                Int32 Symbol = ReadSymbol(BitLengthTree);

                // switch based on symbol
                switch (Symbol)
                {
                // symbol is less than 16 it is a literal
                default:
                    LiteralDistanceArray[Ptr++] = LastCode = (Byte)Symbol;
                    continue;

                case RepeatSymbol_3_6:
                    for (Int32 Count = GetBits(2) + 3; Count > 0; Count--)
                    {
                        LiteralDistanceArray[Ptr++] = LastCode;
                    }
                    continue;

                case RepeatSymbol_3_10:
                    for (Int32 Count = GetBits(3) + 3; Count > 0; Count--)
                    {
                        LiteralDistanceArray[Ptr++] = 0;
                    }
                    continue;

                case RepeatSymbol_11_138:
                    for (Int32 Count = GetBits(7) + 11; Count > 0; Count--)
                    {
                        LiteralDistanceArray[Ptr++] = 0;
                    }
                    continue;
                }
            }

            // create the literal array and distance array
            LiteralTree.SetDynamic();
            LiteralTree.BuildTree(LiteralDistanceArray, 0, LiteralLength);
            DistanceTree.SetDynamic();
            DistanceTree.BuildTree(LiteralDistanceArray, LiteralLength, DistanceLength);
            return;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decode dynamic Huffman header
        /// </summary>
        private void DecodeDynamicHuffamTrees()
        {
            // length of length/literal tree
            int literalLength = GetBits(5) + 257;

            // length of distance tree
            int distanceLength = GetBits(5) + 1;

            // length of bit length tree
            int bitLengthReceived = GetBits(4) + 4;

            // get bit length info from input stream
            // note: array length must be 19 and not length received
            Array.Clear(_bitLengthArray, 0, _bitLengthArray.Length);
            for (int index = 0; index < bitLengthReceived; index++)
            {
                _bitLengthArray[BitLengthOrder[index]] = (byte)GetBits(3);
            }

            // create bit length hauffman tree
            _bitLengthTree.BuildTree(_bitLengthArray, 0, _bitLengthArray.Length);

            // create a combined array of length/literal and distance
            int totalLength = literalLength + distanceLength;

            Array.Clear(_literalDistanceArray, 0, _literalDistanceArray.Length);
            byte lastCode = 0;

            for (int ptr = 0; ptr < totalLength;)
            {
                // get next symbol from input stream
                int symbol = ReadSymbol(_bitLengthTree);

                // switch based on symbol
                switch (symbol)
                {
                // symbol is less than 16 it is a literal
                default:
                    _literalDistanceArray[ptr++] = lastCode = (byte)symbol;
                    continue;

                case RepeatSymbol_3_6:
                    for (int count = GetBits(2) + 3; count > 0; count--)
                    {
                        _literalDistanceArray[ptr++] = lastCode;
                    }
                    continue;

                case RepeatSymbol_3_10:
                    for (int count = GetBits(3) + 3; count > 0; count--)
                    {
                        _literalDistanceArray[ptr++] = 0;
                    }
                    continue;

                case RepeatSymbol_11_138:
                    for (int count = GetBits(7) + 11; count > 0; count--)
                    {
                        _literalDistanceArray[ptr++] = 0;
                    }
                    continue;
                }
            }

            // create the literal array and distance array
            _literalTree.SetDynamic();
            _literalTree.BuildTree(_literalDistanceArray, 0, literalLength);
            _distanceTree.SetDynamic();
            _distanceTree.BuildTree(_literalDistanceArray, literalLength, distanceLength);
        }