diff options
author | Daniel Friesel <derf@finalrewind.org> | 2021-01-24 13:23:05 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2021-01-24 13:23:05 +0100 |
commit | 3e69313f789834c6e83bec187fc7cb7d4dbfe919 (patch) | |
tree | 4d78f3251d4cd80c642ccc6481091050c454f112 /src/app/deflatetest/deflate.py | |
parent | fbbb4f04074c7f21d3b334b01db87f88920db18e (diff) |
deflatetest: Move data to separate file; add deflate helper
Diffstat (limited to 'src/app/deflatetest/deflate.py')
-rwxr-xr-x | src/app/deflatetest/deflate.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/app/deflatetest/deflate.py b/src/app/deflatetest/deflate.py new file mode 100755 index 0000000..3b1d9bd --- /dev/null +++ b/src/app/deflatetest/deflate.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import sys +import zlib + +level = -1 +if len(sys.argv) > 2: + level = int(sys.argv[2]) + +input_data = sys.argv[1].encode("utf-8") + +try: + with open(sys.argv[1], "rb") as f: + input_data = f.read() +except FileNotFoundError: + input_data = sys.argv[1].encode("utf-8") + +output = zlib.compress(input_data, level=level) + +cinfo = (output[0] & 0xF0) >> 4 +window_size = 1 << (8 + cinfo) +print(f"// CINFO = {cinfo} (window size = {window_size})") + +fdict = output[1] & 0x20 +if fdict: + print("// FDICT = 1 (preset dictionary in use)") +else: + print("// FDICT = 0 (no preset dictionary)") + +is_final = output[2] & 0x01 +block_type = (output[2] & 0x06) >> 1 + +print(f"// first Huffman block: type={block_type} is_final={is_final}") + +print(f"// {sys.argv[1]}") +print("unsigned char const inflate_input[] = {" + ", ".join(map(str, output)) + "};") +print(f"unsigned short const inflate_input_size = {len(output)};") |