This commit is contained in:
dborth 2010-06-20 00:01:57 +00:00
parent c270e2613a
commit 4a6ca4b34d

View File

@ -61,21 +61,6 @@ static s64 readInt4(MFILE *f) {
return res; return res;
} }
static s64 readInt8(MFILE *f) {
s64 tmp, res = 0;
int c;
for (int i = 0; i < 8; i++) {
c = memfgetc(f);
if (c == MEOF)
return -1;
tmp = c;
res = res + (tmp << (i * 8));
}
return res;
}
static s64 readVarPtr(MFILE *f) { static s64 readVarPtr(MFILE *f) {
s64 offset = 0, shift = 1; s64 offset = 0, shift = 1;
for (;;) { for (;;) {
@ -239,188 +224,20 @@ bool patchApplyUPS(MFILE * f, u8 **rom, int *size) {
return true; return true;
} }
static int ppfVersion(MFILE *f) {
memfseek(f, 0, MSEEK_SET);
if (memfgetc(f) != 'P' || memfgetc(f) != 'P' || memfgetc(f) != 'F')
return 0;
switch (memfgetc(f)) {
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
default:
return 0;
}
}
int ppfFileIdLen(MFILE *f, int version) {
if (version == 2) {
memfseek(f, -8, MSEEK_END);
} else {
memfseek(f, -6, MSEEK_END);
}
if (memfgetc(f) != '.' || memfgetc(f) != 'D' || memfgetc(f) != 'I'
|| memfgetc(f) != 'Z')
return 0;
return (version == 2) ? readInt4(f) : readInt2(f);
}
static bool patchApplyPPF1(MFILE *f, u8 **rom, int *size) {
memfseek(f, 0, MSEEK_END);
int count = memftell(f);
if (count < 56)
return false;
count -= 56;
memfseek(f, 56, MSEEK_SET);
u8 *mem = *rom;
while (count > 0) {
int offset = readInt4(f);
if (offset == -1)
break;
int len = memfgetc(f);
if (len == MEOF)
break;
if (offset + len > *size)
break;
if (memfread(&mem[offset], 1, len, f) != (size_t) len)
break;
count -= 4 + 1 + len;
}
return (count == 0);
}
static bool patchApplyPPF2(MFILE *f, u8 **rom, int *size) {
memfseek(f, 0, MSEEK_END);
int count = memftell(f);
if (count < 56 + 4 + 1024)
return false;
count -= 56 + 4 + 1024;
memfseek(f, 56, MSEEK_SET);
int datalen = readInt4(f);
if (datalen != *size)
return false;
u8 *mem = *rom;
u8 block[1024];
memfread(&block, 1, 1024, f);
if (memcmp(&mem[0x9320], &block, 1024) != 0)
return false;
int idlen = ppfFileIdLen(f, 2);
if (idlen > 0)
count -= 16 + 16 + idlen;
memfseek(f, 56 + 4 + 1024, MSEEK_SET);
while (count > 0) {
int offset = readInt4(f);
if (offset == -1)
break;
int len = memfgetc(f);
if (len == MEOF)
break;
if (offset + len > *size)
break;
if (memfread(&mem[offset], 1, len, f) != (size_t) len)
break;
count -= 4 + 1 + len;
}
return (count == 0);
}
static bool patchApplyPPF3(MFILE *f, u8 **rom, int *size) {
memfseek(f, 0, MSEEK_END);
int count = memftell(f);
if (count < 56 + 4 + 1024)
return false;
count -= 56 + 4;
memfseek(f, 56, MSEEK_SET);
int imagetype = memfgetc(f);
int blockcheck = memfgetc(f);
int undo = memfgetc(f);
memfgetc(f);
u8 *mem = *rom;
if (blockcheck) {
u8 block[1024];
memfread(&block, 1, 1024, f);
if (memcmp(&mem[(imagetype == 0) ? 0x9320 : 0x80A0], &block, 1024) != 0)
return false;
count -= 1024;
}
int idlen = ppfFileIdLen(f, 2);
if (idlen > 0)
count -= 16 + 16 + idlen;
memfseek(f, 56 + 4 + (blockcheck ? 1024 : 0), MSEEK_SET);
while (count > 0) {
s64 offset = readInt8(f);
if (offset == -1)
break;
int len = memfgetc(f);
if (len == MEOF)
break;
if (offset + len > *size)
break;
if (memfread(&mem[offset], 1, len, f) != (size_t) len)
break;
if (undo)
memfseek(f, len, MSEEK_CUR);
count -= 8 + 1 + len;
if (undo)
count -= len;
}
return (count == 0);
}
bool patchApplyPPF(MFILE *f, u8 **rom, int *size)
{
bool res = false;
int version = ppfVersion(f);
switch (version)
{
case 1: res = patchApplyPPF1(f, rom, size); break;
case 2: res = patchApplyPPF2(f, rom, size); break;
case 3: res = patchApplyPPF3(f, rom, size); break;
}
return res;
}
int LoadPatch(int size) int LoadPatch(int size)
{ {
int newsize = size; int newsize = size;
int patchsize = 0; int patchsize = 0;
int patchtype; int patchtype;
char patchpath[3][512]; char patchpath[2][512];
AllocSaveBuffer (); AllocSaveBuffer ();
memset(patchpath, 0, sizeof(patchpath)); memset(patchpath, 0, sizeof(patchpath));
sprintf(patchpath[0], "%s%s.ips", browser.dir, romFilename); sprintf(patchpath[0], "%s%s.ips", browser.dir, romFilename);
sprintf(patchpath[1], "%s%s.ups", browser.dir, romFilename); sprintf(patchpath[1], "%s%s.ups", browser.dir, romFilename);
sprintf(patchpath[2], "%s%s.ppf", browser.dir, romFilename);
for(patchtype=0; patchtype<3; patchtype++) for(patchtype=0; patchtype<2; patchtype++)
{ {
patchsize = LoadFile(patchpath[patchtype], SILENT); patchsize = LoadFile(patchpath[patchtype], SILENT);
@ -435,10 +252,8 @@ int LoadPatch(int size)
if(patchtype == 0) if(patchtype == 0)
patchApplyIPS(mf, &nesrom, &newsize); patchApplyIPS(mf, &nesrom, &newsize);
else if(patchtype == 1)
patchApplyUPS(mf, &nesrom, &newsize);
else else
patchApplyPPF(mf, &nesrom, &newsize); patchApplyUPS(mf, &nesrom, &newsize);
memfclose(mf); // close memory file memfclose(mf); // close memory file
} }