diff --git a/sdk/src/asset_handlers/gif_io.rs b/sdk/src/asset_handlers/gif_io.rs index 6ae21b39f..fcc493dc8 100644 --- a/sdk/src/asset_handlers/gif_io.rs +++ b/sdk/src/asset_handlers/gif_io.rs @@ -1118,19 +1118,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)]