use crate::error::{Error, Result};
pub const MAX_METADATA_BYTES: u64 = 512 << 20;
pub const MAX_PLANE_BYTES: u64 = crate::pixel::MAX_PLANE_BYTES;
pub const PLANE_EXPANSION: u64 = 256;
pub const MIN_PLANE_LIMIT: u64 = 1 << 30;
pub const PLANE_LIMIT_ENV: &str = "OPENREADOUT_MAX_PLANE_BYTES";
fn plane_limit_override() -> Option<u64> {
static LIMIT: std::sync::OnceLock<Option<u64>> = std::sync::OnceLock::new();
*LIMIT.get_or_init(|| {
std::env::var(PLANE_LIMIT_ENV)
.ok()
.and_then(|v| v.trim().parse::<u64>().ok())
})
}
pub fn plane_limit(file_len: u64) -> u64 {
plane_limit_override().map_or_else(
|| {
file_len
.saturating_mul(PLANE_EXPANSION)
.clamp(MIN_PLANE_LIMIT, MAX_PLANE_BYTES)
},
|limit| limit.min(MAX_PLANE_BYTES),
)
}
pub const MAX_ITEM_COUNT: u64 = 1 << 24;
pub fn checked_len(
format: &'static str,
declared: u64,
available: u64,
what: &str,
) -> Result<usize> {
if declared > available {
return Err(Error::corrupt(
format,
format!(
"{what} declares {declared} bytes but only {available} are available (truncated or corrupt length field)"
),
));
}
usize::try_from(declared).map_err(|_| {
Error::corrupt(
format,
format!("{what} declares {declared} bytes, more than this platform can address"),
)
})
}
pub fn checked_metadata_len(
format: &'static str,
declared: u64,
available: u64,
what: &str,
) -> Result<usize> {
if declared > MAX_METADATA_BYTES {
return Err(Error::corrupt(
format,
format!(
"{what} declares {declared} bytes of metadata (limit {MAX_METADATA_BYTES}); refusing to load it"
),
));
}
checked_len(format, declared, available, what)
}
pub fn read_metadata_file(
format: &'static str,
fs: &crate::source::Fs,
path: &std::path::Path,
) -> Result<Vec<u8>> {
let len = fs.metadata(path).map_err(|e| Error::io(path, e))?.len();
if len > MAX_METADATA_BYTES {
return Err(Error::corrupt(
format,
format!(
"{} is {len} bytes; refusing to load more than {MAX_METADATA_BYTES} bytes of metadata",
path.display()
),
));
}
fs.read(path).map_err(|e| Error::io(path, e))
}
pub fn read_metadata_text(
format: &'static str,
fs: &crate::source::Fs,
path: &std::path::Path,
) -> Result<String> {
String::from_utf8(read_metadata_file(format, fs, path)?)
.map_err(|_| Error::corrupt(format, format!("{} is not UTF-8 text", path.display())))
}
pub fn remaining(file_len: u64, offset: u64) -> u64 {
file_len.saturating_sub(offset)
}
pub fn plane_len(
format: &'static str,
width: u64,
height: u64,
samples: u64,
bytes_per_sample: u64,
file_len: u64,
) -> Result<usize> {
let n = width
.checked_mul(height)
.and_then(|v| v.checked_mul(samples))
.and_then(|v| v.checked_mul(bytes_per_sample))
.ok_or_else(|| {
Error::corrupt(
format,
format!(
"plane geometry {width}x{height}x{samples}x{bytes_per_sample} bytes overflows"
),
)
})?;
let cap = plane_limit(file_len);
if n > cap {
return Err(Error::unsupported(
format,
format!("a {width}x{height} plane of {n} bytes (limit {cap} for this file)"),
format!(
"The header declares a plane too large to materialize: either the file is corrupt, or it is a whole-slide image that needs tiled access. If the file is sound and memory allows, set {PLANE_LIMIT_ENV}=<bytes> to raise the file-relative limit (the hard 4 GiB ceiling still applies)."
),
));
}
usize::try_from(n).map_err(|_| {
Error::corrupt(
format,
format!("plane of {n} bytes does not fit this platform's address space"),
)
})
}
pub fn checked_plane_count(what: &'static str, c: u32, z: u32, t: u32) -> Result<u64> {
let n = u64::from(c) * u64::from(z) * u64::from(t);
if n > MAX_ITEM_COUNT {
return Err(Error::unsupported(
what,
format!("an image declaring {c} x {z} x {t} = {n} planes"),
format!(
"More than {MAX_ITEM_COUNT} planes per image is not exported; the header is probably corrupt (run `openreadout check`)."
),
));
}
Ok(n)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn checked_len_rejects_lengths_past_the_end() {
assert_eq!(checked_len("t", 10, 10, "x").unwrap(), 10);
let e = checked_len("t", 11, 10, "x").unwrap_err();
assert_eq!(e.exit_code(), 4);
}
#[test]
fn metadata_cap_applies() {
let e = checked_metadata_len("t", MAX_METADATA_BYTES + 1, u64::MAX, "xml").unwrap_err();
assert_eq!(e.code(), "corrupt_file");
}
#[test]
fn plane_len_checks_overflow_and_cap() {
assert_eq!(plane_len("t", 4, 3, 1, 2, 100).unwrap(), 24);
assert_eq!(
plane_len("t", u64::MAX, 2, 1, 1, 100).unwrap_err().code(),
"corrupt_file"
);
assert_eq!(
plane_len("t", 1 << 20, 1 << 20, 3, 8, u64::MAX)
.unwrap_err()
.code(),
"unsupported_feature"
);
assert!(plane_len("t", 65_536, 65_536, 1, 1, u64::MAX).is_ok());
assert!(plane_len("t", 65_536, 65_537, 1, 1, u64::MAX).is_err());
assert!(plane_len("t", 1 << 15, 1 << 15, 1, 2, 1 << 10).is_err());
assert!(plane_len("t", 1 << 15, 1 << 15, 1, 2, 100 << 20).is_ok());
}
#[test]
fn plane_limit_scales_with_the_file() {
if std::env::var_os(PLANE_LIMIT_ENV).is_some() {
return;
}
assert_eq!(plane_limit(0), MIN_PLANE_LIMIT);
assert_eq!(plane_limit(10 << 20), (10 << 20) * PLANE_EXPANSION);
assert_eq!(plane_limit(u64::MAX), MAX_PLANE_BYTES);
}
#[test]
fn remaining_saturates() {
assert_eq!(remaining(10, 4), 6);
assert_eq!(remaining(10, 40), 0);
}
}