From ab1b93f5ca52a8c30ada9d0bc459229d38750d05 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Thu, 23 Jan 2025 21:35:42 -0800 Subject: [PATCH] rust: Fix Unity loading The deku::byte_offset trick we do for alignment requires reader.bits_read to be correct, as that's the number we pad by. In our PackedVec implementations, we read a u8, and then later skip over it, which double-counts from the perspective of reader.bits_read. Fix this by adjusting after we read it. --- rust/src/unity/types/common.rs | 2 ++ rust/src/unity/util.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rust/src/unity/types/common.rs b/rust/src/unity/types/common.rs index 8843f6881..910d29984 100644 --- a/rust/src/unity/types/common.rs +++ b/rust/src/unity/types/common.rs @@ -226,6 +226,7 @@ impl<'a, Ctx> DekuReader<'a, Ctx> for Packedi32Vec where Ctx: Clone { reader.seek(SeekFrom::Current(byte_array_count as i64)).unwrap(); let bit_size: u8 = u8::from_reader_with_ctx(reader, ())?; reader.seek(SeekFrom::Current(-(byte_array_count as i64) - 1)).unwrap(); + reader.bits_read -= 8; let data = unpack_i32s(reader, num_items as usize, bit_size as usize)?; reader.skip_bits(4 * 8)?; // bit_size, padding @@ -255,6 +256,7 @@ impl<'a, Ctx> DekuReader<'a, Ctx> for Packedf32Vec where Ctx: Clone { reader.seek(SeekFrom::Current(byte_array_count as i64)).unwrap(); let bit_size = u8::from_reader_with_ctx(reader, ())?; reader.seek(SeekFrom::Current(-(byte_array_count as i64) - 1)).unwrap(); + reader.bits_read -= 8; let max = ((1 << bit_size) as f32) - 1.0; let ints = unpack_i32s(reader, num_items as usize, bit_size as usize)?; diff --git a/rust/src/unity/util.rs b/rust/src/unity/util.rs index 9564dab98..662891f6d 100644 --- a/rust/src/unity/util.rs +++ b/rust/src/unity/util.rs @@ -5,7 +5,7 @@ pub fn deku_peek<'a, T, R: deku::no_std_io::Read + deku::no_std_io::Seek>(reader where for<'b> T: DekuReader<'b, ()> + Debug { println!("deku_peek - {}", msg); - println!(" offset: {}", reader.bits_read); + println!(" offset: {} / {}", reader.bits_read >> 3, reader.bits_read); match T::from_reader_with_ctx(reader, ()) { Ok(value) => { println!(" value: {:?}", value);