Debian Codegraph @

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

raw
1 #![forbid(unsafe_code)]
2
3 use crate::{
4 deflate::{
5 fill_window, BlockState, DeflateStream, MIN_LOOKAHEAD, STD_MIN_MATCH, WANT_MIN_MATCH,
6 },
7 flush_block, DeflateFlush,
8 };
9
10 pub fn deflate_fast(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
11 let mut bflush; /* set if current block must be flushed */
12 let mut dist;
13 let mut match_len = 0;
14
15 loop {
16 // Make sure that we always have enough lookahead, except
17 // at the end of the input file. We need STD_MAX_MATCH bytes
18 // for the next match, plus WANT_MIN_MATCH bytes to insert the
19 // string following the next match.
20 if stream.state.lookahead < MIN_LOOKAHEAD {
21 fill_window(stream);
22 if stream.state.lookahead < MIN_LOOKAHEAD && flush == DeflateFlush::NoFlush {
23 return BlockState::NeedMore;
24 }
25 if stream.state.lookahead == 0 {
26 break; /* flush the current block */
27 }
28 }
29
30 let state = &mut stream.state;
31
32 // Insert the string window[strstart .. strstart+2] in the
33 // dictionary, and set hash_head to the head of the hash chain:
34
35 if state.lookahead >= WANT_MIN_MATCH {
36 let hash_head = state.quick_insert_string(state.strstart);
37 dist = state.strstart as isize - hash_head as isize;
38
39 /* Find the longest match, discarding those <= prev_length.
40 * At this point we have always match length < WANT_MIN_MATCH
41 */
42 if dist <= state.max_dist() as isize && dist > 0 && hash_head != 0 {
43 // To simplify the code, we prevent matches with the string
44 // of window index 0 (in particular we have to avoid a match
45 // of the string with itself at the start of the input file).
46 (match_len, state.match_start) =
47 crate::deflate::longest_match::longest_match(state, hash_head);
48 }
49 }
50
51 if match_len >= WANT_MIN_MATCH {
52 // check_match(s, s->strstart, s->match_start, match_len);
53
54 // bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
55 bflush = state.tally_dist(
56 state.strstart - state.match_start as usize,
57 match_len - STD_MIN_MATCH,
58 );
59
60 state.lookahead -= match_len;
61
62 /* Insert new strings in the hash table only if the match length
63 * is not too large. This saves time but degrades compression.
64 */
65 if match_len <= state.max_insert_length() && state.lookahead >= WANT_MIN_MATCH {
66 match_len -= 1; /* string at strstart already in table */
67 state.strstart += 1;
68
69 state.insert_string(state.strstart, match_len);
70 state.strstart += match_len;
71 } else {
72 state.strstart += match_len;
73 state.quick_insert_string(state.strstart + 2 - STD_MIN_MATCH);
74
75 /* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
76 * matter since it will be recomputed at next deflate call.
77 */
78 }
79 match_len = 0;
80 } else {
81 /* No match, output a literal byte */
82 let lc = state.window.filled()[state.strstart];
83 bflush = state.tally_lit(lc);
84 state.lookahead -= 1;
85 state.strstart += 1;
86 }
87
88 if bflush {
89 flush_block!(stream, false);
90 }
91 }
92
93 stream.state.insert = if stream.state.strstart < (STD_MIN_MATCH - 1) {
94 stream.state.strstart
95 } else {
96 STD_MIN_MATCH - 1
97 };
98
99 if flush == DeflateFlush::Finish {
100 flush_block!(stream, true);
101 return BlockState::FinishDone;
102 }
103
104 if !stream.state.sym_buf.is_empty() {
105 flush_block!(stream, false);
106 }
107
108 BlockState::BlockDone
109 }