use std::collections::BTreeSet;
use std::num::NonZeroUsize;
#[cfg(test)]
use std::cell::Cell;
use crate::ObjectHandle;
use crate::ObjectRef;
#[cfg(test)]
thread_local! {
static COMPRESSIBLE_PLAN_CALLS: Cell<usize> = const { Cell::new(0) };
}
#[cfg(test)]
pub(crate) fn reset_compressible_plan_call_count() {
COMPRESSIBLE_PLAN_CALLS.with(|calls| calls.set(0));
}
#[cfg(test)]
pub(crate) fn compressible_plan_call_count() -> usize {
COMPRESSIBLE_PLAN_CALLS.with(Cell::get)
}
pub(crate) struct EligibilityContext {
pub encryption_ref: Option<ObjectRef>,
}
pub(crate) fn is_eligible_for_objstm_handle(
object_ref: ObjectRef,
object: &ObjectHandle,
ctx: &EligibilityContext,
) -> crate::Result<bool> {
if object_ref.generation != 0 {
return Ok(false);
}
object.try_dereference()?;
if object.as_stream_dict().is_some() {
return Ok(false);
}
if object.try_is_dictionary_of_type(b"ObjStm", b"")?
|| object.try_is_dictionary_of_type(b"XRef", b"")?
{
return Ok(false);
}
if is_qpdf_signature_dict(object)? {
return Ok(false);
}
if Some(object_ref) == ctx.encryption_ref {
return Ok(false);
}
Ok(true)
}
pub(crate) fn eligibility_context<R: std::io::Read + std::io::Seek>(
pdf: &mut crate::Pdf<R>,
) -> crate::Result<EligibilityContext> {
Ok(EligibilityContext {
encryption_ref: pdf.encryption_ref(),
})
}
pub(crate) fn get_compressible_objgens<R: std::io::Read + std::io::Seek>(
pdf: &mut crate::Pdf<R>,
) -> crate::Result<Vec<ObjectRef>> {
Ok(compressible_objgens_qpdf_plan(pdf)?.eligible)
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct CompressiblePlan {
pub eligible: Vec<ObjectRef>,
pub removed_refs: BTreeSet<ObjectRef>,
pub indirect_objstm_length_refs: BTreeSet<ObjectRef>,
}
pub(crate) fn compressible_objgens_qpdf_plan<R: std::io::Read + std::io::Seek>(
pdf: &mut crate::Pdf<R>,
) -> crate::Result<CompressiblePlan> {
#[cfg(test)]
COMPRESSIBLE_PLAN_CALLS.with(|calls| calls.set(calls.get() + 1));
let mut visited: BTreeSet<u32> = BTreeSet::new();
let mut result: Vec<ObjectRef> = Vec::new();
let mut removed_refs = BTreeSet::new();
let mut indirect_objstm_length_refs = BTreeSet::new();
let max_object = pdf.get_object_count()?;
let trailer_handle = pdf.trailer();
let encrypt_ref = trailer_handle.try_get_key(b"/Encrypt")?.object_ref();
let mut stack: Vec<ObjectHandle> = vec![trailer_handle];
while let Some(object) = stack.pop() {
let Some(object_ref) = object.object_ref() else {
push_handle_children(&object, &mut stack)?;
continue;
};
if object_ref.number == 0 {
continue;
}
if object_ref.number > max_object {
return Err(crate::Error::Internal(
"unexpected object id encountered in getCompressibleObjGens".to_string(),
));
}
if visited.contains(&object_ref.number) {
continue;
}
if pdf.has_newer_cached_generation(object_ref) {
pdf.remove_object_handle(object_ref)?;
removed_refs.insert(object_ref);
continue;
}
visited.insert(object_ref.number);
object.try_dereference()?;
let stream_dict = object.as_stream_dict();
let is_stream = stream_dict.is_some();
if let Some(stream_dict) = stream_dict {
if stream_dict
.try_get_key(b"/Type")
.and_then(|type_value| type_value.try_is_name_and_equals(b"ObjStm"))?
{
if let Some(length_ref) = stream_dict.try_get_key(b"/Length")?.object_ref() {
indirect_objstm_length_refs.insert(length_ref);
}
}
}
let is_signature = !is_stream && is_qpdf_signature_dict(&object)?;
if !is_stream && !is_signature && Some(object_ref) != encrypt_ref {
result.push(object_ref);
}
push_handle_children(&object, &mut stack)?;
}
Ok(CompressiblePlan {
eligible: result,
removed_refs,
indirect_objstm_length_refs,
})
}
pub(crate) fn even_split_into_streams(eligible: &[ObjectRef]) -> Vec<Vec<ObjectRef>> {
even_split_into_streams_with_cap(
eligible,
NonZeroUsize::new(100).expect("qpdf's ObjStm cap is non-zero"),
)
}
pub(crate) fn even_split_into_streams_with_cap(
eligible: &[ObjectRef],
cap: NonZeroUsize,
) -> Vec<Vec<ObjectRef>> {
let n = eligible.len();
if n == 0 {
return Vec::new();
}
let n_streams = n.div_ceil(cap.get());
let n_per = n.div_ceil(n_streams);
eligible.chunks(n_per).map(|chunk| chunk.to_vec()).collect()
}
pub(crate) fn is_qpdf_signature_dict(object: &ObjectHandle) -> crate::Result<bool> {
if !object.try_is_dictionary_of_type(b"", b"")? {
return Ok(false);
}
let type_value = object.try_get_key(b"/Type")?;
if !type_value.try_is_name_and_equals(b"Sig")? {
return Ok(false);
}
Ok(object.try_has_key(b"/ByteRange")? && object.try_has_key(b"/Contents")?)
}
fn push_handle_children(object: &ObjectHandle, stack: &mut Vec<ObjectHandle>) -> crate::Result<()> {
object.try_dereference()?;
if let Some(dict) = object.as_stream_dict() {
return push_handle_dict_children(&dict, stack, true);
}
if object.try_is_dictionary_of_type(b"", b"")? {
return push_handle_dict_children(object, stack, false);
}
if let Some(items) = object.try_as_array()? {
for item in items.into_iter().rev() {
stack.push(item);
}
}
Ok(())
}
fn push_handle_dict_children(
dict: &ObjectHandle,
stack: &mut Vec<ObjectHandle>,
is_stream: bool,
) -> crate::Result<()> {
let keys = dict.try_get_keys()?;
for key in keys.into_iter().rev() {
if is_stream && key.as_slice() == b"/Length" {
continue;
}
stack.push(dict.try_get_key(&key)?);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::compressible_objgens_qpdf_plan;
use crate::{ObjectRef, Pdf};
use std::io::Cursor;
fn reachable_objstm_with_indirect_length() -> Vec<u8> {
let mut pdf = b"%PDF-1.5\n".to_vec();
let catalog_offset = pdf.len();
pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /ObjStm 4 0 R >>\nendobj\n");
let objstm_offset = pdf.len();
pdf.extend_from_slice(
b"4 0 obj\n<< /Type /ObjStm /N 0 /First 0 /Length 5 0 R >>\nstream\n\nendstream\nendobj\n",
);
let length_offset = pdf.len();
pdf.extend_from_slice(b"5 0 obj\n0\nendobj\n");
let xref_offset = pdf.len();
pdf.extend_from_slice(b"xref\n0 6\n0000000000 65535 f \n");
pdf.extend_from_slice(format!("{catalog_offset:010} 00000 n \n").as_bytes());
pdf.extend_from_slice(b"0000000000 65535 f \n0000000000 65535 f \n");
pdf.extend_from_slice(format!("{objstm_offset:010} 00000 n \n").as_bytes());
pdf.extend_from_slice(format!("{length_offset:010} 00000 n \n").as_bytes());
pdf.extend_from_slice(
format!("trailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n{xref_offset}\n%%EOF\n")
.as_bytes(),
);
pdf
}
#[test]
fn compressible_plan_records_reachable_objstm_length_targets_without_following_them() {
let mut pdf = Pdf::open(Cursor::new(reachable_objstm_with_indirect_length()))
.expect("open indirect-length ObjStm fixture");
let plan = compressible_objgens_qpdf_plan(&mut pdf).expect("build compressible plan");
let holder = ObjectRef::new(5, 0);
assert!(
plan.indirect_objstm_length_refs.contains(&holder),
"reachable ObjStm length holder must be recorded"
);
assert!(
!plan.eligible.contains(&holder),
"the stream's /Length edge must not make its holder eligible"
);
}
#[test]
fn indirect_null_type_is_not_signature_eligible() {
let bytes = b"%PDF-1.5\n\
1 0 obj\n<< /Type /Catalog /Sig 2 0 R >>\nendobj\n\
2 0 obj\n<< /Type 3 0 R /ByteRange [0 1 2 3] /Contents <00> >>\nendobj\n\
3 0 obj\nnull\nendobj\n\
trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n0\n%%EOF\n";
let mut pdf = Pdf::open(Cursor::new(bytes.as_slice())).expect("open signature fixture");
let signature = pdf.get_object_handle(ObjectRef::new(2, 0));
assert!(
!super::is_qpdf_signature_dict(&signature).expect("signature eligibility"),
"an indirect-null /Type must not satisfy qpdf's /Sig predicate"
);
}
#[test]
fn signed_signature_dictionary_is_not_objstm_eligible() {
let bytes = b"%PDF-1.5\n\
1 0 obj\n<< /Type /Catalog /Sig 2 0 R >>\nendobj\n\
2 0 obj\n<< /Type /Sig /ByteRange [0 1 2 3] /Contents <00> >>\nendobj\n\
trailer\n<< /Size 3 /Root 1 0 R >>\nstartxref\n0\n%%EOF\n";
let mut pdf = Pdf::open(Cursor::new(bytes.as_slice())).expect("open signature fixture");
let signature_ref = ObjectRef::new(2, 0);
let signature = pdf.get_object_handle(signature_ref);
let ctx = super::EligibilityContext {
encryption_ref: None,
};
assert!(
!super::is_eligible_for_objstm_handle(signature_ref, &signature, &ctx)
.expect("signature eligibility"),
"a signed /Sig dictionary must stay outside an ObjStm"
);
}
}