swf: Fix write_i24

It was writing in the wrong endian.
This commit is contained in:
relrelb 2021-06-24 12:09:26 +03:00 committed by relrelb
parent 1cbbdecccf
commit 32c6d8dba0
1 changed files with 18 additions and 4 deletions

View File

@ -127,10 +127,7 @@ impl<W: Write> Writer<W> {
fn write_i24(&mut self, n: i32) -> Result<()> {
let bytes = n.to_le_bytes();
debug_assert!(bytes[3] == 0 || bytes[3] == 0xFF);
self.write_u8(bytes[2])?;
self.write_u8(bytes[1])?;
self.write_u8(bytes[0])?;
Ok(())
self.output.write_all(&bytes[..3])
}
fn write_i32(&mut self, n: i32) -> Result<()> {
@ -1039,4 +1036,21 @@ pub mod tests {
}
}
}
#[test]
fn write_i24() {
let write = |n: i32| {
let mut out = vec![];
{
let mut writer = Writer::new(&mut out);
writer.write_i24(n).unwrap();
}
out
};
assert_eq!(write(0), &[0, 0, 0]);
assert_eq!(write(2), &[2, 0, 0]);
assert_eq!(write(77777), &[0b1101_0001, 0b0010_1111, 0b0000_0001]);
assert_eq!(write(-77777), &[0b0010_1111, 0b1101_0000, 0b1111_1110]);
}
}