mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-03 15:31:40 -05:00
20830d092c
* refactor(render): route PDF reading and rendering through shared craft-pdf (storytold/craft-libs#778) * fix(render): convert u32 raster dimensions to usize for Pixels::zeroed * fix(render): remove unused hayro_syntax imports in inspect * fix(render): let shared raster dimensions reject rounded over-budget sizes before allocation * chore(pdf): repin craft-libs to #2497 and align tests with shared contract * fix(render): align remaining 8 tests and OutputLimit error with craft-libs contract * docs(attribution): update ATTRIBUTION.md for shared hayro* dependencies * docs(attribution): include adobe_data suffix for hayro-cmap in ATTRIBUTION.md * docs(attribution): sync ATTRIBUTION.md with cargo xtask assets --write * docs(licenses): update NOTICE hash and hayro-interpret asset licences in LICENSES.toml --------- Co-authored-by: Dylan M. Taylor <dylan@dylanmtaylor.com>
42 lines
1.7 KiB
Rust
42 lines
1.7 KiB
Rust
//! Viewer parser policy and error mapping; independent from a generic importer's defaults.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use craft_pdf::syntax::{DecryptionError, LoadPdfError, Pdf};
|
|
|
|
use crate::OpenError;
|
|
|
|
fn open_options() -> craft_pdf::OpenOptions {
|
|
// The existing viewer accepts an already-loaded input without a fixed byte cap.
|
|
// File/link entry points keep their own policies; stream decoding remains bounded.
|
|
craft_pdf::OpenOptions { max_input_bytes: usize::MAX, ..Default::default() }
|
|
}
|
|
|
|
pub(crate) fn open(bytes: Arc<Vec<u8>>, password: Option<&str>) -> Result<Pdf, OpenError> {
|
|
craft_pdf::open_with_options(bytes, password, &open_options()).map_err(|error| match error {
|
|
craft_pdf::OpenError::Load(LoadPdfError::Decryption(DecryptionError::PasswordProtected)) => {
|
|
if password.is_some() {
|
|
OpenError::WrongPassword
|
|
} else {
|
|
OpenError::NeedsPassword
|
|
}
|
|
}
|
|
craft_pdf::OpenError::Load(LoadPdfError::Decryption(other)) => OpenError::Unsupported(format!("encryption not supported: {other:?}")),
|
|
craft_pdf::OpenError::Load(LoadPdfError::Invalid) => OpenError::Invalid("no readable page tree or cross-reference data was found".into()),
|
|
craft_pdf::OpenError::BackendPanic => OpenError::Invalid("the parser crashed".into()),
|
|
other => OpenError::Invalid(other.to_string()),
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn viewer_policy_does_not_inherit_the_small_importer_input_limit() {
|
|
let size = (1usize << 30) + 1;
|
|
assert!(craft_pdf::check_input_size(size).is_err());
|
|
assert!(craft_pdf::check_input_size_with(size, &open_options()).is_ok());
|
|
}
|
|
}
|