Last active
January 7, 2023 18:55
-
-
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)
This file contains 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
#!/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