Skip to main content

qualia_core_db/bundle/stream/protection/
geometry.rs

1//! Actual existing geometry/context codecs over a private source mapping.
2use super::{
3    ids::{self, Mapping},
4    publisher::PublisherError as E,
5    PreparedSource,
6};
7use crate::crypto::network::{digest::sha384, object_stream::*};
8use crate::net::qdnf::types::StrongDigest;
9pub const EMPTY_REF: PageRef = PageRef {
10    page: 0,
11    rows: 0,
12    length: 0,
13    digest: StrongDigest::ZERO,
14};
15pub const EMPTY_ROOT: RootRow = RootRow {
16    id: 0,
17    root: StrongDigest::ZERO,
18};
19pub struct PreparedGeometry<'a, 's> {
20    pub(super) source: PreparedSource<'a, 's>,
21    pub(super) mappings: &'s [Mapping],
22    pub(super) refs: &'s [PageRef],
23    pub(super) source_digest: StrongDigest,
24}
25impl<'a, 's> PreparedGeometry<'a, 's> {
26    pub fn prepare(
27        source: PreparedSource<'a, 's>,
28        mappings: &'s mut [Mapping],
29        refs: &'s mut [PageRef],
30        scratch: &mut [u8],
31    ) -> Result<Self, E> {
32        let n = source.objects().len();
33        let pages = n.div_ceil(PAGE_ROWS);
34        let mappings = mappings.get_mut(..n).ok_or(E::Capacity)?;
35        let refs = refs.get_mut(..pages).ok_or(E::Capacity)?;
36        if scratch.len() < 24 + PAGE_ROWS * 12 {
37            return Err(E::Capacity);
38        }
39        ids::assign(&source, mappings)?;
40        for (page, reference) in refs.iter_mut().enumerate() {
41            *reference = page_bytes(&source, mappings, page, scratch)?;
42        }
43        let source_digest = sha384(source.source_bytes());
44        Ok(Self {
45            source,
46            mappings,
47            refs,
48            source_digest,
49        })
50    }
51    pub fn object_count(&self) -> usize {
52        self.mappings.len()
53    }
54    pub fn metadata_length(&self) -> usize {
55        self.source.metadata_length()
56    }
57    pub fn encode_page(&self, page: usize, out: &mut [u8]) -> Result<PageRef, E> {
58        page_bytes(&self.source, self.mappings, page, out)
59    }
60    pub fn encode_context(&self, fields: ContextFields, out: &mut [u8]) -> Result<usize, E> {
61        if fields.objects as usize != self.object_count()
62            || fields.metadata_length != self.metadata_length() as u64
63        {
64            return Err(E::Source);
65        }
66        encode_context(fields, self.refs, out).map_err(|_| E::Codec)
67    }
68    pub(super) fn check_context(&self, c: Context<'_>) -> Result<(), E> {
69        if c.fields().objects as usize != self.object_count()
70            || c.fields().metadata_length != self.metadata_length() as u64
71            || c.page_count() != self.refs.len()
72        {
73            return Err(E::Source);
74        }
75        for (i, r) in self.refs.iter().enumerate() {
76            if c.page_ref(i).map_err(|_| E::Codec)? != *r {
77                return Err(E::Source);
78            }
79        }
80        Ok(())
81    }
82}
83fn page_bytes(
84    source: &PreparedSource<'_, '_>,
85    maps: &[Mapping],
86    page: usize,
87    out: &mut [u8],
88) -> Result<PageRef, E> {
89    if page >= maps.len().div_ceil(PAGE_ROWS) {
90        return Err(E::State);
91    }
92    let start = page * PAGE_ROWS;
93    let end = (start + PAGE_ROWS).min(maps.len());
94    let mut rows = [GeometryRow { id: 0, length: 0 }; PAGE_ROWS];
95    for (row, m) in rows.iter_mut().zip(&maps[start..end]) {
96        let s = source.objects().get(m.source_slot).ok_or(E::Source)?;
97        *row = GeometryRow {
98            id: m.id,
99            length: s.length(),
100        };
101    }
102    let n = encode_geometry(page as u16, &rows[..end - start], out).map_err(|_| E::Codec)?;
103    Ok(PageRef {
104        page: page as u16,
105        rows: (end - start) as u16,
106        length: n as u32,
107        digest: sha384(&out[..n]),
108    })
109}