1 /* PPF Patch Support for PCSX-Reloaded
2 * Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
4 * Based on P.E.Op.S CDR Plugin by Pete Bernert.
5 * Copyright (c) 2002, Pete Bernert.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA
22 #include "psxcommon.h"
26 typedef struct tagPPF_DATA {
30 struct tagPPF_DATA *pNext;
33 typedef struct tagPPF_CACHE {
35 struct tagPPF_DATA *pNext;
38 static PPF_CACHE *ppfCache = NULL;
39 static PPF_DATA *ppfHead = NULL, *ppfLast = NULL;
40 static int iPPFNum = 0;
42 // using a linked data list, and address array
43 static void FillPPFCache() {
53 if (p->addr != lastaddr) iPPFNum++;
58 if (iPPFNum <= 0) return;
60 pc = ppfCache = (PPF_CACHE *)malloc(iPPFNum * sizeof(PPF_CACHE));
61 if (pc == NULL) return;
68 if (p->addr != lastaddr) {
79 PPF_DATA *p = ppfHead;
90 if (ppfCache != NULL) free(ppfCache);
94 void CheckPPFCache(unsigned char *pB, unsigned char m, unsigned char s, unsigned char f) {
95 PPF_CACHE *pcstart, *pcend, *pcpos;
96 int addr = MSF2SECT(btoi(m), btoi(s), btoi(f)), pos, anz, start;
98 if (ppfCache == NULL) return;
101 if (addr < pcstart->addr) return;
102 pcend = ppfCache + iPPFNum;
103 if (addr > pcend->addr) return;
106 if (addr == pcend->addr) { pcpos = pcend; break; }
108 pcpos = pcstart + (pcend - pcstart) / 2;
109 if (pcpos == pcstart) break;
110 if (addr < pcpos->addr) {
114 if (addr > pcpos->addr) {
121 if (addr == pcpos->addr) {
122 PPF_DATA *p = pcpos->pNext;
123 while (p != NULL && p->addr == addr) {
124 pos = p->pos - (CD_FRAMESIZE_RAW - DATA_SIZE);
126 if (pos < 0) { start = -pos; pos = 0; anz -= start; }
128 memcpy(pB + pos, (unsigned char *)(p + 1) + start, anz);
134 static void AddToPPF(s32 ladr, s32 pos, s32 anz, unsigned char *ppfmem) {
135 if (ppfHead == NULL) {
136 ppfHead = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz);
137 if (ppfHead == NULL) return;
138 ppfHead->addr = ladr;
139 ppfHead->pNext = NULL;
142 memcpy(ppfHead + 1, ppfmem, anz);
146 PPF_DATA *p = ppfHead;
147 PPF_DATA *plast = NULL;
150 if (ladr > ppfLast->addr || (ladr == ppfLast->addr && pos > ppfLast->pos)) {
155 if (ladr < p->addr) break;
156 if (ladr == p->addr) {
157 while (p && ladr == p->addr && pos > p->pos) {
168 padd = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz);
169 if (padd == NULL) return;
174 memcpy(padd + 1, ppfmem, anz);
176 if (plast == NULL) ppfHead = padd;
177 else plast->pNext = padd;
179 if (padd->pNext == NULL) ppfLast = padd;
183 void BuildPPFCache() {
186 char method, undo = 0, blockcheck = 0;
188 unsigned char ppfmem[512];
189 char szPPF[MAXPATHLEN * 2];
190 int count, seekpos, pos;
191 u32 anz; // use 32-bit to avoid stupid overflows
196 if (CdromId[0] == '\0') return;
198 // Generate filename in the format of SLUS_123.45
199 buffer[0] = toupper(CdromId[0]);
200 buffer[1] = toupper(CdromId[1]);
201 buffer[2] = toupper(CdromId[2]);
202 buffer[3] = toupper(CdromId[3]);
204 buffer[5] = CdromId[4];
205 buffer[6] = CdromId[5];
206 buffer[7] = CdromId[6];
208 buffer[9] = CdromId[7];
209 buffer[10] = CdromId[8];
212 sprintf(szPPF, "%s%s", Config.PatchesDir, buffer);
214 ppffile = fopen(szPPF, "rb");
215 if (ppffile == NULL) return;
217 memset(buffer, 0, 5);
218 if (fread(buffer, 1, 3, ppffile) != 3)
221 if (strcmp(buffer, "PPF") != 0) {
222 SysPrintf(_("Invalid PPF patch: %s.\n"), szPPF);
227 fseek(ppffile, 5, SEEK_SET);
228 method = fgetc(ppffile);
232 fseek(ppffile, 0, SEEK_END);
233 count = ftell(ppffile);
239 fseek(ppffile, -8, SEEK_END);
241 memset(buffer, 0, 5);
242 if (fread(buffer, 1, 4, ppffile) != 4)
245 if (strcmp(".DIZ", buffer) != 0) {
248 if (fread(&dizlen, 1, 4, ppffile) != 4)
250 dizlen = SWAP32(dizlen);
254 fseek(ppffile, 0, SEEK_END);
255 count = ftell(ppffile);
269 fseek(ppffile, 57, SEEK_SET);
270 blockcheck = fgetc(ppffile);
271 undo = fgetc(ppffile);
273 fseek(ppffile, -6, SEEK_END);
274 memset(buffer, 0, 5);
275 if (fread(buffer, 1, 4, ppffile) != 4)
279 if (strcmp(".DIZ", buffer) == 0) {
280 fseek(ppffile, -2, SEEK_END);
281 // TODO: Endian/size unsafe?
282 if (fread(&dizlen, 1, 2, ppffile) != 2)
284 dizlen = SWAP32(dizlen);
288 fseek(ppffile, 0, SEEK_END);
289 count = ftell(ppffile);
303 SysPrintf(_("Unsupported PPF version (%d).\n"), method + 1);
307 // now do the data reading
309 fseek(ppffile, seekpos, SEEK_SET);
310 if (fread(&pos, 1, sizeof(pos), ppffile) != sizeof(pos))
315 // skip 4 bytes on ppf3 (no int64 support here)
316 if (fread(buffer, 1, 4, ppffile) != 4)
320 anz = fgetc(ppffile);
321 if (fread(ppfmem, 1, anz, ppffile) != anz)
324 ladr = pos / CD_FRAMESIZE_RAW;
325 off = pos % CD_FRAMESIZE_RAW;
327 if (off + anz > CD_FRAMESIZE_RAW) {
328 anx = off + anz - CD_FRAMESIZE_RAW;
329 anz -= (unsigned char)anx;
330 AddToPPF(ladr + 1, 0, anx, &ppfmem[anz]);
333 AddToPPF(ladr, off, anz, ppfmem); // add to link list
336 if (undo) anz += anz;
340 seekpos = seekpos + 5 + anz;
341 count = count - 5 - anz;
342 } while (count != 0); // loop til end
346 FillPPFCache(); // build address array
348 SysPrintf(_("Loaded PPF %d.0 patch: %s.\n"), method + 1, szPPF);
352 SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
357 // redump.org SBI files, slightly different handling from PCSX-Reloaded
358 unsigned char *sbi_sectors;
360 int LoadSBI(const char *fname, int sector_count) {
361 int good_sectors = 0;
368 sbihandle = fopen(fname, "rb");
369 if (sbihandle == NULL)
372 sbi_sectors = calloc(1, sector_count / 8);
373 if (sbi_sectors == NULL)
377 if (fread(buffer, 1, 4, sbihandle) != 4)
381 s = fread(sbitime, 1, 3, sbihandle);
388 s = MSF2SECT(btoi(sbitime[0]), btoi(sbitime[1]), btoi(sbitime[2]));
389 if (s < sector_count) {
390 sbi_sectors[s >> 3] |= 1 << (s&7);
394 SysPrintf(_("SBI sector %d >= %d?\n"), s, sector_count);
396 // skip to the next record
397 if (fread(&t, 1, sizeof(t), sbihandle) != sizeof(t))
412 if (fseek(sbihandle, s, SEEK_CUR))
421 SysPrintf(_("SBI: parse failure at 0x%lx\n"), ftell(sbihandle));
427 return sbi_sectors ? 0 : -1;
430 void UnloadSBI(void) {