From c6dc68939a9516a1f06d57fb1941f5f95aae2440 Mon Sep 17 00:00:00 2001 From: "Claire V. Hammond" <61138239+cvhammond@users.noreply.github.com> Date: Sun, 20 Aug 2023 19:04:23 -0700 Subject: [PATCH] fixed data block off-by-one error --- src/c3d.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/c3d.rs b/src/c3d.rs index e032d2f..23bcc5d 100644 --- a/src/c3d.rs +++ b/src/c3d.rs @@ -101,10 +101,10 @@ impl C3d { .u16([self.bytes.header[16], self.bytes.header[17]]) as usize; let mut parameter_bytes_tail = Vec::with_capacity( - (self.bytes.data_start_block_index - self.bytes.parameter_start_block_index - 1) * 512, + (self.bytes.data_start_block_index - self.bytes.parameter_start_block_index) * 512, ) as Vec; - for _ in 0..(self.bytes.data_start_block_index - self.bytes.parameter_start_block_index - 1) + for _ in 0..(self.bytes.data_start_block_index - self.bytes.parameter_start_block_index) { let mut block = [0u8; 512]; file.read_exact(&mut block) @@ -133,7 +133,7 @@ impl C3d { self.bytes.parameter_start_block_index = self.bytes.header[0] as usize; - if bytes.len() < 512 * (self.bytes.parameter_start_block_index + 1) { + if bytes.len() < 512 * (self.bytes.parameter_start_block_index) { return Err(C3dParseError::InsufficientBlocks("parameter".to_string())); } let parameter_start_block = &bytes[512..1024]; @@ -144,11 +144,11 @@ impl C3d { self.processor .u16([self.bytes.header[16], self.bytes.header[17]]) as usize; - if bytes.len() < 512 * (self.bytes.data_start_block_index + 1) { + if bytes.len() < 512 * (self.bytes.data_start_block_index) { return Err(C3dParseError::InsufficientBlocks("data".to_string())); } - self.bytes.parameter = bytes[512..(512 * (self.bytes.data_start_block_index + 1))] + self.bytes.parameter = bytes[512..(512 * (self.bytes.data_start_block_index))] .try_into() .unwrap(); @@ -203,7 +203,7 @@ impl C3d { } fn parse_data_from_bytes(mut self, bytes: &[u8]) -> Result { - let data_start_byte = 512 * (self.bytes.data_start_block_index + 1); + let data_start_byte = 512 * (self.bytes.data_start_block_index); if bytes.len() < data_start_byte { return Err(C3dParseError::InsufficientBlocks("data".to_string())); }