Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
+114
@@ -0,0 +1,114 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use super::flush_block;
|
||||
use crate::deflate::hash_calc::StandardHashCalc;
|
||||
use crate::{
|
||||
deflate::{
|
||||
fill_window, BlockState, DeflateStream, MIN_LOOKAHEAD, STD_MIN_MATCH, WANT_MIN_MATCH,
|
||||
},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
pub fn deflate_fast(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
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.
|
||||
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).
|
||||
let mut match_len;
|
||||
(match_len, state.match_start) =
|
||||
crate::deflate::longest_match::longest_match(state, hash_head);
|
||||
if match_len >= WANT_MIN_MATCH {
|
||||
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);
|
||||
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
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use super::flush_block;
|
||||
use crate::{
|
||||
deflate::{fill_window, BlockState, DeflateStream},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
pub fn deflate_huff(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
loop {
|
||||
/* Make sure that we have a literal to write. */
|
||||
if stream.state.lookahead == 0 {
|
||||
fill_window(stream);
|
||||
|
||||
if stream.state.lookahead == 0 {
|
||||
match flush {
|
||||
DeflateFlush::NoFlush => return BlockState::NeedMore,
|
||||
_ => break, /* flush the current block */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Output a literal byte */
|
||||
let state = &mut stream.state;
|
||||
let lc = state.window.filled()[state.strstart];
|
||||
let bflush = state.tally_lit(lc);
|
||||
state.lookahead -= 1;
|
||||
state.strstart += 1;
|
||||
if bflush {
|
||||
flush_block!(stream, false);
|
||||
}
|
||||
}
|
||||
|
||||
stream.state.insert = 0;
|
||||
|
||||
if flush == DeflateFlush::Finish {
|
||||
flush_block!(stream, true);
|
||||
return BlockState::FinishDone;
|
||||
}
|
||||
|
||||
if !stream.state.sym_buf.is_empty() {
|
||||
flush_block!(stream, false);
|
||||
}
|
||||
|
||||
BlockState::BlockDone
|
||||
}
|
||||
+331
@@ -0,0 +1,331 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use super::flush_block;
|
||||
use crate::deflate::hash_calc::StandardHashCalc;
|
||||
use crate::{
|
||||
deflate::{
|
||||
fill_window, BlockState, DeflateStream, State, MIN_LOOKAHEAD, STD_MIN_MATCH, WANT_MIN_MATCH,
|
||||
},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
pub fn deflate_medium(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
let mut state = &mut stream.state;
|
||||
|
||||
// For levels below 5, don't check the next position for a better match
|
||||
let early_exit = state.level < 5;
|
||||
|
||||
let mut current_match = Match {
|
||||
match_start: 0,
|
||||
match_length: 0,
|
||||
strstart: 0,
|
||||
orgstart: 0,
|
||||
};
|
||||
let mut next_match = Match {
|
||||
match_start: 0,
|
||||
match_length: 0,
|
||||
strstart: 0,
|
||||
orgstart: 0,
|
||||
};
|
||||
|
||||
loop {
|
||||
let mut hash_head;
|
||||
|
||||
/* 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 */
|
||||
}
|
||||
|
||||
next_match.match_length = 0;
|
||||
}
|
||||
|
||||
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:
|
||||
|
||||
/* If we already have a future match from a previous round, just use that */
|
||||
if !early_exit && next_match.match_length > 0 {
|
||||
current_match = next_match;
|
||||
next_match.match_length = 0;
|
||||
} else {
|
||||
hash_head = 0;
|
||||
if state.lookahead >= WANT_MIN_MATCH {
|
||||
hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
|
||||
}
|
||||
|
||||
current_match.strstart = state.strstart as u16;
|
||||
current_match.orgstart = current_match.strstart;
|
||||
|
||||
/* Find the longest match, discarding those <= prev_length.
|
||||
* At this point we have always match_length < WANT_MIN_MATCH
|
||||
*/
|
||||
|
||||
let dist = state.strstart as i64 - hash_head as i64;
|
||||
if dist <= state.max_dist() as i64 && 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).
|
||||
*/
|
||||
let (match_length, match_start) =
|
||||
crate::deflate::longest_match::longest_match(state, hash_head);
|
||||
state.match_start = match_start;
|
||||
current_match.match_length = match_length as u16;
|
||||
current_match.match_start = match_start;
|
||||
if (current_match.match_length as usize) < WANT_MIN_MATCH {
|
||||
current_match.match_length = 1;
|
||||
}
|
||||
if current_match.match_start >= current_match.strstart {
|
||||
/* this can happen due to some restarts */
|
||||
current_match.match_length = 1;
|
||||
}
|
||||
} else {
|
||||
/* Set up the match to be a 1 byte literal */
|
||||
current_match.match_start = 0;
|
||||
current_match.match_length = 1;
|
||||
}
|
||||
}
|
||||
|
||||
insert_match(state, current_match);
|
||||
|
||||
/* now, look ahead one */
|
||||
if !early_exit
|
||||
&& state.lookahead > MIN_LOOKAHEAD
|
||||
&& ((current_match.strstart + current_match.match_length) as usize)
|
||||
< (state.window_size - MIN_LOOKAHEAD)
|
||||
{
|
||||
state.strstart = (current_match.strstart + current_match.match_length) as usize;
|
||||
hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
|
||||
|
||||
next_match.strstart = state.strstart as u16;
|
||||
next_match.orgstart = next_match.strstart;
|
||||
|
||||
/* Find the longest match, discarding those <= prev_length.
|
||||
* At this point we have always match_length < WANT_MIN_MATCH
|
||||
*/
|
||||
|
||||
let dist = state.strstart as i64 - hash_head as i64;
|
||||
if dist <= state.max_dist() as i64 && 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).
|
||||
*/
|
||||
let (match_length, match_start) =
|
||||
crate::deflate::longest_match::longest_match(state, hash_head);
|
||||
state.match_start = match_start;
|
||||
next_match.match_length = match_length as u16;
|
||||
next_match.match_start = match_start;
|
||||
|
||||
if next_match.match_start >= next_match.strstart {
|
||||
/* this can happen due to some restarts */
|
||||
next_match.match_length = 1;
|
||||
}
|
||||
if (next_match.match_length as usize) < WANT_MIN_MATCH {
|
||||
next_match.match_length = 1;
|
||||
} else {
|
||||
fizzle_matches(
|
||||
state.window.filled(),
|
||||
state.max_dist(),
|
||||
&mut current_match,
|
||||
&mut next_match,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
/* Set up the match to be a 1 byte literal */
|
||||
next_match.match_start = 0;
|
||||
next_match.match_length = 1;
|
||||
}
|
||||
|
||||
state.strstart = current_match.strstart as usize;
|
||||
} else {
|
||||
next_match.match_length = 0;
|
||||
}
|
||||
|
||||
/* now emit the current match */
|
||||
let bflush = emit_match(state, current_match);
|
||||
|
||||
/* move the "cursor" forward */
|
||||
state.strstart += current_match.match_length as usize;
|
||||
|
||||
if bflush {
|
||||
flush_block!(stream, false);
|
||||
}
|
||||
}
|
||||
|
||||
stream.state.insert = Ord::min(stream.state.strstart, 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
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Match {
|
||||
match_start: u16,
|
||||
match_length: u16,
|
||||
strstart: u16,
|
||||
orgstart: u16,
|
||||
}
|
||||
|
||||
fn emit_match(state: &mut State, m: Match) -> bool {
|
||||
let mut bflush = false;
|
||||
|
||||
/* matches that are not long enough we need to emit as literals */
|
||||
if (m.match_length as usize) < WANT_MIN_MATCH {
|
||||
for lc in &state.window.filled()[state.strstart..][..m.match_length as usize] {
|
||||
bflush |= State::tally_lit_help(&mut state.sym_buf, &mut state.l_desc, *lc);
|
||||
}
|
||||
} else {
|
||||
// check_match(s, m.strstart, m.match_start, m.match_length);
|
||||
|
||||
bflush |= state.tally_dist(
|
||||
(m.strstart - m.match_start) as usize,
|
||||
m.match_length as usize - STD_MIN_MATCH,
|
||||
);
|
||||
}
|
||||
state.lookahead -= m.match_length as usize;
|
||||
|
||||
bflush
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn insert_match(state: &mut State, mut m: Match) {
|
||||
if state.lookahead <= (m.match_length as usize + WANT_MIN_MATCH) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* matches that are not long enough we need to emit as literals */
|
||||
if (m.match_length as usize) < WANT_MIN_MATCH {
|
||||
m.strstart += 1;
|
||||
m.match_length -= 1;
|
||||
if m.match_length > 0 && m.strstart >= m.orgstart {
|
||||
if m.strstart + m.match_length > m.orgstart {
|
||||
state.insert_string(m.strstart as usize, m.match_length as usize);
|
||||
} else {
|
||||
state.insert_string(m.strstart as usize, (m.orgstart - m.strstart + 1) as usize);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert new strings in the hash table only if the match length
|
||||
// is not too large. This saves time but degrades compression.
|
||||
if usize::from(m.match_length) <= 16 * state.max_insert_length()
|
||||
&& state.lookahead >= WANT_MIN_MATCH
|
||||
{
|
||||
m.match_length -= 1; /* string at strstart already in table */
|
||||
m.strstart += 1;
|
||||
|
||||
if m.strstart >= m.orgstart {
|
||||
if m.strstart + m.match_length > m.orgstart {
|
||||
state.insert_string(m.strstart as usize, m.match_length as usize);
|
||||
} else {
|
||||
state.insert_string(m.strstart as usize, (m.orgstart - m.strstart + 1) as usize);
|
||||
}
|
||||
} else if m.orgstart < m.strstart + m.match_length {
|
||||
state.insert_string(
|
||||
m.orgstart as usize,
|
||||
(m.strstart + m.match_length - m.orgstart) as usize,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
m.strstart += m.match_length;
|
||||
m.match_length = 0;
|
||||
|
||||
if (m.strstart as usize) >= (STD_MIN_MATCH - 2) {
|
||||
StandardHashCalc::quick_insert_string(state, m.strstart as usize + 2 - STD_MIN_MATCH);
|
||||
}
|
||||
|
||||
/* If lookahead < WANT_MIN_MATCH, ins_h is garbage, but it does not
|
||||
* matter since it will be recomputed at next deflate call.
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
fn fizzle_matches(window: &[u8], max_dist: usize, current: &mut Match, next: &mut Match) {
|
||||
/* step zero: sanity checks */
|
||||
|
||||
if current.match_length <= 1 {
|
||||
return;
|
||||
}
|
||||
|
||||
if current.match_length > 1 + next.match_start {
|
||||
return;
|
||||
}
|
||||
|
||||
if current.match_length > 1 + next.strstart {
|
||||
return;
|
||||
}
|
||||
|
||||
let m = &window[(-(current.match_length as isize) + 1 + next.match_start as isize) as usize..];
|
||||
let orig = &window[(-(current.match_length as isize) + 1 + next.strstart as isize) as usize..];
|
||||
|
||||
/* quick exit check.. if this fails then don't bother with anything else */
|
||||
if m[0] != orig[0] {
|
||||
return;
|
||||
}
|
||||
|
||||
/* step one: try to move the "next" match to the left as much as possible */
|
||||
let limit = next.strstart.saturating_sub(max_dist as u16);
|
||||
|
||||
let mut c = *current;
|
||||
let mut n = *next;
|
||||
|
||||
let m = &window[..n.match_start as usize];
|
||||
let orig = &window[..n.strstart as usize];
|
||||
|
||||
let mut m = m.iter().rev();
|
||||
let mut orig = orig.iter().rev();
|
||||
|
||||
let mut changed = 0;
|
||||
|
||||
while m.next() == orig.next() {
|
||||
if c.match_length < 1 {
|
||||
break;
|
||||
}
|
||||
if n.strstart <= limit {
|
||||
break;
|
||||
}
|
||||
if n.match_length >= 256 {
|
||||
break;
|
||||
}
|
||||
if n.match_start <= 1 {
|
||||
break;
|
||||
}
|
||||
|
||||
n.strstart -= 1;
|
||||
n.match_start -= 1;
|
||||
n.match_length += 1;
|
||||
c.match_length -= 1;
|
||||
changed += 1;
|
||||
}
|
||||
|
||||
if changed == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
if c.match_length <= 1 && n.match_length != 2 {
|
||||
n.orgstart += 1;
|
||||
*current = c;
|
||||
*next = n;
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
use crate::{
|
||||
deflate::{BlockState, DeflateStream, Strategy},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
use self::{huff::deflate_huff, rle::deflate_rle, stored::deflate_stored};
|
||||
|
||||
mod fast;
|
||||
mod huff;
|
||||
mod medium;
|
||||
mod quick;
|
||||
mod rle;
|
||||
mod slow;
|
||||
mod stored;
|
||||
|
||||
macro_rules! flush_block {
|
||||
($stream:expr, $is_last_block:expr) => {
|
||||
$crate::deflate::flush_block_only($stream, $is_last_block);
|
||||
|
||||
if $stream.avail_out == 0 {
|
||||
return match $is_last_block {
|
||||
true => BlockState::FinishStarted,
|
||||
false => BlockState::NeedMore,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use flush_block;
|
||||
|
||||
pub fn run(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
match stream.state.strategy {
|
||||
_ if stream.state.level == 0 => deflate_stored(stream, flush),
|
||||
Strategy::HuffmanOnly => deflate_huff(stream, flush),
|
||||
Strategy::Rle => deflate_rle(stream, flush),
|
||||
Strategy::Default | Strategy::Filtered | Strategy::Fixed => {
|
||||
(CONFIGURATION_TABLE[stream.state.level as usize].func)(stream, flush)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type CompressFunc = fn(&mut DeflateStream, flush: DeflateFlush) -> BlockState;
|
||||
|
||||
pub struct Config {
|
||||
pub good_length: u16, /* reduce lazy search above this match length */
|
||||
pub max_lazy: u16, /* do not perform lazy search above this match length */
|
||||
pub nice_length: u16, /* quit search above this match length */
|
||||
pub max_chain: u16,
|
||||
pub func: CompressFunc,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
const fn new(
|
||||
good_length: u16,
|
||||
max_lazy: u16,
|
||||
nice_length: u16,
|
||||
max_chain: u16,
|
||||
func: CompressFunc,
|
||||
) -> Self {
|
||||
Self {
|
||||
good_length,
|
||||
max_lazy,
|
||||
nice_length,
|
||||
max_chain,
|
||||
func,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub static CONFIGURATION_TABLE: [Config; 10] = {
|
||||
[
|
||||
Config::new(0, 0, 0, 0, stored::deflate_stored), // 0 /* store only */
|
||||
Config::new(0, 0, 0, 0, quick::deflate_quick), // 1
|
||||
Config::new(4, 4, 8, 4, fast::deflate_fast), // 2 /* max speed, no lazy matches */
|
||||
Config::new(4, 6, 16, 6, medium::deflate_medium), // 3
|
||||
Config::new(4, 12, 32, 24, medium::deflate_medium), // 4 /* lazy matches */
|
||||
Config::new(8, 16, 32, 32, medium::deflate_medium), // 5
|
||||
Config::new(8, 16, 128, 128, medium::deflate_medium), // 6
|
||||
Config::new(8, 32, 128, 256, slow::deflate_slow), // 7
|
||||
Config::new(32, 128, 258, 1024, slow::deflate_slow), // 8
|
||||
Config::new(32, 258, 258, 4096, slow::deflate_slow), // 9 /* max compression */
|
||||
]
|
||||
};
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use crate::deflate::hash_calc::StandardHashCalc;
|
||||
use crate::{
|
||||
deflate::{
|
||||
fill_window, flush_pending, BlockState, BlockType, DeflateStream, State, StaticTreeDesc,
|
||||
MIN_LOOKAHEAD, STD_MAX_MATCH, STD_MIN_MATCH, WANT_MIN_MATCH,
|
||||
},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
pub fn deflate_quick(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
let mut state = &mut stream.state;
|
||||
|
||||
macro_rules! quick_end_block {
|
||||
($last:expr) => {
|
||||
if state.block_open > 0 {
|
||||
state
|
||||
.bit_writer
|
||||
.emit_end_block_and_align(&StaticTreeDesc::L.static_tree, $last);
|
||||
state.block_open = 0;
|
||||
state.block_start = state.strstart as isize;
|
||||
flush_pending(stream);
|
||||
#[allow(unused_assignments)]
|
||||
{
|
||||
state = &mut stream.state;
|
||||
}
|
||||
if stream.avail_out == 0 {
|
||||
return match $last {
|
||||
true => BlockState::FinishStarted,
|
||||
false => BlockState::NeedMore,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! quick_start_block {
|
||||
($last:expr) => {
|
||||
state.bit_writer.emit_tree(BlockType::StaticTrees, $last);
|
||||
state.block_open = 1 + $last as u8;
|
||||
state.block_start = state.strstart as isize;
|
||||
};
|
||||
}
|
||||
|
||||
let last = matches!(flush, DeflateFlush::Finish);
|
||||
|
||||
if last && state.block_open != 2 {
|
||||
/* Emit end of previous block */
|
||||
quick_end_block!(false);
|
||||
/* Emit start of last block */
|
||||
quick_start_block!(last);
|
||||
} else if state.block_open == 0 && state.lookahead > 0 {
|
||||
/* Start new block only when we have lookahead data, so that if no
|
||||
input data is given an empty block will not be written */
|
||||
quick_start_block!(last);
|
||||
}
|
||||
|
||||
loop {
|
||||
if state.bit_writer.pending.pending + State::BIT_BUF_SIZE.div_ceil(8) as usize
|
||||
>= state.pending_buf_size()
|
||||
{
|
||||
flush_pending(stream);
|
||||
state = &mut stream.state;
|
||||
if stream.avail_out == 0 {
|
||||
return if last
|
||||
&& stream.avail_in == 0
|
||||
&& state.bit_writer.bits_valid == 0
|
||||
&& state.block_open == 0
|
||||
{
|
||||
BlockState::FinishStarted
|
||||
} else {
|
||||
BlockState::NeedMore
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if state.lookahead < MIN_LOOKAHEAD {
|
||||
fill_window(stream);
|
||||
state = &mut stream.state;
|
||||
|
||||
if state.lookahead < MIN_LOOKAHEAD && matches!(flush, DeflateFlush::NoFlush) {
|
||||
return BlockState::NeedMore;
|
||||
}
|
||||
if state.lookahead == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
if state.block_open == 0 {
|
||||
// Start new block when we have lookahead data,
|
||||
// so that if no input data is given an empty block will not be written
|
||||
quick_start_block!(last);
|
||||
}
|
||||
}
|
||||
|
||||
let lc = if state.lookahead >= WANT_MIN_MATCH {
|
||||
macro_rules! first_four_bytes {
|
||||
($slice:expr, $offset:expr) => {
|
||||
u32::from_le_bytes($slice[$offset..$offset + 4].try_into().unwrap())
|
||||
};
|
||||
}
|
||||
|
||||
let str_val = {
|
||||
let str_start = &state.window.filled()[state.strstart..];
|
||||
first_four_bytes!(str_start, 0)
|
||||
};
|
||||
let hash_head = StandardHashCalc::quick_insert_value(state, state.strstart, str_val);
|
||||
let dist = state.strstart as isize - hash_head as isize;
|
||||
|
||||
if dist <= state.max_dist() as isize && dist > 0 {
|
||||
let match_start = &state.window.filled()[hash_head as usize..];
|
||||
|
||||
if str_val == first_four_bytes!(match_start, 0) {
|
||||
let mut match_len = crate::deflate::compare256::compare256_slice(
|
||||
&state.window.filled()[state.strstart + 2..],
|
||||
&match_start[2..],
|
||||
) + 2;
|
||||
|
||||
if match_len >= WANT_MIN_MATCH {
|
||||
match_len = Ord::min(match_len, state.lookahead);
|
||||
match_len = Ord::min(match_len, STD_MAX_MATCH);
|
||||
|
||||
// TODO do this with a debug_assert?
|
||||
// check_match(s, state.strstart, hash_head, match_len);
|
||||
|
||||
// The `dist` value is a distance within the window,
|
||||
// and MAX_WBITS == 15 (32k), hence a u16 can always represent this value.
|
||||
let dist = u16::try_from(dist).unwrap();
|
||||
|
||||
state
|
||||
.bit_writer
|
||||
.emit_dist_static((match_len - STD_MIN_MATCH) as u8, dist);
|
||||
state.lookahead -= match_len;
|
||||
state.strstart += match_len;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
str_val as u8
|
||||
} else {
|
||||
state.window.filled()[state.strstart]
|
||||
};
|
||||
state.bit_writer.emit_lit(StaticTreeDesc::L.static_tree, lc);
|
||||
state.strstart += 1;
|
||||
state.lookahead -= 1;
|
||||
}
|
||||
|
||||
state.insert = Ord::min(state.strstart, STD_MIN_MATCH - 1);
|
||||
|
||||
quick_end_block!(last);
|
||||
|
||||
if last {
|
||||
BlockState::FinishDone
|
||||
} else {
|
||||
BlockState::BlockDone
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use super::flush_block;
|
||||
use crate::{
|
||||
deflate::{
|
||||
compare256::compare256_rle_slice, fill_window, BlockState, DeflateStream, MIN_LOOKAHEAD,
|
||||
STD_MAX_MATCH, STD_MIN_MATCH,
|
||||
},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
pub fn deflate_rle(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
let mut match_len = 0;
|
||||
let mut bflush;
|
||||
|
||||
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 */
|
||||
}
|
||||
}
|
||||
|
||||
/* See how many times the previous byte repeats */
|
||||
let state = &mut stream.state;
|
||||
if state.lookahead >= STD_MIN_MATCH && state.strstart > 0 {
|
||||
let scan = &state.window.filled()[state.strstart - 1..][..3 + 256];
|
||||
|
||||
{
|
||||
if scan[0] == scan[1] && scan[1] == scan[2] {
|
||||
match_len = compare256_rle_slice(scan[0], &scan[3..]) + 2;
|
||||
match_len = Ord::min(match_len, state.lookahead);
|
||||
match_len = Ord::min(match_len, STD_MAX_MATCH);
|
||||
}
|
||||
}
|
||||
|
||||
assert!(
|
||||
state.strstart - 1 + match_len < state.window_size,
|
||||
"wild scan"
|
||||
);
|
||||
}
|
||||
|
||||
/* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
|
||||
if match_len >= STD_MIN_MATCH {
|
||||
// check_match(s, s->strstart, s->strstart - 1, match_len);
|
||||
|
||||
bflush = state.tally_dist(1, match_len - STD_MIN_MATCH);
|
||||
|
||||
state.lookahead -= match_len;
|
||||
state.strstart += match_len;
|
||||
match_len = 0;
|
||||
} else {
|
||||
/* No match, output a literal byte */
|
||||
let lc = state.window.filled()[state.strstart];
|
||||
bflush = state.tally_lit(lc);
|
||||
state.lookahead -= 1;
|
||||
state.strstart += 1;
|
||||
}
|
||||
|
||||
if bflush {
|
||||
flush_block!(stream, false);
|
||||
}
|
||||
}
|
||||
|
||||
stream.state.insert = 0;
|
||||
|
||||
if flush == DeflateFlush::Finish {
|
||||
flush_block!(stream, true);
|
||||
return BlockState::FinishDone;
|
||||
}
|
||||
|
||||
if !stream.state.sym_buf.is_empty() {
|
||||
flush_block!(stream, false);
|
||||
}
|
||||
|
||||
BlockState::BlockDone
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use super::flush_block;
|
||||
use crate::{
|
||||
deflate::{
|
||||
fill_window, flush_block_only, BlockState, DeflateStream, Strategy, MIN_LOOKAHEAD,
|
||||
STD_MIN_MATCH, WANT_MIN_MATCH,
|
||||
},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
pub fn deflate_slow(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
let mut hash_head; /* head of hash chain */
|
||||
let mut bflush; /* set if current block must be flushed */
|
||||
let mut dist;
|
||||
let mut match_len;
|
||||
|
||||
let use_longest_match_slow = stream.state.max_chain_length > 1024;
|
||||
let valid_distance_range = 1..=stream.state.max_dist() as isize;
|
||||
|
||||
let mut match_available = stream.state.match_available;
|
||||
|
||||
/* Process the input block. */
|
||||
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:
|
||||
*/
|
||||
hash_head = if state.lookahead >= WANT_MIN_MATCH {
|
||||
state.quick_insert_string(state.strstart)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
// Find the longest match, discarding those <= prev_length.
|
||||
state.prev_match = state.match_start;
|
||||
match_len = STD_MIN_MATCH - 1;
|
||||
dist = state.strstart as isize - hash_head as isize;
|
||||
|
||||
if valid_distance_range.contains(&dist)
|
||||
&& state.prev_length < state.max_lazy_match
|
||||
&& 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) = if use_longest_match_slow {
|
||||
crate::deflate::longest_match::longest_match_slow(state, hash_head)
|
||||
} else {
|
||||
crate::deflate::longest_match::longest_match(state, hash_head)
|
||||
};
|
||||
|
||||
if match_len <= 5 && (state.strategy == Strategy::Filtered) {
|
||||
/* If prev_match is also WANT_MIN_MATCH, match_start is garbage
|
||||
* but we will ignore the current match anyway.
|
||||
*/
|
||||
match_len = STD_MIN_MATCH - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// If there was a match at the previous step and the current
|
||||
// match is not better, output the previous match:
|
||||
if state.prev_length as usize >= STD_MIN_MATCH && match_len <= state.prev_length as usize {
|
||||
let max_insert = state.strstart + state.lookahead - STD_MIN_MATCH;
|
||||
/* Do not insert strings in hash table beyond this. */
|
||||
|
||||
// check_match(s, state.strstart-1, state.prev_match, state.prev_length);
|
||||
|
||||
bflush = state.tally_dist(
|
||||
state.strstart - 1 - state.prev_match as usize,
|
||||
state.prev_length as usize - STD_MIN_MATCH,
|
||||
);
|
||||
|
||||
/* Insert in hash table all strings up to the end of the match.
|
||||
* strstart-1 and strstart are already inserted. If there is not
|
||||
* enough lookahead, the last two strings are not inserted in
|
||||
* the hash table.
|
||||
*/
|
||||
state.prev_length -= 1;
|
||||
state.lookahead -= state.prev_length as usize;
|
||||
|
||||
let mov_fwd = state.prev_length as usize - 1;
|
||||
if max_insert > state.strstart {
|
||||
let insert_cnt = Ord::min(mov_fwd, max_insert - state.strstart);
|
||||
state.insert_string(state.strstart + 1, insert_cnt);
|
||||
}
|
||||
state.prev_length = 0;
|
||||
state.match_available = false;
|
||||
match_available = false;
|
||||
state.strstart += mov_fwd + 1;
|
||||
|
||||
if bflush {
|
||||
flush_block!(stream, false);
|
||||
}
|
||||
} else if match_available {
|
||||
// If there was no match at the previous position, output a
|
||||
// single literal. If there was a match but the current match
|
||||
// is longer, truncate the previous match to a single literal.
|
||||
let lc = state.window.filled()[state.strstart - 1];
|
||||
bflush = state.tally_lit(lc);
|
||||
if bflush {
|
||||
flush_block_only(stream, false);
|
||||
}
|
||||
|
||||
stream.state.prev_length = match_len as u16;
|
||||
stream.state.strstart += 1;
|
||||
stream.state.lookahead -= 1;
|
||||
if stream.avail_out == 0 {
|
||||
return BlockState::NeedMore;
|
||||
}
|
||||
} else {
|
||||
// There is no previous match to compare with, wait for
|
||||
// the next step to decide.
|
||||
state.prev_length = match_len as u16;
|
||||
state.match_available = true;
|
||||
match_available = true;
|
||||
state.strstart += 1;
|
||||
state.lookahead -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
assert_ne!(flush, DeflateFlush::NoFlush, "no flush?");
|
||||
|
||||
let state = &mut stream.state;
|
||||
|
||||
if state.match_available {
|
||||
let lc = state.window.filled()[state.strstart - 1];
|
||||
let _ = state.tally_lit(lc);
|
||||
state.match_available = false;
|
||||
}
|
||||
|
||||
state.insert = Ord::min(state.strstart, 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
|
||||
}
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
use crate::{
|
||||
deflate::{
|
||||
flush_pending, read_buf_window, zng_tr_stored_block, BlockState, DeflateStream, MAX_STORED,
|
||||
},
|
||||
DeflateFlush,
|
||||
};
|
||||
|
||||
pub fn deflate_stored(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
|
||||
// Smallest worthy block size when not flushing or finishing. By default
|
||||
// this is 32K. This can be as small as 507 bytes for memLevel == 1. For
|
||||
// large input and output buffers, the stored block size will be larger.
|
||||
let min_block = Ord::min(
|
||||
stream.state.bit_writer.pending.capacity() - 5,
|
||||
stream.state.w_size,
|
||||
);
|
||||
|
||||
// Copy as many min_block or larger stored blocks directly to next_out as
|
||||
// possible. If flushing, copy the remaining available input to next_out as
|
||||
// stored blocks, if there is enough space.
|
||||
|
||||
// unsigned len, left, have, last = 0;
|
||||
let mut have;
|
||||
let mut last = false;
|
||||
let mut used = stream.avail_in;
|
||||
loop {
|
||||
// maximum deflate stored block length
|
||||
let mut len = MAX_STORED;
|
||||
|
||||
// number of header bytes
|
||||
have = ((stream.state.bit_writer.bits_valid + 42) / 8) as usize;
|
||||
|
||||
// we need room for at least the header
|
||||
if stream.avail_out < have as u32 {
|
||||
break;
|
||||
}
|
||||
|
||||
let left = stream.state.strstart as isize - stream.state.block_start;
|
||||
let left = Ord::max(0, left) as usize;
|
||||
|
||||
have = stream.avail_out as usize - have;
|
||||
|
||||
if len > left + stream.avail_in as usize {
|
||||
// limit len to the input
|
||||
len = left + stream.avail_in as usize;
|
||||
}
|
||||
|
||||
len = Ord::min(len, have);
|
||||
|
||||
// If the stored block would be less than min_block in length, or if
|
||||
// unable to copy all of the available input when flushing, then try
|
||||
// copying to the window and the pending buffer instead. Also don't
|
||||
// write an empty block when flushing -- deflate() does that.
|
||||
if len < min_block
|
||||
&& ((len == 0 && flush != DeflateFlush::Finish)
|
||||
|| flush == DeflateFlush::NoFlush
|
||||
|| len != left + stream.avail_in as usize)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Make a dummy stored block in pending to get the header bytes,
|
||||
// including any pending bits. This also updates the debugging counts.
|
||||
last = flush == DeflateFlush::Finish && len == left + stream.avail_in as usize;
|
||||
zng_tr_stored_block(stream.state, 0..0, last);
|
||||
|
||||
/* Replace the lengths in the dummy stored block with len. */
|
||||
stream.state.bit_writer.pending.rewind(4);
|
||||
stream
|
||||
.state
|
||||
.bit_writer
|
||||
.pending
|
||||
.extend(&(len as u16).to_le_bytes());
|
||||
stream
|
||||
.state
|
||||
.bit_writer
|
||||
.pending
|
||||
.extend(&(!len as u16).to_le_bytes());
|
||||
|
||||
// Write the stored block header bytes.
|
||||
flush_pending(stream);
|
||||
|
||||
// Update debugging counts for the data about to be copied.
|
||||
stream.state.bit_writer.cmpr_bits_add(len << 3);
|
||||
stream.state.bit_writer.sent_bits_add(len << 3);
|
||||
|
||||
if left > 0 {
|
||||
// SAFETY: `len` is effectively `min(stream.avail_in, stream.avail_out)`, so any reads
|
||||
// of `len` won't go out of bounds on `next_out`. `left` is calculated from indices of
|
||||
// the window, so `left` reads of the window won't go out of bounds.
|
||||
let left = Ord::min(left, len);
|
||||
let src = &stream.state.window.filled()[stream.state.block_start as usize..];
|
||||
unsafe { core::ptr::copy_nonoverlapping(src.as_ptr(), stream.next_out, left) };
|
||||
|
||||
stream.next_out = stream.next_out.wrapping_add(left);
|
||||
stream.avail_out = stream.avail_out.wrapping_sub(left as _);
|
||||
stream.total_out = stream.total_out.wrapping_add(left as _);
|
||||
stream.state.block_start += left as isize;
|
||||
len -= left;
|
||||
}
|
||||
|
||||
// Copy uncompressed bytes directly from next_in to next_out, updating the check value.
|
||||
if len > 0 {
|
||||
read_buf_direct_copy(stream, len);
|
||||
}
|
||||
|
||||
if last {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the sliding window with the last s->w_size bytes of the copied
|
||||
// data, or append all of the copied data to the existing window if less
|
||||
// than s->w_size bytes were copied. Also update the number of bytes to
|
||||
// insert in the hash tables, in the event that deflateParams() switches to
|
||||
// a non-zero compression level.
|
||||
used -= stream.avail_in; /* number of input bytes directly copied */
|
||||
|
||||
if used > 0 {
|
||||
let state = &mut stream.state;
|
||||
// If any input was used, then no unused input remains in the window, therefore s->block_start == s->strstart.
|
||||
if used as usize >= state.w_size {
|
||||
/* supplant the previous history */
|
||||
state.matches = 2; /* clear hash */
|
||||
|
||||
// SAFETY: we've advanced the next_in pointer at minimum w_size bytes
|
||||
// read_buf_direct_copy(), so we are able to backtrack that number of bytes.
|
||||
let src = stream.next_in.wrapping_sub(state.w_size);
|
||||
unsafe { state.window.copy_and_initialize(0..state.w_size, src) };
|
||||
|
||||
state.strstart = state.w_size;
|
||||
state.insert = state.strstart;
|
||||
} else {
|
||||
if state.window_size - state.strstart <= used as usize {
|
||||
/* Slide the window down. */
|
||||
state.strstart -= state.w_size;
|
||||
|
||||
// make sure we don't copy uninitialized bytes. While we discard the first lower w_size
|
||||
// bytes, it is not guaranteed that the upper w_size bytes are all initialized
|
||||
let copy = Ord::min(state.strstart, state.window.filled().len() - state.w_size);
|
||||
|
||||
state
|
||||
.window
|
||||
.filled_mut()
|
||||
.copy_within(state.w_size..state.w_size + copy, 0);
|
||||
|
||||
if state.matches < 2 {
|
||||
state.matches += 1; /* add a pending slide_hash() */
|
||||
}
|
||||
state.insert = Ord::min(state.insert, state.strstart);
|
||||
}
|
||||
|
||||
// SAFETY: we've advanced the next_in pointer at least `used` bytes
|
||||
// read_buf_direct_copy(), so we are able to backtrack that number of bytes.
|
||||
let src = stream.next_in.wrapping_sub(used as usize);
|
||||
let dst = state.strstart..state.strstart + used as usize;
|
||||
unsafe { state.window.copy_and_initialize(dst, src) };
|
||||
|
||||
state.strstart += used as usize;
|
||||
state.insert += Ord::min(used as usize, state.w_size - state.insert);
|
||||
}
|
||||
state.block_start = state.strstart as isize;
|
||||
}
|
||||
|
||||
// If the last block was written to next_out, then done.
|
||||
if last {
|
||||
stream.state.bit_writer.bits_used = 8;
|
||||
return BlockState::FinishDone;
|
||||
}
|
||||
|
||||
// If flushing and all input has been consumed, then done.
|
||||
if flush != DeflateFlush::NoFlush
|
||||
&& flush != DeflateFlush::Finish
|
||||
&& stream.avail_in == 0
|
||||
&& stream.state.strstart as isize == stream.state.block_start
|
||||
{
|
||||
return BlockState::BlockDone;
|
||||
}
|
||||
|
||||
// Fill the window with any remaining input
|
||||
let mut have = stream.state.window_size - stream.state.strstart;
|
||||
if stream.avail_in as usize > have && stream.state.block_start >= stream.state.w_size as isize {
|
||||
// slide the window down
|
||||
let state = &mut stream.state;
|
||||
state.block_start -= state.w_size as isize;
|
||||
state.strstart -= state.w_size;
|
||||
|
||||
// make sure we don't copy uninitialized bytes. While we discard the first lower w_size
|
||||
// bytes, it is not guaranteed that the upper w_size bytes are all initialized
|
||||
let copy = Ord::min(state.strstart, state.window.filled().len() - state.w_size);
|
||||
|
||||
state
|
||||
.window
|
||||
.filled_mut()
|
||||
.copy_within(state.w_size..state.w_size + copy, 0);
|
||||
|
||||
if state.matches < 2 {
|
||||
// add a pending slide_hash
|
||||
state.matches += 1;
|
||||
}
|
||||
|
||||
have += state.w_size; // more space now
|
||||
state.insert = Ord::min(state.insert, state.strstart);
|
||||
}
|
||||
|
||||
let have = Ord::min(have, stream.avail_in as usize);
|
||||
if have > 0 {
|
||||
read_buf_window(stream, stream.state.strstart, have);
|
||||
|
||||
let state = &mut stream.state;
|
||||
state.strstart += have;
|
||||
state.insert += Ord::min(have, state.w_size - state.insert);
|
||||
}
|
||||
|
||||
// There was not enough avail_out to write a complete worthy or flushed
|
||||
// stored block to next_out. Write a stored block to pending instead, if we
|
||||
// have enough input for a worthy block, or if flushing and there is enough
|
||||
// room for the remaining input as a stored block in the pending buffer.
|
||||
|
||||
// number of header bytes
|
||||
let state = &mut stream.state;
|
||||
let have = ((state.bit_writer.bits_valid + 42) >> 3) as usize;
|
||||
|
||||
// maximum stored block length that will fit in pending:
|
||||
let have = Ord::min(state.bit_writer.pending.capacity() - have, MAX_STORED);
|
||||
let min_block = Ord::min(have, state.w_size);
|
||||
let left = state.strstart as isize - state.block_start;
|
||||
|
||||
if left >= min_block as isize
|
||||
|| ((left > 0 || flush == DeflateFlush::Finish)
|
||||
&& flush != DeflateFlush::NoFlush
|
||||
&& stream.avail_in == 0
|
||||
&& left <= have as isize)
|
||||
{
|
||||
let len = Ord::min(left as usize, have); // TODO wrapping?
|
||||
last = flush == DeflateFlush::Finish && stream.avail_in == 0 && len == (left as usize);
|
||||
|
||||
let range = state.block_start as usize..state.block_start as usize + len;
|
||||
zng_tr_stored_block(state, range, last);
|
||||
|
||||
state.block_start += len as isize;
|
||||
flush_pending(stream);
|
||||
}
|
||||
|
||||
// We've done all we can with the available input and output.
|
||||
if last {
|
||||
stream.state.bit_writer.bits_used = 8;
|
||||
BlockState::FinishStarted
|
||||
} else {
|
||||
BlockState::NeedMore
|
||||
}
|
||||
}
|
||||
|
||||
fn read_buf_direct_copy(stream: &mut DeflateStream, size: usize) -> usize {
|
||||
let len = Ord::min(stream.avail_in as usize, size);
|
||||
let output = stream.next_out;
|
||||
|
||||
if len == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
stream.avail_in -= len as u32;
|
||||
|
||||
// SAFETY: len is effectively bounded by next_in and next_out (via size derived in the calling
|
||||
// function), so copies are in-bounds.
|
||||
if stream.state.wrap == 2 {
|
||||
// we likely cannot fuse the crc32 and the copy here because the input can be changed by
|
||||
// a concurrent thread. Therefore it cannot be converted into a slice!
|
||||
unsafe { core::ptr::copy_nonoverlapping(stream.next_in, output, len) }
|
||||
|
||||
let data = unsafe { core::slice::from_raw_parts(output, len) };
|
||||
stream.state.crc_fold.fold(data, 0);
|
||||
} else if stream.state.wrap == 1 {
|
||||
// we cannot fuse the adler and the copy in our case, because adler32 takes a slice.
|
||||
// Another process is allowed to concurrently modify stream.next_in, so we cannot turn it
|
||||
// into a rust slice (violates its safety requirements)
|
||||
unsafe { core::ptr::copy_nonoverlapping(stream.next_in, output, len) }
|
||||
|
||||
let data = unsafe { core::slice::from_raw_parts(output, len) };
|
||||
stream.adler = crate::adler32::adler32(stream.adler as u32, data) as _;
|
||||
} else {
|
||||
unsafe { core::ptr::copy_nonoverlapping(stream.next_in, output, len) }
|
||||
}
|
||||
|
||||
stream.next_in = stream.next_in.wrapping_add(len);
|
||||
stream.total_in += len as crate::c_api::z_size;
|
||||
|
||||
stream.next_out = stream.next_out.wrapping_add(len as _);
|
||||
stream.avail_out = stream.avail_out.wrapping_sub(len as _);
|
||||
stream.total_out = stream.total_out.wrapping_add(len as _);
|
||||
|
||||
len
|
||||
}
|
||||
Reference in New Issue
Block a user