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