flpdf 0.7.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
//! Helpers for `/EmbeddedFile` stream dictionaries.
//!
//! qpdf correspondence: QPDFEFStreamObjectHelper.cc.
//!

use super::shared::{ensure_indirect_handle_belongs_to_pdf, qpdf_style_open_error};
use crate::object_handle::{canonical_dictionary_key, StreamDataProvider};
use crate::pdf_string::utf8_value;
use crate::pipeline::count::Count;
use crate::pipeline::md5::PlMd5;
use crate::pipeline::{Discard, Pipeline};
use crate::writer::DecodeLevel;
use crate::{Error, ObjectHandle, ObjectRef, Pdf, Result};
use std::cell::RefCell;
use std::fs::File;
use std::io::{Read, Seek};
use std::path::Path;
use std::rc::Rc;

// ── EmbeddedFileStream ────────────────────────────────────────────────────────

/// Wrapper for a `/EmbeddedFile` stream (ISO 32000-1 §7.11.4).
///
/// Construct via [`crate::FileSpec::embedded_file`] rather than directly.
///
/// All accessors are cheap: only [`payload`](EmbeddedFileStream::payload)
/// performs I/O (decoding the filter chain).
pub struct EmbeddedFileStream<'a, R: Read + Seek + 'static> {
    /// qpdf's shared `/EmbeddedFile` object handle. Unlike a copied stream
    /// value, this preserves identity and lets metadata setters
    /// update its dictionary without cloning its payload.
    stream: ObjectHandle,
    // The wrapper still owns the document's exclusive borrow. RefCell only
    // permits qpdf-shaped read accessors to perform explicit resolution.
    #[allow(dead_code)]
    pdf: RefCell<&'a mut Pdf<R>>,
}

impl<'a, R: Read + Seek> EmbeddedFileStream<'a, R> {
    /// Create an indirect `/EmbeddedFile` stream from decoded data, including
    /// qpdf's computed `/Params /Size` and binary MD5 `/CheckSum` values.
    ///
    /// This Rust form of qpdf's `createEFStream` returns the created
    /// `ObjectHandle`; use [`Self::new`] to obtain the borrowing helper.
    pub fn create_ef_stream(pdf: &mut Pdf<R>, data: impl AsRef<[u8]>) -> Result<ObjectHandle> {
        let stream = pdf.new_stream()?;
        stream.replace_stream_data(
            Rc::new(data.as_ref().to_vec()),
            Some(ObjectHandle::null()),
            Some(ObjectHandle::null()),
        );
        Self::new_from_stream(stream)
    }

    /// Create an indirect `/EmbeddedFile` stream from a deferred qpdf-style
    /// provider.  The provider is retained by the stream and is not invoked
    /// until the common finalization path pipes the stream data.
    ///
    /// This is qpdf's provider overload from
    /// `QPDFEFStreamObjectHelper.cc:102-107`: qpdf creates an empty stream,
    /// installs the provider with `replaceStreamData`, and then delegates to
    /// one `newFromStream` implementation.
    pub fn create_ef_stream_from_provider(
        pdf: &mut Pdf<R>,
        provider: Rc<dyn StreamDataProvider>,
    ) -> Result<ObjectHandle> {
        let stream = pdf.new_stream()?;
        stream.replace_stream_data_provider(
            provider,
            Some(ObjectHandle::null()),
            Some(ObjectHandle::null()),
        )?; // cov:ignore: exercised by create_ef_stream_from_provider_finalizes_the_deferred_payload; llvm-cov attributes the successful multiline call to its argument lines, not this terminator
        Self::new_from_stream(stream)
    }

    /// Apply qpdf's shared `newFromStream` EmbeddedFile finalization.
    ///
    /// `QPDFEFStreamObjectHelper.cc:131-148` sets `/Type` before piping the
    /// decoded stream through `Pl_Count -> Pl_MD5 -> Pl_Discard`.  `/Params`
    /// is populated only after a successful pipe; a failed provider/filter
    /// path gets qpdf's warning and never falls back to a materialized length
    /// or a second digest computation.
    pub(super) fn new_from_stream(stream: ObjectHandle) -> Result<ObjectHandle> {
        let stream_dict = stream.as_stream_dict().ok_or_else(|| {
            Error::System("EmbeddedFile factory received a non-stream object".to_string())
        })?;
        stream_dict.replace_key(b"/Type", ObjectHandle::name(b"EmbeddedFile".to_vec()))?;

        let mut discard = Discard;
        let mut md5 = PlMd5::new("EF md5", &mut discard);
        let (success, size, checksum) = {
            let mut count = Count::new("EF size", &mut md5);
            let mut filtering_attempted = false;
            let success = stream.pipe_stream_data(
                &mut count,
                &mut filtering_attempted,
                0,
                DecodeLevel::All,
                false,
                false,
            )?;
            let size = count.count();
            drop(count);
            let checksum = if success {
                Some(
                    hex::decode(md5.get_hex_digest()?)
                        .expect("PlMd5 always returns a hexadecimal digest"),
                )
            } else {
                None
            };
            (success, size, checksum)
        };

        if success {
            let checksum = checksum.expect("successful EmbeddedFile pipe has a checksum");
            stream_dict.replace_key(
                b"/Params",
                ObjectHandle::dictionary(vec![
                    (
                        b"/Size".to_vec(),
                        ObjectHandle::integer(
                            i64::try_from(size).map_err(|_| {
                                // cov:ignore-start: an in-memory stream cannot exceed PDF's signed integer range in tests
                                Error::System(
                                    "EmbeddedFile size exceeds PDF integer range".to_string(),
                                )
                                // cov:ignore-end
                            })?, // cov:ignore: closing line of the unreachable signed-PDF-range guard
                        ),
                    ),
                    (b"/CheckSum".to_vec(), ObjectHandle::string(checksum)),
                ]),
            )?; // cov:ignore: factory-created stream and parameter values share this PDF
        } else {
            stream.warn_if_possible("unable to get stream data for new embedded file stream")?;
        }

        Ok(stream)
    }

    /// Create an embedded-file stream from a filesystem path.
    ///
    /// The path is retained by qpdf's provider-shaped callback and the file is
    /// opened afresh for each stream pipe. This is the Rust equivalent of
    /// qpdf's `QUtil::file_provider` overload: it does not materialize the
    /// complete file into an intermediate `Vec<u8>`, and repeated reads use
    /// the same provider source. This follows the path overload in
    /// `QPDFFileSpecObjectHelper.cc:85-105` and the provider construction in
    /// `QPDFEFStreamObjectHelper.cc:90-107`.
    pub fn create_ef_stream_from_path<P: AsRef<Path>>(
        pdf: &mut Pdf<R>,
        path: P,
    ) -> Result<ObjectHandle> {
        let path = path.as_ref().to_path_buf();
        let stream = pdf.new_stream()?;
        stream.replace_stream_data_with_callback(
            move |pipeline| {
                let mut file =
                    File::open(&path).map_err(|error| qpdf_style_open_error(&path, error))?;
                let mut buffer = [0_u8; 8192];
                loop {
                    let read = file.read(&mut buffer)?;
                    if read == 0 {
                        break;
                    }
                    pipeline.write(&buffer[..read]).map_err(Error::from)?;
                }
                pipeline.finish().map_err(Error::from)?;
                Ok(())
            },
            Some(ObjectHandle::null()),
            Some(ObjectHandle::null()),
        )?; // cov:ignore: Pdf::new_stream guarantees a stream handle here
        Self::new_from_stream(stream)
    }

    /// Construct a wrapper for a direct or indirect `/EmbeddedFile` stream
    /// handle, matching qpdf's `QPDFEFStreamObjectHelper(QPDFObjectHandle)`
    /// constructor. An indirect handle must be the canonical handle of `pdf`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Unsupported`] for a handle owned by another [`Pdf`].
    /// Rust makes qpdf's document ownership explicit here rather than silently
    /// resolving the same object number in the supplied document.
    pub fn new(stream: ObjectHandle, pdf: &'a mut Pdf<R>) -> Result<Self> {
        ensure_indirect_handle_belongs_to_pdf(&stream, pdf, "embedded-file")?;
        Ok(Self {
            stream,
            pdf: RefCell::new(pdf),
        })
    }

    fn resolved_stream(&self) -> Result<Option<(ObjectHandle, ObjectHandle, Option<ObjectRef>)>> {
        // QPDFEFStreamObjectHelper::getParam calls QPDFObjectHandle::getDict
        // (libqpdf/QPDFEFStreamObjectHelper.cc:20-28), whose
        // asStreamWithAssert/assertType path raises this runtime error for a
        // non-stream object (libqpdf/QPDFObjectHandle.cc:319-324, 2215-2223).
        let terminal_ref = self.stream.object_ref();
        self.stream.try_dereference()?;
        let stream = self.stream.clone();
        if let Some(dictionary) = stream.as_stream_dict() {
            return Ok(Some((stream, dictionary, terminal_ref)));
        }
        if stream.is_null() {
            return Ok(None);
        }
        let type_name = stream.type_name()?;
        Err(Error::System(format!(
            "operation for stream attempted on object of type {}",
            type_name
        )))
    }

    fn resolved_key(&self, dictionary: &ObjectHandle, key: &[u8]) -> Result<ObjectHandle> {
        let key = canonical_dictionary_key(key);
        dictionary.try_get_key(&key)
    }

    fn param_value(&self, key: &[u8]) -> Result<ObjectHandle> {
        let Some((_, stream_dict, _)) = self.resolved_stream()? else {
            return Ok(ObjectHandle::null());
        };
        let params = self.resolved_key(&stream_dict, b"Params")?;
        if !params.try_is_dictionary()? {
            return Ok(ObjectHandle::null());
        }
        self.resolved_key(&params, key)
    }

    /// Decode and return the payload bytes through the canonical qpdf stream
    /// pipeline.
    ///
    /// This is `QPDFObjectHandle::getStreamData`
    /// (`libqpdf/QPDFObjectHandle.cc:1289-1292`) over
    /// `QPDF_Stream::getStreamData` (`libqpdf/QPDF_Stream.cc:344-360`),
    /// applied to the `/EmbeddedFile` stream at the `qpdf_dl_all` decode
    /// level, matching `QPDFJob::doShowAttachment`
    /// (`libqpdf/QPDFJob.cc:914-926`). Because this goes through
    /// [`ObjectHandle::pipe_stream_data`](crate::ObjectHandle::pipe_stream_data)
    /// rather than decoding the `/Filter` chain directly, it also applies any
    /// registered token filters and reads provider-backed or replaced
    /// payloads the same way as every other stream data consumer.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Unsupported`] when the embedded-file entry resolves to
    /// a null object, and [`Error::System`] when it resolves to a non-null
    /// object that is not a stream — the latter is qpdf's `asStreamWithAssert`
    /// runtime error (`libqpdf/QPDFObjectHandle.cc:319-324,2215-2223`).
    /// Returns [`Error::QpdfExc`] with
    /// [`QpdfErrorCode::Unsupported`](crate::QpdfErrorCode::Unsupported)
    /// when the stream itself has no usable filter branch, matching qpdf's
    /// `getStreamData` throw contract — this is also what a document-backed
    /// attachment reports when its decoder fails fatally, because that path
    /// downgrades the codec failure to a warning and an unsuccessful pipe
    /// rather than an error. A run-time failure that does propagate out of
    /// the pipeline itself, such as a registered token filter or a sink that
    /// errors, surfaces as [`Error::System`], this crate's
    /// `std::runtime_error`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use flpdf::{FileSpec, ObjectRef, Pdf};
    /// # use std::fs::File;
    /// # use std::io::BufReader;
    /// # let mut pdf = Pdf::open(BufReader::new(File::open("a.pdf")?))?;
    /// # let mut fs = FileSpec::new(pdf.get_object_handle(ObjectRef::new(5, 0)), &mut pdf).unwrap();
    /// if let Some(mut ef) = fs.embedded_file()? {
    ///     let data = ef.payload()?;
    ///     assert!(!data.is_empty());
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn payload(&self) -> Result<Rc<Vec<u8>>> {
        let Some((stream, _, _)) = self.resolved_stream()? else {
            return Err(Error::Unsupported(
                "expected an /EmbeddedFile stream object".to_string(),
            ));
        };
        stream.get_stream_data(DecodeLevel::All)
    }

    /// Pipe the decoded payload through the canonical qpdf stream pipeline.
    ///
    /// This combines `QPDFFileSpecObjectHelper::getEmbeddedFileStream`
    /// (`libqpdf/QPDFFileSpecObjectHelper.cc:61-77`) with
    /// `QPDFObjectHandle::pipeStreamData` (`libqpdf/QPDFObjectHandle.cc:1301-1324`).
    /// A filter failure is returned as `Ok(false)` after the owning document
    /// has received its warning, matching qpdf's `doShowAttachment` caller,
    /// which intentionally ignores that boolean.
    pub(crate) fn pipe_stream_data(&self, pipeline: &mut dyn Pipeline) -> Result<bool> {
        let Some((stream, _, _)) = self.resolved_stream()? else {
            return Err(Error::Unsupported(
                "expected an /EmbeddedFile stream object".to_string(),
            ));
        };
        let mut filtering_attempted = false;
        stream.pipe_stream_data(
            pipeline,
            &mut filtering_attempted,
            0,
            DecodeLevel::All,
            false,
            false,
        )
    }

    /// Return the MIME type from `/Subtype`, as raw bytes.
    ///
    /// `/Subtype` is a PDF name, e.g. `b"application/pdf"`.  Returns `None`
    /// when the key is absent.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases; never errors.
    pub fn mimetype(&self) -> Result<Option<Vec<u8>>> {
        let Some((_, stream_dict, _)) = self.resolved_stream()? else {
            return Ok(None);
        };
        self.resolved_key(&stream_dict, b"Subtype")?.try_as_name()
    }

    /// Return `/Params /CreationDate` as a raw PDF date byte sequence.
    ///
    /// PDF date format: `D:YYYYMMDDHHmmSSOHH'mm'` (ISO 32000-1 §7.9.4).
    /// No date parsing is performed — the bytes are returned as-is.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn creation_date(&self) -> Result<Option<Vec<u8>>> {
        let value = self.param_value(b"CreationDate")?;
        value.try_dereference()?;
        Ok(value.as_string())
    }

    /// Return `/Params /ModDate` as a raw PDF date byte sequence.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn modification_date(&self) -> Result<Option<Vec<u8>>> {
        let value = self.param_value(b"ModDate")?;
        value.try_dereference()?;
        Ok(value.as_string())
    }

    /// Return `/Params /CheckSum` as raw bytes (typically a 16-byte MD5 hash).
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn checksum(&self) -> Result<Option<Vec<u8>>> {
        let value = self.param_value(b"CheckSum")?;
        value.try_dereference()?;
        Ok(value.as_string())
    }

    /// Return `/Params /Size` — the uncompressed file size in bytes.
    ///
    /// # Errors
    ///
    /// Returns `Ok(None)` for all missing/wrong-type cases.
    pub fn size(&self) -> Result<Option<i64>> {
        self.param_value(b"Size")?.try_as_integer()
    }

    /// Return `/Params /CreationDate` through qpdf's UTF-8 string view.
    ///
    /// The byte vector mirrors qpdf's `std::string`: an explicit UTF-8 BOM
    /// may be followed by invalid UTF-8, which Rust's [`String`] cannot hold.
    pub fn get_creation_date(&self) -> Result<Vec<u8>> {
        Ok(self
            .creation_date()?
            .map(|value| utf8_value(&value))
            .unwrap_or_default())
    }

    /// Return `/Params /ModDate` through qpdf's UTF-8 string view.
    pub fn get_mod_date(&self) -> Result<Vec<u8>> {
        Ok(self
            .modification_date()?
            .map(|value| utf8_value(&value))
            .unwrap_or_default())
    }

    /// Return `/Params /Size`, or qpdf's `0` default when it is absent,
    /// negative, or not representable as `usize`.
    pub fn get_size(&self) -> Result<usize> {
        let Some(value) = self.size()? else {
            return Ok(0);
        };
        if value < 0 {
            return Ok(0);
        }
        Ok(u32::try_from(value).unwrap_or(u32::MAX) as usize)
    }

    /// Return `/Subtype` as a MIME-type string, without a leading slash.
    pub fn get_subtype(&self) -> Result<Vec<u8>> {
        Ok(self
            .mimetype()?
            .filter(|value| !value.is_empty())
            .unwrap_or_default())
    }

    /// Return `/Params /CheckSum` as qpdf's binary string value.
    pub fn get_checksum(&self) -> Result<Vec<u8>> {
        Ok(self.checksum()?.unwrap_or_default())
    }

    fn set_param(&mut self, key: &str, value: Vec<u8>) -> Result<()> {
        let Some((_, stream_dict, _stream_ref)) = self.resolved_stream()? else {
            return Ok(());
        };
        let params = stream_dict.try_get_key(b"/Params")?;
        if params.try_is_dictionary()? {
            let target = params;
            let key = canonical_dictionary_key(key.as_bytes());
            target.replace_key(&key, ObjectHandle::string(value))?;
            return Ok(());
        }

        stream_dict.replace_key(
            b"/Params",
            ObjectHandle::dictionary(vec![(
                canonical_dictionary_key(key.as_bytes()),
                ObjectHandle::string(value),
            )]),
        )?; // cov:ignore: LLVM maps this covered multiline /Params creation call to its argument lines, not this terminator
        Ok(())
    }

    /// Set `/Params /CreationDate` to a raw PDF date string.
    pub fn set_creation_date(&mut self, value: impl AsRef<[u8]>) -> Result<&mut Self> {
        self.set_param("CreationDate", value.as_ref().to_vec())?;
        Ok(self)
    }

    /// Set `/Params /ModDate` to a raw PDF date string.
    pub fn set_mod_date(&mut self, value: impl AsRef<[u8]>) -> Result<&mut Self> {
        self.set_param("ModDate", value.as_ref().to_vec())?;
        Ok(self)
    }

    /// Set `/Subtype` to a MIME type represented as logical PDF Name bytes.
    pub fn set_subtype(&mut self, value: impl AsRef<[u8]>) -> Result<&mut Self> {
        let Some((_, stream_dict, _stream_ref)) = self.resolved_stream()? else {
            return Ok(self);
        };
        stream_dict.replace_key(b"/Subtype", ObjectHandle::name(value.as_ref().to_vec()))?;
        Ok(self)
    }
}