using System.IO;

public static class BinaryReaderExtensions
{
	public static int Read7BitEncodedInt(this BinaryReader reader)
	{
		byte workByte;
		int workValue = 0;
		int shift = 0;
		do
		{
			workByte = reader.ReadByte();
			workValue |= (((int)(workByte & 0x7f)) << shift);
			shift += 7;
		} while((workByte & 0x80) != 0);
		return workValue;
	}	
	
	public static long ReadSIntVar(this BinaryReader reader)
	{
		long count = 0;
		int shift = 0;
		long sign = 0;
		byte b;
		
		b = reader.ReadByte();
		count |= (b & 0x3fu);
		shift += 6;
		
		if((b & 0x40) != 0)
		{
			sign = -1;
		}
		
		while ((b & 0x80) != 0) {
			b = reader.ReadByte();
			count |= (b & 0x7fu) << shift;
			shift += 7;
		}
		return count * sign;
	}
	
	public static ulong ReadUIntVar(this BinaryReader reader)
	{
		ulong count = 0;
		int shift = 0;
		byte b;
		do {
			b = reader.ReadByte();
			count |= (ulong)((b & 0x7fu) << shift);
			shift += 7;
		} while ((b & 0x80) != 0);
		return count;
	}
}


