Last active
August 2, 2019 19:26
-
-
Save Centrinia/4efdbe22bc0695a4147daa763060774d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import struct | |
def main(): | |
# Produce a file with 45899235*repeats of zero bytes | |
if len(sys.argv) <= 1: | |
print('Usage zero_bz2.py filename [repeats]') | |
return | |
filename = sys.argv[1] | |
repeats = 2**10 | |
if len(sys.argv) > 2: | |
repeats = int(sys.argv[2]) | |
header = b'\x42\x5a\x68\x39' | |
block = b'\x31\x41\x59\x26\x53\x59\x0e\x09\xe2\xdf\x01\x5f\x8e\x40\x00\xc0\x00\x00\x08\x20\x00\x30\x80\x4d\x46\x42\xa0\x25\xa9\x0a\x80\x97' | |
block_crc = int.from_bytes(block[6:10], 'big') | |
footer_magic = b'\x17\x72\x45\x38\x50\x90' | |
def compute_stream_crc(block_crc, repeats): | |
stream_crc = 0 | |
def f(x): | |
return (block_crc ^ ((x << 1) | (x >> 31))) & 0xffffffff | |
for k in range(repeats % 32): | |
stream_crc = f(stream_crc) | |
return stream_crc & 0xffffffff | |
stream_crc = compute_stream_crc(block_crc, repeats) | |
with open(filename, 'wb') as f: | |
f.write(header) | |
for _ in range(repeats): | |
f.write(block) | |
f.write(footer_magic) | |
f.write(stream_crc.to_bytes(4, byteorder='big')) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment