summaryrefslogtreecommitdiff
path: root/lib/comp/lzma.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-04 15:32:52 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-04 22:22:48 +0200
commit2b975a449c17268f943403176a7609079b7af084 (patch)
treefee51e2f4b4f424c9a55dc35fb7aad21e3ce6580 /lib/comp/lzma.c
parent6f7ee71165b30272a4f18bca361c324c7671680c (diff)
Remove compressor internal buffers
Pass in an external destination buffer + size and allow for propper bounds checking (especially when unpacking). Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/comp/lzma.c')
-rw-r--r--lib/comp/lzma.c34
1 files changed, 12 insertions, 22 deletions
diff --git a/lib/comp/lzma.c b/lib/comp/lzma.c
index fd2a6c7..3169c33 100644
--- a/lib/comp/lzma.c
+++ b/lib/comp/lzma.c
@@ -9,12 +9,11 @@
typedef struct {
compressor_t base;
-
size_t block_size;
- uint8_t buffer[];
} lzma_compressor_t;
-static ssize_t lzma_comp_block(compressor_t *base, uint8_t *block, size_t size)
+static ssize_t lzma_comp_block(compressor_t *base, const uint8_t *in,
+ size_t size, uint8_t *out, size_t outsize)
{
lzma_compressor_t *lzma = (lzma_compressor_t *)base;
lzma_filter filters[5];
@@ -36,16 +35,10 @@ static ssize_t lzma_comp_block(compressor_t *base, uint8_t *block, size_t size)
filters[1].options = NULL;
ret = lzma_stream_buffer_encode(filters, LZMA_CHECK_CRC32, NULL,
- block, size, lzma->buffer,
- &written, lzma->block_size);
-
- if (ret == LZMA_OK) {
- if (written >= size)
- return 0;
+ in, size, out, &written, outsize);
- memcpy(block, lzma->buffer, size);
- return written;
- }
+ if (ret == LZMA_OK)
+ return (written >= size) ? 0 : written;
if (ret != LZMA_BUF_ERROR) {
fputs("lzma block compress failed\n", stderr);
@@ -55,24 +48,21 @@ static ssize_t lzma_comp_block(compressor_t *base, uint8_t *block, size_t size)
return 0;
}
-static ssize_t lzma_uncomp_block(compressor_t *base, uint8_t *block,
- size_t size)
+static ssize_t lzma_uncomp_block(compressor_t *base, const uint8_t *in,
+ size_t size, uint8_t *out, size_t outsize)
{
- lzma_compressor_t *lzma = (lzma_compressor_t *)base;
uint64_t memlimit = 32 * 1024 * 1024;
size_t dest_pos = 0;
size_t src_pos = 0;
lzma_ret ret;
+ (void)base;
ret = lzma_stream_buffer_decode(&memlimit, 0, NULL,
- block, &src_pos, size,
- lzma->buffer, &dest_pos,
- lzma->block_size);
+ in, &src_pos, size,
+ out, &dest_pos, outsize);
- if (ret == LZMA_OK && size == src_pos) {
- memcpy(block, lzma->buffer, dest_pos);
+ if (ret == LZMA_OK && size == src_pos)
return (ssize_t)dest_pos;
- }
fputs("lzma block extract failed\n", stderr);
return -1;
@@ -85,7 +75,7 @@ static void lzma_destroy(compressor_t *base)
compressor_t *create_lzma_compressor(bool compress, size_t block_size)
{
- lzma_compressor_t *lzma = calloc(1, sizeof(*lzma) + block_size);
+ lzma_compressor_t *lzma = calloc(1, sizeof(*lzma));
compressor_t *base = (compressor_t *)lzma;
if (lzma == NULL) {