From 3bd5f335f75398382944386cd36cfa3224e991c4 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sat, 26 Feb 2022 20:17:05 +0000 Subject: [PATCH] inflate: Add helper for getting decompressed data size Helper for another semi-typical operation: find out how big of a buffer should be allocated before decompressing. This can be useful when the stream container doesn't specify the decompressed size. Change-Id: I5f1536f809bb6f9bc6023120c024c3de7cea4269 --- firmware/common/inflate.c | 9 +++++++++ firmware/include/inflate.h | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/firmware/common/inflate.c b/firmware/common/inflate.c index 62661ee111..e39fe3a14e 100644 --- a/firmware/common/inflate.c +++ b/firmware/common/inflate.c @@ -782,3 +782,12 @@ uint32_t inflate_buffer_writer(const void* block, uint32_t block_size, void* ctx struct inflate_bufferctx* c = ctx; return inflate_buffer_rw(c, c->buf, block, block_size); } + +uint32_t inflate_getsize_writer(const void* block, uint32_t block_size, void* ctx) +{ + (void)block; + + size_t* size = ctx; + *size += block_size; + return block_size; +} diff --git a/firmware/include/inflate.h b/firmware/include/inflate.h index 7dae0448e3..1fce186f34 100644 --- a/firmware/include/inflate.h +++ b/firmware/include/inflate.h @@ -58,4 +58,13 @@ struct inflate_bufferctx { uint32_t inflate_buffer_reader(void* block, uint32_t block_size, void* ctx); uint32_t inflate_buffer_writer(const void* block, uint32_t block_size, void* ctx); +// dummy writer used if you just want to figure out how big the decompressed +// data will be. It does not actually write any data. Example usage: +// +// size_t size = 0; +// inflate(it, st, read, rctx, inflate_getsize_writer, &size); +// +// Now 'size' will be the size of the decompressed data (assuming no errors). +uint32_t inflate_getsize_writer(const void* block, uint32_t block_size, void* ctx); + #endif