From e5d9f9febaaa69768630f870f7286f307a7e2d1d Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 23 Jan 2025 11:56:36 +0000 Subject: [PATCH] fix: decoding of GIF chunks --- sdk/src/asset_handlers/gif_io.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/sdk/src/asset_handlers/gif_io.rs b/sdk/src/asset_handlers/gif_io.rs index d84e122e6..a4f5a4997 100644 --- a/sdk/src/asset_handlers/gif_io.rs +++ b/sdk/src/asset_handlers/gif_io.rs @@ -1117,19 +1117,26 @@ impl DataSubBlocks { } fn to_decoded_bytes(&self) -> Vec { - // Amount of bytes - (length markers + terminator). - let mut bytes = Vec::with_capacity(self.bytes.len() - (self.bytes.len().div_ceil(255) + 1)); - for chunk in self.bytes.chunks(256) { - bytes.extend_from_slice(&chunk[1..]); + let mut bytes = Vec::with_capacity(gif_chunks(&self.bytes).map(|c| c.len()).sum()); + for chunk in gif_chunks(&self.bytes) { + bytes.extend_from_slice(chunk); } - - // Remove terminator. - bytes.truncate(bytes.len() - 1); - bytes } } +fn gif_chunks(mut encoded_bytes: &[u8]) -> impl Iterator { + std::iter::from_fn(move || { + let (&len, rest) = encoded_bytes.split_first()?; + if len == 0 { + return None; + } + let (chunk, rest) = rest.split_at_checked(len.into())?; + encoded_bytes = rest; + Some(chunk) + }) +} + #[cfg(test)] mod tests { #![allow(clippy::unwrap_used)]