Skip to content

Commit

Permalink
make everything oneliner in pspdecrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
zecoxao committed Jun 2, 2022
1 parent 5b584c6 commit 2a2b926
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 72 deletions.
80 changes: 14 additions & 66 deletions PrxDecrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ extern "C"
//#include "Common/Common.h"
//#include "Common/Swap.h"
#include "PrxDecrypter.h"
#include "common.h"
#include "pspdecrypt_lib.h"

#define ROUNDUP16(x) (((x)+15)&~15)

Expand Down Expand Up @@ -1061,97 +1059,47 @@ static int pspDecryptType6(const u8 *inbuf, u8 *outbuf, u32 size)
int pspDecryptPRX(const u8 *inbuf, u8 *outbuf, u32 size, const u8 *seed, bool verbose)
{
kirk_init();
std::string logStr;
u32 tag = (u32)*(u32_le *)&inbuf[0xD0];
u8 * decompbuf = (u8*) malloc (size);
//INFO_LOG(LOADER, "Decrypting tag %08X", tag);
// this would be significantly better if we had a log of the tags
// and their appropriate prx types
// since we don't know the PRX type we attempt a decrypt using all
auto res = pspDecryptType0(inbuf, outbuf, size);
if (res >= 0) {
res = pspDecompress(outbuf, size, decompbuf, size, logStr);
if(res != -1){
memcpy(outbuf, decompbuf,size);
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
}
else {
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
}

res = pspDecryptType1(inbuf, outbuf, size);
if (res >= 0) {
res = pspDecompress(outbuf, size, decompbuf, size, logStr);
if(res != -1){
memcpy(outbuf, decompbuf,size);
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
}
else {
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
if (verbose) {
printf("Decryption successful for tag %08X with type 1\n", tag);
}
return res;
}

res = pspDecryptType2(inbuf, outbuf, size);
if (res >= 0) {
res = pspDecompress(outbuf, size, decompbuf, size, logStr);
if(res != -1){
memcpy(outbuf, decompbuf,size);
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
}
else {
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
if (verbose) {
printf("Decryption successful for tag %08X with type 2\n", tag);
}
return res;
}

res = pspDecryptType5(inbuf, outbuf, size, seed);
if (res >= 0) {
res = pspDecompress(outbuf, size, decompbuf, size, logStr);
if(res != -1){
memcpy(outbuf, decompbuf,size);
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
}
else {
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
return res;
if (verbose) {
printf("Decryption successful for tag %08X with type 5\n", tag);
}
return res;
}

res = pspDecryptType6(inbuf, outbuf, size);
if (res >= 0) {
res = pspDecompress(outbuf, size, decompbuf, size, logStr);
if(res != -1){
memcpy(outbuf, decompbuf,size);
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
} else {
if (verbose) {
printf("Decryption successful for tag %08X with type 0\n", tag);
}
if (verbose) {
printf("Decryption successful for tag %08X with type 6\n", tag);
}
} else if (verbose) {
printf("Decryption failed for tag %08X\n", tag);
Expand Down
21 changes: 15 additions & 6 deletions pspdecrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ int main(int argc, char *argv[]) {
string outDir = "";
string outFile = "";
string preipl = "";
string logStr;
bool preiplSet = false;
u8 preiplBuf[MAX_PREIPL_SIZE];
u32 preiplSize = 0;
Expand Down Expand Up @@ -157,12 +158,11 @@ int main(int argc, char *argv[]) {
}

streampos size = inFile.tellg();
char* inData = new char[size];
unsigned char* inData = new unsigned char[size];
unsigned char* outData = new unsigned char[size];
inFile.seekg(0, ios::beg);
inFile.read(inData, size);
inFile.read((char*)inData, size);
inFile.close();
const auto plainSize = *(s32_le*)&inData[0x28];
char* outData = new char[plainSize];
if (size < 0x30) {
cerr << "Input file is too small!" << endl;
return 1;
Expand Down Expand Up @@ -206,8 +206,17 @@ int main(int argc, char *argv[]) {
if (infoOnly) {
cout << "Input is an encrypted PSP executable encrypted with tag " << hex << setw(8) << *(u32*)&inData[0xD0] << endl;
} else {
int outSize = pspDecryptPRX((const u8*)inData, (u8 *)outData, plainSize, nullptr, true);
WriteFile(outFile.c_str(), outData, plainSize);
int outSize = pspDecryptPRX((const u8*)inData, (u8 *)outData, size, nullptr, true);
u32 plainSize = (u32)*(u32_le *)&inData[0x28];
u8 out[plainSize];
if ((outData[0] == 0x1F && (u8)outData[1] == 0x8B) ||
memcmp(outData, "2RLZ", 4) == 0 || memcmp(outData, "KL4E", 4) == 0
|| memcmp(outData, "KL3E", 4) == 0){
pspDecompress((u8*)outData, outSize, out, plainSize, logStr, NULL);
WriteFile(outFile.c_str(), out, plainSize);
}else {
WriteFile(outFile.c_str(), outData, outSize);
}
}
break;
case PBP_MAGIC:
Expand Down

0 comments on commit 2a2b926

Please sign in to comment.