commit c7bca7a: [Minor] Lua_compression: Add support of the compression level for gzip

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Aug 26 15:14:04 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-08-26 16:08:28 +0100
URL: https://github.com/rspamd/rspamd/commit/c7bca7a471a4d91ba5b7489ff234f30f92821dac (HEAD -> master)

[Minor] Lua_compression: Add support of the compression level for gzip

---
 src/lua/lua_compress.c | 13 +++++++++++--
 src/lua/lua_util.c     |  2 +-
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/lua/lua_compress.c b/src/lua/lua_compress.c
index 599a796e0..f383d27c2 100644
--- a/src/lua/lua_compress.c
+++ b/src/lua/lua_compress.c
@@ -323,7 +323,7 @@ lua_compress_zlib_compress (lua_State *L)
 	struct rspamd_lua_text *t = NULL, *res;
 	gsize sz;
 	z_stream strm;
-	gint rc;
+	gint rc, comp_level = Z_DEFAULT_COMPRESSION;
 	guchar *p;
 	gsize remain;
 
@@ -333,9 +333,18 @@ lua_compress_zlib_compress (lua_State *L)
 		return luaL_error (L, "invalid arguments");
 	}
 
+	if (lua_isnumber (L, 2)) {
+		comp_level = lua_tointeger (L, 2);
+
+		if (comp_level > Z_BEST_COMPRESSION || comp_level < Z_BEST_SPEED) {
+			return luaL_error (L, "invalid arguments: compression level must be between %d and %d",
+					Z_BEST_SPEED, Z_BEST_COMPRESSION);
+		}
+	}
+
 
 	memset (&strm, 0, sizeof (strm));
-	rc = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
+	rc = deflateInit2 (&strm, comp_level, Z_DEFLATED,
 			MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY);
 
 	if (rc != Z_OK) {
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index 006917ad7..1ac69365a 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -401,7 +401,7 @@ LUA_FUNCTION_DEF (util, gzip_decompress);
 LUA_FUNCTION_DEF (util, inflate);
 
 /***
- * @function util.gzip_compress(data)
+ * @function util.gzip_compress(data, [level=1])
  * Compresses input using gzip compression
  *
  * @param {string/rspamd_text} data input data


More information about the Commits mailing list