"""Synthetic PNG generation (stdlib only) for student tests and experiments.""" from __future__ import annotations import os import struct import zlib from pathlib import Path FIXTURES = Path(__file__).resolve().parent / "fixtures" PUBLIC_PNG = Path(__file__).resolve().parents[1] / "tests" / "fixtures" / "sample.png" ADAM7 = ((0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)) def chunk(kind: bytes, data: bytes) -> bytes: return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF) def png_from_raw(width: int, height: int, raw: bytes, *, depth: int = 8, color: int = 2, interlace: int = 0, level: int = 6, extra: bytes = b"", plte: bytes = b"") -> bytes: ihdr = struct.pack(">IIBBBBB", width, height, depth, color, 0, 0, interlace) body = chunk(b"IHDR", ihdr) + extra if plte: body += chunk(b"PLTE", plte) return b"\x89PNG\r\n\x1a\n" + body + chunk(b"IDAT", zlib.compress(raw, level)) + chunk(b"IEND", b"") def rgb_png(width: int, height: int, *, noise: bool = False, level: int = 6) -> bytes: if noise: rows = [b"\x00" + os.urandom(width * 3) for _ in range(height)] else: rows = [b"\x00" + bytes(((x + y) * 7) % 256 for x in range(width * 3)) for y in range(height)] return png_from_raw(width, height, b"".join(rows), level=level) def interlaced_rgb_png(width: int, height: int) -> bytes: def pixel(x: int, y: int) -> bytes: return bytes(((x * 31) % 256, (y * 17) % 256, ((x + y) * 5) % 256)) raw = bytearray() for x0, y0, dx, dy in ADAM7: xs = list(range(x0, width, dx)) ys = list(range(y0, height, dy)) if not xs or not ys: continue for y in ys: raw += b"\x00" + b"".join(pixel(x, y) for x in xs) return png_from_raw(width, height, bytes(raw), interlace=1) def gray1_png(width: int, height: int) -> bytes: """1-bit grayscale: huge pixel counts in a tiny, perfectly valid file.""" row = b"\x00" + b"\x55" * ((width + 7) // 8) return png_from_raw(width, height, row * height, depth=1, color=0, level=9) def palette_png(width: int, height: int) -> bytes: raw = b"".join(b"\x00" + bytes((x + y) % 4 for x in range(width)) for y in range(height)) return png_from_raw(width, height, raw, depth=8, color=3, plte=bytes(range(12))) def png16_rgba(width: int, height: int) -> bytes: raw = b"".join(b"\x00" + bytes((x * 3 + y) % 256 for x in range(width * 8)) for y in range(height)) return png_from_raw(width, height, raw, depth=16, color=6) def large_png(target_bytes: int) -> bytes: """Incompressible RGB PNG of roughly target_bytes (stored with zlib level 0).""" width = 1024 row = 1 + width * 3 height = max(1, target_bytes // row) return rgb_png(width, height, noise=True, level=0) def exact_size_png(total_bytes: int) -> bytes: """Valid PNG of exactly total_bytes: large_png padded with a tEXt chunk before IEND.""" base = large_png(total_bytes - 64 * 1024) pad = total_bytes - len(base) - 12 - len(b"Comment\x00") assert pad >= 0 text = chunk(b"tEXt", b"Comment\x00" + b"x" * pad) data = base[:-12] + text + base[-12:] assert len(data) == total_bytes return data def unknown_critical_chunk_png() -> bytes: return png_from_raw(2, 2, b"\x00" + b"\x10" * 6 + b"\x00" + b"\x20" * 6, extra=chunk(b"ABCD", b"x")) def fixture(name: str) -> bytes: return (FIXTURES / name).read_bytes() def all_filters_png(width: int, height: int, *, interlace: int = 0) -> bytes: """Valid PNG whose scanlines cycle through filter types 0..4 (any bytes unfilter fine).""" passes = ADAM7 if interlace else ((0, 0, 1, 1),) raw = bytearray() row_no = 0 for x0, y0, dx, dy in passes: pw = len(range(x0, width, dx)) ph = len(range(y0, height, dy)) if not pw or not ph: continue for _ in range(ph): raw.append(row_no % 5) raw += bytes((row_no * 13 + i) % 256 for i in range(pw * 3)) row_no += 1 return png_from_raw(width, height, bytes(raw), interlace=interlace) def _with_bad_idat(png: bytes) -> bytes: """Same PNG, IDAT payload damaged in the middle, CRC recomputed (valid chunk framing).""" i = png.index(b"IDAT") length = struct.unpack(">I", png[i - 4:i])[0] data = bytearray(png[i + 4:i + 4 + length]) data[len(data) // 2] ^= 0xFF data[len(data) // 2 + 1] ^= 0x55 return png[:i - 4] + chunk(b"IDAT", bytes(data)) + chunk(b"IEND", b"") def corrupted_pngs() -> dict: """Inputs used by reviewers with an independent decoder: all must be 415.""" public = PUBLIC_PNG.read_bytes() raw = bytearray() for x0, y0, dx, dy in ADAM7: pw, ph = len(range(x0, 9, dx)), len(range(y0, 9, dy)) for _ in range(ph if pw else 0): raw += b"\x00" + bytes(pw * 3) raw[-(1 + 9 * 3)] = 7 # last scanline of the last Adam7 pass return { "first_33_bytes_of_sample_png": public[:33], "sample_png_truncated_half": public[: len(public) // 2], "broken_idat_stream_valid_crc": _with_bad_idat(rgb_png(64, 64)), "filter_byte_05": png_from_raw(1, 1, b"\x05\x00\x00\x00"), "filter_byte_ff": png_from_raw(1, 1, b"\xff\x00\x00\x00"), "interlaced_filter_byte_07": png_from_raw(9, 9, bytes(raw), interlace=1), "plte_257_entries": png_from_raw(1, 1, b"\x00\x00", color=3, plte=bytes(771)), "plte_not_multiple_of_3": png_from_raw(1, 1, b"\x00\x00", color=3, plte=bytes(4)), "plte_too_long_for_1bit": png_from_raw(1, 1, b"\x00\x00", depth=1, color=3, plte=bytes(9)), "plte_in_grayscale": png_from_raw(1, 1, b"\x00\x00", depth=8, color=0, plte=bytes(3)), "unknown_critical_chunk": unknown_critical_chunk_png(), } def corrupted_jpegs() -> dict: sample = fixture("sample.jpg") progressive = fixture("progressive.jpg") sos = sample.index(b"\xff\xda") sos_len = struct.unpack(">H", sample[sos + 2:sos + 4])[0] twelve_bit = bytearray(sample) twelve_bit[sample.index(b"\xff\xc0") + 4] = 12 return { "jpeg_27_bytes_no_tables": bytes.fromhex("ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9"), "jpeg_18_bytes": bytes.fromhex("ffd8ffc00008080001000101ffda0002ffd9"), "jpeg_no_data_after_sos": sample[: sos + 2 + sos_len] + b"\xff\xd9", "jpeg_half_plus_eoi": sample[: len(sample) // 2] + b"\xff\xd9", "progressive_half_plus_eoi": progressive[: len(progressive) // 2] + b"\xff\xd9", "jpeg_truncated_no_eoi": sample[:-200], "jpeg_12_bit": bytes(twelve_bit), }