asset-io 0.3.0

Streaming, container-agnostic access to media file structure and metadata (JUMBF, XMP)
Documentation
// Copyright 2026 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

//! Raw C2PA manifest-store sidecars, without additional container framing.
//!
//! A nonempty sidecar is one metadata segment and one JUMBF item covering the
//! entire stream. An empty sidecar has neither. Explicit parsing accepts opaque
//! bytes; detection recognizes the standard-length C2PA JUMBF prefix, not every
//! JUMBF type. This handler does not validate manifests or signatures.
#![doc = include_str!("README.md")]

use std::sync::Arc;

use crate::{
    container_api::{
        BoxFuture, ContainerHandler, FormatSpec, Io, ItemInfo, MetadataUpdate, PlanBuilder,
        StructureBuilder, WritePlan,
    },
    jumbf::{superbox_type, C2PA_STORE_TYPE},
    Error, Item, ItemKind, Name, Result, SegmentId, SegmentKind, Structure, Updates,
};

/// The C2PA sidecar container's [`ContainerHandler::id`].
pub const ID: &str = "c2pa";

/// A raw manifest-store sidecar handler.
#[derive(Debug)]
pub struct C2paHandler;

/// A handler for explicit registration.
pub fn handler() -> Arc<dyn ContainerHandler> {
    Arc::new(C2paHandler)
}

impl ContainerHandler for C2paHandler {
    // A sidecar is the store; it embeds no manifest to bind.
    fn c2pa_hash(&self, _structure: &Structure) -> Option<crate::C2paHash> {
        None
    }

    // A sidecar is the store, not an asset that embeds one.
    fn c2pa_data_hash_exclusions(
        &self,
        _structure: &Structure,
        _item: &Item,
    ) -> Result<Vec<crate::ByteRange>> {
        Err(Error::UnsupportedFeature(
            "C2PA data-hash exclusions for a sidecar",
        ))
    }

    fn id(&self) -> &'static str {
        ID
    }

    fn formats(&self) -> &[FormatSpec] {
        const FORMATS: &[FormatSpec] = &[FormatSpec::new("application/c2pa", "c2pa")
            .media_type_aliases(&["application/x-c2pa-manifest-store"])];
        FORMATS
    }

    fn detect(&self, header: &[u8]) -> bool {
        superbox_type(header) == Some(C2PA_STORE_TYPE)
    }

    fn parse_async(self: Arc<Self>, mut io: Io) -> BoxFuture<'static, Result<Structure>> {
        Box::pin(async move {
            let total = io.len();
            let mut builder = StructureBuilder::new(self, "application/c2pa", total);
            if total != 0 {
                let info = item_info(io.fetch(0, total.min(32) as usize).await?);
                builder.push_item(SegmentKind::Metadata, Name::None, 0, total, 0, info)?;
            }
            builder.finish()
        })
    }

    fn write_plan_async<'f, 'a: 'f>(
        &'f self,
        source: &'f Structure,
        _io: Io,
        updates: &'a Updates,
    ) -> BoxFuture<'f, Result<WritePlan<'a>>> {
        Box::pin(async move { plan(source, updates) })
    }

    fn compose(&self, kind: ItemKind, data: &[u8]) -> Result<Vec<u8>> {
        if kind != ItemKind::Jumbf {
            return Err(Error::UnsupportedFormat);
        }
        Ok(data.to_vec())
    }

    fn check_patch(&self, _structure: &Structure, item: &Item, len: usize) -> Result<()> {
        if len as u64 != item.len() {
            return Err(Error::InvalidFormat(
                "C2PA sidecar in-place updates require exact-length payloads".into(),
            ));
        }
        Ok(())
    }
}

fn item_info(data: &[u8]) -> ItemInfo {
    ItemInfo::jumbf().jumbf_type(superbox_type(data))
}

fn plan<'a>(source: &Structure, updates: &'a Updates) -> Result<WritePlan<'a>> {
    match updates.xmp() {
        MetadataUpdate::Keep | MetadataUpdate::Remove => {}
        _ => {
            return Err(Error::UnsupportedFeature(
                "C2PA sidecars cannot contain XMP",
            ))
        }
    }

    let mut builder = PlanBuilder::new(source);
    let keep = match updates.jumbf() {
        MetadataUpdate::Keep => true,
        MetadataUpdate::Remove => !source
            .items_of(ItemKind::Jumbf)
            .any(|item| updates.jumbf_targets(item.jumbf_type())),
        MetadataUpdate::Set(data) => {
            if !data.is_empty() {
                let id = builder.put_segment(
                    SegmentKind::Metadata,
                    Name::None,
                    &[][..],
                    data.as_slice(),
                    &[][..],
                )?;
                builder.item(item_info(data), &[id])?;
            }
            false
        }
    };
    if keep {
        for i in 0..source.segments().len() {
            builder.copy_segment(SegmentId::from_index(i)?)?;
        }
        builder.carry_items(|_| true);
    }
    builder.finish()
}