use super::aes;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Permissions {
bits: u32,
revision: u8,
}
impl Permissions {
pub(crate) const PRINT: u32 = 1 << 2;
pub(crate) const MODIFY: u32 = 1 << 3;
pub(crate) const EXTRACT: u32 = 1 << 4;
pub(crate) const ANNOTATE: u32 = 1 << 5;
pub(crate) const FILL_FORMS: u32 = 1 << 8;
pub(crate) const ACCESSIBILITY: u32 = 1 << 9;
pub(crate) const ASSEMBLE: u32 = 1 << 10;
pub(crate) const PRINT_HIGH: u32 = 1 << 11;
#[must_use]
pub const fn new(p: i64, revision: u8) -> Self {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "/P is a 32-bit two's-complement field: keeping the low 32 bits is the point"
)]
let bits = p as u32;
Self { bits, revision }
}
#[must_use]
pub const fn raw(&self) -> u32 {
self.bits
}
#[must_use]
pub const fn bits(&self) -> u32 {
let r2 = Self::PRINT | Self::MODIFY | Self::EXTRACT | Self::ANNOTATE;
let r3 = Self::FILL_FORMS | Self::ACCESSIBILITY | Self::ASSEMBLE | Self::PRINT_HIGH;
self.bits & if self.revision >= 3 { r2 | r3 } else { r2 }
}
const fn has(self, bit: u32) -> bool {
self.bits & bit != 0
}
const fn r2(self) -> bool {
self.revision < 3
}
#[must_use]
pub const fn print_low_res(&self) -> bool {
self.has(Self::PRINT)
}
#[must_use]
pub const fn print_high_res(&self) -> bool {
self.has(Self::PRINT) && (self.r2() || self.has(Self::PRINT_HIGH))
}
#[must_use]
pub const fn modify(&self) -> bool {
self.has(Self::MODIFY)
}
#[must_use]
pub const fn extract(&self) -> bool {
self.has(Self::EXTRACT)
}
#[must_use]
pub const fn annotate(&self) -> bool {
self.has(Self::ANNOTATE)
}
#[must_use]
pub const fn fill_forms(&self) -> bool {
self.has(Self::ANNOTATE) || (!self.r2() && self.has(Self::FILL_FORMS))
}
#[must_use]
pub const fn accessibility(&self) -> bool {
self.has(if self.r2() { Self::EXTRACT } else { Self::ACCESSIBILITY })
}
#[must_use]
pub const fn assemble(&self) -> bool {
self.has(if self.r2() { Self::MODIFY } else { Self::ASSEMBLE })
}
}
pub(crate) fn perms_match(file_key: &[u8], perms: &[u8], p: u32, encrypt_metadata: bool) -> bool {
let Some(plain) = perms
.get(..16)
.and_then(|block| aes::ecb_decrypt_block(file_key, block))
else {
return false;
};
let flag = if encrypt_metadata { b'T' } else { b'F' };
plain.get(..4) == Some(&p.to_le_bytes()[..]) && plain.get(8) == Some(&flag) && plain.get(9..12) == Some(b"adb")
}
#[cfg(test)]
mod tests {
use super::Permissions;
#[test]
fn new_reads_p_as_32_bit_twos_complement() {
assert_eq!(Permissions::new(-4, 4).raw(), 0xFFFF_FFFC);
}
#[test]
fn bits_keeps_only_defined_bits() {
assert_eq!(Permissions::new(-1, 4).bits(), 0b1111_0011_1100);
}
#[test]
fn bits_under_r2_drops_r3_only_bits() {
assert_eq!(Permissions::new(-1, 2).bits(), 0b11_1100);
}
#[test]
fn fill_forms_under_r2_follows_bit_6() {
assert!(Permissions::new(32, 2).fill_forms());
}
#[test]
fn fill_forms_from_r3_follows_bit_6_or_bit_9() {
assert_eq!(
(
Permissions::new(32, 3).fill_forms(),
Permissions::new(256, 3).fill_forms(),
Permissions::new(0, 3).fill_forms()
),
(true, true, false)
);
}
#[test]
fn assemble_from_r3_ignores_bit_4() {
assert!(!Permissions::new(8, 3).assemble());
}
#[test]
fn print_high_res_needs_bit_12_from_r3() {
assert!(!Permissions::new(4, 3).print_high_res());
}
}