static ulong ReadVarint64(Stream stream)
{
long result = 0;
int offset = 0;
for (; offset < 64; offset += 7)
{
int b = stream.ReadByte();
if (b == -1)
throw new EndOfStreamException();
result |= ((long)(b & 0x7f)) << offset;
if ((b & 0x80) == 0)
return (ulong)result;
}
throw new InvalidDataException();
}