use crate::wmf::imports::*;
#[cfg(test)]
mod tests {
use super::Region;
#[test]
fn negative_scan_count_returns_error_instead_of_panicking() {
let mut bytes = Vec::new();
bytes.extend_from_slice(&0u16.to_le_bytes()); bytes.extend_from_slice(&0x0006i16.to_le_bytes()); bytes.extend_from_slice(&0u32.to_le_bytes()); bytes.extend_from_slice(&0i16.to_le_bytes()); bytes.extend_from_slice(&(-1i16).to_le_bytes()); bytes.extend_from_slice(&0i16.to_le_bytes()); bytes.extend_from_slice(&0i16.to_le_bytes()); bytes.extend_from_slice(&0i16.to_le_bytes()); bytes.extend_from_slice(&0i16.to_le_bytes()); bytes.extend_from_slice(&0i16.to_le_bytes());
let mut data: &[u8] = &bytes;
let result = Region::parse(&mut data);
assert!(result.is_err(), "negative scan_count must be rejected");
}
}
#[derive(Clone, Debug)]
pub struct Region {
pub next_in_chain: u16,
pub object_type: i16,
pub object_count: u32,
pub size: i16,
pub scan_count: i16,
pub max_scan: i16,
pub bounding_rectangle: crate::wmf::parser::Rect,
pub a_scans: Vec<crate::wmf::parser::Scan>,
}
impl Region {
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip_all,
err(level = tracing::Level::ERROR, Display),
))]
pub fn parse<R: crate::wmf::Read>(
buf: &mut R,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
let (
(next_in_chain, next_in_chain_bytes),
(object_type, object_type_bytes),
(object_count, object_count_bytes),
(size, size_bytes),
(scan_count, scan_count_bytes),
(max_scan, max_scan_bytes),
(bounding_rectangle, bounding_rectangle_bytes),
) = (
crate::wmf::parser::read_u16_from_le_bytes(buf)?,
crate::wmf::parser::read_i16_from_le_bytes(buf)?,
crate::wmf::parser::read_u32_from_le_bytes(buf)?,
crate::wmf::parser::read_i16_from_le_bytes(buf)?,
crate::wmf::parser::read_i16_from_le_bytes(buf)?,
crate::wmf::parser::read_i16_from_le_bytes(buf)?,
crate::wmf::parser::Rect::parse(buf)?,
);
let mut consumed_bytes = next_in_chain_bytes
+ object_type_bytes
+ object_count_bytes
+ size_bytes
+ scan_count_bytes
+ max_scan_bytes
+ bounding_rectangle_bytes;
if scan_count < 0 {
return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
cause: format!("The scan_count field `{scan_count}` must not be negative"),
});
}
let mut a_scans = Vec::with_capacity(scan_count as usize);
for _ in 0..scan_count {
let (v, c) = crate::wmf::parser::Scan::parse(buf)?;
consumed_bytes += c;
a_scans.push(v);
}
if object_type != 0x0006 {
return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
cause: "The object_type field must be 0x0006".to_owned(),
});
}
Ok((
Self {
next_in_chain,
object_type,
object_count,
size,
scan_count,
max_scan,
bounding_rectangle,
a_scans,
},
consumed_bytes,
))
}
}