diff --git a/crates/hdr/src/lib.rs b/crates/hdr/src/lib.rs index d46232306..731737ce1 100644 --- a/crates/hdr/src/lib.rs +++ b/crates/hdr/src/lib.rs @@ -43,16 +43,19 @@ pub type Result = std::result::Result; #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[non_exhaustive] pub struct Limits { - /// Most pixels (width × height) a picture may have; each takes 12 bytes decoded. Default 2^26. + /// Most pixels (width × height) a picture may have; each takes 12 bytes as RGB or 16 as RGBA. Default 2^26. /// A picture over the target's largest allocation is refused at any setting. pub max_pixels: u64, /// Longest header read, in bytes (real ones are a few hundred). Default 64 KiB. pub max_header: usize, + /// Largest width or height, checked before allocating the validation scan line. + /// Defaults to `u32::MAX`, preserving the existing RGB decoder limit. + pub max_axis: u32, } impl Default for Limits { fn default() -> Self { - Self { max_pixels: 1 << 26, max_header: 64 << 10 } + Self { max_pixels: 1 << 26, max_header: 64 << 10, max_axis: u32::MAX } } } @@ -65,6 +68,15 @@ pub struct Picture { pub rgb: Vec, } +/// A decoded opaque RGBA picture, without an intermediate RGB allocation. +#[derive(Debug, Clone, PartialEq)] +pub struct RgbaPicture { + pub width: u32, + pub height: u32, + /// Scene-linear RGBA, top row first. Every alpha sample is 1.0. + pub rgba: Vec, +} + /// The largest allocation in bytes that a `Vec` or `String` can make on the target. const MAX_ALLOC_BYTES: u64 = isize::MAX as u64; @@ -187,6 +199,19 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result { /// [`decode`] with `max_alloc_bytes` as the largest allocation the target allows. fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result { + let (width, height, rgb) = decode_channels::<3>(b, limits, max_alloc_bytes)?; + Ok(Picture { width, height, rgb }) +} + +/// Decodes directly to opaque linear float RGBA, with no intermediate RGB image. +/// The allocation limit includes all four channels; scan lines are validated first. +pub fn decode_rgba(b: &[u8], limits: &Limits) -> Result { + let (width, height, rgba) = decode_channels::<4>(b, limits, MAX_ALLOC_BYTES)?; + Ok(RgbaPicture { width, height, rgba }) +} + +// Only the RGB and RGBA entry points call this helper. +fn decode_channels(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result<(u32, u32, Vec)> { if !sniff(b) { return Err(Error::NotHdr); } @@ -235,13 +260,16 @@ fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result limits.max_axis || height > limits.max_axis { + return Err(Error::LimitExceeded("axis length")); + } let pixels = u64::from(width) * u64::from(height); if pixels > limits.max_pixels { return Err(Error::LimitExceeded("pixel count")); } // One scan line of RGBE pixels and the float picture, each within the target's largest allocation. let scan_len = alloc_len(u64::from(if x1 { height } else { width }), size_of::<[u8; 4]>(), max_alloc_bytes); - let rgb_len = pixels.checked_mul(3).and_then(|floats| alloc_len(floats, size_of::(), max_alloc_bytes)); + let rgb_len = pixels.checked_mul(CHANNELS as u64).and_then(|floats| alloc_len(floats, size_of::(), max_alloc_bytes)); let (Some(scan_len), Some(total)) = (scan_len, rgb_len) else { return Err(Error::LimitExceeded("pixel count")) }; // A scan line takes at least a few bits of file, so a header cannot ask for more than the data has. if (b.len().saturating_sub(pos) as u64).saturating_mul(4096) < pixels { @@ -255,7 +283,7 @@ fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result Result(bytes, &Limits::default(), 28).is_ok()); + assert_eq!(decode_channels::<4>(bytes, &Limits::default(), 28), Err(Error::LimitExceeded("pixel count"))); +} + +#[test] +fn axis_limit_precedes_pixel_data_and_scratch_allocation() { + let limits = Limits { max_axis: 32768, ..Limits::default() }; + for resolution in ["-Y 1 +X 32769", "-Y 32769 +X 1", "+X 1 -Y 32769"] { + let bytes = format!("#?RADIANCE\n\n{resolution}\n"); + assert_eq!(decode_rgba(bytes.as_bytes(), &limits), Err(Error::LimitExceeded("axis length"))); + assert_eq!(decode(bytes.as_bytes(), &limits), Err(Error::LimitExceeded("axis length"))); + } +}