From 9abe17415b7ef347e6037f1166961fc98bfaea35 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 8 Jul 2020 17:34:44 +0200 Subject: [PATCH] Fix PDF validation --- src/utils/pdf_utils.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/utils/pdf_utils.rs b/src/utils/pdf_utils.rs index 9b46d46..0d980dd 100644 --- a/src/utils/pdf_utils.rs +++ b/src/utils/pdf_utils.rs @@ -2,17 +2,22 @@ //! //! @author Pierre Hubert +use pdf::backend::Backend; + use crate::data::error::{ExecError, ResultBoxError}; /// Check out whether a PDF is a valid PDF or not, by trying to open it pub fn is_valid_pdf(file: &bytes::Bytes) -> ResultBoxError { - let pdf = pdf::file::File::new(Vec::from(file.as_ref())); + let backend = file.to_vec(); - match pdf.get_num_pages() { - Ok(num) if num < 1 => - Err(ExecError::boxed_string(format!("Detected a PDF with {} pages!", num))), - - Ok(_) => Ok(true), + match backend.read_xref_table_and_trailer() { + Ok((refs, _)) => { + if refs.is_empty() { + Err(ExecError::boxed_string(format!("Detected a PDF with 0 references (file size: {})!", file.len()))) + } else { + Ok(true) + } + } Err(e) => { println!("Error while parsing PDF: {}", e);