use super::UsdError;
pub const MAGIC: &[u8; 8] = b"PXR-USDC";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CrateInfo {
pub version: (u8, u8, u8),
pub sections: Vec<(String, u64, u64)>,
}
impl CrateInfo {
pub fn has(&self, name: &str) -> bool {
self.sections.iter().any(|(n, _, _)| n == name)
}
}
fn u64_at(b: &[u8], at: usize) -> Option<u64> {
b.get(at..at + 8)
.map(|s| u64::from_le_bytes(s.try_into().unwrap()))
}
pub fn info(bytes: &[u8]) -> Result<CrateInfo, UsdError> {
if !bytes.starts_with(MAGIC) {
return Err(UsdError::NotUsd);
}
if bytes.len() < 88 {
return Err(UsdError::Corrupt("crate bootstrap is truncated"));
}
let version = (bytes[8], bytes[9], bytes[10]);
let toc = u64_at(bytes, 16).ok_or(UsdError::Corrupt("missing toc offset"))? as usize;
if toc == 0 || toc.checked_add(8).is_none_or(|end| end > bytes.len()) {
return Err(UsdError::Corrupt("toc offset out of range"));
}
let count = u64_at(bytes, toc).ok_or(UsdError::Corrupt("missing toc count"))? as usize;
if count > 64 {
return Err(UsdError::Corrupt("implausible section count"));
}
let mut sections = Vec::with_capacity(count);
for i in 0..count {
let at = toc + 8 + i * 32;
let raw = bytes
.get(at..at + 16)
.ok_or(UsdError::Corrupt("truncated toc entry"))?;
let name = String::from_utf8_lossy(raw)
.trim_end_matches('\0')
.to_string();
let offset = u64_at(bytes, at + 16).ok_or(UsdError::Corrupt("truncated toc entry"))?;
let size = u64_at(bytes, at + 24).ok_or(UsdError::Corrupt("truncated toc entry"))?;
sections.push((name, offset, size));
}
Ok(CrateInfo { version, sections })
}
pub fn read(bytes: &[u8]) -> Result<super::UsdLayer, UsdError> {
let info = info(bytes)?;
if info.version.0 != 0 {
let message: &'static str = Box::leak(
format!(
"crate version {}.{}.{} is newer than this reader understands",
info.version.0, info.version.1, info.version.2
)
.into_boxed_str(),
);
return Err(UsdError::UnsupportedCrate(message));
}
super::crate_read::read(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
fn crate_file(sections: &[(&str, u64, u64)]) -> Vec<u8> {
let toc_at = 88u64;
let mut out = vec![0u8; 88];
out[..8].copy_from_slice(MAGIC);
out[8] = 0;
out[9] = 8;
out[10] = 0;
out[16..24].copy_from_slice(&toc_at.to_le_bytes());
out.extend_from_slice(&(sections.len() as u64).to_le_bytes());
for (name, offset, size) in sections {
let mut padded = [0u8; 16];
padded[..name.len()].copy_from_slice(name.as_bytes());
out.extend_from_slice(&padded);
out.extend_from_slice(&offset.to_le_bytes());
out.extend_from_slice(&size.to_le_bytes());
}
out
}
#[test]
fn the_bootstrap_and_toc_are_read() {
let bytes = crate_file(&[("TOKENS", 200, 40), ("PATHS", 240, 80)]);
let info = info(&bytes).unwrap();
assert_eq!(info.version, (0, 8, 0));
assert_eq!(info.sections.len(), 2);
assert_eq!(info.sections[0], ("TOKENS".to_string(), 200, 40));
assert!(info.has("PATHS"));
assert!(!info.has("SPECS"));
}
#[test]
fn a_version_this_reader_predates_is_declined_by_name() {
let mut bytes = crate_file(&[]);
bytes[8] = 9; match read(&bytes) {
Err(UsdError::UnsupportedCrate(why)) => {
assert!(why.contains("9.8.0"), "the message should say what it found: {why}");
}
other => panic!("expected UnsupportedCrate, got {other:?}"),
}
}
#[test]
fn nonsense_is_rejected_rather_than_trusted() {
assert_eq!(info(b"not usdc at all").unwrap_err(), UsdError::NotUsd);
assert!(info(&MAGIC[..]).is_err());
let mut bytes = crate_file(&[("TOKENS", 0, 0)]);
bytes[16..24].copy_from_slice(&9_999_999u64.to_le_bytes());
assert!(info(&bytes).is_err());
}
#[test]
fn an_implausible_section_count_is_not_allocated_for() {
let mut bytes = crate_file(&[]);
let toc = 88;
bytes[toc..toc + 8].copy_from_slice(&u64::MAX.to_le_bytes());
assert!(info(&bytes).is_err());
}
}