feat(domain): R4.2b inflate_flate on miniz_oxide with DecompressionStream verdicts (O9 26/26)

Non-wrapping output buffer growing to limit + 1, trailing bytes refused, Adler-32 checked;
12 flate rows of O7 and the browser columns agree.
This commit is contained in:
bchanot committed 2026-10-18 15:44:53 +02:00
1 parent a3fc44fd3a
commit 55819fbd32
2 files changed
+206 -2

No files matched your search

+72 -1
View File
@@ -1 +1,72 @@
//! R4.2b: `inflateFlate` of inflate.ts on miniz_oxide. Written by the R4.2b track.
//! FlateDecode under a cap (port of `inflateFlate`, `packages/pdf/src/attachment/inflate.ts`).
//!
//! The TypeScript fed `DecompressionStream('deflate')` (zlib framing) and answered null on any
//! stream error; Node, Chromium, Firefox and WebKit give the same verdicts (O9,
//! `tests/oracles/r4/inflate.json` and `inflate.browsers.json`): the zlib header is required
//! (CM 8, FCHECK, no FDICT, a window up to 32 KiB), the Adler-32 trailer is required and
//! checked, a truncated stream fails, and the output is refused as soon as it exceeds the
//! limit.
//!
//! The inflater is miniz_oxide's `decompress` over one growing, non-wrapping output buffer.
//! Non-wrapping matters: in that mode a match reaching before the first output byte is
//! refused, as zlib refuses it ("invalid distance too far back"); the wrapping 32 KiB window
//! of the streaming API would read stale window bytes instead. The buffer doubles from
//! `INITIAL_OUTPUT_BYTES` up to `limit + 1` and never beyond, so a bomb stops once that
//! much output exists: the work and the memory spent before the refusal are bounded by the
//! limit, whatever the bomb's expansion.
use miniz_oxide::inflate::TINFLStatus;
use miniz_oxide::inflate::core::{DecompressorOxide, decompress, inflate_flags};
/// First output allocation: a report archive inflates to a few hundred KB, a small payload
/// never pays for the whole limit.
const INITIAL_OUTPUT_BYTES: usize = 64 * 1024;
/// zlib framing (header parsed, Adler-32 computed and checked) and the whole output kept in
/// one buffer, which is what lets matches be checked against the output start.
const FLAGS: u32 = inflate_flags::TINFL_FLAG_PARSE_ZLIB_HEADER
| inflate_flags::TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
/// The FlateDecode (zlib framing) output of `bytes` when it holds at most `limit` bytes.
/// `None` on any failure (empty, truncated or corrupt stream, bad header, missing or wrong
/// Adler-32, trailing bytes) or as soon as the output exceeds `limit`; an output of exactly
/// `limit` bytes is returned.
pub fn inflate_flate(bytes: &[u8], limit: usize) -> Option<Vec<u8>> {
let cap = limit.saturating_add(1);
let mut out = vec![0; INITIAL_OUTPUT_BYTES.min(cap)];
let mut decompressor = Box::<DecompressorOxide>::default();
let mut rest = bytes;
let mut written = 0;
loop {
let (status, read, produced) =
decompress(&mut decompressor, rest, &mut out, written, FLAGS);
written += produced;
rest = rest.get(read..)?;
match status {
TINFLStatus::Done => return finish(out, written, rest, limit),
TINFLStatus::HasMoreOutput => grow(&mut out, cap)?,
_ => return None,
}
}
}
/// The output of a completed stream. DecompressionStream errors on any byte after the stream
/// end (one 0x00, a second stream): a PDF stream holds exactly one zlib stream, and data
/// after it is not part of the attachment.
fn finish(mut out: Vec<u8>, written: usize, rest: &[u8], limit: usize) -> Option<Vec<u8>> {
if !rest.is_empty() || written > limit {
return None;
}
out.truncate(written);
Some(out)
}
/// Doubles the output buffer up to `cap`; `None` when it is already full at `cap`, that is
/// when the output exceeds the limit.
fn grow(out: &mut Vec<u8>, cap: usize) -> Option<()> {
if out.len() >= cap {
return None;
}
out.resize(out.len().saturating_mul(2).min(cap), 0);
Some(())
}