Skip to main content

rhwp/wmf/parser/records/drawing/
poly_line.rs

1use crate::wmf::imports::*;
2
3/// The META_POLYLINE Record draws a series of line segments by connecting the
4/// points in the specified array.
5#[derive(Clone, Debug)]
6pub struct META_POLYLINE {
7    /// RecordSize (4 bytes): A 32-bit unsigned integer that defines the number
8    /// of WORD structures, defined in [MS-DTYP] section 2.2.61, in the WMF
9    /// record.
10    pub record_size: crate::wmf::parser::RecordSize,
11    /// RecordFunction (2 bytes): A 16-bit unsigned integer that defines this
12    /// WMF record type. The lower byte MUST match the lower byte of the
13    /// RecordType Enumeration table value META_POLYLINE.
14    pub record_function: u16,
15    /// NumberOfPoints (2 bytes): A 16-bit signed integer that defines the
16    /// number of points in the array.
17    pub number_of_points: i16,
18    /// aPoints (variable): A NumberOfPoints array of 32-bit PointS Objects, in
19    /// logical units.
20    pub a_points: Vec<crate::wmf::parser::PointS>,
21}
22
23impl META_POLYLINE {
24    #[cfg_attr(feature = "tracing", tracing::instrument(
25        level = tracing::Level::TRACE,
26        skip_all,
27        fields(
28            %record_size,
29            record_function = %format!("{record_function:#06X}"),
30        ),
31        err(level = tracing::Level::ERROR, Display),
32    ))]
33    pub fn parse<R: crate::wmf::Read>(
34        buf: &mut R,
35        mut record_size: crate::wmf::parser::RecordSize,
36        record_function: u16,
37    ) -> Result<Self, crate::wmf::parser::ParseError> {
38        crate::wmf::parser::records::check_lower_byte_matches(
39            record_function,
40            crate::wmf::parser::RecordType::META_POLYLINE,
41        )?;
42
43        let (number_of_points, number_of_points_bytes) =
44            crate::wmf::parser::read_i16_from_le_bytes(buf)?;
45        record_size.consume(number_of_points_bytes);
46
47        // `number_of_points` 는 i16 이라 손상된 WMF 가 음수를 담을 수 있다. 음수를
48        // `as usize` 로 넓히면 usize::MAX 근처가 되어 `Vec::with_capacity` 가
49        // capacity overflow 로 패닉한다(-1 → 18446744073709551615).
50        // 같은 결함을 Region 은 이미 이 방식으로 막는다(objects/graphics/region.rs:96).
51        if number_of_points < 0 {
52            return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
53                cause: format!(
54                    "The number_of_points field `{number_of_points}` must not be negative"
55                ),
56            });
57        }
58        let mut a_points = Vec::with_capacity(number_of_points as usize);
59
60        for _ in 0..number_of_points {
61            let (v, c) = crate::wmf::parser::PointS::parse(buf)?;
62
63            record_size.consume(c);
64            a_points.push(v);
65        }
66
67        crate::wmf::parser::records::consume_remaining_bytes(buf, record_size)?;
68
69        Ok(Self {
70            record_size,
71            record_function,
72            number_of_points,
73            a_points,
74        })
75    }
76}