pcsxr-1.9.92
[pcsx_rearmed.git] / libpcsxcore / ppf.c
CommitLineData
ef79bbde
P
1/* PPF Patch Support for PCSX-Reloaded
2 * Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
3 *
4 * Based on P.E.Op.S CDR Plugin by Pete Bernert.
5 * Copyright (c) 2002, Pete Bernert.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "psxcommon.h"
23#include "ppf.h"
24#include "cdrom.h"
25
26typedef struct tagPPF_DATA {
27 s32 addr;
28 s32 pos;
29 s32 anz;
30 struct tagPPF_DATA *pNext;
31} PPF_DATA;
32
33typedef struct tagPPF_CACHE {
34 s32 addr;
35 struct tagPPF_DATA *pNext;
36} PPF_CACHE;
37
38static PPF_CACHE *ppfCache = NULL;
39static PPF_DATA *ppfHead = NULL, *ppfLast = NULL;
40static int iPPFNum = 0;
41
42// using a linked data list, and address array
43static void FillPPFCache() {
44 PPF_DATA *p;
45 PPF_CACHE *pc;
46 s32 lastaddr;
47
48 p = ppfHead;
49 lastaddr = -1;
50 iPPFNum = 0;
51
52 while (p != NULL) {
53 if (p->addr != lastaddr) iPPFNum++;
54 lastaddr = p->addr;
55 p = p->pNext;
56 }
57
58 if (iPPFNum <= 0) return;
59
60 pc = ppfCache = (PPF_CACHE *)malloc(iPPFNum * sizeof(PPF_CACHE));
61
62 iPPFNum--;
63 p = ppfHead;
64 lastaddr = -1;
65
66 while (p != NULL) {
67 if (p->addr != lastaddr) {
68 pc->addr = p->addr;
69 pc->pNext = p;
70 pc++;
71 }
72 lastaddr = p->addr;
73 p = p->pNext;
74 }
75}
76
77void FreePPFCache() {
78 PPF_DATA *p = ppfHead;
79 void *pn;
80
81 while (p != NULL) {
82 pn = p->pNext;
83 free(p);
84 p = (PPF_DATA *)pn;
85 }
86 ppfHead = NULL;
87 ppfLast = NULL;
88
89 if (ppfCache != NULL) free(ppfCache);
90 ppfCache = NULL;
91}
92
93void 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;
96
97 if (ppfCache == NULL) return;
98
99 pcstart = ppfCache;
100 if (addr < pcstart->addr) return;
101 pcend = ppfCache + iPPFNum;
102 if (addr > pcend->addr) return;
103
104 while (1) {
105 if (addr == pcend->addr) { pcpos = pcend; break; }
106
107 pcpos = pcstart + (pcend - pcstart) / 2;
108 if (pcpos == pcstart) break;
109 if (addr < pcpos->addr) {
110 pcend = pcpos;
111 continue;
112 }
113 if (addr > pcpos->addr) {
114 pcstart = pcpos;
115 continue;
116 }
117 break;
118 }
119
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);
124 anz = p->anz;
125 if (pos < 0) { start = -pos; pos = 0; anz -= start; }
126 else start = 0;
127 memcpy(pB + pos, (unsigned char *)(p + 1) + start, anz);
128 p = p->pNext;
129 }
130 }
131}
132
133static 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;
138 ppfHead->pos = pos;
139 ppfHead->anz = anz;
140 memcpy(ppfHead + 1, ppfmem, anz);
141 iPPFNum = 1;
142 ppfLast = ppfHead;
143 } else {
144 PPF_DATA *p = ppfHead;
145 PPF_DATA *plast = NULL;
146 PPF_DATA *padd;
147
148 if (ladr > ppfLast->addr || (ladr == ppfLast->addr && pos > ppfLast->pos)) {
149 p = NULL;
150 plast = ppfLast;
151 } else {
152 while (p != NULL) {
153 if (ladr < p->addr) break;
154 if (ladr == p->addr) {
155 while (p && ladr == p->addr && pos > p->pos) {
156 plast = p;
157 p = p->pNext;
158 }
159 break;
160 }
161 plast = p;
162 p = p->pNext;
163 }
164 }
165
166 padd = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz);
167 padd->addr = ladr;
168 padd->pNext = p;
169 padd->pos = pos;
170 padd->anz = anz;
171 memcpy(padd + 1, ppfmem, anz);
172 iPPFNum++;
173 if (plast == NULL) ppfHead = padd;
174 else plast->pNext = padd;
175
176 if (padd->pNext == NULL) ppfLast = padd;
177 }
178}
179
180void BuildPPFCache() {
181 FILE *ppffile;
182 char buffer[12];
183 char method, undo = 0, blockcheck = 0;
184 int dizlen, dizyn;
185 unsigned char ppfmem[512];
186 char szPPF[MAXPATHLEN];
187 int count, seekpos, pos;
188 u32 anz; // use 32-bit to avoid stupid overflows
189 s32 ladr, off, anx;
190
191 FreePPFCache();
192
193 // Generate filename in the format of SLUS_123.45
194 buffer[0] = toupper(CdromId[0]);
195 buffer[1] = toupper(CdromId[1]);
196 buffer[2] = toupper(CdromId[2]);
197 buffer[3] = toupper(CdromId[3]);
198 buffer[4] = '_';
199 buffer[5] = CdromId[4];
200 buffer[6] = CdromId[5];
201 buffer[7] = CdromId[6];
202 buffer[8] = '.';
203 buffer[9] = CdromId[7];
204 buffer[10] = CdromId[8];
205 buffer[11] = '\0';
206
207 sprintf(szPPF, "%s%s", Config.PatchesDir, buffer);
208
209 ppffile = fopen(szPPF, "rb");
210 if (ppffile == NULL) return;
211
212 memset(buffer, 0, 5);
213 fread(buffer, 3, 1, ppffile);
214
215 if (strcmp(buffer, "PPF") != 0) {
216 SysPrintf(_("Invalid PPF patch: %s.\n"), szPPF);
217 fclose(ppffile);
218 return;
219 }
220
221 fseek(ppffile, 5, SEEK_SET);
222 method = fgetc(ppffile);
223
224 switch (method) {
225 case 0: // ppf1
226 fseek(ppffile, 0, SEEK_END);
227 count = ftell(ppffile);
228 count -= 56;
229 seekpos = 56;
230 break;
231
232 case 1: // ppf2
233 fseek(ppffile, -8, SEEK_END);
234
235 memset(buffer, 0, 5);
236 fread(buffer, 4, 1, ppffile);
237
238 if (strcmp(".DIZ", buffer) != 0) {
239 dizyn = 0;
240 } else {
241 fread(&dizlen, 4, 1, ppffile);
242 dizlen = SWAP32(dizlen);
243 dizyn = 1;
244 }
245
246 fseek(ppffile, 0, SEEK_END);
247 count = ftell(ppffile);
248
249 if (dizyn == 0) {
250 count -= 1084;
251 seekpos = 1084;
252 } else {
253 count -= 1084;
254 count -= 38;
255 count -= dizlen;
256 seekpos = 1084;
257 }
258 break;
259
260 case 2: // ppf3
261 fseek(ppffile, 57, SEEK_SET);
262 blockcheck = fgetc(ppffile);
263 undo = fgetc(ppffile);
264
265 fseek(ppffile, -6, SEEK_END);
266 memset(buffer, 0, 5);
267 fread(buffer, 4, 1, ppffile);
268 dizlen = 0;
269
270 if (strcmp(".DIZ", buffer) == 0) {
271 fseek(ppffile, -2, SEEK_END);
272 fread(&dizlen, 2, 1, ppffile);
273 dizlen = SWAP32(dizlen);
274 dizlen += 36;
275 }
276
277 fseek(ppffile, 0, SEEK_END);
278 count = ftell(ppffile);
279 count -= dizlen;
280
281 if (blockcheck) {
282 seekpos = 1084;
283 count -= 1084;
284 } else {
285 seekpos = 60;
286 count -= 60;
287 }
288 break;
289
290 default:
291 fclose(ppffile);
292 SysPrintf(_("Unsupported PPF version (%d).\n"), method + 1);
293 return;
294 }
295
296 // now do the data reading
297 do {
298 fseek(ppffile, seekpos, SEEK_SET);
299 fread(&pos, 4, 1, ppffile);
300 pos = SWAP32(pos);
301
302 if (method == 2) fread(buffer, 4, 1, ppffile); // skip 4 bytes on ppf3 (no int64 support here)
303
304 anz = fgetc(ppffile);
305 fread(ppfmem, anz, 1, ppffile);
306
307 ladr = pos / CD_FRAMESIZE_RAW;
308 off = pos % CD_FRAMESIZE_RAW;
309
310 if (off + anz > CD_FRAMESIZE_RAW) {
311 anx = off + anz - CD_FRAMESIZE_RAW;
312 anz -= (unsigned char)anx;
313 AddToPPF(ladr + 1, 0, anx, &ppfmem[anz]);
314 }
315
316 AddToPPF(ladr, off, anz, ppfmem); // add to link list
317
318 if (method == 2) {
319 if (undo) anz += anz;
320 anz += 4;
321 }
322
323 seekpos = seekpos + 5 + anz;
324 count = count - 5 - anz;
325 } while (count != 0); // loop til end
326
327 fclose(ppffile);
328
329 FillPPFCache(); // build address array
330
331 SysPrintf(_("Loaded PPF %d.0 patch: %s.\n"), method + 1, szPPF);
332}