cdrom: change pause timing again
[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 "misc.h"
25 #include "cdrom.h"
26
27 typedef struct tagPPF_DATA {
28         s32                                     addr;
29         s32                                     pos;
30         s32                                     anz;
31         struct tagPPF_DATA      *pNext;
32 } PPF_DATA;
33
34 typedef struct tagPPF_CACHE {
35         s32                                     addr;
36         struct tagPPF_DATA      *pNext;
37 } PPF_CACHE;
38
39 static PPF_CACHE                *ppfCache = NULL;
40 static PPF_DATA                 *ppfHead = NULL, *ppfLast = NULL;
41 static int                              iPPFNum = 0;
42
43 // using a linked data list, and address array
44 static void FillPPFCache() {
45         PPF_DATA                *p;
46         PPF_CACHE               *pc;
47         s32                             lastaddr;
48
49         p = ppfHead;
50         lastaddr = -1;
51         iPPFNum = 0;
52
53         while (p != NULL) {
54                 if (p->addr != lastaddr) iPPFNum++;
55                 lastaddr = p->addr;
56                 p = p->pNext;
57         }
58
59         if (iPPFNum <= 0) return;
60
61         pc = ppfCache = (PPF_CACHE *)malloc(iPPFNum * sizeof(PPF_CACHE));
62         if (pc == NULL) return;
63
64         iPPFNum--;
65         p = ppfHead;
66         lastaddr = -1;
67
68         while (p != NULL) {
69                 if (p->addr != lastaddr) {
70                         pc->addr = p->addr;
71                         pc->pNext = p;
72                         pc++;
73                 }
74                 lastaddr = p->addr;
75                 p = p->pNext;
76         }
77 }
78
79 void FreePPFCache() {
80         PPF_DATA *p = ppfHead;
81         void *pn;
82
83         while (p != NULL) {
84                 pn = p->pNext;
85                 free(p);
86                 p = (PPF_DATA *)pn;
87         }
88         ppfHead = NULL;
89         ppfLast = NULL;
90
91         if (ppfCache != NULL) free(ppfCache);
92         ppfCache = NULL;
93 }
94
95 void CheckPPFCache(unsigned char *pB, unsigned char m, unsigned char s, unsigned char f) {
96         PPF_CACHE *pcstart, *pcend, *pcpos;
97         int addr = MSF2SECT(btoi(m), btoi(s), btoi(f)), pos, anz, start;
98
99         if (ppfCache == NULL) return;
100
101         pcstart = ppfCache;
102         if (addr < pcstart->addr) return;
103         pcend = ppfCache + iPPFNum;
104         if (addr > pcend->addr) return;
105
106         while (1) {
107                 if (addr == pcend->addr) { pcpos = pcend; break; }
108
109                 pcpos = pcstart + (pcend - pcstart) / 2;
110                 if (pcpos == pcstart) break;
111                 if (addr < pcpos->addr) {
112                         pcend = pcpos;
113                         continue;
114                 }
115                 if (addr > pcpos->addr) {
116                         pcstart = pcpos;
117                         continue;
118                 }
119                 break;
120         }
121
122         if (addr == pcpos->addr) {
123                 PPF_DATA *p = pcpos->pNext;
124                 while (p != NULL && p->addr == addr) {
125                         pos = p->pos - (CD_FRAMESIZE_RAW - DATA_SIZE);
126                         anz = p->anz;
127                         if (pos < 0) { start = -pos; pos = 0; anz -= start; }
128                         else start = 0;
129                         memcpy(pB + pos, (unsigned char *)(p + 1) + start, anz);
130                         p = p->pNext;
131                 }
132         }
133 }
134
135 static void AddToPPF(s32 ladr, s32 pos, s32 anz, unsigned char *ppfmem) {
136         if (ppfHead == NULL) {
137                 ppfHead = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz);
138                 if (ppfHead == NULL) return;
139                 ppfHead->addr = ladr;
140                 ppfHead->pNext = NULL;
141                 ppfHead->pos = pos;
142                 ppfHead->anz = anz;
143                 memcpy(ppfHead + 1, ppfmem, anz);
144                 iPPFNum = 1;
145                 ppfLast = ppfHead;
146         } else {
147                 PPF_DATA *p = ppfHead;
148                 PPF_DATA *plast = NULL;
149                 PPF_DATA *padd;
150
151                 if (ladr > ppfLast->addr || (ladr == ppfLast->addr && pos > ppfLast->pos)) {
152                         p = NULL;
153                         plast = ppfLast;
154                 } else {
155                         while (p != NULL) {
156                                 if (ladr < p->addr) break;
157                                 if (ladr == p->addr) {
158                                         while (p && ladr == p->addr && pos > p->pos) {
159                                                 plast = p;
160                                                 p = p->pNext;
161                                         }
162                                         break;
163                                 }
164                                 plast = p;
165                                 p = p->pNext;
166                         }
167                 }
168
169                 padd = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz);
170                 if (padd == NULL) return;
171                 padd->addr = ladr;
172                 padd->pNext = p;
173                 padd->pos = pos;
174                 padd->anz = anz;
175                 memcpy(padd + 1, ppfmem, anz);
176                 iPPFNum++;
177                 if (plast == NULL) ppfHead = padd;
178                 else plast->pNext = padd;
179
180                 if (padd->pNext == NULL) ppfLast = padd;
181         }
182 }
183
184 void BuildPPFCache() {
185         FILE                    *ppffile;
186         char                    buffer[12];
187         char                    method, undo = 0, blockcheck = 0;
188         int                             dizlen, dizyn;
189         unsigned char   ppfmem[512];
190         char                    szPPF[MAXPATHLEN * 2];
191         int                             count, seekpos, pos;
192         u32                             anz; // use 32-bit to avoid stupid overflows
193         s32                             ladr, off, anx;
194
195         FreePPFCache();
196
197         if (CdromId[0] == '\0') return;
198
199         // Generate filename in the format of SLUS_123.45
200         buffer[0] = toupper(CdromId[0]);
201         buffer[1] = toupper(CdromId[1]);
202         buffer[2] = toupper(CdromId[2]);
203         buffer[3] = toupper(CdromId[3]);
204         buffer[4] = '_';
205         buffer[5] = CdromId[4];
206         buffer[6] = CdromId[5];
207         buffer[7] = CdromId[6];
208         buffer[8] = '.';
209         buffer[9] = CdromId[7];
210         buffer[10] = CdromId[8];
211         buffer[11] = '\0';
212
213         sprintf(szPPF, "%s%s", Config.PatchesDir, buffer);
214
215         ppffile = fopen(szPPF, "rb");
216         if (ppffile == NULL) return;
217
218         memset(buffer, 0, 5);
219         if (fread(buffer, 1, 3, ppffile) != 3)
220                 goto fail_io;
221
222         if (strcmp(buffer, "PPF") != 0) {
223                 SysPrintf(_("Invalid PPF patch: %s.\n"), szPPF);
224                 fclose(ppffile);
225                 return;
226         }
227
228         fseek(ppffile, 5, SEEK_SET);
229         method = fgetc(ppffile);
230
231         switch (method) {
232                 case 0: // ppf1
233                         fseek(ppffile, 0, SEEK_END);
234                         count = ftell(ppffile);
235                         count -= 56;
236                         seekpos = 56;
237                         break;
238
239                 case 1: // ppf2
240                         fseek(ppffile, -8, SEEK_END);
241
242                         memset(buffer, 0, 5);
243                         if (fread(buffer, 1, 4, ppffile) != 4)
244                                 goto fail_io;
245
246                         if (strcmp(".DIZ", buffer) != 0) {
247                                 dizyn = 0;
248                         } else {
249                                 if (fread(&dizlen, 1, 4, ppffile) != 4)
250                                         goto fail_io;
251                                 dizlen = SWAP32(dizlen);
252                                 dizyn = 1;
253                         }
254
255                         fseek(ppffile, 0, SEEK_END);
256                         count = ftell(ppffile);
257
258                         if (dizyn == 0) {
259                                 count -= 1084;
260                                 seekpos = 1084;
261                         } else {
262                                 count -= 1084;
263                                 count -= 38;
264                                 count -= dizlen;
265                                 seekpos = 1084;
266                         }
267                         break;
268
269                 case 2: // ppf3
270                         fseek(ppffile, 57, SEEK_SET);
271                         blockcheck = fgetc(ppffile);
272                         undo = fgetc(ppffile);
273
274                         fseek(ppffile, -6, SEEK_END);
275                         memset(buffer, 0, 5);
276                         if (fread(buffer, 1, 4, ppffile) != 4)
277                                 goto fail_io;
278                         dizlen = 0;
279
280                         if (strcmp(".DIZ", buffer) == 0) {
281                                 fseek(ppffile, -2, SEEK_END);
282                                 // TODO: Endian/size unsafe?
283                                 if (fread(&dizlen, 1, 2, ppffile) != 2)
284                                         goto fail_io;
285                                 dizlen = SWAP32(dizlen);
286                                 dizlen += 36;
287                         }
288
289                         fseek(ppffile, 0, SEEK_END);
290                         count = ftell(ppffile);
291                         count -= dizlen;
292
293                         if (blockcheck) {
294                                 seekpos = 1084;
295                                 count -= 1084;
296                         } else {
297                                 seekpos = 60;
298                                 count -= 60;
299                         }
300                         break;
301
302                 default:
303                         fclose(ppffile);
304                         SysPrintf(_("Unsupported PPF version (%d).\n"), method + 1);
305                         return;
306         }
307
308         // now do the data reading
309         do {                                                
310                 fseek(ppffile, seekpos, SEEK_SET);
311                 if (fread(&pos, 1, sizeof(pos), ppffile) != sizeof(pos))
312                         goto fail_io;
313                 pos = SWAP32(pos);
314
315                 if (method == 2) {
316                         // skip 4 bytes on ppf3 (no int64 support here)
317                         if (fread(buffer, 1, 4, ppffile) != 4)
318                                 goto fail_io;
319                 }
320
321                 anz = fgetc(ppffile);
322                 if (fread(ppfmem, 1, anz, ppffile) != anz)
323                         goto fail_io;
324
325                 ladr = pos / CD_FRAMESIZE_RAW;
326                 off = pos % CD_FRAMESIZE_RAW;
327
328                 if (off + anz > CD_FRAMESIZE_RAW) {
329                         anx = off + anz - CD_FRAMESIZE_RAW;
330                         anz -= (unsigned char)anx;
331                         AddToPPF(ladr + 1, 0, anx, &ppfmem[anz]);
332                 }
333
334                 AddToPPF(ladr, off, anz, ppfmem); // add to link list
335
336                 if (method == 2) {
337                         if (undo) anz += anz;
338                         anz += 4;
339                 }
340
341                 seekpos = seekpos + 5 + anz;
342                 count = count - 5 - anz;
343         } while (count != 0); // loop til end
344
345         fclose(ppffile);
346
347         FillPPFCache(); // build address array
348
349         SysPrintf(_("Loaded PPF %d.0 patch: %s.\n"), method + 1, szPPF);
350
351 fail_io:
352 #ifndef NDEBUG
353         SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
354 #endif
355         fclose(ppffile);
356 }
357
358 // redump.org SBI files, slightly different handling from PCSX-Reloaded
359 unsigned char *sbi_sectors;
360 int sbi_len;
361
362 int LoadSBI(const char *fname, int sector_count) {
363         int good_sectors = 0;
364         int clean_eof = 0;
365         char buffer[16];
366         FILE *sbihandle;
367         u8 sbitime[3], t;
368         int s;
369
370         sbihandle = fopen(fname, "rb");
371         if (sbihandle == NULL)
372                 return -1;
373
374         sbi_len = (sector_count + 7) / 8;
375         sbi_sectors = calloc(1, sbi_len);
376         if (sbi_sectors == NULL)
377                 goto end;
378
379         // 4-byte SBI header
380         if (fread(buffer, 1, 4, sbihandle) != 4)
381                 goto end;
382
383         while (1) {
384                 s = fread(sbitime, 1, 3, sbihandle);
385                 if (s != 3)
386                 {
387                         if (s == 0)
388                                 clean_eof = 1;
389                         break;
390                 }
391                 s = MSF2SECT(btoi(sbitime[0]), btoi(sbitime[1]), btoi(sbitime[2]));
392                 if (s < sector_count) {
393                         sbi_sectors[s >> 3] |= 1 << (s&7);
394                         good_sectors++;
395                 }
396                 else
397                         SysPrintf(_("SBI sector %d >= %d?\n"), s, sector_count);
398
399                 // skip to the next record
400                 if (fread(&t, 1, sizeof(t), sbihandle) != sizeof(t))
401                         break;
402                 s = -1;
403                 switch (t) {
404                 default:
405                 case 1:
406                         s = 10;
407                         break;
408                 case 2:
409                 case 3:
410                         s = 3;
411                         break;
412                 }
413                 if (s < 0)
414                         break;
415                 if (fseek(sbihandle, s, SEEK_CUR))
416                         break;
417         }
418
419 end:
420         if (!clean_eof)
421                 SysPrintf(_("SBI: parse failure at 0x%lx\n"), ftell(sbihandle));
422         if (!good_sectors) {
423                 free(sbi_sectors);
424                 sbi_sectors = NULL;
425                 sbi_len = 0;
426         }
427         fclose(sbihandle);
428         return sbi_sectors ? 0 : -1;
429 }
430
431 void UnloadSBI(void) {
432         if (sbi_sectors) {
433                 free(sbi_sectors);
434                 sbi_sectors = NULL;
435                 sbi_len = 0;
436         }
437 }