| 1 |
use crate::{ |
| 2 |
deflate::{ |
| 3 |
flush_pending, read_buf_window, zng_tr_stored_block, BlockState, DeflateStream, MAX_STORED, |
| 4 |
}, |
| 5 |
DeflateFlush, |
| 6 |
}; |
| 7 |
|
| 8 |
pub fn deflate_stored(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState { |
| 9 |
// Smallest worthy block size when not flushing or finishing. By default |
| 10 |
// this is 32K. This can be as small as 507 bytes for memLevel == 1. For |
| 11 |
// large input and output buffers, the stored block size will be larger. |
| 12 |
let min_block = Ord::min( |
| 13 |
stream.state.bit_writer.pending.capacity() - 5, |
| 14 |
stream.state.w_size, |
| 15 |
); |
| 16 |
|
| 17 |
// Copy as many min_block or larger stored blocks directly to next_out as |
| 18 |
// possible. If flushing, copy the remaining available input to next_out as |
| 19 |
// stored blocks, if there is enough space. |
| 20 |
|
| 21 |
// unsigned len, left, have, last = 0; |
| 22 |
let mut have; |
| 23 |
let mut last = false; |
| 24 |
let mut used = stream.avail_in; |
| 25 |
loop { |
| 26 |
// maximum deflate stored block length |
| 27 |
let mut len = MAX_STORED; |
| 28 |
|
| 29 |
// number of header bytes |
| 30 |
have = ((stream.state.bit_writer.bits_used + 42) / 8) as usize; |
| 31 |
|
| 32 |
// we need room for at least the header |
| 33 |
if stream.avail_out < have as u32 { |
| 34 |
break; |
| 35 |
} |
| 36 |
|
| 37 |
let left = stream.state.strstart as isize - stream.state.block_start; |
| 38 |
let left = Ord::max(0, left) as usize; |
| 39 |
|
| 40 |
have = stream.avail_out as usize - have; |
| 41 |
|
| 42 |
if len > left + stream.avail_in as usize { |
| 43 |
// limit len to the input |
| 44 |
len = left + stream.avail_in as usize; |
| 45 |
} |
| 46 |
|
| 47 |
len = Ord::min(len, have); |
| 48 |
|
| 49 |
// If the stored block would be less than min_block in length, or if |
| 50 |
// unable to copy all of the available input when flushing, then try |
| 51 |
// copying to the window and the pending buffer instead. Also don't |
| 52 |
// write an empty block when flushing -- deflate() does that. |
| 53 |
if len < min_block |
| 54 |
&& ((len == 0 && flush != DeflateFlush::Finish) |
| 55 |
|| flush == DeflateFlush::NoFlush |
| 56 |
|| len != left + stream.avail_in as usize) |
| 57 |
{ |
| 58 |
break; |
| 59 |
} |
| 60 |
|
| 61 |
// Make a dummy stored block in pending to get the header bytes, |
| 62 |
// including any pending bits. This also updates the debugging counts. |
| 63 |
last = flush == DeflateFlush::Finish && len == left + stream.avail_in as usize; |
| 64 |
zng_tr_stored_block(stream.state, 0..0, last); |
| 65 |
|
| 66 |
/* Replace the lengths in the dummy stored block with len. */ |
| 67 |
stream.state.bit_writer.pending.rewind(4); |
| 68 |
stream |
| 69 |
.state |
| 70 |
.bit_writer |
| 71 |
.pending |
| 72 |
.extend(&(len as u16).to_le_bytes()); |
| 73 |
stream |
| 74 |
.state |
| 75 |
.bit_writer |
| 76 |
.pending |
| 77 |
.extend(&(!len as u16).to_le_bytes()); |
| 78 |
|
| 79 |
// Write the stored block header bytes. |
| 80 |
flush_pending(stream); |
| 81 |
|
| 82 |
// Update debugging counts for the data about to be copied. |
| 83 |
stream.state.bit_writer.cmpr_bits_add(len << 3); |
| 84 |
stream.state.bit_writer.sent_bits_add(len << 3); |
| 85 |
|
| 86 |
if left > 0 { |
| 87 |
// SAFETY: `len` is effectively `min(stream.avail_in, stream.avail_out)`, so any reads |
| 88 |
// of `len` won't go out of bounds on `next_out`. `left` is calculated from indices of |
| 89 |
// the window, so `left` reads of the window won't go out of bounds. |
| 90 |
let left = Ord::min(left, len); |
| 91 |
let src = &stream.state.window.filled()[stream.state.block_start as usize..]; |
| 92 |
unsafe { core::ptr::copy_nonoverlapping(src.as_ptr(), stream.next_out, left) }; |
| 93 |
|
| 94 |
stream.next_out = stream.next_out.wrapping_add(left); |
| 95 |
stream.avail_out = stream.avail_out.wrapping_sub(left as _); |
| 96 |
stream.total_out = stream.total_out.wrapping_add(left as _); |
| 97 |
stream.state.block_start += left as isize; |
| 98 |
len -= left; |
| 99 |
} |
| 100 |
|
| 101 |
// Copy uncompressed bytes directly from next_in to next_out, updating the check value. |
| 102 |
if len > 0 { |
| 103 |
read_buf_direct_copy(stream, len); |
| 104 |
} |
| 105 |
|
| 106 |
if last { |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Update the sliding window with the last s->w_size bytes of the copied |
| 112 |
// data, or append all of the copied data to the existing window if less |
| 113 |
// than s->w_size bytes were copied. Also update the number of bytes to |
| 114 |
// insert in the hash tables, in the event that deflateParams() switches to |
| 115 |
// a non-zero compression level. |
| 116 |
used -= stream.avail_in; /* number of input bytes directly copied */ |
| 117 |
|
| 118 |
if used > 0 { |
| 119 |
let state = &mut stream.state; |
| 120 |
// If any input was used, then no unused input remains in the window, therefore s->block_start == s->strstart. |
| 121 |
if used as usize >= state.w_size { |
| 122 |
/* supplant the previous history */ |
| 123 |
state.matches = 2; /* clear hash */ |
| 124 |
|
| 125 |
// SAFETY: we've advanced the next_in pointer at minimum w_size bytes |
| 126 |
// read_buf_direct_copy(), so we are able to backtrack that number of bytes. |
| 127 |
let src = stream.next_in.wrapping_sub(state.w_size); |
| 128 |
unsafe { state.window.copy_and_initialize(0..state.w_size, src) }; |
| 129 |
|
| 130 |
state.strstart = state.w_size; |
| 131 |
state.insert = state.strstart; |
| 132 |
} else { |
| 133 |
if state.window_size - state.strstart <= used as usize { |
| 134 |
/* Slide the window down. */ |
| 135 |
state.strstart -= state.w_size; |
| 136 |
|
| 137 |
// make sure we don't copy uninitialized bytes. While we discard the first lower w_size |
| 138 |
// bytes, it is not guaranteed that the upper w_size bytes are all initialized |
| 139 |
let copy = Ord::min(state.strstart, state.window.filled().len() - state.w_size); |
| 140 |
|
| 141 |
state |
| 142 |
.window |
| 143 |
.filled_mut() |
| 144 |
.copy_within(state.w_size..state.w_size + copy, 0); |
| 145 |
|
| 146 |
if state.matches < 2 { |
| 147 |
state.matches += 1; /* add a pending slide_hash() */ |
| 148 |
} |
| 149 |
state.insert = Ord::min(state.insert, state.strstart); |
| 150 |
} |
| 151 |
|
| 152 |
// SAFETY: we've advanced the next_in pointer at least `used` bytes |
| 153 |
// read_buf_direct_copy(), so we are able to backtrack that number of bytes. |
| 154 |
let src = stream.next_in.wrapping_sub(used as usize); |
| 155 |
let dst = state.strstart..state.strstart + used as usize; |
| 156 |
unsafe { state.window.copy_and_initialize(dst, src) }; |
| 157 |
|
| 158 |
state.strstart += used as usize; |
| 159 |
state.insert += Ord::min(used as usize, state.w_size - state.insert); |
| 160 |
} |
| 161 |
state.block_start = state.strstart as isize; |
| 162 |
} |
| 163 |
|
| 164 |
if last { |
| 165 |
return BlockState::FinishDone; |
| 166 |
} |
| 167 |
|
| 168 |
// If flushing and all input has been consumed, then done. |
| 169 |
if flush != DeflateFlush::NoFlush |
| 170 |
&& flush != DeflateFlush::Finish |
| 171 |
&& stream.avail_in == 0 |
| 172 |
&& stream.state.strstart as isize == stream.state.block_start |
| 173 |
{ |
| 174 |
return BlockState::BlockDone; |
| 175 |
} |
| 176 |
|
| 177 |
// Fill the window with any remaining input |
| 178 |
let mut have = stream.state.window_size - stream.state.strstart; |
| 179 |
if stream.avail_in as usize > have && stream.state.block_start >= stream.state.w_size as isize { |
| 180 |
// slide the window down |
| 181 |
let state = &mut stream.state; |
| 182 |
state.block_start -= state.w_size as isize; |
| 183 |
state.strstart -= state.w_size; |
| 184 |
|
| 185 |
// make sure we don't copy uninitialized bytes. While we discard the first lower w_size |
| 186 |
// bytes, it is not guaranteed that the upper w_size bytes are all initialized |
| 187 |
let copy = Ord::min(state.strstart, state.window.filled().len() - state.w_size); |
| 188 |
|
| 189 |
state |
| 190 |
.window |
| 191 |
.filled_mut() |
| 192 |
.copy_within(state.w_size..state.w_size + copy, 0); |
| 193 |
|
| 194 |
if state.matches < 2 { |
| 195 |
// add a pending slide_hash |
| 196 |
state.matches += 1; |
| 197 |
} |
| 198 |
|
| 199 |
have += state.w_size; // more space now |
| 200 |
state.insert = Ord::min(state.insert, state.strstart); |
| 201 |
} |
| 202 |
|
| 203 |
let have = Ord::min(have, stream.avail_in as usize); |
| 204 |
if have > 0 { |
| 205 |
read_buf_window(stream, stream.state.strstart, have); |
| 206 |
|
| 207 |
let state = &mut stream.state; |
| 208 |
state.strstart += have; |
| 209 |
state.insert += Ord::min(have, state.w_size - state.insert); |
| 210 |
} |
| 211 |
|
| 212 |
// There was not enough avail_out to write a complete worthy or flushed |
| 213 |
// stored block to next_out. Write a stored block to pending instead, if we |
| 214 |
// have enough input for a worthy block, or if flushing and there is enough |
| 215 |
// room for the remaining input as a stored block in the pending buffer. |
| 216 |
|
| 217 |
// number of header bytes |
| 218 |
let state = &mut stream.state; |
| 219 |
let have = ((state.bit_writer.bits_used + 42) >> 3) as usize; |
| 220 |
|
| 221 |
// maximum stored block length that will fit in pending: |
| 222 |
let have = Ord::min(state.bit_writer.pending.capacity() - have, MAX_STORED); |
| 223 |
let min_block = Ord::min(have, state.w_size); |
| 224 |
let left = state.strstart as isize - state.block_start; |
| 225 |
|
| 226 |
if left >= min_block as isize |
| 227 |
|| ((left > 0 || flush == DeflateFlush::Finish) |
| 228 |
&& flush != DeflateFlush::NoFlush |
| 229 |
&& stream.avail_in == 0 |
| 230 |
&& left <= have as isize) |
| 231 |
{ |
| 232 |
let len = Ord::min(left as usize, have); // TODO wrapping? |
| 233 |
last = flush == DeflateFlush::Finish && stream.avail_in == 0 && len == (left as usize); |
| 234 |
|
| 235 |
let range = state.block_start as usize..state.block_start as usize + len; |
| 236 |
zng_tr_stored_block(state, range, last); |
| 237 |
|
| 238 |
state.block_start += len as isize; |
| 239 |
flush_pending(stream); |
| 240 |
} |
| 241 |
|
| 242 |
// We've done all we can with the available input and output. |
| 243 |
if last { |
| 244 |
BlockState::FinishStarted |
| 245 |
} else { |
| 246 |
BlockState::NeedMore |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
fn read_buf_direct_copy(stream: &mut DeflateStream, size: usize) -> usize { |
| 251 |
let len = Ord::min(stream.avail_in as usize, size); |
| 252 |
let output = stream.next_out; |
| 253 |
|
| 254 |
if len == 0 { |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
stream.avail_in -= len as u32; |
| 259 |
|
| 260 |
// SAFETY: len is effectively bounded by next_in and next_out (via size derived in the calling |
| 261 |
// function), so copies are in-bounds. |
| 262 |
if stream.state.wrap == 2 { |
| 263 |
// we likely cannot fuse the crc32 and the copy here because the input can be changed by |
| 264 |
// a concurrent thread. Therefore it cannot be converted into a slice! |
| 265 |
unsafe { core::ptr::copy_nonoverlapping(stream.next_in, output, len) } |
| 266 |
|
| 267 |
let data = unsafe { core::slice::from_raw_parts(output, len) }; |
| 268 |
stream.state.crc_fold.fold(data, 0); |
| 269 |
} else if stream.state.wrap == 1 { |
| 270 |
// we cannot fuse the adler and the copy in our case, because adler32 takes a slice. |
| 271 |
// Another process is allowed to concurrently modify stream.next_in, so we cannot turn it |
| 272 |
// into a rust slice (violates its safety requirements) |
| 273 |
unsafe { core::ptr::copy_nonoverlapping(stream.next_in, output, len) } |
| 274 |
|
| 275 |
let data = unsafe { core::slice::from_raw_parts(output, len) }; |
| 276 |
stream.adler = crate::adler32::adler32(stream.adler as u32, data) as _; |
| 277 |
} else { |
| 278 |
unsafe { core::ptr::copy_nonoverlapping(stream.next_in, output, len) } |
| 279 |
} |
| 280 |
|
| 281 |
stream.next_in = stream.next_in.wrapping_add(len); |
| 282 |
stream.total_in += len as crate::c_api::z_size; |
| 283 |
|
| 284 |
stream.next_out = stream.next_out.wrapping_add(len as _); |
| 285 |
stream.avail_out = stream.avail_out.wrapping_sub(len as _); |
| 286 |
stream.total_out = stream.total_out.wrapping_add(len as _); |
| 287 |
|
| 288 |
len |
| 289 |
} |