frontend: generic: preliminary SDL support
[pcsx_rearmed.git] / libpcsxcore / ppf.c
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
26 typedef struct tagPPF_DATA {
27         s32                                     addr;
28         s32                                     pos;
29         s32                                     anz;
30         struct tagPPF_DATA      *pNext;
31 } PPF_DATA;
32
33 typedef struct tagPPF_CACHE {
34         s32                                     addr;
35         struct tagPPF_DATA      *pNext;
36 } PPF_CACHE;
37
38 static PPF_CACHE                *ppfCache = NULL;
39 static PPF_DATA                 *ppfHead = NULL, *ppfLast = NULL;
40 static int                              iPPFNum = 0;
41
42 // using a linked data list, and address array
43 static 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
77 void 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
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;
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
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;
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
180 void 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         if (CdromId[0] == '\0') return;
194
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]);
200         buffer[4] = '_';
201         buffer[5] = CdromId[4];
202         buffer[6] = CdromId[5];
203         buffer[7] = CdromId[6];
204         buffer[8] = '.';
205         buffer[9] = CdromId[7];
206         buffer[10] = CdromId[8];
207         buffer[11] = '\0';
208
209         sprintf(szPPF, "%s%s", Config.PatchesDir, buffer);
210
211         ppffile = fopen(szPPF, "rb");
212         if (ppffile == NULL) return;
213
214         memset(buffer, 0, 5);
215         fread(buffer, 3, 1, ppffile);
216
217         if (strcmp(buffer, "PPF") != 0) {
218                 SysPrintf(_("Invalid PPF patch: %s.\n"), szPPF);
219                 fclose(ppffile);
220                 return;
221         }
222
223         fseek(ppffile, 5, SEEK_SET);
224         method = fgetc(ppffile);
225
226         switch (method) {
227                 case 0: // ppf1
228                         fseek(ppffile, 0, SEEK_END);
229                         count = ftell(ppffile);
230                         count -= 56;
231                         seekpos = 56;
232                         break;
233
234                 case 1: // ppf2
235                         fseek(ppffile, -8, SEEK_END);
236
237                         memset(buffer, 0, 5);
238                         fread(buffer, 4, 1, ppffile);
239
240                         if (strcmp(".DIZ", buffer) != 0) {
241                                 dizyn = 0;
242                         } else {
243                                 fread(&dizlen, 4, 1, ppffile);
244                                 dizlen = SWAP32(dizlen);
245                                 dizyn = 1;
246                         }
247
248                         fseek(ppffile, 0, SEEK_END);
249                         count = ftell(ppffile);
250
251                         if (dizyn == 0) {
252                                 count -= 1084;
253                                 seekpos = 1084;
254                         } else {
255                                 count -= 1084;
256                                 count -= 38;
257                                 count -= dizlen;
258                                 seekpos = 1084;
259                         }
260                         break;
261
262                 case 2: // ppf3
263                         fseek(ppffile, 57, SEEK_SET);
264                         blockcheck = fgetc(ppffile);
265                         undo = fgetc(ppffile);
266
267                         fseek(ppffile, -6, SEEK_END);
268                         memset(buffer, 0, 5);
269                         fread(buffer, 4, 1, ppffile);
270                         dizlen = 0;
271
272                         if (strcmp(".DIZ", buffer) == 0) {
273                                 fseek(ppffile, -2, SEEK_END);
274                                 fread(&dizlen, 2, 1, ppffile);
275                                 dizlen = SWAP32(dizlen);
276                                 dizlen += 36;
277                         }
278
279                         fseek(ppffile, 0, SEEK_END);
280                         count = ftell(ppffile);
281                         count -= dizlen;
282
283                         if (blockcheck) {
284                                 seekpos = 1084;
285                                 count -= 1084;
286                         } else {
287                                 seekpos = 60;
288                                 count -= 60;
289                         }
290                         break;
291
292                 default:
293                         fclose(ppffile);
294                         SysPrintf(_("Unsupported PPF version (%d).\n"), method + 1);
295                         return;
296         }
297
298         // now do the data reading
299         do {                                                
300                 fseek(ppffile, seekpos, SEEK_SET);
301                 fread(&pos, 4, 1, ppffile);
302                 pos = SWAP32(pos);
303
304                 if (method == 2) fread(buffer, 4, 1, ppffile); // skip 4 bytes on ppf3 (no int64 support here)
305
306                 anz = fgetc(ppffile);
307                 fread(ppfmem, anz, 1, ppffile);   
308
309                 ladr = pos / CD_FRAMESIZE_RAW;
310                 off = pos % CD_FRAMESIZE_RAW;
311
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]);
316                 }
317
318                 AddToPPF(ladr, off, anz, ppfmem); // add to link list
319
320                 if (method == 2) {
321                         if (undo) anz += anz;
322                         anz += 4;
323                 }
324
325                 seekpos = seekpos + 5 + anz;
326                 count = count - 5 - anz;
327         } while (count != 0); // loop til end
328
329         fclose(ppffile);
330
331         FillPPFCache(); // build address array
332
333         SysPrintf(_("Loaded PPF %d.0 patch: %s.\n"), method + 1, szPPF);
334 }
335
336 // redump.org SBI files, slightly different handling from PCSX-Reloaded
337 unsigned char *sbi_sectors;
338
339 int LoadSBI(const char *fname, int sector_count) {
340         char buffer[16];
341         FILE *sbihandle;
342         u8 sbitime[3];
343         int s;
344
345         sbihandle = fopen(fname, "rb");
346         if (sbihandle == NULL)
347                 return -1;
348
349         sbi_sectors = calloc(1, sector_count / 8);
350         if (sbi_sectors == NULL)
351                 return -1;
352
353         // 4-byte SBI header
354         fread(buffer, 1, 4, sbihandle);
355         while (!feof(sbihandle)) {
356                 fread(sbitime, 1, 3, sbihandle);
357                 fread(buffer, 1, 11, sbihandle);
358
359                 s = MSF2SECT(btoi(sbitime[0]), btoi(sbitime[1]), btoi(sbitime[2]));
360                 if (s < sector_count)
361                         sbi_sectors[s >> 3] |= 1 << (s&7);
362                 else
363                         SysPrintf(_("SBI sector %d >= %d?\n"), s, sector_count);
364         }
365
366         fclose(sbihandle);
367
368         return 0;
369 }
370
371 void UnloadSBI(void) {
372         if (sbi_sectors) {
373                 free(sbi_sectors);
374                 sbi_sectors = NULL;
375         }
376 }