team-ljyzipelo

공개
94줄3.6 KB
1"""고정 버퍼로 대용량 7z 표본을 생성함."""
2import lzma
3from pathlib import Path
4import shutil
5import struct
6import sys
7import zlib
8
9from seven_zip_fixtures import integer, property_bytes, SIGNATURE
10
11
12def generate(root, method, size, declared=None):
13 root = Path(root)
14 packed_path = root / "packed"
15 settings = {
16 "id": lzma.FILTER_LZMA2 if method == "lzma2" else lzma.FILTER_LZMA1,
17 "dict_size": 64 * 1024 * 1024,
18 }
19 compressor = None if method == "copy" else lzma.LZMACompressor(
20 format=lzma.FORMAT_RAW, filters=[settings]
21 )
22 crc = 0
23 packed_crc = 0
24 packed_size = 0
25 with packed_path.open("xb") as output:
26 remaining = size
27 while remaining:
28 chunk = bytes(min(65536, remaining))
29 crc = zlib.crc32(chunk, crc)
30 encoded = chunk if compressor is None else compressor.compress(chunk)
31 output.write(encoded)
32 packed_crc = zlib.crc32(encoded, packed_crc)
33 packed_size += len(encoded)
34 remaining -= len(chunk)
35 if compressor is not None:
36 encoded = compressor.flush()
37 output.write(encoded)
38 packed_crc = zlib.crc32(encoded, packed_crc)
39 packed_size += len(encoded)
40 if method == "copy":
41 coder = b"\x01\x00"
42 elif method == "lzma":
43 coder = b"\x23\x03\x01\x01\x05\x5d" + struct.pack("<I", 64 * 1024 * 1024)
44 else:
45 coder = b"\x21\x21\x01\x1c"
46 info = b"\x06\x00\x01\x09" + integer(packed_size)
47 info += b"\x0a\x01" + struct.pack("<I", packed_crc) + b"\x00"
48 info += b"\x07\x0b\x01\x00\x01" + coder + b"\x0c" + integer(size if declared is None else declared)
49 info += b"\x0a\x01" + struct.pack("<I", crc) + b"\x00\x08\x00\x00"
50 name = "payload.bin".encode("utf-16le") + b"\x00\x00"
51 header = b"\x01\x04" + info + b"\x05\x01" + property_bytes(17, b"\x00" + name) + b"\x00\x00"
52 start = struct.pack("<QQI", packed_size, len(header), zlib.crc32(header))
53 with (root / "input").open("xb") as output, packed_path.open("rb") as packed:
54 output.write(SIGNATURE + struct.pack("<I", zlib.crc32(start)) + start)
55 shutil.copyfileobj(packed, output, 65536)
56 output.write(header)
57 packed_path.unlink()
58 if declared is None and sys.platform == "linux":
59 verify_zero(root / "input", size)
60
61
62
63def verify_zero(path, size):
64 import ctypes as c
65 from seven_zip_reference import library
66 lib = library()
67 lib.archive_entry_size.argtypes = [c.c_void_p]
68 lib.archive_entry_size.restype = c.c_longlong
69 archive = lib.archive_read_new()
70 try:
71 assert lib.archive_read_support_filter_all(archive) == 0
72 assert lib.archive_read_support_format_7zip(archive) == 0
73 assert lib.archive_read_open_filename(archive, str(path).encode(), 65536) == 0
74 entry = c.c_void_p()
75 assert lib.archive_read_next_header(archive, c.byref(entry)) == 0
76 assert lib.archive_entry_pathname(entry) == b"payload.bin"
77 assert lib.archive_entry_size(entry) == size
78 buffer = c.create_string_buffer(65536)
79 total = 0
80 while True:
81 n = lib.archive_read_data(archive, buffer, len(buffer))
82 assert n >= 0, lib.archive_error_string(archive)
83 if not n:
84 break
85 assert buffer.raw[:n] == bytes(n)
86 total += n
87 assert total == size
88 assert lib.archive_read_next_header(archive, c.byref(entry)) == 1
89 finally:
90 lib.archive_read_free(archive)
91
92if __name__ == "__main__":
93 generate(sys.argv[1], sys.argv[2], int(sys.argv[3]),
94 int(sys.argv[4]) if len(sys.argv) > 4 else None)