mirror of
https://github.com/dborth/fceugx.git
synced 2024-10-31 22:45:05 +01:00
Add PocketNES interoperability
* Ability to load PocketNES ROMs that contain one NES ROM * Ability to read/write PocketNES SRAM data if it already exists
This commit is contained in:
parent
e41ae649cb
commit
5800f2e688
@ -25,6 +25,7 @@
|
||||
#include "menu.h"
|
||||
#include "filebrowser.h"
|
||||
#include "fileop.h"
|
||||
#include "pocketnes/goombasav.h"
|
||||
|
||||
static u32 WiiFCEU_GameSave(CartInfo *LocalHWInfo, int operation)
|
||||
{
|
||||
@ -74,6 +75,57 @@ bool SaveRAM (char * filepath, bool silent)
|
||||
else if(GameInfo->type == GIT_VSUNI)
|
||||
datasize = WiiFCEU_GameSave(&UNIFCart, 0);
|
||||
|
||||
if (datasize)
|
||||
{
|
||||
// Check to see if this is a PocketNES save file
|
||||
FILE* file = fopen(filepath, "rb");
|
||||
if (file)
|
||||
{
|
||||
uint32 tag;
|
||||
fread(&tag, sizeof(uint32), 1, file);
|
||||
fclose(file);
|
||||
|
||||
if (goomba_is_sram(&tag))
|
||||
{
|
||||
|
||||
// Look for just one save file. If there aren't any, or there is more than one, don't read any data.
|
||||
stateheader* sh1 = NULL;
|
||||
stateheader* sh2 = NULL;
|
||||
|
||||
stateheader* sh = (stateheader*)(savebuffer + 4);
|
||||
while (sh && stateheader_plausible(sh)) {
|
||||
if (little_endian_conv_16(sh->type) != GOOMBA_SRAMSAVE) {}
|
||||
else if (sh1 == NULL) {
|
||||
sh1 = sh;
|
||||
}
|
||||
else {
|
||||
sh2 = sh;
|
||||
break;
|
||||
}
|
||||
sh = stateheader_advance(sh);
|
||||
}
|
||||
|
||||
if (sh1 == NULL)
|
||||
{
|
||||
ErrorPrompt("PocketNES save file has no SRAM.");
|
||||
datasize = 0;
|
||||
}
|
||||
else if (sh2 != NULL)
|
||||
{
|
||||
ErrorPrompt("PocketNES save file has more than one SRAM.");
|
||||
datasize = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
char* newdata = goomba_new_sav(savebuffer, sh1, savebuffer, datasize);
|
||||
memcpy(savebuffer, newdata, GOOMBA_COLOR_SRAM_SIZE);
|
||||
datasize = GOOMBA_COLOR_SRAM_SIZE;
|
||||
free(newdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (datasize)
|
||||
{
|
||||
offset = SaveFile(filepath, datasize, silent);
|
||||
@ -121,6 +173,53 @@ bool LoadRAM (char * filepath, bool silent)
|
||||
|
||||
offset = LoadFile(filepath, silent);
|
||||
|
||||
// Check to see if this is a PocketNES save file
|
||||
if (goomba_is_sram(savebuffer))
|
||||
{
|
||||
// Look for just one save file. If there aren't any, or there is more than one, don't read any data.
|
||||
stateheader* sh1 = NULL;
|
||||
stateheader* sh2 = NULL;
|
||||
|
||||
stateheader* sh = (stateheader*)(savebuffer + 4);
|
||||
while (sh && stateheader_plausible(sh)) {
|
||||
if (little_endian_conv_16(sh->type) != GOOMBA_SRAMSAVE) {
|
||||
InfoPrompt("Not sram");
|
||||
}
|
||||
else if (sh1 == NULL) {
|
||||
sh1 = sh;
|
||||
}
|
||||
else {
|
||||
sh2 = sh;
|
||||
break;
|
||||
}
|
||||
sh = stateheader_advance(sh);
|
||||
}
|
||||
|
||||
if (sh1 == NULL)
|
||||
{
|
||||
ErrorPrompt("PocketNES save file has no SRAM.");
|
||||
offset = 0;
|
||||
}
|
||||
else if (sh2 != NULL)
|
||||
{
|
||||
ErrorPrompt("PocketNES save file has more than one SRAM.");
|
||||
offset = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
goomba_size_t len;
|
||||
void* extracted = goomba_extract(savebuffer, sh1, &len);
|
||||
if (!extracted)
|
||||
ErrorPrompt(goomba_last_error());
|
||||
else
|
||||
{
|
||||
memcpy(savebuffer, extracted, len);
|
||||
offset = len;
|
||||
free(extracted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
if(GameInfo->type == GIT_CART)
|
||||
|
@ -31,6 +31,8 @@
|
||||
#include "fceuram.h"
|
||||
#include "fceustate.h"
|
||||
#include "patch.h"
|
||||
#include "pocketnes/goombasav.h"
|
||||
#include "pocketnes/pocketnesrom.h"
|
||||
|
||||
BROWSERINFO browser;
|
||||
BROWSERENTRY * browserList = NULL; // list of files/folders in browser
|
||||
@ -353,6 +355,12 @@ static bool IsValidROM()
|
||||
|
||||
if (p != NULL)
|
||||
{
|
||||
if(strcasecmp(p, ".gba") == 0)
|
||||
{
|
||||
// File will be checked for GBA ROMs later.
|
||||
return true;
|
||||
}
|
||||
|
||||
char * zippedFilename = NULL;
|
||||
|
||||
if(stricmp(p, ".zip") == 0 && !inSz)
|
||||
@ -480,6 +488,28 @@ int BrowserLoadFile()
|
||||
goto done;
|
||||
|
||||
filesize = LoadFile ((char *)nesrom, filepath, browserList[browser.selIndex].length, NOTSILENT);
|
||||
|
||||
// check nesrom for PocketNES embedded roms
|
||||
const char *ext = strrchr(filepath, '.');
|
||||
if (ext != NULL && strcmp(ext, ".gba") == 0)
|
||||
{
|
||||
const pocketnes_romheader* rom1 = pocketnes_first_rom(nesrom, filesize);
|
||||
const pocketnes_romheader* rom2 = NULL;
|
||||
if (rom1 != NULL) {
|
||||
rom2 = pocketnes_next_rom(nesrom, filesize, rom1);
|
||||
}
|
||||
|
||||
if (rom1 == NULL)
|
||||
ErrorPrompt("No NES ROMs found in this file.");
|
||||
else if (rom2 != NULL)
|
||||
ErrorPrompt("More than one NES ROM found in this file. Only files with one ROM are supported.");
|
||||
else
|
||||
{
|
||||
const void* rom = rom1 + 1;
|
||||
filesize = little_endian_conv_32(rom1->filesize);
|
||||
memcpy(nesrom, rom, filesize);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -569,6 +569,7 @@ static bool ParseDirEntries()
|
||||
if( stricmp(ext, "nes") != 0 && stricmp(ext, "fds") != 0 &&
|
||||
stricmp(ext, "nsf") != 0 && stricmp(ext, "unf") != 0 &&
|
||||
stricmp(ext, "nez") != 0 && stricmp(ext, "unif") != 0 &&
|
||||
stricmp(ext, "gba") != 0 &&
|
||||
stricmp(ext, "zip") != 0 && stricmp(ext, "7z") != 0)
|
||||
continue;
|
||||
}
|
||||
|
506
source/pocketnes/goombasav.cpp
Normal file
506
source/pocketnes/goombasav.cpp
Normal file
@ -0,0 +1,506 @@
|
||||
/* goombasav.c - functions to handle Goomba / Goomba Color SRAM
|
||||
|
||||
Copyright (C) 2014-2017 libertyernie
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
https://github.com/libertyernie/goombasav
|
||||
|
||||
When compiling in Visual Studio, set the project to compile
|
||||
as C++ code (Properties -> C/C++ -> Advanced -> Compile As.)
|
||||
*/
|
||||
|
||||
#include <memory.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "goombasav.h"
|
||||
#include "minilzo/minilzo.h"
|
||||
|
||||
#define goomba_error(...) { sprintf(last_error, __VA_ARGS__); }
|
||||
|
||||
#define F16 little_endian_conv_16
|
||||
#define F32 little_endian_conv_32
|
||||
|
||||
static const char* const sleeptxt[] = { "5min", "10min", "30min", "OFF" };
|
||||
static const char* const brightxt[] = { "I", "II", "III", "IIII", "IIIII" };
|
||||
|
||||
static char last_error[256] = "No error has occured yet.";
|
||||
static char goomba_strbuf[256];
|
||||
|
||||
const char* goomba_last_error() {
|
||||
return (const char*)last_error;
|
||||
}
|
||||
|
||||
size_t goomba_set_last_error(const char* msg) {
|
||||
size_t len = strlen(msg);
|
||||
if (len > sizeof(last_error)-1) {
|
||||
len = sizeof(last_error)-1;
|
||||
}
|
||||
memcpy(last_error, msg, len);
|
||||
last_error[sizeof(last_error)-1] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
// For making a checksum of the compressed data.
|
||||
// output_bytes is limited to 8 at maximum
|
||||
uint64_t checksum_slow(const void* ptr, size_t length, int output_bytes) {
|
||||
const unsigned char* p = (const unsigned char*)ptr;
|
||||
uint64_t sum=0;
|
||||
char* sumptr = (char*)∑
|
||||
size_t j;
|
||||
for (j=0;j<length;j++) {
|
||||
int index = j%output_bytes;
|
||||
sumptr[index] += *p;
|
||||
p++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
uint16_t little_endian_conv_16(uint16_t value) {
|
||||
if (*(uint16_t *)"\0\xff" < 0x100) {
|
||||
uint16_t buffer;
|
||||
((char*)&buffer)[0] = ((char*)&value)[1];
|
||||
((char*)&buffer)[1] = ((char*)&value)[0];
|
||||
return buffer;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t little_endian_conv_32(uint32_t value) {
|
||||
if (*(uint16_t *)"\0\xff" < 0x100) {
|
||||
uint32_t buffer;
|
||||
((char*)&buffer)[0] = ((char*)&value)[3];
|
||||
((char*)&buffer)[1] = ((char*)&value)[2];
|
||||
((char*)&buffer)[2] = ((char*)&value)[1];
|
||||
((char*)&buffer)[3] = ((char*)&value)[0];
|
||||
return buffer;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
configdata_misc_strings configdata_get_misc(char misc) {
|
||||
configdata_misc_strings s;
|
||||
s.sleep = sleeptxt[misc & 0x3];
|
||||
s.autoload_state = ((misc & 0x10) >> 4) ? "ON" : "OFF";
|
||||
s.gamma = brightxt[(misc & 0xE0) >> 5];
|
||||
return s;
|
||||
}
|
||||
|
||||
const char* stateheader_typestr(uint16_t type) {
|
||||
switch (type) {
|
||||
case GOOMBA_STATESAVE:
|
||||
return "Savestate";
|
||||
case GOOMBA_SRAMSAVE:
|
||||
return "SRAM";
|
||||
case GOOMBA_CONFIGSAVE:
|
||||
return "Configuration";
|
||||
case GOOMBA_PALETTE: // Used by Goomba Paletted
|
||||
return "Palette";
|
||||
default:
|
||||
return "Unknown"; // Stateheaders with these types are rejected by stateheader_plausible
|
||||
}
|
||||
}
|
||||
|
||||
const char* stateheader_str(const stateheader* sh) {
|
||||
int j = 0;
|
||||
j += sprintf(goomba_strbuf + j, "size: %u\n", F16(sh->size));
|
||||
j += sprintf(goomba_strbuf + j, "type: %s (%u)\n", stateheader_typestr(F16(sh->type)), F16(sh->type));
|
||||
if (F16(sh->type) == GOOMBA_CONFIGSAVE) {
|
||||
configdata* cd = (configdata*)sh;
|
||||
if (cd->size == sizeof(goomba_configdata)) {
|
||||
const goomba_configdata* gcd = (const goomba_configdata*)cd;
|
||||
j += sprintf(goomba_strbuf + j, "(goomba) bordercolor: %u\n", gcd->bordercolor);
|
||||
j += sprintf(goomba_strbuf + j, "(goomba) palettebank: %u\n", gcd->palettebank);
|
||||
configdata_misc_strings strs = configdata_get_misc(gcd->misc);
|
||||
j += sprintf(goomba_strbuf + j, "(goomba) sleep: %s\n", strs.sleep);
|
||||
j += sprintf(goomba_strbuf + j, "(goomba) autoload state: %s\n", strs.autoload_state);
|
||||
j += sprintf(goomba_strbuf + j, "(goomba) gamma: %s\n", strs.gamma);
|
||||
const pocketnes_configdata* pcd = (const pocketnes_configdata*)cd;
|
||||
j += sprintf(goomba_strbuf + j, "(pocketnes) displaytype: %u\n", pcd->displaytype);
|
||||
j += sprintf(goomba_strbuf + j, "(pocketnes) misc: %u\n", pcd->misc);
|
||||
j += sprintf(goomba_strbuf + j, "rom checksum: %8X (0xE000-0xFFFF %s)\n", F32(gcd->sram_checksum),
|
||||
gcd->sram_checksum != 0 ? "occupied" : "free");
|
||||
j += sprintf(goomba_strbuf + j, "title: %s", gcd->reserved4);
|
||||
} else if (cd->size == sizeof(smsadvance_configdata)) {
|
||||
const smsadvance_configdata* scd = (const smsadvance_configdata*)cd;
|
||||
j += sprintf(goomba_strbuf + j, "displaytype: %u\n", scd->displaytype);
|
||||
j += sprintf(goomba_strbuf + j, "gammavalue: %u\n", scd->gammavalue);
|
||||
j += sprintf(goomba_strbuf + j, "region: %u\n", scd->region);
|
||||
j += sprintf(goomba_strbuf + j, "sleepflick: %u\n", scd->sleepflick);
|
||||
j += sprintf(goomba_strbuf + j, "config: %u\n", scd->config);
|
||||
j += sprintf(goomba_strbuf + j, "bcolor: %u\n", scd->bcolor);
|
||||
j += sprintf(goomba_strbuf + j, "rom checksum: %8X (0xE000-0xFFFF %s)\n", F32(scd->sram_checksum),
|
||||
scd->sram_checksum != 0 ? "occupied" : "free");
|
||||
j += sprintf(goomba_strbuf + j, "title: %s", scd->reserved3);
|
||||
}
|
||||
} else {
|
||||
j += sprintf(goomba_strbuf + j, "%scompressed_size: %u\n",
|
||||
(F32(sh->uncompressed_size) < F16(sh->size) ? "" : "un"),
|
||||
F32(sh->uncompressed_size));
|
||||
j += sprintf(goomba_strbuf + j, "framecount: %u\n", F32(sh->framecount));
|
||||
j += sprintf(goomba_strbuf + j, "rom checksum: %8X\n", F32(sh->checksum));
|
||||
j += sprintf(goomba_strbuf + j, "title: %s", sh->title);
|
||||
}
|
||||
return goomba_strbuf;
|
||||
}
|
||||
|
||||
const char* stateheader_summary_str(const stateheader* sh) {
|
||||
sprintf(goomba_strbuf, "%s: %s (%u b / %u uncomp)", stateheader_typestr(
|
||||
F16(sh->type)), sh->title, F16(sh->size), F32(sh->uncompressed_size));
|
||||
return goomba_strbuf;
|
||||
}
|
||||
|
||||
int stateheader_plausible(const void* ptr) {
|
||||
const stateheader* sh = (const stateheader*)ptr;
|
||||
uint16_t type = F16(sh->type);
|
||||
if (type < 0 || type == 3 || type == 4 || type > 5) return 0;
|
||||
return F16(sh->size) >= sizeof(stateheader) && // check size (at least 48)
|
||||
(F16(sh->type) == GOOMBA_CONFIGSAVE || sh->uncompressed_size != 0); // check uncompressed_size, but not for configsave
|
||||
// when checking for whether something equals 0, endian conversion is not necessary
|
||||
}
|
||||
|
||||
stateheader* stateheader_advance(const stateheader* sh) {
|
||||
if (!stateheader_plausible(sh)) return NULL;
|
||||
|
||||
uint16_t s = F16(sh->size);
|
||||
char* c = (char*)sh;
|
||||
c += s;
|
||||
return (stateheader*)c;
|
||||
}
|
||||
|
||||
const stateheader* stateheader_first(const void* gba_data) {
|
||||
uint32_t* check = (uint32_t*)gba_data;
|
||||
uint32_t check_le = F32(*check);
|
||||
if (check_le == GOOMBA_STATEID) check++;
|
||||
else if (check_le == POCKETNES_STATEID) check++;
|
||||
else if (check_le == POCKETNES_STATEID2) check++;
|
||||
else if (check_le == SMSADVANCE_STATEID) check++;
|
||||
|
||||
return stateheader_plausible(check)
|
||||
? (stateheader*)check
|
||||
: NULL;
|
||||
}
|
||||
|
||||
const stateheader** stateheader_scan(const void* gba_data) {
|
||||
const goomba_size_t psize = sizeof(stateheader*);
|
||||
const stateheader** headers = (const stateheader**)malloc(psize * 64);
|
||||
memset(headers, 0, psize * 64);
|
||||
|
||||
const stateheader* sh = stateheader_first(gba_data);
|
||||
int i = 0;
|
||||
while (stateheader_plausible(sh) && i < 63) {
|
||||
headers[i] = sh;
|
||||
i++;
|
||||
sh = stateheader_advance(sh);
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
const stateheader* stateheader_for(const void* gba_data, const char* gbc_title) {
|
||||
char title[0x10];
|
||||
memcpy(title, gbc_title, 0x0F);
|
||||
title[0x0F] = '\0';
|
||||
const stateheader* use_this = NULL;
|
||||
const stateheader** headers = stateheader_scan(gba_data);
|
||||
int i;
|
||||
for (i = 0; headers[i] != NULL; i++) {
|
||||
if (strcmp(headers[i]->title, title) == 0 && headers[i]->type == GOOMBA_SRAMSAVE) {
|
||||
use_this = headers[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(headers);
|
||||
if (use_this == NULL) sprintf(last_error, "Could not find SRAM data for %s", title);
|
||||
return use_this;
|
||||
}
|
||||
|
||||
// Uses checksum_slow, and looks at the compressed data (not the header).
|
||||
// output_bytes is limited to 8 at maximum
|
||||
uint64_t goomba_compressed_data_checksum(const stateheader* sh, int output_bytes) {
|
||||
return checksum_slow(sh+1, F16(sh->size) - sizeof(stateheader), output_bytes);
|
||||
}
|
||||
|
||||
int goomba_is_sram(const void* data) {
|
||||
uint32_t stateid_le = F32(*(uint32_t*)data);
|
||||
return stateid_le == GOOMBA_STATEID
|
||||
|| stateid_le == POCKETNES_STATEID
|
||||
|| stateid_le == POCKETNES_STATEID2
|
||||
|| stateid_le == SMSADVANCE_STATEID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 32-bit checksum (unsigned) in the configdata header, or -1 if
|
||||
* an error occurred.
|
||||
*/
|
||||
int64_t goomba_get_configdata_checksum_field(const void* gba_data) {
|
||||
const stateheader* sh = stateheader_first(gba_data);
|
||||
|
||||
while (sh && stateheader_plausible(sh)) {
|
||||
if (F16(sh->type) == GOOMBA_CONFIGSAVE) {
|
||||
// found configdata
|
||||
const configdata* cd = (configdata*)sh;
|
||||
if (F16(cd->size) == sizeof(goomba_configdata)) {
|
||||
const goomba_configdata* gcd = (const goomba_configdata*)cd;
|
||||
return F32(gcd->sram_checksum); // 0 = clean, postitive = unclean
|
||||
} else if (F16(cd->size) == sizeof(smsadvance_configdata)) {
|
||||
const smsadvance_configdata* scd = (const smsadvance_configdata*)cd;
|
||||
return F32(scd->sram_checksum); // 0 = clean, postitive = unclean
|
||||
} else {
|
||||
goomba_error("This is not a recognized type of configheader (by cd->size).");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
sh = stateheader_advance(sh);
|
||||
}
|
||||
|
||||
goomba_error("Unknown error (no configdata?)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char* goomba_cleanup(const void* gba_data_param) {
|
||||
char gba_data[GOOMBA_COLOR_SRAM_SIZE]; // on stack - do not need to free
|
||||
memcpy(gba_data, gba_data_param, GOOMBA_COLOR_SRAM_SIZE);
|
||||
|
||||
const stateheader** headers = stateheader_scan(gba_data);
|
||||
if (headers == NULL) return NULL;
|
||||
|
||||
int i, j;
|
||||
for (i = 0; headers[i] != NULL; i++) {
|
||||
if (F16(headers[i]->type) == GOOMBA_CONFIGSAVE) {
|
||||
// found configdata
|
||||
configdata* cd = (configdata*)headers[i];
|
||||
uint32_t checksum = 0;
|
||||
|
||||
goomba_configdata* gcd = NULL;
|
||||
smsadvance_configdata* scd = NULL;
|
||||
|
||||
if (cd->size == sizeof(goomba_configdata)) {
|
||||
gcd = (goomba_configdata*)cd;
|
||||
checksum = F32(gcd->sram_checksum); // 0 = clean, postitive = unclean
|
||||
} else if (cd->size == sizeof(smsadvance_configdata)) {
|
||||
scd = (smsadvance_configdata*)cd;
|
||||
checksum = F32(scd->sram_checksum); // 0 = clean, postitive = unclean
|
||||
}
|
||||
|
||||
for (j = 0; headers[j] != NULL; j++) {
|
||||
const stateheader* sh = headers[j];
|
||||
if (F16(sh->type) == GOOMBA_SRAMSAVE && F32(sh->checksum) == checksum) {
|
||||
// found stateheader
|
||||
free(headers); // so make sure we return something before the loop goes around again!!
|
||||
|
||||
if (gcd) gcd->sram_checksum = 0; // because we do this here, goomba_new_sav should not complain about an unclean file
|
||||
if (scd) scd->sram_checksum = 0;
|
||||
|
||||
char gbc_data[GOOMBA_COLOR_SRAM_SIZE - GOOMBA_COLOR_AVAILABLE_SIZE];
|
||||
memcpy(gbc_data,
|
||||
gba_data + GOOMBA_COLOR_AVAILABLE_SIZE,
|
||||
sizeof(gbc_data)); // Extract GBC data at 0xe000 to an array
|
||||
|
||||
char* new_gba_data = goomba_new_sav(gba_data, sh, gbc_data, sizeof(gbc_data));
|
||||
if (new_gba_data != NULL) memset(new_gba_data + GOOMBA_COLOR_AVAILABLE_SIZE, 0, sizeof(gbc_data));
|
||||
return new_gba_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
free(headers);
|
||||
return (char*)gba_data_param;
|
||||
}
|
||||
|
||||
void* goomba_extract(const void* gba_data, const stateheader* header_ptr, goomba_size_t* size_output) {
|
||||
const stateheader* sh = (const stateheader*)header_ptr;
|
||||
|
||||
if (F16(sh->type) != GOOMBA_SRAMSAVE) {
|
||||
goomba_error("Error: this program can only extract SRAM data.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const int64_t ck = goomba_get_configdata_checksum_field(gba_data);
|
||||
if (ck < 0) {
|
||||
return NULL;
|
||||
} else if (ck == F32(sh->checksum)) {
|
||||
goomba_error("File is unclean - run goomba_cleanup before trying to extract SRAM, or you might get old data\n");
|
||||
return NULL;
|
||||
} else if (ck != 0) {
|
||||
fprintf(stderr, "File is unclean, but it shouldn't affect retrieval of the data you asked for\n");
|
||||
}
|
||||
|
||||
lzo_uint compressed_size = F16(sh->size) - sizeof(stateheader);
|
||||
lzo_uint output_size = 32768;
|
||||
const unsigned char* compressed_data = (unsigned char*)header_ptr + sizeof(stateheader);
|
||||
unsigned char* uncompressed_data = (unsigned char*)malloc(output_size);
|
||||
int r = lzo1x_decompress_safe(compressed_data, compressed_size,
|
||||
uncompressed_data, &output_size,
|
||||
(void*)NULL);
|
||||
//fprintf(stderr, "Actual uncompressed size: %lu\n", output_size);
|
||||
if (r == LZO_E_INPUT_NOT_CONSUMED) {
|
||||
//goomba_error("Warning: input not fully used. Double-check the result to make sure it works.\n");
|
||||
} else if (r < 0) {
|
||||
goomba_error("Cannot decompress data (lzoconf.h error code %d).\n", r);
|
||||
free(uncompressed_data);
|
||||
return NULL;
|
||||
}
|
||||
*size_output = output_size;
|
||||
return uncompressed_data;
|
||||
}
|
||||
|
||||
goomba_size_t copy_until_invalid_header(void* dest, const stateheader* src_param) {
|
||||
const void* src = src_param;
|
||||
goomba_size_t bytes_copied = 0;
|
||||
while (1) {
|
||||
const stateheader* sh = (const stateheader*)src;
|
||||
if (!stateheader_plausible(sh)) break;
|
||||
|
||||
memcpy(dest, src, F16(sh->size));
|
||||
|
||||
src = (char*)src + F16(sh->size);
|
||||
dest = (char*)dest + F16(sh->size);
|
||||
bytes_copied += F16(sh->size);
|
||||
}
|
||||
memcpy(dest, src, sizeof(stateheader)); // copy "footer"
|
||||
return bytes_copied + sizeof(stateheader);
|
||||
}
|
||||
|
||||
char* goomba_new_sav(const void* gba_data, const void* gba_header, const void* gbc_sram, goomba_size_t gbc_length) {
|
||||
unsigned char* gba_header_ptr = (unsigned char*)gba_header;
|
||||
stateheader* sh = (stateheader*)gba_header_ptr;
|
||||
|
||||
int64_t ck = goomba_get_configdata_checksum_field(gba_data);
|
||||
if (ck < 0) {
|
||||
return NULL;
|
||||
} else if (ck == F32(sh->checksum)) {
|
||||
// have to clean file
|
||||
goomba_error("File is unclean - run goomba_cleanup before trying to replace SRAM, or your new data might get overwritten");
|
||||
return NULL;
|
||||
} else if (ck != 0) {
|
||||
fprintf(stderr, "File is unclean, but it shouldn't affect replacement of the data you asked for\n");
|
||||
}
|
||||
|
||||
if (F16(sh->type) != GOOMBA_SRAMSAVE) {
|
||||
goomba_error("Error - This program cannot replace non-SRAM data.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// sh->uncompressed_size is valid for Goomba Color.
|
||||
// For Goomba, it's actually compressed size (and will be less than sh->size).
|
||||
goomba_size_t uncompressed_size;
|
||||
if (F16(sh->size) > F32(sh->uncompressed_size)) {
|
||||
// Uncompress to a temporary location, just so we can see how big it is
|
||||
goomba_size_t output;
|
||||
void* dump = goomba_extract(gba_data, sh, &output);
|
||||
if (dump == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
free(dump);
|
||||
uncompressed_size = output;
|
||||
} else {
|
||||
// Goomba Color header - use size from there
|
||||
uncompressed_size = F32(sh->uncompressed_size);
|
||||
}
|
||||
|
||||
if (gbc_length == 0) {
|
||||
// Remove data instead of replacing it
|
||||
uncompressed_size = 0;
|
||||
} else if (gbc_length < uncompressed_size) {
|
||||
goomba_error("Error: the length of the GBC data (%u) is too short - expected %u bytes.\n",
|
||||
gbc_length, uncompressed_size);
|
||||
return NULL;
|
||||
} else if (gbc_length - 4 == uncompressed_size) {
|
||||
goomba_error("Note: RTC data (TGB_Dual format) will not be copied\n");
|
||||
} else if (gbc_length - 44 == uncompressed_size) {
|
||||
goomba_error("Note: RTC data (old VBA format) will not be copied\n");
|
||||
} else if (gbc_length - 48 == uncompressed_size) {
|
||||
goomba_error("Note: RTC data (new VBA format) will not be copied\n");
|
||||
} else if (gbc_length > uncompressed_size) {
|
||||
goomba_error("Warning: unknown data at end of GBC save file - only first %u bytes will be used\n", uncompressed_size);
|
||||
}
|
||||
|
||||
if (F16(sh->type) != GOOMBA_SRAMSAVE) {
|
||||
goomba_error("The data at gba_header is not SRAM data.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* const goomba_new_sav = (char*)malloc(GOOMBA_COLOR_SRAM_SIZE);
|
||||
memset(goomba_new_sav, 0, GOOMBA_COLOR_SRAM_SIZE);
|
||||
char* working = goomba_new_sav; // will be incremented throughout
|
||||
|
||||
goomba_size_t before_header = (char*)gba_header - (char*)gba_data;
|
||||
// copy anything before stateheader
|
||||
memcpy(goomba_new_sav, gba_data, before_header);
|
||||
working += before_header;
|
||||
// copy stateheader
|
||||
memcpy(working, sh, sizeof(stateheader));
|
||||
stateheader* new_sh = (stateheader*)working;
|
||||
working += sizeof(stateheader);
|
||||
|
||||
// backup data that comes after this header
|
||||
unsigned char* backup = (unsigned char*)malloc(GOOMBA_COLOR_SRAM_SIZE);
|
||||
goomba_size_t backup_len = copy_until_invalid_header(backup, (stateheader*)(gba_header_ptr + F16(sh->size)));
|
||||
|
||||
// compress gbc sram
|
||||
if (uncompressed_size == 0) {
|
||||
// never mind about that header
|
||||
working -= sizeof(stateheader);
|
||||
memset(working, 0, sizeof(stateheader));
|
||||
} else {
|
||||
lzo_uint compressed_size;
|
||||
unsigned char* dest = (unsigned char*)working;
|
||||
void* wrkmem = malloc(LZO1X_1_MEM_COMPRESS);
|
||||
lzo1x_1_compress((const unsigned char*)gbc_sram, uncompressed_size,
|
||||
dest, &compressed_size,
|
||||
wrkmem);
|
||||
free(wrkmem);
|
||||
working += compressed_size;
|
||||
//fprintf(stderr, "Compressed %u bytes (compressed size: %lu)\n", uncompressed_size, compressed_size);
|
||||
|
||||
if (F16(sh->size) > F32(sh->uncompressed_size)) {
|
||||
// Goomba header (not Goomba Color)
|
||||
new_sh->uncompressed_size = F32(compressed_size);
|
||||
}
|
||||
|
||||
new_sh->size = F16((uint16_t)(compressed_size + sizeof(stateheader)));
|
||||
// pad to 4 bytes!
|
||||
// if I don't do this, goomba color might not load the palette settings, or seemingly 'forget' them later
|
||||
// btw, the settings are stored in the configdata struct defined in goombasav.h
|
||||
uint16_t s = F16(new_sh->size);
|
||||
while (s % 4 != 0) {
|
||||
*working = 0;
|
||||
working++;
|
||||
s++;
|
||||
}
|
||||
new_sh->size = F16(s);
|
||||
}
|
||||
|
||||
goomba_size_t used = working - goomba_new_sav;
|
||||
if (used + backup_len > GOOMBA_COLOR_AVAILABLE_SIZE) {
|
||||
goomba_error("Not enough room in file for the new save data (0xe000-0xffff must be kept free, I think)\n");
|
||||
free(backup);
|
||||
free(goomba_new_sav);
|
||||
return NULL;
|
||||
}
|
||||
// restore the backup - just assume we have enough space
|
||||
memcpy(working, backup, backup_len);
|
||||
free(backup);
|
||||
|
||||
// restore data from 0xe000 to 0xffff
|
||||
memcpy(goomba_new_sav + GOOMBA_COLOR_AVAILABLE_SIZE,
|
||||
(char*)gba_data + GOOMBA_COLOR_AVAILABLE_SIZE,
|
||||
GOOMBA_COLOR_SRAM_SIZE - GOOMBA_COLOR_AVAILABLE_SIZE);
|
||||
|
||||
return goomba_new_sav;
|
||||
}
|
231
source/pocketnes/goombasav.h
Normal file
231
source/pocketnes/goombasav.h
Normal file
@ -0,0 +1,231 @@
|
||||
/* goombasav.h - functions to handle Goomba / Goomba Color SRAM
|
||||
|
||||
Copyright (C) 2014-2017 libertyernie
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
https://github.com/libertyernie/goombasav
|
||||
|
||||
When compiling in Visual Studio, set all goombasav files to compile
|
||||
as C++ code (Properties -> C/C++ -> Advanced -> Compile As.)
|
||||
*/
|
||||
|
||||
#ifndef __GOOMBASAV_H
|
||||
#define __GOOMBASAV_H
|
||||
|
||||
#include <stdint.h>
|
||||
#define GOOMBA_COLOR_SRAM_SIZE 65536
|
||||
#define GOOMBA_COLOR_AVAILABLE_SIZE 57344
|
||||
#define GOOMBA_STATEID 0x57a731dc
|
||||
#define POCKETNES_STATEID 0x57a731d7
|
||||
#define POCKETNES_STATEID2 0x57a731d8
|
||||
#define SMSADVANCE_STATEID 0x57a731dc
|
||||
#define GOOMBA_STATESAVE 0
|
||||
#define GOOMBA_SRAMSAVE 1
|
||||
#define GOOMBA_CONFIGSAVE 2
|
||||
#define GOOMBA_PALETTE 5
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef uint32_t goomba_size_t; // want a consistent size for printf. This is an alias for uint32_t, but this makes it clear that we're counting the size of something.
|
||||
|
||||
/* 16-bit and 32-bit values in the stateheader are stored as little endian
|
||||
(native to the GBA's ARM chip as well as x86 processors.) Use this function
|
||||
after getting or before setting a value if your code might run on a big-endian
|
||||
processor (e.g. PowerPC.) */
|
||||
uint16_t little_endian_conv_16(uint16_t value);
|
||||
|
||||
/* 16-bit and 32-bit values in the stateheader are stored as little endian
|
||||
(native to the GBA's ARM chip as well as x86 processors.) Use this function
|
||||
after getting or before setting a value if your code might run on a big-endian
|
||||
processor (e.g. PowerPC.) */
|
||||
uint32_t little_endian_conv_32(uint32_t value);
|
||||
|
||||
typedef struct {
|
||||
uint16_t size;
|
||||
uint16_t type; //=CONFIGSAVE
|
||||
} configdata;
|
||||
|
||||
typedef struct {
|
||||
uint16_t size; //=48 (0x30)
|
||||
uint16_t type; //=CONFIGSAVE
|
||||
char bordercolor;
|
||||
char palettebank;
|
||||
char misc;
|
||||
char reserved3;
|
||||
uint32_t sram_checksum; //checksum of rom using SRAM e000-ffff
|
||||
uint32_t zero; //=0
|
||||
char reserved4[32]; //="CFG"
|
||||
} goomba_configdata;
|
||||
|
||||
typedef struct {
|
||||
uint16_t size; //=48 (0x30)
|
||||
uint16_t type; //=CONFIGSAVE
|
||||
char displaytype;
|
||||
char misc;
|
||||
char reserved2;
|
||||
char reserved3;
|
||||
uint32_t sram_checksum; //checksum of rom using SRAM e000-ffff
|
||||
uint32_t zero; //=0
|
||||
char reserved4[32]; //="CFG"
|
||||
} pocketnes_configdata;
|
||||
|
||||
typedef struct {
|
||||
uint16_t size; //=52 (0x34)
|
||||
uint16_t type; //=CONFIGSAVE
|
||||
char displaytype;
|
||||
char gammavalue;
|
||||
char region; // bit 0 & 1 = region.
|
||||
char sleepflick;
|
||||
char config;
|
||||
char bcolor;
|
||||
char reserved1;
|
||||
char reserved2;
|
||||
uint32_t sram_checksum; //checksum of rom using SRAM e000-ffff
|
||||
uint32_t zero; //=0
|
||||
char reserved3[32]; //="CFG"
|
||||
} smsadvance_configdata;
|
||||
|
||||
typedef struct {
|
||||
uint16_t size; //header+data
|
||||
uint16_t type; //=STATESAVE or SRAMSAVE
|
||||
uint32_t uncompressed_size;
|
||||
uint32_t framecount;
|
||||
uint32_t checksum;
|
||||
char title[32];
|
||||
} stateheader;
|
||||
|
||||
typedef struct {
|
||||
const char* sleep;
|
||||
const char* autoload_state;
|
||||
const char* gamma;
|
||||
} configdata_misc_strings;
|
||||
|
||||
/**
|
||||
* Returns the last error encountered by goombasav (for functions that return
|
||||
* NULL on error.) This string is stored statically by goombasav and its
|
||||
* contents may change over time.
|
||||
*/
|
||||
const char* goomba_last_error();
|
||||
|
||||
/**
|
||||
* Set the string buffer used by goomba_last_error to a custom value. If the
|
||||
* input string is too long, the resulting goomba_last_error string will be
|
||||
* truncated.
|
||||
* Returns the number of bytes copied to the buffer (at present, the maximum
|
||||
* is 255.)
|
||||
*/
|
||||
size_t goomba_set_last_error(const char* msg);
|
||||
|
||||
/**
|
||||
* Gets a struct containing pointers to three static strings (which do not
|
||||
* need to be deallocated.)
|
||||
*/
|
||||
configdata_misc_strings configdata_get_misc(char misc);
|
||||
|
||||
/**
|
||||
* Returns a multi-line description string that includes all parameters of the
|
||||
* given stateheader or configdata structure.
|
||||
* This string is stored in a static character buffer, and subsequent calls to
|
||||
* stateheader_str or stateheader_summary_str will overwrite this buffer.
|
||||
*/
|
||||
const char* stateheader_str(const stateheader* sh);
|
||||
|
||||
/**
|
||||
* Returns a one-line summary string displaying the size and title of the
|
||||
* stateheader or configdata structure.
|
||||
* This string is stored in a static character buffer, and subsequent calls to
|
||||
* stateheader_str or stateheader_summary_str will overwrite this buffer.
|
||||
*/
|
||||
const char* stateheader_summary_str(const stateheader* sh);
|
||||
|
||||
int stateheader_plausible(const void* sh);
|
||||
|
||||
/**
|
||||
* When given a pointer to a stateheader, returns a pointer to where the next
|
||||
* stateheader will be located (if any). Use stateheader_plausible to
|
||||
* determine if there really is a header at this location.
|
||||
*
|
||||
* If stateheader_plausible determines that the input is not a valid
|
||||
* stateheader, NULL will be returned.
|
||||
*/
|
||||
stateheader* stateheader_advance(const stateheader* sh);
|
||||
|
||||
/**
|
||||
* Scans for valid stateheaders and allocates an array to store them. The array
|
||||
* will have a capacity of 64, and any difference between that
|
||||
* and the number of headers found will be filled with NULL entries. The last
|
||||
* entry (array[63]) is guaranteed to be NULL.
|
||||
* NOTE: the gba_data parameter can point to a valid header, or to a sequence
|
||||
* equal to GOOMBA_STATEID immediately before a valid header.
|
||||
*/
|
||||
const stateheader** stateheader_scan(const void* gba_data);
|
||||
|
||||
/**
|
||||
* Returns the stateheader in gba_data with the title field = gbc_title,
|
||||
* or NULL if there is none. Only the first 15 bytes of gbc_title will be
|
||||
* used in the comparison.
|
||||
*/
|
||||
const stateheader* stateheader_for(const void* gba_data, const char* gbc_title_ptr);
|
||||
|
||||
/**
|
||||
* Returns true if the given data starts with GOOMBA_STATEID (little endian.)
|
||||
*/
|
||||
int goomba_is_sram(const void* data);
|
||||
|
||||
/**
|
||||
* Returns the 32-bit checksum (unsigned) in the configdata header, or -1 if
|
||||
* an error occurred.
|
||||
*/
|
||||
int64_t goomba_get_configdata_checksum_field(const void* gba_data);
|
||||
|
||||
/**
|
||||
* Computes a simple additive checksum of the compressed data that comes after
|
||||
* the given header, using anywhere from 1 to 8 bytes.
|
||||
*/
|
||||
uint64_t goomba_compressed_data_checksum(const stateheader* sh, int output_bytes);
|
||||
|
||||
/**
|
||||
* If there is save data in 0xe000-0xffff (as signaled by the configdata),
|
||||
* this function compresses it to where it's supposed to go. In the event that
|
||||
* the data passed in is already clean, the same pointer will be returned.
|
||||
* NULL will be returned if an error occurs.
|
||||
*
|
||||
* The given pointer must be at least GOOMBA_COLOR_SRAM_SIZE bytes in length.
|
||||
* If it is longer, any information after GOOMBA_COLOR_SRAM_SIZE bytes will be
|
||||
* ignored.
|
||||
*/
|
||||
char* goomba_cleanup(const void* gba_data_param);
|
||||
|
||||
/**
|
||||
* Allocates memory to store the uncompressed GB/GBC save file extracted from
|
||||
* the Goomba Color save file stored in header_ptr, or returns NULL if the
|
||||
* decompression failed.
|
||||
*/
|
||||
void* goomba_extract(const void* gba_data, const stateheader* header_ptr, goomba_size_t* size_output);
|
||||
|
||||
/**
|
||||
* Copies data from gba_data to a new buffer allocated with malloc, replacing
|
||||
* the data of the section pointed to by gba_header with a compressed version
|
||||
* of the data pointed to by gbc_sram. If gbc_length is 0, the section pointed
|
||||
* to by gba_header will be removed instead of replaced.
|
||||
*/
|
||||
char* goomba_new_sav(const void* gba_data, const void* gba_header, const void* gbc_sram, goomba_size_t gbc_length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
339
source/pocketnes/minilzo/COPYING
Normal file
339
source/pocketnes/minilzo/COPYING
Normal file
@ -0,0 +1,339 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
123
source/pocketnes/minilzo/README.LZO
Normal file
123
source/pocketnes/minilzo/README.LZO
Normal file
@ -0,0 +1,123 @@
|
||||
|
||||
============================================================================
|
||||
miniLZO -- mini subset of the LZO real-time data compression library
|
||||
============================================================================
|
||||
|
||||
Author : Markus Franz Xaver Johannes Oberhumer
|
||||
<markus@oberhumer.com>
|
||||
http://www.oberhumer.com/opensource/lzo/
|
||||
Version : 2.09
|
||||
Date : 04 Feb 2015
|
||||
|
||||
I've created miniLZO for projects where it is inconvenient to
|
||||
include (or require) the full LZO source code just because you
|
||||
want to add a little bit of data compression to your application.
|
||||
|
||||
miniLZO implements the LZO1X-1 compressor and both the standard and
|
||||
safe LZO1X decompressor. Apart from fast compression it also useful
|
||||
for situations where you want to use pre-compressed data files (which
|
||||
must have been compressed with LZO1X-999).
|
||||
|
||||
miniLZO consists of one C source file and three header files:
|
||||
minilzo.c
|
||||
minilzo.h, lzoconf.h, lzodefs.h
|
||||
|
||||
To use miniLZO just copy these files into your source directory, add
|
||||
minilzo.c to your Makefile and #include minilzo.h from your program.
|
||||
Note: you also must distribute this file ('README.LZO') with your project.
|
||||
|
||||
minilzo.o compiles to about 6 KiB (using gcc or Visual C on an i386), and
|
||||
the sources are about 30 KiB when packed with zip - so there's no more
|
||||
excuse that your application doesn't support data compression :-)
|
||||
|
||||
For more information, documentation, example programs and other support
|
||||
files (like Makefiles and build scripts) please download the full LZO
|
||||
package from
|
||||
http://www.oberhumer.com/opensource/lzo/
|
||||
|
||||
Have fun,
|
||||
Markus
|
||||
|
||||
|
||||
P.S. minilzo.c is generated automatically from the LZO sources and
|
||||
therefore functionality is completely identical
|
||||
|
||||
|
||||
Appendix A: building miniLZO
|
||||
----------------------------
|
||||
miniLZO is written such a way that it should compile and run
|
||||
out-of-the-box on most machines.
|
||||
|
||||
If you are running on a very unusual architecture and lzo_init() fails then
|
||||
you should first recompile with '-DLZO_DEBUG' to see what causes the failure.
|
||||
The most probable case is something like 'sizeof(void *) != sizeof(size_t)'.
|
||||
After identifying the problem you can compile by adding some defines
|
||||
like '-DSIZEOF_VOID_P=8' to your Makefile.
|
||||
|
||||
The best solution is (of course) using Autoconf - if your project uses
|
||||
Autoconf anyway just add '-DMINILZO_HAVE_CONFIG_H' to your compiler
|
||||
flags when compiling minilzo.c. See the LZO distribution for an example
|
||||
how to set up configure.ac.
|
||||
|
||||
|
||||
Appendix B: list of public functions available in miniLZO
|
||||
---------------------------------------------------------
|
||||
Library initialization
|
||||
lzo_init()
|
||||
|
||||
Compression
|
||||
lzo1x_1_compress()
|
||||
|
||||
Decompression
|
||||
lzo1x_decompress()
|
||||
lzo1x_decompress_safe()
|
||||
|
||||
Checksum functions
|
||||
lzo_adler32()
|
||||
|
||||
Version functions
|
||||
lzo_version()
|
||||
lzo_version_string()
|
||||
lzo_version_date()
|
||||
|
||||
Portable (but slow) string functions
|
||||
lzo_memcmp()
|
||||
lzo_memcpy()
|
||||
lzo_memmove()
|
||||
lzo_memset()
|
||||
|
||||
|
||||
Appendix C: suggested macros for 'configure.ac' when using Autoconf
|
||||
-------------------------------------------------------------------
|
||||
Checks for typedefs and structures
|
||||
AC_CHECK_TYPE(ptrdiff_t,long)
|
||||
AC_TYPE_SIZE_T
|
||||
AC_CHECK_SIZEOF(short)
|
||||
AC_CHECK_SIZEOF(int)
|
||||
AC_CHECK_SIZEOF(long)
|
||||
AC_CHECK_SIZEOF(long long)
|
||||
AC_CHECK_SIZEOF(__int64)
|
||||
AC_CHECK_SIZEOF(void *)
|
||||
AC_CHECK_SIZEOF(size_t)
|
||||
AC_CHECK_SIZEOF(ptrdiff_t)
|
||||
|
||||
Checks for compiler characteristics
|
||||
AC_C_CONST
|
||||
|
||||
Checks for library functions
|
||||
AC_CHECK_FUNCS(memcmp memcpy memmove memset)
|
||||
|
||||
|
||||
Appendix D: Copyright
|
||||
---------------------
|
||||
LZO and miniLZO are Copyright (C) 1996-2015 Markus Franz Xaver Oberhumer
|
||||
All Rights Reserved.
|
||||
|
||||
LZO and miniLZO are distributed under the terms of the GNU General
|
||||
Public License (GPL). See the file COPYING.
|
||||
|
||||
Special licenses for commercial and other applications which
|
||||
are not willing to accept the GNU General Public License
|
||||
are available by contacting the author.
|
||||
|
||||
|
3134
source/pocketnes/minilzo/lzodefs.h
Normal file
3134
source/pocketnes/minilzo/lzodefs.h
Normal file
File diff suppressed because it is too large
Load Diff
6231
source/pocketnes/minilzo/minilzo.cpp
Normal file
6231
source/pocketnes/minilzo/minilzo.cpp
Normal file
File diff suppressed because it is too large
Load Diff
106
source/pocketnes/minilzo/minilzo.h
Normal file
106
source/pocketnes/minilzo/minilzo.h
Normal file
@ -0,0 +1,106 @@
|
||||
/* minilzo.h -- mini subset of the LZO real-time data compression library
|
||||
|
||||
This file is part of the LZO real-time data compression library.
|
||||
|
||||
Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer
|
||||
All Rights Reserved.
|
||||
|
||||
The LZO library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
The LZO library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with the LZO library; see the file COPYING.
|
||||
If not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
Markus F.X.J. Oberhumer
|
||||
<markus@oberhumer.com>
|
||||
http://www.oberhumer.com/opensource/lzo/
|
||||
*/
|
||||
|
||||
/*
|
||||
* NOTE:
|
||||
* the full LZO package can be found at
|
||||
* http://www.oberhumer.com/opensource/lzo/
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __MINILZO_H_INCLUDED
|
||||
#define __MINILZO_H_INCLUDED 1
|
||||
|
||||
#define MINILZO_VERSION 0x2090
|
||||
|
||||
#if defined(__LZOCONF_H_INCLUDED)
|
||||
# error "you cannot use both LZO and miniLZO"
|
||||
#endif
|
||||
|
||||
/* internal Autoconf configuration file - only used when building miniLZO */
|
||||
#ifdef MINILZO_HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef __LZODEFS_H_INCLUDED
|
||||
#include "lzodefs.h"
|
||||
#endif
|
||||
#undef LZO_HAVE_CONFIG_H
|
||||
#include "lzoconf.h"
|
||||
|
||||
#if !defined(LZO_VERSION) || (LZO_VERSION != MINILZO_VERSION)
|
||||
# error "version mismatch in header files"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
//
|
||||
************************************************************************/
|
||||
|
||||
/* Memory required for the wrkmem parameter.
|
||||
* When the required size is 0, you can also pass a NULL pointer.
|
||||
*/
|
||||
|
||||
#define LZO1X_MEM_COMPRESS LZO1X_1_MEM_COMPRESS
|
||||
#define LZO1X_1_MEM_COMPRESS ((lzo_uint32_t) (16384L * lzo_sizeof_dict_t))
|
||||
#define LZO1X_MEM_DECOMPRESS (0)
|
||||
|
||||
|
||||
/* compression */
|
||||
LZO_EXTERN(int)
|
||||
lzo1x_1_compress ( const lzo_bytep src, lzo_uint src_len,
|
||||
lzo_bytep dst, lzo_uintp dst_len,
|
||||
lzo_voidp wrkmem );
|
||||
|
||||
/* decompression */
|
||||
LZO_EXTERN(int)
|
||||
lzo1x_decompress ( const lzo_bytep src, lzo_uint src_len,
|
||||
lzo_bytep dst, lzo_uintp dst_len,
|
||||
lzo_voidp wrkmem /* NOT USED */ );
|
||||
|
||||
/* safe decompression with overrun testing */
|
||||
LZO_EXTERN(int)
|
||||
lzo1x_decompress_safe ( const lzo_bytep src, lzo_uint src_len,
|
||||
lzo_bytep dst, lzo_uintp dst_len,
|
||||
lzo_voidp wrkmem /* NOT USED */ );
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* already included */
|
||||
|
||||
|
||||
/* vim:set ts=4 sw=4 et: */
|
117
source/pocketnes/pocketnesrom.cpp
Normal file
117
source/pocketnes/pocketnesrom.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
/* pocketnesrom.c - functions to find uncompressed NES ROM images
|
||||
stored within PocketNES ROMs
|
||||
|
||||
Copyright (C) 2016 libertyernie
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
When compiling in Visual Studio, set the project to compile
|
||||
as C++ code (Properties -> C/C++ -> Advanced -> Compile As.)
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "pocketnesrom.h"
|
||||
|
||||
const char NES_WORD[4] = { 'N','E','S',0x1A };
|
||||
|
||||
/* Finds the first PocketNES ROM header in the given data block by looking for
|
||||
the segment 4E45531A (N,E,S,^Z) in the ROM itself. If no valid data is found,
|
||||
this method will return NULL. */
|
||||
const pocketnes_romheader* pocketnes_first_rom(const void* data, size_t length) {
|
||||
const char* ptr = (const char*)data;
|
||||
const char* end = ptr + length;
|
||||
int logo_pos = 0;
|
||||
while (ptr < end) {
|
||||
if (*ptr == NES_WORD[logo_pos]) {
|
||||
// match
|
||||
logo_pos++;
|
||||
if (logo_pos == 4) { // matched all of GB logo - on last byte (0x133)
|
||||
// check if length fits
|
||||
const pocketnes_romheader* candidate = (pocketnes_romheader*)(ptr - 3 - sizeof(pocketnes_romheader));
|
||||
size_t filesize = candidate->filesize;
|
||||
if (*(uint16_t *)"\0\xff" < 0x100) {
|
||||
uint32_t buffer;
|
||||
((char*)&buffer)[0] = ((char*)&filesize)[3];
|
||||
((char*)&buffer)[1] = ((char*)&filesize)[2];
|
||||
((char*)&buffer)[2] = ((char*)&filesize)[1];
|
||||
((char*)&buffer)[3] = ((char*)&filesize)[0];
|
||||
filesize = buffer;
|
||||
}
|
||||
const char* candidate_end_ptr = (ptr - 3) + filesize;
|
||||
if (candidate_end_ptr <= end) {
|
||||
return candidate;
|
||||
} else {
|
||||
// no match, try again
|
||||
logo_pos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// no match, try again
|
||||
if (logo_pos > 0) {
|
||||
ptr -= logo_pos;
|
||||
logo_pos = 0;
|
||||
}
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Returns a pointer to the next PocketNES ROM header in the data. If the
|
||||
location where the next ROM header would be does not contain a 'N,E,S,^Z'
|
||||
segment at the start of the ROM, this method will return NULL. */
|
||||
const pocketnes_romheader* pocketnes_next_rom(const void* data, size_t length, const pocketnes_romheader* first_rom) {
|
||||
size_t diff = (const char*)first_rom - (const char*)data;
|
||||
if (diff > length) {
|
||||
return NULL;
|
||||
}
|
||||
size_t effective_length = length - diff;
|
||||
if (effective_length <= 0x200) {
|
||||
// Assume there will never be a ROM this small
|
||||
return NULL;
|
||||
}
|
||||
return pocketnes_first_rom(
|
||||
(const char*)first_rom + 4 + sizeof(pocketnes_romheader),
|
||||
effective_length - 4 - sizeof(pocketnes_romheader));
|
||||
}
|
||||
|
||||
/* Returns true if the given data region looks like a PocketNES ROM header
|
||||
(based on the 'N,E,S,^Z' segment), or false otherwise. */
|
||||
int pocketnes_is_romheader(const void* data) {
|
||||
const char* rom_ptr = (const char*)data + sizeof(pocketnes_romheader);
|
||||
return memcmp(rom_ptr, NES_WORD, 4) == 0;
|
||||
}
|
||||
|
||||
/* Returns the checksum that PocketNES would use for this ROM.
|
||||
You can pass the PocketNES ROM header or the NES ROM itself. */
|
||||
uint32_t pocketnes_get_checksum(const void* rom) {
|
||||
const uint8_t* p = (const uint8_t*)rom;
|
||||
|
||||
// Checksum should not include NES ROM format header
|
||||
if (memcmp(p, NES_WORD, 4) == 0) {
|
||||
p += 16;
|
||||
}
|
||||
|
||||
// TODO: add support for compressed roms
|
||||
|
||||
uint32_t sum = 0;
|
||||
int i;
|
||||
for (i = 0; i<128; i++) {
|
||||
sum += *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
|
||||
p += 128;
|
||||
}
|
||||
return sum;
|
||||
}
|
58
source/pocketnes/pocketnesrom.h
Normal file
58
source/pocketnes/pocketnesrom.h
Normal file
@ -0,0 +1,58 @@
|
||||
/* pocketnesrom.h - functions to find uncompressed NES ROM images
|
||||
stored within PocketNES ROMs
|
||||
|
||||
Copyright (C) 2016 libertyernie
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
When compiling in Visual Studio, set the project to compile
|
||||
as C++ code (Properties -> C/C++ -> Advanced -> Compile As.)
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char name[32];
|
||||
uint32_t filesize;
|
||||
uint32_t flags;
|
||||
uint32_t spritefollow;
|
||||
uint32_t reserved;
|
||||
} pocketnes_romheader;
|
||||
|
||||
/* Finds the first PocketNES ROM header in the given data block by looking for
|
||||
the segment 4E45531A (N,E,S,^Z) in the ROM itself. If no valid data is found,
|
||||
this method will return NULL. */
|
||||
const pocketnes_romheader* pocketnes_first_rom(const void* data, size_t length);
|
||||
|
||||
/* Returns a pointer to the next PocketNES ROM header in the data. If the
|
||||
location where the next ROM header would be does not contain a 'N,E,S,^Z'
|
||||
segment at the start of the ROM, this method will return NULL. */
|
||||
const pocketnes_romheader* pocketnes_next_rom(const void* data, size_t length, const pocketnes_romheader* first_rom);
|
||||
|
||||
/* Returns true if the given data region looks like a PocketNES ROM header
|
||||
(based on the 'N,E,S,^Z' segment), or false otherwise. */
|
||||
int pocketnes_is_romheader(const void* data);
|
||||
|
||||
/* Returns the checksum that PocketNES would use for this ROM.
|
||||
You can pass the PocketNES ROM header or the NES ROM itself. */
|
||||
uint32_t pocketnes_get_checksum(const void* rom);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user