Skip to main content

qualia_core_db/container_10d/section/
metadata.rs

1//! Original descriptor metadata checks, shared by full and selective readers.
2//! No payload, padding or whole-file checksum claim is made by this function.
3use super::{
4    AlignmentTier, HEADER_BYTE_SIZE, SectionDescriptor, SectionTableError as E, SectionType,
5};
6pub fn validate_descriptor_metadata(
7    d: &SectionDescriptor,
8    index: usize,
9    version: u16,
10    extent: u64,
11    table_start: u64,
12    table_end: u64,
13) -> Result<(), E> {
14    if d.reserved16 != 0 {
15        return Err(E::NonZeroDescriptorReserved { index });
16    }
17    let st = SectionType::from_u8(d.section_type).ok_or(E::UndefinedSectionType {
18        index,
19        got: d.section_type,
20    })?;
21    if st == SectionType::Undefined {
22        return Err(E::UndefinedSectionType {
23            index,
24            got: d.section_type,
25        });
26    }
27    if st == SectionType::ManifoldIdentityV2
28        && version < crate::container_10d::header::HEADER_VERSION
29    {
30        return Err(E::SectionRequiresVersion {
31            section_type: d.section_type,
32            minimum_version: crate::container_10d::header::HEADER_VERSION,
33            actual_version: version,
34        });
35    }
36    let tier = AlignmentTier::from_u8(d.alignment_tier).ok_or(E::UndefinedAlignmentTier {
37        index,
38        got: d.alignment_tier,
39    })?;
40    let start = u64::from(d.byte_offset);
41    let length = u64::from(d.byte_length);
42    let end = start + length;
43    if start % tier.to_bytes() as u64 != 0 {
44        return Err(E::MisalignedSection {
45            index,
46            offset: d.byte_offset,
47            tier,
48        });
49    }
50    if end > extent {
51        return Err(E::OutOfBounds {
52            index,
53            offset: d.byte_offset,
54            length: d.byte_length,
55            file_len: usize::try_from(extent).unwrap_or(usize::MAX),
56        });
57    }
58    if length != 0 && (start < HEADER_BYTE_SIZE as u64 || (start < table_end && end > table_start))
59    {
60        return Err(E::SectionOverlapsMetadata { index });
61    }
62    if d.stride > 0 && u64::from(d.stride) * u64::from(d.element_count) != length {
63        return Err(E::StrideInconsistentDescriptor {
64            index,
65            stride: d.stride,
66            element_count: d.element_count,
67            byte_length: d.byte_length,
68        });
69    }
70    Ok(())
71}