Example #1
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");
            }
        }
Example #2
0
 public static int DirectReadBigEndianInt32(Stream source)
 {
     return(ProtoReader.ReadByteOrThrow(source) << 24 | ProtoReader.ReadByteOrThrow(source) << 16 | ProtoReader.ReadByteOrThrow(source) << 8 | ProtoReader.ReadByteOrThrow(source));
 }