Skip to main content

mp4_atom/uuid_boxes/
c2pa.rs

1use crate::*;
2
3/// Represents the C2PA box/atom.
4/// https://spec.c2pa.org/specifications/specifications/2.4/specs/C2PA_Specification.html#uuid_box
5#[derive(Debug, Clone, PartialEq, Eq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct C2pa {
8    pub box_purpose: String,
9    pub data: Vec<u8>,
10}
11
12impl UuidAtomExt for C2pa {
13    const EXTENDED_TYPE_EXT: ExtendedType = ExtendedType::new(&[
14        0xD8, 0xFE, 0xC3, 0xD6, 0x1B, 0x0E, 0x48, 0x3C, 0x92, 0x97, 0x58, 0x28, 0x87, 0x7E, 0xC4,
15        0x81,
16    ]);
17
18    // The ext version is restricted to 0, and flags 0 for C2PA, so they are ignored
19    type Ext = ();
20
21    fn decode_uuid_body_ext<B: Buf>(buf: &mut B, _ext: ()) -> Result<Self> {
22        Ok(C2pa {
23            box_purpose: String::decode(buf)?,
24            data: Vec::decode(buf)?,
25        })
26    }
27
28    fn encode_uuid_body_ext<B: BufMut>(&self, buf: &mut B) -> Result<()> {
29        self.box_purpose.as_str().encode(buf)?;
30        self.data.encode(buf)?;
31        Ok(())
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    // Testing the encoding and decoding logic
40    #[test]
41    fn test_round_trip() -> Result<()> {
42        let input = C2pa {
43            box_purpose: String::from("uuid:test"),
44            data: vec![0xDE, 0xAD, 0xBE, 0xEF],
45        };
46
47        let mut buf = Vec::new();
48
49        C2pa::encode_uuid_body_ext(&input, &mut buf)?;
50
51        let mut cursor = &buf[..];
52
53        let output = C2pa::decode_uuid_body_ext(&mut cursor, ())?;
54
55        assert_eq!(input, output);
56
57        Ok(())
58    }
59}