181 lines
7.4 KiB
Rust
181 lines
7.4 KiB
Rust
mod common;
|
|
|
|
use common::*;
|
|
use lopdf::{Document, Object, Stream, StringFormat, dictionary};
|
|
use pictor_meta::{Group, Kind, MetaError, read, strip};
|
|
|
|
const PDF_AUTHOR: &str = "Jan Novák";
|
|
|
|
fn utf16be(s: &str) -> Vec<u8> {
|
|
let mut v = vec![0xFE, 0xFF];
|
|
s.encode_utf16().for_each(|u| v.extend_from_slice(&u.to_be_bytes()));
|
|
v
|
|
}
|
|
|
|
fn xmp_for_pdf() -> String {
|
|
xmp_packet().replace(AUTHOR, PDF_AUTHOR)
|
|
}
|
|
|
|
/// One page drawing one image XObject; /Info with a UTF-16BE author; XMP on the catalog and on the image;
|
|
/// /PieceInfo on the page.
|
|
fn build(modern: bool) -> Vec<u8> {
|
|
let mut doc = Document::with_version("1.5");
|
|
let pages_id = doc.new_object_id();
|
|
let meta = || dictionary! { "Type" => "Metadata", "Subtype" => "XML" };
|
|
let xmp_doc = doc.add_object(Stream::new(meta(), xmp_for_pdf().into_bytes()).with_compression(false));
|
|
let xmp_img = doc.add_object(Stream::new(meta(), xmp_for_pdf().into_bytes()).with_compression(false));
|
|
let img = doc.add_object(Stream::new(
|
|
dictionary! {
|
|
"Type" => "XObject", "Subtype" => "Image", "Width" => 2, "Height" => 2,
|
|
"ColorSpace" => "DeviceRGB", "BitsPerComponent" => 8, "Metadata" => xmp_img,
|
|
},
|
|
vec![255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255],
|
|
));
|
|
let content = doc.add_object(Stream::new(dictionary! {}, b"q 100 0 0 100 50 50 cm /Im1 Do Q".to_vec()));
|
|
let page = doc.add_object(dictionary! {
|
|
"Type" => "Page", "Parent" => pages_id, "Contents" => content,
|
|
"Resources" => dictionary! { "XObject" => dictionary! { "Im1" => img } },
|
|
"MediaBox" => vec![0.into(), 0.into(), 200.into(), 200.into()],
|
|
"PieceInfo" => dictionary! { "Illustrator" => dictionary! { "Private" => Object::string_literal("secret private") } },
|
|
});
|
|
doc.objects.insert(
|
|
pages_id,
|
|
Object::Dictionary(dictionary! { "Type" => "Pages", "Kids" => vec![page.into()], "Count" => 1 }),
|
|
);
|
|
let catalog = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id, "Metadata" => xmp_doc });
|
|
let info = doc.add_object(dictionary! {
|
|
"Author" => Object::String(utf16be(PDF_AUTHOR), StringFormat::Hexadecimal),
|
|
"Producer" => Object::string_literal("Fixture Producer 1.0"),
|
|
"Creator" => Object::string_literal(SOFTWARE),
|
|
"Title" => Object::string_literal("Secret plans"),
|
|
"CreationDate" => Object::string_literal("D:20240501102030+02'00'"),
|
|
"Department" => Object::string_literal("Custom department"),
|
|
});
|
|
doc.trailer.set("Root", catalog);
|
|
doc.trailer.set("Info", info);
|
|
let mut out = Vec::new();
|
|
if modern {
|
|
doc.save_modern(&mut out).unwrap()
|
|
} else {
|
|
doc.save_to(&mut out).unwrap()
|
|
}
|
|
out
|
|
}
|
|
|
|
fn any_metadata_key(o: &Object) -> bool {
|
|
match o {
|
|
Object::Dictionary(d) => {
|
|
d.has(b"Metadata") || d.has(b"PieceInfo") || d.iter().any(|(_, v)| any_metadata_key(v))
|
|
}
|
|
Object::Stream(s) => {
|
|
s.dict.has(b"Metadata") || s.dict.has_type(b"Metadata") || s.dict.iter().any(|(_, v)| any_metadata_key(v))
|
|
}
|
|
Object::Array(a) => a.iter().any(any_metadata_key),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
fn first_page_content(bytes: &[u8]) -> Vec<u8> {
|
|
let doc = Document::load_mem(bytes).unwrap();
|
|
let page = *doc.get_pages().values().next().unwrap();
|
|
doc.get_page_content(page)
|
|
}
|
|
|
|
fn assert_clean(bytes: &[u8]) {
|
|
let doc = Document::load_mem(bytes).unwrap();
|
|
assert!(!doc.trailer.has(b"Info"), "trailer /Info removed");
|
|
for (id, o) in &doc.objects {
|
|
assert!(!any_metadata_key(o), "object {id:?} still has metadata: {o:?}");
|
|
}
|
|
let lower = String::from_utf8_lossy(bytes).to_lowercase();
|
|
for needle in [
|
|
"Novák",
|
|
"Fixture Producer",
|
|
"Secret plans",
|
|
"Custom department",
|
|
"secret private",
|
|
XMP_NS,
|
|
"adobe:ns:meta",
|
|
SOFTWARE,
|
|
] {
|
|
assert!(!contains(bytes, needle.as_bytes()), "raw bytes still contain {needle:?}");
|
|
}
|
|
assert!(!contains(bytes, &utf16be("Jan")[2..]), "UTF-16 author");
|
|
assert!(!lower.contains("feff004a"), "hex UTF-16 author");
|
|
}
|
|
|
|
#[test]
|
|
fn pdf_read_lists_info_and_xmp() {
|
|
let original = build(false);
|
|
assert!(contains(&original, XMP_NS.as_bytes()), "fixture has XMP");
|
|
let r = read(&original).unwrap();
|
|
assert_eq!(r.kind, Kind::Pdf);
|
|
assert_eq!(r.find(Group::Author, "Author").unwrap().value, PDF_AUTHOR);
|
|
assert_eq!(r.find(Group::Software, "Producer").unwrap().value, "Fixture Producer 1.0");
|
|
assert_eq!(r.find(Group::Software, "Creator").unwrap().value, SOFTWARE);
|
|
assert_eq!(r.find(Group::PdfInfo, "Title").unwrap().value, "Secret plans");
|
|
assert_eq!(r.find(Group::PdfInfo, "Department").unwrap().value, "Custom department");
|
|
assert_eq!(r.find(Group::Dates, "CreationDate").unwrap().value, "2024-05-01 10:20:30 +02:00");
|
|
assert_eq!(r.find(Group::PdfXmp, "Creator (dc:creator)").unwrap().value, PDF_AUTHOR);
|
|
assert_eq!(r.find(Group::PdfXmp, "Creator tool (xmp:CreatorTool)").unwrap().value, SOFTWARE);
|
|
assert_eq!(r.find(Group::PdfXmp, "XMP on pages, images or other objects").unwrap().value, "1 objects");
|
|
assert!(r.find(Group::Other, "Private application data (PieceInfo)").is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn pdf_strip_removes_info_and_every_xmp_stream() {
|
|
let original = build(false);
|
|
let s = strip(&original).unwrap();
|
|
assert_clean(&s.bytes);
|
|
assert_eq!(first_page_content(&original), first_page_content(&s.bytes));
|
|
let doc = Document::load_mem(&s.bytes).unwrap();
|
|
let images =
|
|
doc.objects.values().filter(|o| matches!(o, Object::Stream(st) if st.dict.has_type(b"XObject"))).count();
|
|
assert_eq!(images, 1, "the image is kept");
|
|
assert!(read(&s.bytes).unwrap().items.is_empty());
|
|
for g in [Group::Author, Group::Software, Group::Dates, Group::PdfInfo, Group::PdfXmp, Group::Other] {
|
|
assert!(s.removed.contains(&g), "{g:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pdf_with_object_streams_is_stripped_too() {
|
|
let original = build(true);
|
|
let r = read(&original).unwrap();
|
|
assert_eq!(r.find(Group::Author, "Author").unwrap().value, PDF_AUTHOR);
|
|
let s = strip(&original).unwrap();
|
|
assert_clean(&s.bytes);
|
|
assert_eq!(first_page_content(&original), first_page_content(&s.bytes));
|
|
}
|
|
|
|
#[test]
|
|
fn encrypted_pdf_is_refused() {
|
|
let mut doc = Document::load_mem(&build(false)).unwrap();
|
|
let enc = doc.add_object(dictionary! {
|
|
"Filter" => "Standard", "V" => 1, "R" => 2, "Length" => 40, "P" => -4,
|
|
"O" => Object::String(vec![0x11; 32], StringFormat::Hexadecimal),
|
|
"U" => Object::String(vec![0x22; 32], StringFormat::Hexadecimal),
|
|
});
|
|
doc.trailer.set("Encrypt", enc);
|
|
doc.trailer.set(
|
|
"ID",
|
|
vec![
|
|
Object::String(vec![1; 16], StringFormat::Hexadecimal),
|
|
Object::String(vec![1; 16], StringFormat::Hexadecimal),
|
|
],
|
|
);
|
|
let mut bytes = Vec::new();
|
|
doc.save_to(&mut bytes).unwrap();
|
|
assert_eq!(read(&bytes), Err(MetaError::Encrypted));
|
|
assert_eq!(strip(&bytes), Err(MetaError::Encrypted));
|
|
assert_eq!(MetaError::Encrypted.to_string(), "This PDF is password-protected. Unlock it first.");
|
|
}
|
|
|
|
#[test]
|
|
fn unparseable_pdf_is_a_typed_error() {
|
|
for bytes in [&b"%PDF-1.7\n1 0 obj << /Type /Catalog"[..], b"%PDF-", b"%PDF-1.4\n%%EOF\n"] {
|
|
assert!(matches!(read(bytes), Err(MetaError::Damaged(_))), "{:?}", read(bytes));
|
|
assert!(matches!(strip(bytes), Err(MetaError::Damaged(_))));
|
|
}
|
|
}
|