Skip to content

Commit

Permalink
Fix decoding of GIF chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 23, 2025
1 parent 6d1ab01 commit b1e9bbe
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions sdk/src/asset_handlers/gif_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,19 +1117,26 @@ impl DataSubBlocks {
}

fn to_decoded_bytes(&self) -> Vec<u8> {
// 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<Item = &[u8]> {
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)]
Expand Down

0 comments on commit b1e9bbe

Please sign in to comment.