switch-coreboot/payloads/libpayload/liblzma/lzma.c
Julius Werner d7c15e636c UPSTREAM: libpayload: lzma: Allocate scratchpad on the heap
Allocating a 15980-byte scratchpad on the stack when your default stack
size is set to 16KB is really not a great idea. We're regularly
overflowing into the end of our heap when using LZMA in libpayload, and
just happen not to notice it because the heap rarely gets filled up all
the way. Of course, since we always *have* a heap in libpayload, the
much saner solution is to just use it directly to allocate the
scratchpad rather than accidentally grow backwards into it anyway.

BUG=None
BRANCH=None
TEST=None

Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/16089

Reviewed-by: Aaron Durbin <adurbin@chromium.org>

Change-Id: Ibe4f02057a32bd156a126302178fa6fcab637d2c
Reviewed-on: https://chromium-review.googlesource.com/368287
Commit-Ready: Furquan Shaikh <furquan@chromium.org>
Tested-by: Furquan Shaikh <furquan@chromium.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
2016-08-13 20:28:18 -07:00

61 lines
1.7 KiB
C

/*
* coreboot interface to memory-saving variant of LZMA decoder
*
* Copyright (C) 2006 Carl-Daniel Hailfinger
* Released under the BSD license
*
* Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the LZMA
* SDK 4.42, which is written and distributed to public domain by Igor Pavlov.
*
*/
#include <lzma.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lzmadecode.c"
unsigned long ulzman(const unsigned char *src, unsigned long srcn,
unsigned char *dst, unsigned long dstn)
{
unsigned char properties[LZMA_PROPERTIES_SIZE];
const int data_offset = LZMA_PROPERTIES_SIZE + 8;
UInt32 outSize;
SizeT inProcessed;
SizeT outProcessed;
int res;
CLzmaDecoderState state;
SizeT mallocneeds;
unsigned char *scratchpad;
memcpy(properties, src, LZMA_PROPERTIES_SIZE);
memcpy(&outSize, src + LZMA_PROPERTIES_SIZE, sizeof(outSize));
if (outSize > dstn)
outSize = dstn;
if (LzmaDecodeProperties(&state.Properties, properties,
LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
printf("lzma: Incorrect stream properties.\n");
return 0;
}
mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
scratchpad = malloc(mallocneeds);
if (!scratchpad) {
printf("lzma: Cannot allocate %u bytes for scratchpad!\n",
mallocneeds);
return 0;
}
state.Probs = (CProb *)scratchpad;
res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
&inProcessed, dst, outSize, &outProcessed);
free(scratchpad);
if (res != 0) {
printf("lzma: Decoding error = %d\n", res);
return 0;
}
return outProcessed;
}
unsigned long ulzma(const unsigned char *src, unsigned char *dst)
{
return ulzman(src, (unsigned long)(-1), dst, (unsigned long)(-1));
}