| 1 | import argparse |
| 2 | import hashlib |
| 3 | import io |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | import struct |
| 7 | import zipfile |
| 8 | |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parent |
| 11 | SOURCES = [ |
| 12 | ("hello.txt", b"Zipelo recovery reference\n", zipfile.ZIP_STORED), |
| 13 | ("한글/자료.txt", "복구할 한글 데이터\n".encode("utf-8") * 16, zipfile.ZIP_DEFLATED), |
| 14 | ("empty.txt", b"", zipfile.ZIP_STORED), |
| 15 | ("empty-dir/", b"", zipfile.ZIP_STORED), |
| 16 | ("middle.bin", bytes(range(256)) * 4, zipfile.ZIP_STORED), |
| 17 | ("after.txt", b"Intact entry after the damaged payload\n", zipfile.ZIP_DEFLATED), |
| 18 | ] |
| 19 | |
| 20 | |
| 21 | def sha256(data): |
| 22 | return hashlib.sha256(data).hexdigest() |
| 23 | |
| 24 | |
| 25 | def corpus(): |
| 26 | stream = io.BytesIO() |
| 27 | with zipfile.ZipFile(stream, "w") as archive: |
| 28 | for name, data, method in SOURCES: |
| 29 | info = zipfile.ZipInfo(name, date_time=(2020, 1, 2, 3, 4, 6)) |
| 30 | info.create_system = 3 |
| 31 | info.external_attr = (0o40755 if name.endswith("/") else 0o100644) << 16 |
| 32 | if name.endswith("/"): |
| 33 | info.external_attr |= 0x10 |
| 34 | info.compress_type = method |
| 35 | archive.writestr(info, data, compresslevel=6) |
| 36 | original = stream.getvalue() |
| 37 | with zipfile.ZipFile(io.BytesIO(original)) as archive: |
| 38 | directory_offset = archive.start_dir |
| 39 | entries = [] |
| 40 | for info, (_, data, _) in zip(archive.infolist(), SOURCES): |
| 41 | name_size, extra_size = struct.unpack_from("<HH", original, info.header_offset + 26) |
| 42 | data_offset = info.header_offset + 30 + name_size + extra_size |
| 43 | entries.append({ |
| 44 | "name": info.filename, |
| 45 | "kind": "directory" if info.is_dir() else "file", |
| 46 | "bytes": len(data), |
| 47 | "sha256": sha256(data), |
| 48 | "crc32": f"{info.CRC:08x}", |
| 49 | "method": info.compress_type, |
| 50 | "local_offset": info.header_offset, |
| 51 | "data_offset": data_offset, |
| 52 | "compressed_bytes": info.compress_size, |
| 53 | }) |
| 54 | middle = entries[4] |
| 55 | cut = middle["data_offset"] + middle["compressed_bytes"] // 2 |
| 56 | corrupt_offset = middle["data_offset"] + 17 |
| 57 | corrupt = bytearray(original[:directory_offset]) |
| 58 | corrupt[corrupt_offset] ^= 0x80 |
| 59 | bad_eocd = bytearray(original) |
| 60 | bad_eocd[-22:-18] = b"BAD!" |
| 61 | local_offset = entries[1]["local_offset"] |
| 62 | bad_local = bytearray(original) |
| 63 | bad_local[local_offset:local_offset + 4] = b"BAD!" |
| 64 | cases = [ |
| 65 | ("control.zip", "control", original, [], ["verified"] * 6), |
| 66 | ("missing-directory.zip", "header_damage", original[:directory_offset], |
| 67 | [{"operation": "truncate", "offset": directory_offset}], ["verified"] * 6), |
| 68 | ("broken-eocd.zip", "header_damage", bytes(bad_eocd), |
| 69 | [{"operation": "replace", "offset": len(original) - 22, "hex": "42414421"}], |
| 70 | ["verified"] * 6), |
| 71 | ("middle-truncated.zip", "middle_truncation", original[:cut], |
| 72 | [{"operation": "truncate", "offset": cut}], |
| 73 | ["verified"] * 4 + ["truncated", "not_observable"]), |
| 74 | ("body-corrupt.zip", "payload_corruption", bytes(corrupt), |
| 75 | [{"operation": "truncate", "offset": directory_offset}, |
| 76 | {"operation": "xor", "offset": corrupt_offset, "value": 128}], |
| 77 | ["verified"] * 4 + ["integrity_failed", "verified"]), |
| 78 | ("unrecoverable.zip", "unrecoverable", original[:20], |
| 79 | [{"operation": "truncate", "offset": 20}], ["not_observable"] * 6), |
| 80 | ("broken-local-header.zip", "header_damage", bytes(bad_local), |
| 81 | [{"operation": "replace", "offset": local_offset, "hex": "42414421"}], |
| 82 | ["verified"] * 6), |
| 83 | ] |
| 84 | files = {} |
| 85 | samples = [] |
| 86 | for name, category, data, mutations, states in cases: |
| 87 | verified = [entry["name"] for entry, state in zip(entries, states) if state == "verified"] |
| 88 | samples.append({ |
| 89 | "file": name, |
| 90 | "category": category, |
| 91 | "bytes": len(data), |
| 92 | "sha256": sha256(data), |
| 93 | "mutations_from_control": mutations, |
| 94 | "expected": { |
| 95 | "publish_archive": bool(verified) and category != "control", |
| 96 | "warning_required": category != "control" and name != "broken-local-header.zip", |
| 97 | "must_not_claim_complete_original": category != "control" and name != "broken-local-header.zip", |
| 98 | "must_preserve_source_sha256": sha256(data), |
| 99 | "verified_entries": verified, |
| 100 | "excluded_entries": [entry["name"] for entry, state in zip(entries, states) |
| 101 | if state != "verified"], |
| 102 | "oracle_states": dict(zip((entry["name"] for entry in entries), states)), |
| 103 | }, |
| 104 | }) |
| 105 | if name == "broken-local-header.zip": |
| 106 | samples[-1]["expected"]["recovery_summary"] = { |
| 107 | "mode": "CentralDirectory", |
| 108 | "known_entries": 6, |
| 109 | "verified_entries": 6, |
| 110 | "lost_entries": 0, |
| 111 | "unverified_entries": 0, |
| 112 | "unknown_loss": False, |
| 113 | } |
| 114 | samples[-1]["expected"]["terminal_after_successful_publication"] = { |
| 115 | "outcome": "Completed", |
| 116 | "error": "None", |
| 117 | "publication": "Complete", |
| 118 | "created_count": 1, |
| 119 | } |
| 120 | files[name] = data |
| 121 | manifest = { |
| 122 | "schema_version": 1, |
| 123 | "worker_execution_verified": False, |
| 124 | "description": "자체 생성 표본임. oracle_states는 원본을 아는 시험의 기대값이며 제품 보고 계약이 아님", |
| 125 | "source_entries": entries, |
| 126 | "samples": samples, |
| 127 | } |
| 128 | files["expectations.json"] = (json.dumps(manifest, ensure_ascii=False, indent=4) + "\n").encode() |
| 129 | return files |
| 130 | |
| 131 | |
| 132 | def main(): |
| 133 | parser = argparse.ArgumentParser() |
| 134 | parser.add_argument("--check", action="store_true") |
| 135 | args = parser.parse_args() |
| 136 | files = corpus() |
| 137 | if args.check: |
| 138 | for name, data in files.items(): |
| 139 | if (ROOT / name).read_bytes() != data: |
| 140 | raise SystemExit(f"재생성 불일치: {name}") |
| 141 | print(f"재생성 일치: {len(files) - 1}개 ZIP과 기대값") |
| 142 | else: |
| 143 | for name, data in files.items(): |
| 144 | (ROOT / name).write_bytes(data) |
| 145 | |
| 146 | |
| 147 | if __name__ == "__main__": |
| 148 | main() |