8cf299
From 2d80d3f6b52f9fa454c26c89d2d6a1790e1cecb0 Mon Sep 17 00:00:00 2001
8cf299
From: Mark Adler <madler@alumni.caltech.edu>
8cf299
Date: Sat, 21 Jan 2017 01:50:26 -0800
8cf299
Subject: [PATCH] Limit hash table inserts after switch from stored deflate.
8cf299
8cf299
This limits hash table inserts to the available data in the window
8cf299
and to the sliding window size in deflate_stored(). The hash table
8cf299
inserts are deferred until deflateParams() switches to a non-zero
8cf299
compression level.
8cf299
---
8cf299
 deflate.c | 10 +++++++++-
8cf299
 1 file changed, 9 insertions(+), 1 deletion(-)
8cf299
8cf299
diff --git a/deflate.c b/deflate.c
8cf299
index 20bda4f..d368b25 100644
8cf299
--- a/deflate.c
8cf299
+++ b/deflate.c
8cf299
@@ -1513,6 +1513,8 @@ local void fill_window(s)
8cf299
             s->match_start -= wsize;
8cf299
             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
8cf299
             s->block_start -= (long) wsize;
8cf299
+            if (s->insert > s->strstart)
8cf299
+                s->insert = s->strstart;
8cf299
             slide_hash(s);
8cf299
             more += wsize;
8cf299
         }
8cf299
@@ -1742,6 +1744,7 @@ local block_state deflate_stored(s, flush)
8cf299
             s->matches = 2;         /* clear hash */
8cf299
             zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
8cf299
             s->strstart = s->w_size;
8cf299
+            s->insert = s->strstart;
8cf299
         }
8cf299
         else {
8cf299
             if (s->window_size - s->strstart <= used) {
8cf299
@@ -1750,12 +1753,14 @@ local block_state deflate_stored(s, flush)
8cf299
                 zmemcpy(s->window, s->window + s->w_size, s->strstart);
8cf299
                 if (s->matches < 2)
8cf299
                     s->matches++;   /* add a pending slide_hash() */
8cf299
+                if (s->insert > s->strstart)
8cf299
+                    s->insert = s->strstart;
8cf299
             }
8cf299
             zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
8cf299
             s->strstart += used;
8cf299
+            s->insert += MIN(used, s->w_size - s->insert);
8cf299
         }
8cf299
         s->block_start = s->strstart;
8cf299
-        s->insert += MIN(used, s->w_size - s->insert);
8cf299
     }
8cf299
     if (s->high_water < s->strstart)
8cf299
         s->high_water = s->strstart;
8cf299
@@ -1779,12 +1784,15 @@ local block_state deflate_stored(s, flush)
8cf299
         if (s->matches < 2)
8cf299
             s->matches++;           /* add a pending slide_hash() */
8cf299
         have += s->w_size;          /* more space now */
8cf299
+        if (s->insert > s->strstart)
8cf299
+            s->insert = s->strstart;
8cf299
     }
8cf299
     if (have > s->strm->avail_in)
8cf299
         have = s->strm->avail_in;
8cf299
     if (have) {
8cf299
         read_buf(s->strm, s->window + s->strstart, have);
8cf299
         s->strstart += have;
8cf299
+        s->insert += MIN(have, s->w_size - s->insert);
8cf299
     }
8cf299
     if (s->high_water < s->strstart)
8cf299
         s->high_water = s->strstart;
8cf299
-- 
8cf299
2.39.1
8cf299