Esempio n. 1
0
        public bool Decode(StreamManipulator input)
        {
            try
            {
                lnum  = input.GrabBits(5) + 257;
                dnum  = input.GrabBits(5) + 1;
                blnum = input.GrabBits(4) + 4;
                num   = lnum + dnum;

                lengths = new byte[19];

                for (int i = 0; i < blnum; i++)
                {
                    lengths[BL_ORDER[i]] = (byte)input.GrabBits(3, true);
                }
                blTree  = new InflaterHuffmanTree(lengths);
                lengths = new byte[num];

                int index = 0;
                while (index < lnum + dnum)
                {
                    byte len;

                    int symbol = blTree.GetSymbol(input);
                    if (symbol < 0)
                    {
                        return(false);
                    }
                    if (symbol < 16)
                    {
                        lengths[index++] = (byte)symbol;
                    }
                    else
                    {
                        len = 0;
                        if (symbol == 16)
                        {
                            if (index == 0)
                            {
                                return(false);                                  // No last length!
                            }
                            len    = lengths[index - 1];
                            symbol = input.GrabBits(2, true) + 3;
                        }
                        else if (symbol == 17)
                        {
                            // repeat zero 3..10 times
                            symbol = input.GrabBits(3, true) + 3;
                        }
                        else
                        {
                            // (symbol == 18), repeat zero 11..138 times
                            symbol = input.GrabBits(7, true) + 11;
                        }

                        if (index + symbol > lnum + dnum)
                        {
                            return(false);                            // too many lengths!
                        }
                        // repeat last or zero symbol times
                        while (symbol-- > 0)
                        {
                            lengths[index++] = len;
                        }
                    }
                }

                if (lengths[256] == 0)
                {
                    return(false);                    // No end-of-block code!
                }
                return(true);
            }
            catch (Exception x)
            {
                return(false);
            }
        }