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