def _decompress(binary: bin) -> bin: """ This function decompresses a binary using the scheme defined in the first byte of the input Args: binary (bin): a compressed binary Returns: bin: decompressed binary """ # check the 1-byte header to check the compression scheme used compress_scheme = binary[0] # remove the 1-byte header from the input stream binary = binary[1:] # 1) Decompress or return the original stream if compress_scheme == LZ4: return lz4.frame.decompress(binary) elif compress_scheme == ZSTD: return zstd.decompress(binary) elif compress_scheme == NO_COMPRESSION: return binary else: raise CompressionNotFoundException("compression scheme not found for" " compression code:" + str(compress_scheme))
def _compress(decompressed_input_bin: bin) -> bin: """ This function compresses a binary using the function _apply_compress_scheme if the input has been already compressed in some step, it will return it as it is Args: decompressed_input_bin (bin): binary to be compressed Returns: bin: a compressed binary """ compress_stream, compress_scheme = _apply_compress_scheme(decompressed_input_bin) try: z = scheme_to_bytes[compress_scheme] + compress_stream return z except KeyError: raise CompressionNotFoundException( f"Compression scheme not found for compression code: {str(compress_scheme)}" )
def _decompress(compressed_input_bin: bin, compress_scheme=LZ4) -> bin: """ This function decompresses a binary using LZ4 Args: compressed_input_bin (bin): a compressed binary compress_scheme: the compression method to use Returns: bin: decompressed binary """ if compress_scheme == LZ4: return lz4.frame.decompress(compressed_input_bin) elif compress_scheme == ZSTD: return zstd.decompress(compressed_input_bin) else: raise CompressionNotFoundException("compression scheme note found for" " compression code:" + str(compress_scheme))