#!/usr/bin/env python3 """CC0: LZ4 dictionary-pointer lifecycle fixture; not a supported rebinding API.""" import argparse import ctypes as C import datetime import importlib.util import json import pathlib import platform import struct BASE = pathlib.Path(__file__).parent SPEC = importlib.util.spec_from_file_location("dictionary_contract", BASE / "lz4-dictionary-contract.py") D = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(D) lib, checked, sha, require = D.lib, D.checked, D.sha, D.require BLOCK_BYTES = 65536 PAYLOAD = D.DICTIONARY * 4 ACTIONS = ("retain", "replace-argument", "mutate-retained", "wrong-from-start") def compress(independent, checksum): prefs = D.FRAME.Preferences() prefs.frameInfo.blockSizeID = 4 prefs.frameInfo.blockMode = int(independent) prefs.frameInfo.blockChecksumFlag = 1 prefs.frameInfo.contentChecksumFlag = int(checksum) prefs.frameInfo.dictID = D.EXPECTED_ID context = C.c_void_p() checked(lib.LZ4F_createCompressionContext(C.byref(context), 100)) dictionary = lib.LZ4F_createCDict(D.DICTIONARY, len(D.DICTIONARY)) try: require(bool(dictionary), "dictionary allocation failed") capacity = checked(lib.LZ4F_compressFrameBound(len(PAYLOAD), C.byref(prefs))) output = C.create_string_buffer(capacity) size = checked(lib.LZ4F_compressFrame_usingCDict( context, output, capacity, PAYLOAD, len(PAYLOAD), dictionary, C.byref(prefs))) return output.raw[:size] finally: if dictionary: lib.LZ4F_freeCDict(dictionary) checked(lib.LZ4F_freeCompressionContext(context)) def layout(frame): require(frame[:4] == bytes.fromhex("04224d18"), "unexpected magic") flags = frame[4] require(flags & 0x10 and flags & 1, "fixture needs block checksum and dictionary ID") position = 7 + (8 if flags & 8 else 0) + 4 header = position ends = [] while True: require(position + 4 <= len(frame), "truncated block length") size = struct.unpack_from("