Author Topic: FBA port of Nicola's CPS2 decryption code  (Read 20137 times)

Offline Leaf

  • Jr. Member
  • **
  • Posts: 50
  • Karma: +9/-4
Is there a memory leak in CPS2 decryption?
« Reply #30 on: March 06, 2007, 09:20:29 AM »
I found it when iq_132 post the file ported from MAME, but I'm not sure.
(http://neo-source.com/index.php?topic=590.0)

I noticed that it exists in FBA Alpha 0.2.96.65 too.
Quote
static void cps2_decrypt(const UINT32 *master_key, unsigned int upper_limit)
{
        ......

#if 1
   UINT16 *rom = (UINT16 *)CpsRom;
   unsigned int length = upper_limit;
   CpsCode = (UINT8*)malloc(length);  <== allocated but not free
   UINT16 *dec = (UINT16*)CpsCode;
   unsigned int i;
#endif
       ......

Here I made a simple fix. :biggrin:
For CPS2 is fully decrypted now, we don't need the loading xor code.
In cps.cpp, find these in CpsGetROMs()
Code: [Select]
    unsigned char* CpsCodeLoad = CpsCode;
    .....
    if (!CpsCodeLoad || !CpsRomLoad || !CpsGfxLoad || !CpsZRomLoad || !CpsQSamLoad) {
return 1;
    }
    ....
    // XOR tables
    if ((ri.nType & 7) == 2) {
if (bLoad) {
BurnLoadRom(CpsCodeLoad, i, 1);
CpsCodeLoad += ri.nLen;
} else {
nCpsCodeLen += ri.nLen;
}
continue;
   }
comment them or delete CpsCodeLoad.

in CpsInit(), add this at first:
Code: [Select]
    unsigned char* CpsCodeIn = NULL;

then find:
Code: [Select]
CpsCode = CpsRom + nCpsRomLen;
if (Cps1Qs == 1) {
CpsEncZRom = CpsCode + nCpsCodeLen;
CpsZRom = CpsEncZRom + nCpsZRomLen * 2;
} else {
CpsZRom = CpsCode + nCpsCodeLen;
}
replace it with:
Code: [Select]
CpsCodeIn = CpsRom + nCpsRomLen;
if (Cps1Qs == 1) {
CpsEncZRom = CpsCodeIn + nCpsCodeLen;
CpsZRom = CpsEncZRom + nCpsZRomLen * 2;
} else {
CpsZRom = CpsCodeIn + nCpsCodeLen;
}

in CpsExit(), add
Code: [Select]
free(CpsCode);
before
Code: [Select]
nCpsCodeLen = nCpsRomLen = nCpsGfxLen = nCpsZRomLen = nCpsQSamLen = nCpsAdLen = 0;

If you want to keep the loading xor code, maybe you should define a new pointer used to point the new allocated memory
in cps2_decrypt() and don't forget to free it in CpsExit().
 :p

Offline CaptainCPS

  • FBNeo Dev
  • ******
  • Posts: 1513
  • Karma: +127/-0
  • FB Alpha Team
    • CaptainCPS's Home
Re: Is there a memory leak in CPS2 decryption?
« Reply #31 on: March 06, 2007, 01:19:44 PM »
ThanX Leaf! added the fix to the actual FBA Extras so it should be included in R10 version  :wink:

SeeYaa!
 :biggrin:

Offline iq_132

  • Administrator
  • *****
  • Posts: 3724
  • Karma: +411/-0
  • Definitely not Dink!
    • NeoSource
Re: FBA port of Nicola's CPS2 decryption code
« Reply #32 on: March 06, 2007, 05:21:47 PM »
Hadn't noticed this, nice find Leaf. ^^