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 uint8_t* exeBuffer = srcData;
uint8_t* compressBuffer = NULL;
auto compressBuffer = std::make_unique<uint8_t[]>(exeLength);
const uint8_t* p = NULL;
uint8_t* d = NULL;
sha1::SHA1 s;
compressBuffer = (uint8_t*)calloc(1, exeLength);
p = exeBuffer;
d = compressBuffer;
int resultCode = 0;
d = compressBuffer.get();
uint8_t blockCalcedDigest[0x14];
while (blocks->blockSize) {
while (blocks->blockSize)
{
const uint8_t* pNext = p + blocks->blockSize;
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.finalize(blockCalcedDigest);
if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0) {
resultCode = 2;
break;
}
if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0)
return {};
p += 4;
p += 20;
while (true) {
while (true)
{
const size_t chunkSize = (p[0] << 8) | p[1];
p += 2;
if (!chunkSize) {
if (!chunkSize)
break;
}
memcpy(d, p, chunkSize);
p += chunkSize;
@ -250,22 +246,14 @@ Image Xex2LoadImage(const uint8_t* data, size_t dataSize)
blocks = nextBlock;
}
if (!resultCode)
{
uint32_t uncompressedSize = security->imageSize;
uint8_t* buffer = destData;
int resultCode = 0;
uint32_t uncompressedSize = security->imageSize;
uint8_t* buffer = destData;
resultCode = lzxDecompress(compressBuffer, d - compressBuffer, buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0);
}
if (compressBuffer)
free((void*)compressBuffer);
resultCode = lzxDecompress(compressBuffer.get(), d - compressBuffer.get(), buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0);
if (resultCode)
{
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 uint8_t* exeBuffer = &outBytes[headerTargetSize];
uint8_t* compressBuffer = NULL;
auto compressBuffer = std::make_unique<uint8_t[]>(exeLength);
const uint8_t* p = NULL;
uint8_t* d = NULL;
sha1::SHA1 s;
compressBuffer = (uint8_t*)calloc(1, exeLength);
p = exeBuffer;
d = compressBuffer;
int resultCode = 0;
d = compressBuffer.get();
uint8_t blockCalcedDigest[0x14];
while (blocks->blockSize) {
while (blocks->blockSize)
{
const uint8_t* pNext = p + blocks->blockSize;
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.finalize(blockCalcedDigest);
if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0) {
resultCode = 2;
break;
}
if (memcmp(blockCalcedDigest, blocks->blockHash, 0x14) != 0)
return Result::PatchFailed;
p += 4;
p += 20;
while (true) {
while (true)
{
const size_t chunkSize = (p[0] << 8) | p[1];
p += 2;
if (!chunkSize) {
if (!chunkSize)
break;
}
memcpy(d, p, chunkSize);
p += chunkSize;
@ -454,15 +450,11 @@ XexPatcher::Result XexPatcher::apply(const uint8_t* xexBytes, size_t xexBytesSiz
blocks = nextBlock;
}
if (!resultCode)
{
uint32_t uncompressedSize = originalSecurityInfo->imageSize;
uint8_t* buffer = outBytes.data() + newXexHeaderSize;
resultCode = lzxDecompress(compressBuffer, d - compressBuffer, buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0);
}
int resultCode = 0;
uint32_t uncompressedSize = originalSecurityInfo->imageSize;
uint8_t* buffer = outBytes.data() + newXexHeaderSize;
if (compressBuffer)
free((void*)compressBuffer);
resultCode = lzxDecompress(compressBuffer.get(), d - compressBuffer.get(), buffer, uncompressedSize, ((const Xex2FileNormalCompressionInfo*)(fileFormatInfo + 1))->windowSize, nullptr, 0);
if (resultCode)
return Result::PatchFailed;