use alloc::vec::Vec;
use crate::boxes::{BoxHeader, BoxIter};
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RefKind {
Dimg,
Thmb,
Cdsc,
Auxl,
Other([u8; 4]),
}
impl RefKind {
fn from_fourcc(code: [u8; 4]) -> RefKind {
match &code {
b"dimg" => RefKind::Dimg,
b"thmb" => RefKind::Thmb,
b"cdsc" => RefKind::Cdsc,
b"auxl" => RefKind::Auxl,
_ => RefKind::Other(code),
}
}
}
#[derive(Debug, Clone)]
pub struct ItemReference {
pub kind: RefKind,
pub from: u32,
pub to: Vec<u32>,
}
pub fn parse(b: &BoxHeader<'_>) -> Result<Vec<ItemReference>> {
let (version, _flags, r) = b.full_box("iref")?;
let large = match version {
0 => false,
1 => true,
_ => return Err(Error::Unsupported("iref version above 1")),
};
let mut out = Vec::new();
for child in BoxIter::new(r.rest()) {
let child = child?;
let mut cr = child.reader();
let from = if large {
cr.u32("iref from_item_ID")?
} else {
u32::from(cr.u16("iref from_item_ID")?)
};
let count = cr.u16("iref reference count")?;
let width = if large { 4 } else { 2 };
if usize::from(count) * width > cr.remaining() {
return Err(Error::Malformed(
"iref declares more references than it holds",
));
}
let mut to = Vec::with_capacity(usize::from(count));
for _ in 0..count {
to.push(if large {
cr.u32("iref to_item_ID")?
} else {
u32::from(cr.u16("iref to_item_ID")?)
});
}
out.push(ItemReference {
kind: RefKind::from_fourcc(child.boxtype),
from,
to,
});
}
Ok(out)
}
pub fn targets(refs: &[ItemReference], from: u32, kind: RefKind) -> &[u32] {
for r in refs {
if r.from == from && r.kind == kind {
return &r.to;
}
}
&[]
}
pub fn sources(refs: &[ItemReference], to: u32, kind: RefKind) -> Vec<u32> {
let mut out = Vec::new();
for r in refs {
if r.kind == kind && r.to.contains(&to) {
out.push(r.from);
}
}
out
}