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