Skip to content
14 changes: 9 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,20 @@
longer reaches the game: like a button held as the menu closes, it counts
only once it is released and pressed again.
- A damaged or truncated `.zip` no longer hangs the loader or starts a broken
game: the ROM's size and CRC32 are checked, and the file browser says the
ZIP file is damaged or incomplete. An encrypted `.zip`, or one whose first
file is not a `.gba`, `.agb` or `.bin` ROM, gets the same message.
game: the ROM's size and CRC32 are checked, and Load game and Load state
from file in the menu say the ZIP file is damaged, incomplete or not
supported. An encrypted `.zip`, or one whose first file is not a `.gba`,
`.agb` or `.bin` ROM, gets the same message. A `.zip` picked in the
browser shown at startup, or passed by a launcher, still gets the generic
load error.
- A `.zip` written in streaming mode, with its sizes after the data, now
loads when its ROM is compressed (deflated) and those sizes are not in
the ZIP64 format. Any other `.zip` written that way, such as one with an
uncompressed (stored) ROM, gets the damaged or incomplete message, like
an encrypted one.
uncompressed (stored) ROM, gets the same ZIP message as an encrypted one.
- A `.gba` file that cannot be read in full now fails to load instead of
starting the game from part of the ROM.
- An empty `.gba`, `.agb` or `.bin` file now shows the load error instead
of restarting the game loaded before it, or a blank cartridge.
- The emulator no longer shows an error at startup when `dir.ini` is
missing: `dir.ini` is optional. Without it, ROMs are listed from the
`roms/` folder next to `EBOOT.PBP` when that folder exists, otherwise from
Expand Down
4 changes: 2 additions & 2 deletions docs/BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6762,7 +6762,7 @@ Il gruppo raccoglie quello che le altre sezioni avevano lasciato fuori: tre dife
- **Licenza:** codice nuovo GPL-2.0-or-later; la zlib è già nel tree (licenza zlib) e il formato viene dalla specifica APPNOTE di PKWARE.

#### EXT-2.2 — zip.c sopra zip_rom e messaggio per lo zip danneggiato · S, 0,5 gg · dipende da: EXT-2.1 · PSP: sì
- **Cosa:** `load_file_zip` diventa un adattatore: `read` chiama `FILE_READ`, e `progress` chiama `draw_progress` e `flip_screen` come oggi (`zip.c:164-165`). Restituisce la dimensione se va tutto bene, −2 per `TOO_BIG` come oggi e −3 per ogni altro esito. Il browser (`gui.c:3968-3971`, `:4195`) mostra il nuovo `MSG_ERR_ZIP_CORRUPT` per −3 e `MSG_ERR_LOAD_GAMEPACK` negli altri casi. L'avvio da riga di comando (`main.c:1697`, `:1716`, `:1732`) resta con il messaggio generico.
- **Cosa:** `load_file_zip` diventa un adattatore: `read` chiama `FILE_READ`, e `progress` chiama `draw_progress` e `flip_screen` come oggi (`zip.c:164-165`). Restituisce la dimensione se va tutto bene, −2 per `TOO_BIG` come oggi e `LOAD_GAMEPAK_ZIP_DAMAGED` (−4, `memory.h`) per ogni altro esito: −3 resta `LOAD_GAMEPAK_CANCELLED` di SAV-1.5. Il browser (`gui.c:3968-3971`, `:4195`) mostra il nuovo `MSG_ERR_ZIP_CORRUPT` per `LOAD_GAMEPAK_ZIP_DAMAGED` e `MSG_ERR_LOAD_GAMEPACK` negli altri casi. Gli avvii di `user_main` (`main.c:1697` da riga di comando, `:1716` `roms/game.gba`, `:1732` il browser mostrato all'avvio) restano con il messaggio generico.
- **File:** `source/src/zip.c`, `source/src/zip.h`, `source/src/gui.c`, `source/src/message.h`, `source/src/text/message_{jpn,eng,chs,cht,ita}.h`, `CHANGELOG.md`
- **Test:** `scripts/check-messages.sh`; `make check`. Su PSP-1000:
- uno zip valido da 16 MiB, con il tempo di caricamento misurato in 3 run prima e 3 dopo;
Expand All @@ -6771,7 +6771,7 @@ Il gruppo raccoglie quello che le altre sezioni avevano lasciato fuori: tre dife
- uno zip scritto in streaming (bit 3) da uno script Python di prova.
- **Accettazione:**
- Lo zip valido si gioca come prima, e il tempo di caricamento cresce al massimo del 10%.
- Lo zip corrotto e quello troncato mostrano il messaggio e tornano al browser senza blocchi.
- Lo zip corrotto e quello troncato mostrano il messaggio e tornano senza blocchi al menu da «Carica gioco» (come in 1.4.3) o alla lista da «Carica salvataggio da file».
- Lo zip in streaming si carica.
- Nel CHANGELOG c'è una voce Fixed.
- **Licenza:** `zip.c` conserva intestazione e autori (Exophase, SiberianSTAR, takka; GPL-2.0-or-later).
Expand Down
2 changes: 1 addition & 1 deletion docs/wip/rom-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

- [ ] **CMP-6.1** — Gate: Classic NES Series su 1.4.2 · S, 0,5 gg · dipende da: — · PSP: sì · **subito**
- [x] **EXT-2.1** — Modulo puro zip_rom: inflate a blocchi con esiti distinti e CRC32 · M, 1,5 gg · dipende da: — · PSP: no · **subito** · _CI verde su 65c2d6b (run 42803702949): in `make test-sanitize` `test_zip_rom`, costruito con `-fsanitize=address,undefined`, stampa «Zip ROM tests passed», e passa anche in `make check`; codice e test host in 37756d8, f9643c6, f140760_
- [ ] **EXT-2.2** — zip.c sopra zip_rom e messaggio per lo zip danneggiato · S, 0,5 gg · dipende da: EXT-2.1 · PSP: sì · _codice pronto (c60f182), manca la prova su PSP; per la sessione lo zip in streaming si scrive con `zipfile.ZipFile(stream non seekable, 'w', compression=zipfile.ZIP_DEFLATED)` e il suo primo header locale deve avere `flags & 8` e metodo 8 (uno zip in streaming con la ROM non compressa, metodo 0, è rifiutato di proposito)_
- [ ] **EXT-2.2** — zip.c sopra zip_rom e messaggio per lo zip danneggiato · S, 0,5 gg · dipende da: EXT-2.1 · PSP: sì · _codice pronto (c60f182), manca la prova su PSP; per la sessione lo zip in streaming si scrive con `zipfile.ZipFile(stream non seekable, 'w', compression=zipfile.ZIP_DEFLATED)` e il suo primo header locale deve avere `flags & 8` e metodo 8 (uno zip in streaming con la ROM non compressa, metodo 0, è rifiutato di proposito); lo zip danneggiato dà `LOAD_GAMEPAK_ZIP_DAMAGED` (−4: −3 è `LOAD_GAMEPAK_CANCELLED`), e la prova dei messaggi si fa da «Carica gioco» o «Carica salvataggio da file» del menu, perché il browser mostrato all'avvio dà il messaggio generico; dopo il messaggio «Carica gioco» torna al menu (come in 1.4.3) e «Carica salvataggio da file» riapre la lista_
- [ ] **EXT-5.1** — Finestra EEPROM decisa in read16_eeprom, con test host su una ROM paginata da 32 MiB · S, 1 gg · dipende da: EXT-1.1 · PSP: no · **subito**: EXT-1.1 è in main (ef98f28)
- [ ] **EXT-5.2** — Variante da 32 MiB della ROM di test dei salvataggi · S, 1 gg · dipende da: SAV-2.6, EXT-5.1 · PSP: sì · aspetta: EXT-5.1 (questo branch); EXT-1.1 è in main (ef98f28)

Expand Down
15 changes: 13 additions & 2 deletions source/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,7 @@ u64 rom_load_opened_us;
// Reads the whole ROM if it fits the buffer, in ROM_LOAD_CHUNK_SIZE reads
// reported to rom_load_progress; otherwise reads only the header and keeps
// the file open for load_gamepak_page(). Returns the file size, or -1 if
// the file does not open or a read fails or ends early.
// the file does not open, is empty or a read fails or ends early.
static s32 load_gamepak_raw(char *name)
{
SceUID gamepak_file;
Expand All @@ -3763,6 +3763,13 @@ static s32 load_gamepak_raw(char *name)
u32 _gamepak_size = file_length(name);
rom_load_opened_us = sceKernelGetSystemTimeWide();

// load_gamepak()'s callers take 0 as success.
if (_gamepak_size == 0)
{
FILE_CLOSE(gamepak_file);
return -1;
}

// If it's a big file size keep it don't close it, we'll
// probably want to load it later
if (_gamepak_size <= gamepak_ram_buffer_size)
Expand Down Expand Up @@ -3794,7 +3801,11 @@ static s32 load_gamepak_raw(char *name)
else
{
// Read in just enough for the header
FILE_READ(gamepak_file, gamepak_rom, 0x100);
if (FILE_READ(gamepak_file, gamepak_rom, 0x100) != 0x100)
{
FILE_CLOSE(gamepak_file);
return -1;
}

gamepak_file_large = gamepak_file;

Expand Down
2 changes: 1 addition & 1 deletion source/src/text/message_chs.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@
"无法加载游戏文件.",

// MSG_ERR_ZIP_CORRUPT
"ZIP 文件已损坏或不完整.",
"ZIP 文件已损坏、不完整或不受支持.",

// MSG_ERR_OPEN_GAMEPACK
"无法打开游戏文件.",
Expand Down
2 changes: 1 addition & 1 deletion source/src/text/message_cht.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@
"無法載入遊戲檔案.",

// MSG_ERR_ZIP_CORRUPT
"ZIP 檔案已損壞或不完整.",
"ZIP 檔案已損壞、不完整或不受支援.",

// MSG_ERR_OPEN_GAMEPACK
"無法開啟遊戲檔案.",
Expand Down
2 changes: 1 addition & 1 deletion source/src/text/message_eng.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@
"Could not load the game file.",

// MSG_ERR_ZIP_CORRUPT
"The ZIP file is damaged or incomplete.",
"The ZIP file is damaged, incomplete or not supported.",

// MSG_ERR_OPEN_GAMEPACK
"Could not open the game file.",
Expand Down
2 changes: 1 addition & 1 deletion source/src/text/message_ita.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@
"Impossibile caricare il file di gioco.",

// MSG_ERR_ZIP_CORRUPT
"Il file ZIP è danneggiato o incompleto.",
"Il file ZIP è danneggiato, incompleto o non supportato.",

// MSG_ERR_OPEN_GAMEPACK
"Impossibile aprire il file di gioco.",
Expand Down
2 changes: 1 addition & 1 deletion source/src/text/message_jpn.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@
"ゲームファイルを読み込めませんでした.",

// MSG_ERR_ZIP_CORRUPT
"ZIP ファイルが壊れているか、不完全です.",
"ZIP ファイルが壊れているか、不完全か、非対応の形式です.",

// MSG_ERR_OPEN_GAMEPACK
"ゲームファイルを開くことができませんでした.",
Expand Down
28 changes: 27 additions & 1 deletion tests/test_memory_backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
* Regressions it guards: a 64 KiB .sav of an SRAM game was written back as
* one byte; a .sav that failed to read was replaced by a blank image on the
* first flush; a .sav over 128 KiB overran the save buffer; a write that
* failed was dropped as if it had been written.
* failed was dropped as if it had been written; an empty ROM file, or a
* large one whose header did not read, counted as loaded, so the previous
* game restarted.
*/
#include <assert.h>
#include <stdint.h>
Expand All @@ -21,6 +23,7 @@
#include "fake_store_io.h"

void host_stub_file_length(int length);
void host_stub_rom_io(int read_limit, int seek_fails);
void host_stub_yesno(const unsigned *answers, int count);
int host_stub_yesno_asked(void);
int host_stub_yesno_saw_rom(void);
Expand Down Expand Up @@ -289,6 +292,27 @@ static void test_unattended_load_cancels_without_prompts(void)
assert(store.write_calls == 0);
}

/* An empty ROM file fails to load: load_gamepak()'s callers take 0 as
success. */
static void test_empty_rom_fails_to_load(void)
{
fresh_store();
host_stub_file_length(0);
assert(load("SRAM_V113", NULL, 0) == -1);
host_stub_file_length(ROM_SIZE);
}

/* A ROM too big for the buffer fails to load when its header does not
read: the header would be the previous game's. Runs last, because the
ROM reads keep failing afterwards. */
static void test_large_rom_without_header_fails_to_load(void)
{
fresh_store();
host_stub_file_length((int)gamepak_ram_buffer_size + 1);
host_stub_rom_io(-1, 0);
assert(load("SRAM_V113", NULL, 0) == -1);
}

/* 200 KiB for a 128 KiB Flash game: the first 128 KiB load (ASan catches
any write past the buffer) and the file is written back at 128 KiB. */
static void test_oversized_save_loads_a_prefix(void)
Expand Down Expand Up @@ -649,6 +673,7 @@ int main(void)
test_cut_write_is_recovered_at_load();
test_refusing_both_prompts_cancels();
test_unattended_load_cancels_without_prompts();
test_empty_rom_fails_to_load();
test_oversized_save_loads_a_prefix();
test_tag_beats_file_size();
test_failed_write_stays_pending();
Expand All @@ -658,6 +683,7 @@ int main(void)
test_push_link_copy_is_logged();
test_restore_link_copy();
test_failed_restore_asks_when_the_save_will_not_read();
test_large_rom_without_header_fails_to_load();
puts("memory backup tests passed");
return 0;
}
42 changes: 42 additions & 0 deletions tests/test_zip_rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ static void test_valid_entries_load_byte_for_byte(void)
expect_rom(len, SMALL_CHUNK, OUT_CAP);
}

/* A stored ROM smaller than one read, with more archive bytes after it (the
rest of rom stands in for the central directory): only its own bytes are
copied, and nothing past out_cap. */
static void test_small_stored_entry_stops_at_its_size(void)
{
uint32_t size = 0;
uint32_t len = build(0, 0, "game.gba", ROM_SIZE, DESC_NONE);

put32(archive + 14, (uint32_t)crc32(0L, rom, 1000u));
put32(archive + 18, 1000u);
put32(archive + 22, 1000u);
assert(load(len, SMALL_CHUNK, 1000u, NO_FAILURE, &size) == ZIP_ROM_OK);
assert(size == 1000u && memcmp(out, rom, 1000u) == 0);
size = 0;
assert(load(len, LARGE_CHUNK, 1000u, NO_FAILURE, &size) == ZIP_ROM_OK);
assert(size == 1000u && memcmp(out, rom, 1000u) == 0);
}

/* Flag bit 3: CRC and sizes come from the descriptor after the stream. */
static void test_streamed_entries_use_the_descriptor(void)
{
Expand Down Expand Up @@ -296,6 +314,22 @@ static void test_damaged_data_is_corrupt(void)
assert(load_all(len, SMALL_CHUNK) == ZIP_ROM_CORRUPT);
}

/* Loads a stored entry whose extra field is extra bytes long: it ends inside
build()'s stored data, and the 1000 bytes after it are the entry. One
read holds the whole entry, since skipping the field reports no progress. */
static int long_extra_entry(uint32_t extra)
{
uint32_t size;
uint32_t len = build(0, 0, "game.gba", ROM_SIZE, DESC_NONE);

put32(archive + 14, (uint32_t)crc32(0L, archive + data_offset(8) -
EXTRA_SIZE + extra, 1000u));
put32(archive + 18, 1000u);
put32(archive + 22, 1000u);
put16(archive + 28, extra);
return load(len, LARGE_CHUNK, 1000u, NO_FAILURE, &size);
}

static void test_bad_headers_are_corrupt(void)
{
uint32_t len = build(0, 8, "game.gba", ROM_SIZE, DESC_NONE);
Expand All @@ -308,6 +342,11 @@ static void test_bad_headers_are_corrupt(void)
build(0, 8, "game.gba", ROM_SIZE, DESC_NONE);
put16(archive + 28, 0x8000u);
assert(load_all(len, SMALL_CHUNK) == ZIP_ROM_CORRUPT);

/* A real extra field over the old loader's 0x7FFF bytes, before an intact
entry, is refused; one of 0x7FFF bytes loads. */
assert(long_extra_entry(0x8000u) == ZIP_ROM_CORRUPT);
assert(long_extra_entry(0x7FFFu) == ZIP_ROM_OK);
}

static void test_truncated_archives(void)
Expand Down Expand Up @@ -339,6 +378,8 @@ static void test_unsupported_entries(void)
assert(load_all(len, SMALL_CHUNK) == ZIP_ROM_UNSUPPORTED);
len = build(0, 12, "game.gba", ROM_SIZE, DESC_NONE);
assert(load_all(len, SMALL_CHUNK) == ZIP_ROM_UNSUPPORTED);
len = build(0, 6, "game.gba", ROM_SIZE, DESC_NONE);
assert(load_all(len, SMALL_CHUNK) == ZIP_ROM_UNSUPPORTED);
len = build(0, 8, "readme.txt", ROM_SIZE, DESC_NONE);
assert(load_all(len, SMALL_CHUNK) == ZIP_ROM_UNSUPPORTED);
len = build(0, 8, "game", ROM_SIZE, DESC_NONE);
Expand Down Expand Up @@ -529,6 +570,7 @@ int main(void)
{
make_fixture();
test_valid_entries_load_byte_for_byte();
test_small_stored_entry_stops_at_its_size();
test_streamed_entries_use_the_descriptor();
test_damaged_data_is_corrupt();
test_bad_headers_are_corrupt();
Expand Down