예제 #1
0
        public void ParseDHT()
        {
            uint headerLength = (uint)input.ReadInt16() - 2; // Subtract myself

            while (headerLength != 0)
            {
                uint b  = input.ReadByte();
                uint Tc = (b >> 4);
                if (Tc != 0)
                {
                    throw new RawDecoderException("Unsupported Table class.");
                }

                uint Th = b & 0xf;
                if (Th > 3)
                {
                    throw new RawDecoderException("Invalid huffman table destination id.");
                }

                uint         acc   = 0;
                HuffmanTable table = huff[Th];

                if (table.Initialized)
                {
                    throw new RawDecoderException("Duplicate table definition");
                }

                for (int i = 0; i < 16; i++)
                {
                    table.bits[i + 1] = input.ReadByte();
                    acc += table.bits[i + 1];
                }
                table.bits[0] = 0;
                if (acc > 256)
                {
                    throw new RawDecoderException("Invalid DHT table.");
                }

                if (headerLength < 1 + 16 + acc)
                {
                    throw new RawDecoderException("Invalid DHT table length.");
                }

                for (int i = 0; i < acc; i++)
                {
                    table.huffval[i] = input.ReadByte();
                }
                table.Create(frame.precision);
                headerLength -= 1 + 16 + acc;
            }
        }