Files
mtusi2026lab5/scripts/lab4/pngkit.py
T
ilya.tyunin 1982bdb690 lab5: базовый коммит из lab4 (abba248)
Перенесено дерево lab4 целиком: NestJS-приложение с вложениями и S3,
Dockerfile, compose.yaml, infra/{haproxy,minio}, стендовые скрипты,
scripts/lab4, evidence/lab4 и contracts/extensions.openapi.json.

Задание lab5 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,examples},
tests, compose.example.yaml, .github/pull_request_template.md.

Адаптации при переносе:
- docker-проект mtusi-lab4 -> mtusi-lab5 и тег образа mtusi-messenger:lab5,
  чтобы тома и контейнеры не пересекались с lab4 (scripts/stop-one.sh:
  LAB_PROJECT вместо LAB4_PROJECT);
- Makefile: цели lab5 сохранены, добавлены стендовые (stand-up/down,
  wait-ready, init-env, test-e2e, test-dependencies, s3-security,
  consistency, bench);
- .env.example: реальные переменные приложения и стенда lab4 (без них
  compose.yaml не поднимается) + блок lab5 (BROKER_URL_FILE, WORKER_*).
2026-11-04 23:44:11 +03:00

79 lines
2.8 KiB
Python

"""Synthetic PNG helpers (stdlib only); same semantics as app/test/support/png.ts."""
import random
import struct
import zlib
SIGNATURE = b"\x89PNG\r\n\x1a\n"
NOISE_WIDTH = 1024
NOISE_ROW = 1 + NOISE_WIDTH * 3
def chunk(kind: bytes, data: bytes) -> bytes:
crc = zlib.crc32(kind + data) & 0xFFFFFFFF
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", crc)
def build_png(width: int, height: int, idat: bytes) -> bytes:
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
return SIGNATURE + chunk(b"IHDR", ihdr) + chunk(b"IDAT", idat) + chunk(b"IEND", b"")
def pad_png(data: bytes, size: int) -> bytes:
"""Insert a private ancillary chunk before IEND to reach exactly `size` bytes."""
extra = size - len(data)
if extra == 0:
return data
if extra < 12:
raise ValueError("padding needs at least 12 bytes")
return data[:-12] + chunk(b"prVt", bytes(extra - 12)) + data[-12:]
def _noise_of_height(height: int, rng: random.Random) -> bytes:
raw = bytearray(rng.randbytes(NOISE_ROW * height))
for row in range(height):
raw[row * NOISE_ROW] = 0 # filter type "none"
compressor = zlib.compressobj(0)
return build_png(NOISE_WIDTH, height, compressor.compress(bytes(raw)) + compressor.flush())
def noise_png(size: int, seed: int = 0) -> bytes:
"""Incompressible, structurally valid PNG of exactly `size` bytes."""
rng = random.Random(seed)
height = max(1, size // NOISE_ROW)
while True:
png = _noise_of_height(height, rng)
if len(png) > size or 0 < size - len(png) < 12:
height -= 1
if height < 1:
raise ValueError("size is too small")
continue
return pad_png(png, size)
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 worst_decode_png(size: int = 10_485_760, width: int = 8000, height: int = 5000) -> bytes:
"""The heaviest valid upload to decode: 40 MP RGBA 16-bit, Adam7-interlaced
(the decoder must hold the whole image), padded to exactly `size` bytes."""
compressor = zlib.compressobj(1)
parts = []
for x0, y0, dx, dy in ADAM7:
pass_width = (width - x0 + dx - 1) // dx if width > x0 else 0
pass_height = (height - y0 + dy - 1) // dy if height > y0 else 0
if pass_width == 0 or pass_height == 0:
continue
row = bytes(1 + pass_width * 8)
for _ in range(pass_height):
parts.append(compressor.compress(row))
parts.append(compressor.flush())
ihdr = struct.pack(">IIBBBBB", width, height, 16, 6, 0, 0, 1)
png = SIGNATURE + chunk(b"IHDR", ihdr) + chunk(b"IDAT", b"".join(parts)) + chunk(b"IEND", b"")
return pad_png(png, size)
if __name__ == "__main__":
import sys
sys.stdout.buffer.write(worst_decode_png())