flpdf 0.5.0

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! qpdf correspondence: QPDF_optimization.cc inherited-page-attribute push.
//!
//! Deviation: null checks on an inheritable key's value use the canonical
//! handle resolver before inspecting the value. This keeps lazy indirect
//! values aligned with qpdf's accessors without introducing a second value
//! snapshot route. See `pages.rs`'s
//! `resolve_inherited_handle_with_max_depth` for the corresponding
//! compensation in the sibling bottom-up attribute climb, and the inline
//! deviation-marker comments below for the exact call sites.

use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Seek};

use crate::object_handle::is_scalar;
use crate::pages::repair::{PageTreeRoot, PreparedPages};
use crate::{Error, Pdf, Result};
use crate::{ObjectHandle, ObjectRef};

const INHERITABLE_KEYS: [&[u8]; 4] = [b"/CropBox", b"/MediaBox", b"/Resources", b"/Rotate"];
const MAX_DEPTH: usize = crate::pages::DEFAULT_MAX_PAGE_TREE_DEPTH;

pub(crate) fn push<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    prepared: &PreparedPages,
    allow_changes: bool,
    warn_skipped_keys: bool,
) -> Result<()> {
    let mut key_ancestors: BTreeMap<&'static [u8], Vec<ObjectHandle>> = BTreeMap::new();
    let mut visited = BTreeSet::new();
    match prepared.root {
        PageTreeRoot::Indirect(root) => push_internal(
            pdf,
            root,
            &mut key_ancestors,
            &mut visited,
            allow_changes,
            warn_skipped_keys,
            0,
        )?,
        PageTreeRoot::Direct { catalog } => {
            let catalog = pdf.get_object_handle(catalog);
            push_direct_root(
                pdf,
                catalog,
                &mut key_ancestors,
                &mut visited,
                allow_changes,
                warn_skipped_keys,
            )? // cov:ignore: llvm-cov attributes this covered multi-line Direct branch terminator to the call setup
        }
        PageTreeRoot::DirectCatalog => {
            let catalog = pdf.root_handle()?;
            push_direct_root(
                pdf,
                catalog,
                &mut key_ancestors,
                &mut visited,
                allow_changes,
                warn_skipped_keys,
            )? // cov:ignore: direct-root integration tests exercise this generic dispatch; llvm maps its counter to the callee
        }
    }
    debug_assert!(
        key_ancestors.values().all(Vec::is_empty),
        "key_ancestors not empty after pushing inherited attributes to pages"
    );
    Ok(())
}

fn push_direct_root<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    catalog: ObjectHandle,
    key_ancestors: &mut BTreeMap<&'static [u8], Vec<ObjectHandle>>,
    visited: &mut BTreeSet<ObjectRef>,
    allow_changes: bool,
    warn_skipped_keys: bool,
) -> Result<()> {
    // cov:ignore-start: PreparedPages::Direct is created and consumed without an intervening public mutation
    pdf.resolve(&catalog)?;
    let pages = catalog.get_key(b"/Pages");
    if pages.as_dictionary().is_none() {
        return Ok(());
    }
    // cov:ignore-end
    push_direct_node(
        pdf,
        &pages,
        key_ancestors,
        visited,
        allow_changes,
        warn_skipped_keys,
        0,
    )?;
    Ok(())
}

fn push_direct_node<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    dict: &ObjectHandle,
    key_ancestors: &mut BTreeMap<&'static [u8], Vec<ObjectHandle>>,
    visited: &mut BTreeSet<ObjectRef>,
    allow_changes: bool,
    warn_skipped_keys: bool,
    depth: usize,
) -> Result<()> {
    // cov:ignore-start: prepare_for_optimization traverses the same direct tree with this depth bound first
    if depth >= MAX_DEPTH {
        return Err(Error::Unsupported(format!(
            "page tree depth exceeds maximum of {MAX_DEPTH} in direct /Pages node"
        )));
    }
    if !is_pages_dictionary(dict) {
        return Ok(());
    }
    // cov:ignore-end

    // cov:ignore-start: all production callers pass warn_skipped_keys=false
    if warn_skipped_keys && dict.try_has_key(b"/Parent")? {
        for key in dict.try_get_keys()? {
            if !INHERITABLE_KEYS.contains(&key.as_slice())
                && ![b"/Type".as_slice(), b"/Parent", b"/Kids", b"/Count"].contains(&key.as_slice())
            {
                pdf.push_warning(format!(
                    "Unknown key /{} in /Pages object is being discarded as a result of flattening the /Pages tree",
                    String::from_utf8_lossy(key.strip_prefix(b"/").unwrap_or(&key)),
                ))?;
            }
        }
    }
    // cov:ignore-end

    let own_keys = push_node_attributes(pdf, dict, key_ancestors, allow_changes)?;
    let kids = dict.get_key(b"/Kids");
    if let Some(kids) = kids.as_array() {
        for kid in kids {
            if let Some(kid_ref) = handle_reference(&kid) {
                push_child_reference(
                    pdf,
                    kid_ref,
                    key_ancestors,
                    visited,
                    allow_changes,
                    warn_skipped_keys,
                    depth,
                )?; // cov:ignore: direct-root integration test exercises this branch; LLVM attributes the counter to push_child_reference
            } else if kid.as_dictionary().is_some() && kid.has_key(b"/Kids") {
                push_direct_node(
                    pdf,
                    &kid,
                    key_ancestors,
                    visited,
                    allow_changes,
                    warn_skipped_keys,
                    depth + 1,
                )?; // cov:ignore: direct-descendant integration test exercises this branch; LLVM attributes the counter to push_direct_node
            } // cov:ignore: direct-descendant integration test exercises the branch; LLVM attributes the counter to the recursive callee
        }
    }
    pop_node_attributes(key_ancestors, own_keys);
    Ok(())
}

fn push_node_attributes<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    dict: &ObjectHandle,
    key_ancestors: &mut BTreeMap<&'static [u8], Vec<ObjectHandle>>,
    allow_changes: bool,
) -> Result<Vec<&'static [u8]>> {
    let mut own_keys = Vec::new();
    for &key in &INHERITABLE_KEYS {
        let Some(value) = dict
            .as_dictionary()
            .and_then(|entries| entries.get(key).cloned())
        else {
            continue;
        };
        // Resolve the live value before applying qpdf's null-as-absent rule.
        if pdf.resolve_handle(&value)?.is_null() {
            continue;
        }
        if !allow_changes {
            return Err(Error::Unsupported(
                "optimize detected an inheritable attribute when called in no-change mode"
                    .to_owned(),
            ));
        }
        dict.remove_key(key);
        let value = if value.is_indirect() {
            value
        } else if !is_scalar(&value)? {
            pdf.make_indirect_from_object_handle(value)?
        } else {
            value
        };
        key_ancestors.entry(key).or_default().push(value);
        own_keys.push(key);
    }
    if !own_keys.is_empty() {
        pdf.mark_object_handle_dirty(dict)?;
    }
    Ok(own_keys)
}

fn pop_node_attributes(
    key_ancestors: &mut BTreeMap<&'static [u8], Vec<ObjectHandle>>,
    own_keys: Vec<&'static [u8]>,
) {
    for key in own_keys {
        let stack = key_ancestors
            .get_mut(key)
            .expect("own inherited key must have an ancestor stack");
        stack.pop();
        if stack.is_empty() {
            key_ancestors.remove(key);
        }
    }
}

fn push_child_reference<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    kid_ref: ObjectRef,
    key_ancestors: &mut BTreeMap<&'static [u8], Vec<ObjectHandle>>,
    visited: &mut BTreeSet<ObjectRef>,
    allow_changes: bool,
    warn_skipped_keys: bool,
    depth: usize,
) -> Result<()> {
    let child = pdf.get_object_handle(kid_ref);
    pdf.resolve(&child)?;
    let is_pages_node = is_pages_dictionary(&child);
    if is_pages_node {
        return push_internal(
            pdf,
            kid_ref,
            key_ancestors,
            visited,
            allow_changes,
            warn_skipped_keys,
            depth + 1,
        );
    }

    if child.as_dictionary().is_none() {
        return Ok(()); // cov:ignore: page-tree repair guarantees indirect children are dictionaries
    }
    for (&key, values) in key_ancestors.iter() {
        let present = match child
            .as_dictionary()
            .and_then(|entries| entries.get(key).cloned())
        {
            None => false,
            // Resolve the live value before applying qpdf's null-as-absent rule.
            Some(value) => !pdf.resolve_handle(&value)?.is_null(),
        };
        if !present {
            if let Some(value) = values.last() {
                child.replace_key(key, value.clone())?;
                pdf.mark_object_handle_dirty(&child)?;
            }
        }
    }
    Ok(())
}

fn push_internal<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    node_ref: ObjectRef,
    key_ancestors: &mut BTreeMap<&'static [u8], Vec<ObjectHandle>>,
    visited: &mut BTreeSet<ObjectRef>,
    allow_changes: bool,
    warn_skipped_keys: bool,
    depth: usize,
) -> Result<()> {
    if depth >= MAX_DEPTH {
        return Err(Error::Unsupported(format!(
            "page tree depth exceeds maximum of {MAX_DEPTH} at {node_ref}"
        )));
    }
    if !visited.insert(node_ref) {
        return Ok(()); // cov:ignore: page-tree repair rejects cycles before inherited-attribute push
    }

    let dict = pdf.get_object_handle(node_ref);
    pdf.resolve(&dict)?;
    if dict.as_dictionary().is_none() {
        return Ok(());
    }
    if !is_pages_dictionary(&dict) {
        return Ok(());
    }

    if warn_skipped_keys && dict.try_has_key(b"/Parent")? {
        for key in dict.try_get_keys()? {
            if !INHERITABLE_KEYS.contains(&key.as_slice())
                && ![b"/Type".as_slice(), b"/Parent", b"/Kids", b"/Count"].contains(&key.as_slice())
            {
                pdf.push_warning(format!(
                    "Unknown key /{} in /Pages object is being discarded as a result of flattening the /Pages tree",
                    String::from_utf8_lossy(key.strip_prefix(b"/").unwrap_or(&key)),
                ))?;
            }
        }
    }

    let own_keys = push_node_attributes(pdf, &dict, key_ancestors, allow_changes)?;

    let kids = dict.get_key(b"/Kids").as_array();

    if let Some(kids) = kids {
        for kid in kids {
            let Some(kid_ref) = handle_reference(&kid) else {
                continue;
            };
            push_child_reference(
                pdf,
                kid_ref,
                key_ancestors,
                visited,
                allow_changes,
                warn_skipped_keys,
                depth,
            )?;
        }
    }

    pop_node_attributes(key_ancestors, own_keys);
    Ok(())
}

fn is_pages_dictionary(handle: &ObjectHandle) -> bool {
    handle
        .as_dictionary()
        .and_then(|entries| entries.get(b"/Type".as_slice()).cloned())
        .and_then(|value| value.as_name())
        .is_some_and(|name| name == b"Pages")
}

fn handle_reference(handle: &ObjectHandle) -> Option<ObjectRef> {
    handle.object_ref()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::test_support::NthWriteFailure;
    use crate::pipeline::PipelineHandle;
    use crate::{ObjectHandle, Pdf};

    fn pdf_bytes(bodies: &[(u32, &[u8])]) -> Vec<u8> {
        let mut pdf = b"%PDF-1.4\n".to_vec();
        let mut offsets = vec![0_u64; bodies.last().map_or(1, |(number, _)| *number as usize + 1)];
        for &(number, body) in bodies {
            offsets[number as usize] = pdf.len() as u64;
            pdf.extend_from_slice(format!("{number} 0 obj\n").as_bytes());
            pdf.extend_from_slice(body);
            pdf.extend_from_slice(b"\nendobj\n");
        }
        let xref = pdf.len() as u64;
        pdf.extend_from_slice(format!("xref\n0 {}\n", offsets.len()).as_bytes());
        pdf.extend_from_slice(b"0000000000 65535 f \n");
        for offset in offsets.into_iter().skip(1) {
            pdf.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!(
                "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n",
                bodies.last().map_or(1, |(number, _)| number + 1)
            )
            .as_bytes(),
        );
        pdf
    }

    fn pdf_with_inherited_scalar_rotate() -> Vec<u8> {
        pdf_bytes(&[
            (1, b"<< /Type /Catalog /Pages 2 0 R >>"),
            (2, b"<< /Type /Pages /Kids [3 0 R] /Count 1 /Rotate 90 >>"),
            (
                3,
                b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>",
            ),
        ])
    }

    fn pdf_with_unknown_intermediate_pages_key() -> Vec<u8> {
        pdf_bytes(&[
            (1, b"<< /Type /Catalog /Pages 2 0 R >>"),
            (
                2,
                b"<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 612 792] >>",
            ),
            (
                3,
                b"<< /Type /Pages /Parent 2 0 R /Kids [4 0 R] /Count 1 /Unknown 7 >>",
            ),
            (4, b"<< /Type /Page /Parent 3 0 R >>"),
        ])
    }

    #[test]
    fn no_change_mode_rejects_inheritable_key_before_mutation() {
        let mut pdf = Pdf::open_mem_owned(pdf_with_inherited_scalar_rotate()).unwrap();
        let prepared = crate::pages::repair::prepare_for_optimization(&mut pdf)
            .unwrap()
            .unwrap();
        let PageTreeRoot::Indirect(root) = prepared.root else {
            // cov:ignore-start: fixture catalog has an indirect /Pages root
            panic!("fixture has an indirect /Pages root");
            // cov:ignore-end
        };
        let root_handle = pdf.get_object_handle(root);
        pdf.resolve(&root_handle).unwrap();
        let before = root_handle.get_key(b"/Rotate").as_integer();

        let error = push(&mut pdf, &prepared, false, false).unwrap_err();

        assert!(error.to_string().contains("inheritable attribute"));
        assert_eq!(root_handle.get_key(b"/Rotate").as_integer(), before);
    }

    #[test]
    fn pushing_an_inherited_key_onto_a_leaf_preserves_its_source_extent() {
        // The fixture's page 3 lacks /Rotate, so it actually inherits
        // /Rotate 90 from its /Pages parent -- exercising
        // `push_child_reference`'s `changed` branch, not the unchanged-leaf
        // skip. qpdf's `kid.replaceKey` never clears the leaf's recorded
        // source extent for this same mutation; the leaf's extent here must
        // match.
        let mut pdf = Pdf::open_mem_owned(pdf_with_inherited_scalar_rotate()).unwrap();
        let leaf_ref = ObjectRef::new(3, 0);
        pdf.get_object_handle(leaf_ref).try_dereference().unwrap();
        let before = pdf.get_object_handle(leaf_ref).end_offsets();
        assert_ne!(
            before,
            (-1, -1),
            "fixture leaf must have a real parse extent"
        );

        let prepared = crate::pages::repair::prepare_for_optimization(&mut pdf)
            .unwrap()
            .unwrap();
        push(&mut pdf, &prepared, true, false).unwrap();

        let leaf = pdf.get_object_handle(leaf_ref);
        pdf.resolve(&leaf).unwrap();
        assert_eq!(
            leaf.get_key(b"/Rotate").as_integer(),
            Some(90),
            "leaf must have actually inherited /Rotate"
        );
        assert_eq!(
            pdf.get_object_handle(leaf_ref).end_offsets(),
            before,
            "inheriting a key must not clear the leaf's source extent"
        );
    }

    #[test]
    fn direct_pages_root_walks_direct_descendant_and_reference_kids() {
        let mut pdf = Pdf::open_mem_owned(pdf_bytes(&[
            (
                1,
                b"<< /Type /Catalog /Pages << /Type /Pages /Kids [<< /Type /Pages /Kids [3 0 R] /Count 1 /Rotate 90 >>] /Count 1 >> >>",
            ),
            (
                3,
                b"<< /Type /Page /MediaBox [0 0 612 792] >>",
            ),
        ]))
        .unwrap();
        let prepared = PreparedPages {
            root: PageTreeRoot::Direct {
                catalog: ObjectRef::new(1, 0),
            },
            pages: vec![ObjectRef::new(3, 0)],
        };

        push(&mut pdf, &prepared, true, false).expect("direct root walk");

        let page = pdf.get_object_handle(ObjectRef::new(3, 0));
        pdf.resolve(&page).expect("resolve direct-root page");
        assert_eq!(page.get_key(b"/Rotate").as_integer(), Some(90));
    }

    #[test]
    fn warning_mode_reports_unknown_intermediate_pages_key() {
        let mut pdf = Pdf::open_mem_owned(pdf_with_unknown_intermediate_pages_key()).unwrap();
        let prepared = crate::pages::repair::prepare_for_optimization(&mut pdf)
            .unwrap()
            .unwrap();

        push(&mut pdf, &prepared, true, true).unwrap();

        assert!(pdf.repair_diagnostics().entries().iter().any(|diagnostic| {
            diagnostic.message.contains("Unknown key /Unknown")
                && diagnostic.message.contains("/Pages")
        }));
        let page = pdf.get_object_handle(prepared.pages[0]);
        pdf.resolve(&page).unwrap();
        assert!(page.has_key(b"/MediaBox"));
    }

    #[test]
    fn warning_sink_failure_propagates_from_an_intermediate_pages_key() {
        let mut pdf = Pdf::open_mem_owned(pdf_with_unknown_intermediate_pages_key()).unwrap();
        let prepared = crate::pages::repair::prepare_for_optimization(&mut pdf)
            .unwrap()
            .unwrap();
        let logger = crate::QPDFLogger::create();
        logger.set_warn(Some(PipelineHandle::new(NthWriteFailure::new(1))));
        pdf.set_logger(logger);

        assert!(matches!(
            push(&mut pdf, &prepared, true, true),
            Err(crate::Error::System(ref message)) if message == "sink write failure 1"
        ));
        assert!(pdf
            .repair_diagnostics()
            .entries()
            .iter()
            .any(|diagnostic| diagnostic.message.contains("Unknown key /Unknown")));
    }

    #[test]
    fn excessive_depth_error_propagates_from_a_child_pages_node() {
        let mut pdf = Pdf::open_mem_owned(pdf_bytes(&[
            (1, b"<< /Type /Catalog /Pages 2 0 R >>"),
            (2, b"<< /Type /Pages /Kids [] /Count 0 >>"),
        ]))
        .unwrap();
        for depth in 0..MAX_DEPTH {
            let number = 2 + depth as u32;
            let child = number + 1;
            let node = ObjectHandle::dictionary(vec![
                (b"/Type".to_vec(), ObjectHandle::name(b"Pages".to_vec())),
                (
                    b"/Kids".to_vec(),
                    ObjectHandle::array(vec![pdf.get_object_handle(ObjectRef::new(child, 0))]),
                ),
                (b"/Count".to_vec(), ObjectHandle::integer(0)),
            ]);
            pdf.replace_object(ObjectRef::new(number, 0), node).unwrap();
        }
        let boundary = ObjectHandle::dictionary(vec![
            (b"/Type".to_vec(), ObjectHandle::name(b"Pages".to_vec())),
            (b"/Kids".to_vec(), ObjectHandle::array(Vec::new())),
            (b"/Count".to_vec(), ObjectHandle::integer(0)),
        ]);
        pdf.replace_object(ObjectRef::new(2 + MAX_DEPTH as u32, 0), boundary)
            .unwrap();
        let prepared = PreparedPages {
            root: PageTreeRoot::Indirect(ObjectRef::new(2, 0)),
            pages: Vec::new(),
        };

        let error = push(&mut pdf, &prepared, true, false).unwrap_err();

        assert!(matches!(error, Error::Unsupported(ref message)
                if message.contains("page tree depth exceeds maximum")));
    }
}