use crate::{Error, WorkLimit};
pub(crate) const MAX_IMAGE_SAMPLES: u64 = 1 << 29;
pub(crate) const ADDRESSABLE_SAMPLES: u64 = isize::MAX as u64 / 4;
pub(crate) fn cap(limit: &WorkLimit) -> u64 {
limit.apply(MAX_IMAGE_SAMPLES).min(ADDRESSABLE_SAMPLES)
}
pub(crate) fn over(what: &str, total: u64, cap: u64) -> Error {
if total > ADDRESSABLE_SAMPLES {
return Error::limit_fixed(
crate::LimitKind::AddressSpace,
format!(
"{what} needs {total} samples, more than this target can address \
({ADDRESSABLE_SAMPLES})"
),
);
}
let (retry, advice) = crate::limit_hint::samples(total, MAX_IMAGE_SAMPLES);
Error::limit_retry(
crate::LimitKind::Samples,
retry,
format!(
"{what} needs {total} samples, over the sample limit of {cap} (see \
`DecodeOptions::sample_limit`); {advice}"
),
)
}
pub(crate) fn at(reduction: u8) -> String {
if reduction == 0 {
String::new()
} else {
format!(" at resolution reduction {reduction}")
}
}
pub(crate) fn check_codestream(
siz: &crate::codestream::markers::Siz,
reduction: u8,
cap: u64,
) -> crate::Result<()> {
check_extent(siz, reduction, cap, false)
}
pub(crate) fn check_read(
siz: &crate::codestream::markers::Siz,
selection: &crate::window::Selection,
reduction: u8,
cap: u64,
) -> crate::Result<()> {
if selection.area.image_rect() == siz.image_rect() {
return check_codestream(siz, reduction, cap);
}
check_extent(
&siz.windowed(selection.read_rect(siz)),
reduction,
cap,
true,
)
}
fn check_extent(
siz: &crate::codestream::markers::Siz,
reduction: u8,
cap: u64,
tiles: bool,
) -> crate::Result<()> {
let (width, height) = siz.image_extent_at(reduction);
let samples = u64::from(width) * u64::from(height);
if samples > cap {
let what = if tiles {
format!(
"the area of the tiles read ({width}x{height}{})",
at(reduction)
)
} else {
format!("image area {width}x{height}{}", at(reduction))
};
return Err(over(&what, samples, cap));
}
let mut total: u64 = 0;
for index in 0..siz.components.len() {
let (width, height) = siz
.component_extent_at(index, reduction)
.ok_or_else(|| Error::inconsistent(format!("`SIZ` declares no component {index}")))?;
total = total.saturating_add(u64::from(width) * u64::from(height));
}
if total > cap {
let what = format!(
"a decode of {}{} components{}",
if tiles { "the tiles read in " } else { "" },
siz.components.len(),
at(reduction)
);
return Err(over(&what, total, cap));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn message(total: u64) -> String {
match over("image area", total, MAX_IMAGE_SAMPLES) {
Error::Limit(m) => m.message().to_owned(),
other => panic!("{other:?}"),
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn the_hint_names_the_smallest_factor() {
assert!(message(MAX_IMAGE_SAMPLES + 1).contains("`WorkLimit::Raised(2)`"));
let largest = u64::from(u32::MAX) * MAX_IMAGE_SAMPLES;
if largest <= ADDRESSABLE_SAMPLES {
let m = message(largest);
assert!(
m.contains(&format!("`WorkLimit::Raised({})`", u32::MAX)),
"{m}"
);
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn past_every_factor_the_hint_names_only_unlimited() {
let largest = u64::from(u32::MAX) * MAX_IMAGE_SAMPLES;
for total in [largest + 1, ADDRESSABLE_SAMPLES] {
let m = message(total);
assert!(!m.contains("Raised"), "{m}");
assert!(m.contains("only `WorkLimit::Unlimited` admits it"), "{m}");
let Error::Limit(limit) = over("image area", total, MAX_IMAGE_SAMPLES) else {
unreachable!()
};
assert_eq!(limit.kind(), crate::LimitKind::Samples);
assert_eq!(limit.retry_with(), Some(WorkLimit::Unlimited));
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn the_hint_is_typed() {
let Error::Limit(limit) = over("image area", MAX_IMAGE_SAMPLES * 3, MAX_IMAGE_SAMPLES)
else {
unreachable!()
};
assert_eq!(limit.kind(), crate::LimitKind::Samples);
assert_eq!(limit.retry_with(), Some(WorkLimit::Raised(3)));
}
#[test]
fn past_the_address_space_there_is_no_retry() {
let error = over("image area", ADDRESSABLE_SAMPLES + 1, MAX_IMAGE_SAMPLES);
assert!(
crate::error::is_fixed_limit(&error, crate::LimitKind::AddressSpace),
"{error:?}"
);
}
}