Debian Codegraph @

debian/rust-vendor/zlib-rs/src/deflate/algorithm/medium.rs

raw
1 #![forbid(unsafe_code)]
2
3 use super::flush_block;
4 use crate::deflate::hash_calc::StandardHashCalc;
5 use crate::{
6 deflate::{
7 fill_window, BlockState, DeflateStream, State, MIN_LOOKAHEAD, STD_MIN_MATCH, WANT_MIN_MATCH,
8 },
9 DeflateFlush,
10 };
11
12 pub fn deflate_medium(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
13 let mut state = &mut stream.state;
14
15 // For levels below 5, don't check the next position for a better match
16 let early_exit = state.level < 5;
17
18 let mut current_match = Match {
19 match_start: 0,
20 match_length: 0,
21 strstart: 0,
22 orgstart: 0,
23 };
24 let mut next_match = Match {
25 match_start: 0,
26 match_length: 0,
27 strstart: 0,
28 orgstart: 0,
29 };
30
31 loop {
32 let mut hash_head;
33
34 /* Make sure that we always have enough lookahead, except
35 * at the end of the input file. We need STD_MAX_MATCH bytes
36 * for the next match, plus WANT_MIN_MATCH bytes to insert the
37 * string following the next match.
38 */
39 if stream.state.lookahead < MIN_LOOKAHEAD {
40 fill_window(stream);
41
42 if stream.state.lookahead < MIN_LOOKAHEAD && flush == DeflateFlush::NoFlush {
43 return BlockState::NeedMore;
44 }
45
46 if stream.state.lookahead == 0 {
47 break; /* flush the current block */
48 }
49
50 next_match.match_length = 0;
51 }
52
53 state = &mut stream.state;
54
55 // Insert the string window[strstart .. strstart+2] in the
56 // dictionary, and set hash_head to the head of the hash chain:
57
58 /* If we already have a future match from a previous round, just use that */
59 if !early_exit && next_match.match_length > 0 {
60 current_match = next_match;
61 next_match.match_length = 0;
62 } else {
63 hash_head = 0;
64 if state.lookahead >= WANT_MIN_MATCH {
65 hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
66 }
67
68 current_match.strstart = state.strstart as u16;
69 current_match.orgstart = current_match.strstart;
70
71 /* Find the longest match, discarding those <= prev_length.
72 * At this point we have always match_length < WANT_MIN_MATCH
73 */
74
75 let dist = state.strstart as i64 - hash_head as i64;
76 if dist <= state.max_dist() as i64 && dist > 0 && hash_head != 0 {
77 /* To simplify the code, we prevent matches with the string
78 * of window index 0 (in particular we have to avoid a match
79 * of the string with itself at the start of the input file).
80 */
81 let (match_length, match_start) =
82 crate::deflate::longest_match::longest_match(state, hash_head);
83 state.match_start = match_start;
84 current_match.match_length = match_length as u16;
85 current_match.match_start = match_start;
86 if (current_match.match_length as usize) < WANT_MIN_MATCH {
87 current_match.match_length = 1;
88 }
89 if current_match.match_start >= current_match.strstart {
90 /* this can happen due to some restarts */
91 current_match.match_length = 1;
92 }
93 } else {
94 /* Set up the match to be a 1 byte literal */
95 current_match.match_start = 0;
96 current_match.match_length = 1;
97 }
98 }
99
100 insert_match(state, current_match);
101
102 /* now, look ahead one */
103 if !early_exit
104 && state.lookahead > MIN_LOOKAHEAD
105 && ((current_match.strstart + current_match.match_length) as usize)
106 < (state.window_size - MIN_LOOKAHEAD)
107 {
108 state.strstart = (current_match.strstart + current_match.match_length) as usize;
109 hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
110
111 next_match.strstart = state.strstart as u16;
112 next_match.orgstart = next_match.strstart;
113
114 /* Find the longest match, discarding those <= prev_length.
115 * At this point we have always match_length < WANT_MIN_MATCH
116 */
117
118 let dist = state.strstart as i64 - hash_head as i64;
119 if dist <= state.max_dist() as i64 && dist > 0 && hash_head != 0 {
120 /* To simplify the code, we prevent matches with the string
121 * of window index 0 (in particular we have to avoid a match
122 * of the string with itself at the start of the input file).
123 */
124 let (match_length, match_start) =
125 crate::deflate::longest_match::longest_match(state, hash_head);
126 state.match_start = match_start;
127 next_match.match_length = match_length as u16;
128 next_match.match_start = match_start;
129
130 if next_match.match_start >= next_match.strstart {
131 /* this can happen due to some restarts */
132 next_match.match_length = 1;
133 }
134 if (next_match.match_length as usize) < WANT_MIN_MATCH {
135 next_match.match_length = 1;
136 } else {
137 fizzle_matches(
138 state.window.filled(),
139 state.max_dist(),
140 &mut current_match,
141 &mut next_match,
142 );
143 }
144 } else {
145 /* Set up the match to be a 1 byte literal */
146 next_match.match_start = 0;
147 next_match.match_length = 1;
148 }
149
150 state.strstart = current_match.strstart as usize;
151 } else {
152 next_match.match_length = 0;
153 }
154
155 /* now emit the current match */
156 let bflush = emit_match(state, current_match);
157
158 /* move the "cursor" forward */
159 state.strstart += current_match.match_length as usize;
160
161 if bflush {
162 flush_block!(stream, false);
163 }
164 }
165
166 stream.state.insert = Ord::min(stream.state.strstart, STD_MIN_MATCH - 1);
167
168 if flush == DeflateFlush::Finish {
169 flush_block!(stream, true);
170 return BlockState::FinishDone;
171 }
172
173 if !stream.state.sym_buf.is_empty() {
174 flush_block!(stream, false);
175 }
176
177 BlockState::BlockDone
178 }
179
180 #[repr(C)]
181 #[derive(Debug, Clone, Copy)]
182 struct Match {
183 match_start: u16,
184 match_length: u16,
185 strstart: u16,
186 orgstart: u16,
187 }
188
189 fn emit_match(state: &mut State, m: Match) -> bool {
190 let mut bflush = false;
191
192 /* matches that are not long enough we need to emit as literals */
193 if (m.match_length as usize) < WANT_MIN_MATCH {
194 for lc in &state.window.filled()[state.strstart..][..m.match_length as usize] {
195 bflush |= State::tally_lit_help(&mut state.sym_buf, &mut state.l_desc, *lc);
196 }
197 } else {
198 // check_match(s, m.strstart, m.match_start, m.match_length);
199
200 bflush |= state.tally_dist(
201 (m.strstart - m.match_start) as usize,
202 m.match_length as usize - STD_MIN_MATCH,
203 );
204 }
205 state.lookahead -= m.match_length as usize;
206
207 bflush
208 }
209
210 #[inline(always)]
211 fn insert_match(state: &mut State, mut m: Match) {
212 if state.lookahead <= (m.match_length as usize + WANT_MIN_MATCH) {
213 return;
214 }
215
216 /* matches that are not long enough we need to emit as literals */
217 if (m.match_length as usize) < WANT_MIN_MATCH {
218 m.strstart += 1;
219 m.match_length -= 1;
220 if m.match_length > 0 && m.strstart >= m.orgstart {
221 if m.strstart + m.match_length > m.orgstart {
222 state.insert_string(m.strstart as usize, m.match_length as usize);
223 } else {
224 state.insert_string(m.strstart as usize, (m.orgstart - m.strstart + 1) as usize);
225 }
226 }
227 return;
228 }
229
230 // Insert new strings in the hash table only if the match length
231 // is not too large. This saves time but degrades compression.
232 if usize::from(m.match_length) <= 16 * state.max_insert_length()
233 && state.lookahead >= WANT_MIN_MATCH
234 {
235 m.match_length -= 1; /* string at strstart already in table */
236 m.strstart += 1;
237
238 if m.strstart >= m.orgstart {
239 if m.strstart + m.match_length > m.orgstart {
240 state.insert_string(m.strstart as usize, m.match_length as usize);
241 } else {
242 state.insert_string(m.strstart as usize, (m.orgstart - m.strstart + 1) as usize);
243 }
244 } else if m.orgstart < m.strstart + m.match_length {
245 state.insert_string(
246 m.orgstart as usize,
247 (m.strstart + m.match_length - m.orgstart) as usize,
248 );
249 }
250 } else {
251 m.strstart += m.match_length;
252 m.match_length = 0;
253
254 if (m.strstart as usize) >= (STD_MIN_MATCH - 2) {
255 StandardHashCalc::quick_insert_string(state, m.strstart as usize + 2 - STD_MIN_MATCH);
256 }
257
258 /* If lookahead < WANT_MIN_MATCH, ins_h is garbage, but it does not
259 * matter since it will be recomputed at next deflate call.
260 */
261 }
262 }
263
264 fn fizzle_matches(window: &[u8], max_dist: usize, current: &mut Match, next: &mut Match) {
265 /* step zero: sanity checks */
266
267 if current.match_length <= 1 {
268 return;
269 }
270
271 if current.match_length > 1 + next.match_start {
272 return;
273 }
274
275 if current.match_length > 1 + next.strstart {
276 return;
277 }
278
279 let m = &window[(-(current.match_length as isize) + 1 + next.match_start as isize) as usize..];
280 let orig = &window[(-(current.match_length as isize) + 1 + next.strstart as isize) as usize..];
281
282 /* quick exit check.. if this fails then don't bother with anything else */
283 if m[0] != orig[0] {
284 return;
285 }
286
287 /* step one: try to move the "next" match to the left as much as possible */
288 let limit = next.strstart.saturating_sub(max_dist as u16);
289
290 let mut c = *current;
291 let mut n = *next;
292
293 let m = &window[..n.match_start as usize];
294 let orig = &window[..n.strstart as usize];
295
296 let mut m = m.iter().rev();
297 let mut orig = orig.iter().rev();
298
299 let mut changed = 0;
300
301 while m.next() == orig.next() {
302 if c.match_length < 1 {
303 break;
304 }
305 if n.strstart <= limit {
306 break;
307 }
308 if n.match_length >= 256 {
309 break;
310 }
311 if n.match_start <= 1 {
312 break;
313 }
314
315 n.strstart -= 1;
316 n.match_start -= 1;
317 n.match_length += 1;
318 c.match_length -= 1;
319 changed += 1;
320 }
321
322 if changed == 0 {
323 return;
324 }
325
326 if c.match_length <= 1 && n.match_length != 2 {
327 n.orgstart += 1;
328 *current = c;
329 *next = n;
330 }
331 }