Skip to content

Commit

Permalink
Decompress second ME image
Browse files Browse the repository at this point in the history
  • Loading branch information
artart78 committed Nov 13, 2021
1 parent c74d65a commit ac4da80
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
19 changes: 18 additions & 1 deletion PsarDecrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,14 @@ int pspDecryptPSAR(u8 *dataPSAR, u32 size, std::string outdir, bool extractOnly,
int cbToSave = cbDecrypted;

logStr += ",decrypted";
u8 *endPtr;
int cbRemain = 0;

if ((data1[0] == 0x1F && data1[1] == 0x8B) ||
memcmp(data1, "2RLZ", 4) == 0 || memcmp(data1, "KL4E", 4) == 0)
{
int cbExp = pspDecompress(data1, cbToSave, data2, 3000000, logStr);
int cbExp = pspDecompress(data1, cbToSave, data2, 3000000, logStr, &endPtr);
cbRemain = data1 + cbToSave - endPtr;

if (cbExp > 0)
{
Expand All @@ -692,6 +695,20 @@ int pspDecryptPSAR(u8 *dataPSAR, u32 size, std::string outdir, bool extractOnly,
if (CheckExtractReboot(name, pbToSave, cbToSave, data1, data2, outdir, extractOnly, logStr) < 0) {
logStr += ",error extracting/decrypting reboot.bin";
}

if (cbRemain > 0) {
u8 *endPtr2;
logStr += "Has part2";
int cbExp = pspDecompress(endPtr, cbRemain, data2, 3000000, logStr, &endPtr2);
logStr += ",decompressed,saved!";
if (endPtr + cbRemain - endPtr2 != 0) {
logStr += "Error: garbage at end.";
}
if (WriteFile((szDataPath + ".2").c_str(), data2, cbExp) != cbExp)
{
printf("Error writing %s.\n", (szDataPath + ".2").c_str());
}
}
}
else
{
Expand Down
14 changes: 9 additions & 5 deletions pspdecrypt_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,28 +336,32 @@ int pspIsCompressed(u8 *buf)
return res;
}

int pspDecompress(u8 *inbuf, u32 insize, u8 *outbuf, u32 outcapacity, std::string &logStr)
int pspDecompress(u8 *inbuf, u32 insize, u8 *outbuf, u32 outcapacity, std::string &logStr, u8 **inbufEnd)
{
int retsize;

if (inbuf[0] == 0x1F && inbuf[1] == 0x8B)
{
retsize = gunzip(inbuf, insize, outbuf, outcapacity);
u32 realSize;
retsize = gunzip(inbuf, insize, outbuf, outcapacity, &realSize);
if (inbufEnd != NULL) {
*inbufEnd = inbuf + realSize;
}
logStr += ",gzip";
}
else if (memcmp(inbuf, "2RLZ", 4) == 0)
{
retsize = LZRDecompress(outbuf, outcapacity, inbuf+4, NULL);
retsize = LZRDecompress(outbuf, outcapacity, inbuf+4, inbufEnd);
logStr += ",lzrc";
}
else if (memcmp(inbuf, "KL4E", 4) == 0)
{
retsize = decompress_kle(outbuf, outcapacity, inbuf+4, NULL, 1);
retsize = decompress_kle(outbuf, outcapacity, inbuf+4, (void **)inbufEnd, 1);
logStr += ",kl4e";
}
else if (memcmp(inbuf, "KL3E", 4) == 0)
{
retsize = decompress_kle(outbuf, outcapacity, inbuf+4, NULL, 0);
retsize = decompress_kle(outbuf, outcapacity, inbuf+4, (void **)inbufEnd, 0);
logStr += ",kl3e";
}
else
Expand Down
3 changes: 2 additions & 1 deletion pspdecrypt_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ int pspIsCompressed(u8 *buf);
* @param inbuf - The input buffer with the compressed data
* @param outbuf - The output buffer that receives the decompressed data
* @param outcapacity - The max capacity of the output buffer
* @param inbufEnd - Pointer to the end of the compressed stream of the input buffer
*
* @returns the size of the decompressed data on success, < 0 on error
*/
int pspDecompress(u8 *inbuf, u32 insize, u8 *outbuf, u32 outcapacity, std::string &logStr);
int pspDecompress(u8 *inbuf, u32 insize, u8 *outbuf, u32 outcapacity, std::string &logStr, u8 **inbufEnd = NULL);

/**
* Decrypts a file table (3.70+)
Expand Down

0 comments on commit ac4da80

Please sign in to comment.