From fc0f03a6870dc71a23a66ace939346e42c9180b0 Mon Sep 17 00:00:00 2001 From: sleepycatcoding <131554884+sleepycatcoding@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:24:31 +0300 Subject: [PATCH] swf: Fix writing of a DefineButton2 with zero actions Previously, `swf` crate always wrote out the action offset, but this should be 0 when there are no actions. --- swf/src/write.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/swf/src/write.rs b/swf/src/write.rs index 26e06222f3d9..e8c08fd8a271 100644 --- a/swf/src/write.rs +++ b/swf/src/write.rs @@ -973,7 +973,14 @@ impl Writer { } writer_2.write_u8(0)?; // End button records } - writer.write_u16(record_data.len() as u16 + 2)?; + + // Write action offset, this is explicitly 0 when there are no actions. + if button.actions.is_empty() { + writer.write_u16(0)?; + } else { + writer.write_u16(record_data.len() as u16 + 2)?; + } + writer.output.write_all(&record_data)?; let mut iter = button.actions.iter().peekable(); @@ -2797,4 +2804,36 @@ mod tests { assert_eq!(tag, Tag::DefineFont2(Box::new(font))); } + + #[test] + fn write_define_button_2() { + use crate::read::Reader; + + let button = Button { + id: 3, + is_track_as_menu: false, + records: vec![ButtonRecord { + states: ButtonState::UP + | ButtonState::OVER + | ButtonState::DOWN + | ButtonState::HIT_TEST, + id: 2, + depth: 1, + matrix: Matrix::translate(Twips::from_pixels(2.0), Twips::from_pixels(3.0)), + color_transform: ColorTransform::default(), + filters: vec![], + blend_mode: BlendMode::Normal, + }], + actions: vec![], + }; + + let mut buf = Vec::new(); + let mut writer = Writer::new(&mut buf, 15); + writer.write_define_button_2(&button).unwrap(); + + let mut reader = Reader::new(&buf, 15); + let tag = reader.read_tag().unwrap(); + + assert_eq!(tag, Tag::DefineButton2(Box::new(button))); + } }