| 1 |
use lopdf::{Document, Object}; |
| 2 |
use std::env; |
| 3 |
use std::collections::HashMap; |
| 4 |
|
| 5 |
#[cfg(feature = "async")] |
| 6 |
use tokio::runtime::Builder; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
let args: Vec<String> = env::args().collect(); |
| 10 |
if args.len() < 2 { |
| 11 |
eprintln!("Usage: {} <pdf_file>", args[0]); |
| 12 |
std::process::exit(1); |
| 13 |
} |
| 14 |
|
| 15 |
let pdf_path = &args[1]; |
| 16 |
println!("Analyzing PDF: {}", pdf_path); |
| 17 |
println!("{}", "=".repeat(80)); |
| 18 |
|
| 19 |
match load_document(pdf_path) { |
| 20 |
Ok(doc) => { |
| 21 |
analyze_document(&doc); |
| 22 |
} |
| 23 |
Err(e) => { |
| 24 |
eprintln!("Failed to load PDF: {}", e); |
| 25 |
std::process::exit(1); |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
#[cfg(not(feature = "async"))] |
| 31 |
fn load_document(path: &str) -> Result<Document, lopdf::Error> { |
| 32 |
Document::load(path) |
| 33 |
} |
| 34 |
|
| 35 |
#[cfg(feature = "async")] |
| 36 |
fn load_document(path: &str) -> Result<Document, lopdf::Error> { |
| 37 |
Builder::new_current_thread() |
| 38 |
.enable_all() |
| 39 |
.build() |
| 40 |
.unwrap() |
| 41 |
.block_on(async move { |
| 42 |
Document::load(path).await |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
fn analyze_document(doc: &Document) { |
| 47 |
println!("PDF Version: {:?}", doc.version); |
| 48 |
println!("Total objects: {}", doc.objects.len()); |
| 49 |
|
| 50 |
// Find object streams |
| 51 |
let mut object_streams = Vec::new(); |
| 52 |
let mut compressed_objects = HashMap::new(); |
| 53 |
|
| 54 |
for (id, obj) in &doc.objects { |
| 55 |
if let Object::Stream(stream) = obj { |
| 56 |
if let Ok(type_obj) = stream.dict.get(b"Type") { |
| 57 |
if let Ok(type_name) = type_obj.as_name() { |
| 58 |
if type_name == b"ObjStm" { |
| 59 |
object_streams.push(id); |
| 60 |
|
| 61 |
// Get info about this object stream |
| 62 |
if let Ok(n) = stream.dict.get(b"N") { |
| 63 |
if let Ok(count) = n.as_i64() { |
| 64 |
println!("\nObject Stream {} 0 R:", id.0); |
| 65 |
println!(" Contains {} objects", count); |
| 66 |
|
| 67 |
if let Ok(first) = stream.dict.get(b"First") { |
| 68 |
if let Ok(first_offset) = first.as_i64() { |
| 69 |
println!(" First object at offset: {}", first_offset); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Try to parse the stream content |
| 74 |
match stream.decompressed_content() { |
| 75 |
Ok(decompressed) => { |
| 76 |
println!(" Decompressed size: {} bytes", decompressed.len()); |
| 77 |
|
| 78 |
// Parse the offset table |
| 79 |
if let Ok(first_offset) = stream.dict.get(b"First").and_then(|o| o.as_i64()) { |
| 80 |
if first_offset as usize <= decompressed.len() { |
| 81 |
let offset_table = &decompressed[..first_offset as usize]; |
| 82 |
if let Ok(offset_str) = std::str::from_utf8(offset_table) { |
| 83 |
let numbers: Vec<_> = offset_str.split_whitespace().collect(); |
| 84 |
println!(" Objects in stream:"); |
| 85 |
for chunk in numbers.chunks(2) { |
| 86 |
if chunk.len() == 2 { |
| 87 |
println!(" - Object {}: offset {}", chunk[0], chunk[1]); |
| 88 |
if let Ok(obj_num) = chunk[0].parse::<u32>() { |
| 89 |
compressed_objects.insert(obj_num, id.0); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} else { |
| 94 |
println!(" ERROR: Could not parse offset table as UTF-8"); |
| 95 |
println!(" First 100 bytes: {:?}", &offset_table[..offset_table.len().min(100)]); |
| 96 |
} |
| 97 |
} else { |
| 98 |
println!(" ERROR: First offset {} exceeds decompressed size {}", first_offset, decompressed.len()); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
Err(e) => { |
| 103 |
println!(" ERROR: Could not decompress stream: {}", e); |
| 104 |
println!(" Stream is compressed: {}", stream.allows_compression); |
| 105 |
if let Ok(filter) = stream.dict.get(b"Filter") { |
| 106 |
println!(" Filter: {:?}", filter); |
| 107 |
} else { |
| 108 |
println!(" No Filter found in dictionary!"); |
| 109 |
println!(" Dictionary: {:?}", stream.dict); |
| 110 |
} |
| 111 |
println!(" Raw content size: {} bytes", stream.content.len()); |
| 112 |
println!(" First 20 bytes of raw content: {:?}", &stream.content[..stream.content.len().min(20)]); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
println!("\nTotal object streams: {}", object_streams.len()); |
| 124 |
println!("Total compressed objects: {}", compressed_objects.len()); |
| 125 |
|
| 126 |
// Check pages |
| 127 |
println!("\n{}", "=".repeat(80)); |
| 128 |
println!("Page Analysis:"); |
| 129 |
|
| 130 |
let pages = doc.get_pages(); |
| 131 |
println!("Total pages: {}", pages.len()); |
| 132 |
|
| 133 |
for (page_num, &page_id) in pages.iter() { |
| 134 |
println!("\nPage {}:", page_num); |
| 135 |
match doc.get_object(page_id) { |
| 136 |
Ok(page_obj) => { |
| 137 |
if let Object::Dictionary(page_dict) = page_obj { |
| 138 |
// Check if page is in object stream |
| 139 |
if compressed_objects.contains_key(&page_id.0) { |
| 140 |
println!(" ⚠️ Page object is compressed in object stream {}!", compressed_objects[&page_id.0]); |
| 141 |
} |
| 142 |
|
| 143 |
// Check page contents |
| 144 |
if let Ok(contents) = page_dict.get(b"Contents") { |
| 145 |
match contents { |
| 146 |
Object::Reference(ref_id) => { |
| 147 |
println!(" Contents: {} {} R", ref_id.0, ref_id.1); |
| 148 |
if compressed_objects.contains_key(&ref_id.0) { |
| 149 |
println!(" ⚠️ Contents is compressed in object stream {}!", compressed_objects[&ref_id.0]); |
| 150 |
} |
| 151 |
} |
| 152 |
Object::Array(refs) => { |
| 153 |
println!(" Contents array with {} elements", refs.len()); |
| 154 |
for (i, content_ref) in refs.iter().enumerate() { |
| 155 |
if let Object::Reference(ref_id) = content_ref { |
| 156 |
if compressed_objects.contains_key(&ref_id.0) { |
| 157 |
println!(" ⚠️ Content[{}] {} {} R is compressed!", i, ref_id.0, ref_id.1); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
_ => println!(" Unexpected Contents type: {:?}", contents), |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Check resources |
| 167 |
if let Ok(resources) = page_dict.get(b"Resources") { |
| 168 |
if let Object::Reference(ref_id) = resources { |
| 169 |
if compressed_objects.contains_key(&ref_id.0) { |
| 170 |
println!(" ⚠️ Resources {} {} R is compressed!", ref_id.0, ref_id.1); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
Err(e) => { |
| 177 |
println!(" Error getting page object: {}", e); |
| 178 |
if compressed_objects.contains_key(&page_id.0) { |
| 179 |
println!(" Note: Page is in object stream {}", compressed_objects[&page_id.0]); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// Check cross-reference |
| 186 |
println!("\n{}", "=".repeat(80)); |
| 187 |
println!("Cross-reference Analysis:"); |
| 188 |
println!("Cross-reference type: {:?}", doc.reference_table.cross_reference_type); |
| 189 |
|
| 190 |
// Check if any critical objects are compressed |
| 191 |
println!("\n{}", "=".repeat(80)); |
| 192 |
println!("Critical Objects Check:"); |
| 193 |
|
| 194 |
if let Ok(root) = doc.trailer.get(b"Root") { |
| 195 |
if let Object::Reference(root_id) = root { |
| 196 |
if compressed_objects.contains_key(&root_id.0) { |
| 197 |
println!("⚠️ WARNING: Catalog (Root) object {} is compressed!", root_id.0); |
| 198 |
} else { |
| 199 |
println!("✓ Catalog (Root) object {} is not compressed", root_id.0); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |