예제 #1
0
        public int ReadSignedInt(int numBits)
        {
            BitStreamUtil.AssertMaxBits(32, numBits);

            // Just like PeekInt, but we cast to signed long before the shr because we need to sext
            var result =
                (int)
                ((long)(BitConverter.ToUInt64(Buffer, (Offset / 8) & ~3) << (8 * 8 - Offset % (8 * 4) - numBits)) >>
                 (8 * 8 - numBits));

            Advance(numBits);
            return(result);
        }
예제 #2
0
        public int ReadProtobufVarInt()
        {
            var availableBits = BitsInBuffer + SLED * 8 - Offset;
            // Start by overflowingly reading 32 bits.
            // Reading beyond the buffer contents is safe in this case,
            // because the sled ensures that we stay inside of the buffer.
            var buf = PeekInt(32, true);

            // always take the first bytes; others if necessary
            var result = buf & MSK_1;

            BitStreamUtil.AssertMaxBits(availableBits, 1 * 8);
            if ((buf & MSB_1) != 0)
            {
                result |= (buf & MSK_2) >> 1;
                BitStreamUtil.AssertMaxBits(availableBits, 1 * 8);
                if ((buf & MSB_2) != 0)
                {
                    result |= (buf & MSK_3) >> 2;
                    BitStreamUtil.AssertMaxBits(availableBits, 2 * 8);
                    if ((buf & MSB_3) != 0)
                    {
                        result |= (buf & MSK_4) >> 3;
                        BitStreamUtil.AssertMaxBits(availableBits, 3 * 8);
                        if ((buf & MSB_4) != 0)
                        {
                            // dammit, it's too large (probably negative)
                            // fall back to the slow implementation, that's rare
                            return(BitStreamUtil.ReadProtobufVarIntStub(this));
                        }

                        Advance(4 * 8);
                    }
                    else
                    {
                        Advance(3 * 8);
                    }
                }
                else
                {
                    Advance(2 * 8);
                }
            }
            else
            {
                Advance(1 * 8);
            }

            return(unchecked ((int)result));
        }
예제 #3
0
        private uint PeekInt(int numBits, bool mayOverflow = false)
        {
            BitStreamUtil.AssertMaxBits(32, numBits);
            Debug.Assert(mayOverflow || Offset + numBits <= BitsInBuffer + SLED * 8, "gg",
                         "This code just fell apart. We're all dead. Offset={0} numBits={1} BitsInBuffer={2}", Offset, numBits,
                         BitsInBuffer);


            // _      xxxnno      _
            // _   xxxnno         _
            // _    xxxnno


            return
                ((uint)
                 ((BitConverter.ToUInt64(Buffer, (Offset / 8) & ~3) << (8 * 8 - Offset % (8 * 4) - numBits)) >>
                  (8 * 8 - numBits)));
        }
예제 #4
0
 public int ReadProtobufVarInt()
 {
     return(BitStreamUtil.ReadProtobufVarIntStub(this));
 }
예제 #5
0
 public byte ReadByte(int bits)
 {
     BitStreamUtil.AssertMaxBits(8, bits);
     return((byte)ReadInt(bits));
 }