alpha2_extensions/budget.rs
1//! Limits every provider checks before it reads, decodes, or draws a source. A source past one
2//! is refused with [`ExtensionError::Budget`], natively and in the browser alike.
3//!
4//! The limits cover document size ([`document_limit`]), canvas size ([`check_canvas`]), decoded
5//! pixels ([`MAX_ANIMATION_PIXELS`], [`MAX_SOURCE_IMAGE_PIXELS`], [`DEFAULT_FRAME_BUDGET`]) and
6//! drawing cost ([`MAX_DRAWING`]). Format-specific counts (SVG elements, Lottie draws, dotLottie
7//! files) live next to each parser.
8
9use alpha2_model::{
10 MAX_LOTTIE_DOCUMENT_BYTES, MAX_PDF_DOCUMENT_BYTES, MAX_PROVIDER_DOCUMENT_BYTES,
11 MAX_SVG_DOCUMENT_BYTES, ProviderSource,
12};
13
14use crate::api::ExtensionError;
15
16/// Largest canvas side a provider opens, in pixels.
17pub const MAX_CANVAS_SIDE: u32 = 16_384;
18/// Largest canvas a provider opens, in pixels (8K UHD).
19pub const MAX_CANVAS_PIXELS: u64 = 7_680 * 4_320;
20/// Most frames an animated image may have.
21pub const MAX_ANIMATION_FRAMES: usize = 10_000;
22/// Most pixels all of an animated image's frames may cover together. About a thousand 1080p
23/// frames, roughly ten seconds of native decoding.
24pub const MAX_ANIMATION_PIXELS: u64 = 1 << 31;
25/// Decoded bytes an animated source or an image sequence keeps between requests.
26pub const DEFAULT_FRAME_BUDGET: usize = 256 * 1024 * 1024;
27/// Most pixels the raster images inside one source may decode to together. An SVG counts every
28/// image draw per render (an image reused through `<use>` counts twice); Lottie counts its assets
29/// once at open.
30pub const MAX_SOURCE_IMAGE_PIXELS: u64 = 4 * MAX_CANVAS_PIXELS;
31
32/// Most canvases of blended fill one SVG render or Lottie frame may draw. A 1080p canvas of
33/// blended fill costs about 4 to 10 ms natively, so this is about 2 s. Opaque fills are not
34/// discounted, since whether something above fades is unknown before drawing.
35#[cfg(any(feature = "svg", feature = "lottie"))]
36pub(crate) const MAX_DRAWING: f64 = 256.0;
37
38/// Minimum canvas an SVG counts as for [`MAX_DRAWING`], since a small icon can still draw many
39/// blurs. Lottie uses its own canvas because all its layers are frame-sized.
40#[cfg(feature = "svg")]
41pub(crate) const FLOOR_CANVAS: f64 = 1920.0 * 1080.0;
42
43/// Refuses a canvas larger than a provider opens.
44///
45/// # Errors
46/// [`ExtensionError::Budget`] or, for an empty canvas,
47/// [`ExtensionError::Malformed`].
48pub fn check_canvas(width: u32, height: u32) -> Result<(), ExtensionError> {
49 if width == 0 || height == 0 {
50 return Err(ExtensionError::Malformed("the canvas is empty".into()));
51 }
52 if width > MAX_CANVAS_SIDE
53 || height > MAX_CANVAS_SIDE
54 || u64::from(width) * u64::from(height) > MAX_CANVAS_PIXELS
55 {
56 return Err(ExtensionError::Budget(format!(
57 "a {width}x{height} canvas is larger than {MAX_CANVAS_PIXELS} pixels"
58 )));
59 }
60 Ok(())
61}
62
63/// The most bytes a provider reads of a whole-file format (SVG, Lottie, dotLottie, PDF), with a
64/// name for the file kind; `None` for streamed formats. `head` is the file's start, used to tell
65/// a dotLottie ZIP from Lottie JSON.
66#[must_use]
67pub fn document_limit(source: &ProviderSource, head: &[u8]) -> Option<(usize, &'static str)> {
68 match source {
69 ProviderSource::Svg => Some((MAX_SVG_DOCUMENT_BYTES, "SVG document")),
70 ProviderSource::Lottie { .. } if head.starts_with(b"PK\x03\x04") => {
71 Some((MAX_PROVIDER_DOCUMENT_BYTES, "dotLottie package"))
72 }
73 ProviderSource::Lottie { .. } => Some((MAX_LOTTIE_DOCUMENT_BYTES, "Lottie file")),
74 ProviderSource::Pdf { .. } => Some((MAX_PDF_DOCUMENT_BYTES, "PDF document")),
75 _ => None,
76 }
77}
78
79/// Refuses a source file of `length` bytes larger than its [`document_limit`].
80///
81/// # Errors
82/// [`ExtensionError::Budget`].
83pub fn check_document_size(
84 source: &ProviderSource,
85 head: &[u8],
86 length: u64,
87) -> Result<(), ExtensionError> {
88 match document_limit(source, head) {
89 Some((limit, what)) if length > limit as u64 => Err(ExtensionError::Budget(format!(
90 "the {what} is {length} bytes, more than the {limit} a provider reads"
91 ))),
92 _ => Ok(()),
93 }
94}
95
96/// Refuses images that together decode to more than [`MAX_SOURCE_IMAGE_PIXELS`].
97///
98/// # Errors
99/// [`ExtensionError::Budget`].
100pub fn check_image_pixels(pixels: u64) -> Result<(), ExtensionError> {
101 if pixels > MAX_SOURCE_IMAGE_PIXELS {
102 return Err(ExtensionError::Budget(format!(
103 "its images decode to {pixels} pixels, more than the {MAX_SOURCE_IMAGE_PIXELS} one source may"
104 )));
105 }
106 Ok(())
107}