Skip to main content

qualia_core_db/render/source_frame/
model.rs

1//! Cold integrity/count admission using the canonical .10d mesh owners only.
2use super::{SourceFrameBindLimits, SourceFrameError};
3use crate::{container_10d, render::assets::Mesh};
4use sha2::{Digest, Sha256};
5
6pub fn decode_bound_model(
7    bytes: &[u8],
8    limits: SourceFrameBindLimits,
9) -> Result<(Mesh, [u8; 32]), SourceFrameError> {
10    if bytes.is_empty() || bytes.len() > limits.max_model_bytes {
11        return Err(SourceFrameError::Budget);
12    }
13    let header =
14        container_10d::Container10dHeader::parse(bytes).map_err(|_| SourceFrameError::Model)?;
15    // Include borrowed input + owned CRC copy + descriptor vector before parsing.
16    let base = bytes
17        .len()
18        .checked_mul(2)
19        .and_then(|n| {
20            (header.section_count as usize)
21                .checked_mul(core::mem::size_of::<container_10d::SectionDescriptor>())
22                .and_then(|d| n.checked_add(d))
23        })
24        .ok_or(SourceFrameError::Budget)?;
25    if base > limits.max_cold_bytes {
26        return Err(SourceFrameError::Budget);
27    }
28    let mut checked = bytes.to_vec();
29    container_10d::verify_whole_file_crc32c(&mut checked)
30        .map_err(|_| SourceFrameError::Integrity)?;
31    let sections = container_10d::parse_section_table(&checked, &header)
32        .map_err(|_| SourceFrameError::Integrity)?;
33    let mut meshes = sections
34        .iter()
35        .filter(|s| s.typ() == Some(container_10d::SectionType::QuantizedMesh));
36    let section = meshes.next().ok_or(SourceFrameError::Model)?;
37    if meshes.next().is_some() {
38        return Err(SourceFrameError::Model);
39    }
40    let start = usize::try_from(section.byte_offset).map_err(|_| SourceFrameError::Budget)?;
41    let len = usize::try_from(section.byte_length).map_err(|_| SourceFrameError::Budget)?;
42    let end = start.checked_add(len).ok_or(SourceFrameError::Budget)?;
43    let payload = checked.get(start..end).ok_or(SourceFrameError::Model)?;
44    let (mini, exact_len) = container_10d::mesh_section::parse_mesh_header(payload)
45        .map_err(|_| SourceFrameError::Model)?;
46    let vertices = mini.vertex_count as usize;
47    let indices = (mini.triangle_count as usize)
48        .checked_mul(3)
49        .ok_or(SourceFrameError::Budget)?;
50    if exact_len != payload.len()
51        || vertices == 0
52        || indices == 0
53        || vertices > limits.max_vertices
54        || indices > limits.max_indices
55    {
56        return Err(SourceFrameError::Budget);
57    }
58    let cold = vertices
59        .checked_mul(12)
60        .and_then(|n| indices.checked_mul(4).and_then(|i| n.checked_add(i)))
61        .and_then(|n| base.checked_add(n))
62        .ok_or(SourceFrameError::Budget)?;
63    if cold > limits.max_cold_bytes {
64        return Err(SourceFrameError::Budget);
65    }
66    let mesh = container_10d::decode_mesh_section(payload).map_err(|_| SourceFrameError::Model)?;
67    if mesh.positions.iter().flatten().any(|v| !v.is_finite())
68        || mesh
69            .triangles
70            .iter()
71            .flatten()
72            .any(|&i| i as usize >= vertices)
73    {
74        return Err(SourceFrameError::Model);
75    }
76    Ok((mesh, Sha256::digest(bytes).into()))
77}
78
79/// Bit-identical authored geometry and topology, not equal counts/nearest weld.
80/// Materials, texture rights and source authority remain the admitted host's job.
81pub fn validate_resident_mesh(
82    mesh: &Mesh,
83    positions: &[[f32; 3]],
84    indices: &[u32],
85) -> Result<(), SourceFrameError> {
86    if mesh.positions.len() != positions.len()
87        || mesh.triangles.len().checked_mul(3) != Some(indices.len())
88        || !mesh
89            .positions
90            .iter()
91            .flatten()
92            .zip(positions.iter().flatten())
93            .all(|(a, b)| a.to_bits() == b.to_bits())
94        || !mesh
95            .triangles
96            .iter()
97            .flatten()
98            .copied()
99            .eq(indices.iter().copied())
100    {
101        return Err(SourceFrameError::ResidentMismatch);
102    }
103    Ok(())
104}