def read_vint(reader: ByteReader, raw: bool = False): header_byte = ord(reader.read(1)) if header_byte == 0: raise WrongFile("VINT with zero header byte") tail_length = 0 mask = 0x80 while header_byte & mask == 0: tail_length += 1 mask >>= 1 header_number_part = header_byte & ~mask if not raw else header_byte return parse_big_endian_number( bytes([header_number_part]) + reader.read(tail_length))
def __init__(self, reader: ByteReader): self.offset = reader.position size = parse_big_endian_number(reader.read(4)) kind = reader.read(4) if size == 1: size = parse_big_endian_number(reader.read(8)) elif size == 0: size = reader.end - reader.position if kind == b"uuid": kind += reader.read(16) content_offset = reader.position content_size = size - (content_offset - self.offset) self.kind = kind self.full_size = size self.reader = RegionByteReader(reader, content_offset, content_size) reader.skip(content_size)
def read_uint(reader: ByteReader, default: int = 0) -> int: if reader.ended: return default return parse_big_endian_number(reader.read())