Files
models/fbx/binary.sakura
2026-11-14 15:40:07 -06:00

200 lines
8.0 KiB
Plaintext

#! Contains reading binary FBX: a header and its version, then the nodes at the
#! root, each its end, how many properties it has and how long they are, its
#! name, its properties, and its children, ended by a record of zeros. From
#! 7.5, the first three are 64 bits wide, not 32. An array's elements may be
#! compressed by zlib, which `inflate` undoes.
import std::math;
module malunal::fbx;
# The bytes binary FBX begins with, before its version.
const binary_magic: strview = "Kaydara FBX Binary ";
# Whether bytes are of binary FBX.
def is_binary(bytes: span<u8>): bool {
return false if bytes.length < 27;
for i in 0..18u {
return false if bytes[i] != binary_magic[i];
}
return true;
}
# A little-endian number of bytes, at a place.
def word_at(bytes: span<u8>, at: u64): u32 {
return bytes[at] as u32 | ((bytes[at + 1] as u32) << 8) | ((bytes[at + 2] as u32) << 16) | ((bytes[at + 3] as u32) << 24);
}
def long_at(bytes: span<u8>, at: u64): u64 {
return word_at(bytes, at) as u64 | ((word_at(bytes, at + 4) as u64) << 32);
}
# Reads binary FBX's nodes.
struct binary_reader {
var bytes: span<u8>;
var at: u64;
#! Whether a node's end and counts are 64 bits wide, as from 7.5.
var wide: bool;
}
# Reads binary FBX.
def read_binary(bytes: span<u8>): result<document, fbx_error> {
var made := document{ binary = true };
made.version = word_at(bytes, 23);
return failed(failure::unsupported, $"It's FBX {made.version}, of before 7.0, which names its objects rather than numbering them.".span) if made.version < 7000;
var r := binary_reader{ bytes = bytes, at = 27, wide = made.version >= 7500 };
while r.at < bytes.length {
val more := r.read_node(&mut made, absent) else (e) { return e; };
break if !more;
}
return made;
}
# Whether as many bytes are left, from where it's reading.
def binary_reader::holds(&this, count: u64): bool {
return this.at <= this.bytes.length && count <= this.bytes.length - this.at;
}
def binary_reader::byte(&mut this): u8 {
val made := this.bytes[this.at];
this.at++;
return made;
}
def binary_reader::word(&mut this): u32 {
val made := word_at(this.bytes, this.at);
this.at += 4;
return made;
}
def binary_reader::long(&mut this): u64 {
val made := long_at(this.bytes, this.at);
this.at += 8;
return made;
}
# Reads a node and its children, beneath a parent.
# @returns Whether it was one, rather than the record of zeros ending a list.
def binary_reader::read_node(&mut this, made: &mut document, parent: u32): result<bool, fbx_error> {
val header: u64 = this.wide ? 25 : 13;
if !this.holds(header) {
# A list at the root may end with the file, before its record of zeros.
return false if parent == absent;
return failed(failure::malformed, $"A node's header at byte {this.at} is past the file's end.".span);
}
val begun := this.at;
val end := this.wide ? this.long() : this.word() as u64;
val count := this.wide ? this.long() : this.word() as u64;
val length := this.wide ? this.long() : this.word() as u64;
val name_length := this.byte() as u64;
return false if end == 0;
if end <= begun || end > this.bytes.length || !this.holds(name_length) {
return failed(failure::malformed, $"The node at byte {begun} ends past the file's end, or before it begins.".span);
}
val name := strview::over(this.bytes.data + this.at, name_length);
this.at += name_length;
val index := made.add(parent, name);
val properties_end := this.at + length;
if properties_end > end {
return failed(failure::malformed, $"{name}: its properties run past its end.".span);
}
made.nodes[index as u64].properties.reserve(count);
for _ in 0..count {
var held := this.read_property(name) else (e) { return e; };
made.nodes[index as u64].properties.append(move(held));
}
if this.at != properties_end {
return failed(failure::malformed, $"{name}: its properties aren't as long as it says.".span);
}
while this.at < end {
val more := this.read_node(made, index) else (e) { return e; };
break if !more;
}
this.at = end;
return true;
}
# Reads a property: its type's letter, then its value.
def binary_reader::read_property(&mut this, name: strview): result<property, fbx_error> {
return failed(failure::malformed, $"{name}: a property is past the file's end.".span) if !this.holds(1);
val code := this.byte();
var made := property{};
if code == 'C' || code == 'Y' || code == 'I' || code == 'L' || code == 'F' || code == 'D' {
val size: u64 = code == 'C' ? 1 : (code == 'Y' ? 2 : (code == 'I' || code == 'F' ? 4 : 8));
return failed(failure::malformed, $"{name}: a property is past the file's end.".span) if !this.holds(size);
if code == 'C' {
made.kind = property_kind::boolean;
made.integer = this.byte() as i64;
} else if code == 'Y' {
made.kind = property_kind::int16;
val low := this.byte() as u16;
val high := this.byte() as u16;
made.integer = (low | (high << 8)) as i16 as i64;
} else if code == 'I' {
made.kind = property_kind::int32;
made.integer = this.word() as i32 as i64;
} else if code == 'L' {
made.kind = property_kind::int64;
made.integer = this.long() as i64;
} else if code == 'F' {
made.kind = property_kind::float32;
made.real = math::real_of_bits(this.word()) as f64;
} else {
made.kind = property_kind::float64;
made.real = math::real_of_bits(this.long());
}
return made;
}
if code == 'S' || code == 'R' {
return failed(failure::malformed, $"{name}: a property is past the file's end.".span) if !this.holds(4);
val length := this.word() as u64;
return failed(failure::malformed, $"{name}: its text runs past the file's end.".span) if !this.holds(length);
made.kind = code == 'S' ? property_kind::text : property_kind::raw;
made.bytes.extend(span::<u8>::over(this.bytes.data + this.at, length));
this.at += length;
return made;
}
if code == 'b' || code == 'i' || code == 'l' || code == 'f' || code == 'd' {
return failed(failure::malformed, $"{name}: a property is past the file's end.".span) if !this.holds(12);
val count := this.word() as u64;
val encoding := this.word();
val stored := this.word() as u64;
return failed(failure::malformed, $"{name}: an array runs past the file's end.".span) if !this.holds(stored);
val size: u64 = code == 'b' ? 1 : (code == 'i' || code == 'f' ? 4 : 8);
val data := span::<u8>::over(this.bytes.data + this.at, stored);
this.at += stored;
var inflated := vector::<u8>::new();
var raw := data;
if encoding == 1 {
inflated.resize(count * size);
val length := inflate(data, inflated.slice) else { return failed(failure::malformed, $"{name}: an array's data won't inflate.".span); };
return failed(failure::malformed, $"{name}: an array's data inflates to {length} bytes, not {count * size}.".span) if length != count * size;
raw = inflated.span;
} else if encoding != 0 {
return failed(failure::malformed, $"{name}: an array's encoding is {encoding}, of 0 and 1.".span);
} else if stored != count * size {
return failed(failure::malformed, $"{name}: an array is {stored} bytes, not {count * size}.".span);
}
if code == 'f' || code == 'd' {
made.kind = code == 'f' ? property_kind::float32s : property_kind::float64s;
made.reals.resize(count);
for i in 0..count {
made.reals[i] = code == 'f' ? math::real_of_bits(word_at(raw, i * 4)) as f64 : math::real_of_bits(long_at(raw, i * 8));
}
} else {
made.kind = code == 'b' ? property_kind::booleans : (code == 'i' ? property_kind::int32s : property_kind::int64s);
made.integers.resize(count);
for i in 0..count {
if code == 'b' {
made.integers[i] = raw[i] as i64;
} else if code == 'i' {
made.integers[i] = word_at(raw, i * 4) as i32 as i64;
} else {
made.integers[i] = long_at(raw, i * 8) as i64;
}
}
}
return made;
}
return failed(failure::malformed, $"{name}: a property is of type `{code}`, which isn't FBX's.".span);
}