mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Merge pull request #1611 from storytold/codex/bento-public
aaf: expose bounded Bento container access
This commit is contained in:
3 files changed
+46
-8
No files matched your search
@@ -21,7 +21,7 @@
|
||||
//! transition's name) are not in the specification; they were first written by FilmCraft and keep
|
||||
//! their names so that existing files read back.
|
||||
|
||||
pub(crate) mod bento;
|
||||
pub mod bento;
|
||||
mod read;
|
||||
mod write;
|
||||
|
||||
|
||||
@@ -35,8 +35,8 @@
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
pub(crate) const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7];
|
||||
pub(crate) const LABEL_SIZE: usize = 24;
|
||||
pub const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7];
|
||||
pub const LABEL_SIZE: usize = 24;
|
||||
|
||||
// standard object ids
|
||||
const TOC_OBJECT: u32 = 1;
|
||||
@@ -82,7 +82,7 @@ const MAX_VALUE_BYTES: usize = 16 << 20;
|
||||
const MAX_NAME_BYTES: usize = 4 << 20;
|
||||
|
||||
/// Builds a Bento container.
|
||||
pub(crate) struct Writer {
|
||||
pub struct Writer {
|
||||
data: Vec<u8>,
|
||||
names: HashMap<(u32, String), u32>,
|
||||
next: u32,
|
||||
@@ -90,6 +90,12 @@ pub(crate) struct Writer {
|
||||
entries: Vec<(u32, u32, u32, Vec<Seg>)>,
|
||||
}
|
||||
|
||||
impl Default for Writer {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
pub fn new() -> Writer {
|
||||
Writer { data: Vec::new(), names: HashMap::new(), next: MIN_USER_ID, entries: Vec::new() }
|
||||
@@ -217,9 +223,9 @@ pub(crate) struct Prop {
|
||||
}
|
||||
|
||||
/// A parsed Bento container.
|
||||
pub(crate) struct Container<'a> {
|
||||
pub struct Container<'a> {
|
||||
data: &'a [u8],
|
||||
pub objects: BTreeMap<u32, Vec<Prop>>,
|
||||
pub(crate) objects: BTreeMap<u32, Vec<Prop>>,
|
||||
names: HashMap<u32, String>,
|
||||
by_name: HashMap<String, u32>,
|
||||
}
|
||||
@@ -229,11 +235,16 @@ fn be32(b: &[u8], at: usize) -> Option<u32> {
|
||||
}
|
||||
|
||||
/// Whether `b` ends with a Bento label.
|
||||
pub(crate) fn sniff(b: &[u8]) -> bool {
|
||||
pub fn sniff(b: &[u8]) -> bool {
|
||||
b.len() >= LABEL_SIZE && b[b.len() - LABEL_SIZE..b.len() - LABEL_SIZE + 8] == MAGIC
|
||||
}
|
||||
|
||||
impl<'a> Container<'a> {
|
||||
/// Object identifiers in ascending order.
|
||||
pub fn object_ids(&self) -> impl Iterator<Item = u32> + '_ {
|
||||
self.objects.keys().copied()
|
||||
}
|
||||
|
||||
pub fn open(data: &'a [u8]) -> Result<Container<'a>, String> {
|
||||
if !sniff(data) {
|
||||
return Err("no Bento label at the end of the file".into());
|
||||
@@ -324,7 +335,7 @@ impl<'a> Container<'a> {
|
||||
}
|
||||
|
||||
/// The bytes of a value.
|
||||
pub fn value(&self, p: &Prop) -> Result<Vec<u8>, String> {
|
||||
pub(crate) fn value(&self, p: &Prop) -> Result<Vec<u8>, String> {
|
||||
let total = p.segs.iter().try_fold(0usize, |total, s| {
|
||||
let len = match s {
|
||||
Seg::Imm(b) => b.len(),
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
use craft_aaf::omf::bento::{Container, Writer, sniff};
|
||||
|
||||
#[test]
|
||||
fn external_consumer_reads_named_values_and_references() {
|
||||
let mut writer = Writer::new();
|
||||
let first = writer.new_object();
|
||||
let second = writer.new_object();
|
||||
writer.set(first, "test:value", "test:type", b"long value");
|
||||
writer.set_references(first, &[second]);
|
||||
let bytes = writer.finish();
|
||||
assert!(sniff(&bytes));
|
||||
let container = Container::open(&bytes).unwrap();
|
||||
assert!(container.object_ids().any(|id| id == first));
|
||||
assert_eq!(container.get(first, "test:value").as_deref(), Some(b"long value".as_slice()));
|
||||
assert_eq!(container.resolve(first, second), second);
|
||||
assert!(Container::open(&bytes[..bytes.len() - 1]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_writer_builds_a_readable_empty_container() -> Result<(), String> {
|
||||
let bytes = Writer::default().finish();
|
||||
let container = Container::open(&bytes)?;
|
||||
let ids: Vec<_> = container.object_ids().collect();
|
||||
assert!(!ids.is_empty());
|
||||
assert!(ids.windows(2).all(|pair| pair[0] < pair[1]));
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user