use cadmpeg_ir::le::u32_at;
use cadmpeg_ir::le::u64_at as read_le_u64;
#[derive(Debug, Clone, PartialEq)]
pub struct AsmHeader {
pub width: u8,
pub release: Option<u32>,
pub record_count: Option<u32>,
pub entity_count: Option<u32>,
pub flags: Option<u32>,
pub product_family: Option<String>,
pub product_version: Option<String>,
pub save_date: Option<String>,
pub scale: Option<f64>,
pub linear: Option<f64>,
pub angular: Option<f64>,
}
const MAGIC_PREFIX: &[u8] = b"ASM BinaryFile";
const BF8_STRING_REGION_START: usize = 47;
const BF4_STRING_REGION_START: usize = 31;
pub fn has_asm_magic(bytes: &[u8]) -> bool {
bytes.len() >= 16 && bytes.starts_with(MAGIC_PREFIX) && matches!(bytes[14], b'4' | b'8')
}
fn string_region_start(bytes: &[u8]) -> Option<usize> {
match bytes[14] {
b'8' => Some(BF8_STRING_REGION_START),
b'4' => Some(BF4_STRING_REGION_START),
_ => None,
}
}
pub fn parse(bytes: &[u8]) -> Option<AsmHeader> {
if !has_asm_magic(bytes) {
return None;
}
let width = bytes[14] - b'0';
let mut header = AsmHeader {
width,
release: None,
record_count: None,
entity_count: None,
flags: None,
product_family: None,
product_version: None,
save_date: None,
scale: None,
linear: None,
angular: None,
};
match width {
8 => {
header.release = u32_at(bytes, 15);
header.entity_count = read_le_u64(bytes, 31).and_then(|v| u32::try_from(v).ok());
header.flags = read_le_u64(bytes, 39).and_then(|v| u32::try_from(v).ok());
}
4 => {
header.release = u32_at(bytes, 15);
header.record_count = u32_at(bytes, 19);
header.entity_count = u32_at(bytes, 23);
header.flags = u32_at(bytes, 27);
}
_ => return Some(header),
}
let Some(start) = string_region_start(bytes) else {
return Some(header);
};
let (strings, doubles, _) = read_string_region(bytes, start);
let mut it = strings.into_iter();
header.product_family = it.next();
header.product_version = it.next();
header.save_date = it.next();
let mut dit = doubles.into_iter();
header.scale = dit.next();
header.linear = dit.next();
header.angular = dit.next();
Some(header)
}
pub fn record_stream_start(bytes: &[u8]) -> Option<usize> {
if !has_asm_magic(bytes) {
return None;
}
let start = string_region_start(bytes)?;
let (strings, doubles, cur) = read_string_region(bytes, start);
(strings.len() == 3 && doubles.len() == 3).then_some(cur)
}
fn read_string_region(bytes: &[u8], start: usize) -> (Vec<String>, Vec<f64>, usize) {
let mut cur = start;
let mut strings = Vec::new();
while strings.len() < 3 {
match read_u8_string(bytes, cur) {
Some((s, next)) => {
strings.push(s);
cur = next;
}
None => break,
}
}
let mut doubles = Vec::new();
while doubles.len() < 3 {
match read_tagged_f64(bytes, cur) {
Some((v, next)) => {
doubles.push(v);
cur = next;
}
None => break,
}
}
(strings, doubles, cur)
}
const DELTA_STATE: &[u8] = b"delta_state";
pub fn first_delta_state_offset(bytes: &[u8]) -> Option<usize> {
bytes
.windows(DELTA_STATE.len())
.position(|w| w == DELTA_STATE)
}
fn read_u8_string(bytes: &[u8], at: usize) -> Option<(String, usize)> {
if *bytes.get(at)? != 0x07 {
return None;
}
let len = *bytes.get(at + 1)? as usize;
let start = at + 2;
let slice = bytes.get(start..start + len)?;
let s = std::str::from_utf8(slice).ok()?.to_string();
Some((s, start + len))
}
fn read_tagged_f64(bytes: &[u8], at: usize) -> Option<(f64, usize)> {
if *bytes.get(at)? != 0x06 {
return None;
}
let slice = bytes.get(at + 1..at + 9)?;
Some((
f64::from_le_bytes(
slice
.try_into()
.expect("invariant: bytes.get(at+1..at+9) is an 8-byte slice"),
),
at + 9,
))
}