diff --git a/fftools/resources/resman.c b/fftools/resources/resman.c index a9e21626fa81c..e3e082abbfa03 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,55 +60,76 @@ typedef struct ResourceManagerContext { static AVMutex mutex = AV_MUTEX_INITIALIZER; -ResourceManagerContext *resman_ctx = NULL; +static ResourceManagerContext *resman_ctx = NULL; #if CONFIG_RESOURCE_COMPRESSION 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 = (uint8_t *)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); @@ -156,7 +176,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 */