Ejemplo n.º 1
0
        public void UInt_WrittenAsVarint_IsSameUInt_When_ReadedBack()
        {
            Prop.ForAll<uint>(num =>
            {
                using (var wms = new WriteOnlyMemoryStream())
                {
                    Varint.WriteUInt32(num, wms);
                    var varintBytes = wms.ToArray();

                    using (var ms = new ReadOnlyMemoryStream(varintBytes))
                    {
                        var readed = Varint.ReadUInt32(ms);
                        return readed == num;
                    }
                }
            }).QuickCheckThrowOnFailure();
        }
Ejemplo n.º 2
0
        public static uint ReadUInt32(ReadOnlyMemoryStream input)
        {
            int result = 0;
            int offset = 0;
            for (; offset < 32; offset += 7)
            {
                int b = input.ReadByte();
                if (b == -1) throw Truncated;

                result |= (b & 0x7f) << offset;
                if ((b & 0x80) == 0)
                    return (uint)result;
            }
            // keep reading up to 64 bits
            for (; offset < 64; offset += 7)
            {
                int b = input.ReadByte();
                if (b == -1) throw Truncated;
                if ((b & 0x80) == 0)
                    return (uint)result;
            }
            throw Malformed;
        }