Skip to content

Commit 873d05d

Browse files
committed
Refactor LZMA decompression API and integrate into ProcessImage
- Rename `ProcessCompressedData` to `static void *DecompressLzmaData(const void *data, size_t data_size, size_t *decompressed_size)` - Change all failure paths in `DecompressLzmaData` (header truncation, size limits, allocation failure, decompression failure, unsupported) to `return NULL` instead of `false` - Remove initial DEBUG of compressed data segment size; add `DEBUG("LZMA decompressed %zu bytes", data_size)` after successful decompression - Update `ProcessImage` to: - Log raw segment size via `DEBUG("Data segment size: %zu bytes", context->data_size)` - Use `IsDataCompressed` flag to choose between direct use of `context->data` and calling `DecompressLzmaData` - Store returned buffer and size in local `data`/`data_size`, call `process_opcodes`, free the buffer only if decompressed, and return the bool result
1 parent 5413c27 commit 873d05d

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

src/unpack.c

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,6 @@ static bool decompress_lzma(void *dest, unsigned long long dest_size,
205205
APP_ERROR("LZMA decompression error: %d, status: %d", res, status);
206206
return false;
207207
}
208-
209-
DEBUG(
210-
"LZMA decompressed %zu bytes from %zu input bytes",
211-
(size_t)decompressed_size, inSizePure
212-
);
213-
214208
return true;
215209
}
216210

@@ -225,61 +219,49 @@ static unsigned long long parse_lzma_unpack_size(const void *data)
225219
}
226220
#endif
227221

228-
bool ProcessCompressedData(const void *data, size_t data_len)
222+
static void *DecompressLzmaData(const void *data, size_t data_size,
223+
size_t *decompressed_size)
229224
{
230225
#if WITH_LZMA
231-
DEBUG("LZMA compressed data segment size: %zu bytes", data_len);
232-
233-
if (data_len < LZMA_HEADER_SIZE) {
226+
if (data_size < LZMA_HEADER_SIZE) {
234227
APP_ERROR("LZMA header is truncated");
235-
return false;
228+
return NULL;
236229
}
237230

238231
unsigned long long unpack_size = parse_lzma_unpack_size(data);
239232

240-
DEBUG("Parsed LZMA unpack size: %llu bytes", unpack_size);
233+
DEBUG("Parsed LZMA decompressed size: %llu bytes", unpack_size);
241234

242235
if (unpack_size > (unsigned long long)LZMA_SIZET_MAX) {
243236
APP_ERROR("Decompression size exceeds LZMA SizeT limit");
244-
return false;
237+
return NULL;
245238
}
246239

247240
if (unpack_size > (unsigned long long)SIZE_MAX) {
248241
APP_ERROR("Size too large to fit in size_t");
249-
return false;
242+
return NULL;
250243
}
251244

252245
void *unpack_data = malloc(unpack_size);
253246
if (!unpack_data) {
254247
APP_ERROR("Memory allocation failed during decompression");
255-
return false;
248+
return NULL;
256249
}
257250

258-
if (!decompress_lzma(unpack_data, unpack_size, data, data_len)) {
251+
if (!decompress_lzma(unpack_data, unpack_size, data, data_size)) {
259252
APP_ERROR("LZMA decompression failed");
260253
free(unpack_data);
261-
return false;
254+
return NULL;
262255
}
263256

264-
bool result = process_opcodes(unpack_data, unpack_size);
265-
free(unpack_data);
266-
267-
return result;
257+
*decompressed_size = unpack_size;
258+
return unpack_data;
268259
#else
269260
APP_ERROR("Does not support LZMA");
270-
return false;
261+
return NULL;
271262
#endif
272263
}
273264

274-
bool ProcessUncompressedData(const void *data, size_t data_len)
275-
{
276-
DEBUG("Uncompressed data segment size: %zu bytes", data_len);
277-
278-
bool result = process_opcodes(data, data_len);
279-
280-
return result;
281-
}
282-
283265
const uint8_t Signature[] = { 0x41, 0xb6, 0xba, 0x4e };
284266

285267
static const void *find_signature(const void *buffer, size_t buffer_size)
@@ -444,13 +426,35 @@ bool ProcessImage(const UnpackContext *context)
444426
{
445427
if (!context) {
446428
APP_ERROR("context is NULL");
447-
448429
return false;
449430
}
450431

451-
if (IsDataCompressed(context->modes)) {
452-
return ProcessCompressedData(context->data, context->data_size);
432+
DEBUG("Data segment size: %zu bytes", context->data_size);
433+
434+
bool compressed = IsDataCompressed(context->modes);
435+
void *data;
436+
size_t data_size;
437+
438+
if (compressed) {
439+
data = DecompressLzmaData(
440+
context->data,
441+
context->data_size,
442+
&data_size
443+
);
444+
if (!data) {
445+
APP_ERROR("LZMA decompression failed");
446+
return false;
447+
}
448+
DEBUG("LZMA decompressed %zu bytes", data_size);
453449
} else {
454-
return ProcessUncompressedData(context->data, context->data_size);
450+
data = (void *)context->data;
451+
data_size = context->data_size;
452+
}
453+
454+
bool ok = process_opcodes(data, data_size);
455+
456+
if (compressed) {
457+
free(data);
455458
}
459+
return ok;
456460
}

0 commit comments

Comments
 (0)