| 1 |
#![forbid(unsafe_code)] |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
deflate::{ |
| 5 |
fill_window, BlockState, DeflateStream, State, MIN_LOOKAHEAD, STD_MIN_MATCH, WANT_MIN_MATCH, |
| 6 |
}, |
| 7 |
flush_block, DeflateFlush, |
| 8 |
}; |
| 9 |
|
| 10 |
pub fn deflate_medium(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState { |
| 11 |
let mut state = &mut stream.state; |
| 12 |
|
| 13 |
// For levels below 5, don't check the next position for a better match |
| 14 |
let early_exit = state.level < 5; |
| 15 |
|
| 16 |
let mut current_match = Match { |
| 17 |
match_start: 0, |
| 18 |
match_length: 0, |
| 19 |
strstart: 0, |
| 20 |
orgstart: 0, |
| 21 |
}; |
| 22 |
let mut next_match = Match { |
| 23 |
match_start: 0, |
| 24 |
match_length: 0, |
| 25 |
strstart: 0, |
| 26 |
orgstart: 0, |
| 27 |
}; |
| 28 |
|
| 29 |
loop { |
| 30 |
let mut hash_head; |
| 31 |
|
| 32 |
/* Make sure that we always have enough lookahead, except |
| 33 |
* at the end of the input file. We need STD_MAX_MATCH bytes |
| 34 |
* for the next match, plus WANT_MIN_MATCH bytes to insert the |
| 35 |
* string following the next match. |
| 36 |
*/ |
| 37 |
if stream.state.lookahead < MIN_LOOKAHEAD { |
| 38 |
fill_window(stream); |
| 39 |
|
| 40 |
if stream.state.lookahead < MIN_LOOKAHEAD && flush == DeflateFlush::NoFlush { |
| 41 |
return BlockState::NeedMore; |
| 42 |
} |
| 43 |
|
| 44 |
if stream.state.lookahead == 0 { |
| 45 |
break; /* flush the current block */ |
| 46 |
} |
| 47 |
|
| 48 |
next_match.match_length = 0; |
| 49 |
} |
| 50 |
|
| 51 |
state = &mut stream.state; |
| 52 |
|
| 53 |
// Insert the string window[strstart .. strstart+2] in the |
| 54 |
// dictionary, and set hash_head to the head of the hash chain: |
| 55 |
|
| 56 |
/* If we already have a future match from a previous round, just use that */ |
| 57 |
if !early_exit && next_match.match_length > 0 { |
| 58 |
current_match = next_match; |
| 59 |
next_match.match_length = 0; |
| 60 |
} else { |
| 61 |
hash_head = 0; |
| 62 |
if state.lookahead >= WANT_MIN_MATCH { |
| 63 |
hash_head = state.quick_insert_string(state.strstart); |
| 64 |
} |
| 65 |
|
| 66 |
current_match.strstart = state.strstart as u16; |
| 67 |
current_match.orgstart = current_match.strstart; |
| 68 |
|
| 69 |
/* Find the longest match, discarding those <= prev_length. |
| 70 |
* At this point we have always match_length < WANT_MIN_MATCH |
| 71 |
*/ |
| 72 |
|
| 73 |
let dist = state.strstart as i64 - hash_head as i64; |
| 74 |
if dist <= state.max_dist() as i64 && dist > 0 && hash_head != 0 { |
| 75 |
/* To simplify the code, we prevent matches with the string |
| 76 |
* of window index 0 (in particular we have to avoid a match |
| 77 |
* of the string with itself at the start of the input file). |
| 78 |
*/ |
| 79 |
let (match_length, match_start) = |
| 80 |
crate::deflate::longest_match::longest_match(state, hash_head); |
| 81 |
state.match_start = match_start; |
| 82 |
current_match.match_length = match_length as u16; |
| 83 |
current_match.match_start = match_start; |
| 84 |
if (current_match.match_length as usize) < WANT_MIN_MATCH { |
| 85 |
current_match.match_length = 1; |
| 86 |
} |
| 87 |
if current_match.match_start >= current_match.strstart { |
| 88 |
/* this can happen due to some restarts */ |
| 89 |
current_match.match_length = 1; |
| 90 |
} |
| 91 |
} else { |
| 92 |
/* Set up the match to be a 1 byte literal */ |
| 93 |
current_match.match_start = 0; |
| 94 |
current_match.match_length = 1; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
insert_match(state, current_match); |
| 99 |
|
| 100 |
/* now, look ahead one */ |
| 101 |
if !early_exit |
| 102 |
&& state.lookahead > MIN_LOOKAHEAD |
| 103 |
&& ((current_match.strstart + current_match.match_length) as usize) |
| 104 |
< (state.window_size - MIN_LOOKAHEAD) |
| 105 |
{ |
| 106 |
state.strstart = (current_match.strstart + current_match.match_length) as usize; |
| 107 |
hash_head = state.quick_insert_string(state.strstart); |
| 108 |
|
| 109 |
next_match.strstart = state.strstart as u16; |
| 110 |
next_match.orgstart = next_match.strstart; |
| 111 |
|
| 112 |
/* Find the longest match, discarding those <= prev_length. |
| 113 |
* At this point we have always match_length < WANT_MIN_MATCH |
| 114 |
*/ |
| 115 |
|
| 116 |
let dist = state.strstart as i64 - hash_head as i64; |
| 117 |
if dist <= state.max_dist() as i64 && dist > 0 && hash_head != 0 { |
| 118 |
/* To simplify the code, we prevent matches with the string |
| 119 |
* of window index 0 (in particular we have to avoid a match |
| 120 |
* of the string with itself at the start of the input file). |
| 121 |
*/ |
| 122 |
let (match_length, match_start) = |
| 123 |
crate::deflate::longest_match::longest_match(state, hash_head); |
| 124 |
state.match_start = match_start; |
| 125 |
next_match.match_length = match_length as u16; |
| 126 |
next_match.match_start = match_start; |
| 127 |
|
| 128 |
if next_match.match_start >= next_match.strstart { |
| 129 |
/* this can happen due to some restarts */ |
| 130 |
next_match.match_length = 1; |
| 131 |
} |
| 132 |
if (next_match.match_length as usize) < WANT_MIN_MATCH { |
| 133 |
next_match.match_length = 1; |
| 134 |
} else { |
| 135 |
fizzle_matches( |
| 136 |
state.window.filled(), |
| 137 |
state.max_dist(), |
| 138 |
&mut current_match, |
| 139 |
&mut next_match, |
| 140 |
); |
| 141 |
} |
| 142 |
} else { |
| 143 |
/* Set up the match to be a 1 byte literal */ |
| 144 |
next_match.match_start = 0; |
| 145 |
next_match.match_length = 1; |
| 146 |
} |
| 147 |
|
| 148 |
state.strstart = current_match.strstart as usize; |
| 149 |
} else { |
| 150 |
next_match.match_length = 0; |
| 151 |
} |
| 152 |
|
| 153 |
/* now emit the current match */ |
| 154 |
let bflush = emit_match(state, current_match); |
| 155 |
|
| 156 |
/* move the "cursor" forward */ |
| 157 |
state.strstart += current_match.match_length as usize; |
| 158 |
|
| 159 |
if bflush { |
| 160 |
flush_block!(stream, false); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
stream.state.insert = Ord::min(stream.state.strstart, STD_MIN_MATCH - 1); |
| 165 |
|
| 166 |
if flush == DeflateFlush::Finish { |
| 167 |
flush_block!(stream, true); |
| 168 |
return BlockState::FinishDone; |
| 169 |
} |
| 170 |
|
| 171 |
if !stream.state.sym_buf.is_empty() { |
| 172 |
flush_block!(stream, false); |
| 173 |
} |
| 174 |
|
| 175 |
BlockState::BlockDone |
| 176 |
} |
| 177 |
|
| 178 |
#[repr(C)] |
| 179 |
#[derive(Debug, Clone, Copy)] |
| 180 |
struct Match { |
| 181 |
match_start: u16, |
| 182 |
match_length: u16, |
| 183 |
strstart: u16, |
| 184 |
orgstart: u16, |
| 185 |
} |
| 186 |
|
| 187 |
fn emit_match(state: &mut State, m: Match) -> bool { |
| 188 |
let mut bflush = false; |
| 189 |
|
| 190 |
/* matches that are not long enough we need to emit as literals */ |
| 191 |
if (m.match_length as usize) < WANT_MIN_MATCH { |
| 192 |
for lc in &state.window.filled()[state.strstart..][..m.match_length as usize] { |
| 193 |
bflush |= State::tally_lit_help(&mut state.sym_buf, &mut state.l_desc, *lc); |
| 194 |
state.lookahead -= 1; |
| 195 |
} |
| 196 |
} else { |
| 197 |
// check_match(s, m.strstart, m.match_start, m.match_length); |
| 198 |
|
| 199 |
bflush |= state.tally_dist( |
| 200 |
(m.strstart - m.match_start) as usize, |
| 201 |
m.match_length as usize - STD_MIN_MATCH, |
| 202 |
); |
| 203 |
|
| 204 |
state.lookahead -= m.match_length as usize; |
| 205 |
} |
| 206 |
|
| 207 |
bflush |
| 208 |
} |
| 209 |
|
| 210 |
#[inline(always)] |
| 211 |
fn insert_match(state: &mut State, mut m: Match) { |
| 212 |
if state.lookahead <= (m.match_length as usize + WANT_MIN_MATCH) { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
/* matches that are not long enough we need to emit as literals */ |
| 217 |
if (m.match_length as usize) < WANT_MIN_MATCH { |
| 218 |
m.strstart += 1; |
| 219 |
m.match_length -= 1; |
| 220 |
if m.match_length > 0 && m.strstart >= m.orgstart { |
| 221 |
if m.strstart + m.match_length > m.orgstart { |
| 222 |
state.insert_string(m.strstart as usize, m.match_length as usize); |
| 223 |
} else { |
| 224 |
state.insert_string(m.strstart as usize, (m.orgstart - m.strstart + 1) as usize); |
| 225 |
} |
| 226 |
m.strstart += m.match_length; |
| 227 |
m.match_length = 0; |
| 228 |
} |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
// Insert new strings in the hash table |
| 233 |
if state.lookahead >= WANT_MIN_MATCH { |
| 234 |
m.match_length -= 1; /* string at strstart already in table */ |
| 235 |
m.strstart += 1; |
| 236 |
|
| 237 |
if m.strstart >= m.orgstart { |
| 238 |
if m.strstart + m.match_length > m.orgstart { |
| 239 |
state.insert_string(m.strstart as usize, m.match_length as usize); |
| 240 |
} else { |
| 241 |
state.insert_string(m.strstart as usize, (m.orgstart - m.strstart + 1) as usize); |
| 242 |
} |
| 243 |
} else if m.orgstart < m.strstart + m.match_length { |
| 244 |
state.insert_string( |
| 245 |
m.orgstart as usize, |
| 246 |
(m.strstart + m.match_length - m.orgstart) as usize, |
| 247 |
); |
| 248 |
} |
| 249 |
m.strstart += m.match_length; |
| 250 |
m.match_length = 0; |
| 251 |
} else { |
| 252 |
m.strstart += m.match_length; |
| 253 |
m.match_length = 0; |
| 254 |
|
| 255 |
if (m.strstart as usize) >= (STD_MIN_MATCH - 2) { |
| 256 |
state.quick_insert_string(m.strstart as usize + 2 - STD_MIN_MATCH); |
| 257 |
} |
| 258 |
|
| 259 |
/* If lookahead < WANT_MIN_MATCH, ins_h is garbage, but it does not |
| 260 |
* matter since it will be recomputed at next deflate call. |
| 261 |
*/ |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
fn fizzle_matches(window: &[u8], max_dist: usize, current: &mut Match, next: &mut Match) { |
| 266 |
/* step zero: sanity checks */ |
| 267 |
|
| 268 |
if current.match_length <= 1 { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
if current.match_length > 1 + next.match_start { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
if current.match_length > 1 + next.strstart { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
let m = &window[(-(current.match_length as isize) + 1 + next.match_start as isize) as usize..]; |
| 281 |
let orig = &window[(-(current.match_length as isize) + 1 + next.strstart as isize) as usize..]; |
| 282 |
|
| 283 |
/* quick exit check.. if this fails then don't bother with anything else */ |
| 284 |
if m[0] != orig[0] { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
/* step one: try to move the "next" match to the left as much as possible */ |
| 289 |
let limit = next.strstart.saturating_sub(max_dist as u16); |
| 290 |
|
| 291 |
let mut c = *current; |
| 292 |
let mut n = *next; |
| 293 |
|
| 294 |
let m = &window[..n.match_start as usize]; |
| 295 |
let orig = &window[..n.strstart as usize]; |
| 296 |
|
| 297 |
let mut m = m.iter().rev(); |
| 298 |
let mut orig = orig.iter().rev(); |
| 299 |
|
| 300 |
let mut changed = 0; |
| 301 |
|
| 302 |
while m.next() == orig.next() { |
| 303 |
if c.match_length < 1 { |
| 304 |
break; |
| 305 |
} |
| 306 |
if n.strstart <= limit { |
| 307 |
break; |
| 308 |
} |
| 309 |
if n.match_length >= 256 { |
| 310 |
break; |
| 311 |
} |
| 312 |
if n.match_start <= 1 { |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
n.strstart -= 1; |
| 317 |
n.match_start -= 1; |
| 318 |
n.match_length += 1; |
| 319 |
c.match_length -= 1; |
| 320 |
changed += 1; |
| 321 |
} |
| 322 |
|
| 323 |
if changed == 0 { |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
if c.match_length <= 1 && n.match_length != 2 { |
| 328 |
n.orgstart += 1; |
| 329 |
*current = c; |
| 330 |
*next = n; |
| 331 |
} |
| 332 |
} |