use crate::{EncryptionError, Result};
use typed_builder::TypedBuilder;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Permissions(u32);
impl Permissions {
pub const PRINT: Self = Self(1 << 2);
pub const MODIFY: Self = Self(1 << 3);
pub const COPY: Self = Self(1 << 4);
pub const ANNOTATE: Self = Self(1 << 5);
pub const FILL_FORMS: Self = Self(1 << 8);
pub const EXTRACT_ACCESSIBILITY: Self = Self(1 << 9);
pub const ASSEMBLE: Self = Self(1 << 10);
pub const PRINT_HIGH_QUALITY: Self = Self(1 << 11);
pub const ALL: Self = Self(0xFFFF_FFFC);
pub const NONE: Self = Self(0xFFFF_F0C0);
#[must_use]
pub const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(&self) -> u32 {
self.0
}
}
impl std::ops::BitOr for Permissions {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl std::ops::BitAnd for Permissions {
type Output = Self;
fn bitand(self, rhs: Self) -> Self {
Self(self.0 & rhs.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EncryptionAlgorithm {
#[default]
Aes256,
Aes128,
Rc4_128,
}
#[derive(Debug, Clone, TypedBuilder)]
pub struct EncryptionOptions {
#[builder(setter(into))]
pub owner_password: String,
#[builder(default, setter(into, strip_option))]
pub user_password: Option<String>,
#[builder(default = Permissions::ALL)]
pub permissions: Permissions,
#[builder(default)]
pub algorithm: EncryptionAlgorithm,
#[builder(default = true)]
pub encrypt_metadata: bool,
}
#[derive(Debug)]
pub struct EncryptedPdf {
pub data: Vec<u8>,
}
impl EncryptedPdf {
pub fn write_to_file(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
std::fs::write(path, &self.data)?;
Ok(())
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.data
}
#[must_use]
pub fn into_bytes(self) -> Vec<u8> {
self.data
}
}
pub fn encrypt_pdf(_pdf_data: &[u8], _options: &EncryptionOptions) -> Result<EncryptedPdf> {
Err(EncryptionError::RequiresLicense.into())
}
pub fn decrypt_pdf(_pdf_data: &[u8], _password: &str) -> Result<Vec<u8>> {
Err(EncryptionError::RequiresLicense.into())
}