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));
67 if (p->addr != lastaddr) {
78 PPF_DATA *p = ppfHead;
89 if (ppfCache != NULL) free(ppfCache);
93 void CheckPPFCache(unsigned char *pB, unsigned char m, unsigned char s, unsigned char f) {
94 PPF_CACHE *pcstart, *pcend, *pcpos;
95 int addr = MSF2SECT(btoi(m), btoi(s), btoi(f)), pos, anz, start;
97 if (ppfCache == NULL) return;
100 if (addr < pcstart->addr) return;
101 pcend = ppfCache + iPPFNum;
102 if (addr > pcend->addr) return;
105 if (addr == pcend->addr) { pcpos = pcend; break; }
107 pcpos = pcstart + (pcend - pcstart) / 2;
108 if (pcpos == pcstart) break;
109 if (addr < pcpos->addr) {
113 if (addr > pcpos->addr) {
120 if (addr == pcpos->addr) {
121 PPF_DATA *p = pcpos->pNext;
122 while (p != NULL && p->addr == addr) {
123 pos = p->pos - (CD_FRAMESIZE_RAW - DATA_SIZE);
125 if (pos < 0) { start = -pos; pos = 0; anz -= start; }
127 memcpy(pB + pos, (unsigned char *)(p + 1) + start, anz);
133 static void AddToPPF(s32 ladr, s32 pos, s32 anz, unsigned char *ppfmem) {
134 if (ppfHead == NULL) {
135 ppfHead = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz);
136 ppfHead->addr = ladr;
137 ppfHead->pNext = NULL;
140 memcpy(ppfHead + 1, ppfmem, anz);
144 PPF_DATA *p = ppfHead;
145 PPF_DATA *plast = NULL;
148 if (ladr > ppfLast->addr || (ladr == ppfLast->addr && pos > ppfLast->pos)) {
153 if (ladr < p->addr) break;
154 if (ladr == p->addr) {
155 while (p && ladr == p->addr && pos > p->pos) {
166 padd = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz);
171 memcpy(padd + 1, ppfmem, anz);
173 if (plast == NULL) ppfHead = padd;
174 else plast->pNext = padd;
176 if (padd->pNext == NULL) ppfLast = padd;
180 void BuildPPFCache() {
183 char method, undo = 0, blockcheck = 0;
185 unsigned char ppfmem[512];
186 char szPPF[MAXPATHLEN * 2];
187 int count, seekpos, pos;
188 u32 anz; // use 32-bit to avoid stupid overflows
193 if (CdromId[0] == '\0') return;
195 // Generate filename in the format of SLUS_123.45
196 buffer[0] = toupper(CdromId[0]);
197 buffer[1] = toupper(CdromId[1]);
198 buffer[2] = toupper(CdromId[2]);
199 buffer[3] = toupper(CdromId[3]);
201 buffer[5] = CdromId[4];
202 buffer[6] = CdromId[5];
203 buffer[7] = CdromId[6];
205 buffer[9] = CdromId[7];
206 buffer[10] = CdromId[8];
209 sprintf(szPPF, "%s%s", Config.PatchesDir, buffer);
211 ppffile = fopen(szPPF, "rb");
212 if (ppffile == NULL) return;
214 memset(buffer, 0, 5);
215 fread(buffer, 3, 1, ppffile);
217 if (strcmp(buffer, "PPF") != 0) {
218 SysPrintf(_("Invalid PPF patch: %s.\n"), szPPF);
223 fseek(ppffile, 5, SEEK_SET);
224 method = fgetc(ppffile);
228 fseek(ppffile, 0, SEEK_END);
229 count = ftell(ppffile);
235 fseek(ppffile, -8, SEEK_END);
237 memset(buffer, 0, 5);
238 fread(buffer, 4, 1, ppffile);
240 if (strcmp(".DIZ", buffer) != 0) {
243 fread(&dizlen, 4, 1, ppffile);
244 dizlen = SWAP32(dizlen);
248 fseek(ppffile, 0, SEEK_END);
249 count = ftell(ppffile);
263 fseek(ppffile, 57, SEEK_SET);
264 blockcheck = fgetc(ppffile);
265 undo = fgetc(ppffile);
267 fseek(ppffile, -6, SEEK_END);
268 memset(buffer, 0, 5);
269 fread(buffer, 4, 1, ppffile);
272 if (strcmp(".DIZ", buffer) == 0) {
273 fseek(ppffile, -2, SEEK_END);
274 fread(&dizlen, 2, 1, ppffile);
275 dizlen = SWAP32(dizlen);
279 fseek(ppffile, 0, SEEK_END);
280 count = ftell(ppffile);
294 SysPrintf(_("Unsupported PPF version (%d).\n"), method + 1);
298 // now do the data reading
300 fseek(ppffile, seekpos, SEEK_SET);
301 fread(&pos, 4, 1, ppffile);
304 if (method == 2) fread(buffer, 4, 1, ppffile); // skip 4 bytes on ppf3 (no int64 support here)
306 anz = fgetc(ppffile);
307 fread(ppfmem, anz, 1, ppffile);
309 ladr = pos / CD_FRAMESIZE_RAW;
310 off = pos % CD_FRAMESIZE_RAW;
312 if (off + anz > CD_FRAMESIZE_RAW) {
313 anx = off + anz - CD_FRAMESIZE_RAW;
314 anz -= (unsigned char)anx;
315 AddToPPF(ladr + 1, 0, anx, &ppfmem[anz]);
318 AddToPPF(ladr, off, anz, ppfmem); // add to link list
321 if (undo) anz += anz;
325 seekpos = seekpos + 5 + anz;
326 count = count - 5 - anz;
327 } while (count != 0); // loop til end
331 FillPPFCache(); // build address array
333 SysPrintf(_("Loaded PPF %d.0 patch: %s.\n"), method + 1, szPPF);
336 // redump.org SBI files, slightly different handling from PCSX-Reloaded
337 unsigned char *sbi_sectors;
339 int LoadSBI(const char *fname, int sector_count) {
345 sbihandle = fopen(fname, "rb");
346 if (sbihandle == NULL)
349 sbi_sectors = calloc(1, sector_count / 8);
350 if (sbi_sectors == NULL) {
356 fread(buffer, 1, 4, sbihandle);
358 s = fread(sbitime, 1, 3, sbihandle);
361 fread(&t, 1, 1, sbihandle);
372 fseek(sbihandle, s, SEEK_CUR);
374 s = MSF2SECT(btoi(sbitime[0]), btoi(sbitime[1]), btoi(sbitime[2]));
375 if (s < sector_count)
376 sbi_sectors[s >> 3] |= 1 << (s&7);
378 SysPrintf(_("SBI sector %d >= %d?\n"), s, sector_count);
386 void UnloadSBI(void) {