use super::*;
impl BlendInspector {
pub fn inspect_bytes(bytes: &[u8]) -> Result<BlendInspectionReport, String> {
let compression: BlendCompression = detect_blend_compression(bytes);
let owned_decompressed: Vec<u8>;
let data: &[u8] = match compression {
BlendCompression::None => bytes,
BlendCompression::Gzip => {
owned_decompressed = inflate_gzip(bytes)?;
&owned_decompressed
}
BlendCompression::Zstd => {
return Err(ERR_BLEND_ZSTD_UNSUPPORTED.to_string());
}
};
let header: BlendHeader = parse_blend_header(data)?;
let is_le: bool = header.get_is_little_endian();
let ptr_size: usize = usize::from(header.get_pointer_size_bytes());
let bhead_size: usize =
if header.get_is_large_header_v5() && header.get_format_version() >= 1 {
BLEND_LARGE_BHEAD_BYTES
} else if ptr_size == 8 {
BLEND_CLASSIC_BHEAD8_BYTES
} else {
BLEND_CLASSIC_BHEAD4_BYTES
};
let mut cursor: usize = header.get_header_size_bytes();
let mut block_count: usize = 0;
let mut saw_endb: bool = false;
let mut warnings: Vec<String> = Vec::new();
let mut id_blocks: Vec<(BlendBlockKind, [u8; 4], usize, usize)> = Vec::new();
let mut dna_summary: Option<BlendDnaSchemaSummary> = None;
while cursor + bhead_size <= data.len() && block_count < BLEND_MAX_BLOCK_COUNT {
let code: [u8; 4] = [
data[cursor],
data[cursor + 1],
data[cursor + 2],
data[cursor + 3],
];
let payload_len_u64: u64 =
if header.get_is_large_header_v5() && header.get_format_version() >= 1 {
let raw_len: i64 = read_endian_i64(data, cursor + 16, is_le).unwrap_or(-1);
if raw_len < 0 {
warnings.push(WARN_BLEND_TRUNCATED_BLOCK.to_string());
break;
}
raw_len as u64
} else {
let raw_len: i32 = read_endian_u32(data, cursor + 4, is_le).unwrap_or(0) as i32;
if raw_len < 0 {
warnings.push(WARN_BLEND_TRUNCATED_BLOCK.to_string());
break;
}
raw_len as u64
};
let payload_start: usize = cursor + bhead_size;
let payload_end: usize = match payload_start.checked_add(payload_len_u64 as usize) {
Some(end) if end <= data.len() => end,
_ => {
warnings.push(WARN_BLEND_TRUNCATED_BLOCK.to_string());
break;
}
};
block_count += 1;
let kind: BlendBlockKind = classify_blend_code(code);
match kind {
BlendBlockKind::EndOfFile => {
saw_endb = true;
break;
}
BlendBlockKind::DnaSchema => {
dna_summary =
parse_blend_dna1(&data[payload_start..payload_end], is_le, ptr_size);
}
BlendBlockKind::Scene
| BlendBlockKind::Collection
| BlendBlockKind::Object
| BlendBlockKind::Mesh
| BlendBlockKind::Material
| BlendBlockKind::Camera
| BlendBlockKind::Light
| BlendBlockKind::Armature
| BlendBlockKind::Action
| BlendBlockKind::Image
| BlendBlockKind::NodeTree => {
id_blocks.push((kind, code, payload_start, payload_end));
}
BlendBlockKind::Other => {}
}
cursor = payload_end;
}
if !saw_endb {
warnings.push(WARN_BLEND_MISSING_ENDB.to_string());
}
if dna_summary.is_none() {
warnings.push(WARN_BLEND_DNA_INCOMPLETE.to_string());
}
let default_offset: usize = if ptr_size == 8 {
BLEND_DEFAULT_ID_NAME_OFFSET_64
} else {
BLEND_DEFAULT_ID_NAME_OFFSET_32
};
let (id_name_offset, id_name_max_len): (usize, usize) = match &dna_summary {
Some(dna) => (
dna.try_get_id_name_offset().unwrap_or(default_offset),
dna.try_get_id_name_max_len().unwrap_or(258),
),
None => (default_offset, 258),
};
let mut scenes: Vec<String> = Vec::new();
let mut collections: Vec<String> = Vec::new();
let mut objects: Vec<String> = Vec::new();
let mut meshes: Vec<String> = Vec::new();
let mut materials: Vec<String> = Vec::new();
let mut cameras: Vec<String> = Vec::new();
let mut lights: Vec<String> = Vec::new();
let mut armatures: Vec<String> = Vec::new();
let mut actions: Vec<String> = Vec::new();
let mut images: Vec<String> = Vec::new();
let mut node_trees: Vec<String> = Vec::new();
for (kind, code, start, end) in id_blocks {
if let Some(name) =
extract_blend_id_name(&data[start..end], code, id_name_offset, id_name_max_len)
{
match kind {
BlendBlockKind::Scene => scenes.push(name),
BlendBlockKind::Collection => collections.push(name),
BlendBlockKind::Object => objects.push(name),
BlendBlockKind::Mesh => meshes.push(name),
BlendBlockKind::Material => materials.push(name),
BlendBlockKind::Camera => cameras.push(name),
BlendBlockKind::Light => lights.push(name),
BlendBlockKind::Armature => armatures.push(name),
BlendBlockKind::Action => actions.push(name),
BlendBlockKind::Image => images.push(name),
BlendBlockKind::NodeTree => node_trees.push(name),
BlendBlockKind::DnaSchema
| BlendBlockKind::EndOfFile
| BlendBlockKind::Other => {}
}
}
}
let (dna_struct_count, dna_type_count, dna_name_count): (usize, usize, usize) =
match &dna_summary {
Some(dna) => (
dna.get_struct_count(),
dna.get_type_count(),
dna.get_name_count(),
),
None => (0, 0, 0),
};
let version_string: String = format!(
"{}.{}",
header.get_version_major(),
header.get_version_minor()
);
Ok(BlendInspectionReport {
blender_version_major: header.get_version_major(),
blender_version_minor: header.get_version_minor(),
version_string,
pointer_size_bytes: header.get_pointer_size_bytes(),
is_little_endian: header.get_is_little_endian(),
is_large_header_v5: header.get_is_large_header_v5(),
compression,
block_count,
dna_struct_count,
dna_type_count,
dna_name_count,
scenes,
collections,
objects,
meshes,
materials,
cameras,
lights,
armatures,
actions,
images,
node_trees,
warnings,
})
}
}