imprimatur 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/Cargo.lock +473 -5
  4. data/Cargo.toml +17 -1
  5. data/README.md +63 -7
  6. data/ext/imprimatur_native/cli/Cargo.toml +1 -1
  7. data/ext/imprimatur_native/cli/src/app.rs +3 -1
  8. data/ext/imprimatur_native/core/Cargo.toml +30 -13
  9. data/ext/imprimatur_native/core/README.md +18 -1
  10. data/ext/imprimatur_native/core/src/bin/imprimatur-render-worker.rs +10 -0
  11. data/ext/imprimatur_native/core/src/bytes.rs +16 -0
  12. data/ext/imprimatur_native/core/src/doc/picture.rs +3 -7
  13. data/ext/imprimatur_native/core/src/doc/size.rs +14 -0
  14. data/ext/imprimatur_native/core/src/doc/symbol.rs +3 -5
  15. data/ext/imprimatur_native/core/src/doc.rs +24 -0
  16. data/ext/imprimatur_native/core/src/docx.rs +6 -5
  17. data/ext/imprimatur_native/core/src/error.rs +6 -0
  18. data/ext/imprimatur_native/core/src/font.rs +12 -0
  19. data/ext/imprimatur_native/core/src/lib.rs +6 -0
  20. data/ext/imprimatur_native/core/src/pdf/layout.rs +13 -5
  21. data/ext/imprimatur_native/core/src/pdf/objects.rs +1 -1
  22. data/ext/imprimatur_native/core/src/pdf/truetype/descriptor.rs +1 -0
  23. data/ext/imprimatur_native/core/src/read/budget.rs +10 -5
  24. data/ext/imprimatur_native/core/src/read/content.rs +16 -0
  25. data/ext/imprimatur_native/core/src/read/image/dimensions.rs +2 -2
  26. data/ext/imprimatur_native/core/src/read/ole.rs +1 -8
  27. data/ext/imprimatur_native/core/src/read/pdf/bounded.rs +249 -0
  28. data/ext/imprimatur_native/core/src/read/pdf/content/state.rs +50 -0
  29. data/ext/imprimatur_native/core/src/read/pdf/content.rs +43 -54
  30. data/ext/imprimatur_native/core/src/read/pdf/document/pages.rs +140 -0
  31. data/ext/imprimatur_native/core/src/read/pdf/document.rs +20 -110
  32. data/ext/imprimatur_native/core/src/read/pdf/filter.rs +25 -13
  33. data/ext/imprimatur_native/core/src/read/pdf/font.rs +4 -3
  34. data/ext/imprimatur_native/core/src/read/pdf/isolation/protocol.rs +224 -0
  35. data/ext/imprimatur_native/core/src/read/pdf/isolation/stream.rs +113 -0
  36. data/ext/imprimatur_native/core/src/read/pdf/isolation/system.rs +98 -0
  37. data/ext/imprimatur_native/core/src/read/pdf/isolation/worker.rs +74 -0
  38. data/ext/imprimatur_native/core/src/read/pdf/isolation.rs +255 -0
  39. data/ext/imprimatur_native/core/src/read/pdf/render.rs +206 -0
  40. data/ext/imprimatur_native/core/src/read/pdf.rs +16 -8
  41. data/ext/imprimatur_native/core/src/read/xls/strings.rs +2 -1
  42. data/ext/imprimatur_native/core/src/read/xls.rs +1 -8
  43. data/ext/imprimatur_native/core/src/read.rs +6 -5
  44. data/ext/imprimatur_native/core/src/test_support/font.rs +37 -0
  45. data/ext/imprimatur_native/core/src/xlsx/validation.rs +28 -18
  46. data/ext/imprimatur_native/core/src/zip/reader.rs +1 -8
  47. data/ext/imprimatur_native/extconf.rb +18 -0
  48. data/ext/imprimatur_native/ffi/Cargo.toml +2 -2
  49. data/ext/imprimatur_native/ffi/src/document.rs +6 -0
  50. data/ext/imprimatur_native/ffi/src/lib.rs +7 -0
  51. data/ext/imprimatur_native/ffi/src/pdf.rs +88 -0
  52. data/ext/imprimatur_native/ffi/src/read.rs +18 -12
  53. data/lib/imprimatur/document.rb +4 -2
  54. data/lib/imprimatur/native.rb +7 -0
  55. data/lib/imprimatur/pdf_reader.rb +29 -0
  56. data/lib/imprimatur/version.rb +1 -1
  57. data/lib/imprimatur.rb +26 -2
  58. metadata +20 -4
@@ -0,0 +1,98 @@
1
+ //! OS resource enforcement. macOS uses the supervisor's RSS watchdog.
2
+
3
+ #[cfg(test)]
4
+ mod tests;
5
+
6
+ use std::process::Child;
7
+
8
+ use nix::errno::Errno;
9
+ use nix::sys::resource::{Resource, getrlimit, rlim_t, setrlimit};
10
+ use nix::sys::signal::{Signal, killpg};
11
+ use nix::unistd::Pid;
12
+
13
+ use crate::{Error, Result};
14
+
15
+ pub(super) fn apply_limits(memory: u64, cpu: u64, output: u64) -> Result<()> {
16
+ if memory == 0 {
17
+ return Err(Error::InvalidPdfOption("memory limit must be positive"));
18
+ }
19
+ #[cfg(target_os = "linux")]
20
+ lower(Resource::RLIMIT_AS, memory)?;
21
+ #[cfg(target_os = "freebsd")]
22
+ lower(Resource::RLIMIT_VMEM, memory)?;
23
+ #[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "macos")))]
24
+ return Err(Error::UnsupportedDocument("process limits are unsupported on this OS".into()));
25
+ lower(Resource::RLIMIT_CORE, 0)?;
26
+ lower(Resource::RLIMIT_CPU, cpu)?;
27
+ lower(Resource::RLIMIT_FSIZE, output)?;
28
+ Ok(())
29
+ }
30
+
31
+ /// Replace the bootstrap after setting address-space limits, so the loader
32
+ /// and parser cannot reuse memory mapped before the cap was installed.
33
+ #[cfg(any(target_os = "linux", target_os = "freebsd"))]
34
+ pub(super) fn reexec(arguments: &[std::ffi::OsString]) -> Result<()> {
35
+ use std::ffi::CString;
36
+ use std::os::unix::ffi::OsStrExt;
37
+
38
+ let string = |value: &[u8]| {
39
+ CString::new(value)
40
+ .map_err(|error| std::io::Error::new(std::io::ErrorKind::InvalidInput, error))
41
+ };
42
+ let executable = string(std::env::current_exe()?.as_os_str().as_bytes())?;
43
+ let mut args = vec![executable.clone(), string(b"1")?];
44
+ for argument in arguments.iter().skip(1) {
45
+ args.push(string(argument.as_os_str().as_bytes())?);
46
+ }
47
+ match nix::unistd::execv(&executable, &args) {
48
+ Ok(never) => match never {},
49
+ // A tiny address-space cap can prevent exec from allocating its
50
+ // argument/environment pages and surface as E2BIG rather than ENOMEM.
51
+ Err(Errno::ENOMEM | Errno::E2BIG) => {
52
+ Err(Error::LimitExceeded("PDF worker startup resources"))
53
+ }
54
+ Err(error) => Err(std::io::Error::from(error).into()),
55
+ }
56
+ }
57
+
58
+ fn lower(resource: Resource, value: u64) -> Result<()> {
59
+ let (soft, hard) = getrlimit(resource).map_err(std::io::Error::from)?;
60
+ let value = bounded_limit(value, soft, hard)?;
61
+ setrlimit(resource, value, value).map_err(std::io::Error::from)?;
62
+ Ok(())
63
+ }
64
+
65
+ fn bounded_limit(value: u64, soft: rlim_t, hard: rlim_t) -> Result<rlim_t> {
66
+ let value = i128::from(value).min(i128::from(soft)).min(i128::from(hard));
67
+ value.try_into().map_err(|_| Error::InvalidPdfOption("resource limit is out of range"))
68
+ }
69
+
70
+ #[cfg(target_os = "macos")]
71
+ pub(super) fn check_memory(pid: u32, maximum: u64) -> Result<()> {
72
+ let pid = i32::try_from(pid).map_err(|_| Error::InvalidPdfOption("invalid worker PID"))?;
73
+ let usage = libproc::proc_pid::pidinfo::<libproc::task_info::TaskInfo>(pid, 0)
74
+ .map_err(|error| std::io::Error::other(format!("cannot monitor render worker: {error}")))?;
75
+ if usage.pti_resident_size > maximum {
76
+ return Err(Error::LimitExceeded("render worker memory"));
77
+ }
78
+ Ok(())
79
+ }
80
+
81
+ pub(super) fn terminate(child: &mut Child) {
82
+ if let Ok(pid) = i32::try_from(child.id())
83
+ && let Err(error) = killpg(Pid::from_raw(pid), Signal::SIGKILL)
84
+ && error != Errno::ESRCH
85
+ {
86
+ eprintln!("imprimatur: could not kill render worker group: {error}");
87
+ }
88
+ // Also handle a child that failed before joining its new process group.
89
+ if let Err(error) = child.kill()
90
+ && error.kind() != std::io::ErrorKind::InvalidInput
91
+ && error.raw_os_error() != Some(Errno::ESRCH as i32)
92
+ {
93
+ eprintln!("imprimatur: could not kill render worker: {error}");
94
+ }
95
+ if let Err(error) = child.wait() {
96
+ eprintln!("imprimatur: could not reap render worker: {error}");
97
+ }
98
+ }
@@ -0,0 +1,74 @@
1
+ //! Apply resource limits before parsing any document bytes.
2
+
3
+ use std::io::{Read, Write};
4
+ use std::process::ExitCode;
5
+
6
+ use super::protocol::{Operation, Request};
7
+ use super::{protocol, stream, system};
8
+ use crate::read::pdf::{self, bounded};
9
+ use crate::{Error, Result};
10
+
11
+ /// Internal entry point for the bundled PDF worker executable.
12
+ #[doc(hidden)]
13
+ #[must_use]
14
+ pub fn worker_main() -> ExitCode {
15
+ let mut out = std::io::stdout().lock();
16
+ let result = Request::parse().and_then(|request| run(&request, &mut out));
17
+ match result.or_else(|error| protocol::write_reply(&mut out, Err(error))) {
18
+ Ok(()) => ExitCode::SUCCESS,
19
+ Err(error) => {
20
+ eprintln!("imprimatur PDF worker: {error}");
21
+ ExitCode::FAILURE
22
+ }
23
+ }
24
+ }
25
+
26
+ fn run(request: &Request, out: &mut impl Write) -> Result<()> {
27
+ let output = u64::try_from(request.operation.maximum()?)
28
+ .map_err(|_| Error::InvalidPdfOption("worker output limit is too large"))?;
29
+ system::apply_limits(request.memory, request.cpu_seconds, output)?;
30
+ #[cfg(any(target_os = "linux", target_os = "freebsd"))]
31
+ if !request.restricted {
32
+ system::reexec(&request.arguments())?;
33
+ }
34
+ let maximum = u64::try_from(request.limits.max_input)
35
+ .ok()
36
+ .and_then(|value| value.checked_add(1))
37
+ .ok_or(Error::InvalidPdfOption("input limit is too large"))?;
38
+ let mut bytes = Vec::new();
39
+ std::io::stdin().lock().take(maximum).read_to_end(&mut bytes)?;
40
+ if bytes.len() > request.limits.max_input {
41
+ return Err(Error::LimitExceeded("input size"));
42
+ }
43
+ match &request.operation {
44
+ Operation::Info => protocol::write_reply(
45
+ out,
46
+ pdf::info(&bytes, &request.limits).and_then(|info| {
47
+ serde_json::to_vec(&info)
48
+ .map_err(|error| Error::malformed("PDF worker", error.to_string()))
49
+ }),
50
+ ),
51
+ Operation::Render(number, options) => protocol::write_reply(
52
+ out,
53
+ pdf::render_page(&bytes, *number, options, &request.limits).map(|image| image.data),
54
+ ),
55
+ Operation::Pages(options) => {
56
+ out.write_all(protocol::HEADER)?;
57
+ let result = pdf::info(&bytes, &request.limits).and_then(|info| {
58
+ stream::write(out, &stream::Frame::Info(info))?;
59
+ bounded::visit_pages(&bytes, options, &request.limits, true, |page| {
60
+ stream::write(out, &stream::Frame::Page(page))
61
+ })?;
62
+ Ok(())
63
+ });
64
+ let frame = match result {
65
+ Ok(()) => stream::Frame::End,
66
+ Err(error) => {
67
+ let (kind, message) = protocol::failure(&error);
68
+ stream::Frame::Error { kind, message }
69
+ }
70
+ };
71
+ stream::write(out, &frame)
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,255 @@
1
+ //! Read and render in a resource-limited process. No untrusted PDF parsing
2
+ //! occurs in the supervisor.
3
+
4
+ mod protocol;
5
+ mod stream;
6
+ mod system;
7
+ #[cfg(test)]
8
+ mod tests;
9
+ mod worker;
10
+
11
+ use std::fs::{self, File};
12
+ use std::io::{Read, Write};
13
+ use std::os::unix::process::CommandExt;
14
+ use std::path::Path;
15
+ use std::process::{Child, Command, ExitStatus, Stdio};
16
+ use std::time::{Duration, Instant};
17
+
18
+ use super::{Info, Pages, ReadOptions, RenderOptions};
19
+ use crate::read::{Budget, Image, Limits};
20
+ use crate::{Error, Result};
21
+ pub use worker::worker_main;
22
+
23
+ /// Inspects a PDF in the trusted, resource-limited worker.
24
+ ///
25
+ /// # Errors
26
+ /// Invalid options, worker errors, or exceeded input, memory or time limits.
27
+ pub fn info_isolated(
28
+ worker: &Path,
29
+ data: &[u8],
30
+ limits: &Limits,
31
+ process: &ProcessLimits,
32
+ ) -> Result<Info> {
33
+ let output = invoke(worker, data, protocol::Operation::Info, limits, process)?;
34
+ let bytes = output.complete(protocol::MAX_ERROR_BYTES)?;
35
+ serde_json::from_slice(&bytes)
36
+ .map_err(|error| Error::malformed("PDF worker", error.to_string()))
37
+ }
38
+
39
+ /// Reads PDF pages in the worker, retaining completed records on resource failure.
40
+ ///
41
+ /// Page-local stream limits trigger bounded individual recovery.
42
+ /// All attempts share one external wall-clock and process-memory limit.
43
+ ///
44
+ /// # Errors
45
+ /// As [`info_isolated`], and cancellation or an invalid worker reply.
46
+ /// A failure before page metadata is available remains a document error.
47
+ pub fn pages_isolated(
48
+ worker: &Path,
49
+ data: &[u8],
50
+ options: &ReadOptions,
51
+ limits: &Limits,
52
+ process: &ProcessLimits,
53
+ ) -> Result<Pages> {
54
+ let output =
55
+ invoke(worker, data, protocol::Operation::Pages(options.clone()), limits, process)?;
56
+ stream::decode(output, options)
57
+ }
58
+
59
+ /// Mandatory process limits, in addition to the reader/render limits.
60
+ #[derive(Debug, Clone)]
61
+ pub struct ProcessLimits {
62
+ /// Address-space cap on Linux/FreeBSD; RSS watchdog threshold on macOS.
63
+ pub memory_bytes: u64,
64
+ /// A finite upper bound even when the reader's timeout is disabled.
65
+ pub wall_time: Duration,
66
+ }
67
+
68
+ impl Default for ProcessLimits {
69
+ fn default() -> Self {
70
+ Self { memory_bytes: 1024 * 1024 * 1024, wall_time: Duration::from_mins(1) }
71
+ }
72
+ }
73
+
74
+ /// Renders through the explicitly selected, trusted worker executable.
75
+ ///
76
+ /// The child is killed and reaped on timeout, cancellation, resource failure
77
+ /// or supervisor errors. Missing workers never fall back to in-process work.
78
+ /// macOS uses a sampled RSS watchdog and may briefly overshoot its threshold.
79
+ ///
80
+ /// # Errors
81
+ /// Invalid limits, failed worker startup/protocol, render errors, or exceeded
82
+ /// input/output, process memory or wall-clock limits.
83
+ pub fn render_page_isolated(
84
+ worker: &Path,
85
+ data: &[u8],
86
+ number: u32,
87
+ options: &RenderOptions,
88
+ limits: &Limits,
89
+ process: &ProcessLimits,
90
+ ) -> Result<Image> {
91
+ let output = invoke(
92
+ worker,
93
+ data,
94
+ protocol::Operation::Render(number, options.clone()),
95
+ limits,
96
+ process,
97
+ )?;
98
+ let data = output.complete(options.max_output_bytes)?;
99
+ // The trusted worker writes only PNG; reject malformed/empty replies too.
100
+ if !data.starts_with(b"\x89PNG\r\n\x1a\n") || data.len() < 26 {
101
+ return Err(Error::malformed("PDF worker", "invalid PNG response"));
102
+ }
103
+ let dimension = |at| u32::from_be_bytes(data[at..at + 4].try_into().unwrap_or([0; 4]));
104
+ let (width, height) = (dimension(16), dimension(20));
105
+ options.image(data, number, (width, height))
106
+ }
107
+
108
+ struct Output {
109
+ data: Vec<u8>,
110
+ failure: Option<Error>,
111
+ }
112
+
113
+ impl Output {
114
+ fn complete(self, maximum: usize) -> Result<Vec<u8>> {
115
+ if let Some(error) = self.failure {
116
+ return Err(error);
117
+ }
118
+ protocol::decode(self.data, maximum)
119
+ }
120
+ }
121
+
122
+ fn invoke(
123
+ worker: &Path,
124
+ data: &[u8],
125
+ operation: protocol::Operation,
126
+ limits: &Limits,
127
+ process: &ProcessLimits,
128
+ ) -> Result<Output> {
129
+ let budget = Budget::new(limits, data.len())?;
130
+ budget.check()?;
131
+ let deadline = Instant::now()
132
+ .checked_add(process.wall_time)
133
+ .ok_or(Error::InvalidPdfOption("worker wall time is too large"))?;
134
+ if !worker.is_absolute() || !worker.is_file() {
135
+ return Err(Error::InvalidPdfOption("an absolute path to the render worker is required"));
136
+ }
137
+ if process.memory_bytes == 0 || process.wall_time.is_zero() {
138
+ return Err(Error::InvalidPdfOption("process limits must be positive"));
139
+ }
140
+ let request = protocol::Request::new(operation, limits, process)?;
141
+ let maximum = u64::try_from(request.operation.maximum()?)
142
+ .map_err(|_| Error::InvalidPdfOption("worker output limit is too large"))?;
143
+ let directory = tempfile::tempdir()?;
144
+ let input_path = directory.path().join("input.pdf");
145
+ File::create(&input_path)?.write_all(data)?;
146
+ let output_path = directory.path().join("output");
147
+ let error_path = directory.path().join("stderr");
148
+ check(&budget, deadline)?;
149
+ let child = Command::new(worker)
150
+ .args(request.arguments())
151
+ .stdin(Stdio::from(File::open(&input_path)?))
152
+ .stdout(Stdio::from(File::create(&output_path)?))
153
+ .stderr(Stdio::from(File::create(&error_path)?))
154
+ .process_group(0)
155
+ .spawn()?;
156
+ let mut child = Running { child, finished: false };
157
+ let failure = match supervise(&mut child, &budget, deadline, process.memory_bytes) {
158
+ Ok(status) if status.success() => check_finished(&budget, deadline).err(),
159
+ Ok(_) => Some(Error::LimitExceeded("PDF worker resources")),
160
+ Err(Error::Cancelled) => return Err(Error::Cancelled),
161
+ Err(error) => Some(error),
162
+ };
163
+ // Stop and reap an unfinished worker before inspecting its flushed output.
164
+ drop(child);
165
+ if fs::metadata(&output_path)?.len() > maximum {
166
+ return Err(Error::LimitExceeded("PDF worker output"));
167
+ }
168
+ let mut output = Vec::new();
169
+ File::open(output_path)?.take(maximum).read_to_end(&mut output)?;
170
+ Ok(Output { data: output, failure })
171
+ }
172
+
173
+ fn check(budget: &Budget<'_>, deadline: Instant) -> Result<()> {
174
+ budget.check()?;
175
+ check_finished(budget, deadline)
176
+ }
177
+
178
+ fn check_finished(budget: &Budget<'_>, deadline: Instant) -> Result<()> {
179
+ budget.check_time()?;
180
+ if Instant::now() >= deadline {
181
+ return Err(Error::LimitExceeded("render worker wall time"));
182
+ }
183
+ Ok(())
184
+ }
185
+
186
+ struct Running {
187
+ child: Child,
188
+ finished: bool,
189
+ }
190
+
191
+ impl Running {
192
+ fn poll(&mut self) -> Result<Option<ExitStatus>> {
193
+ let status = self.child.try_wait()?;
194
+ self.finished = status.is_some();
195
+ Ok(status)
196
+ }
197
+ }
198
+
199
+ impl Drop for Running {
200
+ fn drop(&mut self) {
201
+ if !self.finished {
202
+ system::terminate(&mut self.child);
203
+ }
204
+ }
205
+ }
206
+
207
+ fn supervise(
208
+ child: &mut Running,
209
+ budget: &Budget<'_>,
210
+ deadline: Instant,
211
+ memory: u64,
212
+ ) -> Result<ExitStatus> {
213
+ #[cfg(not(target_os = "macos"))]
214
+ let _ = memory;
215
+
216
+ loop {
217
+ // SIGCHLD can invoke Ruby's unblock callback. Prefer a completed
218
+ // worker; the binding checks pending Ruby exceptions before returning.
219
+ if let Some(status) = child.poll()? {
220
+ return Ok(status);
221
+ }
222
+ check(budget, deadline)?;
223
+ #[cfg(target_os = "macos")]
224
+ if let Err(error) = system::check_memory(child.child.id(), memory) {
225
+ if matches!(error, Error::Io(_)) {
226
+ return finish_monitor_error(child, budget, deadline, error);
227
+ }
228
+ return Err(error);
229
+ }
230
+ std::thread::sleep(Duration::from_millis(5));
231
+ }
232
+ }
233
+
234
+ #[cfg(target_os = "macos")]
235
+ fn finish_monitor_error(
236
+ child: &mut Running,
237
+ budget: &Budget<'_>,
238
+ deadline: Instant,
239
+ error: Error,
240
+ ) -> Result<ExitStatus> {
241
+ // proc_pidinfo can stop seeing an exiting task before waitpid can reap it.
242
+ // Never bypass monitoring for a live worker: allow only a bounded exit
243
+ // grace, still respecting cancellation and the original overall deadline.
244
+ let grace = Instant::now() + Duration::from_millis(50);
245
+ loop {
246
+ if let Some(status) = child.poll()? {
247
+ return Ok(status);
248
+ }
249
+ check(budget, deadline)?;
250
+ if Instant::now() >= grace {
251
+ return Err(error);
252
+ }
253
+ std::thread::sleep(Duration::from_millis(5));
254
+ }
255
+ }
@@ -0,0 +1,206 @@
1
+ //! Full-page rasterization. Optional, pure Rust, and independent of text extraction.
2
+
3
+ #[cfg(test)]
4
+ mod tests;
5
+
6
+ use std::panic::{AssertUnwindSafe, catch_unwind};
7
+ use std::sync::Arc;
8
+ use std::sync::atomic::{AtomicBool, Ordering};
9
+
10
+ use hayro::hayro_interpret::InterpreterSettings;
11
+ use hayro::hayro_syntax::{LoadPdfError, Pdf};
12
+ use hayro::vello_cpu::color::palette::css::WHITE;
13
+ use hayro::{PixmapSettings, RenderCache, RenderSettings};
14
+
15
+ use super::bounded;
16
+ use super::content::{Drawing, page_images};
17
+ use crate::read::image::{Image, ImageFormat, ImageLocation, PngColor, png};
18
+ use crate::read::{Budget, Limits};
19
+ use crate::{Error, Result};
20
+
21
+ #[derive(Debug, Clone)]
22
+ pub struct RenderOptions {
23
+ /// Longest output side in pixels; 1568 by default.
24
+ pub max_side: u16,
25
+ /// Maximum output surface, before allocating it.
26
+ pub max_pixels: u64,
27
+ pub max_output_bytes: usize,
28
+ pub grayscale: bool,
29
+ }
30
+
31
+ impl Default for RenderOptions {
32
+ fn default() -> Self {
33
+ Self {
34
+ max_side: 1568,
35
+ max_pixels: 4_000_000,
36
+ max_output_bytes: 8 * 1024 * 1024,
37
+ grayscale: true,
38
+ }
39
+ }
40
+ }
41
+
42
+ impl RenderOptions {
43
+ pub(super) fn check_pixels(&self, width: u32, height: u32) -> Result<()> {
44
+ if width == 0 || height == 0 || u64::from(width) * u64::from(height) > self.max_pixels {
45
+ return Err(Error::LimitExceeded("rendered pixels"));
46
+ }
47
+ Ok(())
48
+ }
49
+
50
+ pub(super) fn image(&self, data: Vec<u8>, number: u32, size: (u32, u32)) -> Result<Image> {
51
+ self.check_pixels(size.0, size.1)?;
52
+ if data.len() > self.max_output_bytes {
53
+ return Err(Error::LimitExceeded("rendered bytes"));
54
+ }
55
+ Ok(Image {
56
+ data,
57
+ format: ImageFormat::Png,
58
+ width: Some(size.0),
59
+ height: Some(size.1),
60
+ location: ImageLocation::Page { number, bounds: None },
61
+ })
62
+ }
63
+ }
64
+
65
+ /// A one-based page, including native text and graphics, as an opaque PNG.
66
+ ///
67
+ /// Output dimensions and encoded bytes are bounded. Hayro has no cancellation
68
+ /// hook: time/cancellation are checked before and after rasterization, not
69
+ /// during it. Use `isolation::render_page_isolated` for untrusted renders.
70
+ /// `max_inflated` bounds our preflight, not Hayro's internal allocations.
71
+ ///
72
+ /// # Errors
73
+ /// As [`super::info`], invalid options/page number, an oversized surface or
74
+ /// output, and renderer warnings (rather than silently missing content).
75
+ pub fn render_page(
76
+ data: &[u8],
77
+ number: u32,
78
+ options: &RenderOptions,
79
+ limits: &Limits,
80
+ ) -> Result<Image> {
81
+ if number == 0
82
+ || options.max_side == 0
83
+ || options.max_pixels == 0
84
+ || options.max_output_bytes == 0
85
+ {
86
+ return Err(Error::InvalidPdfOption("page number and rendering limits must be positive"));
87
+ }
88
+ let budget = Budget::new(limits, data.len())?;
89
+ let inspected = bounded::info(data, limits)?;
90
+ if number > inspected.page_count {
91
+ return Err(Error::InvalidPdfOption("page number is past the end of the document"));
92
+ }
93
+ budget.check()?;
94
+ preflight(data, number, &budget)?;
95
+ catch_unwind(AssertUnwindSafe(|| render(data, number, options, &budget)))
96
+ .unwrap_or_else(|_| Err(Error::malformed("PDF", "the renderer failed on this page")))
97
+ }
98
+
99
+ fn preflight(data: &[u8], number: u32, budget: &Budget<'_>) -> Result<()> {
100
+ let document = bounded::open(data, budget)?;
101
+ let selected = document.select_pages(number, number, 1)?;
102
+ let (_, page) = selected
103
+ .pages
104
+ .into_iter()
105
+ .next()
106
+ .ok_or(Error::InvalidPdfOption("page number is past the end of the document"))?;
107
+ let mut drawn = Vec::new();
108
+ page_images(&document, &page?, &mut drawn)?;
109
+ let mut pixels = 0_u64;
110
+ for image in drawn {
111
+ budget.check()?;
112
+ let dict = match &image.image {
113
+ Drawing::Object { object, .. } => object.as_dict(),
114
+ Drawing::Inline { dict, .. } => Some(dict),
115
+ }
116
+ .ok_or_else(|| Error::malformed("PDF", "invalid image dictionary"))?;
117
+ let dimension = |full: &[u8], short: &[u8]| -> Result<u64> {
118
+ let value = document.entry(dict, full)?.or(document.entry(dict, short)?);
119
+ let value = value
120
+ .and_then(|value| value.as_integer())
121
+ .and_then(|value| u64::try_from(value).ok());
122
+ value
123
+ .filter(|&value| value != 0)
124
+ .ok_or_else(|| Error::malformed("PDF", "invalid image dimensions"))
125
+ };
126
+ let size = dimension(b"Width", b"W")?
127
+ .checked_mul(dimension(b"Height", b"H")?)
128
+ .ok_or(Error::LimitExceeded("source image pixels"))?;
129
+ pixels = pixels.saturating_add(size);
130
+ if pixels > 40_000_000 {
131
+ return Err(Error::LimitExceeded("source image pixels"));
132
+ }
133
+ }
134
+ Ok(())
135
+ }
136
+
137
+ fn render(data: &[u8], number: u32, options: &RenderOptions, budget: &Budget<'_>) -> Result<Image> {
138
+ let pdf = Pdf::new(data.to_vec()).map_err(|error| match error {
139
+ LoadPdfError::Decryption(_) => Error::PasswordRequired,
140
+ LoadPdfError::Invalid => Error::malformed("PDF", "the renderer cannot open this document"),
141
+ })?;
142
+ budget.check()?;
143
+ let page = pdf.pages().get(number as usize - 1).ok_or_else(|| {
144
+ Error::malformed("PDF", "the renderer's page tree differs from the reader's")
145
+ })?;
146
+ let (width, height) = page.render_dimensions();
147
+ if !width.is_finite() || !height.is_finite() || width <= 0.0 || height <= 0.0 {
148
+ return Err(Error::malformed("PDF", "invalid page dimensions"));
149
+ }
150
+ let scale = f32::from(options.max_side) / width.max(height);
151
+ let pixels = |side: f32| (side * scale).floor().max(1.0).min(f32::from(options.max_side));
152
+ let (target_width, target_height) = (pixels(width), pixels(height));
153
+ #[expect(
154
+ clippy::cast_possible_truncation,
155
+ clippy::cast_sign_loss,
156
+ reason = "bounded by max_side"
157
+ )]
158
+ let (w, h) = (target_width as u32, target_height as u32);
159
+ options.check_pixels(w, h)?;
160
+ let warned = Arc::new(AtomicBool::new(false));
161
+ let warning = Arc::clone(&warned);
162
+ let settings = InterpreterSettings {
163
+ warning_sink: Arc::new(move |_| {
164
+ warning.store(true, Ordering::Relaxed);
165
+ }),
166
+ ..InterpreterSettings::default()
167
+ };
168
+ let pixmap = hayro::render(
169
+ page,
170
+ &RenderCache::new(),
171
+ &settings,
172
+ &RenderSettings::default(),
173
+ &PixmapSettings {
174
+ x_scale: target_width / width,
175
+ y_scale: target_height / height,
176
+ bg_color: WHITE,
177
+ },
178
+ );
179
+ budget.check()?;
180
+ if warned.load(Ordering::Relaxed) {
181
+ return Err(Error::UnsupportedDocument(
182
+ "the renderer could not render all page content".into(),
183
+ ));
184
+ }
185
+ let (w, h) = (u32::from(pixmap.width()), u32::from(pixmap.height()));
186
+ options.check_pixels(w, h)?;
187
+ let rgba = pixmap.data_as_u8_slice();
188
+ let encoded = if options.grayscale {
189
+ let gray: Vec<u8> = rgba
190
+ .as_chunks::<4>()
191
+ .0
192
+ .iter()
193
+ .map(|pixel| {
194
+ let value = u32::from(pixel[0]) * 299
195
+ + u32::from(pixel[1]) * 587
196
+ + u32::from(pixel[2]) * 114;
197
+ u8::try_from(value / 1000).unwrap_or(255)
198
+ })
199
+ .collect();
200
+ png(w, h, PngColor::Gray, 8, None, &gray)
201
+ } else {
202
+ png(w, h, PngColor::Rgba, 8, None, rgba)
203
+ };
204
+ budget.check()?;
205
+ options.image(encoded, number, (w, h))
206
+ }
@@ -1,8 +1,11 @@
1
- //! PDF: the text layer as Markdown. Every page's content runs through a
1
+ //! PDF inspection, bounded text reads and optional full-page rendering.
2
+ //!
3
+ //! Every page's content runs through a
2
4
  //! small interpreter that places each glyph; layout turns the glyphs into
3
5
  //! lines, paragraphs and headings. Scanned pages have no text layer:
4
6
  //! they are images, and need OCR.
5
7
 
8
+ mod bounded;
6
9
  mod cmap;
7
10
  mod content;
8
11
  mod crypt;
@@ -18,20 +21,28 @@ mod object;
18
21
  mod tests;
19
22
  mod xref;
20
23
 
24
+ pub use bounded::{Info, PageResult, Pages, ReadOptions, Status, info, pages};
25
+ #[cfg(feature = "render")]
26
+ mod render;
27
+ #[cfg(feature = "render")]
28
+ pub use render::{RenderOptions, render_page};
29
+ #[cfg(all(feature = "isolation", unix))]
30
+ pub mod isolation;
31
+
21
32
  use super::Budget;
22
33
  use super::content::Page;
23
- use crate::{Error, Result};
34
+ use crate::Result;
24
35
  use content::{Fonts, PageText};
25
36
  use document::Document;
26
- pub use images::images;
37
+ pub(super) use images::images;
27
38
  use xref::is_limit;
28
39
 
29
40
  /// `%PDF-` within the first kilobyte, where readers look for it.
30
- pub fn is_pdf(document: &[u8]) -> bool {
41
+ pub(super) fn is_pdf(document: &[u8]) -> bool {
31
42
  document[..document.len().min(1024)].windows(5).any(|window| window == b"%PDF-")
32
43
  }
33
44
 
34
- pub fn content(data: &[u8], budget: &Budget<'_>) -> Result<Vec<Page>> {
45
+ pub(super) fn content(data: &[u8], budget: &Budget<'_>) -> Result<Vec<Page>> {
35
46
  let document = Document::open(data, budget)?;
36
47
  let pages = document.pages()?;
37
48
  let mut fonts = Fonts::new();
@@ -46,8 +57,5 @@ pub fn content(data: &[u8], budget: &Budget<'_>) -> Result<Vec<Page>> {
46
57
  Err(_) => lines.push(layout::PageLines::default()),
47
58
  }
48
59
  }
49
- if lines.iter().all(|page| page.text.trim().is_empty()) {
50
- return Err(Error::NoTextLayer);
51
- }
52
60
  Ok(layout::pages(&lines))
53
61
  }