use std::io::Write;
use flate2::{Compress, Compression, FlushCompress, Status};
use crate::{ErrorKind, Result};
const MAGIC: &[u8; 23] = b"Kaydara FBX Binary \0\x1a\0";
const FOOT_ID: [u8; 16] = [
0xfa, 0xbc, 0xab, 0x09, 0xd0, 0xc8, 0xd4, 0x66, 0xb1, 0x76, 0xfb, 0x83, 0x1c, 0xf7, 0x26, 0x7e,
];
const FOOT_MAGIC: [u8; 16] = [
0xf8, 0x5a, 0x8c, 0x6a, 0xde, 0xf5, 0xd9, 0x7e, 0xec, 0xe9, 0x0c, 0xe3, 0x75, 0x8f, 0x29, 0x0b,
];
const DEFLATE_FROM: usize = 128;
const LEVEL: u32 = 1;
const PARALLEL_FROM: usize = 1 << 18;
#[derive(Clone, Debug, Default)]
pub(crate) struct Node {
name: &'static str,
count: u32,
props: Vec<u8>,
children: Vec<Node>,
closed: bool,
raw: Vec<usize>,
}
pub(crate) trait Elem: bytemuck::Pod {
const CODE: u8;
#[cfg_attr(target_endian = "little", allow(dead_code, reason = "only big-endian targets swap"))]
fn le(self) -> Self;
}
macro_rules! elem {
($t:ty, $code:literal) => {
impl Elem for $t {
const CODE: u8 = $code;
fn le(self) -> Self {
<$t>::from_ne_bytes(self.to_le_bytes())
}
}
};
}
elem!(f64, b'd');
elem!(f32, b'f');
elem!(i32, b'i');
elem!(i64, b'l');
impl Node {
pub(crate) fn new(name: &'static str) -> Self {
Self {
name,
..Self::default()
}
}
#[must_use]
pub(crate) fn closed(mut self) -> Self {
self.closed = true;
self
}
fn head(&mut self, code: u8) {
self.count += 1;
self.props.push(code);
}
pub(crate) fn bool(mut self, v: bool) -> Self {
self.head(b'C');
self.props.push(u8::from(v));
self
}
pub(crate) fn i32(mut self, v: i32) -> Self {
self.head(b'I');
self.props.extend_from_slice(&v.to_le_bytes());
self
}
pub(crate) fn i64(mut self, v: i64) -> Self {
self.head(b'L');
self.props.extend_from_slice(&v.to_le_bytes());
self
}
pub(crate) fn f64(mut self, v: f64) -> Self {
self.head(b'D');
self.props.extend_from_slice(&v.to_le_bytes());
self
}
pub(crate) fn str(self, s: &str) -> Self {
self.bytes_as(b'S', s.as_bytes())
}
pub(crate) fn raw(self, b: &[u8]) -> Self {
self.bytes_as(b'R', b)
}
fn bytes_as(mut self, code: u8, b: &[u8]) -> Self {
self.head(code);
self.props.extend_from_slice(&len32(b.len()).to_le_bytes());
self.props.extend_from_slice(b);
self
}
pub(crate) fn arr<T: Elem>(mut self, v: &[T]) -> Self {
self.head(T::CODE);
self.props.extend_from_slice(&len32(v.len()).to_le_bytes());
#[cfg(target_endian = "little")]
let bytes: &[u8] = bytemuck::cast_slice(v);
#[cfg(not(target_endian = "little"))]
let owned: Vec<T> = v.iter().map(|x| x.le()).collect();
#[cfg(not(target_endian = "little"))]
let bytes: &[u8] = bytemuck::cast_slice(&owned);
self.array_bytes(bytes);
self
}
fn array_bytes(&mut self, bytes: &[u8]) {
if bytes.len() >= DEFLATE_FROM {
self.raw.push(self.props.len());
}
self.props.extend_from_slice(&0u32.to_le_bytes());
self.props.extend_from_slice(&len32(bytes.len()).to_le_bytes());
self.props.extend_from_slice(bytes);
}
pub(crate) fn push(&mut self, child: Self) {
self.children.push(child);
}
#[must_use]
pub(crate) fn with(mut self, child: Self) -> Self {
self.children.push(child);
self
}
#[must_use]
pub(crate) fn child(self, name: &'static str, f: impl FnOnce(Self) -> Self) -> Self {
self.with(f(Self::new(name)))
}
fn ends(&self, last: bool) -> bool {
!self.children.is_empty() || self.closed || (self.count == 0 && !last)
}
fn size(&self, wide: bool, last: bool) -> u64 {
let head = if wide { 25 } else { 13 };
let n = self.children.len();
let kids: u64 = self
.children
.iter()
.enumerate()
.map(|(i, c)| c.size(wide, i + 1 == n))
.sum();
head + self.name.len() as u64 + self.props.len() as u64 + kids + if self.ends(last) { head } else { 0 }
}
fn write(&self, w: &mut dyn Write, at: &mut u64, wide: bool, last: bool) -> std::io::Result<()> {
let end = *at + self.size(wide, last);
let name = u8::try_from(self.name.len()).unwrap_or(u8::MAX);
if wide {
w.write_all(&end.to_le_bytes())?;
w.write_all(&u64::from(self.count).to_le_bytes())?;
w.write_all(&(self.props.len() as u64).to_le_bytes())?;
*at += 25;
} else {
w.write_all(&len32_u64(end).to_le_bytes())?;
w.write_all(&self.count.to_le_bytes())?;
w.write_all(&len32(self.props.len()).to_le_bytes())?;
*at += 13;
}
w.write_all(&[name])?;
w.write_all(self.name.as_bytes())?;
w.write_all(&self.props)?;
*at += u64::from(name) + self.props.len() as u64;
let n = self.children.len();
for (i, c) in self.children.iter().enumerate() {
c.write(w, at, wide, i + 1 == n)?;
}
if self.ends(last) {
null(w, at, wide)?;
}
debug_assert_eq!(*at, end, "record {} sized wrong", self.name);
Ok(())
}
}
fn len32(n: usize) -> u32 {
u32::try_from(n).unwrap_or(u32::MAX)
}
fn len32_u64(n: u64) -> u32 {
u32::try_from(n).unwrap_or(u32::MAX)
}
fn null(w: &mut dyn Write, at: &mut u64, wide: bool) -> std::io::Result<()> {
let n = if wide { 25 } else { 13 };
w.write_all(&[0; 25][..n])?;
*at += n as u64;
Ok(())
}
type Packing<'a> = (&'a mut Vec<u8>, &'a mut Vec<usize>);
fn stored(props: &[u8], at: usize) -> usize {
u32::from_le_bytes(props[at + 4..at + 8].try_into().expect("four bytes")) as usize
}
fn to_pack<'a>(nodes: &'a mut [Node], out: &mut Vec<Packing<'a>>) {
for n in nodes {
let Node {
props, raw, children, ..
} = n;
if !raw.is_empty() {
out.push((props, raw));
}
to_pack(children, out);
}
}
fn pack(props: &[u8], raw: &[usize], z: &mut Compress) -> Vec<u8> {
let mut out = Vec::with_capacity(props.len());
let mut from = 0;
for &at in raw {
let len = stored(props, at);
out.extend_from_slice(&props[from..at]);
let head = out.len();
out.extend_from_slice(&[0; 8]);
out.reserve_exact(len);
let start = out.len();
z.reset();
let done = matches!(
z.compress_vec(&props[at + 8..at + 8 + len], &mut out, FlushCompress::Finish),
Ok(Status::StreamEnd)
);
let packed = out.len() - start;
if done && packed < len {
out[head..head + 4].copy_from_slice(&1u32.to_le_bytes());
out[head + 4..head + 8].copy_from_slice(&len32(packed).to_le_bytes());
} else {
out.truncate(head);
out.extend_from_slice(&props[at..at + 8 + len]);
}
from = at + 8 + len;
}
out.extend_from_slice(&props[from..]);
out
}
pub(crate) fn deflate(top: &mut [Node], threads: usize) {
let mut records = Vec::new();
to_pack(top, &mut records);
let total: usize = records
.iter()
.map(|(props, raw)| raw.iter().map(|&at| stored(props, at)).sum::<usize>())
.sum();
let threads = if total < PARALLEL_FROM { 1 } else { threads };
let packed = crate::keep::par_map_with(
&records,
threads,
|| Compress::new(Compression::new(LEVEL), true),
|z, (props, raw)| pack(props, raw, z),
);
for ((props, raw), p) in records.iter_mut().zip(packed) {
**props = p;
raw.clear();
}
}
fn largest(nodes: &[Node]) -> usize {
nodes
.iter()
.map(|n| n.props.len().max(largest(&n.children)))
.max()
.unwrap_or(0)
}
pub(crate) fn wide(top: &[Node]) -> bool {
let n = top.len();
let body: u64 = top.iter().enumerate().map(|(i, c)| c.size(false, i + 1 == n)).sum();
27 + body + 13 > u64::from(u32::MAX)
}
pub(crate) fn write_file(top: &[Node], w: &mut dyn Write) -> Result<()> {
if largest(top) > u32::MAX as usize {
return Err(crate::Error::from_args(
ErrorKind::LimitExceeded,
format_args!("an FBX record's attributes would be over 4 GiB"),
));
}
let n = top.len();
let wide = wide(top);
let version: u32 = if wide { 7500 } else { 7400 };
w.write_all(MAGIC)?;
w.write_all(&version.to_le_bytes())?;
let mut at = 27u64;
for (i, c) in top.iter().enumerate() {
c.write(w, &mut at, wide, i + 1 == n)?;
}
null(w, &mut at, wide)?;
w.write_all(&FOOT_ID)?;
at += 16;
let pad = (16 - (at % 16) as usize) % 16;
w.write_all(&[0; 20][..pad + 4])?;
w.write_all(&version.to_le_bytes())?;
w.write_all(&[0; 120])?;
w.write_all(&FOOT_MAGIC)?;
Ok(())
}
pub(crate) fn p(name: &str, kind: &str, label: &str, flags: &str) -> Node {
Node::new("P").str(name).str(kind).str(label).str(flags)
}
pub(crate) fn p_f64(name: &str, kind: &str, flags: &str, v: f64) -> Node {
let label = if kind == "double" { "Number" } else { "" };
p(name, kind, label, flags).f64(v)
}
pub(crate) fn p_vec(name: &str, kind: &str, label: &str, flags: &str, v: [f64; 3]) -> Node {
p(name, kind, label, flags).f64(v[0]).f64(v[1]).f64(v[2])
}
pub(crate) fn p_int(name: &str, kind: &str, v: i32) -> Node {
let label = if kind == "int" { "Integer" } else { "" };
p(name, kind, label, "").i32(v)
}
pub(crate) fn p_str(name: &str, v: &str) -> Node {
p(name, "KString", "", "").str(v)
}
pub(crate) fn p_time(name: &str, v: i64) -> Node {
p(name, "KTime", "Time", "").i64(v)
}
pub(crate) fn props70(entries: Vec<Node>) -> Node {
Node {
name: "Properties70",
children: entries,
..Node::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn records_size_themselves_and_end_where_they_say() {
let top = vec![
Node::new("A").i32(1).with(Node::new("B").arr(&[1.0f64; 400])),
Node::new("C"),
Node::new("D").str("x"),
];
let mut out = Vec::new();
write_file(&top, &mut out).unwrap();
assert_eq!(&out[..23], MAGIC);
assert_eq!(u32::from_le_bytes(out[23..27].try_into().unwrap()), 7400);
let end = u32::from_le_bytes(out[27..31].try_into().unwrap()) as usize;
let size = usize::try_from(top[0].size(false, false)).unwrap();
assert_eq!(end, 27 + size);
assert_eq!(&out[out.len() - 16..], &FOOT_MAGIC);
let v = out.len() - 16 - 120 - 4;
assert_eq!(u32::from_le_bytes(out[v..v + 4].try_into().unwrap()), 7400);
assert_eq!(v % 16, 4);
}
#[test]
fn the_footer_pads_to_a_boundary_whatever_the_length() {
let mut pads = std::collections::BTreeSet::new();
for len in 0..16 {
let top = vec![Node::new("A").str(&"x".repeat(len))];
let mut out = Vec::new();
write_file(&top, &mut out).unwrap();
let id = out.windows(16).position(|w| w == FOOT_ID).unwrap() + 16;
let v = out.len() - 16 - 120 - 4;
assert_eq!(v % 16, 4);
assert!(out[id..v].iter().all(|&b| b == 0));
pads.insert(v - 4 - id);
}
assert_eq!(pads.len(), 16);
}
#[test]
fn big_arrays_are_deflated_and_small_ones_stored() {
let mut top = [Node::new("A").arr(&[7i32; 1000])];
deflate(&mut top, 1);
let [n] = top;
assert_eq!(n.props[0], b'i');
assert_eq!(u32::from_le_bytes(n.props[5..9].try_into().unwrap()), 1);
let n = Node::new("A").arr(&[7i32; 4]);
assert_eq!(u32::from_le_bytes(n.props[5..9].try_into().unwrap()), 0);
assert_eq!(u32::from_le_bytes(n.props[9..13].try_into().unwrap()), 16);
}
}