utils.py 389 B

12345678910111213
  1. import typing
  2. def pack_int(length: int, data: bytes) -> bytes:
  3. return len(data).to_bytes(length, "big") + data
  4. def pack_list(length: int, iterable: typing.Iterable[bytes]) -> bytes:
  5. return pack_int(length, b"".join(data for data in iterable))
  6. def pack_all(length: int, iterable: typing.Iterable) -> bytes:
  7. return pack_int(length, b"".join(obj.pack() for obj in iterable))