[PATCH] Gzip: compatibility with zlib in LIT_MEM mode

Maxim Dounin mdounin at mdounin.ru
Sat Feb 8 01:17:25 UTC 2025


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1738536371 -10800
#      Mon Feb 03 01:46:11 2025 +0300
# Node ID 18f61fc6a6c092680e98cb7741226c498ad64163
# Parent  dbf76fdd109fbbba40a7c5299cc277d180f4bbad
Gzip: compatibility with zlib in LIT_MEM mode.

As of zlib 1.3.1, with the LIT_MEM define zlib uses an additional buffer
for literals/lengths, that is, (1 << (memlevel + 6)) more memory in the
pending buffer.

Notably, the LIT_MEM define is enabled by default in zlib-ng.

diff --git a/src/http/modules/ngx_http_gzip_filter_module.c b/src/http/modules/ngx_http_gzip_filter_module.c
--- a/src/http/modules/ngx_http_gzip_filter_module.c
+++ b/src/http/modules/ngx_http_gzip_filter_module.c
@@ -57,6 +57,7 @@ typedef struct {
     unsigned             nomem:1;
     unsigned             buffering:1;
     unsigned             zlib_ng:1;
+    unsigned             zlib_lit_mem:1;
     unsigned             state_allocated:1;
 
     size_t               zin;
@@ -215,6 +216,7 @@ static ngx_http_output_header_filter_pt 
 static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;
 
 static ngx_uint_t  ngx_http_gzip_assume_zlib_ng;
+static ngx_uint_t  ngx_http_gzip_assume_zlib_lit_mem;
 
 
 static ngx_int_t
@@ -512,13 +514,23 @@ ngx_http_gzip_filter_memory(ngx_http_req
         ctx->allocated = 8192 + 16 + (1 << (wbits + 2))
                          + (1 << (memlevel + 9));
 
+        if (ngx_http_gzip_assume_zlib_lit_mem) {
+            /*
+             * When compiled with the LIT_MEM define, zlib uses an additional
+             * buffer for literals/lengths.
+             */
+
+            ctx->allocated += (1 << (memlevel + 6));
+            ctx->zlib_lit_mem = 1;
+        }
+
     } else {
         /*
          * Another zlib variant, https://github.com/zlib-ng/zlib-ng.
          * It used to force window bits to 13 for fast compression level,
          * uses (64 + sizeof(void*)) additional space on all allocations
          * for alignment, 16-byte padding in one of window-sized buffers,
-         * and 128K hash.
+         * 128K hash, and defines LIT_MEM by default.
          */
 
         if (conf->level == 1) {
@@ -527,6 +539,7 @@ ngx_http_gzip_filter_memory(ngx_http_req
 
         ctx->allocated = 8192 + 16 + (1 << (wbits + 2))
                          + 131072 + (1 << (memlevel + 8))
+                         + (1 << (memlevel + 6))
                          + 4 * (64 + sizeof(void*));
         ctx->zlib_ng = 1;
     }
@@ -959,8 +972,11 @@ ngx_http_gzip_filter_alloc(void *opaque,
                       "gzip filter failed to use preallocated memory: "
                       "%ud of %ui", items * size, ctx->allocated);
 
+    } else if (ctx->zlib_lit_mem) {
+        ngx_http_gzip_assume_zlib_ng = 1;
+
     } else {
-        ngx_http_gzip_assume_zlib_ng = 1;
+        ngx_http_gzip_assume_zlib_lit_mem = 1;
     }
 
     p = ngx_palloc(ctx->request->pool, items * size);



More information about the nginx-devel mailing list