| 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("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" or kind.startswith("locator64"): |
| 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 | original = data.getvalue() |
| 81 | if kind.startswith("locator64"): |
| 82 | end = original[-22:] |
| 83 | count, central_size, central_at = struct.unpack_from("<HII", end, 10) |
| 84 | record_at = len(original) - 22 |
| 85 | record = bytearray(struct.pack( |
| 86 | "<4sQHHIIQQQQ", b"PK\x06\x06", 44, 45, 45, |
| 87 | 0, 0, count, count, central_size, central_at, |
| 88 | )) |
| 89 | locator = bytearray(struct.pack("<4sIQI", b"PK\x06\x07", 0, record_at, 1)) |
| 90 | if kind == "locator64-bad-offset": |
| 91 | struct.pack_into("<Q", locator, 8, (1 << 64) - 1) |
| 92 | elif kind == "locator64-bad-size": |
| 93 | struct.pack_into("<Q", record, 4, (1 << 64) - 1) |
| 94 | elif kind == "locator64-conflicting-count": |
| 95 | struct.pack_into("<QQ", record, 24, count + 1, count + 1) |
| 96 | elif kind == "locator64-multidisk": |
| 97 | struct.pack_into("<I", locator, 16, 2) |
| 98 | original = original[:-22] + record + locator + end |
| 99 | if kind == "locator64": |
| 100 | assert len(original) == 260 |
| 101 | assert count == 1 and central_size < 0xffffffff and central_at < 0xffffffff |
| 102 | output.write_bytes(original) |
| 103 | if kind in ("descriptor64", "locator64"): |
| 104 | with zipfile.ZipFile(output) as reference: |
| 105 | assert reference.read(fixtures.NAME) == bytes(size) |
| 106 | elif kind == "classic-locator-decoy": |
| 107 | info = zipfile.ZipInfo(fixtures.NAME) |
| 108 | info.comment = struct.pack("<4sIQI", b"PK\x06\x07", 0, 0, 1) |
| 109 | with zipfile.ZipFile(output, "x") as archive: |
| 110 | archive.writestr(info, bytes(size)) |
| 111 | elif kind == "gzip-bomb-joined": |
| 112 | with output.open("xb") as raw: |
| 113 | for level in (6, 0): |
| 114 | with gzip.GzipFile(fileobj=raw, mode="wb", compresslevel=level, mtime=0) as target: |
| 115 | source = fixtures.ZeroStream(size) |
| 116 | while chunk := source.read(fixtures.CHUNK): |
| 117 | target.write(chunk) |
| 118 | elif kind == "store": |
| 119 | fixtures.write_zip(output, size, False) |
| 120 | elif kind in ("deflate", "bomb", "zip64"): |
| 121 | info = zipfile.ZipInfo(fixtures.NAME) |
| 122 | info.file_size = size |
| 123 | info.compress_type = zipfile.ZIP_DEFLATED |
| 124 | info._compresslevel = 6 if kind == "bomb" else 1 |
| 125 | with zipfile.ZipFile(output, "x") as archive: |
| 126 | with archive.open(info, "w", force_zip64=(kind == "zip64")) as target: |
| 127 | source = fixtures.ZeroStream(size) |
| 128 | while chunk := source.read(fixtures.CHUNK): |
| 129 | target.write(chunk) |
| 130 | else: |
| 131 | with output.open("xb") as raw: |
| 132 | if kind == "tar": |
| 133 | target = raw |
| 134 | else: |
| 135 | target = gzip.GzipFile(filename="", fileobj=raw, mode="wb", compresslevel=1, mtime=0) |
| 136 | if kind in ("tar", "targz"): |
| 137 | info = tarfile.TarInfo(fixtures.NAME) |
| 138 | info.size = size |
| 139 | info.mode = 0o600 |
| 140 | target.write(info.tobuf(format=tarfile.USTAR_FORMAT)) |
| 141 | source = fixtures.ZeroStream(size) |
| 142 | while chunk := source.read(fixtures.CHUNK): |
| 143 | target.write(chunk) |
| 144 | if kind in ("tar", "targz"): |
| 145 | target.write(bytes((-size) % 512 + 1024)) |
| 146 | if target is not raw: |
| 147 | target.close() |
| 148 | |
| 149 | |
| 150 | if __name__ == "__main__": |
| 151 | generate(sys.argv[1], sys.argv[2], int(sys.argv[3])) |