PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
A counted decode bounds all filter stages together and counts output before the predictor
aiongg03366c3c7072
diff --git a/crates/cos/src/object.rs b/crates/cos/src/object.rsindex c09a8ffe3..c9b2ff9a3 100644--- a/crates/cos/src/object.rs+++ b/crates/cos/src/object.rs@@ -283,13 +283,30 @@ impl Stream {/// Like [`Stream::decoded`], for a stream whose decoded size has a tighter bound than/// [`MAX_DECODED`]: decoding past `max` bytes is an error.pub fn decoded_within(&self, max: usize) -> Result<Vec<u8>, CosError> {- self.decoded_within_counted(max).map(|(v, _)| v).map_err(|(e, _)| CosError::Filter(e.to_string()))+ let decode = |chain: &[(Filter, Params)], data: &[u8], max| {+ pdfcraft_filters::decode_tolerant(chain, data, max).map(|(v, _)| (v, 0)).map_err(|e| (e, 0))+ };+ self.decode_chain(max, decode).map(|(v, _)| v).map_err(|(e, _)| CosError::Filter(e.to_string()))}- /// [`Stream::decoded_within`] with the filter error kept typed, so a caller can tell a- /// spent limit from corrupt data, and the bytes every filter stage decoded (the work done,- /// on success and on failure).+ /// [`Stream::decoded_within`] for a caller charging an allowance: the filter error is kept+ /// typed, so a caller can tell a spent limit from corrupt data, `max` bounds all filter+ /// stages together, and the result gives the bytes they decoded (the work done, on success+ /// and on failure).pub(crate) fn decoded_within_counted(&self, max: usize) -> Result<(Vec<u8>, usize), (pdfcraft_filters::FilterError, usize)> {+ let decode = |chain: &[(Filter, Params)], data: &[u8], max| {+ pdfcraft_filters::decode_tolerant_counted(chain, data, max).map(|(v, _, spent)| (v, spent))+ };+ self.decode_chain(max, decode)+ }++ /// The stream decoded through its filters by `decode`, or copied when it has none, within+ /// `max` and [`MAX_DECODED`].+ fn decode_chain(+ &self,+ max: usize,+ decode: impl FnOnce(&[(Filter, Params)], &[u8], usize) -> Result<(Vec<u8>, usize), (pdfcraft_filters::FilterError, usize)>,+ ) -> Result<(Vec<u8>, usize), (pdfcraft_filters::FilterError, usize)> {let max = max.min(MAX_DECODED);let chain = self.readable_filters().map_err(|e| (e, 0))?;if chain.is_empty() {@@ -301,28 +318,19 @@ impl Stream {}).map_err(|e| (e, 0));}- pdfcraft_filters::decode_tolerant_counted(&chain, &self.raw, max).map(|(v, _, spent)| (v, spent))+ decode(&chain, &self.raw, max)}/// Decoded data, bounded by [`MAX_DECODED`]; any corruption is an error.pub fn decoded_strict(&self) -> Result<Vec<u8>, CosError> {- self.decoded_strict_within_counted(MAX_DECODED).map(|(v, _)| v).map_err(|(e, _)| CosError::Filter(e.to_string()))+ let decode = |chain: &[(Filter, Params)], data: &[u8], max| pdfcraft_filters::decode(chain, data, max).map(|v| (v, 0)).map_err(|e| (e, 0));+ self.decode_chain(MAX_DECODED, decode).map(|(v, _)| v).map_err(|(e, _)| CosError::Filter(e.to_string()))}- /// [`Stream::decoded_strict`] bounded by `max`, counted like [`Stream::decoded_within_counted`].+ /// [`Stream::decoded_strict`] bounded by `max` and counted like+ /// [`Stream::decoded_within_counted`].pub(crate) fn decoded_strict_within_counted(&self, max: usize) -> Result<(Vec<u8>, usize), (pdfcraft_filters::FilterError, usize)> {- let max = max.min(MAX_DECODED);- let chain = self.readable_filters().map_err(|e| (e, 0))?;- if chain.is_empty() {- return self- .raw_within_typed(max)- .map(|v| {- let n = v.len();- (v, n)- })- .map_err(|e| (e, 0));- }- pdfcraft_filters::decode_counted(&chain, &self.raw, max)+ self.decode_chain(max, pdfcraft_filters::decode_counted)}/// Data after the first filter alone, bounded by `max`: the rest of the chain stays encoded.diff --git a/crates/filters/README.md b/crates/filters/README.mdindex be5151733..959180d87 100644--- a/crates/filters/README.md+++ b/crates/filters/README.md@@ -27,7 +27,7 @@ encode_flate(data) -> Vec<u8> // zlib, level 6- `chain` is applied first to last, as in a `/Filter` array.- Image codecs (DCT, JPX, JBIG2, CCITT), `Crypt` and unknown filters anywhere in a chain give `Unsupported`: callers keep such data encoded. So does a chain longer than `MAX_CHAIN` (8) filters, before any stage runs. Decryption belongs to `crypt`.- `max_output` bounds every intermediate buffer (including the pre-predictor buffer) and gives `LimitExceeded(max_output)`. This also holds when Flate retries raw deflate after a misleading zlib header, in both strict and tolerant decoding.-- `decode_counted` and `decode_tolerant_counted` also give the bytes every stage that ran decoded, on success and on failure, so a caller charging a budget (`pdfcraft_cos::DecodeBudget`) pays for the work: a stage's output counts even when a later stage shrinks it, and a failed decode pays for what it decoded first.+- `decode_counted` and `decode_tolerant_counted` are for a caller charging a budget (`pdfcraft_cos::DecodeBudget`). `max_output` bounds all stages together (each stage gets what the earlier ones left), and they give the bytes every stage that ran decoded, on success and on failure. A stage's output counts as its decoder produced it, before a predictor or a later stage shrinks it, and a failed decode pays for what it decoded first.- `decode_tolerant` turns `Corrupt` into "what was decoded so far" plus `partial = true`, then runs the rest of the chain on it (predictors are applied to partial output too). `LimitExceeded` and `Unsupported` are still errors.## Behaviourdiff --git a/crates/filters/src/lib.rs b/crates/filters/src/lib.rsindex 11dc473d3..22a78aefd 100644--- a/crates/filters/src/lib.rs+++ b/crates/filters/src/lib.rs@@ -140,27 +140,27 @@ pub(crate) type Step = Result<Vec<u8>, Failure>;/// [`FilterError::Unsupported`] (callers keep such data encoded), as does a chain longer than/// [`MAX_CHAIN`]. `max_output` bounds every intermediate buffer.pub fn decode(chain: &[(Filter, Params)], data: &[u8], max_output: usize) -> Result<Vec<u8>, FilterError> {- decode_counted(chain, data, max_output).map(|(v, _)| v).map_err(|(e, _)| e)+ run(chain, data, max_output, true, false).map(|(v, _, _)| v).map_err(|(e, _)| e)}-/// Like [`decode`], also giving the bytes every stage that ran decoded, on success and on-/// failure: the work a caller charging an allowance spent. A stage's output counts even when a-/// later stage shrinks it.+/// Like [`decode`] for a caller charging an allowance: `max_output` bounds the bytes all stages+/// decode together, each stage getting what the earlier ones left, and the result gives those+/// bytes, on success and on failure. A stage's output counts even when a later stage or its+/// predictor shrinks it.pub fn decode_counted(chain: &[(Filter, Params)], data: &[u8], max_output: usize) -> Result<(Vec<u8>, usize), (FilterError, usize)> {- run(chain, data, max_output, true).map(|(v, _, spent)| (v, spent))+ run(chain, data, max_output, true, true).map(|(v, _, spent)| (v, spent))}/// Like [`decode`], but on corrupt data returns what was decoded before the error and/// reports `partial = true` (the behaviour viewers expect). The remaining filters of the/// chain still run on the partial data. Limit and unsupported errors are still errors.pub fn decode_tolerant(chain: &[(Filter, Params)], data: &[u8], max_output: usize) -> Result<(Vec<u8>, bool), FilterError> {- decode_tolerant_counted(chain, data, max_output).map(|(v, partial, _)| (v, partial)).map_err(|(e, _)| e)+ run(chain, data, max_output, false, false).map(|(v, partial, _)| (v, partial)).map_err(|(e, _)| e)}-/// [`decode_tolerant`], also giving the bytes every stage that ran decoded, as-/// [`decode_counted`] does.+/// [`decode_tolerant`] bounded and counted as [`decode_counted`] is.pub fn decode_tolerant_counted(chain: &[(Filter, Params)], data: &[u8], max_output: usize) -> Result<(Vec<u8>, bool, usize), (FilterError, usize)> {- run(chain, data, max_output, false)+ run(chain, data, max_output, false, true)}/// Longest filter chain decoded. Real streams use one to three filters; every stage may produce@@ -168,8 +168,9 @@ pub fn decode_tolerant_counted(chain: &[(Filter, Params)], data: &[u8], max_outppub const MAX_CHAIN: usize = 8;/// The decoded data, whether it is partial and the bytes all stages decoded, or the error and-/// the bytes decoded before it.-fn run(chain: &[(Filter, Params)], data: &[u8], max: usize, strict: bool) -> Result<(Vec<u8>, bool, usize), (FilterError, usize)> {+/// the bytes decoded before it. `max` bounds each stage, or with `cumulative` all of them+/// together.+fn run(chain: &[(Filter, Params)], data: &[u8], max: usize, strict: bool, cumulative: bool) -> Result<(Vec<u8>, bool, usize), (FilterError, usize)> {if chain.len() > MAX_CHAIN {return Err((FilterError::Unsupported(format!("a chain of {} filters", chain.len())), 0));}@@ -182,33 +183,49 @@ fn run(chain: &[(Filter, Params)], data: &[u8], max: usize, strict: bool) -> Resfor (filter, params) in chain {#[cfg(test)]STAGE_BYTES.with(|c| c.set(c.get() + cur.len()));- cur = Cow::Owned(match decode_one(filter, params, &cur, max) {+ let limit = if cumulative { max.saturating_sub(spent) } else { max };+ let (step, work) = decode_one(filter, params, &cur, limit);+ spent = spent.saturating_add(work);+ cur = Cow::Owned(match step {Ok(v) => v,Err(Failure { error: FilterError::Corrupt { .. }, partial: out }) if !strict => {partial = true;out}- Err(f) => return Err((f.error, spent.saturating_add(f.partial.len()))),+ Err(Failure { error: FilterError::LimitExceeded(_), .. }) => return Err((FilterError::LimitExceeded(max), spent)),+ Err(f) => return Err((f.error, spent)),});- spent = spent.saturating_add(cur.len());}Ok((cur.into_owned(), partial, spent))}-fn decode_one(filter: &Filter, params: &Params, data: &[u8], max: usize) -> Step {+/// One stage's result and the bytes its decoder produced (before any predictor).+fn decode_one(filter: &Filter, params: &Params, data: &[u8], max: usize) -> (Step, usize) {match filter {Filter::Flate => with_predictor(flate::decode(data, max), params, "FlateDecode"),Filter::Lzw => with_predictor(lzw::decode(data, params.early_change != 0, max), params, "LZWDecode"),- Filter::AsciiHex => asciihex::decode(data, max),- Filter::Ascii85 => ascii85::decode(data, max),- Filter::RunLength => runlength::decode(data, max),- other => Err(Failure { error: FilterError::Unsupported(other.name().to_owned()), partial: Vec::new() }),+ Filter::AsciiHex => counted(asciihex::decode(data, max)),+ Filter::Ascii85 => counted(ascii85::decode(data, max)),+ Filter::RunLength => counted(runlength::decode(data, max)),+ other => (Err(Failure { error: FilterError::Unsupported(other.name().to_owned()), partial: Vec::new() }), 0),}}-/// Applies the predictor to a decoder's result, including to partial output on failure.-fn with_predictor(step: Step, params: &Params, name: &'static str) -> Step {- match step {+/// A step with the bytes it produced, its output or its partial output.+fn counted(step: Step) -> (Step, usize) {+ let n = match &step {+ Ok(v) => v.len(),+ Err(f) => f.partial.len(),+ };+ (step, n)+}++/// Applies the predictor to a decoder's result, including to partial output on failure. The+/// count is the decoder's output: a PNG predictor drops a tag byte per row, but the decoder+/// produced it.+fn with_predictor(step: Step, params: &Params, name: &'static str) -> (Step, usize) {+ let (step, n) = counted(step);+ let step = match step {Ok(v) => predictor::decode(params, v, name),Err(Failure { error, partial }) => {let partial = match predictor::decode(params, partial, name) {@@ -217,7 +234,8 @@ fn with_predictor(step: Step, params: &Params, name: &'static str) -> Step {};Err(Failure { error, partial })}- }+ };+ (step, n)}/// Encodes `data` with `filter`. Supported: Flate (zlib, level 6), LZW, ASCIIHex,@@ -388,4 +406,35 @@ mod tests {assert!(partial);assert_eq!(&data[..out.len()], &out[..]);}++ /// A counted decode bounds what all stages decode together: Flate inflating 1 MiB of hex+ /// that ASCIIHex halves is 1.5 MiB of work, past a 1.25 MiB allowance. A plain decode keeps+ /// the limit per stage.+ #[test]+ fn a_counted_decode_bounds_all_stages_together() {+ let hex: Vec<u8> = std::iter::repeat_n(b"ab".as_slice(), 1 << 19).flatten().copied().collect();+ let data = encode_flate(&hex);+ let chain = [(Filter::Flate, Params::default()), (Filter::AsciiHex, Params::default())];+ let max = (1 << 20) + (1 << 18);+ assert_eq!(decode(&chain, &data, max).map(|d| d.len()).ok(), Some(1 << 19));+ assert!(matches!(decode_counted(&chain, &data, max), Err((FilterError::LimitExceeded(m), _)) if m == max));+ assert_eq!(decode_counted(&chain, &data, 2 << 20).map(|(_, spent)| spent).ok(), Some((1 << 20) + (1 << 19)));+ }++ /// A PNG predictor drops a tag byte per row, but Flate produced it: the count includes it,+ /// also when the predictor fails on a bad tag.+ #[test]+ fn a_counted_decode_counts_output_before_the_predictor() {+ let params = Params { predictor: 12, ..Params::default() };+ let rows = vec![7u8; 1 << 16];+ let data = encode(&Filter::Flate, ¶ms, &rows).unwrap();+ let chain = [(Filter::Flate, params.clone())];+ let (out, spent) = decode_counted(&chain, &data, usize::MAX).unwrap();+ assert!(out == rows);+ assert_eq!(spent, 2 << 16);+ let bad: Vec<u8> = std::iter::repeat_n([0u8, 7], 1 << 15).flatten().chain([9, 7]).collect();+ let (e, spent) = decode_counted(&chain, &encode_flate(&bad), usize::MAX).unwrap_err();+ assert!(matches!(e, FilterError::Corrupt { .. }), "{e}");+ assert_eq!(spent, bad.len());+ }}