Debian Codegraph @

third_party/rust/zlib-rs/src/deflate/algorithm/slow.rs

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