From 4d7af186ce9f8a8d3b12cdc2c979ea54320eed96 Mon Sep 17 00:00:00 2001 From: softworkz Date: Sat, 31 May 2025 01:11:19 +0200 Subject: [PATCH 1/2] fftools/resman: remove unused includes, fix declaration Signed-off-by: softworkz --- fftools/resources/resman.c | 7 +++---- fftools/resources/resman.h | 10 +--------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/fftools/resources/resman.c b/fftools/resources/resman.c index a9e21626fa81c..f098bd6687b3f 100644 --- a/fftools/resources/resman.c +++ b/fftools/resources/resman.c @@ -34,7 +34,6 @@ #include "resman.h" #include "fftools/ffmpeg_filter.h" #include "libavutil/avassert.h" -#include "libavutil/pixdesc.h" #include "libavutil/dict.h" #include "libavutil/common.h" @@ -61,7 +60,7 @@ typedef struct ResourceManagerContext { static AVMutex mutex = AV_MUTEX_INITIALIZER; -ResourceManagerContext *resman_ctx = NULL; +static ResourceManagerContext *resman_ctx = NULL; #if CONFIG_RESOURCE_COMPRESSION @@ -77,7 +76,7 @@ static int decompress_gzip(ResourceManagerContext *ctx, uint8_t *in, unsigned in memset(&strm, 0, sizeof(strm)); // Allocate output buffer with extra byte for null termination - buf = (uint8_t *)av_mallocz(chunk + 1); + buf = av_mallocz(chunk + 1); if (!buf) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate decompression buffer\n"); return AVERROR(ENOMEM); @@ -156,7 +155,7 @@ void ff_resman_uninit(void) } -char *ff_resman_get_string(FFResourceId resource_id) +const char *ff_resman_get_string(FFResourceId resource_id) { ResourceManagerContext *ctx = get_resman_context(); FFResourceDefinition resource_definition = { 0 }; diff --git a/fftools/resources/resman.h b/fftools/resources/resman.h index 6485db5091f5b..dfe6b324e4ffa 100644 --- a/fftools/resources/resman.h +++ b/fftools/resources/resman.h @@ -21,14 +21,6 @@ #ifndef FFTOOLS_RESOURCES_RESMAN_H #define FFTOOLS_RESOURCES_RESMAN_H -#include - -#include "config.h" -#include "fftools/ffmpeg.h" -#include "libavutil/avutil.h" -#include "libavutil/bprint.h" -#include "fftools/textformat/avtextformat.h" - typedef enum { FF_RESOURCE_GRAPH_CSS, FF_RESOURCE_GRAPH_HTML, @@ -45,6 +37,6 @@ typedef struct FFResourceDefinition { void ff_resman_uninit(void); -char *ff_resman_get_string(FFResourceId resource_id); +const char *ff_resman_get_string(FFResourceId resource_id); #endif /* FFTOOLS_RESOURCES_RESMAN_H */ From 32cd61ebd4196f09ca2ed1d1c062ac2370844f75 Mon Sep 17 00:00:00 2001 From: softworkz Date: Sat, 31 May 2025 05:44:17 +0200 Subject: [PATCH 2/2] fftools/resman: uncompress to actual-size memory buffers Signed-off-by: softworkz --- fftools/resources/resman.c | 51 +++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/fftools/resources/resman.c b/fftools/resources/resman.c index f098bd6687b3f..e3e082abbfa03 100644 --- a/fftools/resources/resman.c +++ b/fftools/resources/resman.c @@ -67,48 +67,69 @@ static ResourceManagerContext *resman_ctx = NULL; static int decompress_gzip(ResourceManagerContext *ctx, uint8_t *in, unsigned in_len, char **out, size_t *out_len) { - z_stream strm; - unsigned chunk = 65534; + z_stream strm = { 0 }; + uint8_t dummy[4096]; + size_t uncompressed_size; int ret; uint8_t *buf; *out = NULL; - memset(&strm, 0, sizeof(strm)); + + // 15 + 16 tells zlib to detect GZIP or zlib automatically + ret = inflateInit2(&strm, 15 + 16); + if (ret != Z_OK) { + av_log(ctx, AV_LOG_ERROR, "Error during zlib initialization: %s\n", strm.msg); + return AVERROR(ENOSYS); + } + + strm.avail_in = in_len; + strm.next_in = in; + + do { + strm.avail_out = sizeof(dummy); + strm.next_out = dummy; + + ret = inflate(&strm, Z_NO_FLUSH); + if (ret != Z_OK && ret != Z_STREAM_END) { + av_log(ctx, AV_LOG_ERROR, "Inflate failed (1): %d, %s\n", ret, strm.msg); + inflateEnd(&strm); + return AVERROR(EINVAL); + } + } while (ret != Z_STREAM_END); + + uncompressed_size = strm.total_out; // Allocate output buffer with extra byte for null termination - buf = av_mallocz(chunk + 1); + buf = av_mallocz(uncompressed_size + 1); if (!buf) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate decompression buffer\n"); + inflateEnd(&strm); return AVERROR(ENOMEM); } - // 15 + 16 tells zlib to detect GZIP or zlib automatically - ret = inflateInit2(&strm, 15 + 16); + ret = inflateReset(&strm); if (ret != Z_OK) { - av_log(ctx, AV_LOG_ERROR, "Error during zlib initialization: %s\n", strm.msg); + av_log(ctx, AV_LOG_ERROR, "inflateReset failed: %s\n", strm.msg); av_free(buf); return AVERROR(ENOSYS); } strm.avail_in = in_len; strm.next_in = in; - strm.avail_out = chunk; + strm.avail_out = uncompressed_size; strm.next_out = buf; ret = inflate(&strm, Z_FINISH); if (ret != Z_OK && ret != Z_STREAM_END) { - av_log(ctx, AV_LOG_ERROR, "Inflate failed: %d, %s\n", ret, strm.msg); + av_log(ctx, AV_LOG_ERROR, "Inflate failed (2): %d, %s\n", ret, strm.msg); inflateEnd(&strm); av_free(buf); - return (ret == Z_STREAM_END) ? Z_OK : ((ret == Z_OK) ? Z_BUF_ERROR : ret); + return AVERROR(EINVAL); } - if (strm.avail_out == 0) { - // TODO: Error or loop decoding? - av_log(ctx, AV_LOG_WARNING, "Decompression buffer may be too small\n"); - } + av_assert0(strm.avail_out == 0); - *out_len = chunk - strm.avail_out; + *out_len = uncompressed_size; buf[*out_len] = 0; // Ensure null termination inflateEnd(&strm);