diff --git a/scripts/find_midisuite_save.py b/scripts/find_midisuite_save.py new file mode 100644 index 0000000..a7d0230 --- /dev/null +++ b/scripts/find_midisuite_save.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +"""Find how MidiSuite saves an SMC-PAD preset. + +Downloads the current Windows MidiSuite and reads the compiled app. +It prints the save method names and any finished vendor packets stored +in the file. It does not connect to the pad. + + python3 scripts/find_midisuite_save.py + python3 scripts/find_midisuite_save.py --zip ~/MidiSuite_new.zip +""" + +from __future__ import annotations + +import argparse +import io +import sys +import urllib.request +import zipfile +from pathlib import Path + +MIDISUITE_URL = "https://yms-file-store.oss-cn-hongkong.aliyuncs.com/software/pc/MidiSuite_new.zip" +APP_SO = "Midi Suite/data/app.so" + +# Names compiled into MidiSuite's SMC-PAD save path. +SAVE_NAMES = ( + "flashSave0", + "save0Packet", + "flashWrite", + "flashRead", + "flashErase", + "smc_pad_desktop_save", +) + +READ_COMMAND = 0x23 +WRITE_COMMAND = 0x22 + + +def strings(blob: bytes, shortest: int = 4) -> list[str]: + found: list[str] = [] + current = bytearray() + for byte in blob: + if 32 <= byte < 127: + current.append(byte) + continue + if len(current) >= shortest: + found.append(current.decode()) + current = bytearray() + if len(current) >= shortest: + found.append(current.decode()) + return found + + +def save_names(blob: bytes) -> list[str]: + present = set(strings(blob)) + return [name for name in SAVE_NAMES if name in present] + + +def literal_packets(blob: bytes) -> list[tuple[int, int]]: + """Offsets of stored ``00 59`` read or write packets.""" + found: list[tuple[int, int]] = [] + start = 0 + marker = b"\x00\x59" + while True: + index = blob.find(marker, start) + if index < 0 or index + 2 >= len(blob): + break + command = blob[index + 2] + if command in (READ_COMMAND, WRITE_COMMAND): + found.append((index, command)) + start = index + 1 + return found + + +def app_so(package: bytes) -> bytes: + with zipfile.ZipFile(io.BytesIO(package)) as archive: + try: + return archive.read(APP_SO) + except KeyError: + names = [name for name in archive.namelist() if name.endswith("app.so")] + if not names: + raise SystemExit("MidiSuite zip has no app.so") + return archive.read(names[0]) + + +def report(blob: bytes) -> str: + names = save_names(blob) + packets = literal_packets(blob) + lines = ["MidiSuite save names:"] + if names: + lines.extend(f" {name}" for name in names) + else: + lines.append(" none") + lines.append("Stored 00 59 read/write packets:") + if packets: + for offset, command in packets: + kind = "write" if command == WRITE_COMMAND else "read" + lines.append(f" {offset:#x} command {command:#04x} {kind}") + else: + lines.append(" none") + if "flashSave0" in names and not packets: + lines.append( + "Save is the flashSave0 method. It builds save0Packet when MidiSuite runs. " + "The finished packet is not stored in the file." + ) + lines.append("Nothing was sent to the pad.") + return "\n".join(lines) + + +def download(url: str = MIDISUITE_URL) -> bytes: + with urllib.request.urlopen(url, timeout=120) as response: + return response.read() + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--zip", type=Path, help="MidiSuite_new.zip, downloaded when omitted") + parser.add_argument("--so", type=Path, help="an already extracted app.so") + args = parser.parse_args(argv) + + if args.so: + blob = args.so.read_bytes() + else: + package = args.zip.read_bytes() if args.zip else download() + blob = app_so(package) + print(report(blob)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tests/test_find_midisuite_save.py b/tests/test_find_midisuite_save.py new file mode 100644 index 0000000..1ed7e81 --- /dev/null +++ b/tests/test_find_midisuite_save.py @@ -0,0 +1,40 @@ +"""The MidiSuite save report stays quiet when the packet is not in the file.""" + +from __future__ import annotations + +import importlib.util +import io +import sys +import zipfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location( + "find_midisuite_save", ROOT / "scripts" / "find_midisuite_save.py" +) +finder = importlib.util.module_from_spec(spec) +assert spec.loader is not None +sys.modules["find_midisuite_save"] = finder +spec.loader.exec_module(finder) + + +def test_report_names_the_save_method_when_no_packet_is_stored() -> None: + blob = b"\x00flashSave0\x00save0Packet\x00smc_pad_desktop_save\x00" + text = finder.report(blob) + assert "flashSave0" in text + assert "save0Packet" in text + assert "Stored 00 59 read/write packets:\n none" in text + assert "Nothing was sent to the pad." in text + + +def test_report_lists_a_stored_write_packet() -> None: + blob = b"\x00\x59\x22\x01\x00\x00" + text = finder.report(blob) + assert "command 0x22 write" in text + + +def test_app_so_reads_the_snapshot_out_of_the_zip() -> None: + raw = io.BytesIO() + with zipfile.ZipFile(raw, "w") as archive: + archive.writestr(finder.APP_SO, b"flashSave0") + assert finder.app_so(raw.getvalue()) == b"flashSave0"