Swap calloc for unique_ptr, tidied up code layout

This commit is contained in:
TheSpicyChef 2025-04-12 09:24:08 +01:00
parent fda7d86aec
commit f8637cf7cf
2 changed files with 28 additions and 48 deletions

View file

@ -205,20 +205,17 @@ Image Xex2LoadImage(const uint8_t* data, size_t dataSize)
const uint32_t exeLength = dataSize - headerSize; const uint32_t exeLength = dataSize - headerSize;
const uint8_t* exeBuffer = srcData; const uint8_t* exeBuffer = srcData;
uint8_t* compressBuffer = NULL; auto compressBuffer = std::make_unique<uint8_t[]>(exeLength);
const uint8_t* p = NULL; const uint8_t* p = NULL;
uint8_t* d = NULL; uint8_t* d = NULL;
sha1::SHA1 s; sha1::SHA1 s;
compressBuffer = (uint8_t*)calloc(1, exeLength);
p = exeBuffer; p = exeBuffer;
d = compressBuffer; d = compressBuffer.get();
int resultCode = 0;
uint8_t blockCalcedDigest[0x14]; uint8_t blockCalcedDigest[0x14];
while (blocks->blockSize) { while (blocks->blockSize)
{
const uint8_t* pNext = p + blocks->blockSize; const uint8_t* pNext = p + blocks->blockSize;
const auto* nextBlock = (const Xex2CompressedBlockInfo*)p; const auto* nextBlock = (const Xex2CompressedBlockInfo*)p;
@ -226,20 +223,19 @@ Image Xex2LoadImage(const uint8_t* data, size_t dataSize)
s.processBytes(p, blocks->blockSize); s.processBytes(p, blocks->blockSize);
s.finalize(blockCalcedDigest); s.finalize(blockCalcedDigest);
if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0) { if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0)
resultCode = 2; return {};
break;
}
p += 4; p += 4;
p += 20; p += 20;
while (true) { while (true)
{
const size_t chunkSize = (p[0] << 8) | p[1]; const size_t chunkSize = (p[0] << 8) | p[1];
p += 2; p += 2;
if (!chunkSize) {
if (!chunkSize)
break; break;
}
memcpy(d, p, chunkSize); memcpy(d, p, chunkSize);
p += chunkSize; p += chunkSize;
@ -250,22 +246,14 @@ Image Xex2LoadImage(const uint8_t* data, size_t dataSize)
blocks = nextBlock; blocks = nextBlock;
} }
if (!resultCode) int resultCode = 0;
{ uint32_t uncompressedSize = security->imageSize;
uint32_t uncompressedSize = security->imageSize; uint8_t* buffer = destData;
uint8_t* buffer = destData;
resultCode = lzxDecompress(compressBuffer, d - compressBuffer, buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0); resultCode = lzxDecompress(compressBuffer.get(), d - compressBuffer.get(), buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0);
}
if (compressBuffer)
free((void*)compressBuffer);
if (resultCode) if (resultCode)
{
return {}; return {};
}
} }
} }

View file

@ -409,20 +409,17 @@ XexPatcher::Result XexPatcher::apply(const uint8_t* xexBytes, size_t xexBytesSiz
const uint32_t exeLength = xexBytesSize - xexHeader->headerSize.get(); const uint32_t exeLength = xexBytesSize - xexHeader->headerSize.get();
const uint8_t* exeBuffer = &outBytes[headerTargetSize]; const uint8_t* exeBuffer = &outBytes[headerTargetSize];
uint8_t* compressBuffer = NULL; auto compressBuffer = std::make_unique<uint8_t[]>(exeLength);
const uint8_t* p = NULL; const uint8_t* p = NULL;
uint8_t* d = NULL; uint8_t* d = NULL;
sha1::SHA1 s; sha1::SHA1 s;
compressBuffer = (uint8_t*)calloc(1, exeLength);
p = exeBuffer; p = exeBuffer;
d = compressBuffer; d = compressBuffer.get();
int resultCode = 0;
uint8_t blockCalcedDigest[0x14]; uint8_t blockCalcedDigest[0x14];
while (blocks->blockSize) { while (blocks->blockSize)
{
const uint8_t* pNext = p + blocks->blockSize; const uint8_t* pNext = p + blocks->blockSize;
const auto* nextBlock = (const Xex2CompressedBlockInfo*)p; const auto* nextBlock = (const Xex2CompressedBlockInfo*)p;
@ -430,20 +427,19 @@ XexPatcher::Result XexPatcher::apply(const uint8_t* xexBytes, size_t xexBytesSiz
s.processBytes(p, blocks->blockSize); s.processBytes(p, blocks->blockSize);
s.finalize(blockCalcedDigest); s.finalize(blockCalcedDigest);
if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0) { if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0)
resultCode = 2; return Result::PatchFailed;
break;
}
p += 4; p += 4;
p += 20; p += 20;
while (true) { while (true)
{
const size_t chunkSize = (p[0] << 8) | p[1]; const size_t chunkSize = (p[0] << 8) | p[1];
p += 2; p += 2;
if (!chunkSize) {
if (!chunkSize)
break; break;
}
memcpy(d, p, chunkSize); memcpy(d, p, chunkSize);
p += chunkSize; p += chunkSize;
@ -454,15 +450,11 @@ XexPatcher::Result XexPatcher::apply(const uint8_t* xexBytes, size_t xexBytesSiz
blocks = nextBlock; blocks = nextBlock;
} }
if (!resultCode) int resultCode = 0;
{ uint32_t uncompressedSize = originalSecurityInfo->imageSize;
uint32_t uncompressedSize = originalSecurityInfo->imageSize; uint8_t* buffer = outBytes.data() + newXexHeaderSize;
uint8_t* buffer = outBytes.data() + newXexHeaderSize;
resultCode = lzxDecompress(compressBuffer, d - compressBuffer, buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0);
}
if (compressBuffer) resultCode = lzxDecompress(compressBuffer.get(), d - compressBuffer.get(), buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0);
free((void*)compressBuffer);
if (resultCode) if (resultCode)
return Result::PatchFailed; return Result::PatchFailed;