def read_var_int(handle):
	"""
	Read a variable-length integer from the given file handle.
	"""
	res = 0
	shift = 1

	while True:
		byte = handle.read(1)[0]
		res += (byte & 0x7f) * shift
		if byte & 0x80: break
		shift <<= 7
		res += shift

	return res