mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 18:04:48 +00:00
hayro-syntax, hayro-interpret: callers set the image byte and pixel limits (#1901)
This commit is contained in:
@@ -342,3 +342,125 @@ mod rectangles {
|
||||
assert_eq!(shapes.clips, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "interpret")]
|
||||
mod image_limits {
|
||||
use craft_pdf::interpret::{
|
||||
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, ImageData, InterpreterCache, InterpreterSettings, Paint, PathDrawMode, SoftMask,
|
||||
TransformExt, font::Glyph,
|
||||
};
|
||||
use craft_pdf::syntax::DecodeLimits;
|
||||
use kurbo::{Affine, BezPath, Rect};
|
||||
|
||||
/// The RGB samples and alpha of a raster image.
|
||||
type Pixels = (Vec<u8>, Option<Vec<u8>>);
|
||||
|
||||
/// The pixels of each raster image drawn (`None`: it did not decode).
|
||||
#[derive(Default)]
|
||||
struct Images(Vec<Option<Pixels>>);
|
||||
|
||||
impl Images {
|
||||
/// The RGB samples of each image, without alpha.
|
||||
fn rgb(&self) -> Vec<Option<Vec<u8>>> {
|
||||
self.0.iter().map(|i| i.as_ref().map(|(rgb, _)| rgb.clone())).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl Device<'_> for Images {
|
||||
fn set_soft_mask(&mut self, _: Option<SoftMask<'_>>) {}
|
||||
fn set_blend_mode(&mut self, _: BlendMode) {}
|
||||
fn draw_path(&mut self, _: &BezPath, _: Affine, _: &Paint<'_>, _: &PathDrawMode) {}
|
||||
fn push_clip_path(&mut self, _: &ClipPath) {}
|
||||
fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'_>>, _: BlendMode) {}
|
||||
fn draw_glyph(&mut self, _: &Glyph<'_>, _: Affine, _: Affine, _: &Paint<'_>, _: &GlyphDrawMode) {}
|
||||
fn draw_image(&mut self, image: Image<'_, '_>, _: Affine) {
|
||||
let mut rgb = None;
|
||||
if let Image::Raster(r) = image {
|
||||
r.with_rgba(
|
||||
|img, alpha| {
|
||||
if let ImageData::Rgb(d) = img {
|
||||
rgb = Some((d.data, alpha.map(|a| a.data)));
|
||||
}
|
||||
},
|
||||
None,
|
||||
);
|
||||
}
|
||||
self.0.push(rgb);
|
||||
}
|
||||
fn pop_clip_path(&mut self) {}
|
||||
fn pop_transparency_group(&mut self) {}
|
||||
}
|
||||
|
||||
/// A page that draws a 4 × 2 `DeviceRGB` image of 8-bit samples 0, 1, … 23, read with `limits`.
|
||||
/// The samples are one run-length literal run, written in hex.
|
||||
fn draw(limits: DecodeLimits) -> Images {
|
||||
draw_with(limits, RGB)
|
||||
}
|
||||
|
||||
/// The color space and sample size of [`draw`]'s image.
|
||||
const RGB: &str = "/ColorSpace /DeviceRGB /BitsPerComponent 8";
|
||||
|
||||
/// [`draw`], with `extra` in the image's dictionary instead of [`RGB`].
|
||||
fn draw_with(limits: DecodeLimits, extra: &str) -> Images {
|
||||
let run: Vec<u8> = [23].into_iter().chain(0..24).chain([128]).collect();
|
||||
let hex: String = run.iter().map(|b| format!("{b:02X}")).collect();
|
||||
let content = "q 4 0 0 2 0 0 cm /Im0 Do Q";
|
||||
let bytes = format!(
|
||||
"%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 4 2] /Contents 4 0 R /Resources << /XObject << /Im0 5 0 R >> >> >> endobj\n4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\n5 0 obj << /Type /XObject /Subtype /Image /Width 4 /Height 2 /Filter [/ASCIIHexDecode /RunLengthDecode] {extra} /Length {} >> stream\n{hex}>\nendstream endobj\ntrailer << /Root 1 0 R >>\n%%EOF",
|
||||
content.len(),
|
||||
hex.len() + 1
|
||||
);
|
||||
let options = craft_pdf::OpenOptions { decode_limits: limits, ..Default::default() };
|
||||
let pdf = craft_pdf::open_slice_with_options(bytes.as_bytes(), None, &options).unwrap();
|
||||
let page = &pdf.pages()[0];
|
||||
let cache = InterpreterCache::new();
|
||||
let mut context =
|
||||
Context::new(page.initial_transform(true).to_kurbo(), Rect::new(0.0, 0.0, 4.0, 2.0), &cache, page.xref(), InterpreterSettings::default());
|
||||
let mut images = Images::default();
|
||||
craft_pdf::interpret_page(page, &mut context, &mut images).unwrap();
|
||||
images
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||
fn device_rgb_samples_arrive_as_written() {
|
||||
assert_eq!(draw(DecodeLimits::default()).0, [Some(((0..24).collect::<Vec<u8>>(), None))]);
|
||||
// An inverted `/Decode` inverts them.
|
||||
let inverted: Vec<u8> = (0..24).map(|s| 255 - s).collect();
|
||||
assert_eq!(draw_with(DecodeLimits::default(), &format!("{RGB} /Decode [1 0 1 0 1 0]")).rgb(), [Some(inverted)]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||
fn a_color_key_mask_reads_device_rgb_samples() {
|
||||
// Pixels 0 (0, 1, 2) and 1 (3, 4, 5) have every sample in 0 to 5, so they are masked out.
|
||||
let images = draw_with(DecodeLimits::default(), &format!("{RGB} /Mask [0 5 0 5 0 5]"));
|
||||
assert_eq!(images.0, [Some(((0..24).collect::<Vec<u8>>(), Some(vec![0, 0, 255, 255, 255, 255, 255, 255])))]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||
fn a_color_key_mask_of_an_emptied_gray_image_reads_its_padding() {
|
||||
// A 1-bit inverted gray image whose samples the byte limit leaves out: the missing samples
|
||||
// read as 0, white once inverted, and inside the key range, so every pixel is masked out.
|
||||
let images =
|
||||
draw_with(DecodeLimits::default().with_max_decoded_image(0), "/ColorSpace /DeviceGray /BitsPerComponent 1 /Decode [1 0] /Mask [0 0]");
|
||||
assert_eq!(images.0, [Some((vec![255; 24], Some(vec![0; 8])))]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||
fn the_pixel_limit_comes_from_the_document() {
|
||||
assert_eq!(draw(DecodeLimits::default().with_max_image_pixels(7)).0, [None], "8 pixels are past a limit of 7");
|
||||
assert_eq!(draw(DecodeLimits::default().with_max_image_pixels(8)).rgb(), [Some((0..24).collect::<Vec<u8>>())]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||
fn the_image_byte_limit_comes_from_the_document() {
|
||||
// The run-length filter stops at 10 bytes: one row is left, its last two samples zero.
|
||||
let cut: Vec<u8> = (0..10).chain([0, 0]).collect();
|
||||
assert_eq!(draw(DecodeLimits::default().with_max_decoded_image(10)).rgb(), [Some(cut)]);
|
||||
assert_eq!(draw(DecodeLimits::default().with_max_decoded_image(1 << 40)).rgb(), [Some((0..24).collect::<Vec<u8>>())]);
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+10
@@ -98,6 +98,16 @@ the transferred decoder patches.
|
||||
Synthetic unit tests cover these boundaries; two version tests and the introductory doctest
|
||||
use synthetic PDFs so they do not rely on the absent upstream corpus. No corpus is copied.
|
||||
|
||||
`DecodeLimits` also carries the two image limits: `with_max_decoded_image` sets the most bytes
|
||||
the last filter of an image of known size may produce (1 GiB by default), and
|
||||
`with_max_image_pixels` sets the most pixels an image or mask may have for the interpreter to
|
||||
decode it (2^28 by default, at most 2^32), before and after its codec decodes it. With 64-bit
|
||||
pointers a caller may raise either one; elsewhere a raise is clamped to the default.
|
||||
`image_size_ok` keeps the 2^28 default. An 8-bit `DeviceRGB` image with the default or inverted `/Decode` and no transfer function keeps its decoded samples as its
|
||||
RGB data instead of copying them into a second buffer. Tests:
|
||||
`decode_limits_tests::image_limits_default_and_caller_figures` in hayro-syntax and
|
||||
`image_limits` in `crates/pdf/tests/interpretation.rs`.
|
||||
|
||||
Compatibility patches follow the resource scopes and text-showing order in
|
||||
[Hayro 0.8.0, revision ea9c81dc70cde40c64205986fdcfcd481fe934d8](https://github.com/LaurenzV/hayro/tree/ea9c81dc70cde40c64205986fdcfcd481fe934d8),
|
||||
under the same MIT OR Apache-2.0 licence. Explicit page and nested-object resource
|
||||
|
||||
Vendored
+5
@@ -400,6 +400,11 @@ impl ColorSpace {
|
||||
matches!(self.0.as_ref(), ColorSpaceType::DeviceGray)
|
||||
}
|
||||
|
||||
/// Whether this is `DeviceRGB`, whose 8-bit samples are their own RGB values.
|
||||
pub(crate) fn is_device_rgb(&self) -> bool {
|
||||
matches!(self.0.as_ref(), ColorSpaceType::DeviceRgb)
|
||||
}
|
||||
|
||||
/// Get the number of components of the color space.
|
||||
pub(crate) fn num_components(&self) -> u8 {
|
||||
match self.0.as_ref() {
|
||||
|
||||
+33
-2
@@ -673,7 +673,8 @@ fn decode_context<'a>(
|
||||
) -> Option<DecodeContext<'a>> {
|
||||
// PdfCraft patch: images and masks over `MAX_IMAGE_PIXELS` are not decoded at all (the
|
||||
// renderer skips them anyway): a fuzzed `/W 4294967295` stencil mask allocated gigabytes here.
|
||||
if !image_size_ok(obj.width, obj.height) {
|
||||
// The document's decode limits may set another pixel count.
|
||||
if !image_size_within(obj.width, obj.height, obj.stream.decode_limits().max_image_pixels()) {
|
||||
(obj.warning_sink)(InterpreterWarning::ImageDecodeFailure);
|
||||
return None;
|
||||
}
|
||||
@@ -711,6 +712,11 @@ fn decode_context<'a>(
|
||||
(d.width, d.height)
|
||||
})
|
||||
.unwrap_or((obj.width, obj.height));
|
||||
// Craft patch: the size an image codec decoded to is held to the same limit.
|
||||
if !image_size_within(width, height, obj.stream.decode_limits().max_image_pixels()) {
|
||||
(obj.warning_sink)(InterpreterWarning::ImageDecodeFailure);
|
||||
return None;
|
||||
}
|
||||
|
||||
let color_space = color_space
|
||||
.or_else(|| {
|
||||
@@ -810,6 +816,7 @@ fn decode_raster(
|
||||
.color_space
|
||||
.inverted_default_decode_arr(ctx.bits_per_component as f32);
|
||||
|
||||
let mut rgb_moved = false;
|
||||
let image_data = if ctx.bits_per_component == 8
|
||||
&& ctx.color_space.supports_u8()
|
||||
&& obj.transfer_function.is_none()
|
||||
@@ -842,6 +849,17 @@ fn decode_raster(
|
||||
interpolate: obj.interpolate,
|
||||
scale_factors: ctx.scale_factors,
|
||||
}))
|
||||
} else if ctx.color_space.is_device_rgb() {
|
||||
// Craft patch: `DeviceRGB` samples are taken as they are, without a second buffer
|
||||
// of the image's size (`fix_image_length` made them whole rows).
|
||||
rgb_moved = true;
|
||||
Some(ImageData::Rgb(RgbData {
|
||||
data: core::mem::take(&mut ctx.decoded.data).into_owned(),
|
||||
width: ctx.width,
|
||||
height,
|
||||
interpolate: obj.interpolate,
|
||||
scale_factors: ctx.scale_factors,
|
||||
}))
|
||||
} else {
|
||||
let mut output_buf = vec![0; ctx.width as usize * height as usize * 3];
|
||||
ctx.color_space
|
||||
@@ -944,6 +962,7 @@ fn decode_raster(
|
||||
obj,
|
||||
&mut ctx.decoded,
|
||||
Some(&image),
|
||||
rgb_moved,
|
||||
&ctx.color_space,
|
||||
ctx.bits_per_component,
|
||||
ctx.width,
|
||||
@@ -994,6 +1013,7 @@ pub(crate) fn decode_inks(
|
||||
obj,
|
||||
&mut ctx.decoded,
|
||||
None,
|
||||
false,
|
||||
&cs,
|
||||
bpc,
|
||||
ctx.width,
|
||||
@@ -1115,10 +1135,14 @@ fn decode_mask_bytes(
|
||||
Some(data)
|
||||
}
|
||||
|
||||
/// Craft patch: `rgb_moved` says that `image_data` holds the image's own 8-bit `DeviceRGB`
|
||||
/// samples, moved out of `decoded` (see `decode_raster`).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn resolve_alpha(
|
||||
obj: &ImageXObject<'_>,
|
||||
decoded: &mut FilterResult<'_>,
|
||||
image_data: Option<&ImageData>,
|
||||
rgb_moved: bool,
|
||||
color_space: &ColorSpace,
|
||||
bits_per_component: u8,
|
||||
width: u32,
|
||||
@@ -1158,6 +1182,7 @@ fn resolve_alpha(
|
||||
// TODO: Make this less ugly.
|
||||
let raw_data = match image_data {
|
||||
Some(ImageData::Luma(d)) => &d.data,
|
||||
Some(ImageData::Rgb(d)) if rgb_moved => &d.data,
|
||||
_ => decoded.data.as_ref(),
|
||||
};
|
||||
|
||||
@@ -1486,7 +1511,13 @@ fn convert_each(cs: &ColorSpace, input: &[f32], n: usize, output: &mut [u8]) {
|
||||
/// PdfCraft patch: whether an image or mask of `width` × `height` may be decoded (at most
|
||||
/// 2^28 pixels, the renderer's `MAX_IMAGE_PIXELS`).
|
||||
pub fn image_size_ok(width: u32, height: u32) -> bool {
|
||||
width > 0 && height > 0 && u64::from(width) * u64::from(height) <= 1 << 28
|
||||
image_size_within(width, height, 1 << 28)
|
||||
}
|
||||
|
||||
/// Whether an image or mask of `width` × `height` has at most `max_pixels` pixels and is not
|
||||
/// empty.
|
||||
fn image_size_within(width: u32, height: u32, max_pixels: u64) -> bool {
|
||||
width > 0 && height > 0 && u64::from(width) * u64::from(height) <= max_pixels
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+27
@@ -179,6 +179,33 @@ fn final_known_size_images_keep_their_distinct_pixel_budget() {
|
||||
assert_eq!(crate::filter::decode_limit(&ImageDecodeParams::default(), true, limits), 32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_limits_default_and_caller_figures() {
|
||||
let limits = DecodeLimits::default();
|
||||
assert_eq!((limits.max_decoded_image(), limits.max_image_pixels()), (1 << 30, 1 << 28));
|
||||
// An image of 31404 × 31403 RGB pixels: 2.96 GB, past the default.
|
||||
let params = ImageDecodeParams { width: 31_404, height: 31_403, bpc: Some(8), num_components: Some(3), ..Default::default() };
|
||||
assert_eq!(crate::filter::decode_limit(¶ms, true, limits) as u64, 1 << 30);
|
||||
let raised = limits.with_max_decoded_image(4 << 30).with_max_image_pixels(1 << 30);
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
{
|
||||
assert_eq!((raised.max_decoded_image(), raised.max_image_pixels()), (4 << 30, 1 << 30));
|
||||
assert_eq!(crate::filter::decode_limit(¶ms, true, raised) as u64, 4 << 30);
|
||||
assert_eq!(raised.with_max_decoded_stream(32).max_decoded_image(), 4 << 30, "lowering the stream limit keeps the image limits");
|
||||
assert_eq!(limits.with_max_image_pixels(u64::MAX).max_image_pixels(), 1 << 32);
|
||||
}
|
||||
#[cfg(not(target_pointer_width = "64"))]
|
||||
assert_eq!(raised, limits, "a raise is clamped to the defaults");
|
||||
assert_eq!(raised.max_decoded_stream(), MAX_DECODED_STREAM, "the stream limit is kept");
|
||||
assert_eq!(raised.with_max_decoded_image(64).max_decoded_stream(), MAX_DECODED_STREAM);
|
||||
// A context made without a document keeps every limit it is given.
|
||||
for given in [limits, limits.with_max_decoded_stream(32), raised, limits.with_max_image_pixels(5)] {
|
||||
assert_eq!(crate::reader::ReaderContext::dummy_with_limits(given).decode_limits(), given);
|
||||
}
|
||||
let small = ImageDecodeParams { width: 64, height: 1, bpc: Some(8), num_components: Some(1), ..Default::default() };
|
||||
assert_eq!(crate::filter::decode_limit(&small, true, limits.with_max_decoded_image(100)), 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn object_stream_limit_applies_after_successful_root_xref() {
|
||||
let (bytes, _, _) = object_stream_pdf(false);
|
||||
|
||||
+7
-3
@@ -145,8 +145,12 @@ impl Filter {
|
||||
pub const MAX_DECODED_STREAM: usize = 256 << 20;
|
||||
|
||||
/// PdfCraft patch: the most bytes they may produce for an image of known size, however large it
|
||||
/// says it is.
|
||||
const MAX_DECODED_IMAGE: u64 = 1 << 30;
|
||||
/// says it is, unless the document's [`DecodeLimits`](crate::DecodeLimits) set another figure.
|
||||
pub(crate) const MAX_DECODED_IMAGE: u64 = 1 << 30;
|
||||
|
||||
/// The most pixels an image or mask may have for the interpreter to decode it, unless the
|
||||
/// document's [`DecodeLimits`](crate::DecodeLimits) set another figure.
|
||||
pub(crate) const MAX_IMAGE_PIXELS: u64 = 1 << 28;
|
||||
|
||||
/// PdfCraft patch: the output limit for one filter of a stream decoded with `params`; `last`
|
||||
/// says whether it is the last filter. The last filter of an image whose size, bits per
|
||||
@@ -165,7 +169,7 @@ pub(crate) fn decode_limit(params: &ImageDecodeParams, last: bool, limits: crate
|
||||
}
|
||||
let bits = u64::from(params.width) * u64::from(components) * u64::from(bpc);
|
||||
let pixels = bits.div_ceil(8).saturating_mul(u64::from(params.height));
|
||||
let image = pixels.saturating_mul(2).min(MAX_DECODED_IMAGE);
|
||||
let image = pixels.saturating_mul(2).min(limits.max_decoded_image());
|
||||
usize::try_from(image).unwrap_or(MAX_DECODED_STREAM)
|
||||
}
|
||||
|
||||
|
||||
Vendored
+50
-3
@@ -20,26 +20,63 @@ use crate::metadata::Metadata;
|
||||
/// budget, not this ordinary materialization limit. `Page::page_stream` materializes that
|
||||
/// same stream and does use this limit, so the two paths can produce different lengths.
|
||||
/// These limits do not impose a total document memory or execution-time limit.
|
||||
///
|
||||
/// The image limits bound one image `XObject` or mask of known size: the bytes its last Flate,
|
||||
/// LZW or RunLength filter may produce, and the pixels the interpreter decodes, whatever its
|
||||
/// filters. Their defaults are 1 GiB and 2^28 pixels. A caller that can hold larger images
|
||||
/// raises them; where pointers are narrower than 64 bits, a raise is clamped to the default.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct DecodeLimits {
|
||||
max_decoded_stream: usize,
|
||||
max_decoded_image: u64,
|
||||
max_image_pixels: u64,
|
||||
}
|
||||
|
||||
impl DecodeLimits {
|
||||
/// The existing ordinary stream limit, 256 MiB.
|
||||
pub const DEFAULT: Self = Self { max_decoded_stream: crate::filter::MAX_DECODED_STREAM };
|
||||
/// The existing limits: 256 MiB for an ordinary stream, 1 GiB and 2^28 pixels for an image.
|
||||
pub const DEFAULT: Self = Self {
|
||||
max_decoded_stream: crate::filter::MAX_DECODED_STREAM,
|
||||
max_decoded_image: crate::filter::MAX_DECODED_IMAGE,
|
||||
max_image_pixels: crate::filter::MAX_IMAGE_PIXELS,
|
||||
};
|
||||
|
||||
/// Lower the maximum expanded bytes per ordinary stream. Requests above the existing
|
||||
/// 256 MiB ceiling are clamped; zero permits no ordinary materialized expansion.
|
||||
/// This does not lower the separate incremental page-content budget.
|
||||
pub const fn with_max_decoded_stream(self, max: usize) -> Self {
|
||||
Self { max_decoded_stream: if max < self.max_decoded_stream { max } else { self.max_decoded_stream } }
|
||||
Self { max_decoded_stream: if max < self.max_decoded_stream { max } else { self.max_decoded_stream }, ..self }
|
||||
}
|
||||
|
||||
/// The maximum expanded bytes per ordinary stream.
|
||||
pub const fn max_decoded_stream(self) -> usize {
|
||||
self.max_decoded_stream
|
||||
}
|
||||
|
||||
/// Set the most bytes the last Flate, LZW or RunLength filter of an image of known size may
|
||||
/// produce, lower or (with 64-bit pointers) higher than the 1 GiB default. The filter also
|
||||
/// stops at twice the image's own pixel bytes.
|
||||
pub const fn with_max_decoded_image(self, max: u64) -> Self {
|
||||
Self { max_decoded_image: narrow_clamp(max, crate::filter::MAX_DECODED_IMAGE), ..self }
|
||||
}
|
||||
|
||||
/// The most bytes the last Flate, LZW or RunLength filter of an image of known size may
|
||||
/// produce.
|
||||
pub const fn max_decoded_image(self) -> u64 {
|
||||
self.max_decoded_image
|
||||
}
|
||||
|
||||
/// Set the most pixels an image or mask may have for the interpreter to decode it, lower or
|
||||
/// (with 64-bit pointers) higher than the 2^28 default, up to 2^32. A larger image is
|
||||
/// skipped with an image decode warning.
|
||||
pub const fn with_max_image_pixels(self, max: u64) -> Self {
|
||||
let max = if max > MAX_RAISED_IMAGE_PIXELS { MAX_RAISED_IMAGE_PIXELS } else { max };
|
||||
Self { max_image_pixels: narrow_clamp(max, crate::filter::MAX_IMAGE_PIXELS), ..self }
|
||||
}
|
||||
|
||||
/// The most pixels an image or mask may have for the interpreter to decode it.
|
||||
pub const fn max_image_pixels(self) -> u64 {
|
||||
self.max_image_pixels
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DecodeLimits {
|
||||
@@ -48,6 +85,16 @@ impl Default for DecodeLimits {
|
||||
}
|
||||
}
|
||||
|
||||
/// The most pixels [`DecodeLimits::with_max_image_pixels`] sets: the sample and byte counts of
|
||||
/// such an image (up to 32 components of 4 bytes) fit a 64-bit `usize`.
|
||||
const MAX_RAISED_IMAGE_PIXELS: u64 = 1 << 32;
|
||||
|
||||
/// `max`, or `default` when `max` is higher and pointers are narrower than 64 bits: the sizes
|
||||
/// derived from a larger image overflow `usize` there.
|
||||
const fn narrow_clamp(max: u64, default: u64) -> u64 {
|
||||
if usize::BITS < 64 && max > default { default } else { max }
|
||||
}
|
||||
|
||||
/// A PDF file.
|
||||
pub struct Pdf {
|
||||
xref: Arc<XRef>,
|
||||
|
||||
Vendored
+12
-9
@@ -127,7 +127,9 @@ struct ReaderContextData<'a> {
|
||||
#[derive(Clone, Debug)]
|
||||
enum ReaderContextInner<'a> {
|
||||
Shared(Arc<ReaderContextData<'a>>),
|
||||
Dummy { in_content_stream: bool, limits: crate::DecodeLimits },
|
||||
/// Craft patch: the default limits with `max_decoded_stream` (other limits are in a
|
||||
/// `ControlledDummy`), so the context stays the size of an object's fields.
|
||||
Dummy { in_content_stream: bool, max_decoded_stream: usize },
|
||||
ControlledDummy(Arc<ControlledDummy>),
|
||||
}
|
||||
|
||||
@@ -135,7 +137,7 @@ enum ReaderContextInner<'a> {
|
||||
struct ControlledDummy {
|
||||
in_content_stream: bool,
|
||||
limits: crate::DecodeLimits,
|
||||
control: crate::cancel::Control,
|
||||
control: Option<crate::cancel::Control>,
|
||||
}
|
||||
|
||||
/// Context for reading PDF objects.
|
||||
@@ -158,34 +160,35 @@ impl<'a> ReaderContext<'a> {
|
||||
}
|
||||
|
||||
pub(crate) fn dummy_with_limits(limits: crate::DecodeLimits) -> Self {
|
||||
Self(ReaderContextInner::Dummy { in_content_stream: false, limits })
|
||||
Self::dummy_with_control(limits, None)
|
||||
}
|
||||
|
||||
pub(crate) fn dummy_with_control(limits: crate::DecodeLimits, control: Option<crate::cancel::Control>) -> Self {
|
||||
match control {
|
||||
Some(control) => Self(ReaderContextInner::ControlledDummy(Arc::new(ControlledDummy { in_content_stream: false, limits, control }))),
|
||||
None => Self::dummy_with_limits(limits),
|
||||
let max_decoded_stream = limits.max_decoded_stream();
|
||||
if control.is_none() && limits == crate::DecodeLimits::DEFAULT.with_max_decoded_stream(max_decoded_stream) {
|
||||
return Self(ReaderContextInner::Dummy { in_content_stream: false, max_decoded_stream });
|
||||
}
|
||||
Self(ReaderContextInner::ControlledDummy(Arc::new(ControlledDummy { in_content_stream: false, limits, control })))
|
||||
}
|
||||
|
||||
pub(crate) fn control(&self) -> Option<&crate::cancel::Control> {
|
||||
match &self.0 {
|
||||
ReaderContextInner::Shared(inner) => inner.xref.control(),
|
||||
ReaderContextInner::Dummy { .. } => None,
|
||||
ReaderContextInner::ControlledDummy(inner) => Some(&inner.control).filter(|c| c.active()),
|
||||
ReaderContextInner::ControlledDummy(inner) => inner.control.as_ref().filter(|c| c.active()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn decode_limits(&self) -> crate::DecodeLimits {
|
||||
match &self.0 {
|
||||
ReaderContextInner::Shared(inner) => inner.xref.decode_limits(),
|
||||
ReaderContextInner::Dummy { limits, .. } => *limits,
|
||||
ReaderContextInner::Dummy { max_decoded_stream, .. } => crate::DecodeLimits::DEFAULT.with_max_decoded_stream(*max_decoded_stream),
|
||||
ReaderContextInner::ControlledDummy(inner) => inner.limits,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn dummy_ref() -> &'static Self {
|
||||
&Self(ReaderContextInner::Dummy { in_content_stream: true, limits: crate::DecodeLimits::DEFAULT })
|
||||
&Self(ReaderContextInner::Dummy { in_content_stream: true, max_decoded_stream: crate::filter::MAX_DECODED_STREAM })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
Reference in New Issue
Block a user