#[cfg(feature = "zip")]
mod spec;
#[cfg(feature = "zip")]
mod spec_shape;
#[cfg(feature = "zip")]
mod unzip;
#[cfg(all(test, feature = "zip"))]
mod tests;
use crate::{
Container, Document, Limits, Movie, ValueKind,
error::{Error, Result},
};
const IMAGE_EXTENSION: &str = ".png";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Format {
Zlib,
ZipBinary,
ZipSpec,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ArchiveFile {
pub name: String,
pub bytes: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct Animation {
format: Format,
movie: Movie,
document: Option<Document>,
files: Vec<ArchiveFile>,
names: Vec<(String, String)>,
limits: Limits,
}
impl Animation {
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
Self::from_bytes_with(bytes, &Limits::default())
}
pub fn from_bytes_with(bytes: &[u8], limits: &Limits) -> Result<Self> {
if Container::detect(bytes) == Some(Container::Zip) {
return Self::from_zip(bytes, limits);
}
let document = Document::from_bytes_with(bytes, limits)?;
Ok(Self {
format: Format::Zlib,
movie: document.movie_with(limits)?,
document: Some(document),
files: Vec::new(),
names: Vec::new(),
limits: *limits,
})
}
#[cfg(not(feature = "zip"))]
fn from_zip(_: &[u8], _: &Limits) -> Result<Self> {
Err(Error::unsupported("svga_zip_support_disabled"))
}
#[cfg(feature = "zip")]
fn from_zip(bytes: &[u8], limits: &Limits) -> Result<Self> {
let files = unzip::extract(bytes, limits)?;
let find = |name: &str| files.iter().find(|file| file.name == name);
if let Some(binary) = find(unzip::MOVIE_BINARY) {
let extracted: usize = files.iter().map(|file| file.bytes.len()).sum();
let document = unzip::binary_document(&binary.bytes, limits, extracted)?;
return Ok(Self {
format: Format::ZipBinary,
movie: document.movie_with(limits)?,
document: Some(document),
files,
names: Vec::new(),
limits: *limits,
});
}
let spec = find(unzip::MOVIE_SPEC).ok_or(Error::malformed("svga_zip_without_movie"))?;
let (movie, names) = spec::movie(&spec.bytes, &files, limits)?;
Ok(Self {
format: Format::ZipSpec,
movie,
document: None,
files,
names,
limits: *limits,
})
}
pub fn format(&self) -> Format {
self.format
}
pub fn movie(&self) -> &Movie {
&self.movie
}
pub fn into_movie(self) -> Movie {
self.movie
}
pub fn image_kind(&self, key: &str) -> Option<ValueKind> {
self.image(key).map(ValueKind::sniff)
}
pub fn document(&self) -> Option<&Document> {
self.document.as_ref()
}
pub fn image(&self, key: &str) -> Option<&[u8]> {
match self.document.as_ref().map(|document| document.image(key)) {
Some(Some(value)) if ValueKind::sniff(value) == ValueKind::FileName => {
let name = std::str::from_utf8(value).ok()?;
self.file(name).or(Some(value))
}
Some(stored) => stored,
None => {
let named = self.names.iter().find(|(known, _)| known == key);
named.and_then(|(_, name)| self.file(name))
}
}
}
fn file(&self, name: &str) -> Option<&[u8]> {
find_file(&self.files, name)
}
pub fn to_document(&self) -> Result<Document> {
let max_bytes = self.limits.max_inflated_bytes;
let Some(document) = &self.document else {
let images: Vec<(&str, &[u8])> = self
.movie
.images
.iter()
.filter_map(|image| Some((image.key.as_str(), self.image(&image.key)?)))
.collect();
let embedded = images
.iter()
.try_fold(0usize, |total, (_, bytes)| total.checked_add(bytes.len()));
if embedded.is_none_or(|total| total > max_bytes) {
return Err(Error::limit("svga_inflated_size_exceeds_limit"));
}
return Document::from_movie(&self.movie, &images);
};
document.with_embedded(|name| self.file(name), max_bytes)
}
}
pub(crate) fn find_file<'a>(files: &'a [ArchiveFile], name: &str) -> Option<&'a [u8]> {
let with_extension = format!("{name}{IMAGE_EXTENSION}");
let named = |wanted: &str| files.iter().find(|file| file.name == wanted);
named(name)
.or_else(|| named(&with_extension))
.map(|file| file.bytes.as_slice())
}