fix: reject JPEG scans without image data

This commit is contained in:
2026-10-30 18:46:03 +03:00
parent 63378b5cf3
commit a2047b59b2
7 changed files with 143 additions and 7 deletions
+9
View File
@@ -99,3 +99,12 @@ def jpeg_without_eoi(data: bytes) -> bytes:
"""JPEG без маркера конца: обрезаны последние два байта."""
assert data.endswith(b"\xff\xd9")
return data[:-2]
def jpeg_with_scan_data(data: bytes, scan_data: bytes = b"") -> bytes:
"""Заменяет данные единственного скана, сохраняя заголовки и таблицы."""
start = data.index(b"\xff\xda") + 2
(length,) = struct.unpack_from(">H", data, start)
end = data.index(b"\xff\xd9", start + length)
assert b"\xff\xda" not in data[start + length : end]
return data[: start + length] + scan_data + data[end:]