From abfda2ce69b81812d01a5923ab2c5d4c976bb99d Mon Sep 17 00:00:00 2001 From: Mirco Miranda Date: Thu, 23 Oct 2025 09:12:22 +0200 Subject: [PATCH] On demand buffer allocation on PCHG decompression --- src/imageformats/chunks.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/imageformats/chunks.cpp b/src/imageformats/chunks.cpp index 2b0c5ef..7c91e0c 100644 --- a/src/imageformats/chunks.cpp +++ b/src/imageformats/chunks.cpp @@ -2795,10 +2795,6 @@ static QByteArray pchgFastDecomp(const QByteArray& input, int treeSize, int orig return {}; } - QByteArray out; - out.resize(originalSize); - char* outPtr = out.data(); - // Emulate a3 pointer to words: // a2 points to the *last word* => word index (0..treeWords-1) auto resetA3 = [&]() { @@ -2823,10 +2819,9 @@ static QByteArray pchgFastDecomp(const QByteArray& input, int treeSize, int orig return true; }; - int produced = 0; - // Main decode loop: produce exactly originalSize bytes - while (produced < originalSize) { + QByteArray out; + while (out.size() < qsizetype(originalSize)) { if (bits == 0) { if (!refill()) { // Not enough bits to complete output @@ -2862,7 +2857,7 @@ static QByteArray pchgFastDecomp(const QByteArray& input, int treeSize, int orig a3_word = next; } else { // Leaf: emit low 8 bits, reset a3 - outPtr[produced++] = static_cast(w & 0xFF); + out.append(static_cast(w & 0xFF)); a3_word = resetA3(); } } else { @@ -2882,7 +2877,7 @@ static QByteArray pchgFastDecomp(const QByteArray& input, int treeSize, int orig // Non-negative: check bit #8; if set -> leaf if ((w & 0x0100) != 0) { - outPtr[produced++] = static_cast(w & 0xFF); + out.append(static_cast(w & 0xFF)); a3_word = resetA3(); } else { // Not a leaf: continue scanning