import struct from io import BytesIO class BCDataStream: def __init__(self, data=None, fp=None): self.data = fp or BytesIO(data) def tell(self): return self.data.tell() def reset(self): self.data.seek(0) def get_bytes(self): return self.data.getvalue() def read(self, size): return self.data.read(size) def write(self, data): self.data.write(data) def write_many(self, many): self.data.writelines(many) def read_string(self): return self.read(self.read_compact_size()) def write_string(self, s): self.write_compact_size(len(s)) self.write(s) def read_compact_size(self): size = self.read_uint8() if size < 253: return size if size == 253: return self.read_uint16() if size == 254: return self.read_uint32() if size == 255: return self.read_uint64() def write_compact_size(self, size): if size < 253: self.write_uint8(size) elif size <= 0xFFFF: self.write_uint8(253) self.write_uint16(size) elif size <= 0xFFFFFFFF: self.write_uint8(254) self.write_uint32(size) else: self.write_uint8(255) self.write_uint64(size) def read_boolean(self): return self.read_uint8() != 0 def write_boolean(self, val): return self.write_uint8(1 if val else 0) int8 = struct.Struct('b') uint8 = struct.Struct('B') int16 = struct.Struct('