Author: myles Date: 2008-04-14 16:19:09 +0200 (Mon, 14 Apr 2008) New Revision: 658
Modified: coreboot-v3/util/lar/stream.c coreboot-v3/util/lzma/minilzma.cc coreboot-v3/util/nrv2b/nrv2b.c Log: 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 mylesgw@gmail.com Acked-by: Stefan Reinauer stepan@coresystems.de
Modified: coreboot-v3/util/lar/stream.c =================================================================== --- coreboot-v3/util/lar/stream.c 2008-04-05 20:18:47 UTC (rev 657) +++ coreboot-v3/util/lar/stream.c 2008-04-14 14:19:09 UTC (rev 658) @@ -846,7 +846,7 @@
/** * 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
Modified: coreboot-v3/util/lzma/minilzma.cc =================================================================== --- coreboot-v3/util/lzma/minilzma.cc 2008-04-05 20:18:47 UTC (rev 657) +++ coreboot-v3/util/lzma/minilzma.cc 2008-04-14 14:19:09 UTC (rev 658) @@ -283,11 +283,21 @@ #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<unsigned char> result; result = LZMACompress(std::vector<unsigned char>(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) {
Modified: coreboot-v3/util/nrv2b/nrv2b.c =================================================================== --- coreboot-v3/util/nrv2b/nrv2b.c 2008-04-05 20:18:47 UTC (rev 657) +++ coreboot-v3/util/nrv2b/nrv2b.c 2008-04-14 14:19:09 UTC (rev 658) @@ -1304,11 +1304,28 @@ #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