[FFmpeg-devel] [PATCH v2 2/2] fftools/resman: uncompress to actual-size memory buffers

softworkz ffmpegagent at gmail.com
Sat May 31 06:58:45 EEST 2025


From: softworkz <softworkz at hotmail.com>

Signed-off-by: softworkz <softworkz at hotmail.com>
---
 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 f098bd6687..e3e082abbf 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);
-- 
ffmpeg-codebot


More information about the ffmpeg-devel mailing list