Skip to content

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

Merge upstream main into prep/4013-merge-main-2

Cursor Agentc580b5505558
diff --cc crates/cos/src/document/open.rs
index 000000000,c90d5c090..8b9b914ce
mode 000000,100644..100644
--- a/crates/cos/src/document/open.rs
+++ b/crates/cos/src/document/open.rs
@@@ -1,0 -1,344 +1,343 @@@
-use super::reader::{find, generated_id};
++use super::reader::find;
+ use super::state::Credentials;
+ use super::xref::XrefIndex;
+ use super::*;
+
+ impl Document {
+ /// A new document with an empty page tree (for split / extract / combine). It has no
+ /// original bytes, so saving always writes a full file.
+ pub fn new_empty() -> Self {
+ let mut doc = Document {
+ data: Arc::new(Vec::new()),
+ endstreams: Arc::default(),
+ source_identity: Arc::new(()),
+ entries: Arc::new(XrefIndex::empty()),
+ trailer: Dict::new(),
+ revisions: Arc::new(Vec::new()),
+ xref_oddities: Arc::default(),
+ located: Arc::default(),
+ repair_log: Arc::new(Vec::new()),
+ cache: Arc::default(),
+ objstms: Arc::default(),
+ stream_limit: crate::object::MAX_DECODED,
+ overlay: BTreeMap::new(),
+ next_num: 1,
+ header_offset: 0,
+ version: "1.7".into(),
+ security: None,
+ encrypt_num: None,
+ encryption_changed: false,
+ full_save: false,
+ out_security: None,
+ out_encrypt_num: None,
+ };
+ let mut pages = Dict::new();
+ pages.set(b"Type".to_vec(), Object::name("Pages"));
+ pages.set(b"Kids".to_vec(), Object::Array(Vec::new()));
+ pages.set(b"Count".to_vec(), Object::Int(0));
+ let pages = doc.add(pages);
+ let mut catalog = Dict::new();
+ catalog.set(b"Type".to_vec(), Object::name("Catalog"));
+ catalog.set(b"Pages".to_vec(), Object::Ref(pages));
+ let root = doc.add(catalog);
+ doc.trailer.set(b"Root".to_vec(), Object::Ref(root));
+ doc
+ }
+
+ /// Parse a document. Damaged cross-reference data is reconstructed; see `repair_log`.
+ /// Encrypted documents open only if their user password is empty; see `open_with_password`.
+ pub fn open(data: Arc<Vec<u8>>) -> Result<Self, CosError> {
+ Self::open_with_password(data, None)
+ }
+
+ /// Parse a document, authenticating encrypted ones with `password` (owner or user; `None`
+ /// tries the empty password). Fails with `NeedsPassword` / `WrongPassword` as appropriate.
+ pub fn open_with_password(data: Arc<Vec<u8>>, password: Option<&str>) -> Result<Self, CosError> {
+ Self::open_with_stream_limit(data, password, crate::object::MAX_DECODED)
+ }
+
+ /// Open with a tighter decompression bound for object and cross-reference streams.
+ /// Inspection can use this without changing the editing reader's default limit. Such a
+ /// document is read-only: objects in a larger object stream read as missing, so the
+ /// writers refuse to save it ([`CosError::ReadOnlyLimit`]).
+ pub fn open_with_stream_limit(data: Arc<Vec<u8>>, password: Option<&str>, stream_limit: usize) -> Result<Self, CosError> {
+ Self::open_as(data, Credentials::Password(password), stream_limit)
+ }
+
+ /// Open `data`, an earlier revision of this file (the bytes a signature covers) or another
+ /// save of it, with this document's credentials: an encrypted one must have the same
+ /// `/Encrypt` dictionary and `/ID` as this document, whose file key is then reused, so no
+ /// password is needed or kept; unencrypted data opens as [`open`](Self::open) does. Data
+ /// encrypted differently fails with `NeedsPassword`.
+ pub fn open_with_same_credentials(&self, data: Arc<Vec<u8>>) -> Result<Self, CosError> {
+ let credentials = match &self.security {
+ Some(handler) => Credentials::Handler { handler, id0: self.first_id() },
+ None => Credentials::Password(None),
+ };
+ Self::open_as(data, credentials, self.stream_limit)
+ }
+
+ fn open_as(data: Arc<Vec<u8>>, credentials: Credentials<'_>, stream_limit: usize) -> Result<Self, CosError> {
+ // Viewers accept files whose header is missing or damaged as long as the body looks
+ // like PDF; so do we (a note goes to the repair log).
+ let header = find(&data, b"%PDF-", 0, 1024);
+ let headerless = header.is_none();
+ if headerless && find(&data, b" obj", 0, 4096).is_none() {
+ return Err(CosError::NotPdf);
+ }
+ let header_offset = header.unwrap_or(0);
+ let version = match header {
+ Some(h) => String::from_utf8_lossy(&data[(h + 5).min(data.len())..(h + 8).min(data.len())]).trim().to_string(),
+ None => "1.4".to_string(),
+ };
+ let mut doc = Document {
+ data,
+ endstreams: Arc::default(),
+ source_identity: Arc::new(()),
+ entries: Arc::new(XrefIndex::empty()),
+ trailer: Dict::new(),
+ revisions: Arc::new(Vec::new()),
+ xref_oddities: Arc::default(),
+ located: Arc::default(),
+ repair_log: Arc::new(Vec::new()),
+ cache: Arc::default(),
+ objstms: Arc::default(),
+ stream_limit: stream_limit.min(crate::object::MAX_DECODED),
+ overlay: BTreeMap::new(),
+ next_num: 1,
+ header_offset,
+ version,
+ security: None,
+ encrypt_num: None,
+ encryption_changed: false,
+ full_save: false,
+ out_security: None,
+ out_encrypt_num: None,
+ };
+ let mut log = Vec::new();
+ if headerless {
+ log.push("the %PDF- header is missing; reading the file as PDF 1.4".into());
+ }
+ let parsed = doc.read_xref_chain(&mut log);
+ let mut authenticated = false;
+ let ok = match parsed {
+ Ok((entries, trailer, revisions, oddities)) => {
+ doc.entries = Arc::new(entries);
+ doc.trailer = trailer;
+ doc.revisions = Arc::new(revisions);
+ doc.xref_oddities = Arc::new(oddities);
+ doc.fill_xref_gaps(&mut log);
+ // Authenticate before checking the catalog: it may sit in an encrypted object
+ // stream, which reads as garbage until the key is known.
+ if let Some(enc) = doc.trailer.get(b"Encrypt").cloned() {
+ doc.authenticate(&enc, &credentials)?;
+ authenticated = true;
+ }
+ doc.root_is_catalog()
+ }
+ Err(e) => {
+ log.push(format!("cross-reference data unreadable ({e}); rebuilding from object headers"));
+ false
+ }
+ };
+ if !ok {
+ if !doc.entries.is_empty() {
+ log.push("cross-reference data does not lead to a valid catalog; rebuilding from object headers".into());
+ }
+ doc.reconstruct(&mut log)?;
+ }
+ doc.repair_log = Arc::new(log);
+ // Reconstruction may have found a different trailer: (re)authenticate against it.
+ if (!ok || !authenticated)
+ && let Some(enc) = doc.trailer.get(b"Encrypt").cloned()
+ {
+ doc.authenticate(&enc, &credentials)?;
+ }
+ if Arc::make_mut(&mut doc.entries).remove_exhausted() {
+ Arc::make_mut(&mut doc.repair_log).push("skipped exhausted xref object number 4294967295".into());
+ }
+ let max = doc.entries.max_num();
+ let size = u32::try_from(doc.trailer.int(b"Size").unwrap_or(0).max(0)).unwrap_or(u32::MAX);
+ if size == u32::MAX {
+ Arc::make_mut(&mut doc.repair_log).push("exhausted trailer /Size; new objects use unused object numbers".into());
+ }
+ doc.next_num = max.saturating_add(1).max(size).max(1);
+ Ok(doc)
+ }
+
+ fn authenticate(&mut self, enc: &Object, credentials: &Credentials<'_>) -> Result<(), CosError> {
+ // Authentication can change how the same input is decoded.
+ self.source_identity = Arc::new(());
+ let (num, dict) = match enc {
+ Object::Ref(r) => (Some(r.num), self.get(*r).as_dict().cloned()),
+ Object::Dict(d) => (None, Some(d.clone())),
+ _ => (None, None),
+ };
+ let Some(dict) = dict else {
+ // A dangling /Encrypt: viewers treat the file as unencrypted, and so do we.
+ let mut log = self.repair_log.as_ref().clone();
+ log.push("the trailer names an /Encrypt dictionary that does not exist; reading the file as unencrypted".into());
+ self.repair_log = Arc::new(log);
+ self.trailer.remove(b"Encrypt");
+ return Ok(());
+ };
+ let params = crate::security::encrypt_dict(self, &dict);
+ let id0 = self.first_id();
+ let handler = match credentials {
+ Credentials::Password(password) => Arc::new(pdfcraft_crypt::SecurityHandler::open(params, &id0, *password).map_err(|e| match e {
+ pdfcraft_crypt::CryptError::WrongPassword if password.is_none() => CosError::NeedsPassword,
+ pdfcraft_crypt::CryptError::WrongPassword => CosError::WrongPassword,
+ other => CosError::Security(other.to_string()),
+ })?),
+ // The same encryption as the document whose key this is: that key opens it.
+ Credentials::Handler { handler, id0: known } if *handler.dict() == params && *known == id0 => Arc::clone(handler),
+ Credentials::Handler { .. } => return Err(CosError::NeedsPassword),
+ };
+ self.security = Some(handler);
+ self.encrypt_num = num;
+ // Objects read while locating the catalog were read before decryption was possible.
+ self.cache = Arc::default();
+ self.objstms = Arc::default();
+ Ok(())
+ }
+
+ /// The first file identifier, part of the encryption key (empty when there is none).
+ fn first_id(&self) -> Vec<u8> {
+ match self.trailer.get(b"ID") {
+ Some(Object::Array(a)) => a.first().and_then(|s| s.as_string()).map(|s| s.bytes.clone()).unwrap_or_default(),
+ _ => Vec::new(),
+ }
+ }
+
+ /// Lift the restrictions of a document opened with its user password by entering the
+ /// owner (permissions) password (§7.6.4). Fails with `WrongPassword` for any other
+ /// password. Does nothing when the document is unencrypted or already opened as owner.
+ /// Both passwords unlock the same file key, so nothing read so far is decoded again.
+ pub fn unlock(&mut self, password: &str) -> Result<(), CosError> {
+ let Some(current) = self.security.as_deref() else { return Ok(()) };
+ if current.auth() == pdfcraft_crypt::Auth::Owner {
+ return Ok(());
+ }
+ let owner = current.authenticate_owner(&self.first_id(), password).map_err(|e| match e {
+ pdfcraft_crypt::CryptError::WrongPassword => CosError::WrongPassword,
+ other => CosError::Security(other.to_string()),
+ })?;
+ self.security = Some(Arc::new(owner));
+ Ok(())
+ }
+
+ /// The security handler, when the document is encrypted.
+ pub fn security(&self) -> Option<&pdfcraft_crypt::SecurityHandler> {
+ self.security.as_deref()
+ }
+
+ /// What the opening password allows (`None` for unencrypted documents: everything).
+ pub fn permissions(&self) -> Option<pdfcraft_crypt::Permissions> {
+ self.security().map(|s| s.permissions())
+ }
+
+ /// Make the next save a full rewrite: earlier revisions (which still hold what an edit
+ /// removed) must not stay in the file. Redaction requires this.
+ pub fn require_full_save(&mut self) {
+ self.full_save = true;
+ }
+
+ /// The next save must rewrite the whole file (see [`Document::require_full_save`]).
+ pub fn full_save_required(&self) -> bool {
+ self.full_save
+ }
+
+ /// Security was set or removed since the document was opened (the next save applies it).
+ pub fn encryption_changed(&self) -> bool {
+ self.encryption_changed
+ }
+
+ /// The handler used to write: the new one after `set_encryption` / `remove_encryption`,
+ /// otherwise the one the document was opened with.
+ pub(crate) fn output_security(&self) -> (Option<&pdfcraft_crypt::SecurityHandler>, Option<u32>) {
+ if self.encryption_changed { (self.out_security.as_deref(), self.out_encrypt_num) } else { (self.security.as_deref(), self.encrypt_num) }
+ }
+
+ /// The security the next save writes: protection applied with `set_encryption`, none after
+ /// `remove_encryption`, otherwise the security the document was opened with.
+ pub fn output_handler(&self) -> Option<&pdfcraft_crypt::SecurityHandler> {
+ self.output_security().0
+ }
+
+ /// Protect the document with a password (§7.6.4). Takes effect on the next save, which is
+ /// always a full rewrite. Returns the handler (authenticated as owner).
+ pub fn set_encryption(&mut self, params: &pdfcraft_crypt::NewEncryption) -> Result<(), CosError> {
+ // The file identifier is part of the key; make sure it exists and keep it.
+ let id0 = match self.trailer.get(b"ID") {
+ Some(Object::Array(a)) if a.len() == 2 => a[0].as_string().map(|s| s.bytes.clone()).unwrap_or_default(),
+ _ => {
- let mut id = generated_id(&params.seed);
- id.truncate(16);
++ let id = pdfcraft_crypt::file_id(&params.seed);
+ let s = Object::String(crate::PdfString { bytes: id.clone(), hex: true });
+ self.trailer.set(b"ID".to_vec(), Object::Array(vec![s.clone(), s]));
+ id
+ }
+ };
+ let h = pdfcraft_crypt::create(params, &id0).map_err(|e| CosError::Security(e.to_string()))?;
+ let d = h.dict();
+ let s = |b: &[u8]| Object::String(crate::PdfString { bytes: b.to_vec(), hex: true });
+ let mut e = Dict::new();
+ e.set(b"Filter".to_vec(), Object::name("Standard"));
+ e.set(b"V".to_vec(), Object::Int(d.v));
+ e.set(b"R".to_vec(), Object::Int(d.r));
+ e.set(b"Length".to_vec(), Object::Int(d.length_bits));
+ e.set(b"O".to_vec(), s(&d.o));
+ e.set(b"U".to_vec(), s(&d.u));
+ e.set(b"P".to_vec(), Object::Int(i64::from(d.p)));
+ if d.v >= 4 {
+ let mut cf = Dict::new();
+ let mut std_cf = Dict::new();
+ std_cf.set(b"Type".to_vec(), Object::name("CryptFilter"));
+ let (cfm, len) = if d.v >= 5 { ("AESV3", 32) } else { ("AESV2", 16) };
+ std_cf.set(b"CFM".to_vec(), Object::name(cfm));
+ std_cf.set(b"Length".to_vec(), Object::Int(len));
+ std_cf.set(b"AuthEvent".to_vec(), Object::name("DocOpen"));
+ cf.set(b"StdCF".to_vec(), Object::Dict(std_cf));
+ e.set(b"CF".to_vec(), Object::Dict(cf));
+ e.set(b"StmF".to_vec(), Object::name("StdCF"));
+ e.set(b"StrF".to_vec(), Object::name("StdCF"));
+ if !d.encrypt_metadata {
+ e.set(b"EncryptMetadata".to_vec(), Object::Bool(false));
+ }
+ }
+ if d.v >= 5 {
+ e.set(b"OE".to_vec(), s(&d.oe));
+ e.set(b"UE".to_vec(), s(&d.ue));
+ e.set(b"Perms".to_vec(), s(&d.perms));
+ }
+ if let Some(Object::Ref(old)) = self.trailer.get(b"Encrypt").cloned() {
+ self.free(old);
+ }
+ let r = self.add(e);
+ self.trailer.set(b"Encrypt".to_vec(), Object::Ref(r));
+ // Objects are still read with the original handler; saves use the new one.
+ self.out_security = Some(Arc::new(h));
+ self.out_encrypt_num = Some(r.num);
+ self.encryption_changed = true;
+ Ok(())
+ }
+
+ /// Remove password protection (requires the document to be open — any authenticated
+ /// password; callers should require the owner password, §7.6.4). Applied by a full save.
+ pub fn remove_encryption(&mut self) {
+ if let Some(Object::Ref(r)) = self.trailer.get(b"Encrypt").cloned() {
+ self.free(r);
+ }
+ self.trailer.remove(b"Encrypt");
+ self.out_security = None;
+ self.out_encrypt_num = None;
+ self.encryption_changed = true;
+ }
+
+ /// Decrypt a freshly parsed indirect object if the document is encrypted.
+ pub(super) fn decrypted(&self, id: ObjRef, o: Object) -> Object {
+ match &self.security {
+ Some(h) if Some(id.num) != self.encrypt_num => crate::security::transform(h, &o, id.num, id.generation, crate::security::Pass::Decrypt),
+ _ => o,
+ }
+ }
+ }
diff --cc crates/cos/src/document/reader.rs
index 000000000,ead09c36e..7c56281f8
mode 000000,100644..100644
--- a/crates/cos/src/document/reader.rs
+++ b/crates/cos/src/document/reader.rs
@@@ -1,0 -1,874 +1,862 @@@
+ #[cfg(test)]
+ use super::state::SECTION_READS;
+ use super::xref::{
+ MAX_COMPACT_XREF_BYTES, MAX_COMPACT_XREF_ROWS, Revision, XrefEntry, XrefIndex, XrefRange, XrefRevisionSection, XrefSection, XrefSectionData,
+ };
+ use super::*;
+
+ impl Document {
+ #[allow(clippy::type_complexity)]
+ pub(super) fn read_xref_chain(&self, log: &mut Vec<String>) -> Result<(XrefIndex, Dict, Vec<Revision>, Vec<(u64, u32)>), CosError> {
+ let mut compact_log = Vec::new();
+ if let Ok(Some((index, trailer, revisions))) = self.read_compact_xref_chain(&mut compact_log) {
+ let size = trailer.int(b"Size").unwrap_or(0).max(0);
+ if size > 0 && size <= i64::from(u32::MAX) && u64::from(index.max_num()) < size as u64 {
+ log.extend(compact_log);
+ // The compact reader declines any section with set-aside entries, so there are none here.
+ return Ok((index, trailer, revisions, Vec::new()));
+ }
+ }
+ let (entries, trailer, revisions, oddities) = self.read_xref_chain_materialized(log)?;
+ Ok((XrefIndex::Materialized(Arc::new(entries)), trailer, revisions, oddities))
+ }
+
+ fn read_compact_xref_chain(&self, log: &mut Vec<String>) -> Result<Option<(XrefIndex, Dict, Vec<Revision>)>, CosError> {
+ let data = &self.data;
+ let tail_start = data.len().saturating_sub(4096);
+ let sx = rfind(data, b"startxref", tail_start).ok_or(CosError::Syntax { offset: data.len(), detail: "no startxref".into() })?;
+ let mut lx = Lexer::new(data, sx + 9);
+ let first = match lx.object() {
+ Ok(Object::Int(n)) if n >= 0 => n as u64,
+ _ => return Err(CosError::Syntax { offset: sx, detail: "bad startxref value".into() }),
+ };
+ let mut sections = Vec::new();
+ let mut retained_rows = 0usize;
+ let mut retained_bytes = 0usize;
+ let mut trailer: Option<Dict> = None;
+ let mut revisions = Vec::new();
+ let mut visited = HashSet::new();
+ let mut next = Some(first);
+ while let Some(off) = next.take() {
+ if !visited.insert(off) || visited.len() > 10_000 {
+ log.push(format!("cross-reference chain loops at offset {off}; stopped"));
+ break;
+ }
+ let ((primary, sect_trailer, is_stream), header_relative) = match self.read_compact_section(off as usize) {
+ Ok(Some(v)) => (v, false),
+ Ok(None) => return Ok(None),
+ Err(e) if self.header_offset > 0 => {
+ log.push(format!("xref at {off} unreadable ({e}); retrying relative to the header"));
+ match self.read_compact_section(off as usize + self.header_offset)? {
+ Some(v) => (v, true),
+ None => return Ok(None),
+ }
+ }
+ // An unreadable older section loses every object only it locates: the
+ // materialized reader logs it and the file is rebuilt from object headers (#1656).
+ Err(e) => return Err(e),
+ };
+ revisions.push(Revision { xref_offset: off, is_stream, header_relative });
+ if primary.renumbered {
+ log.push(format!("xref subsection at {off} is numbered from 1 but begins with object 0's entry; read from 0"));
+ }
+ let Some(rows) = retained_rows.checked_add(primary.row_count).filter(|rows| *rows <= MAX_COMPACT_XREF_ROWS) else { return Ok(None) };
+ let Some(bytes) = retained_bytes.checked_add(primary.retained_bytes()).filter(|bytes| *bytes <= MAX_COMPACT_XREF_BYTES) else {
+ return Ok(None);
+ };
+ retained_rows = rows;
+ retained_bytes = bytes;
+ let hybrid = if let Some(x) = sect_trailer.int(b"XRefStm").filter(|x| *x >= 0) {
+ match self.read_compact_section(x as usize) {
+ Ok(Some((section, _, _))) => {
+ let Some(rows) = retained_rows.checked_add(section.row_count).filter(|rows| *rows <= MAX_COMPACT_XREF_ROWS) else {
+ return Ok(None);
+ };
+ let Some(bytes) = retained_bytes.checked_add(section.retained_bytes()).filter(|bytes| *bytes <= MAX_COMPACT_XREF_BYTES)
+ else {
+ return Ok(None);
+ };
+ retained_rows = rows;
+ retained_bytes = bytes;
+ Some(section)
+ }
+ Ok(None) => return Ok(None),
+ Err(e) => {
+ log.push(format!("hybrid /XRefStm at {x} unreadable: {e}"));
+ None
+ }
+ }
+ } else {
+ None
+ };
+ if let Some(hybrid) = &hybrid {
+ // A hybrid entry that disagrees with the table is set aside by the materialized reader.
+ let mut numbers = Vec::new();
+ hybrid.extend_numbers(&mut numbers);
+ for num in numbers {
+ let Some(entry) = hybrid.lookup(num, &self.data) else { continue };
+ if primary.lookup(num, &self.data).is_some_and(|old| !matches!(old, XrefEntry::Free { .. }) && old != entry) {
+ return Ok(None);
+ }
+ }
+ }
+ sections.push(XrefRevisionSection { primary, hybrid });
+ next = prev_offset(data, &sect_trailer, off, |num| sections.iter().find_map(|s| s.lookup(num, data)), log);
+ if trailer.is_none() {
+ trailer = Some(sect_trailer);
+ }
+ }
+ revisions.reverse();
+ let mut trailer = trailer.ok_or(CosError::Syntax { offset: 0, detail: "no trailer".into() })?;
+ for k in [&b"Prev"[..], b"XRefStm", b"Type", b"W", b"Index", b"Filter", b"DecodeParms", b"Length"] {
+ trailer.remove(k);
+ }
+ let mut max_num = 0;
+ let mut has_entries = false;
+ for revision in &sections {
+ max_num = max_num.max(revision.primary.max_num);
+ has_entries |= revision.primary.has_entries;
+ if let Some(hybrid) = &revision.hybrid {
+ max_num = max_num.max(hybrid.max_num);
+ has_entries |= hybrid.has_entries;
+ }
+ }
+ Ok(Some((XrefIndex::Compact { revisions: sections, max_num, has_entries }, trailer, revisions)))
+ }
+
+ fn read_compact_section(&self, off: usize) -> Result<Option<(XrefSection, Dict, bool)>, CosError> {
+ let data = &self.data;
+ let mut lx = Lexer::new(data, off);
+ if lx.eat_keyword(b"xref") {
+ let mut ranges = Vec::new();
+ let mut layout_ok = true;
+ let mut max_num = 0;
+ let mut has_entries = false;
+ let mut previous_end = None;
+ let mut row_count = 0usize;
+ let mut renumbered = false;
+ loop {
+ lx.skip_ws();
+ if lx.eat_keyword(b"trailer") {
+ let t = lx.object()?;
+ let d = t.as_dict().cloned().ok_or(CosError::Syntax { offset: lx.pos, detail: "trailer is not a dictionary".into() })?;
+ if !layout_ok {
+ return Ok(None);
+ }
+ return Ok(Some((XrefSection { data: XrefSectionData::Table, ranges, row_count, max_num, has_entries, renumbered }, d, false)));
+ }
+ let (Ok(Object::Int(start)), Ok(Object::Int(count))) = (lx.object(), lx.object()) else {
+ return Err(CosError::Syntax { offset: lx.pos, detail: "bad xref subsection header".into() });
+ };
+ if !valid_xref_range(start, count) || count > 10_000_000 {
+ return Err(CosError::Syntax { offset: lx.pos, detail: "invalid xref object range".into() });
+ }
+ let shifted = subsection_start(data, self.header_offset, lx.pos, start, count);
+ renumbered |= shifted != start;
+ let start = shifted;
+ let Some(next_rows) = usize::try_from(count).ok().and_then(|count| row_count.checked_add(count)) else { return Ok(None) };
+ if next_rows > MAX_COMPACT_XREF_ROWS {
+ return Ok(None);
+ }
+ row_count = next_rows;
+ let mut range_offset = None;
+ let mut stride = None;
+ let mut previous_record = None;
+ for i in 0..count {
+ lx.skip_ws();
+ let record = lx.pos;
+ if range_offset.is_none() {
+ range_offset = Some(record);
+ }
+ if let Some(previous) = previous_record {
+ let Some(delta) = record.checked_sub(previous) else {
+ layout_ok = false;
+ continue;
+ };
+ if stride.is_some_and(|known| known != delta) {
+ layout_ok = false;
+ }
+ stride.get_or_insert(delta);
+ }
+ previous_record = Some(record);
+ let o = lx.token();
+ lx.skip_ws();
+ let _generation = lx.token();
+ lx.skip_ws();
+ let kind = lx.token();
+ let num = (start + i) as u32;
+ let offset = std::str::from_utf8(o).ok().and_then(|s| s.parse::<u64>().ok());
+ match (kind, offset) {
+ (b"n", Some(offset)) if offset != 0 => {
+ has_entries = true;
+ max_num = max_num.max(num);
+ }
+ (b"n", Some(_)) => {
+ // Set aside by the materialized reader, which records it.
+ if num != 0 {
+ return Ok(None);
+ }
+ }
+ (b"f", _) => {
+ has_entries = true;
+ max_num = max_num.max(num);
+ }
+ _ => return Err(CosError::Syntax { offset: lx.pos, detail: "bad xref entry".into() }),
+ }
+ }
+ if count > 0 {
+ let range_start = start as u32;
+ let range_count = count as u32;
+ let end = u64::from(range_start) + u64::from(range_count);
+ if previous_end.is_some_and(|previous| u64::from(range_start) < previous) {
+ layout_ok = false;
+ }
+ previous_end = Some(end);
+ ranges.push(XrefRange {
+ start: range_start,
+ count: range_count,
+ offset: range_offset.unwrap_or(lx.pos),
+ stride: stride.unwrap_or(1),
+ });
+ }
+ }
+ }
+ let (_, obj) = parse_indirect(data, off, &|_| None)?;
+ let Object::Stream(stream) = obj else { return Err(CosError::Syntax { offset: off, detail: "expected xref table or stream".into() }) };
+ if stream.dict.name(b"Type") != Some(b"XRef") {
+ return Err(CosError::Syntax { offset: off, detail: "stream at startxref is not /Type /XRef".into() });
+ }
+ let w: Vec<usize> = stream
+ .dict
+ .get(b"W")
+ .and_then(Object::as_array)
+ .map(|a| a.iter().map(|x| x.as_int().unwrap_or(0).clamp(0, 8) as usize).collect())
+ .unwrap_or_default();
+ if w.len() < 3 {
+ return Err(CosError::Syntax { offset: off, detail: "xref stream /W invalid".into() });
+ }
+ let widths = [w[0], w[1], w[2]];
+ let row = widths.iter().sum::<usize>();
+ if row == 0 {
+ return Err(CosError::Syntax { offset: off, detail: "xref stream row width 0".into() });
+ }
+ let size = stream.dict.int(b"Size").unwrap_or(0).max(0);
+ let index: Vec<i64> = match stream.dict.get(b"Index").and_then(Object::as_array) {
+ Some(a) => a.iter().filter_map(Object::as_int).collect(),
+ None => vec![0, size],
+ };
+ let mut requested_rows = 0usize;
+ let mut previous_end = None;
+ for pair in index.chunks(2) {
+ let [start, count] = pair else { break };
+ if !valid_xref_range(*start, *count) {
+ return Err(CosError::Syntax { offset: off, detail: "invalid xref object range".into() });
+ }
+ let Ok(count) = usize::try_from(*count) else { return Ok(None) };
+ let Some(next) = requested_rows.checked_add(count).filter(|rows| *rows <= MAX_COMPACT_XREF_ROWS) else { return Ok(None) };
+ if count > 0 {
+ let end = (*start as u64).saturating_add(count as u64);
+ if previous_end.is_some_and(|previous| (*start as u64) < previous) {
+ return Ok(None);
+ }
+ previous_end = Some(end);
+ }
+ requested_rows = next;
+ }
+ if requested_rows.checked_mul(row).is_none_or(|bytes| bytes > MAX_COMPACT_XREF_BYTES) {
+ return Ok(None);
+ }
+ let raw = stream.decoded_within(MAX_XREF_OBJECTS.saturating_mul(row).min(self.stream_limit).min(MAX_COMPACT_XREF_BYTES))?;
+ if raw.capacity() > MAX_COMPACT_XREF_BYTES {
+ return Ok(None);
+ }
+ let mut ranges = Vec::new();
+ let mut row_start = 0usize;
+ let available_rows = raw.len() / row;
+ for pair in index.chunks(2) {
+ let [start, count] = pair else { break };
+ let range_start = *start as u32;
+ let Ok(requested) = usize::try_from(*count) else { return Ok(None) };
+ let range_count = requested.min(available_rows.saturating_sub(row_start));
+ if range_count > 0 {
+ let Ok(range_count_u32) = u32::try_from(range_count) else { return Ok(None) };
+ let Some(byte_offset) = row_start.checked_mul(row) else { return Ok(None) };
+ ranges.push(XrefRange { start: range_start, count: range_count_u32, offset: byte_offset, stride: row });
+ }
+ let Some(next) = row_start.checked_add(range_count) else { return Ok(None) };
+ row_start = next;
+ }
+ // Rows the materialized reader sets aside (reserved types, offset 0) are left to it.
+ for range in &ranges {
+ for i in 0..range.count as usize {
+ let at = range.offset + i * row;
+ let Some(r) = raw.get(at..at + row) else { break };
+ let field = |from: usize, width: usize, default: u64| -> u64 {
+ if width == 0 {
+ return default;
+ }
+ r[from..from + width].iter().fold(0u64, |acc, byte| acc << 8 | u64::from(*byte))
+ };
+ let kind = field(0, widths[0], 1);
+ let offset = field(widths[0], widths[1], 0);
+ if kind > 2 || (kind == 1 && offset == 0 && range.start + i as u32 != 0) {
+ return Ok(None);
+ }
+ }
+ }
+ let max_num = ranges.iter().filter_map(|range| range.start.checked_add(range.count.saturating_sub(1))).max().unwrap_or(0);
+ let row_count = row_start;
+ let section = XrefSection {
+ data: XrefSectionData::Stream { bytes: Arc::new(raw), widths },
+ ranges,
+ row_count,
+ max_num,
+ has_entries: false,
+ renumbered: false,
+ };
+ let has_entries = section.has_any_entry();
+ let section = XrefSection { has_entries, ..section };
+ Ok(Some((section, stream.dict.clone(), true)))
+ }
+
+ #[allow(clippy::type_complexity)]
+ pub(super) fn read_xref_chain_materialized(
+ &self,
+ log: &mut Vec<String>,
+ ) -> Result<(BTreeMap<u32, XrefEntry>, Dict, Vec<Revision>, Vec<(u64, u32)>), CosError> {
+ let data = &self.data;
+ let tail_start = data.len().saturating_sub(4096);
+ let sx = rfind(data, b"startxref", tail_start).ok_or(CosError::Syntax { offset: data.len(), detail: "no startxref".into() })?;
+ let mut lx = Lexer::new(data, sx + 9);
+ let first = match lx.object() {
+ Ok(Object::Int(n)) if n >= 0 => n as u64,
+ _ => return Err(CosError::Syntax { offset: sx, detail: "bad startxref value".into() }),
+ };
+ let mut entries = BTreeMap::new();
+ let mut trailer: Option<Dict> = None;
+ let mut revisions = Vec::new();
+ let mut oddities = Vec::new();
+ let mut visited = HashSet::new();
+ // Hybrid streams already read, by offset: their entries, or `None` when unreadable. A newer
+ // section has merged every entry of one into `entries`, so an older section that names it
+ // again only has its own table compared with them, without decoding the stream again.
+ // At most MAX_XREF_OBJECTS entries are kept in all; a stream past that is read again.
+ let mut hybrids: HashMap<i64, Option<BTreeMap<u32, XrefEntry>>> = HashMap::new();
+ let (mut hybrid_rows, mut noted) = (0usize, HashSet::new());
+ let mut next = Some(first);
+ while let Some(off) = next.take() {
+ if !visited.insert(off) || visited.len() > 10_000 {
+ log.push(format!("cross-reference chain loops at offset {off}; stopped"));
+ break;
+ }
+ // This section's own entries; older sections only fill what newer ones left out.
+ let mut sect = BTreeMap::new();
+ // Object numbers whose entry in this section was set aside ([`Self::xref_oddities`]).
+ let mut odd = Vec::new();
+ let ((sect_trailer, is_stream), header_relative) = match self.read_section(off as usize, &mut sect, &mut odd, log) {
+ Ok(v) => (v, false),
+ Err(e) if self.header_offset > 0 => {
+ log.push(format!("xref at {off} unreadable ({e}); retrying relative to the header"));
+ sect.clear();
+ odd.clear();
+ (self.read_section(off as usize + self.header_offset, &mut sect, &mut odd, log)?, true)
+ }
+ Err(e) => {
+ // An unreadable older section loses every object only it locates (the page
+ // tree, often): rebuild from object headers, as readers do (#1656).
+ if !revisions.is_empty() {
+ log.push(format!("older cross-reference section at {off} is unreadable: {e}"));
+ }
+ return Err(e);
+ }
+ };
+ revisions.push(Revision { xref_offset: off, is_stream, header_relative });
+ // Hybrid files (§7.5.8.4): the /XRefStm entries supplement the table and replace the
+ // free entries it gives objects only a cross-reference stream can locate.
+ let hybrid = sect_trailer.int(b"XRefStm").filter(|x| *x >= 0);
+ if let Some((x, read)) = hybrid.and_then(|x| hybrids.get(&x).map(|read| (x, read))) {
+ if noted.insert(x) {
+ log.push(format!("hybrid /XRefStm at {x} named again by the section at {off}; read once"));
+ }
+ if let Some(stm) = read {
+ for (num, old) in &sect {
+ // The table places the object somewhere other than the stream does.
+ if !matches!(old, XrefEntry::Free { .. }) && stm.get(num).is_some_and(|e| e != old) {
+ odd.push(*num);
+ }
+ }
+ }
+ } else if let Some(x) = hybrid {
+ let mut stm = BTreeMap::new();
+ let read = match self.read_section(x as usize, &mut stm, &mut odd, log) {
+ Ok(_) => {
+ for (&num, &e) in &stm {
+ match sect.get(&num) {
+ None | Some(XrefEntry::Free { .. }) => {
+ sect.insert(num, e);
+ }
+ // The table already places the object somewhere else.
+ Some(old) if *old != e => odd.push(num),
+ Some(_) => {}
+ }
+ }
+ Some(stm)
+ }
+ Err(e) => {
+ log.push(format!("hybrid /XRefStm at {x} unreadable: {e}"));
+ None
+ }
+ };
+ let rows = read.as_ref().map_or(0, BTreeMap::len);
+ if let Some(total) = hybrid_rows.checked_add(rows).filter(|total| *total <= MAX_XREF_OBJECTS) {
+ hybrid_rows = total;
+ hybrids.insert(x, read);
+ }
+ }
+ oddities.extend(odd.into_iter().map(|num| (off, num)));
+ for (num, e) in sect {
+ entries.entry(num).or_insert(e);
+ }
+ next = prev_offset(data, &sect_trailer, off, |num| entries.get(&num).copied(), log);
+ if trailer.is_none() {
+ trailer = Some(sect_trailer);
+ }
+ }
+ revisions.reverse();
+ let mut trailer = trailer.ok_or(CosError::Syntax { offset: 0, detail: "no trailer".into() })?;
+ for k in [&b"Prev"[..], b"XRefStm", b"Type", b"W", b"Index", b"Filter", b"DecodeParms", b"Length"] {
+ trailer.remove(k);
+ }
+ Ok((entries, trailer, revisions, oddities))
+ }
+
+ /// Read one section at `off`, adding entries not already known (newer sections win). `odd`
+ /// collects the object numbers whose entry here is set aside ([`Self::xref_oddities`]).
+ fn read_section(
+ &self,
+ off: usize,
+ entries: &mut BTreeMap<u32, XrefEntry>,
+ odd: &mut Vec<u32>,
+ log: &mut Vec<String>,
+ ) -> Result<(Dict, bool), CosError> {
+ #[cfg(test)]
+ SECTION_READS.with(|c| c.set(c.get() + 1));
+ let data = &self.data;
+ let mut lx = Lexer::new(data, off);
+ if lx.eat_keyword(b"xref") {
+ loop {
+ lx.skip_ws();
+ if lx.eat_keyword(b"trailer") {
+ let t = lx.object()?;
+ let d = t.as_dict().cloned().ok_or(CosError::Syntax { offset: lx.pos, detail: "trailer is not a dictionary".into() })?;
+ return Ok((d, false));
+ }
+ let (Ok(Object::Int(start)), Ok(Object::Int(count))) = (lx.object(), lx.object()) else {
+ return Err(CosError::Syntax { offset: lx.pos, detail: "bad xref subsection header".into() });
+ };
+ if !valid_xref_range(start, count) || count > 10_000_000 {
+ return Err(CosError::Syntax { offset: lx.pos, detail: "invalid xref object range".into() });
+ }
+ let shifted = subsection_start(data, self.header_offset, lx.pos, start, count);
+ if shifted != start {
+ log.push(format!("xref subsection at {off} is numbered from 1 but begins with object 0's entry; read from 0"));
+ }
+ let start = shifted;
+ for i in 0..count {
+ lx.skip_ws();
+ let o = lx.token();
+ lx.skip_ws();
+ let g = lx.token();
+ lx.skip_ws();
+ let kind = lx.token();
+ let num = (start + i) as u32;
+ let offset = std::str::from_utf8(o).ok().and_then(|s| s.parse::<u64>().ok());
+ let generation = std::str::from_utf8(g).ok().and_then(|s| s.parse::<u32>().ok()).unwrap_or(0).min(u16::MAX as u32) as u16;
+ let entry = match (kind, offset) {
+ (b"n", Some(offset)) => XrefEntry::InFile { offset, generation },
+ (b"f", _) => XrefEntry::Free { next_generation: generation },
+ _ => return Err(CosError::Syntax { offset: lx.pos, detail: "bad xref entry".into() }),
+ };
+ // Offset 0 "in use" entries are a known producer bug: ignore them. (Object 0
+ // is never a real object, so its entry cannot mislead anyone.)
+ if matches!(entry, XrefEntry::InFile { offset: 0, .. }) {
+ if num != 0 {
+ odd.push(num);
+ }
+ continue;
+ }
+ if *entries.entry(num).or_insert(entry) != entry {
+ odd.push(num);
+ }
+ }
+ }
+ }
+ // Cross-reference stream.
+ let (_, obj) = parse_indirect(data, off, &|_| None)?;
+ let Object::Stream(s) = obj else { return Err(CosError::Syntax { offset: off, detail: "expected xref table or stream".into() }) };
+ if s.dict.name(b"Type") != Some(b"XRef") {
+ return Err(CosError::Syntax { offset: off, detail: "stream at startxref is not /Type /XRef".into() });
+ }
+ let w: Vec<usize> = s
+ .dict
+ .get(b"W")
+ .and_then(Object::as_array)
+ .map(|a| a.iter().map(|x| x.as_int().unwrap_or(0).clamp(0, 8) as usize).collect())
+ .unwrap_or_default();
+ if w.len() < 3 {
+ return Err(CosError::Syntax { offset: off, detail: "xref stream /W invalid".into() });
+ }
+ let size = s.dict.int(b"Size").unwrap_or(0).max(0);
+ let index: Vec<i64> = match s.dict.get(b"Index").and_then(Object::as_array) {
+ Some(a) => a.iter().filter_map(Object::as_int).collect(),
+ None => vec![0, size],
+ };
+ let row = w[0] + w[1] + w[2];
+ if row == 0 {
+ return Err(CosError::Syntax { offset: off, detail: "xref stream row width 0".into() });
+ }
+ // One row per object, and a document has at most 8,388,607 indirect objects (the classic
+ // implementation limit): decoding more than that (about 200 MB at the widest rows) is a
+ // decompression bomb, not cross-reference data. The section is then reconstructed.
+ let raw = s.decoded_within(MAX_XREF_OBJECTS.saturating_mul(row).min(self.stream_limit))?;
+ let field = |r: &[u8], from: usize, len: usize, default: u64| -> u64 {
+ if len == 0 {
+ return default;
+ }
+ r[from..from + len].iter().fold(0u64, |acc, b| acc << 8 | *b as u64)
+ };
+ let mut rows = raw.chunks_exact(row);
+ for pair in index.chunks(2) {
+ let [start, count] = pair else { break };
+ if !valid_xref_range(*start, *count) {
+ return Err(CosError::Syntax { offset: off, detail: "invalid xref object range".into() });
+ }
+ for i in 0..*count {
+ let Some(r) = rows.next() else { break };
+ let t = field(r, 0, w[0], 1);
+ let a = field(r, w[0], w[1], 0);
+ let b = field(r, w[0] + w[1], w[2], 0);
+ let num = (*start + i) as u32;
+ let entry = match t {
+ 0 => XrefEntry::Free { next_generation: b.min(u16::MAX as u64) as u16 },
+ 1 => XrefEntry::InFile { offset: a, generation: b.min(u16::MAX as u64) as u16 },
+ 2 => XrefEntry::InStream { stream: a.min(u32::MAX as u64) as u32, index: b.min(u32::MAX as u64) as u32 },
+ // Reserved types are treated as null references (§7.5.8.3).
+ _ => {
+ odd.push(num);
+ continue;
+ }
+ };
+ if matches!(entry, XrefEntry::InFile { offset: 0, .. }) {
+ if num != 0 {
+ odd.push(num);
+ }
+ continue;
+ }
+ if *entries.entry(num).or_insert(entry) != entry {
+ odd.push(num);
+ }
+ }
+ }
+ Ok((s.dict.clone(), true))
+ }
+
+ /// Cross-reference data that leaves out objects the file contains: a subsection numbered one
+ /// too low (object 0 in use, the last object without an entry), or an in-use entry at offset 0
+ /// that nothing else replaces. (A number no section lists is not evidence: it is a null
+ /// object, §7.3.10, and valid files have them below an oversized `/Size`.) Viewers rebuild
+ /// the table and show these objects; reading them as null would drop them from every copy.
+ /// When one of those shows, the file is scanned once for object headers, and wrong offsets
+ /// are looked up there (`load`). Only the numbers the damage leaves out take the last
+ /// header's location: one whose entry was set aside, or the last object of a subsection
+ /// numbered one too low. Any other unlisted number stays null, and existing
+ /// entries are never replaced: an object freed by an update stays freed.
+ pub(super) fn fill_xref_gaps(&mut self, log: &mut Vec<String>) {
+ let entries = &self.entries;
+ let shifted = matches!(entries.lookup(0, &self.data), Some(XrefEntry::InFile { .. }));
+ let set_aside = self.xref_oddities.iter().any(|(_, num)| entries.lookup(*num, &self.data).is_none());
+ if !(shifted || set_aside) {
+ return;
+ }
+ let mut located = BTreeMap::new();
+ for (num, offset, generation, _) in object_headers(&self.data) {
+ located.insert(num, (offset, generation));
+ }
+ let mut filled = entries.to_map(&self.data);
+ // A subsection `0 N` whose rows are those of objects 1 to N leaves out object N, whatever
+ // newer sections say about its neighbours.
+ let past_shift: HashSet<u32> = if shifted {
+ self.revisions
+ .iter()
+ .filter(|r| !r.is_stream)
+ .filter_map(|r| usize::try_from(r.xref_offset).ok().map(|o| if r.header_relative { o.saturating_add(self.header_offset) } else { o }))
+ .filter_map(|at| shifted_subsection_end(&self.data, at))
+ .collect()
+ } else {
+ HashSet::new()
+ };
+ let odd: HashSet<u32> = self.xref_oddities.iter().map(|(_, n)| *n).collect();
+ let mut found = 0usize;
+ for (&num, &(offset, generation)) in &located {
+ let left_out = past_shift.contains(&num) || odd.contains(&num);
+ if num != 0 && !filled.contains_key(&num) && left_out {
+ filled.insert(num, XrefEntry::InFile { offset, generation });
+ found += 1;
+ }
+ }
+ if found > 0 {
+ log.push(format!("the cross-reference data leaves out {found} objects the file contains; located them by their object headers"));
+ self.entries = Arc::new(XrefIndex::Materialized(Arc::new(filled)));
+ }
+ self.located = Arc::new(located);
+ }
+
+ /// Rebuild the cross-reference index by scanning for `N G obj` headers (§7.5.4 recovery).
+ pub(super) fn reconstruct(&mut self, log: &mut Vec<String>) -> Result<(), CosError> {
+ let data = self.data.clone();
+ let mut entries: BTreeMap<u32, XrefEntry> = BTreeMap::new();
+ let mut objstms = Vec::new();
+ // Offsets of objects that may be cross-reference streams: their dictionaries are the
+ // trailers of files that have no `trailer` keyword (§7.5.8).
+ let mut xref_streams = Vec::new();
+ for (num, offset, generation, body) in object_headers(&data) {
+ entries.insert(num, XrefEntry::InFile { offset, generation });
+ objstms.push(num);
+ if find(&data, b"/XRef", body, body.saturating_add(1024)).is_some()
+ && let Ok(at) = usize::try_from(offset)
+ {
+ xref_streams.push(at);
+ }
+ }
+ if entries.is_empty() {
+ return Err(CosError::Syntax { offset: 0, detail: "no objects found".into() });
+ }
+ self.entries = Arc::new(XrefIndex::Materialized(Arc::new(entries.clone())));
+ self.cache.lock().map_err(|_| CosError::Poisoned)?.clear();
+ // Objects inside object streams.
+ for num in objstms {
+ if let Ok(o) = self.try_get(num)
+ && let Object::Stream(s) = &*o
+ && s.dict.name(b"Type") == Some(b"ObjStm")
+ && let Ok(stm) = self.objstm(num)
+ {
+ for (idx, member) in stm.index.iter().enumerate() {
+ if let Some((inner, _)) = member {
+ entries.entry(*inner).or_insert(XrefEntry::InStream { stream: num, index: idx as u32 });
+ }
+ }
+ }
+ }
+ let object_count = entries.len();
+ self.entries = Arc::new(XrefIndex::Materialized(Arc::new(entries)));
+ self.cache.lock().map_err(|_| CosError::Poisoned)?.clear();
+ // Trailer: the last `trailer` dictionary or cross-reference stream dictionary with a
+ // usable /Root (last in the file, whichever kind), else the catalog itself. One whose
+ // /Root is a catalog (as `root_is_catalog` judges it) beats one whose /Root is another
+ // dictionary, such as /Info, wherever it is; an FDF's /Root (no /Type, no /Pages) still
+ // counts when nothing better is there.
+ // A file whose only trailers are cross-reference streams (and whose `startxref` is gone
+ // or points nowhere) keeps its /Info, /ID and /Encrypt this way.
+ let rank = |at: usize, d: &Dict| {
+ let root = self.try_get(d.reference(b"Root")?.num).ok()?;
+ let Object::Dict(c) = &*root else { return None };
+ Some((c.name(b"Type") == Some(b"Catalog") || c.contains(b"Pages"), at))
+ };
+ let mut trailer: Option<((bool, usize), Dict)> = None;
+ let mut from = 0;
+ while let Some(p) = find(&data, b"trailer", from, data.len()) {
+ from = p + 7;
+ let mut lx = Lexer::new(&data, from);
+ if let Ok(Object::Dict(d)) = lx.object()
+ && let Some(r) = rank(p, &d)
+ && trailer.as_ref().is_none_or(|(best, _)| r > *best)
+ {
+ trailer = Some((r, d));
+ }
+ }
+ for at in xref_streams {
+ if trailer.as_ref().is_some_and(|((catalog, p), _)| *catalog && *p > at) {
+ continue;
+ }
+ if let Ok((_, Object::Stream(s))) = parse_indirect_shared(&data, at, &|_| None, &self.endstreams)
+ && s.dict.name(b"Type") == Some(b"XRef")
+ && let Some(r) = rank(at, &s.dict)
+ && trailer.as_ref().is_none_or(|(best, _)| r > *best)
+ {
+ trailer = Some((r, s.dict));
+ }
+ }
+ let trailer = match trailer {
+ Some((_, t)) => t,
+ None => {
+ let catalog = self
+ .entries
+ .object_numbers(&self.data)
+ .into_iter()
+ .find(|n| matches!(self.try_get(*n).as_deref(), Ok(Object::Dict(d)) if d.name(b"Type") == Some(b"Catalog")));
+ let Some(c) = catalog else { return Err(CosError::Syntax { offset: 0, detail: "no document catalog found".into() }) };
+ let mut t = Dict::new();
+ t.set(b"Root".to_vec(), Object::Ref(ObjRef::new(c, self.generation(c))));
+ t
+ }
+ };
+ let mut trailer = trailer;
+ // What a cross-reference stream's dictionary carries for itself goes, as the chain
+ // readers drop it: a save writes its own.
+ for k in [&b"Prev"[..], b"XRefStm", b"Type", b"W", b"Index", b"Filter", b"DecodeParms", b"Length"] {
+ trailer.remove(k);
+ }
+ self.trailer = trailer;
+ self.revisions = Arc::new(Vec::new());
+ log.push(format!("rebuilt cross-reference index from {object_count} object headers"));
+ Ok(())
+ }
+ }
+
+ /// For a classic table at `at` whose first subsection is `0 N` and opens with an in-use row
+ /// (the rows of objects 1 to N), the number N its rows really end at.
+ fn shifted_subsection_end(data: &[u8], at: usize) -> Option<u32> {
+ let mut lx = Lexer::new(data, at);
+ if !lx.eat_keyword(b"xref") {
+ return None;
+ }
+ let (Ok(Object::Int(0)), Ok(Object::Int(count))) = (lx.object(), lx.object()) else { return None };
+ lx.skip_ws();
+ let offset = std::str::from_utf8(lx.token()).ok().and_then(|s| s.parse::<u64>().ok());
+ lx.skip_ws();
+ lx.token();
+ lx.skip_ws();
+ match (offset, lx.token()) {
+ (Some(offset), b"n") if offset != 0 => u32::try_from(count).ok().filter(|n| *n > 0),
+ _ => None,
+ }
+ }
+
+ /// Every `N G obj` header in `data`, in file order, as (number, offset, generation, position
+ /// after `obj`).
+ fn object_headers(data: &[u8]) -> Vec<(u32, u64, u16, usize)> {
+ let mut headers = Vec::new();
+ let mut i = 0;
+ while i < data.len() {
+ // Object headers start at a line start (or after whitespace).
+ let at_start = i == 0 || is_whitespace(data[i - 1]) || data[i - 1] == b'>';
+ if at_start && data[i].is_ascii_digit() {
+ let mut lx = Lexer::new(data, i);
+ let n = lx.token();
+ lx.skip_ws();
+ let g = lx.token();
+ if !n.is_empty() && !g.is_empty() && n.iter().all(u8::is_ascii_digit) && g.iter().all(u8::is_ascii_digit) && lx.eat_keyword(b"obj") {
+ let num = std::str::from_utf8(n).ok().and_then(|s| s.parse::<u32>().ok());
+ let generation = std::str::from_utf8(g).ok().and_then(|s| s.parse::<u16>().ok()).unwrap_or(0);
+ if let Some(num) = num {
+ headers.push((num, i as u64, generation, lx.pos));
+ }
+ i = lx.pos;
+ continue;
+ }
+ }
+ i += 1;
+ }
+ headers
+ }
+
+ /// The offset of the section before the one whose trailer this is (`/Prev`), if any. Some writers
+ /// give it as a reference, `N 0 R`. When the sections read so far (`lookup`) place that object
+ /// and it holds an offset (iText's `indirectPrev.pdf`), that is the offset; otherwise, as the
+ /// tiff2pdf files in JHOVE's PDF-HUL-157 cases need, the sections before are at N. Either way the
+ /// reference is noted. Any other value that is not an offset is noted and ends the chain.
+ fn prev_offset(data: &[u8], trailer: &Dict, off: u64, lookup: impl Fn(u32) -> Option<XrefEntry>, log: &mut Vec<String>) -> Option<u64> {
+ match trailer.get(b"Prev") {
+ Some(Object::Int(p)) if *p >= 0 => Some(*p as u64),
+ Some(Object::Ref(r)) => {
+ let held = match lookup(r.num) {
+ Some(XrefEntry::InFile { offset, generation }) if generation == r.generation => {
+ usize::try_from(offset).ok().and_then(|at| parse_indirect(data, at, &|_| None).ok()).and_then(|(at, value)| match value {
+ Object::Int(p) if at == *r && p >= 0 => Some(p as u64),
+ _ => None,
+ })
+ }
+ _ => None,
+ };
+ match held {
+ Some(p) => {
+ log.push(format!(
+ "the trailer at {off} gives /Prev as the reference {} {} R; reading the offset {p} it holds",
+ r.num, r.generation
+ ));
+ Some(p)
+ }
+ None => {
+ log.push(format!(
+ "the trailer at {off} gives /Prev as the reference {} {} R; reading it as offset {}",
+ r.num, r.generation, r.num
+ ));
+ Some(u64::from(r.num))
+ }
+ }
+ }
+ Some(_) => {
+ log.push(format!("the trailer at {off} has a /Prev that is not an offset; the sections before it are unreachable"));
+ None
+ }
+ None => None,
+ }
+ }
+
+ pub(crate) fn find(hay: &[u8], needle: &[u8], from: usize, to: usize) -> Option<usize> {
+ let to = to.min(hay.len());
+ if from >= to || needle.is_empty() || to - from < needle.len() {
+ return None;
+ }
+ hay[from..to].windows(needle.len()).position(|w| w == needle).map(|p| p + from)
+ }
+
+ pub(super) fn rfind(hay: &[u8], needle: &[u8], from: usize) -> Option<usize> {
+ if hay.len() < needle.len() {
+ return None;
+ }
+ (from..=hay.len() - needle.len()).rev().find(|&i| &hay[i..i + needle.len()] == needle)
+ }
+
-/// A file identifier derived from entropy supplied by the caller.
-pub(super) fn generated_id(seed: &[u8; 32]) -> Vec<u8> {
- use std::hash::{Hash, Hasher};
- let mut out = Vec::new();
- for i in 0..2u8 {
- let mut h = std::collections::hash_map::DefaultHasher::new();
- (seed, i, b"pdfcraft id").hash(&mut h);
- out.extend_from_slice(&h.finish().to_be_bytes());
- }
- out
-}
-
+ /// The object number a classic xref subsection really starts at. A common producer bug heads the
+ /// table `1 n` and then writes object 0's free entry (`0000000000 65535 f`) as its first row, so
+ /// every row would be read one number too high and object 1 as free. Such a subsection is read
+ /// from 0 when its second row, read that way, locates an object that is indeed numbered 1, at
+ /// that offset or, as `load` reads offsets, relative to a header shifted by `header_offset`.
+ /// `rows` is the position just after the subsection header.
+ fn subsection_start(data: &[u8], header_offset: usize, rows: usize, start: i64, count: i64) -> i64 {
+ if start != 1 || count < 2 {
+ return start;
+ }
+ let mut lx = Lexer::new(data, rows);
+ let mut row = || {
+ lx.skip_ws();
+ let offset = std::str::from_utf8(lx.token()).ok().and_then(|s| s.parse::<u64>().ok());
+ lx.skip_ws();
+ let generation = std::str::from_utf8(lx.token()).ok().and_then(|s| s.parse::<u32>().ok());
+ lx.skip_ws();
+ (offset, generation, lx.token() == b"f")
+ };
+ let (Some(0), Some(65535), true) = row() else { return start };
+ let (Some(second), _, false) = row() else { return start };
+ let object_one = |at: usize| {
+ if at >= data.len() {
+ return false;
+ }
+ let mut obj = Lexer::new(data, at);
+ matches!((obj.object(), obj.object()), (Ok(Object::Int(1)), Ok(Object::Int(_)))) && obj.eat_keyword(b"obj")
+ };
+ let Ok(second) = usize::try_from(second) else { return start };
+ if object_one(second) || (header_offset != 0 && object_one(second.saturating_add(header_offset))) { 0 } else { start }
+ }
+
+ /// Object numbers must fit the representation used by references and the xref map.
+ /// Validate the whole subsection before adding entries, so neither arithmetic overflow nor
+ /// truncation can turn a damaged range into entries for unrelated objects.
+ fn valid_xref_range(start: i64, count: i64) -> bool {
+ u32::try_from(start).is_ok() && count >= 0 && start.checked_add(count).is_some_and(|end| end <= i64::from(u32::MAX) + 1)
+ }
+
+ /// The most indirect objects a document can have (the classic implementation limit, ISO
+ /// 32000-1 Annex C): it bounds how much data a cross-reference stream can hold.
+ const MAX_XREF_OBJECTS: usize = 8_388_607;
diff --cc crates/cos/src/document/tests.rs
index 000000000,bf22f23d7..0ec2cc3a8
mode 000000,100644..100644
--- a/crates/cos/src/document/tests.rs
+++ b/crates/cos/src/document/tests.rs
@@@ -1,0 -1,1287 +1,1319 @@@
+ use super::*;
+
++#[test]
++fn a_missing_file_id_is_derived_from_the_encryption_seed() {
++ let mut doc = Document::new_empty();
++ let seed = [9u8; 32];
++ doc.set_encryption(&pdfcraft_crypt::NewEncryption {
++ algorithm: pdfcraft_crypt::Algorithm::Aes256,
++ user_password: "user",
++ owner_password: "owner",
++ permissions: -4,
++ encrypt_metadata: true,
++ seed,
++ })
++ .unwrap();
++ let id = doc.trailer().get(b"ID").and_then(|o| o.as_array()).and_then(|a| a.first()).and_then(|o| o.as_string()).map(|s| s.bytes.clone());
++ assert_eq!(id.as_deref(), Some(pdfcraft_crypt::file_id(&seed).as_slice()));
++
++ let mut kept = Document::new_empty();
++ let fixed = Object::String(crate::PdfString { bytes: b"0123456789abcdef".to_vec(), hex: true });
++ kept.trailer_mut().set(b"ID".to_vec(), Object::Array(vec![fixed.clone(), fixed]));
++ kept.set_encryption(&pdfcraft_crypt::NewEncryption {
++ algorithm: pdfcraft_crypt::Algorithm::Aes256,
++ user_password: "user",
++ owner_password: "owner",
++ permissions: -4,
++ encrypt_metadata: true,
++ seed,
++ })
++ .unwrap();
++ let still = kept.trailer().get(b"ID").and_then(|o| o.as_array()).and_then(|a| a.first()).and_then(|o| o.as_string()).map(|s| s.bytes.clone());
++ assert_eq!(still.as_deref(), Some(b"0123456789abcdef".as_slice()));
++}
++
+ #[test]
+ fn a_read_state_matches_a_clone_and_no_changed_document() {
+ let mut doc = Document::new_empty();
+ let a = doc.add(Object::Int(1));
+ let state = doc.read_state();
+ let snapshot = doc.clone();
+ assert!(state.matches(&doc) && state.matches(&snapshot));
+ // The same value set again is still a change: another allocation.
+ doc.set(a, Object::Int(1));
+ assert!(!state.matches(&doc) && state.matches(&snapshot));
+ let edited = doc.read_state();
+ doc.free(a);
+ assert!(!edited.matches(&doc));
+ let mut trailer_changed = snapshot.clone();
+ trailer_changed.trailer_mut().set(b"Info".to_vec(), Object::Null);
+ assert!(!state.matches(&trailer_changed));
+ // Another document's state never matches, even with the same changes.
+ let mut other = Document::new_empty();
+ other.add(Object::Int(1));
+ assert!(!other.read_state().matches(&snapshot));
+ }
+
+ /// Build a classic-xref file from object bodies (object i+1 = bodies[i]).
+ pub(crate) fn build(bodies: &[&str], trailer: &str) -> Vec<u8> {
+ let mut out = b"%PDF-1.7\n".to_vec();
+ let mut offsets = Vec::new();
+ for (i, b) in bodies.iter().enumerate() {
+ offsets.push(out.len());
+ out.extend_from_slice(format!("{} 0 obj\n{b}\nendobj\n", i + 1).as_bytes());
+ }
+ let xref = out.len();
+ out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", bodies.len() + 1).as_bytes());
+ for o in offsets {
+ out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
+ }
+ out.extend_from_slice(format!("trailer\n<< /Size {} {trailer} >>\nstartxref\n{xref}\n%%EOF\n", bodies.len() + 1).as_bytes());
+ out
+ }
+
+ fn assert_compact_matches_materialized(doc: &Document) {
+ assert!(matches!(doc.entries.as_ref(), XrefIndex::Compact { .. }), "valid xrefs should not retain a per-entry map");
+ let (entries, _, revisions, _) = doc.read_xref_chain_materialized(&mut Vec::new()).unwrap();
+ assert_eq!(revisions.as_slice(), doc.revisions());
+ let max = entries.keys().next_back().copied().unwrap_or(0);
+ assert_eq!(doc.entries.max_num(), max);
+ for num in 0..=max {
+ assert_eq!(doc.xref_entry(num), entries.get(&num).copied(), "object {num}");
+ }
+ let expected: Vec<_> = entries.iter().filter_map(|(&num, entry)| (!matches!(entry, XrefEntry::Free { .. })).then_some(num)).collect();
+ assert_eq!(doc.object_numbers(), expected);
+ }
+
+ #[test]
+ fn compact_xref_tables_streams_and_revisions_match_the_materialized_reader() {
+ let classic = Document::open(Arc::new(build(
+ &["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "<< /Value 9 >>"],
+ "/Root 1 0 R",
+ )))
+ .unwrap();
+ assert_compact_matches_materialized(&classic);
+
+ let mut sparse = b"%PDF-1.7\n".to_vec();
+ let root_offset = sparse.len();
+ sparse.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 5 0 R >>\nendobj\n");
+ let pages_offset = sparse.len();
+ sparse.extend_from_slice(b"5 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n");
+ let xref = sparse.len();
+ sparse.extend_from_slice(
+ format!(
+ "xref\n0 1\n0000000000 65535 f \n1 1\n{root_offset:010} 00000 n \n5 1\n{pages_offset:010} 00000 n \ntrailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n"
+ )
+ .as_bytes(),
+ );
+ let sparse = Document::open(Arc::new(sparse)).unwrap();
+ assert_eq!(sparse.object_numbers(), vec![1, 5]);
+ assert_compact_matches_materialized(&sparse);
+
+ let mut sparse_stream = b"%PDF-1.7\n".to_vec();
+ let mut offsets = [0u32; 8];
+ offsets[1] = sparse_stream.len() as u32;
+ sparse_stream.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 5 0 R >>\nendobj\n");
+ offsets[5] = sparse_stream.len() as u32;
+ sparse_stream.extend_from_slice(b"5 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n");
+ offsets[6] = sparse_stream.len() as u32;
+ sparse_stream.extend_from_slice(b"6 0 obj\n<< /Unused true >>\nendobj\n");
+ offsets[7] = sparse_stream.len() as u32;
+ let xref_row = |kind: u8, offset: u32, generation: u16| {
+ let mut row = Vec::with_capacity(7);
+ row.push(kind);
+ row.extend_from_slice(&offset.to_be_bytes());
+ row.extend_from_slice(&generation.to_be_bytes());
+ row
+ };
+ let mut rows = Vec::new();
+ rows.extend(xref_row(0, 0, u16::MAX));
+ rows.extend(xref_row(1, offsets[1], 0));
+ rows.extend(xref_row(1, offsets[5], 0));
+ rows.extend(xref_row(1, offsets[6], 0));
+ rows.extend(xref_row(1, offsets[7], 0));
+ sparse_stream.extend_from_slice(
+ format!("7 0 obj\n<< /Type /XRef /Size 8 /Root 1 0 R /W [1 4 2] /Index [0 2 5 3] /Length {} >>\nstream\n", rows.len()).as_bytes(),
+ );
+ sparse_stream.extend_from_slice(&rows);
+ sparse_stream.extend_from_slice(format!("\nendstream\nendobj\nstartxref\n{}\n%%EOF\n", offsets[7]).as_bytes());
+ let sparse_stream = Document::open(Arc::new(sparse_stream)).unwrap();
+ assert_eq!(sparse_stream.object_numbers(), vec![1, 5, 6, 7]);
+ assert_compact_matches_materialized(&sparse_stream);
+
+ let stream_bytes = crate::write_full(&classic, &crate::SaveOptions::default()).unwrap();
+ let stream = Document::open(Arc::new(stream_bytes)).unwrap();
+ assert!(stream.revisions().last().is_some_and(|revision| revision.is_stream));
+ assert_compact_matches_materialized(&stream);
+
+ let mut edited = classic.clone();
+ edited.update_dict(ObjRef::new(1, 0), |dict| dict.set(b"Lang".to_vec(), Object::String(crate::PdfString::literal("en")))).unwrap();
+ edited.free(ObjRef::new(3, 0));
+ let incremental = crate::write_incremental(&edited, &crate::SaveOptions::default()).unwrap();
+ let updated = Document::open(Arc::new(incremental)).unwrap();
+ assert_eq!(updated.revisions().len(), 2);
+ assert_compact_matches_materialized(&updated);
+ }
+
+ #[test]
+ fn malformed_size_uses_the_materialized_compatibility_path() {
+ let mut bytes = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "<< /Value 9 >>"], "/Root 1 0 R");
+ let marker = b"/Size 4";
+ let at = bytes.windows(marker.len()).position(|window| window == marker).unwrap();
+ bytes[at..at + marker.len()].copy_from_slice(b"/Size 2");
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert!(matches!(doc.entries.as_ref(), XrefIndex::Materialized(_)));
+ assert_eq!(doc.next_num, 4);
+ assert_eq!(doc.object_numbers(), vec![1, 2, 3]);
+ }
+
+ /// Entries the reader sets aside are remembered, so a caller comparing revisions can tell
+ /// that another reader may resolve the object differently.
+ #[test]
+ fn cross_reference_entries_that_are_set_aside_are_recorded() {
+ let base = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "(three)"], "/Root 1 0 R");
+ let plain = Document::open(Arc::new(base.clone())).unwrap();
+ assert!(plain.xref_oddities().is_empty());
+ assert!((1..=3).all(|n| plain.xref_entry_is_exact(n)));
+ let prev = String::from_utf8_lossy(&base).rfind("xref\n0 4").unwrap();
+ let third = plain.xref_entry(3).unwrap();
+
+ // A later section: object 3 "in use" at offset 0, object 2 listed twice with different
+ // offsets, and object 0 at offset 0 (the usual producer slip, which names no object).
+ let mut bytes = base.clone();
+ let section = bytes.len();
+ bytes.extend_from_slice(
+ format!("xref\n0 1\n0000000000 65535 n \n3 1\n0000000000 00000 n \n2 1\n0000000058 00000 n \n2 1\n0000000009 00000 n \ntrailer\n<< /Size 4 /Root 1 0 R /Prev {prev} >>\nstartxref\n{section}\n%%EOF\n").as_bytes(),
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.xref_oddities(), [(section as u64, 3), (section as u64, 2)]);
+ // What the reader resolves is what it resolved before: the first entry it kept.
+ assert_eq!(doc.xref_entry(3), Some(third));
+ assert_eq!(doc.xref_entry(2), Some(XrefEntry::InFile { offset: 58, generation: 0 }));
+ }
+
+ /// An entry that misses its object still resolves (by searching) but is not exact.
+ #[test]
+ fn an_entry_that_misses_its_object_is_found_by_searching_but_is_not_exact() {
+ let base = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "(three)"], "/Root 1 0 R");
+ let text = String::from_utf8_lossy(&base).into_owned();
+ let at = text.find("3 0 obj").unwrap();
+ let bytes = text.replacen(&format!("{at:010} 00000 n"), &format!("{:010} 00000 n", at + 2), 1).into_bytes();
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.get(ObjRef { num: 3, generation: 0 }).as_string().map(|s| s.bytes.clone()), Some(b"three".to_vec()));
+ assert!(!doc.xref_entry_is_exact(3));
+ assert!(doc.xref_entry_is_exact(1) && doc.xref_entry_is_exact(2));
+ }
+
+ /// A classic table with `/Size` rows and object 0's free entry first, its subsection numbered
+ /// `1 n` instead of `0 n` (#2676): every row is one number off.
+ fn misnumbered_table(start: u32) -> Vec<u8> {
+ let bodies = ["<< /Marker (first object) >>", "<< /Type /Pages /Kids [] /Count 0 >>", "<< /Type /Catalog /Pages 2 0 R >>"];
+ let mut out = b"%PDF-1.7
+ "
+ .to_vec();
+ let mut offsets = Vec::new();
+ for (i, body) in bodies.iter().enumerate() {
+ offsets.push(out.len());
+ out.extend_from_slice(
+ format!(
+ "{} 0 obj
+ {body}
+ endobj
+ ",
+ i + 1
+ )
+ .as_bytes(),
+ );
+ }
+ let xref = out.len();
+ out.extend_from_slice(format!("xref\n{start} 4\n0000000000 65535 f \n").as_bytes());
+ for o in offsets {
+ out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
+ }
+ out.extend_from_slice(format!("trailer\n<< /Size 4 /Root 3 0 R >>\nstartxref\n{xref}\n%%EOF\n").as_bytes());
+ out
+ }
+
+ #[test]
+ fn a_table_numbered_from_1_that_begins_with_object_0_is_read_from_0() {
+ let doc = Document::open(Arc::new(misnumbered_table(1))).unwrap();
+ let first = doc.get(ObjRef::new(1, 0)).as_dict().cloned().expect("object 1 is not free");
+ assert!(first.contains(b"Marker"));
+ assert_eq!(doc.root(), Some(ObjRef::new(3, 0)));
+ assert!(doc.repair_log().iter().any(|l| l.contains("numbered from 1")), "{:?}", doc.repair_log());
+ // A correct table reads the same and logs nothing.
+ let fine = Document::open(Arc::new(misnumbered_table(0))).unwrap();
+ assert!(fine.get(ObjRef::new(1, 0)).as_dict().is_some_and(|d| d.contains(b"Marker")));
+ assert!(!fine.repair_log().iter().any(|l| l.contains("numbered from 1")));
+ }
+
+ #[test]
+ fn a_table_that_really_starts_at_1_keeps_its_numbers() {
+ // Object 1 deleted for good (generation 65535) in a subsection that does start at 1: its
+ // second row locates object 2, so nothing is shifted.
+ let mut bytes = b"%PDF-1.7\n".to_vec();
+ let pages = bytes.len();
+ bytes.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n");
+ let catalog = bytes.len();
+ bytes.extend_from_slice(b"3 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
+ let xref = bytes.len();
+ bytes.extend_from_slice(
+ format!(
+ "xref\n0 1\n0000000000 65535 f \n1 3\n0000000000 65535 f \n{pages:010} 00000 n \n{catalog:010} 00000 n \ntrailer\n<< /Size 4 /Root 3 0 R >>\nstartxref\n{xref}\n%%EOF\n"
+ )
+ .as_bytes(),
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert!(doc.get(ObjRef::new(2, 0)).as_dict().is_some_and(|d| d.name(b"Type") == Some(b"Pages")));
+ assert!(doc.get(ObjRef::new(1, 0)).as_dict().is_none(), "object 1 stays free");
+ assert!(!doc.repair_log().iter().any(|l| l.contains("numbered from 1")));
+ }
+
+ /// A one-page scan: the page draws image 5 with content stream 4.
+ fn scanned_page() -> Vec<u8> {
+ build(
+ &[
+ "<< /Type /Catalog /Pages 2 0 R >>",
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] /Resources << /XObject << /Im0 5 0 R >> >> /Contents 4 0 R >>",
+ "<< /Length 21 >>\nstream\n10 0 0 10 0 0 cm /Im0 Do\nendstream",
+ "<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 1 >>\nstream\nA\nendstream",
+ ],
+ "/Root 1 0 R",
+ )
+ }
+
+ /// `bytes` with its cross-reference table replaced by `table` (the lines after `xref`).
+ fn with_table(bytes: &[u8], table: &[String], size: usize) -> Vec<u8> {
+ let text = String::from_utf8_lossy(bytes).into_owned();
+ let at = text.rfind("\nxref\n").unwrap() + 1;
+ let mut out = bytes[..at].to_vec();
+ out.extend_from_slice(format!("xref\n{}\ntrailer\n<< /Size {size} /Root 1 0 R >>\nstartxref\n{at}\n%%EOF\n", table.join("\n")).as_bytes());
+ out
+ }
+
+ /// The table's entry lines, object 0 first.
+ fn table_entries(bytes: &[u8]) -> Vec<String> {
+ let text = String::from_utf8_lossy(bytes).into_owned();
+ let at = text.rfind("\nxref\n").unwrap() + 1;
+ text[at..].lines().skip(2).take_while(|l| !l.starts_with("trailer")).map(str::to_string).collect()
+ }
+
+ /// The classic off-by-one: the subsection says it starts at 0 but lists objects 1 to 5, so
+ /// the last object has no entry. Viewers rebuild the table and show the page; a reader that
+ /// took the missing entry for a null object dropped the image when pages were copied (#1628).
+ #[test]
+ fn an_off_by_one_xref_subsection_still_finds_the_last_object() {
+ let base = scanned_page();
+ let entries = table_entries(&base);
+ let mut table = vec!["0 5".to_string()];
+ table.extend(entries[1..6].iter().cloned());
+ let doc = Document::open(Arc::new(with_table(&base, &table, 5))).unwrap();
+ let image = doc.get(ObjRef::new(5, 0));
+ assert_eq!(image.as_dict().and_then(|d| d.name(b"Subtype")), Some(&b"Image"[..]), "{image:?}");
+ assert!(matches!(&*image, Object::Stream(_)));
+ assert!(matches!(&*doc.get(ObjRef::new(4, 0)), Object::Stream(s) if &s.raw[..] == b"10 0 0 10 0 0 cm /Im0 Do"));
+ assert!(doc.repair_log().iter().any(|l| l.contains("object headers")), "{:?}", doc.repair_log());
+ }
+
+ /// An "in use" entry at offset 0 says nothing about where the object is: the object is
+ /// found by its header, and the repair is recorded.
+ #[test]
+ fn an_object_listed_at_offset_zero_is_found_by_its_header() {
+ let base = scanned_page();
+ for num in [4usize, 5] {
+ let mut table = vec!["0 6".to_string()];
+ table.extend(table_entries(&base));
+ table[num + 1] = "0000000000 00000 n ".into();
+ let doc = Document::open(Arc::new(with_table(&base, &table, 6))).unwrap();
+ assert!(matches!(&*doc.get(ObjRef::new(num as u32, 0)), Object::Stream(_)), "object {num}");
+ assert!(doc.repair_log().iter().any(|l| l.contains("object headers")), "object {num}: {:?}", doc.repair_log());
+ }
+ }
+
+ /// A valid table that simply does not list every number below `/Size` is not repaired: an
+ /// unlisted number is a null object (§7.3.10), even when an old body with that number is
+ /// still in the file.
+ #[test]
+ fn a_consistent_table_with_unlisted_numbers_is_taken_as_it_is() {
+ let base = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "(three)"], "/Root 1 0 R");
+ let at = String::from_utf8_lossy(&base).rfind("\nxref\n").unwrap() + 1;
+ let mut bytes = base[..at].to_vec();
+ bytes.extend_from_slice(b"4 0 obj\n(stale)\nendobj\n");
+ let mut table = vec!["0 4".to_string()];
+ table.extend(table_entries(&base));
+ let doc = Document::open(Arc::new(with_table(&[&bytes[..], &base[at..]].concat(), &table, 6))).unwrap();
+ assert!(doc.repair_log().is_empty(), "{:?}", doc.repair_log());
+ assert_eq!(*doc.get(ObjRef::new(4, 0)), Object::Null);
+ assert!(matches!(&*doc.get(ObjRef::new(3, 0)), Object::String(_)));
+ }
+
+ /// Filling the gaps of a damaged table never brings back an object that an incremental
+ /// update freed: its body is still in the file, but the newer section says it is gone.
+ #[test]
+ fn an_object_freed_by_an_update_stays_freed_when_the_table_is_repaired() {
+ let base = scanned_page();
+ // The original table lists the content stream at offset 0, so the gaps get filled.
+ let mut table = vec!["0 6".to_string()];
+ table.extend(table_entries(&base));
+ table[5] = "0000000000 00000 n ".into();
+ let mut bytes = with_table(&base, &table, 6);
+ let prev = String::from_utf8_lossy(&bytes).rfind("\nxref\n").unwrap() + 1;
+ let section = bytes.len();
+ bytes.extend_from_slice(
+ format!("xref\n5 1\n0000000000 00001 f \ntrailer\n<< /Size 6 /Root 1 0 R /Prev {prev} >>\nstartxref\n{section}\n%%EOF\n").as_bytes(),
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert!(matches!(&*doc.get(ObjRef::new(4, 0)), Object::Stream(_)), "the gap is filled");
+ assert_eq!(*doc.get(ObjRef::new(5, 0)), Object::Null, "the freed image stays freed");
+ assert_eq!(doc.xref_entry(5), Some(XrefEntry::Free { next_generation: 1 }));
+ }
+
+ /// Repairing a damaged table fills only the numbers the damage leaves out: an old body of a
+ /// number no section lists, under an oversized `/Size`, stays a null object.
+ #[test]
+ fn a_repaired_table_leaves_an_unlisted_number_null() {
+ let base = scanned_page();
+ let at = String::from_utf8_lossy(&base).rfind("\nxref\n").unwrap() + 1;
+ let entries = table_entries(&base);
+ let mut offset_zero = vec!["0 6".to_string()];
+ offset_zero.extend(entries.iter().cloned());
+ offset_zero[5] = "0000000000 00000 n ".into();
+ let mut off_by_one = vec!["0 5".to_string()];
+ off_by_one.extend(entries[1..6].iter().cloned());
+ for (name, table, found) in [("offset 0", offset_zero, 4), ("off by one", off_by_one, 5)] {
+ let bytes = [&base[..at], b"7 0 obj\n(stale)\nendobj\n", &base[at..]].concat();
+ let doc = Document::open(Arc::new(with_table(&bytes, &table, 9))).unwrap();
+ assert!(matches!(&*doc.get(ObjRef::new(found, 0)), Object::Stream(_)), "{name}: the gap is filled");
+ assert_eq!(*doc.get(ObjRef::new(7, 0)), Object::Null, "{name}: {:?}", doc.repair_log());
+ assert_eq!(doc.xref_entry(7), None, "{name}");
+ }
+ }
+
+ /// The last object of a subsection numbered one too low is filled from that subsection,
+ /// whatever later happens to the row before it: an incremental save that rewrites object 4,
+ /// or object 4's own row at offset 0.
+ #[test]
+ fn the_last_object_of_a_shifted_subsection_is_filled_whatever_its_neighbour_says() {
+ let base = scanned_page();
+ let entries = table_entries(&base);
+ let mut off_by_one = vec!["0 5".to_string()];
+ off_by_one.extend(entries[1..6].iter().cloned());
+ let shifted = with_table(&base, &off_by_one, 5);
+ let prev = String::from_utf8_lossy(&shifted).rfind("\nxref\n").unwrap() + 1;
+ let mut updated = shifted.clone();
+ let body = updated.len();
+ updated.extend_from_slice(b"4 0 obj\n<< /Length 21 >>\nstream\n10 0 0 10 0 0 cm /Im0 Do\nendstream\nendobj\n");
+ let section = updated.len();
+ updated.extend_from_slice(
+ format!("xref\n4 1\n{body:010} 00000 n \ntrailer\n<< /Size 6 /Root 1 0 R /Prev {prev} >>\nstartxref\n{section}\n%%EOF\n").as_bytes(),
+ );
+ let mut neighbour_at_zero = off_by_one.clone();
+ neighbour_at_zero[5] = "0000000000 00000 n ".into();
+ for (name, bytes) in [("updated", updated), ("row 4 at offset 0", with_table(&base, &neighbour_at_zero, 5))] {
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ let image = doc.get(ObjRef::new(5, 0));
+ assert_eq!(image.as_dict().and_then(|d| d.name(b"Subtype")), Some(&b"Image"[..]), "{name}: {:?}", doc.repair_log());
+ }
+ }
+
+ /// The image the first page draws as `/Im1`, followed through the page tree.
+ fn first_page_image(doc: &Document) -> Arc<Object> {
+ let catalog = doc.get(doc.root().unwrap());
+ let pages = doc.resolve(catalog.as_dict().unwrap().get(b"Pages").unwrap());
+ let page = doc.resolve(&pages.as_dict().unwrap().get(b"Kids").unwrap().as_array().unwrap()[0]);
+ let resources = doc.resolve(page.as_dict().unwrap().get(b"Resources").unwrap());
+ let xobjects = doc.resolve(resources.as_dict().unwrap().get(b"XObject").unwrap());
+ doc.resolve(xobjects.as_dict().unwrap().get(b"Im1").unwrap())
+ }
+
+ /// The opposite off-by-one (#1628): the subsection says it starts at 1, but its first entry
+ /// is object 0's free-list head, so every entry lands one number too high and object 1, the
+ /// image, reads as free. The offset the table gives object 2 holds `1 0 obj`, so the
+ /// subsection is read from 0, and the image survives a full save.
+ #[test]
+ fn a_subsection_from_one_that_opens_with_the_free_list_head_is_read_from_zero() {
+ let base = build(
+ &[
+ "<< /Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Length 12 >>\nstream\nABCDEFGHIJKL\nendstream",
+ "<< /Length 29 >>\nstream\nq 72 0 0 72 0 0 cm /Im1 Do Q\nendstream",
+ "<< /Type /Page /Parent 4 0 R /MediaBox [0 0 72 72] /Resources << /XObject << /Im1 1 0 R >> >> /Contents 2 0 R >>",
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
+ "<< /Type /Catalog /Pages 4 0 R >>",
+ "<< /Subject (synthetic) >>",
+ ],
+ "/Root 5 0 R /Info 6 0 R",
+ );
+ // /Size 7 is one short of the shifted numbers; /Size 8 covers them all. Junk before the
+ // header moves every object past the offsets the table gives; it is not a comment, which
+ // the lexer would skip on its way to `1 0 obj` anyway.
+ for (size, junk) in [(7, ""), (8, ""), (7, "Content-Type: application/pdf\n\n")] {
+ let text = String::from_utf8_lossy(&base).replace("\nxref\n0 7\n", "\nxref\n1 7\n").replace("/Size 7 ", &format!("/Size {size} "));
+ let doc = Document::open(Arc::new(format!("{junk}{text}").into_bytes())).unwrap();
+ let image = first_page_image(&doc);
+ assert_eq!(image.as_dict().and_then(|d| d.name(b"Subtype")), Some(&b"Image"[..]), "/Size {size} {junk:?}: {image:?}");
+ assert_eq!(doc.xref_entry(0), Some(XrefEntry::Free { next_generation: 65535 }), "/Size {size} {junk:?}");
+ assert!(doc.repair_log().iter().any(|l| l.contains("from 0")), "/Size {size} {junk:?}: {:?}", doc.repair_log());
+ let saved = Document::open(Arc::new(crate::write_full(&doc, &crate::SaveOptions::default()).unwrap())).unwrap();
+ let image = first_page_image(&saved);
+ assert!(matches!(&*image, Object::Stream(s) if &s.raw[..] == b"ABCDEFGHIJKL"), "/Size {size} {junk:?}: {image:?}");
+ }
+ }
+
+ /// Object 1 really freed: the subsection from 1 opens with a free entry, but the next entry's
+ /// offset holds object 2, not object 1. The table is taken as it is, and the old body of
+ /// object 1 still in the file stays freed.
+ #[test]
+ fn a_subsection_from_one_whose_next_offset_holds_object_two_is_taken_as_it_is() {
+ let base = build(&["(freed)", "<< /Type /Catalog /Pages 3 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 2 0 R");
+ let entries = table_entries(&base);
+ let mut table = vec!["1 3".to_string(), "0000000000 65535 f ".to_string()];
+ table.extend(entries[2..].iter().cloned());
+ let text = String::from_utf8_lossy(&base).into_owned();
+ let at = text.rfind("\nxref\n").unwrap() + 1;
+ for junk in ["", "Content-Type: application/pdf\n\n"] {
+ let mut bytes = junk.as_bytes().to_vec();
+ bytes.extend_from_slice(&base[..at]);
+ bytes.extend_from_slice(format!("xref\n{}\ntrailer\n<< /Size 4 /Root 2 0 R >>\nstartxref\n{at}\n%%EOF\n", table.join("\n")).as_bytes());
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.xref_entry(1), Some(XrefEntry::Free { next_generation: 65535 }), "{junk:?}");
+ assert_eq!(*doc.get(ObjRef::new(1, 0)), Object::Null, "{junk:?}: the freed object stays freed");
+ assert!(matches!(&*doc.get(ObjRef::new(2, 0)), Object::Dict(d) if d.name(b"Type") == Some(&b"Catalog"[..])), "{junk:?}");
+ assert!(!doc.repair_log().iter().any(|l| l.contains("from 0")), "{junk:?}: {:?}", doc.repair_log());
+ assert!(!junk.is_empty() || doc.repair_log().is_empty(), "{:?}", doc.repair_log());
+ }
+ }
+
+ #[test]
+ fn opening_an_exhausted_classic_xref_repairs_and_allocates() {
+ // The extra in-use row need not be loaded to exhaust the next object number.
+ let bytes = b"%PDF-1.7\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n2 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\nxref\n0 3\n0000000000 65535 f \n0000000009 00000 n \n0000000058 00000 n \n4294967295 1\n0000000058 00000 n \ntrailer\n<< /Size 3 /Root 1 0 R >>\nstartxref\n110\n%%EOF\n";
+ assert_eq!(bytes.len(), 266);
+ let mut doc = Document::open(Arc::new(bytes.to_vec())).unwrap();
+ assert_eq!(doc.root(), Some(ObjRef::new(1, 0)));
+ assert!(doc.repair_log().iter().any(|line| line.contains("exhausted xref")));
+ assert_eq!(doc.add(Object::Null), ObjRef::new(3, 0));
+ }
+
+ #[test]
+ fn opening_an_exhausted_xref_stream_repairs_and_allocates() {
+ let mut bytes = b"%PDF-1.7\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n2 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n".to_vec();
+ let offset = bytes.len();
+ bytes.extend_from_slice(b"3 0 obj\n<< /Type /XRef /Size 4 /Root 1 0 R /W [1 4 2] /Index [0 3 4294967295 1] /Length 28 >>\nstream\n");
+ for (kind, position, generation) in [(0_u8, 0_u32, 65535_u16), (1, 9, 0), (1, 58, 0), (1, 58, 0)] {
+ bytes.push(kind);
+ bytes.extend_from_slice(&position.to_be_bytes());
+ bytes.extend_from_slice(&generation.to_be_bytes());
+ }
+ bytes.extend_from_slice(format!("\nendstream\nendobj\nstartxref\n{offset}\n%%EOF\n").as_bytes());
+ let mut doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.root(), Some(ObjRef::new(1, 0)));
+ assert!(doc.repair_log().iter().any(|line| line.contains("exhausted xref")));
+ assert_eq!(doc.add(Object::Null), ObjRef::new(4, 0));
+ }
+
+ #[test]
+ fn exhausted_size_and_high_edits_allocate_without_overwriting_objects() {
+ let ordinary = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 1 0 R");
+ for size in ["4294967295", "4294967296", "9223372036854775807"] {
+ let bytes = String::from_utf8(ordinary.clone()).unwrap().replace("/Size 3", &format!("/Size {size}"));
+ let mut doc = Document::open(Arc::new(bytes.into_bytes())).unwrap();
+ assert!(doc.repair_log().iter().any(|line| line.contains("exhausted trailer")));
+ assert_eq!(doc.add(Object::Int(42)), ObjRef::new(3, 0));
+ doc.set(ObjRef::new(u32::MAX, 0), Object::Int(99));
+ doc.set(ObjRef::new(u32::MAX - 1, 0), Object::Int(98));
+ assert_eq!(doc.add(Object::Int(43)), ObjRef::new(4, 0));
+ assert_eq!(doc.get(ObjRef::new(3, 0)).as_int(), Some(42));
+ assert_eq!(doc.get(ObjRef::new(u32::MAX, 0)).as_int(), Some(99));
+ assert_eq!(doc.get(ObjRef::new(1, 0)).as_dict().unwrap().name(b"Type"), Some(b"Catalog".as_slice()));
+ }
+ let mut doc = Document::new_empty();
+ doc.set(ObjRef::new(u32::MAX - 2, 0), Object::Null);
+ assert_eq!(doc.add(Object::Null), ObjRef::new(u32::MAX - 1, 0));
+ assert_eq!(doc.add(Object::Null), ObjRef::new(3, 0));
+ assert_eq!(doc.add(Object::Null), ObjRef::new(4, 0));
+ }
+
+ #[test]
+ fn exhausted_xref_controls_preserve_ordinary_ignored_and_repaired_entries() {
+ let ordinary = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 1 0 R");
+ for (extra, repaired) in
+ [("", false), ("4294967295 1\n0000000000 00000 n \n", false), ("4294967295 2\n0000000058 00000 n \n0000000058 00000 n \n", true)]
+ {
+ let bytes = String::from_utf8(ordinary.clone()).unwrap().replacen("trailer\n", &format!("{extra}trailer\n"), 1);
+ let mut doc = Document::open(Arc::new(bytes.into_bytes())).unwrap();
+ assert_eq!(doc.root(), Some(ObjRef::new(1, 0)));
+ assert_eq!(doc.get(ObjRef::new(1, 0)).as_dict().unwrap().name(b"Type"), Some(b"Catalog".as_slice()));
+ assert_eq!(!doc.repair_log().is_empty(), repaired);
+ if repaired {
+ assert!(doc.repair_log().iter().any(|line| line.contains("xref") && line.contains("range")));
+ }
+ assert_eq!(doc.add(Object::Null), ObjRef::new(3, 0));
+ }
+ }
+
+ #[test]
+ fn invalid_xref_object_ranges_are_reconstructed() {
+ // Original in-memory PDF: an intact catalog followed by a damaged xref section.
+ for stream in [false, true] {
+ for (start, count) in [(i64::MAX, 2), (-1, 2), (4_294_967_296, 1), (4_294_967_295, 2)] {
+ let mut bytes =
+ b"%PDF-1.7\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n2 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n".to_vec();
+ let offset = bytes.len();
+ if stream {
+ bytes.extend_from_slice(
+ format!("3 0 obj\n<< /Type /XRef /Root 1 0 R /Size 4 /W [1 1 1] /Index [{start} {count}] /Length 6 >>\nstream\n").as_bytes(),
+ );
+ bytes.extend_from_slice(&[0, 0, 0, 0, 0, 0]);
+ bytes.extend_from_slice(b"\nendstream\nendobj\n");
+ } else {
+ bytes.extend_from_slice(
+ format!("xref\n{start} {count}\n0000000000 65535 f \n0000000000 65535 f \ntrailer\n<< /Root 1 0 R /Size 3 >>\n").as_bytes(),
+ );
+ }
+ bytes.extend_from_slice(format!("startxref\n{offset}\n%%EOF\n").as_bytes());
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert!(
+ doc.repair_log().iter().any(|line| line.contains("xref") && line.contains("range")),
+ "stream={stream}, start={start}, count={count}: {:?}",
+ doc.repair_log()
+ );
+ assert_eq!(doc.get(ObjRef::new(1, 0)).as_dict().unwrap().name(b"Type"), Some(b"Catalog".as_slice()));
+ }
+ }
+ }
+
+ #[test]
+ fn objects_only_an_unreadable_older_section_locates_are_found_by_scanning() {
+ // The newest section lists the catalog only; its /Prev points past the end of the file,
+ // so the page tree is reachable only by scanning object headers, as readers do (#1656).
+ let mut bytes = b"%PDF-1.7\n".to_vec();
+ let mut offsets = Vec::new();
+ for (num, body) in
+ [(1, "<< /Type /Catalog /Pages 2 0 R >>"), (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"), (3, "<< /Type /Page /Parent 2 0 R >>")]
+ {
+ offsets.push(bytes.len());
+ bytes.extend_from_slice(format!("{num} 0 obj\n{body}\nendobj\n").as_bytes());
+ }
+ let xref = bytes.len();
+ bytes.extend_from_slice(
+ format!(
+ "xref\n0 2\n0000000000 65535 f \n{:010} 00000 n \ntrailer\n<< /Root 1 0 R /Size 4 /Prev 999999 >>\nstartxref\n{xref}\n%%EOF\n",
+ offsets[0]
+ )
+ .as_bytes(),
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.get(ObjRef::new(2, 0)).as_dict().and_then(|d| d.name(b"Type")), Some(b"Pages".as_slice()));
+ assert_eq!(doc.get(ObjRef::new(3, 0)).as_dict().and_then(|d| d.name(b"Type")), Some(b"Page".as_slice()));
+ assert!(doc.repair_log().iter().any(|l| l.contains("999999")), "{:?}", doc.repair_log());
+ }
+
+ #[test]
+ fn self_referential_length_does_not_overflow() {
+ let bytes = build(
+ &["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "<< /Length 3 0 R >>\nstream\nabc\nendstream"],
+ "/Root 1 0 R",
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ // Either recovered via the endstream search or reported — but never a stack overflow.
+ let o = doc.get(ObjRef::new(3, 0));
+ if let Object::Stream(s) = o.as_ref() {
+ assert_eq!(&s.raw[..], b"abc");
+ }
+ }
+
+ #[test]
+ fn mutually_referential_lengths_do_not_overflow() {
+ let bytes = build(
+ &[
+ "<< /Type /Catalog /Pages 2 0 R >>",
+ "<< /Type /Pages /Kids [] /Count 0 >>",
+ "<< /Length 4 0 R >>\nstream\nabc\nendstream",
+ "<< /Length 3 0 R >>\nstream\nxyz\nendstream",
+ ],
+ "/Root 1 0 R",
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ let _ = doc.get(ObjRef::new(3, 0));
+ let _ = doc.get(ObjRef::new(4, 0));
+ }
+
+ #[test]
+ fn reads_classic_revision_and_edits_are_copy_on_write() {
+ let bytes = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 1 0 R");
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.revisions().len(), 1);
+ assert!(doc.repair_log().is_empty());
+ let mut edited = doc.clone();
+ edited.update_dict(ObjRef::new(1, 0), |d| d.set(b"Lang".to_vec(), Object::String(crate::PdfString::literal("en")))).unwrap();
+ assert!(edited.is_modified() && !doc.is_modified(), "clone is an independent snapshot");
+ assert!(doc.get(ObjRef::new(1, 0)).as_dict().unwrap().get(b"Lang").is_none());
+ }
+
+ #[test]
+ fn object_numbers_merge_source_and_overlay_in_order() {
+ let mut doc = Document::new_empty();
+ doc.overlay.clear();
+ doc.entries = Arc::new(XrefIndex::Materialized(Arc::new(BTreeMap::from([
+ (0, XrefEntry::Free { next_generation: 65535 }),
+ (2, XrefEntry::InFile { offset: 10, generation: 7 }),
+ (4, XrefEntry::Free { next_generation: 9 }),
+ (6, XrefEntry::InStream { stream: 80, index: 1 }),
+ (8, XrefEntry::InFile { offset: 20, generation: 5 }),
+ (10, XrefEntry::InFile { offset: 30, generation: 3 }),
+ (u32::MAX, XrefEntry::InFile { offset: 40, generation: 1 }),
+ ]))));
+ doc.set(ObjRef::new(1, 2), Object::Int(1)); // before the first live source slot
+ doc.set(ObjRef::new(2, 8), Object::Int(2)); // override, without duplication
+ doc.set(ObjRef::new(4, 9), Object::Int(4)); // reuse a source free slot
+ doc.free(ObjRef::new(6, 0)); // remove a compressed source object
+ doc.free(ObjRef::new(7, 2)); // free a number absent from the source
+ doc.free(ObjRef::new(10, 3));
+ doc.set(ObjRef::new(11, 4), Object::Int(11));
+ assert_eq!(doc.object_numbers(), vec![1, 2, 4, 8, 11, u32::MAX]);
+ assert_eq!(doc.generation(2), 8);
+ assert_eq!(doc.generation(4), 9);
+ assert_eq!(doc.generation(6), 1);
+ assert_eq!(doc.generation(8), 5);
+ assert_eq!(doc.generation(10), 4);
+ doc.overlay.clear();
+ assert_eq!(doc.object_numbers(), vec![2, 6, 8, 10, u32::MAX]);
+ doc.entries = Arc::new(XrefIndex::empty());
+ assert!(doc.object_numbers().is_empty());
+ doc.set(ObjRef::new(5, 7), Object::Int(5));
+ assert_eq!(doc.object_numbers(), vec![5]);
+ }
+
+ #[test]
+ fn full_object_scans_preserve_edits_without_retaining_the_scan() {
+ let mut bodies = vec!["<< /Type /Catalog /Pages 2 0 R >>".to_owned(), "<< /Type /Pages /Kids [] /Count 0 >>".to_owned()];
+ bodies.extend((0..400).map(|i| format!("<< /Value {i} >>")));
+ let refs: Vec<_> = bodies.iter().map(String::as_str).collect();
+ let mut doc = Document::open(Arc::new(build(&refs, "/Root 1 0 R"))).unwrap();
+ doc.set(ObjRef::new(3, 0), Object::Int(900));
+ doc.free(ObjRef::new(4, 0));
+ let added = doc.add(Object::Int(901));
+ let cached = doc.cache.lock().unwrap().len();
+ let held: Vec<_> = doc.scan_objects().collect();
+ assert_eq!(held.len(), 402);
+ assert_eq!(doc.cache.lock().unwrap().len(), cached, "scanning must not populate the editor cache");
+ assert!(doc.objstms.lock().unwrap().is_empty());
+ assert!(!held.iter().any(|(r, _)| r.num == 4));
+ assert_eq!(held.iter().find(|(r, _)| r.num == 3).unwrap().1.as_int(), Some(900));
+ assert_eq!(held.iter().find(|(r, _)| *r == added).unwrap().1.as_int(), Some(901));
+ assert_eq!(held.iter().find(|(r, _)| r.num == 400).unwrap().1.as_dict().unwrap().int(b"Value"), Some(397));
+ // Previously returned objects survive cache eviction and dropping the source.
+ drop(doc);
+ assert_eq!(held.iter().find(|(r, _)| r.num == 128).unwrap().1.as_dict().unwrap().int(b"Value"), Some(125));
+ }
+
+ #[test]
+ fn temporary_reader_keeps_file_views_and_evicts_data_of_its_own() {
+ // A stream read from the file is a view into the input: caching it costs no copy, so
+ // even a large one stays cached (its /Length object too) instead of being re-parsed.
+ let body = "x".repeat(ObjectReader::STREAM_BYTES + 1);
+ let stream = format!("<< /Length 4 0 R >>\nstream\n{body}\nendstream");
+ let length = body.len().to_string();
+ let bytes = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", &stream, &length], "/Root 1 0 R");
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ let mut reader = doc.object_reader();
+ let read = reader.get(ObjRef::new(3, 0));
+ let Object::Stream(s) = &*read else { panic!("stream") };
+ assert!(s.raw.shares(&reader.document.data), "no copy of the file's bytes");
+ assert!(reader.document.cache.lock().unwrap().contains_key(&3), "a view costs nothing to keep");
+ // Data of its own (decrypted streams, strings) counts: past the budget the cache is
+ // cleared after the next read, including a failed one.
+ let owned = Object::Stream(crate::Stream::from_raw(Dict::new(), vec![b'y'; ObjectReader::STREAM_BYTES + 1]));
+ reader.document.cache.lock().unwrap().insert(3, Arc::new(owned));
+ assert_eq!(*reader.get(ObjRef::new(99, 0)), Object::Null);
+ assert!(reader.document.cache.lock().unwrap().is_empty(), "owned data over budget is evicted");
+ }
+
+ #[test]
+ fn temporary_reader_and_full_save_do_not_fill_snapshot_caches() {
+ let mut source = Document::new_empty();
+ let objects: Vec<_> = (0..260)
+ .map(|i| {
+ let mut value = vec![b'x'; 48 * 1024];
+ value[..4].copy_from_slice(&(i as u32).to_le_bytes());
+ Object::Ref(source.add(Object::String(crate::PdfString::literal(value))))
+ })
+ .collect();
+ source.update_dict(source.root().unwrap(), |d| d.set(b"Extension".to_vec(), Object::Array(objects))).unwrap();
+ let bytes = Arc::new(crate::write_full(&source, &crate::SaveOptions::default()).unwrap());
+ let doc = Document::open(bytes).unwrap();
+ let root = doc.get(doc.root().unwrap());
+ let references = root.as_dict().unwrap().get(b"Extension").unwrap().as_array().unwrap();
+ let snapshot = doc.clone();
+ let cached = doc.cache.lock().unwrap().len();
+ let streams = doc.objstms.lock().unwrap().len();
+ let mut reader = doc.object_reader();
+ let mut held = None;
+ for (i, reference) in references.iter().enumerate() {
+ let Object::Ref(reference) = reference else { panic!("indirect object") };
+ let object = reader.get(*reference);
+ assert_eq!(&object.as_string().unwrap().bytes[..4], &(i as u32).to_le_bytes());
+ if i == 0 {
+ held = Some(object);
+ }
+ assert!(reader.document.cache.lock().unwrap().len() < ObjectReader::OBJECTS);
+ let decoded = reader.document.objstms.lock().unwrap();
+ assert!(decoded.len() < ObjectReader::OBJECTS);
+ let bytes: usize = decoded.values().map(|s| s.data.capacity() + s.index.capacity() * std::mem::size_of::<Option<(u32, usize)>>()).sum();
+ assert!(bytes <= ObjectReader::STREAM_BYTES || decoded.len() == 1, "only one oversized stream may remain");
+ }
+ drop(reader);
+ assert_eq!(&held.unwrap().as_string().unwrap().bytes[..4], &0u32.to_le_bytes(), "owned results survive evictions");
+ for object_streams in [false, true] {
+ let saved = crate::write_full(&doc, &crate::SaveOptions { object_streams, ..Default::default() }).unwrap();
+ assert!(Document::open(Arc::new(saved)).unwrap().repair_log().is_empty());
+ assert_eq!(doc.cache.lock().unwrap().len(), cached, "full saves must not fill the editor cache");
+ assert_eq!(doc.objstms.lock().unwrap().len(), streams);
+ assert!(Arc::ptr_eq(&doc.cache, &snapshot.cache), "do not detach or clear caller snapshots");
+ }
+ }
+
+ #[test]
+ fn streams_without_length_share_one_endstream_search() {
+ let n = 2_000;
+ let mut file = b"%PDF-1.7\n".to_vec();
+ for k in 1..=n {
+ file.extend_from_slice(format!("{k} 0 obj\n<< >>\nstream\nxx\n").as_bytes());
+ }
+ file.extend_from_slice(b"endstream\nendobj\n");
+ file.extend_from_slice(
+ format!("{0} 0 obj\n<< /Type /Catalog /Pages {1} 0 R >>\nendobj\n{1} 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n", n + 1, n + 2)
+ .as_bytes(),
+ );
+ file.extend_from_slice(format!("trailer\n<< /Root {} 0 R /Size {} >>\n%%EOF\n", n + 1, n + 3).as_bytes());
+ let len = file.len();
+ crate::parser::ENDSTREAM_SCAN.with(|c| c.set(0));
+ let doc = Document::open(Arc::new(file)).unwrap();
+ for k in [1, n / 2, n] {
+ assert!(matches!(doc.try_get(k).as_deref(), Ok(Object::Stream(_))), "object {k}");
+ }
+ let scanned = crate::parser::ENDSTREAM_SCAN.with(|c| c.get());
+ assert!(scanned <= 4 * len, "{scanned} bytes searched in a {len}-byte file");
+ }
+
+ #[test]
+ fn editor_cache_counts_deeply_nested_strings() {
+ let mut nested = Object::String(crate::PdfString::literal(vec![b'x'; 1 << 20]));
+ for _ in 0..8 {
+ nested = Object::Array(vec![nested]);
+ }
+ let mut source = Document::new_empty();
+ let objects: Vec<_> = (0..48).map(|_| Object::Ref(source.add(nested.clone()))).collect();
+ source.update_dict(source.root().unwrap(), |d| d.set(b"Extension".to_vec(), Object::Array(objects))).unwrap();
+ let bytes = Arc::new(crate::write_full(&source, &crate::SaveOptions::default()).unwrap());
+ let doc = Document::open(bytes).unwrap();
+ assert_eq!(owned_bytes(&nested, &doc.data, 0), 1 << 20);
+ let root = doc.get(doc.root().unwrap());
+ for reference in root.as_dict().unwrap().get(b"Extension").unwrap().as_array().unwrap() {
+ let Object::Ref(reference) = reference else { panic!("indirect object") };
+ drop(doc.get(*reference));
+ let cache = doc.cache.lock().unwrap();
+ let owned = cache.values().fold(0usize, |n, o| n.saturating_add(owned_bytes(o, &doc.data, 0)));
+ let strings = cache.values().filter(|o| o.as_array().is_some()).count();
+ assert!(strings << 20 <= Document::EDITOR_CACHE_BYTES + (1 << 20), "{strings} MiB strings cached, {owned} bytes counted");
+ }
+ }
+
+ #[test]
+ fn editor_cache_stays_bounded() {
+ let mut source = Document::new_empty();
+ let objects: Vec<_> = (0..(Document::EDITOR_CACHE_OBJECTS + 64))
+ .map(|i| {
+ let mut value = vec![b'x'; 1024];
+ value[..4].copy_from_slice(&(i as u32).to_le_bytes());
+ Object::Ref(source.add(Object::String(crate::PdfString::literal(value))))
+ })
+ .collect();
+ source.update_dict(source.root().unwrap(), |d| d.set(b"Extension".to_vec(), Object::Array(objects))).unwrap();
+ let bytes = Arc::new(crate::write_full(&source, &crate::SaveOptions::default()).unwrap());
+ let doc = Document::open(bytes).unwrap();
+ let root = doc.get(doc.root().unwrap());
+ let references = root.as_dict().unwrap().get(b"Extension").unwrap().as_array().unwrap().clone();
+ let mut held = None;
+ for (i, reference) in references.iter().enumerate() {
+ let Object::Ref(reference) = reference else { panic!("indirect object") };
+ let object = doc.get(*reference);
+ assert_eq!(&object.as_string().unwrap().bytes[..4], &(i as u32).to_le_bytes());
+ if i == 0 {
+ held = Some(object);
+ }
+ assert!(doc.cache.lock().unwrap().len() <= Document::EDITOR_CACHE_OBJECTS, "editing must not pin the whole object graph");
+ // Eviction keeps the newcomer.
+ assert!(doc.cache.lock().unwrap().contains_key(&reference.num), "eviction must keep the newcomer");
+ }
+ // Returned objects survive eviction, through both handles of the shared cache.
+ assert_eq!(&held.unwrap().as_string().unwrap().bytes[..4], &0u32.to_le_bytes());
+ let snapshot = doc.clone();
+ let first = references[0].as_ref().unwrap();
+ assert_eq!(&snapshot.get(first).as_string().unwrap().bytes[..4], &0u32.to_le_bytes());
+ }
+
+ #[test]
+ fn editor_cache_byte_bound_fires() {
+ // 40 × 1 MB strings: far below the count cap, above the 32 MB byte budget. Each
+ // newcomer exceeds EDITOR_LARGE_OBJECT, so every insert takes a full accounting pass.
+ let mut source = Document::new_empty();
+ let objects: Vec<_> = (0..40)
+ .map(|i| {
+ let mut value = vec![b'y'; 1024 * 1024];
+ value[..4].copy_from_slice(&(i as u32).to_le_bytes());
+ Object::Ref(source.add(Object::String(crate::PdfString::literal(value))))
+ })
+ .collect();
+ source.update_dict(source.root().unwrap(), |d| d.set(b"Extension".to_vec(), Object::Array(objects))).unwrap();
+ let bytes = Arc::new(crate::write_full(&source, &crate::SaveOptions::default()).unwrap());
+ let doc = Document::open(bytes).unwrap();
+ let root = doc.get(doc.root().unwrap());
+ let references = root.as_dict().unwrap().get(b"Extension").unwrap().as_array().unwrap().clone();
+ let mut held = None;
+ for (i, reference) in references.iter().enumerate() {
+ let Object::Ref(reference) = reference else { panic!("indirect object") };
+ let object = doc.get(*reference);
+ assert_eq!(&object.as_string().unwrap().bytes[..4], &(i as u32).to_le_bytes());
+ if i == 0 {
+ held = Some(object);
+ }
+ // Eviction keeps the newcomer.
+ assert!(doc.cache.lock().unwrap().contains_key(&reference.num), "eviction must keep the newcomer");
+ }
+ assert!(doc.cache.lock().unwrap().len() < references.len(), "the byte budget must evict even below the count cap");
+ let last = references[references.len() - 1].as_ref().unwrap();
+ assert!(doc.cache.lock().unwrap().contains_key(&last.num), "eviction must keep the newcomer");
+ assert_eq!(&held.unwrap().as_string().unwrap().bytes[..4], &0u32.to_le_bytes());
+ }
+
+ #[test]
+ fn a_stream_limited_document_cannot_be_saved() {
+ let bytes = Arc::new(crate::write_full(&Document::new_empty(), &crate::SaveOptions::default()).unwrap());
+ let doc = Document::open_with_stream_limit(bytes.clone(), None, 1 << 20).unwrap();
+ for saved in [crate::write_full(&doc, &crate::SaveOptions::default()), crate::write_incremental(&doc, &crate::SaveOptions::default())] {
+ assert!(matches!(saved, Err(CosError::ReadOnlyLimit)), "{saved:?}");
+ }
+ let doc = Document::open(bytes).unwrap();
+ assert!(crate::write_full(&doc, &crate::SaveOptions::default()).is_ok());
+ }
+
+ #[test]
+ fn inspection_stream_limit_applies_before_loading_compressed_objects() {
+ let mut doc = Document::new_empty();
+ let large = doc.add(Object::String(crate::PdfString::literal(vec![b'x'; 4096])));
+ doc.update_dict(doc.root().unwrap(), |catalog| catalog.set(b"Extension".to_vec(), Object::Ref(large))).unwrap();
+ let bytes = Arc::new(crate::write_full(&doc, &crate::SaveOptions::default()).unwrap());
+ assert!(Document::open(bytes.clone()).is_ok());
+ assert!(Document::open_with_stream_limit(bytes, None, 512).is_err());
+ }
+
+ /// A /Prev chain that loops back on itself stops at the loop (and says so).
+ #[test]
+ fn looping_revision_chains_stop() {
+ let mut bytes = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 1 0 R");
+ // An update whose /Prev points at itself.
+ let at = bytes.len();
+ bytes.extend_from_slice(
+ format!("xref\n0 1\n0000000000 65535 f \ntrailer << /Size 3 /Root 1 0 R /Prev {at} >>\nstartxref\n{at}\n%%EOF\n").as_bytes(),
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.root(), Some(ObjRef::new(1, 0)));
+ assert!(doc.repair_log().iter().any(|l| l.contains("loops")), "{:?}", doc.repair_log());
+ }
+
+ /// A hybrid-reference file: the classic table leaves object 3 out; the `/XRefStm` stream
+ /// says it lives in object stream 4.
+ #[test]
+ fn hybrid_reference_files_read_the_xref_stream_too() {
+ let doc = Document::open(Arc::new(object_stream_file("3 0 ", "<< /Hidden (yes) >>", 0))).unwrap();
+ assert!(doc.repair_log().is_empty(), "{:?}", doc.repair_log());
+ assert_compact_matches_materialized(&doc);
+ assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes"));
+ }
+
+ /// A hybrid-reference file whose classic table leaves object 3 out, and whose `/XRefStm`
+ /// stream puts it at position `member` of object stream 4, which holds `header` (the number
+ /// and offset pairs) and then `members`.
+ fn object_stream_file(header: &str, members: &str, member: u8) -> Vec<u8> {
+ let mut out = b"%PDF-1.5\n".to_vec();
+ let mut offs = [0usize; 6];
+ let mut obj = |out: &mut Vec<u8>, n: usize, body: &[u8]| {
+ offs[n] = out.len();
+ out.extend_from_slice(format!("{n} 0 obj\n").as_bytes());
+ out.extend_from_slice(body);
+ out.extend_from_slice(b"\nendobj\n");
+ };
+ obj(&mut out, 1, b"<< /Type /Catalog /Pages 2 0 R >>");
+ obj(&mut out, 2, b"<< /Type /Pages /Kids [] /Count 0 /Extra 3 0 R >>");
+ let count = header.split_whitespace().count() / 2;
+ let inner = format!("{header}{members}");
+ let mut stm = format!("<< /Type /ObjStm /N {count} /First {} /Length {} >>\nstream\n", header.len(), inner.len()).into_bytes();
+ stm.extend_from_slice(inner.as_bytes());
+ stm.extend_from_slice(b"\nendstream");
+ obj(&mut out, 4, &stm);
+ let data = [2u8, 0, 4, member];
+ let mut xs = format!("<< /Type /XRef /Size 6 /W [1 2 1] /Index [3 1] /Length {} >>\nstream\n", data.len()).into_bytes();
+ xs.extend_from_slice(&data);
+ xs.extend_from_slice(b"\nendstream");
+ obj(&mut out, 5, &xs);
+ let table = out.len();
+ out.extend_from_slice(b"xref\n0 6\n0000000000 65535 f \n");
+ for (n, off) in offs.iter().enumerate().skip(1) {
+ let line = if n == 3 { "0000000000 65535 f \n".to_string() } else { format!("{off:010} 00000 n \n") };
+ out.extend_from_slice(line.as_bytes());
+ }
+ out.extend_from_slice(format!("trailer << /Size 6 /Root 1 0 R /XRefStm {} >>\nstartxref\n{table}\n%%EOF\n", offs[5]).as_bytes());
+ out
+ }
+
+ /// Many revisions naming one hybrid /XRefStm read it once.
+ #[test]
+ fn a_hybrid_stream_named_by_many_revisions_is_read_once() {
+ let mut out = object_stream_file("3 0 ", "<< /Hidden (yes) >>", 0);
+ let stream = find(&out, b"5 0 obj", 0, out.len()).unwrap();
+ let mut prev = rfind(&out, b"\nxref\n", 0).unwrap() + 1;
+ let revisions = 200;
+ for _ in 0..revisions {
+ let at = out.len();
+ out.extend_from_slice(format!("xref\ntrailer << /Size 6 /Root 1 0 R /XRefStm {stream} /Prev {prev} >>\nstartxref\n{at}\n%%EOF\n").as_bytes());
+ prev = at;
+ }
+ let doc = Document::open(Arc::new(out)).unwrap();
+ SECTION_READS.with(|c| c.set(0));
+ let (entries, ..) = doc.read_xref_chain_materialized(&mut Vec::new()).unwrap();
+ let reads = SECTION_READS.with(|c| c.get());
+ assert!(reads <= revisions + 2, "{reads} sections read for {} revisions", revisions + 1);
+ assert!(matches!(entries.get(&3), Some(XrefEntry::InStream { stream: 4, .. })), "{:?}", entries.get(&3));
+ assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes"));
+ assert_compact_matches_materialized(&doc);
+ }
+
+ /// An older section that names an /XRefStm a newer section already read still has its own
+ /// table compared with that stream, so an entry it sets aside is recorded for it.
+ #[test]
+ fn a_repeated_hybrid_stream_records_each_sections_conflicts() {
+ let mut out = object_stream_file("3 0 ", "<< /Hidden (yes) >>", 0);
+ let stream = find(&out, b"5 0 obj", 0, out.len()).unwrap();
+ let catalog = find(&out, b"1 0 obj", 0, out.len()).unwrap();
+ let base = rfind(&out, b"\nxref\n", 0).unwrap() + 1;
+ // This section's table places object 3 at the catalog; the stream puts it in object stream 4.
+ let older = out.len();
+ out.extend_from_slice(
+ format!("xref\n3 1\n{catalog:010} 00000 n \ntrailer << /Size 6 /Root 1 0 R /XRefStm {stream} /Prev {base} >>\nstartxref\n{older}\n%%EOF\n")
+ .as_bytes(),
+ );
+ let newer = out.len();
+ out.extend_from_slice(format!("xref\ntrailer << /Size 6 /Root 1 0 R /XRefStm {stream} /Prev {older} >>\nstartxref\n{newer}\n%%EOF\n").as_bytes());
+ let doc = Document::open(Arc::new(out)).unwrap();
+ assert_eq!(doc.xref_oddities(), [(older as u64, 3)]);
+ assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes"));
+ }
+
+ fn text_of(doc: &Document, num: u32, key: &[u8]) -> Option<String> {
+ doc.get(ObjRef::new(num, 0)).as_dict().and_then(|d| d.get(key).and_then(|v| v.as_string().map(|s| s.to_text())))
+ }
+
+ /// An object stream member numbered 2^32 + 3 is not object 3 (#1815): the number used to be
+ /// cast to `u32`, which dropped the high bits.
+ #[test]
+ fn object_stream_numbers_beyond_u32_are_not_truncated() {
+ let control = Document::open(Arc::new(object_stream_file("3 0 ", "<< /Member (yes) >>", 0))).unwrap();
+ assert_eq!(text_of(&control, 3, b"Member").as_deref(), Some("yes"));
+ let doc = Document::open(Arc::new(object_stream_file("4294967299 0 ", "<< /Member (yes) >>", 0))).unwrap();
+ assert!(matches!(*doc.get(ObjRef::new(3, 0)), Object::Null), "{:?}", doc.get(ObjRef::new(3, 0)));
+ assert!(!doc.xref_entry_is_exact(3));
+ }
+
+ /// Member starts are checked rather than cast, so they cannot wrap on any target (#1815).
+ #[test]
+ fn object_stream_member_starts_are_checked() {
+ assert_eq!(objstm_member(3, 4, Some(10), 20), Some((3, 14)));
+ assert_eq!(objstm_member(3, 9, Some(10), 20), Some((3, 19)), "the last byte");
+ assert_eq!(objstm_member(3, 10, Some(10), 20), None, "at the end of the data");
+ assert_eq!(objstm_member(3, 1, Some(usize::MAX), 20), None, "/First plus the offset overflows");
+ assert_eq!(objstm_member(3, i64::MAX, Some(10), 20), None);
+ assert_eq!(objstm_member(3, -1, Some(10), 20), None);
+ assert_eq!(objstm_member(3, 0, None, 20), None, "/First does not fit in usize");
+ assert_eq!(objstm_member(i64::from(u32::MAX), 0, Some(0), 1), Some((u32::MAX, 0)));
+ assert_eq!(objstm_member(i64::from(u32::MAX) + 1, 0, Some(0), 1), None);
+ assert_eq!(objstm_member(-1, 0, Some(0), 1), None);
+ }
+
+ /// A member at a negative offset is unusable, not the stream's first member (#1815): the
+ /// offset used to be clamped to zero, which read the object at `/First`.
+ #[test]
+ fn object_stream_negative_offsets_are_refused() {
+ let doc = Document::open(Arc::new(object_stream_file("7 0 3 -1 ", "<< /First (yes) >>", 1))).unwrap();
+ assert!(matches!(*doc.get(ObjRef::new(3, 0)), Object::Null), "{:?}", doc.get(ObjRef::new(3, 0)));
+ let control = Document::open(Arc::new(object_stream_file("7 0 3 19 ", "<< /First (yes) >> << /Second (yes) >>", 1))).unwrap();
+ assert_eq!(text_of(&control, 3, b"Second").as_deref(), Some("yes"));
+ }
+
+ #[test]
+ fn revision_ends_split_incremental_updates() {
+ let mut bytes = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 1 0 R");
+ let first = bytes.len();
+ let doc = Document::open(Arc::new(bytes.clone())).unwrap();
+ assert_eq!(doc.revision_ends(), vec![first]);
+ let mut edited = doc.clone();
+ edited.update_dict(ObjRef::new(1, 0), |d| d.set(b"Lang".to_vec(), Object::String(crate::PdfString::literal("en")))).unwrap();
+ bytes = crate::write_incremental(&edited, &crate::SaveOptions::default()).unwrap();
+ let doc = Document::open(Arc::new(bytes.clone())).unwrap();
+ let ends = doc.revision_ends();
+ assert_eq!(ends.len(), 2, "{ends:?}");
+ assert!(ends[0] >= first && ends[0] < bytes.len() && ends[1] == bytes.len(), "{ends:?} of {}", bytes.len());
+ // The first revision opens on its own, without the update.
+ let old = Document::open(Arc::new(bytes[..ends[0]].to_vec())).unwrap();
+ assert!(old.get(ObjRef::new(1, 0)).as_dict().unwrap().get(b"Lang").is_none());
+ }
+
+ /// qpdf's `xref-compressed-in-compressed.pdf` in miniature: object 3 is an object stream
+ /// holding object 4, and the cross-reference stream says object 3 itself lives in stream 99.
+ fn compressed_in_compressed_fixture() -> Document {
+ let mut out = b"%PDF-1.5\n".to_vec();
+ let at = |out: &mut Vec<u8>, num: u32, body: &str| {
+ let off = out.len();
+ out.extend_from_slice(format!("{num} 0 obj\n{body}\nendobj\n").as_bytes());
+ off as u16
+ };
+ let o1 = at(&mut out, 1, "<< /Type /Catalog /Pages 2 0 R /Outlines 4 0 R >>");
+ let o2 = at(&mut out, 2, "<< /Type /Pages /Kids [] /Count 0 >>");
+ let inner = "<< /Type /Outlines /Count 0 >>";
+ at(&mut out, 3, &format!("<< /Type /ObjStm /N 1 /First 4 /Length {} >>\nstream\n4 0 {inner}\nendstream", 4 + inner.len()));
+ // /W [1 2 1]: object 3 is said to live in stream 99 (which does not exist), 4 in stream 3.
+ let rows: Vec<[u8; 4]> =
+ vec![[0, 0, 0, 255], [1, 0, o1 as u8, 0], [1, 0, o2 as u8, 0], [2, 0, 99, 0], [2, 0, 3, 0], [1, (out.len() >> 8) as u8, out.len() as u8, 0]];
+ let rows: Vec<u8> = rows.concat();
+ let xref = out.len();
+ out.extend_from_slice(
+ format!("5 0 obj\n<< /Type /XRef /W [1 2 1] /Index [0 6] /Size 6 /Root 1 0 R /Length {} >>\nstream\n", rows.len()).as_bytes(),
+ );
+ out.extend_from_slice(&rows);
+ out.extend_from_slice(format!("\nendstream\nendobj\nstartxref\n{xref}\n%%EOF\n").as_bytes());
+ Document::open(Arc::new(out)).unwrap()
+ }
+
+ /// An object stream whose own cross-reference entry says it is compressed inside another
+ /// stream (which the format forbids, and which does not exist here: qpdf's
+ /// `xref-compressed-in-compressed.pdf`, #2367) could not be loaded, so every object in it
+ /// read as null and a full save wrote `/Outlines null`. The stream is now found by its own
+ /// header, as a plain object with a wrong offset is.
+ #[test]
+ fn an_object_stream_marked_as_compressed_is_found_by_its_header() {
+ let doc = compressed_in_compressed_fixture();
+ assert_eq!(doc.revisions().len(), 1, "the chain itself reads: {:?}", doc.repair_log());
+ let outlines = doc.get(ObjRef::new(4, 0));
+ assert_eq!(outlines.as_dict().and_then(|d| d.name(b"Type")), Some(&b"Outlines"[..]), "{outlines:?}");
+ let saved = crate::write_full(&doc, &crate::SaveOptions::default()).unwrap();
+ let back = Document::open(Arc::new(saved)).unwrap();
+ let catalog = back.get(back.root().unwrap());
+ let outlines = back.resolve(catalog.as_dict().unwrap().get(b"Outlines").unwrap());
+ assert_eq!(outlines.as_dict().and_then(|d| d.name(b"Type")), Some(&b"Outlines"[..]), "the full save keeps it: {outlines:?}");
+ }
+
+ /// The header fallback is for file entries only: once the stream is freed (or replaced) in
+ /// the overlay, its objects must not come back from the file's copy of it.
+ #[test]
+ fn a_freed_object_stream_is_not_found_by_its_header() {
+ let doc = compressed_in_compressed_fixture();
+ assert!(doc.get(ObjRef::new(4, 0)).as_dict().is_some());
+ let mut edited = doc.clone();
+ edited.free(ObjRef::new(3, 0));
+ let fresh = edited.object_reader().document;
+ assert!(matches!(*fresh.get(ObjRef::new(4, 0)), Object::Null), "{:?}", fresh.get(ObjRef::new(4, 0)));
+ let mut replaced = doc.clone();
+ replaced.set(ObjRef::new(3, 0), Object::Int(0));
+ let fresh = replaced.object_reader().document;
+ assert!(matches!(*fresh.get(ObjRef::new(4, 0)), Object::Null), "{:?}", fresh.get(ObjRef::new(4, 0)));
+ }
+
+ /// The same once the free is saved: the revision's table says the stream is free, and
+ /// neither reading the file back nor rewriting it in full brings its objects back.
+ #[test]
+ fn a_container_freed_in_a_saved_revision_is_not_found_by_its_header() {
+ let mut edited = compressed_in_compressed_fixture();
+ edited.free(ObjRef::new(3, 0));
+ let incremental = crate::write_incremental(&edited, &crate::SaveOptions::default()).unwrap();
+ let reopened = Document::open(Arc::new(incremental)).unwrap();
+ assert_eq!(reopened.revisions().len(), 2);
+ assert!(matches!(reopened.xref_entry(3), Some(XrefEntry::Free { .. })));
+ assert!(matches!(*reopened.get(ObjRef::new(4, 0)), Object::Null), "{:?}", reopened.get(ObjRef::new(4, 0)));
+ for object_streams in [true, false] {
+ let opts = crate::SaveOptions { object_streams, ..Default::default() };
+ let full = crate::write_full(&reopened, &opts).unwrap();
+ let back = Document::open(Arc::new(full)).unwrap();
+ let catalog = back.get(back.root().unwrap());
+ let outlines = catalog.as_dict().unwrap().get(b"Outlines").map(|o| back.resolve(o));
+ assert!(outlines.as_ref().is_none_or(|o| matches!(**o, Object::Null)), "the full save keeps it out: {outlines:?}");
+ }
+ }
+
+ /// A trailer whose /Prev is written as the reference `N 0 R` (tiff2pdf did this; the files
+ /// are JHOVE's PDF-HUL-157 cases, #2366) used to end the cross-reference chain silently, so
+ /// every object of the earlier sections read as null: a four-page file showed one page, and
+ /// a full save wrote the three missing kids as null. The offset is read from the reference.
+ #[test]
+ fn a_prev_written_as_a_reference_still_reaches_the_earlier_sections() {
+ let base =
+ build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", "<< /Type /Page /Parent 2 0 R >>"], "/Root 1 0 R");
+ let prev = String::from_utf8_lossy(&base).rfind("xref\n0 4").unwrap();
+ let update = |prev_entry: &str| {
+ let mut bytes = base.clone();
+ let at = bytes.len();
+ bytes.extend_from_slice(b"4 0 obj\n<< /Title (Update) >>\nendobj\n");
+ let xref = bytes.len();
+ bytes.extend_from_slice(
+ format!("xref\n4 1\n{at:010} 00000 n \ntrailer\n<< /Size 5 /Root 1 0 R /Info 4 0 R /Prev {prev_entry} >>\nstartxref\n{xref}\n%%EOF\n")
+ .as_bytes(),
+ );
+ Document::open(Arc::new(bytes)).unwrap()
+ };
+ let good = update(&prev.to_string());
+ let bad = update(&format!("{prev} 0 R"));
+ assert!(good.repair_log().is_empty(), "{:?}", good.repair_log());
+ assert_eq!(bad.revisions().len(), 2, "{:?}", bad.repair_log());
+ assert_eq!(bad.repair_log().len(), 1, "a note about the /Prev: {:?}", bad.repair_log());
+ assert_eq!(bad.xref_entry(3), good.xref_entry(3), "the page of the first section is reached");
+ assert_eq!(bad.get(ObjRef::new(3, 0)).as_dict().and_then(|d| d.name(b"Type")), Some(&b"Page"[..]));
+ assert_eq!(
+ bad.get(ObjRef::new(4, 0)).as_dict().and_then(|d| d.get(b"Title").and_then(|t| t.as_string().map(|s| s.to_text()))),
+ Some("Update".into())
+ );
+ }
+
+ /// iText's `indirectPrev.pdf` in miniature: the update's trailer gives `/Prev 4 0 R`, and
+ /// object 4, listed in that update, holds the offset of the first section. Read as offset 4
+ /// (inside the header), the chain broke and the file was rebuilt without its revisions.
+ #[test]
+ fn a_prev_reference_to_an_object_holding_the_offset_reads_that_offset() {
+ let mut bytes =
+ build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", "<< /Type /Page /Parent 2 0 R >>"], "/Root 1 0 R");
+ let prev = String::from_utf8_lossy(&bytes).rfind("xref\n0 4").unwrap();
+ let mut at = Vec::new();
+ for (num, body) in
+ [(2, "<< /Type /Pages /Kids [3 0 R 5 0 R] /Count 2 >>".to_string()), (4, prev.to_string()), (5, "<< /Type /Page /Parent 2 0 R >>".into())]
+ {
+ at.push(bytes.len());
+ bytes.extend_from_slice(format!("{num} 0 obj\n{body}\nendobj\n").as_bytes());
+ }
+ let xref = bytes.len();
+ bytes.extend_from_slice(
+ format!(
+ "xref\n2 1\n{:010} 00000 n \n4 2\n{:010} 00000 n \n{:010} 00000 n \ntrailer\n<< /Size 6 /Root 1 0 R /Prev 4 0 R >>\nstartxref\n{xref}\n%%EOF\n",
+ at[0], at[1], at[2]
+ )
+ .as_bytes(),
+ );
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.revisions().len(), 2, "{:?}", doc.repair_log());
+ assert_eq!(doc.repair_log().len(), 1, "a note about the /Prev, and no rebuild: {:?}", doc.repair_log());
+ assert!(doc.repair_log()[0].contains(&format!("the offset {prev}")), "{:?}", doc.repair_log());
+ assert_eq!(doc.get(ObjRef::new(3, 0)).as_dict().and_then(|d| d.name(b"Type")), Some(&b"Page"[..]));
+ assert_compact_matches_materialized(&doc);
+ }
+
+ /// A file whose only trailers are cross-reference stream dictionaries, with its `startxref`
+ /// out of reach (here a second file appended after it, cut off before its own
+ /// cross-reference data, as in #2365), is rebuilt from its object headers. The rebuild used
+ /// to look only for the `trailer` keyword, so /Info and /ID were lost, and a full save after
+ /// editing one Info field dropped every other.
+ #[test]
+ fn reconstruction_takes_the_trailer_from_a_cross_reference_stream() {
+ let table = build(
+ &["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "<< /Title (Kept) /Creator (Me) >>"],
+ "/Root 1 0 R /Info 3 0 R",
+ );
+ let packed = crate::write_full(&Document::open(Arc::new(table)).unwrap(), &crate::SaveOptions::default()).unwrap();
+ let text = String::from_utf8_lossy(&packed);
+ assert!(text.contains("/XRef") && !text.contains("trailer"), "a cross-reference stream is the only trailer");
+ let mut bytes = packed.clone();
+ let appended = format!("%PDF-1.3\n90 0 obj\n<< /Length 5000 >>\nstream\n{}\nendstream\nendobj\n", "x".repeat(5000));
+ bytes.extend_from_slice(appended.as_bytes());
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert!(doc.revisions().is_empty(), "rebuilt: {:?}", doc.repair_log());
+ let info = doc.trailer().reference(b"Info").expect("the cross-reference stream's /Info");
+ let field = |d: &Document, key: &[u8]| d.get(info).as_dict().unwrap().get(key).unwrap().as_string().unwrap().bytes.clone();
+ assert_eq!(field(&doc, b"Title"), b"Kept");
+ assert!(doc.trailer().get(b"ID").is_some(), "and its /ID");
+ let mut edited = doc.clone();
+ edited.update_dict(info, |d| d.set(b"Subject".to_vec(), Object::String(crate::PdfString::literal("new")))).unwrap();
+ let saved = crate::write_full(&edited, &crate::SaveOptions::default()).unwrap();
+ let back = Document::open(Arc::new(saved)).unwrap();
+ assert!(back.repair_log().is_empty(), "{:?}", back.repair_log());
+ let info = back.trailer().reference(b"Info").expect("the full save keeps /Info");
+ let field = |key: &[u8]| back.get(info).as_dict().unwrap().get(key).unwrap().as_string().unwrap().bytes.clone();
+ assert_eq!((field(b"Title"), field(b"Creator"), field(b"Subject")), (b"Kept".to_vec(), b"Me".to_vec(), b"new".to_vec()));
+ }
+
+ /// A rebuild prefers a trailer whose /Root is the catalog (as `root_is_catalog` judges it): a
+ /// later cross-reference stream whose /Root points at the Info dictionary does not replace an
+ /// earlier good trailer, or a full save would write the Info dictionary as /Root.
+ #[test]
+ fn reconstruction_skips_a_trailer_whose_root_is_not_the_catalog() {
+ let mut bytes =
+ build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>", "<< /Title (Kept) >>"], "/Root 1 0 R /Info 3 0 R");
+ bytes.extend_from_slice(b"4 0 obj\n<< /Type /XRef /Size 5 /W [1 1 1] /Root 3 0 R /Length 3 >>\nstream\nabc\nendstream\nendobj\n");
+ let appended = format!("%PDF-1.3\n90 0 obj\n<< /Length 5000 >>\nstream\n{}\nendstream\nendobj\n", "x".repeat(5000));
+ bytes.extend_from_slice(appended.as_bytes());
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert!(doc.repair_log().iter().any(|l| l.contains("rebuilt")), "{:?}", doc.repair_log());
+ assert_eq!(doc.trailer().reference(b"Root").map(|r| r.num), Some(1), "the catalog, not the Info dictionary");
+ assert_eq!(doc.trailer().reference(b"Info").map(|r| r.num), Some(3));
+ }
+
+ #[test]
+ fn damaged_xref_is_reconstructed() {
+ let mut bytes = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 1 0 R");
+ let n = bytes.len();
+ bytes[n - 12..n - 6].copy_from_slice(b"999999"); // point startxref into nowhere
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert!(doc.revisions().is_empty());
+ assert!(!doc.repair_log().is_empty());
+ assert_eq!(doc.root(), Some(ObjRef::new(1, 0)));
+ }
+
+ #[test]
+ fn a_decompression_bomb_in_an_xref_stream_is_refused_and_the_file_reconstructed() {
+ // 32 MiB of rows behind two FlateDecode filters (a few hundred bytes in the file) for
+ // /W [1 1 1]: at 3 bytes a row that is more than 8,388,607 objects' worth, so it is not
+ // decoded (in full: the limit applies while inflating) and the objects are found by
+ // reconstruction instead.
+ let bomb = pdfcraft_filters::encode_flate(&pdfcraft_filters::encode_flate(&vec![0u8; 32 << 20]));
+ let mut bytes = b"%PDF-1.7\n".to_vec();
+ let o1 = bytes.len();
+ bytes.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
+ let o2 = bytes.len();
+ bytes.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n");
+ let x = bytes.len();
+ bytes.extend_from_slice(
+ format!("3 0 obj\n<< /Type /XRef /Size 4 /W [1 1 1] /Root 1 0 R /Filter [/FlateDecode /FlateDecode] /Length {} >>\nstream\n", bomb.len())
+ .as_bytes(),
+ );
+ bytes.extend_from_slice(&bomb);
+ bytes.extend_from_slice(format!("\nendstream\nendobj\nstartxref\n{x}\n%%EOF\n").as_bytes());
+ assert!(o1 < o2 && o2 < x);
+ let doc = Document::open(Arc::new(bytes)).unwrap();
+ assert_eq!(doc.root(), Some(ObjRef::new(1, 0)));
+ assert!(doc.repair_log().iter().any(|l| l.contains("limit")), "{:?}", doc.repair_log());
+ }
+
+ #[test]
+ fn a_stream_whose_length_is_itself_and_never_ends_loads_quickly() {
+ // From the nightly fuzz job: `6 0 obj << /Length 6 0 R >> stream` with no `endstream`
+ // after it. Every parse attempt failed, and each attempt resolved the length by loading
+ // object 6 again, retrying the same ways down to the nesting limit: about 2^32 parses.
+ // Objects being loaded are now a seen-set, lengths are memoised, and the second parse
+ // only runs when the header is shifted.
+ let mut bytes = b"%PDF-1.7\n1 0 obj\n<< /Type /Catalog >>\nendobj\n".to_vec();
+ bytes.extend_from_slice(b"6 0 obj\n<< /Length 6 0 R >>\nstream\nno end marker follows\n");
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ if let Ok(doc) = Document::open(Arc::new(bytes)) {
+ let _ = doc.try_get(6);
+ }
+ let _ = tx.send(());
+ });
+ rx.recv_timeout(std::time::Duration::from_secs(20)).expect("loading must not retry exponentially");
+ }
diff --cc crates/engine/src/edit/run.rs
index 000000000,7901a3b7d..2c64fbb75
mode 000000,100644..100644
--- a/crates/engine/src/edit/run.rs
+++ b/crates/engine/src/edit/run.rs
@@@ -1,0 -1,696 +1,698 @@@
+ use super::*;
+
+ /// Whether the opening password allows an edit (§7.6.4.2, Table 22).
+ pub(crate) fn check_permission(edit: &Edit, p: &pdfcraft_cos::Permissions) -> Result<(), EditError> {
+ match edit {
+ Edit::RotatePages { .. }
+ | Edit::DeletePages { .. }
+ | Edit::MovePages { .. }
+ | Edit::ReversePages { .. }
+ | Edit::InsertBlankPage { .. }
+ | Edit::DuplicatePages { .. }
+ | Edit::ReplacePages { .. }
+ | Edit::SetPageBox { .. }
+ | Edit::SetTransition { .. }
+ | Edit::InsertPagesFrom { .. }
+ // "Assemble the document: insert, rotate or delete pages and create bookmarks" (Table 22).
+ | Edit::AddBookmark { .. }
+ | Edit::RenameBookmark { .. }
+ | Edit::DeleteBookmark { .. }
+ | Edit::MoveBookmark { .. }
+ | Edit::SetBookmarkPage { .. }
+ | Edit::SetBookmarkColor { .. }
+ | Edit::SetBookmarkStyle { .. }
+ | Edit::BookmarksFromStructure
+ | Edit::NumberPages { .. } => {
+ if p.assemble() {
+ Ok(())
+ } else {
+ Err(EditError::NotPermitted("page changes"))
+ }
+ }
+ Edit::AddMeasurement(_)
+ | Edit::AddAnnotation(_)
+ | Edit::AddCustomStamp { .. }
+ | Edit::DeleteAnnotation { .. }
+ | Edit::SetAnnotationContents { .. }
+ | Edit::ReplyToAnnotation { .. }
+ | Edit::SetAnnotationStatus { .. }
+ | Edit::MarkAnnotation { .. }
+ | Edit::ReplaceText { .. }
+ | Edit::EraseInk { .. }
+ | Edit::LockAnnotation { .. }
+ | Edit::MoveAnnotation { .. }
+ | Edit::ReshapeAnnotation { .. }
+ | Edit::ResizeAnnotation { .. }
+ | Edit::StyleAnnotation { .. }
+ | Edit::FillAnnotation { .. }
+ | Edit::FontSizeAnnotation { .. }
+ | Edit::SetAnnotationInfo { .. }
+ | Edit::SetMeasurementScale { .. } => {
+ if p.annotate() {
+ Ok(())
+ } else {
+ Err(EditError::NotPermitted("comments"))
+ }
+ }
+ Edit::SetFieldValue { .. } | Edit::ResetForm { .. } | Edit::SetFieldImage { .. } | Edit::ApplyScriptChanges { .. } | Edit::XfaEvent { .. } => {
+ if p.fill_forms() {
+ Ok(())
+ } else {
+ Err(EditError::NotPermitted("filling in form fields"))
+ }
+ }
+ Edit::Protect(_) | Edit::RemoveProtection => {
+ if unrestricted(p) {
+ Ok(())
+ } else {
+ Err(EditError::NotPermitted("changing security"))
+ }
+ }
+ // Converting to PDF/A takes the encryption off (PDF/A forbids it), so on an encrypted
+ // document it is a change of security (#3921).
+ Edit::ConvertPdfA { .. } => {
+ if unrestricted(p) {
+ Ok(())
+ } else {
+ Err(EditError::NotPermitted("removing the encryption, which PDF/A forbids"))
+ }
+ }
+ Edit::SetInfo { .. }
+ | Edit::AddHeaderFooter { .. }
+ | Edit::AddWatermark { .. }
+ | Edit::AddBackground { .. }
+ | Edit::RemoveMarks { .. }
+ | Edit::AddField { .. }
+ | Edit::ApplyRedactions { .. }
+ | Edit::AddText { .. }
+ | Edit::AddImage { .. }
+ | Edit::UpdateContent { .. }
+ | Edit::MoveObjects { .. }
+ | Edit::DeleteContent { .. }
+ | Edit::ReplaceImage { .. }
+ | Edit::PasteContent { .. }
+ | Edit::ClearRedactions
+ | Edit::RemoveHidden { .. }
+ | Edit::ImportData { .. }
+ | Edit::AddLink { .. }
+ | Edit::SetLink { .. }
+ | Edit::DeleteLink { .. }
+ | Edit::RemoveLinks { .. }
+ | Edit::AddLinks { .. }
+ | Edit::Sanitize
+ | Edit::XfaToStatic
+ | Edit::SetFieldProps { .. }
+ | Edit::DeleteField { .. }
+ | Edit::DuplicateField { .. }
+ | Edit::SetTabOrder { .. }
+ | Edit::MoveInTabOrder { .. }
+ | Edit::SetCalculationOrder { .. }
+ | Edit::SetInitialView(_)
+ | Edit::AddAttachment { .. }
+ | Edit::DeleteAttachment { .. }
+ | Edit::AutoTagText
+ | Edit::SetAltText { .. }
+ | Edit::AutoTagFormFields
+ | Edit::SetTagProperty { .. }
+ | Edit::MoveTag { .. }
+ | Edit::MarkDecorative { .. }
+ | Edit::MarkTagArtifact { .. }
+ | Edit::AddOcrText { .. }
+ | Edit::SetDocumentScript { .. }
+ | Edit::SetFieldScript { .. }
+ | Edit::RemoveStandardId { .. }
+ | Edit::EditTextLine { .. }
+ | Edit::EditTextBlock { .. }
+ | Edit::RegisterTextFont { .. }
+ | Edit::EditPageImage { .. }
+ | Edit::Flatten { .. }
+ | Edit::FlattenFillSign => {
+ if p.modify() {
+ Ok(())
+ } else {
+ Err(EditError::NotPermitted("changes to the document"))
+ }
+ }
+ Edit::Batch { edits, .. } => edits.iter().try_for_each(|e| check_permission(e, p)),
+ }
+ }
+
+ /// Dates and unique ids stamped onto what an edit creates.
+ pub(crate) struct EditCtx {
+ /// Field scripts run (`None`: JavaScript is off) and what they produced.
+ pub(crate) js: Option<js::JsRunner>,
+ /// A laid-out XFA form's template (`None`: not an XFA form, or JavaScript is off) and what
+ /// its scripts produced.
+ pub(crate) xfa: Option<Arc<pdfcraft_xfa::model::Template>>,
+ pub(crate) xfa_out: js::JsOutput,
+ /// The datasets stream this edit wrote (later writes in the edit replace it in place).
+ pub(crate) xfa_datasets: Option<pdfcraft_cos::ObjRef>,
+ /// A form script ran too long and was abandoned: the document's scripts go off.
+ pub(crate) xfa_ran_away: bool,
+ /// What the XFA scripts' time limits are multiplied by ([`Session::with_xfa_time_scale`]).
+ pub(crate) xfa_time_scale: u32,
+ /// The edit took the XFA out of the form: what went with it, for the document's warnings.
+ pub(crate) xfa_removed: Vec<String>,
+ /// Pages in the document, for `xfa.layout.pageCount()`.
+ pub(crate) pages: usize,
+ pub(crate) date: Option<String>,
+ /// Today in local time, for date tokens.
+ pub(crate) today: (i64, u32, u32),
+ pub(crate) seed: u64,
+ pub(crate) count: u64,
++ /// Platform CSPRNG (`getrandom::fill`). Tests replace it so Protect can fail closed.
++ pub(crate) rng: fn(&mut [u8]) -> Result<(), getrandom::Error>,
+ /// Fields the edit renamed to keep imported names separate (old → new), for the repair
+ /// log and the undo label.
+ pub(crate) renamed_fields: Vec<(String, String)>,
+ /// The overlay streams the edit's applied redactions added, which alone the renderer's check
+ /// leaves out.
+ pub(crate) redaction_overlays: Vec<pdfcraft_cos::ObjRef>,
+ }
+
+ impl EditCtx {
+ pub(crate) fn new(now: Option<i64>, salt: u64) -> Self {
+ let mut seed = 0x9E37_79B9_7F4A_7C15u64 ^ salt;
+ if let Some(t) = now {
+ seed ^= (t as u64).rotate_left(17);
+ }
+ #[cfg(not(target_arch = "wasm32"))]
+ if now.is_some() {
+ // Real clock: mix in sub-second time so ids from two sessions don't collide.
+ seed ^= std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.subsec_nanos() as u64).unwrap_or(0) << 32;
+ }
+ Self {
+ js: None,
+ xfa: None,
+ xfa_out: Default::default(),
+ xfa_datasets: None,
+ xfa_ran_away: false,
+ xfa_time_scale: 1,
+ xfa_removed: Vec::new(),
+ pages: 0,
+ date: now.map(pdfcraft_cos::pdf_date),
+ today: (1970, 1, 1),
+ seed,
+ count: 0,
++ rng: getrandom::fill,
+ renamed_fields: Vec::new(),
+ redaction_overlays: Vec::new(),
+ }
+ }
+
- /// 32 bytes of entropy for new encryption keys and salts. `RandomState` is seeded by the
- /// operating system, so no extra dependency is needed.
- fn entropy(&mut self) -> [u8; 32] {
- use std::hash::{BuildHasher, Hasher};
++ /// 32 bytes of entropy for new encryption keys, salts and generated owner passwords from the
++ /// platform CSPRNG. Fails closed when secure randomness is unavailable.
++ fn entropy(&mut self) -> Result<[u8; 32], EditError> {
+ let mut out = [0u8; 32];
- for (i, chunk) in out.chunks_mut(8).enumerate() {
- let mut h = std::collections::hash_map::RandomState::new().build_hasher();
- h.write_u64(self.seed ^ i as u64);
- h.write_u64(self.count);
- self.count += 1;
- chunk.copy_from_slice(&h.finish().to_le_bytes());
++ (self.rng)(&mut out).map_err(|e| EditError::Protection(format!("no secure randomness available: {e}")))?;
++ if out == [0u8; 32] {
++ return Err(EditError::Protection("no secure randomness available".into()));
+ }
- out
++ Ok(out)
+ }
+
+ /// A fresh `/NM`: a random-looking UUID (version 4 layout) from a splitmix64 stream.
+ fn meta(&mut self) -> pdfcraft_annot::Meta {
+ let mut next = || {
+ self.count += 1;
+ let mut z = self.seed.wrapping_add(self.count.wrapping_mul(0x9E37_79B9_7F4A_7C15));
+ z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
+ z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
+ z ^ (z >> 31)
+ };
+ let (a, b) = (next(), next());
+ let id = format!(
+ "{:08x}-{:04x}-4{:03x}-{:04x}-{:012x}",
+ a >> 32,
+ (a >> 16) & 0xffff,
+ a & 0xfff,
+ 0x8000 | (b >> 48) & 0x3fff,
+ b & 0xffff_ffff_ffff
+ );
+ pdfcraft_annot::Meta { date: self.date.clone(), id }
+ }
+ }
+
+ /// Perform an edit on a working copy (the caller discards it on error).
+ pub(crate) fn run_edit(doc: &mut pdfcraft_cos::Document, edit: &Edit, cx: &mut EditCtx) -> Result<(), EditError> {
+ match edit {
+ Edit::RotatePages { pages, degrees } => pdfcraft_organize::rotate_pages(doc, pages, *degrees)?,
+ Edit::DeletePages { pages } => pdfcraft_organize::delete_pages(doc, pages)?,
+ Edit::MovePages { pages, to } => pdfcraft_organize::move_pages(doc, pages, *to)?,
+ Edit::ReversePages { pages } => pdfcraft_organize::reverse_pages(doc, pages)?,
+ Edit::InsertBlankPage { at, width, height } => {
+ pdfcraft_organize::insert_blank_page(doc, *at, *width, *height)?;
+ }
+ Edit::SetInfo { key, value } => pdfcraft_organize::set_info(doc, key, value)?,
+ Edit::DuplicatePages { pages, at } => pdfcraft_organize::duplicate_pages(doc, pages, *at)?,
+ Edit::ReplacePages { pages, name, bytes, src_pages } => {
+ let src = open_source(name, bytes)?;
+ pdfcraft_organize::replace_pages(doc, pages, &src, src_pages)?;
+ }
+ Edit::SetPageBox { pages, which, spec } => pdfcraft_organize::set_page_box(doc, pages, *which, *spec)?,
+ Edit::SetTransition { pages, spec } => pdfcraft_organize::set_transition(doc, pages, spec)?,
+ Edit::InsertPagesFrom { name, bytes, pages, at, field_conflicts } => {
+ let src = open_source(name, bytes)?;
+ let pages = match pages {
+ Some(p) => p.clone(),
+ None => (0..pdfcraft_organize::page_count(&src)?).collect(),
+ };
+ // The file's bookmarks come along under one named after it, as in Combine Files.
+ let title = std::path::Path::new(name).file_stem().map_or_else(|| name.clone(), |s| s.to_string_lossy().into_owned());
+ let outcome = pdfcraft_organize::insert_file_pages(doc, &src, &title, &pages, *at, *field_conflicts)?;
+ cx.renamed_fields = outcome.renamed_fields;
+ }
+ Edit::AddBookmark { parent, index, title, page, left, top } => {
+ pdfcraft_organize::add_bookmark_at(doc, parent, *index, title, *page, *left, *top)?;
+ }
+ Edit::RenameBookmark { path, title } => pdfcraft_organize::rename_bookmark(doc, path, title)?,
+ Edit::DeleteBookmark { path } => pdfcraft_organize::delete_bookmark(doc, path)?,
+ Edit::MoveBookmark { from, to_parent, index } => {
+ pdfcraft_organize::move_bookmark(doc, from, to_parent, *index)?;
+ }
+ Edit::SetBookmarkPage { path, page } => pdfcraft_organize::set_bookmark_page(doc, path, *page)?,
+ Edit::SetBookmarkColor { path, color } => pdfcraft_organize::set_bookmark_color(doc, path, *color)?,
+ Edit::SetBookmarkStyle { path, bold, italic } => pdfcraft_organize::set_bookmark_style(doc, path, *bold, *italic)?,
+ Edit::BookmarksFromStructure => {
+ let entries: Vec<_> = pdfcraft_a11y::headings(doc)
+ .into_iter()
+ .map(|h| pdfcraft_organize::OutlineEntry { level: h.level, title: h.title, page: h.page, element: h.obj })
+ .collect();
+ pdfcraft_organize::add_bookmark_tree(doc, "Untitled", &entries)?;
+ }
+ Edit::NumberPages { from, to, style, prefix, first } => pdfcraft_organize::number_pages(doc, *from, *to, *style, prefix, *first)?,
+ Edit::AddMeasurement(m) => {
+ measure::add(doc, m, &cx.meta())?;
+ }
+ Edit::SetMeasurementScale { page, bbox, name, scale } => measure::set_scale(doc, *page, *bbox, name, scale)?,
+ Edit::AddAnnotation(a) => {
+ pdfcraft_annot::add_annotation(doc, a, &cx.meta())?;
+ }
+ Edit::AddCustomStamp { page, rect, name, file, author } => {
+ let src = mark_source(doc, file)?;
+ let (sw, sh) = (src.size.0.max(1.0), src.size.1.max(1.0));
+ let natural = rect[2] == rect[0] && rect[3] == rect[1];
+ let rotation = pdfcraft_organize::page_rotation(doc, *page)?;
+ // The picture as displayed: a quarter turn swaps its sides in user space.
+ let (dw, dh) = if matches!(rotation, 90 | 270) { (sh, sw) } else { (sw, sh) };
+ let rect = if natural {
+ let k = (200.0 / sw.max(sh)).min(1.0);
+ let (w, h) = (dw * k, dh * k);
+ [rect[0] - w / 2.0, rect[1] - h / 2.0, rect[0] + w / 2.0, rect[1] + h / 2.0]
+ } else {
+ fit_picture(*rect, dw, dh)
+ };
+ let shape = Shape::CustomStamp { rect, name: name.clone(), picture: src.xobject, image: src.image, size: (sw, sh) };
+ let style = pdfcraft_annot::Style::default_for(&shape);
+ let new = NewAnnotation { page: *page, shape, style, contents: name.clone(), author: author.clone() };
+ let index = pdfcraft_annot::add_annotation(doc, &new, &cx.meta())?;
+ pdfcraft_annot::orient_image_stamp(doc, *page, index, rotation)?;
+ }
+ Edit::DeleteAnnotation { page, index } => pdfcraft_annot::delete_annotation(doc, *page, *index)?,
+ Edit::SetAnnotationContents { page, index, text } => pdfcraft_annot::set_contents(doc, *page, *index, text, &cx.meta())?,
+ Edit::ReplyToAnnotation { page, index, text, author } => {
+ pdfcraft_annot::add_reply(doc, *page, *index, text, author, &cx.meta())?;
+ }
+ Edit::SetAnnotationStatus { page, index, state, author } => {
+ pdfcraft_annot::set_review_state(doc, *page, *index, *state, author, &cx.meta())?;
+ }
+ Edit::EraseInk { page, index, path, radius } => {
+ pdfcraft_annot::erase_ink(doc, *page, *index, path, *radius, &cx.meta())?;
+ }
+ Edit::AddAttachment { name, bytes, description, mime } => {
+ pdfcraft_organize::add_attachment(doc, name, bytes, description.as_deref(), mime.as_deref(), cx.date.as_deref())?;
+ }
+ Edit::DeleteAttachment { key } => pdfcraft_organize::delete_attachment(doc, key)?,
+ Edit::ReplaceText { page, quads, text, author, strike, caret } => {
+ // One `/NM` each: the caret replies to the strikeout, and XFDF names that reply by
+ // the strikeout's `/NM` (#2666).
+ let (strike_meta, caret_meta) = (cx.meta(), cx.meta());
+ pdfcraft_annot::add_text_replacement(doc, *page, quads, text, author, strike, caret, &strike_meta, &caret_meta)?;
+ }
+ Edit::MarkAnnotation { page, index, marked, author } => {
+ pdfcraft_annot::set_marked(doc, *page, *index, *marked, author, &cx.meta())?;
+ }
+ Edit::LockAnnotation { page, index, locked } => pdfcraft_annot::set_locked(doc, *page, *index, *locked)?,
+ Edit::MoveAnnotation { page, index, dx, dy } => pdfcraft_annot::move_annotation(doc, *page, *index, *dx, *dy, &cx.meta())?,
+ Edit::ReshapeAnnotation { page, index, points } => pdfcraft_annot::set_points(doc, *page, *index, points, &cx.meta())?,
+ Edit::ResizeAnnotation { page, index, rect } => pdfcraft_annot::set_rect(doc, *page, *index, *rect, &cx.meta())?,
+ Edit::StyleAnnotation { page, index, color, opacity, width, endings } => {
+ pdfcraft_annot::set_style(doc, *page, *index, *color, *opacity, *width, endings.as_deref(), &cx.meta())?;
+ }
+ Edit::FillAnnotation { page, index, fill } => pdfcraft_annot::set_fill(doc, *page, *index, *fill, &cx.meta())?,
+ Edit::FontSizeAnnotation { page, index, size } => pdfcraft_annot::set_font_size(doc, *page, *index, *size, &cx.meta())?,
+ Edit::SetAnnotationInfo { page, index, author, subject, icon } => {
+ pdfcraft_annot::set_info(doc, *page, *index, author.as_deref(), subject.as_deref(), *icon, &cx.meta())?;
+ }
+ Edit::SetFieldValue { name, value } => {
+ let mut set = |doc: &mut pdfcraft_cos::Document, value: &FieldValue| match cx.js.as_mut() {
+ Some(js) => pdfcraft_forms::set_value_with(doc, name, value, js),
+ None => pdfcraft_forms::set_value(doc, name, value),
+ };
+ match set(doc, value) {
+ Ok(()) => {}
+ // A date the field's format doesn't read may be typed by another of an XFA date
+ // field's patterns: it is kept in the first. Only a format refusal is tried
+ // again: it comes before any of the field's scripts run, so they run once.
+ Err(refused @ pdfcraft_forms::FormError::Format(_)) => match xfa::typed_date(doc, cx.xfa.as_deref(), name, value) {
+ Some(typed) => set(doc, &typed)?,
+ None => return Err(refused.into()),
+ },
+ Err(refused) => return Err(refused.into()),
+ }
+ }
+ Edit::ApplyScriptChanges { changes } => match cx.js.as_mut() {
+ Some(js) => pdfcraft_forms::apply_script_changes(doc, changes, js)?,
+ None => pdfcraft_forms::apply_script_changes(doc, changes, &mut pdfcraft_forms::NoScripts)?,
+ },
+ Edit::XfaEvent { som, activity } => {
+ let tpl = cx.xfa.clone().ok_or_else(|| EditError::Invalid("this is not a laid-out XFA form, or JavaScript is off".into()))?;
+ let mut run =
+ xfa::XfaRun { page_count: cx.pages, out: &mut cx.xfa_out, datasets: cx.xfa_datasets, ran_away: false, time_scale: cx.xfa_time_scale };
+ let ran = xfa::on_event(doc, &tpl, som, activity, &mut run);
+ cx.xfa_datasets = run.datasets;
+ cx.xfa_ran_away |= run.ran_away;
+ ran.map_err(EditError::Invalid)?;
+ }
+ Edit::SetFieldImage { name, image } => {
+ let (img, _) = embed_image(doc, name, image)?;
+ let px = match &*doc.get(img) {
+ pdfcraft_cos::Object::Stream(s) => (s.dict.int(b"Width").unwrap_or(1) as u32, s.dict.int(b"Height").unwrap_or(1) as u32),
+ _ => (1, 1),
+ };
+ // An XFA image field keeps the picture in the data too (as Designer saves it), and a
+ // laid-out form shows it from there: so only pictures the layout can show, and only
+ // for image fields (a laid-out one carries its SOM path; a static form's field name
+ // is its path).
+ let is_xfa = pdfcraft_xfa::read_packets(doc).ok().flatten().is_some();
+ let image_field = is_xfa
+ && pdfcraft_forms::fields(doc).iter().any(|f| f.name == *name && f.button == Some(pdfcraft_forms::af::ButtonAction::ImportIcon));
+ if image_field {
+ let Some(content_type) = image_content_type(image) else {
+ return Err(EditError::Invalid(format!("{name}: an XFA form keeps a PNG, JPEG, GIF, BMP or TIFF picture; choose one of those")));
+ };
+ pdfcraft_forms::set_button_icon(doc, name, img, px)?;
+ let som = pdfcraft_xfa::fields_by_som(doc).into_iter().find(|(_, n)| n == name).map(|(s, _)| s).unwrap_or_else(|| name.clone());
+ let path = match cx.xfa.as_deref() {
+ Some(tpl) => pdfcraft_xfa::data_path(tpl, &som),
+ None => pdfcraft_xfa::som_to_path(&som),
+ };
+ let r =
+ pdfcraft_xfa::write_data_image(doc, &path, content_type, image, cx.xfa_datasets).map_err(|e| EditError::Write(e.to_string()))?;
+ if !r.written && r.warnings.is_empty() {
+ return Err(EditError::Write(format!("{name}: the picture could not be written into the form's data")));
+ }
+ cx.xfa_datasets = r.stream.or(cx.xfa_datasets);
+ cx.xfa_out.errors.extend(r.warnings);
+ xfa_after_data_change(doc, cx, false)?;
+ } else {
+ pdfcraft_forms::set_button_icon(doc, name, img, px)?;
+ }
+ }
+ Edit::ResetForm { names } => {
+ pdfcraft_forms::reset(doc, names.as_deref())?;
+ }
+ Edit::AddField { page, rect, kind, name } => {
+ pdfcraft_forms::add_field(doc, *page, *rect, kind, name.as_deref())?;
+ }
+ Edit::SetFieldProps { name, props } => {
+ pdfcraft_forms::set_props(doc, name, props)?;
+ }
+ Edit::DuplicateField { name, pages } => {
+ if pdfcraft_forms::duplicate_field(doc, name, pages)? == 0 {
+ return Err(EditError::Form(pdfcraft_forms::FormError::Invalid(format!("{name} is already on those pages"))));
+ }
+ }
+ Edit::DeleteField { name } => pdfcraft_forms::delete_field(doc, name)?,
+ Edit::SetTabOrder { pages, order } => pdfcraft_forms::set_tab_order(doc, pages, *order)?,
+ Edit::MoveInTabOrder { name, earlier } => pdfcraft_forms::move_in_tab_order(doc, name, *earlier)?,
+ // As after a script edit: the values the new order gives show at once.
+ Edit::SetCalculationOrder { names } => {
+ pdfcraft_forms::set_calculation_order(doc, names)?;
+ match cx.js.as_mut() {
+ Some(js) => pdfcraft_forms::recalculate_with(doc, js)?,
+ None => pdfcraft_forms::recalculate(doc)?,
+ };
+ }
+ Edit::SetInitialView(v) => pdfcraft_organize::set_initial_view(doc, v)?,
+ Edit::AutoTagText => {
+ a11y::autotag_text(doc).map_err(|e| EditError::Accessibility(e.to_string()))?;
+ }
+ Edit::SetAltText { figure, alt } => {
+ let r = pdfcraft_cos::ObjRef::new(*figure, doc.generation(*figure));
+ a11y::set_alt(doc, r, alt.as_deref()).map_err(|e| EditError::Accessibility(e.to_string()))?;
+ }
+ Edit::AutoTagFormFields => {
+ a11y::tag_form_fields(doc).map_err(EditError::Accessibility)?;
+ }
+ Edit::SetTagProperty { object, property, value } => {
+ a11y::set_tag_property(doc, *object, *property, value.as_deref()).map_err(|e| EditError::Accessibility(e.to_string()))?;
+ }
+ Edit::MoveTag { object, anchor, placement } => {
+ a11y::move_tag(doc, *object, *anchor, *placement).map_err(|e| EditError::Accessibility(e.to_string()))?;
+ }
+ Edit::EditTextLine { page, line, text } => {
+ pdfcraft_edit::replace_line(doc, *page, *line, text)?;
+ }
+ Edit::SetFieldScript { name, event, script } => {
+ pdfcraft_forms::set_field_script(doc, name, event, script.as_deref())?;
+ match cx.js.as_mut() {
+ Some(js) => pdfcraft_forms::recalculate_with(doc, js)?,
+ None => pdfcraft_forms::recalculate(doc)?,
+ };
+ }
+ Edit::ConvertPdfA { level } => {
+ pdfcraft_preflight::convert(doc, *level).map_err(|e| EditError::Invalid(e.to_string()))?;
+ }
+ Edit::RemoveStandardId { standard } => pdfcraft_organize::remove_standard_id(doc, *standard)?,
+ Edit::SetDocumentScript { name, script } => pdfcraft_forms::set_document_script(doc, name, script.as_deref())?,
+ Edit::AddOcrText { page, words } => {
+ pdfcraft_edit::stamp(doc, *page, "OCR", pdfcraft_ocr::text_layer(words))?;
+ }
+ Edit::EditPageImage { page, index, change } => {
+ let img = pdfcraft_edit::page_images(doc, *page)?
+ .into_iter()
+ .nth(*index)
+ .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid(format!("page {} has no image {}", page + 1, index + 1))))?;
+ let c = match change {
+ ImageEdit::Move(to) => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::rect_to_rect(img.rect, *to)),
+ ImageEdit::Rotate(q) => {
+ pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(img.visible_rect.unwrap_or(img.rect), *q, false, false))
+ }
+ ImageEdit::RotateBy(degrees) => {
+ let t = pdfcraft_edit::turn_by_degrees(img.visible_rect.unwrap_or(img.rect), *degrees)
+ .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid("the rotation angle must be a finite number".into())))?;
+ pdfcraft_edit::ImageChange::Transform(t)
+ }
+ ImageEdit::Flip { horizontal } => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(
+ img.visible_rect.unwrap_or(img.rect),
+ 0,
+ *horizontal,
+ !*horizontal,
+ )),
+ ImageEdit::Replace { name, bytes } => {
+ if img.is_form {
+ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid(
+ "grouped Form artwork cannot be replaced with a bitmap".into(),
+ )));
+ }
+ pdfcraft_edit::ImageChange::Replace(embed_image(doc, name, bytes)?.0)
+ }
+ ImageEdit::Crop(keep) => pdfcraft_edit::ImageChange::Crop(*keep),
+ ImageEdit::Delete => pdfcraft_edit::ImageChange::Delete,
+ };
+ pdfcraft_edit::change_image(doc, *page, *index, &c)?;
+ }
+ Edit::MoveObjects { page, objects, offset } => pdfcraft_edit::move_objects(doc, *page, objects, *offset)?,
+ Edit::EditTextBlock { page, block, text, style } => {
+ pdfcraft_edit::rewrite_block(doc, *page, *block, Some(text), style)?;
+ }
+ Edit::RegisterTextFont { page, bytes } => {
+ pdfcraft_edit::text::register_text_font(doc, *page, bytes)?;
+ }
+ Edit::MarkDecorative { figure } => {
+ let r = pdfcraft_cos::ObjRef::new(*figure, doc.generation(*figure));
+ a11y::mark_decorative(doc, r).map_err(|e| EditError::Accessibility(e.to_string()))?;
+ }
+ Edit::MarkTagArtifact { object } => {
+ a11y::mark_tag_artifact(doc, *object).map_err(|e| EditError::Accessibility(e.to_string()))?;
+ }
+ Edit::AddHeaderFooter { pages, settings, replace } => {
+ let date = cx.today;
+ pdfcraft_edit::add_header_footer(doc, pages, settings, *replace, &pdfcraft_edit::Context { date })?;
+ }
+ Edit::AddWatermark { pages, settings, replace, file } => {
+ let mut s = settings.clone();
+ if let Some(f) = file {
+ s.source = Some(mark_source(doc, f)?);
+ }
+ pdfcraft_edit::add_watermark(doc, pages, &s, *replace)?
+ }
+ Edit::AddBackground { pages, settings, replace, file } => {
+ let mut s = settings.clone();
+ if let Some(f) = file {
+ s.source = Some(mark_source(doc, f)?);
+ }
+ pdfcraft_edit::add_background(doc, pages, &s, *replace)?
+ }
+ Edit::RemoveMarks { kind } => {
+ let n = pdfcraft_model::pages(doc).len();
+ if pdfcraft_edit::remove_marks(doc, &(0..n).collect::<Vec<_>>(), *kind)? == 0 {
+ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("there is nothing to remove".into())));
+ }
+ }
+ Edit::AddText { page, text } => {
+ pdfcraft_edit::add_content(doc, *page, &AddedContent::Text(text.clone()))?;
+ }
+ Edit::AddImage { page, rect, name, bytes } => {
+ let (image, natural) = embed_image(doc, name, bytes)?;
+ let rect = match rect {
+ Some(r) => *r,
+ None => {
+ let p = pdfcraft_model::pages(doc).swap_remove(*page);
+ let (pw, ph) = p.display_size(doc);
+ let k = ((pw * 0.8) / natural.0).min((ph * 0.8) / natural.1).min(1.0);
+ let (w, h) = (natural.0 * k, natural.1 * k);
+ [(pw - w) / 2.0, (ph - h) / 2.0, (pw + w) / 2.0, (ph + h) / 2.0]
+ }
+ };
+ pdfcraft_edit::add_content(doc, *page, &AddedContent::Image(pdfcraft_edit::AddedImage::new(rect, image)))?;
+ }
+ Edit::UpdateContent { page, index, content } => pdfcraft_edit::update_content(doc, *page, *index, content)?,
+ Edit::DeleteContent { page, index } => pdfcraft_edit::delete_content(doc, *page, *index)?,
+ Edit::ReplaceImage { page, index, name, bytes } => {
+ let item = pdfcraft_edit::list_added(doc).into_iter().filter(|a| a.page == *page).nth(*index);
+ let Some(AddedContent::Image(old)) = item.map(|a| a.content) else {
+ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("that item is not an image".into())));
+ };
+ let (image, _) = embed_image(doc, name, bytes)?;
+ pdfcraft_edit::update_content(doc, *page, *index, &AddedContent::Image(pdfcraft_edit::AddedImage { image, ..old }))?;
+ }
+ Edit::PasteContent { page, content, file } => {
+ let content = match content {
+ AddedContent::Text(_) => content.clone(),
+ AddedContent::Image(img) => {
+ let Some((name, bytes)) = file else {
+ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("a pasted image needs its picture".into())));
+ };
+ // The same picture in this document (copied here, pasted here before, or an
+ // identical file): drawn again from the one image object rather than stored twice.
+ let image = if copy_image::is_picture(doc, img.image, bytes) {
+ img.image
+ } else {
+ match copy_image::added_picture(doc, bytes) {
+ Some(image) => image,
+ None => embed_image(doc, name, bytes)?.0,
+ }
+ };
+ AddedContent::Image(pdfcraft_edit::AddedImage { image, ..img.clone() })
+ }
+ };
+ pdfcraft_edit::add_content(doc, *page, &content)?;
+ }
+ Edit::ApplyRedactions { pages } => {
+ cx.redaction_overlays = pdfcraft_redact::apply(doc, pages.as_deref())?.overlays;
+ }
+ Edit::ClearRedactions => {
+ pdfcraft_redact::clear_marks(doc, None)?;
+ }
+ Edit::AddLink { page, rect, action, style } => {
+ pdfcraft_annot::links::add(doc, *page, *rect, action, style)?;
+ }
+ Edit::SetLink { page, index, rect, action, style } => pdfcraft_annot::links::set(doc, *page, *index, *rect, action.as_ref(), style.as_ref())?,
+ Edit::DeleteLink { page, index } => pdfcraft_annot::links::delete(doc, *page, *index)?,
+ Edit::RemoveLinks { pages } => {
+ if pdfcraft_annot::links::remove_all(doc, pages.as_deref())? == 0 {
+ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("there are no links to remove".into())));
+ }
+ }
+ Edit::AddLinks { links, style } => {
+ if pdfcraft_annot::links::add_many(doc, links, style)? == 0 {
+ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("no web addresses were found".into())));
+ }
+ }
+ Edit::ImportData { bytes, .. } => {
+ // An XFA form takes XFA data files (XDP, datasets, or a data document) as its data;
+ // XFDF, FDF, Acrobat's XML and tables go the usual way.
+ let text = (pdfcraft_xfa::read_packets(doc).ok().flatten().is_some()).then(|| decode_data_file(bytes)).flatten();
+ match text {
+ Some(Ok(text)) if is_xfa_data_file(&text) => {
+ let expect_root = cx.xfa.as_ref().and_then(|t| t.root.common.name.clone());
+ let r = pdfcraft_xfa::import_data(doc, &text, expect_root.as_deref(), cx.xfa_datasets)
+ .map_err(|e| EditError::Write(e.to_string()))?;
+ cx.xfa_datasets = r.stream.or(cx.xfa_datasets);
+ cx.xfa_out.errors.extend(r.warnings);
+ cx.xfa_out.console.push(format!("Imported XFA data: {} node(s)", r.nodes));
+ xfa_after_data_change(doc, cx, true)?;
+ }
+ Some(Err(why)) => return Err(EditError::Invalid(why)),
+ _ => {
+ pdfcraft_xfdf::import(doc, bytes)?;
+ }
+ }
+ }
+ Edit::RemoveHidden { which } => {
+ pdfcraft_redact::sanitize::remove_hidden(doc, which)?;
+ }
+ Edit::Sanitize => {
+ pdfcraft_redact::sanitize::sanitize(doc)?;
+ }
+ Edit::Flatten { comments, fields } => {
+ // An XFA form's fields baked into the pages: the XFA goes too, or Adobe's viewers
+ // would lay the form out again from its template over the baked pages. A dynamic
+ // form that was never laid out keeps its XFA: without it only placeholders remain.
+ let shape = (*fields).then(|| pdfcraft_xfa::xfa_shape(doc)).flatten();
+ let n = pdfcraft_model::pages(doc).len();
+ pdfcraft_edit::flatten(doc, &(0..n).collect::<Vec<_>>(), *comments, *fields)?;
+ match shape {
+ Some(shape) if shape.strippable() => strip_xfa(doc, cx)?,
+ Some(_) => cx.xfa_removed.push("The XFA was kept: this dynamic form was not laid out, so its fields were not baked in".into()),
+ None => {}
+ }
+ }
+ Edit::XfaToStatic => {
+ let Some(shape) = pdfcraft_xfa::xfa_shape(doc) else {
+ return Err(EditError::Invalid("this document is not an XFA form".into()));
+ };
+ if !shape.strippable() {
+ return Err(EditError::Invalid("this dynamic XFA form was not laid out, so there is no static form to keep".into()));
+ }
+ strip_xfa(doc, cx)?;
+ }
+ Edit::FlattenFillSign => {
+ let n = pdfcraft_model::pages(doc).len();
+ pdfcraft_edit::flatten_fill_sign(doc, &(0..n).collect::<Vec<_>>())?;
+ }
+ Edit::Protect(p) => {
+ p.validate()?;
- let seed = cx.entropy();
++ let seed = cx.entropy()?;
+ // Without a permissions password nothing is restricted, so the owner password is a
+ // random one nobody needs.
- let random_owner: String = cx.entropy().iter().map(|b| format!("{b:02x}")).collect();
++ let random_owner = match p.permissions_password {
++ Some(_) => String::new(),
++ None => cx.entropy()?.iter().map(|b| format!("{b:02x}")).collect(),
++ };
+ let params = pdfcraft_cos::NewEncryption {
+ algorithm: p.algorithm,
+ user_password: p.open_password.as_deref().unwrap_or(""),
+ owner_password: p.permissions_password.as_deref().unwrap_or(&random_owner),
+ permissions: p.permission_bits(),
+ encrypt_metadata: p.encrypt_metadata,
+ seed,
+ };
+ doc.set_encryption(&params).map_err(|e| EditError::Protection(e.to_string()))?;
+ }
+ Edit::RemoveProtection => {
+ if doc.output_handler().is_none() {
+ return Err(EditError::Protection("the document isn't protected".into()));
+ }
+ doc.remove_encryption();
+ }
+ Edit::Batch { edits, .. } => {
+ for e in edits {
+ run_edit(doc, e, cx)?;
+ }
+ }
+ }
+ Ok(())
+ }