Files
romm/backend/tests/_embedded_archives.py
MMagTech ac9f5ab505 fix(saves): hash bytes that only end like a zip as one file (#5719)
* fix(saves): hash bytes that only end like a zip as one file

zipfile.is_zipfile is true for any file whose last 64 KB hold a zip's end
record. An emulator state can: Power Stone's Flycast (Naomi) state holds
the tail of the game's own TorrentZip archive. Reading such a file as a
zip fails, so compute_content_hash returned None, and a snapshot push
carrying the state was refused whole with "does not hash to the
manifest's value".

When the bytes cannot be read as a zip (BadZipFile), compute_content_hash,
hash_save_content and the memory card hash now hash them as one file, as
the web player's saveContentHash and the snapshot identity hash already
do. A real zip they cannot read within the limits stays unknown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Hash bytes as one file when the copied central directory points an entry
before the bytes start, as the tail of an archive longer than the state does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: plain-prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): decide what is a zip archive by one rule

Bytes are a zip archive only when their entries start at the first byte.
A state or save holding another archive, whole or only its tail, after
data of its own is hashed, shaped, uploaded and unpacked as one raw file
everywhere: the content hashers, the identity hash, save_shape_of,
check_upload and unpack_save_unit. A real archive with a corrupt entry
stays unknown to the content hashers, and the identity hash falls back to
the bytes for one past the limits too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-22 10:00:16 -05:00

36 lines
1.2 KiB
Python

"""Bytes that end like a zip without being one, as an emulator state can."""
import io
import zipfile
from tests._zipfile_shim import reload_zipfile
# The tail of an archive after a longer lead, the tail after a shorter lead, and
# a whole archive after a lead of its own.
EMBEDDINGS = ["tail-state-longer", "tail-archive-longer", "whole-archive"]
def game_archive() -> bytes:
reload_zipfile()
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr("game.bin", bytes(range(256)) * 64)
zf.writestr("game.cfg", b"region=jp\n" * 16)
zf.comment = b"TORRENTZIPPED-00000000"
return buffer.getvalue()
def embedded_archive(embedding: str) -> bytes:
"""State-like bytes holding `game_archive()` the way `embedding` names."""
archive = game_archive()
with zipfile.ZipFile(io.BytesIO(archive)) as zf:
start_dir = zf.start_dir
tail = archive[start_dir:]
if embedding == "tail-state-longer":
body = b"\x00" * (start_dir + 4096) + tail
elif embedding == "tail-archive-longer":
body = b"\x00" * (start_dir // 2) + tail
else:
body = b"\x00" * 4096 + archive
return body + b"\xff" * 120