laminapdf-cos 0.9.0

Read PDF files in Rust, broken ones included: the cross-reference table with repair, object streams, decryption and the page tree, plus an overlay that records edits.
Documentation
//! [`OpenOptions`]: limits, strict mode and the password (ADR 0003 §3).

use std::fmt;

use laminapdf_syntax::Limits;
use zeroize::Zeroizing;

/// How [`Document::open`](crate::Document::open) reads its input.
///
/// Private fields; start from [`OpenOptions::default`] and use the `with_*` builders:
///
/// ```
/// use laminapdf_cos::{Limits, OpenOptions};
///
/// let options = OpenOptions::default()
///     .with_limits(Limits::default().with_max_stream_decoded(1 << 20))
///     .with_strict(true);
/// assert!(options.strict());
/// assert_eq!(options.limits().max_stream_decoded(), 1 << 20);
/// ```
///
/// Added in 0.2.0.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct OpenOptions {
    limits: Limits,
    strict: bool,
    password: Option<Zeroizing<Vec<u8>>>,
}

impl OpenOptions {
    /// The defaults, as [`OpenOptions::default`].
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the resource [`Limits`] (default: [`Limits::default`]).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn with_limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    /// Strict mode (default off): every [`Warning`](crate::Warning) becomes an
    /// [`Error::Strict`](crate::Error::Strict), so a file that needs repair is reported
    /// instead of silently mended (validators, signature verification).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn with_strict(mut self, strict: bool) -> Self {
        self.strict = strict;
        self
    }

    /// The password for an encrypted file: the user or the owner password, as bytes or text.
    /// Without one, the empty user password is tried.
    ///
    /// R2–R4 passwords are PDFDocEncoding bytes (text with non-ASCII characters is also tried
    /// as Latin-1); R5/R6 passwords are UTF-8, prepared with SASLprep. A wrong password makes
    /// [`Document::open`](crate::Document::open) fail with
    /// [`Error::WrongPassword`](crate::Error::WrongPassword); it is never printed by `Debug`.
    ///
    /// The options, and each clone of them, overwrite their password when dropped. A `String` or
    /// `Vec<u8>` passed here hands over its buffer; a `&str` or slice is copied, and the original
    /// stays the caller's to wipe.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn with_password(mut self, password: impl Into<Vec<u8>>) -> Self {
        self.password = Some(Zeroizing::new(password.into()));
        self
    }

    /// The resource limits.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn limits(&self) -> &Limits {
        &self.limits
    }

    /// Whether strict mode is on.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn strict(&self) -> bool {
        self.strict
    }

    /// The password, if one was given.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn password(&self) -> Option<&[u8]> {
        self.password.as_deref().map(Vec::as_slice)
    }
}

impl fmt::Debug for OpenOptions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Never print the password.
        f.debug_struct("OpenOptions")
            .field("limits", &self.limits)
            .field("strict", &self.strict)
            .field("password", &self.password.as_ref().map(|_| "<redacted>"))
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn debug_redacts_the_password() {
        let options = OpenOptions::default().with_password("hunter2");
        assert!(!format!("{options:?}").contains("hunter2"));
    }

    #[test]
    fn the_password_is_held_in_a_buffer_wiped_on_drop() {
        let options = OpenOptions::default().with_password("hunter2");
        // Safe code can't read freed memory, so the field's type is what guarantees the wipe.
        let held: Option<&Zeroizing<Vec<u8>>> = options.password.as_ref();
        assert_eq!(held.map(|p| p.as_slice()), Some(b"hunter2".as_slice()));
    }

    #[test]
    fn default_is_tolerant_with_default_limits() {
        let options = OpenOptions::default();
        assert_eq!((options.strict(), *options.limits()), (false, Limits::default()));
    }
}