Created
December 25, 2025 00:06
-
-
Save cppio/ead28f6c92cf9b998c653b755c33510f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import struct, sys, zlib | |
| PNG_HEADER = b"\x89PNG\r\n\x1a\n" | |
| for path in sys.argv[1:]: | |
| with open(path, "rb") as png: | |
| png = png.read() | |
| print(path + ":") | |
| if not png.startswith(PNG_HEADER): | |
| print("not a png") | |
| continue | |
| png = memoryview(png)[len(PNG_HEADER):] | |
| while png: | |
| length, chunk_type = struct.unpack_from("!I4s", png) | |
| print(chunk_type.decode() + ":", length, "bytes") | |
| expected, actual = png[8 + length:12 + length], zlib.crc32(png[4:8 + length]).to_bytes(4) | |
| if expected != actual: | |
| print(f"expected {expected.hex()}, got {actual.hex()}") | |
| png = png[12 + length:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment