/src/zlib-ng/deflate_slow.c
Line | Count | Source |
1 | | /* deflate_slow.c -- compress data using the slow strategy of deflation algorithm |
2 | | * |
3 | | * Copyright (C) 1995-2024 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 "insert_string_p.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 | | */ |
17 | 6.26k | Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) { |
18 | 6.26k | longest_match_func longest_match = s->longest_match; |
19 | 6.26k | insert_batch_func insert_batch = s->insert_batch; |
20 | 6.26k | unsigned char *window = s->window; |
21 | 6.26k | int bflush; /* set if current block must be flushed */ |
22 | 6.26k | int level = s->level; |
23 | | |
24 | | /* Process the input block. */ |
25 | 3.42M | for (;;) { |
26 | | /* Make sure that we always have enough lookahead, except |
27 | | * at the end of the input file. We need STD_MAX_MATCH bytes |
28 | | * for the next match, plus WANT_MIN_MATCH bytes to insert the |
29 | | * string following the next match. |
30 | | */ |
31 | 3.42M | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
32 | 28.2k | PREFIX(fill_window)(s); |
33 | 28.2k | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
34 | 3.13k | return need_more; |
35 | 3.13k | } |
36 | 25.0k | if (UNLIKELY(s->lookahead == 0)) |
37 | 3.13k | break; /* flush the current block */ |
38 | 25.0k | } |
39 | | |
40 | | /* Insert the string window[strstart .. strstart+2] in the |
41 | | * dictionary, and set hash_head to the head of the hash chain: |
42 | | */ |
43 | 3.41M | uint32_t hash_head = 0; |
44 | 3.41M | if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
45 | 3.41M | if (level >= MIN_ROLL_LEVEL) |
46 | 3.41M | hash_head = insert_roll(s, window, s->strstart); |
47 | 0 | else |
48 | 0 | hash_head = insert_knuth(s, window, s->strstart); |
49 | 3.41M | } |
50 | | |
51 | | /* Find the longest match, discarding those <= prev_length. |
52 | | */ |
53 | 3.41M | s->prev_match = s->match_start; |
54 | 3.41M | uint32_t match_len = STD_MIN_MATCH - 1; |
55 | 3.41M | int64_t dist = (int64_t)s->strstart - hash_head; |
56 | | |
57 | 3.41M | if (dist <= MAX_DIST(s) && dist > 0 && s->prev_length < s->max_lazy_match && hash_head != 0) { |
58 | | /* To simplify the code, we prevent matches with the string |
59 | | * of window index 0 (in particular we have to avoid a match |
60 | | * of the string with itself at the start of the input file). |
61 | | */ |
62 | 1.71M | match_len = longest_match(s, hash_head); |
63 | | /* longest_match() sets match_start */ |
64 | | |
65 | 1.71M | if (match_len <= 5 && (s->strategy == Z_FILTERED)) { |
66 | | /* If prev_match is also WANT_MIN_MATCH, match_start is garbage |
67 | | * but we will ignore the current match anyway. |
68 | | */ |
69 | 134 | match_len = STD_MIN_MATCH - 1; |
70 | 134 | } |
71 | 1.71M | } |
72 | | /* If there was a match at the previous step and the current |
73 | | * match is not better, output the previous match: |
74 | | */ |
75 | 3.41M | if (s->prev_length >= STD_MIN_MATCH && match_len <= s->prev_length) { |
76 | 1.70M | unsigned int max_insert = s->strstart + s->lookahead - STD_MIN_MATCH; |
77 | | /* Do not insert strings in hash table beyond this. */ |
78 | | |
79 | 1.70M | Assert((s->strstart-1) <= UINT16_MAX, "strstart-1 should fit in uint16_t"); |
80 | 1.70M | check_match(s, s->strstart - 1, s->prev_match, s->prev_length); |
81 | | |
82 | 1.70M | bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - STD_MIN_MATCH); |
83 | | |
84 | | /* Insert in hash table all strings up to the end of the match. |
85 | | * strstart-1 and strstart are already inserted. If there is not |
86 | | * enough lookahead, the last two strings are not inserted in |
87 | | * the hash table. |
88 | | */ |
89 | 1.70M | s->prev_length -= 1; |
90 | 1.70M | s->lookahead -= s->prev_length; |
91 | | |
92 | 1.70M | unsigned int mov_fwd = s->prev_length - 1; |
93 | 1.70M | if (max_insert > s->strstart) { |
94 | 1.70M | unsigned int insert_cnt = mov_fwd; |
95 | 1.70M | if (UNLIKELY(insert_cnt > max_insert - s->strstart)) |
96 | 2.86k | insert_cnt = max_insert - s->strstart; |
97 | 1.70M | insert_batch(s, window, s->strstart + 1, insert_cnt); |
98 | 1.70M | } |
99 | 1.70M | s->prev_length = 0; |
100 | 1.70M | s->match_available = 0; |
101 | 1.70M | s->strstart += mov_fwd + 1; |
102 | | |
103 | 1.70M | if (UNLIKELY(bflush)) |
104 | 1.70M | FLUSH_BLOCK(s, window, 0); |
105 | | |
106 | 1.71M | } else if (s->match_available) { |
107 | | /* If there was no match at the previous position, output a |
108 | | * single literal. If there was a match but the current match |
109 | | * is longer, truncate the previous match to a single literal. |
110 | | */ |
111 | 3.66k | bflush = zng_tr_tally_lit(s, window[s->strstart-1]); |
112 | 3.66k | if (UNLIKELY(bflush)) |
113 | 0 | FLUSH_BLOCK_ONLY(s, window, 0); |
114 | 3.66k | s->prev_length = match_len; |
115 | 3.66k | s->strstart++; |
116 | 3.66k | s->lookahead--; |
117 | 3.66k | if (UNLIKELY(s->strm->avail_out == 0)) |
118 | 0 | return need_more; |
119 | 1.70M | } else { |
120 | | /* There is no previous match to compare with, wait for |
121 | | * the next step to decide. |
122 | | */ |
123 | 1.70M | s->prev_length = match_len; |
124 | 1.70M | s->match_available = 1; |
125 | 1.70M | s->strstart++; |
126 | 1.70M | s->lookahead--; |
127 | 1.70M | } |
128 | 3.41M | } |
129 | 3.13k | Assert(flush != Z_NO_FLUSH, "no flush?"); |
130 | 3.13k | if (UNLIKELY(s->match_available)) { |
131 | 321 | Z_UNUSED(zng_tr_tally_lit(s, window[s->strstart-1])); |
132 | 321 | s->match_available = 0; |
133 | 321 | } |
134 | 3.13k | s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1); |
135 | 3.13k | if (UNLIKELY(flush == Z_FINISH)) { |
136 | 1.56k | FLUSH_BLOCK(s, window, 1); |
137 | 1.56k | return finish_done; |
138 | 1.56k | } |
139 | 1.56k | if (UNLIKELY(s->sym_next)) |
140 | 1.56k | FLUSH_BLOCK(s, window, 0); |
141 | 1.56k | return block_done; |
142 | 1.56k | } |