cdrom: merge a fix from PCSX-Reloaded
[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         // 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 }
333
334 // redump.org SBI files, slightly different handling from PCSX-Reloaded
335 unsigned char *sbi_sectors;
336
337 int LoadSBI(const char *fname, int sector_count) {
338         char buffer[16], sbifile[MAXPATHLEN];
339         FILE *sbihandle;
340         u8 sbitime[3];
341         int s;
342
343         sbihandle = fopen(fname, "rb");
344         if (sbihandle == NULL)
345                 return -1;
346
347 if (sbi_sectors != NULL) printf("sbi_sectors?\n");
348         sbi_sectors = calloc(1, sector_count / 8);
349         if (sbi_sectors == NULL)
350                 return -1;
351
352         // 4-byte SBI header
353         fread(buffer, 1, 4, sbihandle);
354         while (!feof(sbihandle)) {
355                 fread(sbitime, 1, 3, sbihandle);
356                 fread(buffer, 1, 11, sbihandle);
357
358                 s = MSF2SECT(btoi(sbitime[0]), btoi(sbitime[1]), btoi(sbitime[2]));
359                 if (s < sector_count)
360                         sbi_sectors[s >> 3] |= 1 << (s&7);
361                 else
362                         SysPrintf(_("SBI sector %d >= %d?\n"), s, sector_count);
363         }
364
365         fclose(sbihandle);
366
367         return 0;
368 }
369
370 void UnloadSBI(void) {
371         if (sbi_sectors) {
372                 free(sbi_sectors);
373                 sbi_sectors = NULL;
374         }
375 }