Skip to content

Instantly share code, notes, and snippets.

@equal-l2
Last active January 7, 2023 18:55
Show Gist options
  • Save equal-l2/05226cada6de3cb29aab2857f2e3f9c8 to your computer and use it in GitHub Desktop.
Save equal-l2/05226cada6de3cb29aab2857f2e3f9c8 to your computer and use it in GitHub Desktop.
Extract data after IEND from PNG (read from stdin, write to stdout)
#!/usr/bin/env python3
import sys
# read all from stdin
data = sys.stdin.buffer.read()
# check header
png_header = b"\x89PNG\x0D\x0A\x1A\x0A"
header_len = len(png_header)
if data[0:header_len] != png_header:
print("The input is not a valid PNG", file=sys.stderr)
sys.exit(1)
# find IEND
iend = b"\x00\x00\x00\x00IEND\xAE\x42\x60\x82"
iend_len = len(iend)
iend_start = data.find(iend)
if iend_start == -1:
print("IEND was not found", file=sys.stderr)
sys.exit(1)
iend_after = iend_start + iend_len
if iend_after == len(data):
print("No extra data after IEND", file=sys.stderr)
sys.exit(1)
# write all data after IEND to stdout
sys.stdout.buffer.write(data[iend_after:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment