use std::{borrow::Cow, sync::Arc};
use crate::{
container_api::{
BoxFuture, ContainerHandler, Existing, FormatSpec, Io, ItemInfo, MetadataSlot, PlanBuilder,
StructureBuilder, WritePlan,
},
io::Reader,
jumbf::superbox_type,
ByteRange, Error, ItemKind, Name, Result, Segment, SegmentId, SegmentKind, Structure, Updates,
};
pub const ID: &str = "gif";
const MEDIA_TYPE: &str = "image/gif";
const EXTENSION: u8 = 0x21;
const APPLICATION: u8 = 0xff;
const IMAGE: u8 = 0x2c;
const TRAILER: u8 = 0x3b;
const HEADER: Name = Name::FourCC(*b"GIFh");
const SCREEN: Name = Name::FourCC(*b"LSD ");
const IMAGE_DATA: Name = Name::FourCC(*b"TBID");
const fn extension(label: u8) -> Name {
const HEX: &[u8; 16] = b"0123456789ABCDEF";
Name::FourCC([
b'2',
b'1',
HEX[(label >> 4) as usize],
HEX[(label & 0xf) as usize],
])
}
const C2PA_APP: &[u8; 11] = b"C2PA_GIF\x01\x00\x00";
const XMP_APP: &[u8; 11] = b"XMP DataXMP";
const INTRO_LEN: u32 = 14;
const MAX_SUB_BLOCK: usize = 255;
static LENGTHS: [u8; 256] = {
let mut t = [0u8; 256];
let mut i = 0;
while i < 256 {
t[i] = i as u8;
i += 1;
}
t
};
static XMP_TRAILER: [u8; 257] = {
let mut t = [0u8; 257];
t[0] = 1;
let mut i = 1;
while i < 257 {
t[i] = (256 - i) as u8;
i += 1;
}
t
};
#[derive(Debug, Clone, Copy, Default)]
pub struct GifHandler;
pub fn handler() -> Arc<dyn ContainerHandler> {
Arc::new(GifHandler)
}
impl ContainerHandler for GifHandler {
fn id(&self) -> &'static str {
ID
}
fn formats(&self) -> &[FormatSpec] {
const F: &[FormatSpec] = &[FormatSpec::new(MEDIA_TYPE, "gif")];
F
}
fn detect(&self, header: &[u8]) -> bool {
header.starts_with(b"GIF87a") || header.starts_with(b"GIF89a")
}
fn box_name(&self, name: Name) -> Option<Cow<'static, str>> {
if name == HEADER {
return Some(Cow::Borrowed("GIF89a"));
}
if name == SCREEN {
return Some(Cow::Borrowed("LSD"));
}
match name {
Name::None => None,
Name::Marker(m) => Some(Cow::Owned(format!("{m:02X}"))),
other => Some(Cow::Owned(other.to_string())),
}
}
fn c2pa_box_continues(&self, _: &Structure, _: SegmentId, element: &Segment) -> bool {
element.kind() == SegmentKind::Metadata && element.name().is_none()
}
fn parse_async(self: Arc<Self>, io: Io) -> BoxFuture<'static, Result<Structure>> {
Box::pin(parse_with(self, io))
}
fn write_plan_async<'f, 'a: 'f>(
&'f self,
src: &'f Structure,
mut io: Io,
updates: &'a Updates,
) -> BoxFuture<'f, Result<WritePlan<'a>>> {
Box::pin(async move {
let mut head = [0u8; 6];
io.read_at(0, &mut head).await?;
plan(src, &head == b"GIF87a", updates)
})
}
fn compose(&self, kind: ItemKind, data: &[u8]) -> Result<Vec<u8>> {
let id = app_id(kind).ok_or(Error::UnsupportedFormat)?;
let mut out = Vec::with_capacity(16 + data.len() + data.len() / MAX_SUB_BLOCK + 260);
out.extend_from_slice(&[EXTENSION, APPLICATION, 11]);
out.extend_from_slice(id);
for (chunk, _) in sub_blocks(kind, data) {
out.push(chunk.len() as u8);
out.extend_from_slice(chunk);
}
out.push(0);
Ok(out)
}
}
fn app_id(kind: ItemKind) -> Option<&'static [u8; 11]> {
match kind {
ItemKind::Jumbf => Some(C2PA_APP),
ItemKind::Xmp => Some(XMP_APP),
_ => None,
}
}
fn sub_blocks(kind: ItemKind, data: &[u8]) -> impl Iterator<Item = (&[u8], bool)> {
let trailer: &'static [u8] = if kind == ItemKind::Xmp {
&XMP_TRAILER
} else {
&[]
};
data.chunks(MAX_SUB_BLOCK)
.map(|c| (c, true))
.chain(trailer.chunks(MAX_SUB_BLOCK).map(|c| (c, false)))
}
fn color_table(packed: u8) -> u64 {
if packed & 0x80 != 0 {
3 << ((packed & 7) + 1)
} else {
0
}
}
async fn parse_with(handler: Arc<dyn ContainerHandler>, mut io: Io) -> Result<Structure> {
let total = io.len();
if total < 13 {
return Err(Error::InvalidFormat("GIF too small".into()));
}
let mut r = io.reader(0);
let head = r.array::<13>().await?;
if &head[..6] != b"GIF87a" && &head[..6] != b"GIF89a" {
return Err(Error::InvalidFormat("Not a GIF file".into()));
}
let mut b = StructureBuilder::new(handler, MEDIA_TYPE, total);
b.push_span(SegmentKind::Preamble, HEADER, 6, 0, 0)?;
b.push_span(SegmentKind::Preamble, SCREEN, 7, color_table(head[10]), 0)?;
loop {
let pos = b.cursor();
match read_byte(&mut r, pos, total).await? {
TRAILER => {
b.push_span(SegmentKind::Other, Name::Marker(TRAILER), 1, 0, 0)?;
break;
}
IMAGE => {
r.seek(pos);
let d = r.array::<10>().await?;
b.push_span(
SegmentKind::MediaData,
Name::Marker(IMAGE),
10,
color_table(d[9]),
0,
)?;
let at = b.cursor();
let end = skip_sub_blocks(&mut r, at + 1, total).await?;
b.push_span(SegmentKind::MediaData, IMAGE_DATA, 1, end - at - 1, 0)?;
}
EXTENSION => {
let label = read_byte(&mut r, pos + 1, total).await?;
let kind = if label == APPLICATION {
app_kind(&mut r, pos + 2, total).await?
} else {
None
};
match kind {
Some(kind) => app_block(&mut b, &mut r, kind, pos, total).await?,
None => {
let end = skip_sub_blocks(&mut r, pos + 2, total).await?;
b.push_span(SegmentKind::Other, extension(label), 2, end - pos - 2, 0)?;
}
}
}
other => {
return Err(Error::invalid_segment(
pos,
format!("unknown GIF block 0x{other:02x}"),
))
}
}
}
b.end_boxes()?;
b.push_tail()?;
b.finish()
}
async fn read_byte(r: &mut Reader<'_>, at: u64, total: u64) -> Result<u8> {
if at >= total {
return Err(Error::invalid_segment(at, "GIF truncated"));
}
r.seek(at);
r.u8().await
}
async fn skip_sub_blocks(r: &mut Reader<'_>, mut at: u64, total: u64) -> Result<u64> {
loop {
let n = read_byte(r, at, total).await?;
at += 1 + u64::from(n);
if n == 0 {
return Ok(at);
}
}
}
async fn app_kind(r: &mut Reader<'_>, at: u64, total: u64) -> Result<Option<ItemKind>> {
if total.saturating_sub(at) < 12 {
return Ok(None);
}
r.seek(at);
let a = r.array::<12>().await?;
Ok(match (a[0], &a[1..]) {
(11, id) if id == C2PA_APP => Some(ItemKind::Jumbf),
(11, id) if id == XMP_APP => Some(ItemKind::Xmp),
_ => None,
})
}
async fn app_block(
b: &mut StructureBuilder,
r: &mut Reader<'_>,
kind: ItemKind,
pos: u64,
total: u64,
) -> Result<()> {
let mut at = pos + u64::from(INTRO_LEN);
let mut n = read_byte(r, at, total).await?;
if n == 0 {
b.push_span(
SegmentKind::Metadata,
extension(APPLICATION),
INTRO_LEN,
0,
1,
)?;
return Ok(());
}
let mut parts = Vec::new();
loop {
let next_at = at + 1 + u64::from(n);
let next = read_byte(r, next_at, total).await?;
let (name, header) = match parts.is_empty() {
true => (extension(APPLICATION), INTRO_LEN + 1),
false => (Name::None, 1),
};
let id = b.push_span(
SegmentKind::Metadata,
name,
header,
u64::from(n),
u16::from(next == 0),
)?;
parts.push((id, b.segment(id)?.payload()));
if next == 0 {
break;
}
(at, n) = (next_at, next);
}
if kind == ItemKind::Jumbf {
let first = parts[0].1;
let ty = if first.len() >= 32 {
r.seek(first.offset());
superbox_type(r.take(32).await?)
} else {
None
};
return b.item(ItemInfo::jumbf().jumbf_type(ty), &parts);
}
match xmp_extents(r, &parts).await? {
Some(extents) => b.item(ItemInfo::xmp(), &extents),
None => Ok(()),
}
}
async fn xmp_extents(
r: &mut Reader<'_>,
parts: &[(SegmentId, ByteRange)],
) -> Result<Option<Vec<(SegmentId, ByteRange)>>> {
let len: u64 = parts.iter().map(|(_, p)| p.len()).sum();
let trailer = XMP_TRAILER.len();
if len <= trailer as u64 {
return Ok(None);
}
let mut tail = [0u8; 257];
let mut need = trailer;
for (_, p) in parts.iter().rev() {
if need == 0 {
break;
}
let take = (p.len() as usize).min(need);
r.seek(p.end() - take as u64);
r.read_exact(&mut tail[need - take..need]).await?;
need -= take;
}
if tail != XMP_TRAILER {
return Ok(None);
}
let mut keep = len - trailer as u64;
let mut extents = Vec::new();
for &(id, p) in parts {
if keep == 0 {
break;
}
let take = p.len().min(keep);
extents.push((id, p.sub(0, take)?));
keep -= take;
}
Ok(Some(extents))
}
enum Op<'a> {
Copy(SegmentId),
New(ItemKind, &'a [u8]),
}
fn plan<'a>(src: &Structure, gif87a: bool, updates: &'a Updates) -> Result<WritePlan<'a>> {
let segs = src.segments();
let mut xmp = MetadataSlot::new(updates.xmp());
let mut jumbf = MetadataSlot::new(updates.jumbf());
let mut ops = Vec::with_capacity(segs.len() + 2);
let (mut changed, mut after_screen) = (false, None);
let mut i = 0;
while i < segs.len() {
let id = SegmentId::from_index(i)?;
if segs[i].kind() != SegmentKind::Metadata {
ops.push(Op::Copy(id));
if segs[i].name() == SCREEN {
after_screen = Some(ops.len());
}
i += 1;
continue;
}
let mut end = i;
while segs[end].trailer().is_empty() && end + 1 < segs.len() {
end += 1;
}
let slot = match src.items_in(id).next().map(|e| e.kind()) {
Some(ItemKind::Xmp) => Some((ItemKind::Xmp, &mut xmp)),
Some(ItemKind::Jumbf) => Some((ItemKind::Jumbf, &mut jumbf)),
_ => None,
};
match slot.map(|(kind, s)| (kind, s.existing())) {
None | Some((_, Existing::Keep)) => {
for k in i..=end {
ops.push(Op::Copy(SegmentId::from_index(k)?));
}
}
Some((kind, Existing::Replace(d))) => {
changed = true;
ops.push(Op::New(kind, d));
}
Some(_) => changed = true,
}
i = end + 1;
}
let adds: Vec<Op<'a>> = [
(ItemKind::Jumbf, jumbf.pending()),
(ItemKind::Xmp, xmp.pending()),
]
.into_iter()
.filter_map(|(kind, d)| d.filter(|d| !d.is_empty()).map(|d| Op::New(kind, d)))
.collect();
if !adds.is_empty() {
let at = after_screen.ok_or(Error::NotFound {
what: "GIF logical screen descriptor",
})?;
changed = true;
let rest = ops.split_off(at);
ops.extend(adds);
ops.extend(rest);
}
let mut b = PlanBuilder::new(src);
if !changed {
for i in 0..segs.len() {
b.copy_segment(SegmentId::from_index(i)?)?;
}
b.carry_items(|_| true);
return b.finish();
}
let upgrade = gif87a && ops.iter().any(|op| matches!(op, Op::New(..)));
for op in ops {
match op {
Op::Copy(id) if upgrade && id.index() == 0 => {
b.put_segment(
SegmentKind::Preamble,
HEADER,
&b"GIF89a"[..],
&[][..],
&[][..],
)?;
}
Op::Copy(id) => {
b.copy_segment(id)?;
}
Op::New(kind, data) => put_block(&mut b, kind, data)?,
}
}
b.carry_items(|_| true);
b.finish()
}
fn put_block<'a>(b: &mut PlanBuilder<'_, 'a>, kind: ItemKind, data: &'a [u8]) -> Result<()> {
let app = app_id(kind).ok_or(Error::UnsupportedFormat)?;
if data.is_empty() {
return Ok(());
}
let parts: Vec<(&'a [u8], bool)> = sub_blocks(kind, data).collect();
let mut content = Vec::with_capacity(parts.len());
for (i, &(chunk, is_content)) in parts.iter().enumerate() {
let n = chunk.len();
let (name, header): (Name, Cow<'a, [u8]>) = if i == 0 {
let mut h = vec![EXTENSION, APPLICATION, 11];
h.extend_from_slice(app);
h.push(n as u8);
(extension(APPLICATION), Cow::Owned(h))
} else {
(Name::None, Cow::Borrowed(&LENGTHS[n..=n]))
};
let trailer: &'static [u8] = if i + 1 == parts.len() { &[0] } else { &[] };
let id = b.put_segment(SegmentKind::Metadata, name, header, chunk, trailer)?;
if is_content {
content.push(id);
}
}
let info = match kind {
ItemKind::Xmp => ItemInfo::xmp(),
_ => ItemInfo::jumbf(),
};
b.item(info, &content)
}
#[cfg(test)]
mod tests;