Beispiel #1
0
        public static SubItemToken StartSubItem(ProtoReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            WireType wireType = reader.wireType;

            if (wireType != WireType.String)
            {
                if (wireType != WireType.StartGroup)
                {
                    throw reader.CreateWireTypeException();
                }
                reader.wireType = WireType.None;
                reader.depth++;
                return(new SubItemToken(-reader.fieldNumber));
            }
            else
            {
                int num = (int)reader.ReadUInt32Variant(false);
                if (num < 0)
                {
                    throw ProtoReader.AddErrorData(new InvalidOperationException(), reader);
                }
                int value = reader.blockEnd;
                reader.blockEnd = reader.position + num;
                reader.depth++;
                return(new SubItemToken(value));
            }
        }
Beispiel #2
0
        internal static object ReadTypedObject(object value, int key, ProtoReader reader, Type type)
        {
            if (reader.model == null)
            {
                throw ProtoReader.AddErrorData(new InvalidOperationException("Cannot deserialize sub-objects unless a model is provided"), reader);
            }
            SubItemToken token = ProtoReader.StartSubItem(reader);

            if (key >= 0)
            {
                value = reader.model.Deserialize(key, value, reader);
            }
            else if (type == null || !reader.model.TryDeserializeAuxiliaryType(reader, DataFormat.Default, 1, type, ref value, true, false, true, false))
            {
                TypeModel.ThrowUnexpectedType(type);
            }
            ProtoReader.EndSubItem(token, reader);
            return(value);
        }
Beispiel #3
0
        public float ReadSingle()
        {
            WireType wireType = this.wireType;

            if (wireType != WireType.Fixed64)
            {
                if (wireType != WireType.Fixed32)
                {
                    throw this.CreateWireTypeException();
                }
                int num = this.ReadInt32();
                return(BitConverter.ToSingle(BitConverter.GetBytes(num), 0));
            }
            else
            {
                double num2 = this.ReadDouble();
                float  num3 = (float)num2;
                if (Helpers.IsInfinity(num3) && !Helpers.IsInfinity(num2))
                {
                    throw ProtoReader.AddErrorData(new OverflowException(), this);
                }
                return(num3);
            }
        }
Beispiel #4
0
        public static void EndSubItem(SubItemToken token, ProtoReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            int      value    = token.value;
            WireType wireType = reader.wireType;

            if (wireType != WireType.EndGroup)
            {
                if (value < reader.position)
                {
                    throw reader.CreateException("Sub-message not read entirely");
                }
                if (reader.blockEnd != reader.position && reader.blockEnd != 2147483647)
                {
                    throw reader.CreateException("Sub-message not read correctly");
                }
                reader.blockEnd = value;
                reader.depth--;
            }
            else
            {
                if (value >= 0)
                {
                    throw ProtoReader.AddErrorData(new ArgumentException("token"), reader);
                }
                if (-value != reader.fieldNumber)
                {
                    throw reader.CreateException("Wrong group was ended");
                }
                reader.wireType = WireType.None;
                reader.depth--;
            }
        }
Beispiel #5
0
 private Exception CreateException(string message)
 {
     return(ProtoReader.AddErrorData(new ProtoException(message), this));
 }
Beispiel #6
0
        public void ThrowEnumException(Type type, int value)
        {
            string text = (type != null) ? type.get_FullName() : "<null>";

            throw ProtoReader.AddErrorData(new ProtoException("No " + text + " enum is mapped to the wire-value " + value.ToString()), this);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }
Beispiel #9
0
 private static Exception EoF(ProtoReader source)
 {
     return(ProtoReader.AddErrorData(new EndOfStreamException(), source));
 }