Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge #4114 (fix/coderabbit-b-shared) into fix/coderabbit-c4-core

aiongg0e643d45a03e
diff --cc crates/cos/src/object.rs
index ee3c49a78,9a07d4822..d11d94711
--- a/crates/cos/src/object.rs
+++ b/crates/cos/src/object.rs
@@@ -258,21 -204,6 +258,21 @@@ impl Stream
.collect()
}
+ /// [`Stream::filters`], or an error when `/Filter` is present but is not a name or an
+ /// array of names (an indirect reference, say): the data is not unfiltered, so it must not
+ /// read as decoded.
- fn readable_filters(&self) -> Result<Vec<(Filter, Params)>, CosError> {
++ fn readable_filters(&self) -> Result<Vec<(Filter, Params)>, pdfcraft_filters::FilterError> {
+ let readable = match self.dict.get(b"Filter") {
+ None | Some(Object::Null | Object::Name(_)) => true,
+ Some(Object::Array(a)) => a.iter().all(|f| f.as_name().is_some()),
+ Some(_) => false,
+ };
+ if !readable {
- return Err(CosError::Filter("/Filter is not a name or an array of names".into()));
++ return Err(pdfcraft_filters::FilterError::Unsupported("/Filter is not a name or an array of names".into()));
+ }
+ Ok(self.filters())
+ }
+
/// Decoded data, tolerating truncated or corrupt encodings the way viewers do (the data
/// decoded before the damage is returned). Streams with image codecs fail with
/// `CosError::Filter` (keep them encoded). Use `decoded_strict` to detect damage.
@@@ -283,26 -214,39 +283,39 @@@
/// 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_typed(max).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.
+ pub(crate) fn decoded_within_typed(&self, max: usize) -> Result<Vec<u8>, pdfcraft_filters::FilterError> {
+ let max = max.min(MAX_DECODED);
- let chain = self.filters();
+ let chain = self.readable_filters()?;
if chain.is_empty() {
- return self.raw_within(max);
+ return self.raw_within_typed(max);
}
- pdfcraft_filters::decode_tolerant(&chain, &self.raw, max.min(MAX_DECODED)).map(|(v, _)| v).map_err(|e| CosError::Filter(e.to_string()))
+ pdfcraft_filters::decode_tolerant(&chain, &self.raw, max).map(|(v, _)| v)
}
/// Decoded data, bounded by [`MAX_DECODED`]; any corruption is an error.
pub fn decoded_strict(&self) -> Result<Vec<u8>, CosError> {
+ self.decoded_strict_within_typed(MAX_DECODED).map_err(|e| CosError::Filter(e.to_string()))
+ }
+
+ /// [`Stream::decoded_strict`] bounded by `max`, with the filter error kept typed.
+ pub(crate) fn decoded_strict_within_typed(&self, max: usize) -> Result<Vec<u8>, pdfcraft_filters::FilterError> {
+ let max = max.min(MAX_DECODED);
- let chain = self.filters();
+ let chain = self.readable_filters()?;
if chain.is_empty() {
- return self.raw_within(MAX_DECODED);
+ return self.raw_within_typed(max);
}
- pdfcraft_filters::decode(&chain, &self.raw, MAX_DECODED).map_err(|e| CosError::Filter(e.to_string()))
+ pdfcraft_filters::decode(&chain, &self.raw, max)
}
/// Data after the first filter alone, bounded by `max`: the rest of the chain stays encoded.
/// Any corruption in that first filter is an error.
pub fn decoded_first_filter_strict(&self, max: usize) -> Result<Vec<u8>, CosError> {
- let mut chain = self.readable_filters()?;
- let mut chain = self.filters();
++ let mut chain = self.readable_filters().map_err(|e| CosError::Filter(e.to_string()))?;
chain.truncate(1);
if chain.is_empty() {
return self.raw_within(max);
diff --cc crates/print/src/lib.rs
index 5f81d1f93,a6337026b..9d47eb1bf
--- a/crates/print/src/lib.rs
+++ b/crates/print/src/lib.rs
@@@ -779,10 -780,15 +783,15 @@@ fn page_form
content: Content,
promoted: &mut std::collections::HashMap<ObjRef, ObjRef>,
) -> Result<ObjRef, PrintError> {
- let page = pdfcraft_model::pages(doc).swap_remove(index);
+ let page = pages.get(index).ok_or_else(|| PrintError::Invalid(format!("there is no page {}", index.saturating_add(1))))?;
let crop = page.crop(doc);
let mut res = page.dict.get(b"Resources").and_then(|r| doc.resolve(r).as_dict().cloned()).unwrap_or_default();
- let mut body = if content == Content::FormFieldsOnly { Vec::new() } else { decoded_contents(doc, &page.dict) };
+ let mut body = if content == Content::FormFieldsOnly {
+ Vec::new()
+ } else {
+ decoded_contents(doc, &page.dict, &mut DecodeBudget::page())
+ .ok_or_else(|| PrintError::Invalid(format!("page {} has more content than can be printed", index + 1)))?
+ };
// Wrap the page content so a state it leaves behind can't affect the annotations.
if !body.is_empty() {
let mut wrapped = b"q\n".to_vec();