示例#1
0
        //
        // This function will try to get enough bits from input and
        // try to decode the bits.
        // If there are no enought bits in the input, this function will return -1.
        //
        public int GetNextSymbol(InputBuffer input)
        {
            // Try to load 16 bits into input buffer if possible and get the bitBuffer value.
            // If there aren't 16 bits available we will return all we have in the
            // input buffer.
            uint bitBuffer = input.TryLoad16Bits();

            if (input.AvailableBits == 0)      // running out of input.
            {
                return(-1);
            }

            // decode an element
            int symbol = table[bitBuffer & tableMask];

            if (symbol < 0)         //  this will be the start of the binary tree
            // navigate the tree
            {
                uint mask = (uint)1 << tableBits;
                do
                {
                    symbol = -symbol;
                    if ((bitBuffer & mask) == 0)
                    {
                        symbol = left[symbol];
                    }
                    else
                    {
                        symbol = right[symbol];
                    }
                    mask <<= 1;
                } while (symbol < 0);
            }

            int codeLength = codeLengthArray[symbol];

            // huffman code lengths must be at least 1 bit long
            if (codeLength <= 0)
            {
                throw new InvalidDataException(SR.GetString(SR.InvalidHuffmanData));
            }

            //
            // If this code is longer than the # bits we had in the bit buffer (i.e.
            // we read only part of the code), we can hit the entry in the table or the tree
            // for another symbol. However the length of another symbol will not match the
            // available bits count.
            if (codeLength > input.AvailableBits)
            {
                // We already tried to load 16 bits and maximum length is 15,
                // so this means we are running out of input.
                return(-1);
            }

            input.SkipBits(codeLength);
            return(symbol);
        }
示例#2
0
        public Int32 GetNextSymbol(InputBuffer input)
        {
            UInt32 num = input.TryLoad16Bits();

            if (input.AvailableBits == 0)
            {
                return(-1);
            }
            Int32 num2 = (Int32)this.table[(Int32)(checked ((IntPtr)(unchecked ((UInt64)num & (UInt64)((Int64)this.tableMask)))))];

            if (num2 < 0)
            {
                UInt32 num3 = 1u << this.tableBits;
                do
                {
                    num2 = -num2;
                    if ((num & num3) == 0u)
                    {
                        num2 = (Int32)this.left[num2];
                    }
                    else
                    {
                        num2 = (Int32)this.right[num2];
                    }
                    num3 <<= 1;
                }while (num2 < 0);
            }
            Int32 num4 = (Int32)this.codeLengthArray[num2];

            if (num4 <= 0)
            {
                throw new InvalidDataException(SR.GetString("Invalid Huffman data"));
            }
            if (num4 > input.AvailableBits)
            {
                return(-1);
            }
            input.SkipBits(num4);
            return(num2);
        }
示例#3
0
        //
        // This function will try to get enough bits from input and
        // try to decode the bits.
        // If there are no enought bits in the input, this function will return -1.
        //
        public int GetNextSymbol(InputBuffer input)
        {
            // Try to load 16 bits into input buffer if possible and get the bitBuffer value.
            // If there aren't 16 bits available we will return all we have in the
            // input buffer.
            uint bitBuffer = input.TryLoad16Bits();
            if( input.AvailableBits == 0) {    // running out of input.
                return -1;
            }

            // decode an element
            int symbol = table[bitBuffer & tableMask];
            if( symbol < 0) {       //  this will be the start of the binary tree
                // navigate the tree
                uint mask = (uint)1 << tableBits;
                do
                {
                    symbol = -symbol;
                    if ((bitBuffer & mask) == 0)
                        symbol = left[symbol];
                    else
                        symbol = right[symbol];
                    mask <<= 1;
                } while (symbol < 0);
            }

            int codeLength = codeLengthArray[symbol];

            // huffman code lengths must be at least 1 bit long
            if (codeLength <= 0)
            {
                throw new InvalidDataException(SR.GetString(SR.InvalidHuffmanData));
            }

            //
            // If this code is longer than the # bits we had in the bit buffer (i.e.
            // we read only part of the code), we can hit the entry in the table or the tree
            // for another symbol. However the length of another symbol will not match the
            // available bits count.
            if (codeLength > input.AvailableBits)
            {
                // We already tried to load 16 bits and maximum length is 15,
                // so this means we are running out of input.
                return -1;
            }

            input.SkipBits(codeLength);
            return symbol;
        }