Debian Codegraph @

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

raw
1 #![forbid(unsafe_code)]
2
3 use super::flush_block;
4 use crate::{
5 deflate::{
6 fill_window, flush_block_only, BlockState, DeflateStream, Strategy, MIN_LOOKAHEAD,
7 STD_MIN_MATCH, WANT_MIN_MATCH,
8 },
9 DeflateFlush,
10 };
11
12 pub fn deflate_slow(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
13 let mut hash_head; /* head of hash chain */
14 let mut bflush; /* set if current block must be flushed */
15 let mut dist;
16 let mut match_len;
17
18 let use_longest_match_slow = stream.state.max_chain_length > 1024;
19 let valid_distance_range = 1..=stream.state.max_dist() as isize;
20
21 let mut match_available = stream.state.match_available;
22
23 /* Process the input block. */
24 loop {
25 /* Make sure that we always have enough lookahead, except
26 * at the end of the input file. We need STD_MAX_MATCH bytes
27 * for the next match, plus WANT_MIN_MATCH bytes to insert the
28 * string following the next match.
29 */
30 if stream.state.lookahead < MIN_LOOKAHEAD {
31 fill_window(stream);
32 if stream.state.lookahead < MIN_LOOKAHEAD && flush == DeflateFlush::NoFlush {
33 return BlockState::NeedMore;
34 }
35
36 if stream.state.lookahead == 0 {
37 break; /* flush the current block */
38 }
39 }
40
41 let state = &mut stream.state;
42
43 /* Insert the string window[strstart .. strstart+2] in the
44 * dictionary, and set hash_head to the head of the hash chain:
45 */
46 hash_head = if state.lookahead >= WANT_MIN_MATCH {
47 state.quick_insert_string(state.strstart)
48 } else {
49 0
50 };
51
52 // Find the longest match, discarding those <= prev_length.
53 state.prev_match = state.match_start;
54 match_len = STD_MIN_MATCH - 1;
55 dist = state.strstart as isize - hash_head as isize;
56
57 if valid_distance_range.contains(&dist)
58 && state.prev_length < state.max_lazy_match
59 && hash_head != 0
60 {
61 // To simplify the code, we prevent matches with the string
62 // of window index 0 (in particular we have to avoid a match
63 // of the string with itself at the start of the input file).
64 (match_len, state.match_start) = if use_longest_match_slow {
65 crate::deflate::longest_match::longest_match_slow(state, hash_head)
66 } else {
67 crate::deflate::longest_match::longest_match(state, hash_head)
68 };
69
70 if match_len <= 5 && (state.strategy == Strategy::Filtered) {
71 /* If prev_match is also WANT_MIN_MATCH, match_start is garbage
72 * but we will ignore the current match anyway.
73 */
74 match_len = STD_MIN_MATCH - 1;
75 }
76 }
77
78 // If there was a match at the previous step and the current
79 // match is not better, output the previous match:
80 if state.prev_length as usize >= STD_MIN_MATCH && match_len <= state.prev_length as usize {
81 let max_insert = state.strstart + state.lookahead - STD_MIN_MATCH;
82 /* Do not insert strings in hash table beyond this. */
83
84 // check_match(s, state.strstart-1, state.prev_match, state.prev_length);
85
86 bflush = state.tally_dist(
87 state.strstart - 1 - state.prev_match as usize,
88 state.prev_length as usize - STD_MIN_MATCH,
89 );
90
91 /* Insert in hash table all strings up to the end of the match.
92 * strstart-1 and strstart are already inserted. If there is not
93 * enough lookahead, the last two strings are not inserted in
94 * the hash table.
95 */
96 state.prev_length -= 1;
97 state.lookahead -= state.prev_length as usize;
98
99 let mov_fwd = state.prev_length as usize - 1;
100 if max_insert > state.strstart {
101 let insert_cnt = Ord::min(mov_fwd, max_insert - state.strstart);
102 state.insert_string(state.strstart + 1, insert_cnt);
103 }
104 state.prev_length = 0;
105 state.match_available = false;
106 match_available = false;
107 state.strstart += mov_fwd + 1;
108
109 if bflush {
110 flush_block!(stream, false);
111 }
112 } else if match_available {
113 // If there was no match at the previous position, output a
114 // single literal. If there was a match but the current match
115 // is longer, truncate the previous match to a single literal.
116 let lc = state.window.filled()[state.strstart - 1];
117 bflush = state.tally_lit(lc);
118 if bflush {
119 flush_block_only(stream, false);
120 }
121
122 stream.state.prev_length = match_len as u16;
123 stream.state.strstart += 1;
124 stream.state.lookahead -= 1;
125 if stream.avail_out == 0 {
126 return BlockState::NeedMore;
127 }
128 } else {
129 // There is no previous match to compare with, wait for
130 // the next step to decide.
131 state.prev_length = match_len as u16;
132 state.match_available = true;
133 match_available = true;
134 state.strstart += 1;
135 state.lookahead -= 1;
136 }
137 }
138
139 assert_ne!(flush, DeflateFlush::NoFlush, "no flush?");
140
141 let state = &mut stream.state;
142
143 if state.match_available {
144 let lc = state.window.filled()[state.strstart - 1];
145 let _ = state.tally_lit(lc);
146 state.match_available = false;
147 }
148
149 state.insert = Ord::min(state.strstart, STD_MIN_MATCH - 1);
150
151 if flush == DeflateFlush::Finish {
152 flush_block!(stream, true);
153 return BlockState::FinishDone;
154 }
155
156 if !stream.state.sym_buf.is_empty() {
157 flush_block!(stream, false);
158 }
159
160 BlockState::BlockDone
161 }