Exemple #1
0
def read_length_delimited(stream: io.BufferedIOBase) -> Optional[bytes]:
    length = read_varint(stream)

    if length is None:
        return None

    data = stream.read1(length)

    return data if len(data) == length else None
Exemple #2
0
def read_fixed(stream: io.BufferedIOBase,
               wire_type: WireType) -> Optional[bytes]:
    fixed_width = {WireType.Fixed32: 4, WireType.Fixed64: 8}
    data = stream.read1(fixed_width[WireType(wire_type)])

    if len(data) == 0 or len(data) != fixed_width[wire_type]:
        return None

    return data
Exemple #3
0
def hash_file(fobj: io.BufferedIOBase,
              blake2b_salt: str = None,
              hash_size_bytes: int = DEFAULT_FILE_HASH_SIZE) -> (str, str):
    if blake2b_salt is None:
        salt = os.urandom(blake2b.SALT_SIZE)
        blake2b_salt = salt.hex()
    else:
        salt = bytes.fromhex(blake2b_salt)
    h = blake2b(digest_size=hash_size_bytes, salt=salt)
    MAX_BUFFER = 1024 * 1024
    while True:
        buf = fobj.read1(MAX_BUFFER)
        if not buf:
            break
        h.update(buf)
    return h.hexdigest(), blake2b_salt
Exemple #4
0
def _iter_bytes(stream: io.BufferedIOBase) -> Iterable[bytes]:
    byte = stream.read1(1)

    while len(byte) != 0:
        yield byte
        byte = stream.read1(1)