• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* deflate_slow.c -- compress data using the slow strategy of deflation algorithm
2  *
3  * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
4  * For conditions of distribution and use, see copyright notice in zlib.h
5  */
6 
7 #include "zbuild.h"
8 #include "deflate.h"
9 #include "deflate_p.h"
10 #include "functable.h"
11 
12 /* ===========================================================================
13  * Same as deflate_medium, but achieves better compression. We use a lazy
14  * evaluation for matches: a match is finally adopted only if there is
15  * no better match at the next window position.
16  */
deflate_slow(deflate_state * s,int flush)17 Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
18     Pos hash_head;           /* head of hash chain */
19     int bflush;              /* set if current block must be flushed */
20     uint32_t match_len;
21 
22     /* Process the input block. */
23     for (;;) {
24         /* Make sure that we always have enough lookahead, except
25          * at the end of the input file. We need MAX_MATCH bytes
26          * for the next match, plus MIN_MATCH bytes to insert the
27          * string following the next match.
28          */
29         if (s->lookahead < MIN_LOOKAHEAD) {
30             fill_window(s);
31             if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
32                 return need_more;
33             }
34             if (UNLIKELY(s->lookahead == 0))
35                 break; /* flush the current block */
36         }
37 
38         /* Insert the string window[strstart .. strstart+2] in the
39          * dictionary, and set hash_head to the head of the hash chain:
40          */
41         hash_head = NIL;
42         if (LIKELY(s->lookahead >= MIN_MATCH)) {
43             hash_head = functable.quick_insert_string(s, s->strstart);
44         }
45 
46         /* Find the longest match, discarding those <= prev_length.
47          */
48         s->prev_match = (Pos)s->match_start;
49         match_len = MIN_MATCH-1;
50 
51         if (hash_head != NIL && s->prev_length < s->max_lazy_match && s->strstart - hash_head <= MAX_DIST(s)) {
52             /* To simplify the code, we prevent matches with the string
53              * of window index 0 (in particular we have to avoid a match
54              * of the string with itself at the start of the input file).
55              */
56             match_len = functable.longest_match(s, hash_head);
57             /* longest_match() sets match_start */
58 
59             if (match_len <= 5 && (s->strategy == Z_FILTERED)) {
60                 /* If prev_match is also MIN_MATCH, match_start is garbage
61                  * but we will ignore the current match anyway.
62                  */
63                 match_len = MIN_MATCH-1;
64             }
65         }
66         /* If there was a match at the previous step and the current
67          * match is not better, output the previous match:
68          */
69         if (s->prev_length >= MIN_MATCH && match_len <= s->prev_length) {
70             unsigned int max_insert = s->strstart + s->lookahead - MIN_MATCH;
71             /* Do not insert strings in hash table beyond this. */
72 
73             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
74 
75             bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - MIN_MATCH);
76 
77             /* Insert in hash table all strings up to the end of the match.
78              * strstart-1 and strstart are already inserted. If there is not
79              * enough lookahead, the last two strings are not inserted in
80              * the hash table.
81              */
82             s->lookahead -= s->prev_length-1;
83 
84             unsigned int mov_fwd = s->prev_length - 2;
85             if (max_insert > s->strstart) {
86                 unsigned int insert_cnt = mov_fwd;
87                 if (UNLIKELY(insert_cnt > max_insert - s->strstart))
88                     insert_cnt = max_insert - s->strstart;
89 
90                 functable.insert_string(s, s->strstart + 1, insert_cnt);
91             }
92             s->prev_length = 0;
93             s->match_available = 0;
94             s->strstart += mov_fwd + 1;
95 
96             if (UNLIKELY(bflush))
97                 FLUSH_BLOCK(s, 0);
98 
99         } else if (s->match_available) {
100             /* If there was no match at the previous position, output a
101              * single literal. If there was a match but the current match
102              * is longer, truncate the previous match to a single literal.
103              */
104             bflush = zng_tr_tally_lit(s, s->window[s->strstart-1]);
105             if (UNLIKELY(bflush))
106                 FLUSH_BLOCK_ONLY(s, 0);
107             s->prev_length = match_len;
108             s->strstart++;
109             s->lookahead--;
110             if (UNLIKELY(s->strm->avail_out == 0))
111                 return need_more;
112         } else {
113             /* There is no previous match to compare with, wait for
114              * the next step to decide.
115              */
116             s->prev_length = match_len;
117             s->match_available = 1;
118             s->strstart++;
119             s->lookahead--;
120         }
121     }
122     Assert(flush != Z_NO_FLUSH, "no flush?");
123     if (UNLIKELY(s->match_available)) {
124         (void) zng_tr_tally_lit(s, s->window[s->strstart-1]);
125         s->match_available = 0;
126     }
127     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
128     if (UNLIKELY(flush == Z_FINISH)) {
129         FLUSH_BLOCK(s, 1);
130         return finish_done;
131     }
132     if (UNLIKELY(s->sym_next))
133         FLUSH_BLOCK(s, 0);
134     return block_done;
135 }
136