"""Synthetic images for the lab 4 tests: valid PNG/JPEG of any size, and the broken files the reviewer used against other solutions (truncated, corrupted IDAT with a valid CRC, JPEG without scan data, decompression-bomb headers, MIME spoofing). Nothing big is stored in Git: every file is generated in memory.""" from __future__ import annotations import io import os import struct import zlib from PIL import Image PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n" def chunk(ctype: bytes, data: bytes) -> bytes: return struct.pack(">I", len(data)) + ctype + data + struct.pack(">I", zlib.crc32(ctype + data)) def ihdr(width: int, height: int, depth: int = 8, color: int = 2, interlace: int = 0) -> bytes: return chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, depth, color, 0, 0, interlace)) def png(width: int = 64, height: int = 48, mode: str = "RGB", noise: bool = False, **save) -> bytes: if noise: channels = len(mode) image = Image.frombytes(mode, (width, height), os.urandom(width * height * channels)) else: image = Image.new(mode, (width, height), (40, 120, 200) if mode == "RGB" else 128) out = io.BytesIO() image.save(out, "PNG", **save) return out.getvalue() def png_of_size(target: int) -> bytes: """A valid PNG of (about, at most) `target` bytes: random pixels do not compress.""" side = int((target / 3) ** 0.5) data = png(side, side, noise=True, compress_level=1) while len(data) > target: side -= 8 data = png(side, side, noise=True, compress_level=1) return data def png_exactly(size: int) -> bytes: """A valid PNG of EXACTLY `size` bytes: noise image + an ancillary padding chunk.""" base = png_of_size(size - 64) pad = size - len(base) - 12 assert pad >= 0 return base[:-12] + chunk(b"tEXt", b"pad\x00" + b"x" * (pad - 4)) + base[-12:] def jpeg(width: int = 64, height: int = 48, progressive: bool = False, noise: bool = False, quality: int = 85) -> bytes: if noise: image = Image.frombytes("RGB", (width, height), os.urandom(width * height * 3)) else: image = Image.new("RGB", (width, height), (200, 30, 30)) out = io.BytesIO() image.save(out, "JPEG", progressive=progressive, quality=quality) return out.getvalue() # ------------------------------------------------------------------ broken files def png_corrupt_idat() -> bytes: """Valid signature, IHDR and CRCs; the IDAT payload is 86 bytes of garbage (reviewer's case).""" return PNG_SIGNATURE + ihdr(20, 20) + chunk(b"IDAT", bytes(range(86))) + chunk(b"IEND", b"") def png_short_pixels() -> bytes: """A complete, CRC-correct PNG whose zlib stream holds fewer rows than the header promises.""" raw = b"".join(b"\x00" + bytes(60) for _ in range(19)) return PNG_SIGNATURE + ihdr(20, 20) + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"") def png_bomb_header(width: int = 20000, height: int = 10000) -> bytes: """Header of a huge picture: must be refused (413) from the first 33 bytes.""" raw_row = b"\x00" + bytes(width * 3) body = zlib.compress(raw_row * 4, 9) # a few rows only: the decoder must never get that far return PNG_SIGNATURE + ihdr(width, height) + chunk(b"IDAT", body) + chunk(b"IEND", b"") def jpeg_without_scan_data() -> bytes: data = jpeg(128, 96) sos = data.index(b"\xff\xda") length = struct.unpack(">H", data[sos + 2:sos + 4])[0] return data[:sos + 2 + length] + b"\xff\xd9" def jpeg_bomb_header() -> bytes: """SOF0 announcing 20000 x 20000 = 400 Mpx.""" return (b"\xff\xd8\xff\xc0\x00\x11\x08" + struct.pack(">HH", 20000, 20000) + b"\x03\x01\x22\x00\x02\x11\x01\x03\x11\x01" + b"\xff\xd9")