From c4a9590044d91d734f483cafdaf3dede318288cc Mon Sep 17 00:00:00 2001 From: Myles Watson Date: Mon, 14 Apr 2008 14:19:09 +0000 Subject: [PATCH] This very short patch fixes nrv2b compression in lar. It also fixes lzma compression in lar to fix the silent memory corruption that was possible when files didn't compress well. It adds some comments to both files and the file that calls them. Signed-off-by: Myles Watson Acked-by: Stefan Reinauer git-svn-id: svn://coreboot.org/repository/coreboot-v3@658 f3766cd6-281f-0410-b1cd-43a5c92072e9 --- util/lar/stream.c | 2 +- util/lzma/minilzma.cc | 12 +++++++++++- util/nrv2b/nrv2b.c | 21 +++++++++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/util/lar/stream.c b/util/lar/stream.c index bf39473bf5..1818c539a2 100644 --- a/util/lar/stream.c +++ b/util/lar/stream.c @@ -846,7 +846,7 @@ int maxsize(struct lar *lar, char *name) /** * Compress an area according to an algorithm. If the area grows, - * use no compression. + * use no compression. The size of temp should be at least size bytes. * @param ptr data to be compressed * @param size size of the data * @param temp destination of compressed data diff --git a/util/lzma/minilzma.cc b/util/lzma/minilzma.cc index cd68e387ca..7610910a19 100644 --- a/util/lzma/minilzma.cc +++ b/util/lzma/minilzma.cc @@ -283,11 +283,21 @@ int main(int argc, char *argv[]) #else extern "C" { +/** + * Compress a buffer with lzma + * Don't copy the result back if it is too large. + * @param in a pointer to the buffer + * @param in_len the length in bytes + * @param out a pointer to a buffer of at least size in_len + * @param out_len a pointer to the compressed length of in + */ + void do_lzma_compress(char *in, int in_len, char *out, int *out_len) { std::vector result; result = LZMACompress(std::vector(in, in + in_len)); *out_len = result.size(); - std::memcpy(out, &result[0], *out_len); + if (*out_len < in_len) + std::memcpy(out, &result[0], *out_len); } void do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len) { diff --git a/util/nrv2b/nrv2b.c b/util/nrv2b/nrv2b.c index 52ef0d138c..832a20014c 100644 --- a/util/nrv2b/nrv2b.c +++ b/util/nrv2b/nrv2b.c @@ -1304,11 +1304,28 @@ void Encode(void) /* compression */ #endif #ifdef COMPACT + +/** + * Compress a buffer with nrv2b + * Don't copy the result back if it is too large + * @param in a pointer to the buffer + * @param in_len the length in bytes + * @param out a pointer to a buffer of at least size in_len + * @param out_len a pointer to the compressed length of in + */ + void do_nrv2b_compress(uint8_t *in, int in_len, uint8_t *out, int *out_len) { unsigned long new_out_len = in_len + (in_len/8) + 256; - out = malloc(new_out_len); - ucl_nrv2b_99_compress(in, in_len, out, &new_out_len, 0 ); + uint8_t *new_out = malloc(new_out_len); + if (new_out == NULL) { + printf("Not enough memory to allocate buffer.\n"); + exit(1); + } + ucl_nrv2b_99_compress(in, in_len, new_out, &new_out_len, 0 ); *out_len = (int) new_out_len; + if (*out_len < in_len) + memcpy(out, new_out, *out_len); + free(new_out); } #endif