use std::cell::RefCell;
use std::io;
use std::sync::atomic::{AtomicUsize, Ordering};
use zlib_rs::{Inflate, InflateError, InflateFlush, Status};
const ZLIB_HEADER: bool = true;
const WINDOW_BITS: u8 = 15;
thread_local! {
static INFLATE_POOL: RefCell<Vec<Inflate>> = const { RefCell::new(Vec::new()) };
}
static POOL_RETENTION: AtomicUsize = AtomicUsize::new(DEFAULT_POOL_RETENTION);
const DEFAULT_POOL_RETENTION: usize = 1;
pub fn set_pool_retention(states_per_thread: usize) {
POOL_RETENTION.store(states_per_thread, Ordering::Relaxed);
}
pub fn warm_pool() {
if POOL_RETENTION.load(Ordering::Relaxed) == 0 {
return;
}
let parked = INFLATE_POOL.with(|p| !p.borrow().is_empty());
if !parked {
park(Inflate::new(ZLIB_HEADER, WINDOW_BITS));
}
}
pub fn drain_pool() {
INFLATE_POOL.with(|p| p.borrow_mut().clear());
}
pub fn parked_states() -> usize {
INFLATE_POOL.with(|p| p.borrow().len())
}
fn checkout() -> Inflate {
INFLATE_POOL.with(|p| p.borrow_mut().pop()).map_or_else(
|| Inflate::new(ZLIB_HEADER, WINDOW_BITS),
|mut decomp| {
decomp.reset(ZLIB_HEADER);
decomp
},
)
}
fn park(decomp: Inflate) {
INFLATE_POOL.with(|p| {
let mut pool = p.borrow_mut();
if pool.len() < POOL_RETENTION.load(Ordering::Relaxed) {
pool.push(decomp);
}
});
}
fn inflate_into_spare(
inflate: &mut Inflate,
input: &[u8],
out: &mut Vec<u8>,
flush: InflateFlush,
) -> Result<Status, InflateError> {
let before = inflate.total_out();
let status = inflate.decompress_uninit(input, out.spare_capacity_mut(), flush)?;
let produced = (inflate.total_out() - before) as usize;
unsafe { out.set_len(out.len() + produced) };
Ok(status)
}
pub struct InflateReader<'a> {
input: &'a [u8],
in_pos: usize,
decomp: Option<Inflate>,
buf: Vec<u8>,
cursor: usize,
chunk: usize,
total_out: u64,
max: u64,
eof: bool,
stream_end: bool,
}
impl<'a> InflateReader<'a> {
pub const CHUNK: usize = 64 * 1024;
pub fn new(input: &'a [u8], max: u64) -> Self {
Self::with_chunk(input, max, Self::CHUNK)
}
pub fn with_chunk(input: &'a [u8], max: u64, chunk: usize) -> Self {
let chunk = chunk.max(1);
Self {
input,
in_pos: 0,
decomp: Some(checkout()),
buf: Vec::with_capacity(chunk),
cursor: 0,
chunk,
total_out: 0,
max,
eof: false,
stream_end: false,
}
}
#[inline]
pub fn available(&self) -> &[u8] {
&self.buf[self.cursor..]
}
#[inline]
pub fn consume(&mut self, n: usize) {
self.cursor = (self.cursor + n).min(self.buf.len());
}
pub fn ensure(&mut self, need: usize) -> io::Result<bool> {
while self.buf.len() - self.cursor < need {
if self.eof {
return Ok(false);
}
self.pump()?;
}
Ok(true)
}
pub fn is_done(&self) -> bool {
self.eof && self.cursor >= self.buf.len()
}
pub fn total_out(&self) -> u64 {
self.total_out
}
#[inline]
pub fn compressed_progress(&self) -> (usize, usize) {
(self.in_pos, self.input.len())
}
pub fn stream_ended(&self) -> bool {
self.stream_end
}
fn pump(&mut self) -> io::Result<()> {
if self.cursor != 0 {
let remaining = self.buf.len() - self.cursor;
self.buf.copy_within(self.cursor.., 0);
self.buf.truncate(remaining);
self.cursor = 0;
}
let decomp = self
.decomp
.as_mut()
.ok_or_else(|| io::Error::other("InflateReader used after pool return"))?;
if self.buf.len() == self.buf.capacity() {
self.buf.reserve(self.chunk);
}
let prev_in = decomp.total_in();
let prev_out = decomp.total_out();
let status = inflate_into_spare(
decomp,
&self.input[self.in_pos..],
&mut self.buf,
InflateFlush::NoFlush,
)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.as_str()))?;
let new_in = decomp.total_in();
let produced = (decomp.total_out() - prev_out) as usize;
self.in_pos += (new_in - prev_in) as usize;
self.total_out += produced as u64;
if self.total_out > self.max {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("decompressed payload exceeds {} bytes", self.max),
));
}
match status {
Status::StreamEnd => {
self.eof = true;
self.stream_end = true;
}
_ if produced == 0 => {
if self.in_pos >= self.input.len() {
self.eof = true;
} else if new_in == prev_in {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"zlib stream stalled (no progress)",
));
}
}
_ => {}
}
Ok(())
}
}
impl InflateReader<'_> {
pub fn read_to_end(mut self) -> io::Result<Vec<u8>> {
while !self.eof {
if self.buf.len() == self.buf.capacity() {
let cap = (self.max as usize).saturating_add(1);
if self.buf.len() >= cap {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("decompressed payload exceeds {} bytes", self.max),
));
}
let decomp = self
.decomp
.as_ref()
.ok_or_else(|| io::Error::other("InflateReader used after pool return"))?;
grow_by_observed_ratio(&mut self.buf, decomp, self.input.len(), cap);
}
self.pump()?;
}
if !self.stream_end {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"zlib stream truncated (no terminator)",
));
}
let mut buf = std::mem::take(&mut self.buf);
if self.cursor != 0 {
buf.drain(..self.cursor);
}
Ok(buf)
}
}
impl Drop for InflateReader<'_> {
fn drop(&mut self) {
if let Some(decomp) = self.decomp.take() {
park(decomp);
}
}
}
fn grow_by_observed_ratio(
scratch: &mut Vec<u8>,
decompressor: &Inflate,
compressed_len: usize,
cap: usize,
) {
let consumed = decompressor.total_in() as usize;
let produced = decompressor.total_out() as usize;
let remaining_in = compressed_len.saturating_sub(consumed) as u64;
let projected = if consumed > 0 && produced > 0 {
((produced as u64).saturating_mul(remaining_in) / consumed as u64).saturating_mul(9) / 8
} else {
0
};
let min_grow = scratch.capacity().max(4096);
let want = (projected.min(usize::MAX as u64) as usize)
.max(min_grow)
.min(cap - scratch.len());
scratch.reserve(want);
}
pub fn decompress_zlib_pooled(compressed: &[u8], max_size: u64) -> io::Result<Vec<u8>> {
let mut decompressor = checkout();
let result = decompress_with(&mut decompressor, compressed, max_size);
park(decompressor);
result
}
fn decompress_with(
decompressor: &mut Inflate,
compressed: &[u8],
max_size: u64,
) -> io::Result<Vec<u8>> {
let cap = (max_size as usize).saturating_add(1);
let floor = 4096.min(cap);
let estimated = compressed.len().saturating_mul(2).clamp(floor, cap);
let mut scratch = Vec::with_capacity(estimated);
let mut input_offset = 0;
loop {
if scratch.len() >= cap {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("decompressed payload exceeds {max_size} bytes"),
));
}
let prev_in = decompressor.total_in();
let prev_out = decompressor.total_out();
let status = inflate_into_spare(
decompressor,
&compressed[input_offset..],
&mut scratch,
InflateFlush::Finish,
)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.as_str()))?;
input_offset = decompressor.total_in() as usize;
if scratch.len() as u64 > max_size {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("decompressed payload exceeds {max_size} bytes"),
));
}
match status {
Status::StreamEnd => break,
Status::Ok => {
grow_by_observed_ratio(&mut scratch, decompressor, compressed.len(), cap);
}
Status::BufError => {
if decompressor.total_in() == prev_in && decompressor.total_out() == prev_out {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"zlib stream truncated (no progress)",
));
}
grow_by_observed_ratio(&mut scratch, decompressor, compressed.len(), cap);
}
}
}
Ok(scratch)
}
#[cfg(test)]
pub(crate) mod test_support {
pub(crate) fn stored_zlib(data: &[u8]) -> Vec<u8> {
let mut out = vec![0x78, 0x01];
let mut blocks = data.chunks(u16::MAX as usize).peekable();
if blocks.peek().is_none() {
out.extend_from_slice(&[0x01, 0, 0, 0xff, 0xff]);
}
while let Some(block) = blocks.next() {
let len = block.len() as u16;
out.push(u8::from(blocks.peek().is_none()));
out.extend_from_slice(&len.to_le_bytes());
out.extend_from_slice(&(!len).to_le_bytes());
out.extend_from_slice(block);
}
let (mut a, mut b) = (1u32, 0u32);
for &byte in data {
a = (a + byte as u32) % 65521;
b = (b + a) % 65521;
}
out.extend_from_slice(&(((b << 16) | a).to_be_bytes()));
out
}
}
#[cfg(test)]
mod tests {
use super::*;
use flate2::Compression;
use flate2::write::ZlibEncoder;
use std::io::Write;
fn zlib(data: &[u8]) -> Vec<u8> {
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(data).unwrap();
e.finish().unwrap()
}
fn varied(n: usize) -> Vec<u8> {
let mut s: u64 = 0x9e37_79b9_7f4a_7c15;
(0..n)
.map(|_| {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
(s >> 24) as u8
})
.collect()
}
use super::test_support::stored_zlib;
#[test]
fn pooled_roundtrip_small_input() {
let original = varied(1024);
let compressed = stored_zlib(&original);
assert_eq!(
decompress_zlib_pooled(&compressed, 64 * 1024).unwrap(),
original
);
assert_eq!(drain_reader(&compressed, original.len()), original);
}
#[test]
#[cfg_attr(miri, ignore)]
fn inflate_reader_roundtrip_across_chunks() {
let original = varied(200 * 1024);
let compressed = zlib(&original);
let mut r = InflateReader::new(&compressed, 64 * 1024 * 1024);
let mut out = Vec::with_capacity(original.len());
while r.ensure(1).unwrap() {
let n = r.available().len().min(7);
out.extend_from_slice(&r.available()[..n]);
r.consume(n);
}
assert!(r.is_done());
assert_eq!(out, original);
}
#[test]
#[cfg_attr(miri, ignore)]
fn inflate_reader_ensure_larger_than_chunk() {
let original: Vec<u8> = (0..150 * 1024).map(|i| (i % 256) as u8).collect();
let compressed = zlib(&original);
let mut r = InflateReader::new(&compressed, 64 * 1024 * 1024);
assert!(r.ensure(150 * 1024).unwrap());
assert_eq!(&r.available()[..150 * 1024], &original[..]);
}
#[test]
#[cfg_attr(miri, ignore)]
fn inflate_reader_keeps_one_window_for_smaller_records() {
const RECORD: usize = 30 * 1024;
let original = varied(RECORD * 8);
let compressed = zlib(&original);
let mut r = InflateReader::new(&compressed, 64 * 1024 * 1024);
for expected in original.chunks(RECORD) {
assert!(r.ensure(expected.len()).unwrap());
assert_eq!(&r.available()[..expected.len()], expected);
r.consume(expected.len());
assert!(
r.buf.capacity() <= InflateReader::CHUNK,
"sub-window records grew the inflate buffer to {} bytes",
r.buf.capacity()
);
}
assert!(!r.ensure(1).unwrap());
assert!(r.is_done());
}
#[test]
#[cfg_attr(miri, ignore)]
fn inflate_reader_enforces_max() {
let original = vec![0u8; 1024 * 1024];
let compressed = zlib(&original);
let mut r = InflateReader::new(&compressed, 4096);
assert!(r.ensure(1024 * 1024).is_err());
}
#[test]
#[cfg_attr(miri, ignore)]
fn pooled_high_ratio_stream_roundtrips() {
let original: Vec<u8> = (0..4_000_000u32).map(|i| ((i / 1024) % 7) as u8).collect();
let compressed = zlib(&original);
assert!(
compressed.len() < original.len() / 20,
"fixture not high-ratio"
);
let out = decompress_zlib_pooled(&compressed, 64 * 1024 * 1024).unwrap();
assert_eq!(out, original);
assert!(
out.capacity() < original.len() * 2,
"capacity {} vs data {}",
out.capacity(),
original.len()
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn pooled_oneshot_matches_streaming() {
let original = varied(100_000);
let compressed = zlib(&original);
let one_shot = decompress_zlib_pooled(&compressed, 64 * 1024 * 1024).unwrap();
assert_eq!(one_shot, original);
}
fn drain_reader(compressed: &[u8], n: usize) -> Vec<u8> {
let mut r = InflateReader::new(compressed, 64 * 1024 * 1024);
let mut out = Vec::with_capacity(n);
while r.ensure(1).unwrap() {
let take = r.available().len();
out.extend_from_slice(r.available());
r.consume(take);
}
assert!(r.is_done());
out
}
#[test]
#[cfg_attr(miri, ignore)]
fn inflate_reader_reuses_pool_state_correctly() {
for n in [10_000usize, 250_000, 1, 80_000] {
let original = varied(n);
assert_eq!(drain_reader(&zlib(&original), n), original, "size {n}");
}
}
#[test]
#[cfg_attr(miri, ignore)]
fn inflate_reader_reuse_after_error() {
{
let compressed = zlib(&varied(500_000));
let mut r = InflateReader::new(&compressed, 4096);
assert!(r.ensure(500_000).is_err());
}
let original = varied(120_000);
assert_eq!(drain_reader(&zlib(&original), 120_000), original);
}
#[test]
#[cfg_attr(miri, ignore)]
fn oversized_window_is_not_retained_for_the_thread() {
let big = varied(2 * 1024 * 1024);
let compressed = zlib(&big);
{
let mut r = InflateReader::new(&compressed, 64 * 1024 * 1024);
assert!(r.ensure(big.len()).unwrap());
assert!(r.buf.capacity() >= big.len(), "buf should grow while alive");
}
let next = InflateReader::new(&compressed, 64 * 1024 * 1024);
assert!(
next.buf.capacity() <= InflateReader::CHUNK,
"fresh reader inherited a {} byte window",
next.buf.capacity()
);
}
#[test]
fn truncated_stream_reports_missing_terminator() {
let original = varied(1024);
let compressed = stored_zlib(&original);
let truncated = &compressed[..compressed.len() - 300];
let mut r = InflateReader::new(truncated, 64 * 1024);
while r.ensure(1).unwrap() {
let n = r.available().len();
r.consume(n);
}
assert!(!r.stream_ended(), "truncated input reported a terminator");
assert!(r.is_done());
drop(r);
assert_eq!(drain_reader(&compressed, original.len()), original);
}
#[test]
fn corrupt_stream_errors_without_poisoning_the_pool() {
let original = varied(1024);
let mut compressed = stored_zlib(&original);
compressed[5] ^= 0xff;
let mut r = InflateReader::new(&compressed, 64 * 1024);
let err = r.ensure(1).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
drop(r);
let good = stored_zlib(&original);
assert_eq!(drain_reader(&good, original.len()), original);
}
static POOL_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
#[cfg_attr(miri, ignore)]
fn one_shot_and_streaming_inflate_share_one_parked_state() {
let _guard = POOL_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
drain_pool();
let original = varied(50_000);
let compressed = zlib(&original);
assert_eq!(
decompress_zlib_pooled(&compressed, 1 << 20).unwrap(),
original
);
assert_eq!(parked_states(), 1);
assert_eq!(drain_reader(&compressed, original.len()), original);
assert_eq!(parked_states(), 1);
assert_eq!(
decompress_zlib_pooled(&compressed, 1 << 20).unwrap(),
original
);
assert_eq!(
parked_states(),
1,
"a second path must not park a second state"
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn retention_zero_parks_nothing() {
let _guard = POOL_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
drain_pool();
set_pool_retention(0);
let original = varied(1024);
let compressed = stored_zlib(&original);
warm_pool();
assert_eq!(parked_states(), 0);
assert_eq!(
decompress_zlib_pooled(&compressed, 64 * 1024).unwrap(),
original
);
assert_eq!(parked_states(), 0);
assert_eq!(drain_reader(&compressed, original.len()), original);
assert_eq!(parked_states(), 0);
set_pool_retention(DEFAULT_POOL_RETENTION);
warm_pool();
assert_eq!(parked_states(), 1);
warm_pool();
assert_eq!(parked_states(), 1, "warming twice must not park twice");
drain_pool();
assert_eq!(parked_states(), 0);
}
#[test]
#[cfg_attr(miri, ignore)]
fn read_to_end_after_a_peek_matches_the_one_shot_result() {
let original = varied(300_000);
let compressed = zlib(&original);
let mut r = InflateReader::with_chunk(&compressed, 1 << 24, 4096);
assert!(r.ensure(100).unwrap());
assert_eq!(&r.available()[..100], &original[..100]);
let whole = r.read_to_end().unwrap();
assert_eq!(whole, original);
assert_eq!(whole, decompress_zlib_pooled(&compressed, 1 << 24).unwrap());
assert!(
whole.capacity() < original.len() * 2,
"drain grew by doubling ({}) instead of by the observed ratio",
whole.capacity()
);
let mut r = InflateReader::with_chunk(&compressed, 1 << 24, 4096);
assert!(r.ensure(100).unwrap());
r.consume(100);
assert_eq!(r.read_to_end().unwrap(), &original[100..]);
}
#[test]
fn read_to_end_rejects_a_truncated_stream_and_the_max() {
let original = varied(1024);
let compressed = stored_zlib(&original);
let truncated = &compressed[..compressed.len() - 300];
let err = InflateReader::with_chunk(truncated, 64 * 1024, 256)
.read_to_end()
.unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
let err = InflateReader::with_chunk(&compressed, 512, 256)
.read_to_end()
.unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
assert_eq!(
InflateReader::with_chunk(&compressed, 64 * 1024, 256)
.read_to_end()
.unwrap(),
original
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn a_small_chunk_keeps_small_records_within_the_chunk() {
const RECORD: usize = 100;
const CHUNK: usize = 4096;
let original = varied(RECORD * 2000);
let compressed = zlib(&original);
let mut r = InflateReader::with_chunk(&compressed, 1 << 24, CHUNK);
for expected in original.chunks(RECORD) {
assert!(r.ensure(expected.len()).unwrap());
assert_eq!(&r.available()[..expected.len()], expected);
r.consume(expected.len());
assert!(
r.buf.capacity() <= CHUNK,
"window grew to {}",
r.buf.capacity()
);
}
assert!(!r.ensure(1).unwrap());
}
}