team-ljyzipelo

공개
70줄2.5 KB
1import gzip
2import hashlib
3import json
4import pathlib
5import platform
6import struct
7import zlib
8
9BASE = pathlib.Path(__file__).resolve().parent
10
11
12def member(payload, flags=0, raw=None, name=b"../../escape", comment=b"note", extra=b"\x00\xff\x07"):
13 header = bytearray(b"\x1f\x8b\x08" + bytes([flags]) + b"\0\0\0\0\0\xff")
14 if flags & 4:
15 header += struct.pack("<H", len(extra)) + extra
16 if flags & 8:
17 header += name + b"\0"
18 if flags & 16:
19 header += comment + b"\0"
20 if flags & 2:
21 header += struct.pack("<H", zlib.crc32(header) & 0xffff)
22 if raw is None:
23 encoder = zlib.compressobj(6, zlib.DEFLATED, -15)
24 raw = encoder.compress(payload) + encoder.flush()
25 trailer = struct.pack("<II", zlib.crc32(payload), len(payload) & 0xffffffff)
26 return bytes(header) + raw + trailer, len(header), len(raw)
27
28
29def produce():
30 records = {}
31 lines = []
32
33 def add(key, payload, **kwargs):
34 archive, header, raw = member(payload, **kwargs)
35 assert gzip.decompress(archive) == payload
36 lines.append(key + ":" + archive.hex())
37 records[key] = {
38 "archive_bytes": len(archive),
39 "header_bytes": header,
40 "deflate_bytes": raw,
41 "payload_bytes": len(payload),
42 "crc32": zlib.crc32(payload),
43 "archive_sha256": hashlib.sha256(archive).hexdigest(),
44 "payload_sha256": hashlib.sha256(payload).hexdigest(),
45 }
46
47 for flags in range(32):
48 add(f"flags_{flags:02}", b"123456789", flags=flags)
49 add("empty_fixed", b"", raw=bytes.fromhex("0300"))
50 add("empty_stored", b"", raw=bytes.fromhex("010000ffff"))
51 add("zeros_three", b"\0\0\0")
52 add("byte_a", b"A", raw=bytes.fromhex("010100feff41"))
53 add("opaque_name", b"123456789", flags=26, name=b"\xff../x", comment=b"\xfe")
54 add("empty_options", b"123456789", flags=30, name=b"", comment=b"", extra=b"")
55 add("large_output", b"A" * 200003)
56 encoder = zlib.compressobj(0, zlib.DEFLATED, -15)
57 payload = bytes(i % 251 for i in range(65537))
58 add("multiple_input", payload, raw=encoder.compress(payload) + encoder.flush())
59 return "\n".join(lines) + "\n", {
60 "generator": "Python standard gzip/zlib; synthetic known payloads only",
61 "python": platform.python_version(),
62 "zlib_runtime": zlib.ZLIB_RUNTIME_VERSION,
63 "fixtures": records,
64 }
65
66
67if __name__ == "__main__":
68 data, manifest = produce()
69 (BASE / "fixtures.hex").write_text(data)
70 (BASE / "fixtures.json").write_text(json.dumps(manifest, indent=4) + "\n")