def init_lz4(): from lz4 import VERSION, block import struct block_compress = block.compress block_decompress = block.decompress LZ4_HEADER = struct.Struct(b'<L') try: from lz4 import library_version_string version = library_version_string() except ImportError: from lz4.version import version def lz4_compress(packet, level): flag = min(15, level) | LZ4_FLAG if level>=9: return flag, block_compress(packet, mode="high_compression", compression=level) if level<=3: return flag, block_compress(packet, mode="fast", acceleration=8-level*2) return flag, block_compress(packet) def lz4_decompress(data): size = LZ4_HEADER.unpack_from(data[:4])[0] #it would be better to use the max_size we have in protocol, #but this hardcoded value will do for now if size>MAX_DECOMPRESSED_SIZE: sizemb = size//1024//1024 maxmb = MAX_DECOMPRESSED_SIZE//1024//1024 raise Exception("uncompressed data is too large: %iMB, limit is %iMB" % (sizemb, maxmb)) return block_decompress(data) return Compression("lz4", version, VERSION.encode("latin1"), lz4_compress, lz4_decompress)
python_lz4_version = None lz4_version = None has_lz4 = False def no_lz4(packet, level): raise Exception("lz4 is not supported!") lz4_compress = no_lz4 try: from lz4 import VERSION as python_lz4_version #@UnresolvedImport from lz4.version import version as lz4_version from lz4.block import compress, decompress as LZ4_uncompress python_lz4_version = python_lz4_version.encode("latin1") has_lz4 = True def lz4_block_compress(packet, level): flag = min(15, level) | LZ4_FLAG if level >= 7: return flag, compress(packet, mode="high_compression", compression=level) if level <= 3: return flag, compress(packet, mode="fast", acceleration=8 - level * 2) return flag, compress(packet) lz4_compress = lz4_block_compress