1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#![forbid(unsafe_code)]
use super::{flush_block, h3_insert_run, h3_probe};
use crate::deflate::hash_calc::StandardHashCalc;
use crate::{
deflate::{
fill_window, BlockState, DeflateStream, Strategy, MIN_LOOKAHEAD, STD_MIN_MATCH,
WANT_MIN_MATCH,
},
DeflateFlush,
};
pub fn deflate_fast(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
// House delta: dense levels also take 3-byte matches (see `h3_probe`). The choice is a const
// parameter, so the zlib-ng table's instantiation compiles to the upstream loop with no
// 3-byte-table code in it at all.
if stream.state.config().hash3 && stream.state.strategy == Strategy::Default {
stream.state.init_h3();
deflate_fast_impl::<true>(stream, flush)
} else {
deflate_fast_impl::<false>(stream, flush)
}
}
#[inline(always)]
fn deflate_fast_impl<const H3: bool>(
stream: &mut DeflateStream,
flush: DeflateFlush,
) -> BlockState {
let use_h3 = H3;
loop {
// Make sure that we always have enough lookahead, except
// at the end of the input file. We need STD_MAX_MATCH bytes
// for the next match, plus WANT_MIN_MATCH bytes to insert the
// string following the next match.
if stream.state.lookahead < MIN_LOOKAHEAD {
fill_window(stream);
if stream.state.lookahead < MIN_LOOKAHEAD && flush == DeflateFlush::NoFlush {
return BlockState::NeedMore;
}
if stream.state.lookahead == 0 {
break; /* flush the current block */
}
}
let state = &mut stream.state;
// Insert the string window[strstart .. strstart+2] in the
// dictionary, and set hash_head to the head of the hash chain:
// Literal character to output if there is no match.
let lc = if state.lookahead >= WANT_MIN_MATCH {
let val = u32::from_le_bytes(
state.window.filled()[state.strstart..state.strstart + 4]
.try_into()
.unwrap(),
);
let hash_head = StandardHashCalc::quick_insert_value(state, state.strstart, val);
let dist = state.strstart as isize - hash_head as isize;
// Find the longest match for the string starting at offset state.strstart.
let mut match_len = 0;
if dist <= state.max_dist() as isize && dist > 0 && hash_head != 0 {
// To simplify the code, we prevent matches with the string
// of window index 0 (in particular we have to avoid a match
// of the string with itself at the start of the input file).
(match_len, state.match_start) =
crate::deflate::longest_match::longest_match(state, hash_head);
}
let min_len = if use_h3 {
match h3_probe(state) {
Some(cand) if match_len < WANT_MIN_MATCH => {
match_len = STD_MIN_MATCH;
state.match_start = cand as u16;
STD_MIN_MATCH
}
_ => WANT_MIN_MATCH,
}
} else {
WANT_MIN_MATCH
};
{
if match_len >= min_len {
let bflush = state.tally_dist(
state.strstart - state.match_start as usize,
match_len - STD_MIN_MATCH,
);
state.lookahead -= match_len;
/* Insert new strings in the hash table only if the match length
* is not too large. This saves time but degrades compression.
*/
if match_len <= state.max_insert_length() && state.lookahead >= WANT_MIN_MATCH {
match_len -= 1; /* string at strstart already in table */
state.strstart += 1;
state.insert_string(state.strstart, match_len);
if use_h3 {
h3_insert_run(state, state.strstart, match_len);
}
state.strstart += match_len;
} else {
state.strstart += match_len;
StandardHashCalc::quick_insert_string(
state,
state.strstart + 2 - STD_MIN_MATCH,
);
/* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
* matter since it will be recomputed at next deflate call.
*/
}
if bflush {
flush_block!(stream, false);
}
continue;
}
}
val as u8
} else {
state.window.filled()[state.strstart]
};
/* No match, output a literal byte */
let bflush = state.tally_lit(lc);
state.lookahead -= 1;
state.strstart += 1;
if bflush {
flush_block!(stream, false);
}
}
stream.state.insert = if stream.state.strstart < (STD_MIN_MATCH - 1) {
stream.state.strstart
} else {
STD_MIN_MATCH - 1
};
if flush == DeflateFlush::Finish {
flush_block!(stream, true);
return BlockState::FinishDone;
}
if !stream.state.sym_buf.is_empty() {
flush_block!(stream, false);
}
BlockState::BlockDone
}