team-ljyzipelo

공개
123줄5.5 KB
1"""공용 생성기의 고정 버퍼를 재사용해 스트리밍 회귀 표본을 만듦."""
2import gzip
3import importlib.util
4from pathlib import Path
5import sys
6import struct
7import io
8import tarfile
9import zipfile
10
11spec = importlib.util.spec_from_file_location(
12 "large_fixtures", Path(__file__).resolve().parents[1] / "large-archive-fixtures/generate.py"
13)
14fixtures = importlib.util.module_from_spec(spec)
15spec.loader.exec_module(fixtures)
16
17
18def generate(directory, kind, size):
19 directory = Path(directory)
20 output = directory / "input"
21 if kind.startswith("offset64-descriptor"):
22 class Pipe(io.BytesIO):
23 def seekable(self):
24 return False
25 def seek(self, *args):
26 raise io.UnsupportedOperation()
27 data = Pipe()
28 with zipfile.ZipFile(data, "w", compression=zipfile.ZIP_STORED) as archive:
29 archive.writestr(fixtures.NAME, bytes(size))
30 original = data.getvalue()
31 central_at = struct.unpack_from("<I", original, len(original) - 6)[0]
32 body = bytearray(original[:central_at])
33 struct.pack_into("<H", body, 4, 45)
34 central = bytearray(original[central_at:-22])
35 struct.pack_into("<H", central, 6, 45)
36 struct.pack_into("<H", central, 30, 12)
37 struct.pack_into("<I", central, 42, 0xffffffff)
38 central += struct.pack("<HHQ", 1, 8, 0)
39 if kind.endswith("unsigned"):
40 assert body[-16:-12] == b"PK\x07\x08"
41 body = body[:-16] + body[-12:]
42 end = bytearray(original[-22:])
43 struct.pack_into("<II", end, 12, len(central), len(body))
44 output.write_bytes(body + central + end)
45 with zipfile.ZipFile(output) as reference:
46 assert reference.read(fixtures.NAME) == bytes(size)
47 elif kind.startswith("central64"):
48 data = io.BytesIO()
49 with zipfile.ZipFile(data, "w", compression=zipfile.ZIP_STORED) as archive:
50 archive.writestr(fixtures.NAME, bytes(size))
51 original = data.getvalue()
52 end = original[-22:]
53 central_at = struct.unpack_from("<I", end, 16)[0]
54 central = bytearray(original[central_at:-22])
55 name_len = struct.unpack_from("<H", central, 28)[0]
56 struct.pack_into("<III", central, 16, struct.unpack_from("<I", central, 16)[0], 0xffffffff, 0xffffffff)
57 struct.pack_into("<H", central, 30, 28)
58 struct.pack_into("<I", central, 42, 0xffffffff)
59 declared = (1 << 64) - 1 if kind == "central64-size-overflow" else size
60 offset = (1 << 64) - 1 if kind == "central64-offset-overflow" else 0
61 central = central[:46 + name_len] + struct.pack("<HHQQQ", 1, 24, declared, size, offset)
62 directory = original[:central_at] + central
63 record = struct.pack("<4sQHHIIQQQQ", b"PK\x06\x06", 44, 45, 45, 0, 0, 1, 1, len(central), central_at)
64 locator = struct.pack("<4sIQI", b"PK\x06\x07", 0, len(directory), 1)
65 end = struct.pack("<4sHHHHIIH", b"PK\x05\x06", 0, 0, 65535, 65535, 0xffffffff, 0xffffffff, 0)
66 output.write_bytes(directory + record + locator + end)
67 if kind == "central64":
68 with zipfile.ZipFile(output) as reference:
69 assert reference.read(fixtures.NAME) == bytes(size)
70 elif kind == "descriptor64":
71 class Pipe(io.BytesIO):
72 def seekable(self):
73 return False
74 def seek(self, *args):
75 raise io.UnsupportedOperation()
76 data = Pipe()
77 with zipfile.ZipFile(data, "w", compression=zipfile.ZIP_STORED) as archive:
78 with archive.open(fixtures.NAME, "w", force_zip64=True) as target:
79 target.write(bytes(size))
80 output.write_bytes(data.getvalue())
81 with zipfile.ZipFile(output) as reference:
82 assert reference.read(fixtures.NAME) == bytes(size)
83 elif kind == "gzip-bomb-joined":
84 with output.open("xb") as raw:
85 for level in (6, 0):
86 with gzip.GzipFile(fileobj=raw, mode="wb", compresslevel=level, mtime=0) as target:
87 source = fixtures.ZeroStream(size)
88 while chunk := source.read(fixtures.CHUNK):
89 target.write(chunk)
90 elif kind == "store":
91 fixtures.write_zip(output, size, False)
92 elif kind in ("deflate", "bomb", "zip64"):
93 info = zipfile.ZipInfo(fixtures.NAME)
94 info.file_size = size
95 info.compress_type = zipfile.ZIP_DEFLATED
96 info._compresslevel = 6 if kind == "bomb" else 1
97 with zipfile.ZipFile(output, "x") as archive:
98 with archive.open(info, "w", force_zip64=(kind == "zip64")) as target:
99 source = fixtures.ZeroStream(size)
100 while chunk := source.read(fixtures.CHUNK):
101 target.write(chunk)
102 else:
103 with output.open("xb") as raw:
104 if kind == "tar":
105 target = raw
106 else:
107 target = gzip.GzipFile(filename="", fileobj=raw, mode="wb", compresslevel=1, mtime=0)
108 if kind in ("tar", "targz"):
109 info = tarfile.TarInfo(fixtures.NAME)
110 info.size = size
111 info.mode = 0o600
112 target.write(info.tobuf(format=tarfile.USTAR_FORMAT))
113 source = fixtures.ZeroStream(size)
114 while chunk := source.read(fixtures.CHUNK):
115 target.write(chunk)
116 if kind in ("tar", "targz"):
117 target.write(bytes((-size) % 512 + 1024))
118 if target is not raw:
119 target.close()
120
121
122if __name__ == "__main__":
123 generate(sys.argv[1], sys.argv[2], int(sys.argv[3]))