Skip to content

Instantly share code, notes, and snippets.

@cppio
Created December 25, 2025 00:06
Show Gist options
  • Select an option

  • Save cppio/ead28f6c92cf9b998c653b755c33510f to your computer and use it in GitHub Desktop.

Select an option

Save cppio/ead28f6c92cf9b998c653b755c33510f to your computer and use it in GitHub Desktop.
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