Skip to content

Commit

Permalink
Attempt decryption with empty password
Browse files Browse the repository at this point in the history
  • Loading branch information
prscoelho authored and jrmuizel committed Apr 16, 2024
1 parent 2e1bc22 commit b9a1a77
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use adobe_cmap_parser::{ByteMapping, CodeRange, CIDRange};
use lopdf::content::Content;
use lopdf::*;
use euclid::*;
use lopdf::encryption::DecryptionError;
use std::fmt::{Debug, Formatter};
extern crate encoding;
extern crate euclid;
Expand Down Expand Up @@ -2123,10 +2124,27 @@ pub fn extract_text<P: std::convert::AsRef<std::path::Path>>(path: P) -> Result<
let mut s = String::new();
{
let mut output = PlainTextOutput::new(&mut s);
let doc = Document::load(path)?;
let mut doc = Document::load(path)?;
maybe_decrypt(&mut doc)?;
output_doc(&doc, &mut output)?;
}
return Ok(s);
Ok(s)
}

fn maybe_decrypt(doc: &mut Document) -> Result<(), OutputError> {
if ! doc.is_encrypted() {
return Ok(());
}

if let Err(e) = doc.decrypt("") {
if let Error::Decryption(DecryptionError::IncorrectPassword) = e {
eprintln!("Encrypted documents must be decrypted with a password using {{extract_text|extract_text_from_mem|output_doc}}_encrypted")
}

return Err(OutputError::PdfError(e));
}

Ok(())
}

pub fn extract_text_encrypted<P: std::convert::AsRef<std::path::Path>, PW: AsRef<[u8]>>(
Expand All @@ -2146,10 +2164,11 @@ pub fn extract_text_from_mem(buffer: &[u8]) -> Result<String, OutputError> {
let mut s = String::new();
{
let mut output = PlainTextOutput::new(&mut s);
let doc = Document::load_mem(buffer)?;
let mut doc = Document::load_mem(buffer)?;
maybe_decrypt(&mut doc)?;
output_doc(&doc, &mut output)?;
}
return Ok(s);
Ok(s)
}

pub fn extract_text_from_mem_encrypted<PW: AsRef<[u8]>>(
Expand Down Expand Up @@ -2189,7 +2208,7 @@ pub fn output_doc_encrypted<PW: AsRef<[u8]>>(
/// Parse a given document and output it to `output`
pub fn output_doc(doc: &Document, output: &mut dyn OutputDev) -> Result<(), OutputError> {
if doc.is_encrypted() {
eprintln!("Encrypted documents must be decrypted with {{extract_text|extract_text_from_mem|output_doc}}_encrypted")
eprintln!("Encrypted documents must be decrypted with a password using {{extract_text|extract_text_from_mem|output_doc}}_encrypted");
}
let empty_resources = &Dictionary::new();

Expand Down

0 comments on commit b9a1a77

Please sign in to comment.