summaryrefslogtreecommitdiff
path: root/lib/comp/zlib.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/zlib.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/zlib.c')
-rw-r--r--lib/comp/zlib.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/lib/comp/zlib.c b/lib/comp/zlib.c
index d3bfe70..f151132 100644
--- a/lib/comp/zlib.c
+++ b/lib/comp/zlib.c
@@ -14,7 +14,6 @@ typedef struct {
bool compress;
size_t block_size;
- uint8_t buffer[];
} zlib_compressor_t;
static void zlib_destroy(compressor_t *base)
@@ -30,7 +29,8 @@ static void zlib_destroy(compressor_t *base)
free(zlib);
}
-static ssize_t zlib_do_block(compressor_t *base, uint8_t *block, size_t size)
+static ssize_t zlib_do_block(compressor_t *base, const uint8_t *in,
+ size_t size, uint8_t *out, size_t outsize)
{
zlib_compressor_t *zlib = (zlib_compressor_t *)base;
size_t written;
@@ -47,10 +47,10 @@ static ssize_t zlib_do_block(compressor_t *base, uint8_t *block, size_t size)
return -1;
}
- zlib->strm.next_in = (void *)block;
+ zlib->strm.next_in = (void *)in;
zlib->strm.avail_in = size;
- zlib->strm.next_out = zlib->buffer;
- zlib->strm.avail_out = zlib->block_size;
+ zlib->strm.next_out = out;
+ zlib->strm.avail_out = outsize;
if (zlib->compress) {
ret = deflate(&zlib->strm, Z_FINISH);
@@ -64,7 +64,6 @@ static ssize_t zlib_do_block(compressor_t *base, uint8_t *block, size_t size)
if (zlib->compress && written >= size)
return 0;
- memcpy(block, zlib->buffer, written);
return (ssize_t)written;
}
@@ -78,7 +77,7 @@ static ssize_t zlib_do_block(compressor_t *base, uint8_t *block, size_t size)
compressor_t *create_zlib_compressor(bool compress, size_t block_size)
{
- zlib_compressor_t *zlib = calloc(1, sizeof(*zlib) + block_size);
+ zlib_compressor_t *zlib = calloc(1, sizeof(*zlib));
compressor_t *base = (compressor_t *)zlib;
int ret;