pub mod constants;
pub mod objects;
pub mod records;
pub use objects::header::Header;
use super::Error;
use records::Record;
const RT_HEADER: u32 = 0x00000001;
const RT_EOF: u32 = 0x0000000E;
const RT_SET_WINDOW_EXT_EX: u32 = 0x00000009;
const RT_SET_WINDOW_ORG_EX: u32 = 0x0000000A;
const RT_SET_VIEWPORT_EXT_EX: u32 = 0x0000000B;
const RT_SET_VIEWPORT_ORG_EX: u32 = 0x0000000C;
const RT_SET_MAP_MODE: u32 = 0x00000011;
const RT_SET_BK_MODE: u32 = 0x00000012;
const RT_SET_POLY_FILL_MODE: u32 = 0x00000013;
const RT_SET_BRUSH_ORG_EX: u32 = 0x0000000D;
const RT_SET_TEXT_ALIGN: u32 = 0x00000016;
const RT_SET_TEXT_COLOR: u32 = 0x00000018;
const RT_SET_BK_COLOR: u32 = 0x00000019;
const RT_SAVE_DC: u32 = 0x00000021;
const RT_RESTORE_DC: u32 = 0x00000022;
const RT_SET_WORLD_TRANSFORM: u32 = 0x00000023;
const RT_MODIFY_WORLD_TRANSFORM: u32 = 0x00000024;
const RT_SELECT_OBJECT: u32 = 0x00000025;
const RT_CREATE_PEN: u32 = 0x00000026;
const RT_CREATE_BRUSH_INDIRECT: u32 = 0x00000027;
const RT_EXT_CREATE_PEN: u32 = 0x0000005F;
const RT_CREATE_DIB_PATTERN_BRUSH: u32 = 0x0000005E;
const RT_INTERSECT_CLIP_RECT: u32 = 0x0000001E;
const RT_EXT_SELECT_CLIP_RGN: u32 = 0x0000004B;
const RT_DELETE_OBJECT: u32 = 0x00000028;
const RT_EXT_CREATE_FONT_INDIRECT_W: u32 = 0x00000052;
const RT_MOVE_TO_EX: u32 = 0x0000001B;
const RT_ELLIPSE: u32 = 0x0000002A;
const RT_RECTANGLE: u32 = 0x0000002B;
const RT_ROUND_RECT: u32 = 0x0000002C;
const RT_ARC: u32 = 0x0000002D;
const RT_CHORD: u32 = 0x0000002E;
const RT_PIE: u32 = 0x0000002F;
const RT_LINE_TO: u32 = 0x00000036;
const RT_POLYBEZIER16: u32 = 0x00000055;
const RT_POLYGON16: u32 = 0x00000056;
const RT_POLYLINE16: u32 = 0x00000057;
const RT_POLYBEZIER_TO16: u32 = 0x00000058;
const RT_POLYLINE_TO16: u32 = 0x00000059;
const RT_POLY_POLYGON16: u32 = 0x0000005B;
const RT_BEGIN_PATH: u32 = 0x0000003B;
const RT_END_PATH: u32 = 0x0000003C;
const RT_CLOSE_FIGURE: u32 = 0x0000003D;
const RT_FILL_PATH: u32 = 0x0000003E;
const RT_STROKE_AND_FILL_PATH: u32 = 0x0000003F;
const RT_STROKE_PATH: u32 = 0x00000040;
const RT_EXT_TEXT_OUT_W: u32 = 0x00000054;
const RT_STRETCH_DI_BITS: u32 = 0x00000051;
const RT_COMMENT: u32 = 0x00000046;
const RT_MAX: u32 = 0x0000007A;
const MAX_RESYNCS: usize = 8;
pub fn parse(bytes: &[u8]) -> Result<Vec<Record>, Error> {
let mut cursor = Cursor::new(bytes);
let mut out = Vec::new();
let first = cursor.peek_record_header()?;
if first.record_type != RT_HEADER {
return Err(Error::InvalidFirstRecord {
got: first.record_type,
});
}
let header_record = records::header::parse(&mut cursor)?;
out.push(Record::Header(header_record));
let mut emfplus_comment_seen = false;
let mut resyncs = 0usize;
while !cursor.is_empty() {
let record_start = cursor.position();
let step = parse_one_record(&mut cursor, &mut emfplus_comment_seen);
match step {
Ok(record) => {
let eof = matches!(record, Record::Eof);
out.push(record);
if eof {
break;
}
}
Err(err) => {
if emfplus_comment_seen && resyncs < MAX_RESYNCS {
if let Some(next) = find_resync(bytes, record_start + 4) {
resyncs += 1;
cursor = Cursor::new(bytes);
let _ = cursor.take(next)?;
continue;
}
}
if emfplus_comment_seen && out.iter().any(is_paintable) {
break;
}
return Err(err);
}
}
}
Ok(out)
}
fn is_paintable(record: &Record) -> bool {
matches!(
record,
Record::StretchDIBits(_)
| Record::ExtTextOutW(_)
| Record::Rectangle(_)
| Record::Ellipse(_)
| Record::RoundRect { .. }
| Record::Arc { .. }
| Record::Chord { .. }
| Record::Pie { .. }
| Record::LineTo(_)
| Record::Polyline16 { .. }
| Record::Polygon16 { .. }
| Record::PolyBezier16 { .. }
| Record::PolylineTo16 { .. }
| Record::PolyBezierTo16 { .. }
| Record::IntersectClipRect(_)
| Record::ExtSelectClipRgn { .. }
| Record::FillPath(_)
| Record::StrokePath(_)
| Record::StrokeAndFillPath(_)
)
}
fn parse_one_record(
cursor: &mut Cursor<'_>,
emfplus_comment_seen: &mut bool,
) -> Result<Record, Error> {
let rh = cursor.peek_record_header()?;
let record_start = cursor.position();
let payload_len = (rh.size as usize)
.checked_sub(8)
.ok_or(Error::RecordTooSmall {
offset: record_start,
size: rh.size,
})?;
let _ = cursor.take(8)?;
let payload = cursor.take(payload_len)?;
if rh.record_type == RT_COMMENT && payload.len() >= 8 && &payload[4..8] == b"EMF+" {
*emfplus_comment_seen = true;
}
let mut sub = Cursor::new(payload);
dispatch(rh.record_type, &mut sub, payload_len)
}
fn find_resync(bytes: &[u8], from: usize) -> Option<usize> {
let mut p = from.checked_add(3)? & !3usize;
while p + 8 <= bytes.len() {
if let Some(h) = plausible_header_at(bytes, p) {
if chain_is_plausible(bytes, p, &h, 2) || stretch_dibits_self_consistent(bytes, p, &h) {
return Some(p);
}
}
p += 4;
}
None
}
fn stretch_dibits_self_consistent(bytes: &[u8], pos: usize, header: &RecordHeader) -> bool {
if header.record_type != RT_STRETCH_DI_BITS {
return false;
}
let size = header.size as usize;
if size < 80 + 40 {
return false;
}
let f = |off: usize| -> usize {
let b = &bytes[pos + off..pos + off + 4];
u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize
};
let (off_bmi, cb_bmi, off_bits, cb_bits) = (f(48), f(52), f(56), f(60));
off_bmi >= 80
&& cb_bmi >= 40
&& cb_bits > 0
&& off_bmi.saturating_add(cb_bmi) <= size
&& off_bits.saturating_add(cb_bits) <= size
&& f(off_bmi) == 40
}
fn plausible_header_at(bytes: &[u8], pos: usize) -> Option<RecordHeader> {
let b = bytes.get(pos..pos + 8)?;
let record_type = u32::from_le_bytes([b[0], b[1], b[2], b[3]]);
let size = u32::from_le_bytes([b[4], b[5], b[6], b[7]]);
if !(1..=RT_MAX).contains(&record_type) {
return None;
}
if size < 8 || size % 4 != 0 {
return None;
}
if pos + size as usize > bytes.len() {
return None;
}
Some(RecordHeader { record_type, size })
}
fn chain_is_plausible(bytes: &[u8], pos: usize, header: &RecordHeader, depth: usize) -> bool {
if header.record_type == RT_EOF || depth == 0 {
return true;
}
let next = pos + header.size as usize;
if next == bytes.len() {
return true;
}
match plausible_header_at(bytes, next) {
Some(h) => chain_is_plausible(bytes, next, &h, depth - 1),
None => false,
}
}
fn dispatch(record_type: u32, c: &mut Cursor<'_>, payload_len: usize) -> Result<Record, Error> {
use records::{bitmap, drawing, object, path, state, text};
let rec = match record_type {
RT_EOF => Record::Eof,
RT_CREATE_PEN => {
let (handle, pen) = object::parse_create_pen(c)?;
Record::CreatePen { handle, pen }
}
RT_CREATE_BRUSH_INDIRECT => {
let (handle, brush) = object::parse_create_brush_indirect(c)?;
Record::CreateBrushIndirect { handle, brush }
}
RT_CREATE_DIB_PATTERN_BRUSH => {
let (handle, brush) = bitmap::parse_pattern_brush(c.full_buf())?;
Record::CreateDibPatternBrush { handle, brush }
}
RT_INTERSECT_CLIP_RECT => Record::IntersectClipRect(drawing::parse_rect(c)?),
RT_EXT_SELECT_CLIP_RGN => {
let cb_rgn_data = c.u32()?;
let mode = c.u32()?;
Record::ExtSelectClipRgn {
mode,
has_region: cb_rgn_data > 0,
}
}
RT_EXT_CREATE_PEN => {
let (handle, pen) = object::parse_ext_create_pen(c)?;
Record::CreatePen { handle, pen }
}
RT_EXT_CREATE_FONT_INDIRECT_W => {
let (handle, font) = object::parse_ext_create_font_indirect_w(c, payload_len)?;
Record::ExtCreateFontIndirectW { handle, font }
}
RT_SELECT_OBJECT => Record::SelectObject {
handle: object::parse_select_object(c)?,
},
RT_DELETE_OBJECT => Record::DeleteObject {
handle: object::parse_delete_object(c)?,
},
RT_SAVE_DC => Record::SaveDC,
RT_RESTORE_DC => Record::RestoreDC {
relative: state::parse_restore_dc(c)?,
},
RT_SET_WORLD_TRANSFORM => Record::SetWorldTransform(state::parse_set_world_transform(c)?),
RT_MODIFY_WORLD_TRANSFORM => {
let (xform, mode) = state::parse_modify_world_transform(c)?;
Record::ModifyWorldTransform { xform, mode }
}
RT_SET_MAP_MODE => Record::SetMapMode(state::parse_u32_single(c)?),
RT_SET_WINDOW_EXT_EX => Record::SetWindowExtEx(state::parse_set_window_ext_ex(c)?),
RT_SET_WINDOW_ORG_EX => Record::SetWindowOrgEx(state::parse_set_window_org_ex(c)?),
RT_SET_VIEWPORT_EXT_EX => Record::SetViewportExtEx(state::parse_set_viewport_ext_ex(c)?),
RT_SET_VIEWPORT_ORG_EX => Record::SetViewportOrgEx(state::parse_set_viewport_org_ex(c)?),
RT_SET_BK_MODE => Record::SetBkMode(state::parse_u32_single(c)?),
RT_SET_POLY_FILL_MODE => Record::SetPolyFillMode(state::parse_u32_single(c)?),
RT_SET_BRUSH_ORG_EX => Record::SetBrushOrgEx(drawing::parse_point(c)?),
RT_SET_TEXT_ALIGN => Record::SetTextAlign(state::parse_u32_single(c)?),
RT_SET_TEXT_COLOR => Record::SetTextColor(state::parse_u32_single(c)?),
RT_SET_BK_COLOR => Record::SetBkColor(state::parse_u32_single(c)?),
RT_MOVE_TO_EX => Record::MoveToEx(drawing::parse_point(c)?),
RT_LINE_TO => Record::LineTo(drawing::parse_point(c)?),
RT_RECTANGLE => Record::Rectangle(drawing::parse_rect(c)?),
RT_ELLIPSE => Record::Ellipse(drawing::parse_rect(c)?),
RT_ROUND_RECT => {
let (rect, corner_w, corner_h) = drawing::parse_round_rect(c)?;
Record::RoundRect {
rect,
corner_w,
corner_h,
}
}
RT_ARC => {
let (r, s, e) = drawing::parse_arc_like(c)?;
Record::Arc {
rect: r,
start: s,
end: e,
}
}
RT_CHORD => {
let (r, s, e) = drawing::parse_arc_like(c)?;
Record::Chord {
rect: r,
start: s,
end: e,
}
}
RT_PIE => {
let (r, s, e) = drawing::parse_arc_like(c)?;
Record::Pie {
rect: r,
start: s,
end: e,
}
}
RT_POLYLINE16 => {
let (bounds, points) = drawing::parse_points16(c)?;
Record::Polyline16 { bounds, points }
}
RT_POLYGON16 => {
let (bounds, points) = drawing::parse_points16(c)?;
Record::Polygon16 { bounds, points }
}
RT_POLY_POLYGON16 => {
let (bounds, polygons) = drawing::parse_poly_polygon16(c)?;
Record::PolyPolygon16 { bounds, polygons }
}
RT_POLYBEZIER16 => {
let (bounds, points) = drawing::parse_points16(c)?;
Record::PolyBezier16 { bounds, points }
}
RT_POLYBEZIER_TO16 => {
let (bounds, points) = drawing::parse_points16(c)?;
Record::PolyBezierTo16 { bounds, points }
}
RT_POLYLINE_TO16 => {
let (bounds, points) = drawing::parse_points16(c)?;
Record::PolylineTo16 { bounds, points }
}
RT_BEGIN_PATH => Record::BeginPath,
RT_END_PATH => Record::EndPath,
RT_CLOSE_FIGURE => Record::CloseFigure,
RT_FILL_PATH => Record::FillPath(path::parse_path_bounds(c)?),
RT_STROKE_PATH => Record::StrokePath(path::parse_path_bounds(c)?),
RT_STROKE_AND_FILL_PATH => Record::StrokeAndFillPath(path::parse_path_bounds(c)?),
RT_EXT_TEXT_OUT_W => {
let payload = c.full_buf();
Record::ExtTextOutW(text::parse(payload)?)
}
RT_STRETCH_DI_BITS => {
let payload = c.full_buf();
Record::StretchDIBits(bitmap::parse(payload)?)
}
_ => Record::Unknown {
record_type,
payload: c.take(payload_len)?.to_vec(),
},
};
Ok(rec)
}
#[derive(Debug, Clone, Copy)]
pub struct RecordHeader {
pub record_type: u32,
pub size: u32,
}
pub struct Cursor<'a> {
buf: &'a [u8],
pos: usize,
}
impl<'a> Cursor<'a> {
#[inline]
pub fn new(buf: &'a [u8]) -> Self {
Self { buf, pos: 0 }
}
#[inline]
pub fn position(&self) -> usize {
self.pos
}
#[inline]
pub fn remaining(&self) -> usize {
self.buf.len() - self.pos
}
#[inline]
pub fn is_empty(&self) -> bool {
self.pos >= self.buf.len()
}
pub fn take(&mut self, n: usize) -> Result<&'a [u8], Error> {
if self.remaining() < n {
return Err(Error::UnexpectedEof {
at: self.pos,
need: n,
});
}
let s = &self.buf[self.pos..self.pos + n];
self.pos += n;
Ok(s)
}
pub fn peek(&self, n: usize) -> Result<&'a [u8], Error> {
if self.remaining() < n {
return Err(Error::UnexpectedEof {
at: self.pos,
need: n,
});
}
Ok(&self.buf[self.pos..self.pos + n])
}
pub fn u32(&mut self) -> Result<u32, Error> {
let b = self.take(4)?;
Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
}
pub fn i32(&mut self) -> Result<i32, Error> {
Ok(self.u32()? as i32)
}
pub fn u16(&mut self) -> Result<u16, Error> {
let b = self.take(2)?;
Ok(u16::from_le_bytes([b[0], b[1]]))
}
#[must_use]
pub fn full_buf(&self) -> &'a [u8] {
self.buf
}
pub fn peek_record_header(&self) -> Result<RecordHeader, Error> {
let b = self.peek(8)?;
let record_type = u32::from_le_bytes([b[0], b[1], b[2], b[3]]);
let size = u32::from_le_bytes([b[4], b[5], b[6], b[7]]);
if size < 8 {
return Err(Error::RecordTooSmall {
offset: self.pos,
size,
});
}
if size % 4 != 0 {
return Err(Error::MisalignedRecord {
offset: self.pos,
size,
});
}
Ok(RecordHeader { record_type, size })
}
}