Example #1
0
        private static int TryReadUInt32Variant(Stream source, out uint value)
        {
            value = 0u;
            int num = source.ReadByte();

            if (num < 0)
            {
                return(0);
            }
            value = (uint)num;
            if ((value & 128u) == 0u)
            {
                return(1);
            }
            value &= 127u;
            num    = source.ReadByte();
            if (num < 0)
            {
                throw ProtoReader.EoF(null);
            }
            value |= (uint)((uint)(num & 127) << 7);
            if ((num & 128) == 0)
            {
                return(2);
            }
            num = source.ReadByte();
            if (num < 0)
            {
                throw ProtoReader.EoF(null);
            }
            value |= (uint)((uint)(num & 127) << 14);
            if ((num & 128) == 0)
            {
                return(3);
            }
            num = source.ReadByte();
            if (num < 0)
            {
                throw ProtoReader.EoF(null);
            }
            value |= (uint)((uint)(num & 127) << 21);
            if ((num & 128) == 0)
            {
                return(4);
            }
            num = source.ReadByte();
            if (num < 0)
            {
                throw ProtoReader.EoF(null);
            }
            value |= (uint)((uint)num << 28);
            if ((num & 240) == 0)
            {
                return(5);
            }
            throw new OverflowException();
        }
Example #2
0
        private static int ReadByteOrThrow(Stream source)
        {
            int num = source.ReadByte();

            if (num < 0)
            {
                throw ProtoReader.EoF(null);
            }
            return(num);
        }
Example #3
0
        public static int DirectReadVarintInt32(Stream source)
        {
            uint result;
            int  num = ProtoReader.TryReadUInt32Variant(source, out result);

            if (num <= 0)
            {
                throw ProtoReader.EoF(null);
            }
            return((int)result);
        }
Example #4
0
        private ulong ReadUInt64Variant()
        {
            ulong result;
            int   num = this.TryReadUInt64VariantWithoutMoving(out result);

            if (num > 0)
            {
                this.ioIndex   += num;
                this.available -= num;
                this.position  += num;
                return(result);
            }
            throw ProtoReader.EoF(this);
        }
Example #5
0
        private uint ReadUInt32Variant(bool trimNegative)
        {
            uint result;
            int  num = this.TryReadUInt32VariantWithoutMoving(trimNegative, out result);

            if (num > 0)
            {
                this.ioIndex   += num;
                this.available -= num;
                this.position  += num;
                return(result);
            }
            throw ProtoReader.EoF(this);
        }
Example #6
0
        public static void DirectReadBytes(Stream source, byte[] buffer, int offset, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            int num;

            while (count > 0 && (num = source.Read(buffer, offset, count)) > 0)
            {
                count  -= num;
                offset += num;
            }
            if (count > 0)
            {
                throw ProtoReader.EoF(null);
            }
        }
Example #7
0
 internal static void Seek(Stream source, int count, byte[] buffer)
 {
     if (source.get_CanSeek())
     {
         source.Seek((long)count, 1);
         count = 0;
     }
     else if (buffer != null)
     {
         int num;
         while (count > buffer.Length && (num = source.Read(buffer, 0, buffer.Length)) > 0)
         {
             count -= num;
         }
         while (count > 0 && (num = source.Read(buffer, 0, count)) > 0)
         {
             count -= num;
         }
     }
     else
     {
         buffer = BufferPool.GetBuffer();
         try
         {
             int num2;
             while (count > buffer.Length && (num2 = source.Read(buffer, 0, buffer.Length)) > 0)
             {
                 count -= num2;
             }
             while (count > 0 && (num2 = source.Read(buffer, 0, count)) > 0)
             {
                 count -= num2;
             }
         }
         finally
         {
             BufferPool.ReleaseBufferToPool(ref buffer);
         }
     }
     if (count > 0)
     {
         throw ProtoReader.EoF(null);
     }
 }
Example #8
0
        internal void Ensure(int count, bool strict)
        {
            if (count > this.ioBuffer.Length)
            {
                BufferPool.ResizeAndFlushLeft(ref this.ioBuffer, count, this.ioIndex, this.available);
                this.ioIndex = 0;
            }
            else if (this.ioIndex + count >= this.ioBuffer.Length)
            {
                Helpers.BlockCopy(this.ioBuffer, this.ioIndex, this.ioBuffer, 0, this.available);
                this.ioIndex = 0;
            }
            count -= this.available;
            int num  = this.ioIndex + this.available;
            int num2 = this.ioBuffer.Length - num;

            if (this.isFixedLength && this.dataRemaining < num2)
            {
                num2 = this.dataRemaining;
            }
            int num3;

            while (count > 0 && num2 > 0 && (num3 = this.source.Read(this.ioBuffer, num, num2)) > 0)
            {
                this.available += num3;
                count          -= num3;
                num2           -= num3;
                num            += num3;
                if (this.isFixedLength)
                {
                    this.dataRemaining -= num3;
                }
            }
            if (strict && count > 0)
            {
                throw ProtoReader.EoF(this);
            }
        }
Example #9
0
        public void SkipField()
        {
            WireType wireType = this.wireType;

            switch (wireType + 1)
            {
            case WireType.Fixed64:
            case (WireType)9:
                this.ReadUInt64Variant();
                return;

            case WireType.String:
                if (this.available < 8)
                {
                    this.Ensure(8, true);
                }
                this.available -= 8;
                this.ioIndex   += 8;
                this.position  += 8;
                return;

            case WireType.StartGroup:
            {
                int num = (int)this.ReadUInt32Variant(false);
                if (num <= this.available)
                {
                    this.available -= num;
                    this.ioIndex   += num;
                    this.position  += num;
                    return;
                }
                this.position += num;
                num           -= this.available;
                this.ioIndex   = (this.available = 0);
                if (this.isFixedLength)
                {
                    if (num > this.dataRemaining)
                    {
                        throw ProtoReader.EoF(this);
                    }
                    this.dataRemaining -= num;
                }
                ProtoReader.Seek(this.source, num, this.ioBuffer);
                return;
            }

            case WireType.EndGroup:
            {
                int num2 = this.fieldNumber;
                this.depth++;
                while (this.ReadFieldHeader() > 0)
                {
                    this.SkipField();
                }
                this.depth--;
                if (this.wireType == WireType.EndGroup && this.fieldNumber == num2)
                {
                    this.wireType = WireType.None;
                    return;
                }
                throw this.CreateWireTypeException();
            }

            case (WireType)6:
                if (this.available < 4)
                {
                    this.Ensure(4, true);
                }
                this.available -= 4;
                this.ioIndex   += 4;
                this.position  += 4;
                return;
            }
            throw this.CreateWireTypeException();
        }
Example #10
0
        private int TryReadUInt64VariantWithoutMoving(out ulong value)
        {
            if (this.available < 10)
            {
                this.Ensure(10, false);
            }
            if (this.available == 0)
            {
                value = 0uL;
                return(0);
            }
            int num = this.ioIndex;

            value = (ulong)this.ioBuffer[num++];
            if ((value & 128uL) == 0uL)
            {
                return(1);
            }
            value &= 127uL;
            if (this.available == 1)
            {
                throw ProtoReader.EoF(this);
            }
            ulong num2 = (ulong)this.ioBuffer[num++];

            value |= (num2 & 127uL) << 7;
            if ((num2 & 128uL) == 0uL)
            {
                return(2);
            }
            if (this.available == 2)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num++];
            value |= (num2 & 127uL) << 14;
            if ((num2 & 128uL) == 0uL)
            {
                return(3);
            }
            if (this.available == 3)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num++];
            value |= (num2 & 127uL) << 21;
            if ((num2 & 128uL) == 0uL)
            {
                return(4);
            }
            if (this.available == 4)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num++];
            value |= (num2 & 127uL) << 28;
            if ((num2 & 128uL) == 0uL)
            {
                return(5);
            }
            if (this.available == 5)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num++];
            value |= (num2 & 127uL) << 35;
            if ((num2 & 128uL) == 0uL)
            {
                return(6);
            }
            if (this.available == 6)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num++];
            value |= (num2 & 127uL) << 42;
            if ((num2 & 128uL) == 0uL)
            {
                return(7);
            }
            if (this.available == 7)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num++];
            value |= (num2 & 127uL) << 49;
            if ((num2 & 128uL) == 0uL)
            {
                return(8);
            }
            if (this.available == 8)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num++];
            value |= (num2 & 127uL) << 56;
            if ((num2 & 128uL) == 0uL)
            {
                return(9);
            }
            if (this.available == 9)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (ulong)this.ioBuffer[num];
            value |= num2 << 63;
            if ((num2 & 18446744073709551614uL) != 0uL)
            {
                throw ProtoReader.AddErrorData(new OverflowException(), this);
            }
            return(10);
        }
Example #11
0
        internal int TryReadUInt32VariantWithoutMoving(bool trimNegative, out uint value)
        {
            if (this.available < 10)
            {
                this.Ensure(10, false);
            }
            if (this.available == 0)
            {
                value = 0u;
                return(0);
            }
            int num = this.ioIndex;

            value = (uint)this.ioBuffer[num++];
            if ((value & 128u) == 0u)
            {
                return(1);
            }
            value &= 127u;
            if (this.available == 1)
            {
                throw ProtoReader.EoF(this);
            }
            uint num2 = (uint)this.ioBuffer[num++];

            value |= (num2 & 127u) << 7;
            if ((num2 & 128u) == 0u)
            {
                return(2);
            }
            if (this.available == 2)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (uint)this.ioBuffer[num++];
            value |= (num2 & 127u) << 14;
            if ((num2 & 128u) == 0u)
            {
                return(3);
            }
            if (this.available == 3)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (uint)this.ioBuffer[num++];
            value |= (num2 & 127u) << 21;
            if ((num2 & 128u) == 0u)
            {
                return(4);
            }
            if (this.available == 4)
            {
                throw ProtoReader.EoF(this);
            }
            num2   = (uint)this.ioBuffer[num];
            value |= num2 << 28;
            if ((num2 & 240u) == 0u)
            {
                return(5);
            }
            if (trimNegative && (num2 & 240u) == 240u && this.available >= 10 && this.ioBuffer[++num] == 255 && this.ioBuffer[++num] == 255 && this.ioBuffer[++num] == 255 && this.ioBuffer[++num] == 255 && this.ioBuffer[num + 1] == 1)
            {
                return(10);
            }
            throw ProtoReader.AddErrorData(new OverflowException(), this);
        }
Example #12
0
        public static int ReadLengthPrefix(Stream source, bool expectHeader, PrefixStyle style, out int fieldNumber, out int bytesRead)
        {
            fieldNumber = 0;
            switch (style)
            {
            case PrefixStyle.None:
                bytesRead = 0;
                return(2147483647);

            case PrefixStyle.Base128:
            {
                bytesRead = 0;
                uint num2;
                int  num;
                if (!expectHeader)
                {
                    num        = ProtoReader.TryReadUInt32Variant(source, out num2);
                    bytesRead += num;
                    return((int)((bytesRead >= 0) ? num2 : 4294967295u));
                }
                num        = ProtoReader.TryReadUInt32Variant(source, out num2);
                bytesRead += num;
                if (num <= 0)
                {
                    bytesRead = 0;
                    return(-1);
                }
                if ((num2 & 7u) != 2u)
                {
                    throw new InvalidOperationException();
                }
                fieldNumber = (int)(num2 >> 3);
                num         = ProtoReader.TryReadUInt32Variant(source, out num2);
                bytesRead  += num;
                if (bytesRead == 0)
                {
                    throw ProtoReader.EoF(null);
                }
                return((int)num2);
            }

            case PrefixStyle.Fixed32:
            {
                int num3 = source.ReadByte();
                if (num3 < 0)
                {
                    bytesRead = 0;
                    return(-1);
                }
                bytesRead = 4;
                return(num3 | ProtoReader.ReadByteOrThrow(source) << 8 | ProtoReader.ReadByteOrThrow(source) << 16 | ProtoReader.ReadByteOrThrow(source) << 24);
            }

            case PrefixStyle.Fixed32BigEndian:
            {
                int num4 = source.ReadByte();
                if (num4 < 0)
                {
                    bytesRead = 0;
                    return(-1);
                }
                bytesRead = 4;
                return(num4 << 24 | ProtoReader.ReadByteOrThrow(source) << 16 | ProtoReader.ReadByteOrThrow(source) << 8 | ProtoReader.ReadByteOrThrow(source));
            }

            default:
                throw new ArgumentOutOfRangeException("style");
            }
        }