Skip to main content

qualia_core_db/bundle/stream/index/
cbor.rs

1//! Borrowed definite-field decoding. No recursion or owned decoder graph.
2use super::{IndexError, IndexReadLimits};
3use minicbor::{Decoder, data::Type};
4
5pub(super) fn checked<T>(v: Result<T, minicbor::decode::Error>) -> Result<T, IndexError> {
6    v.map_err(|_| IndexError::Malformed)
7}
8
9#[derive(Clone, Copy, Debug)]
10pub(super) enum ByteField<'a> {
11    Bytes(&'a [u8]),
12    Array { encoded: &'a [u8], len: usize },
13}
14impl ByteField<'_> {
15    pub(super) fn len(self) -> usize {
16        match self {
17            Self::Bytes(b) => b.len(),
18            Self::Array { len, .. } => len,
19        }
20    }
21    pub(super) fn copy_into(self, out: &mut [u8]) -> Result<usize, IndexError> {
22        let len = self.len();
23        if out.len() < len {
24            return Err(IndexError::Scratch);
25        }
26        match self {
27            Self::Bytes(b) => out[..len].copy_from_slice(b),
28            Self::Array { encoded, .. } => {
29                let mut d = Decoder::new(encoded);
30                checked(d.array())?.ok_or(IndexError::Malformed)?;
31                for v in &mut out[..len] {
32                    *v = checked(d.u8())?;
33                }
34            }
35        }
36        Ok(len)
37    }
38}
39
40fn byte_field<'a>(d: &mut Decoder<'a>, cap: usize) -> Result<ByteField<'a>, IndexError> {
41    match checked(d.datatype())? {
42        Type::Bytes => {
43            let bytes = checked(d.bytes())?;
44            if bytes.len() > cap {
45                return Err(IndexError::ResidentLimit);
46            }
47            Ok(ByteField::Bytes(bytes))
48        }
49        Type::Array => {
50            let start = d.position();
51            let n = checked(d.array())?.ok_or(IndexError::Malformed)?;
52            let len = usize::try_from(n).map_err(|_| IndexError::Overflow)?;
53            if len > cap {
54                return Err(IndexError::ResidentLimit);
55            }
56            for _ in 0..len {
57                checked(d.u8())?;
58            }
59            Ok(ByteField::Array {
60                encoded: &d.input()[start..d.position()],
61                len,
62            })
63        }
64        _ => Err(IndexError::Malformed),
65    }
66}
67
68#[derive(Clone, Copy, Debug)]
69pub struct EntryView<'a> {
70    pub(super) key: &'a str,
71    pub(super) kind: &'a str,
72    pub(super) offset: u64,
73    pub(super) length: u64,
74    pub(super) sha256: [u8; 32],
75    pub(super) meta: Option<ByteField<'a>>,
76}
77impl<'a> EntryView<'a> {
78    pub const fn key(&self) -> &'a str {
79        self.key
80    }
81    pub const fn kind(&self) -> &'a str {
82        self.kind
83    }
84    pub const fn offset(&self) -> u64 {
85        self.offset
86    }
87    pub const fn length(&self) -> u64 {
88        self.length
89    }
90    pub const fn sha256(&self) -> &[u8; 32] {
91        &self.sha256
92    }
93    pub fn metadata_len(&self) -> Option<usize> {
94        self.meta.map(ByteField::len)
95    }
96    pub fn copy_metadata_into(&self, out: &mut [u8]) -> Result<Option<usize>, IndexError> {
97        self.meta.map(|m| m.copy_into(out)).transpose()
98    }
99    pub(super) fn retained_bytes(&self) -> Result<usize, IndexError> {
100        self.key
101            .len()
102            .checked_add(self.kind.len())
103            .and_then(|n| n.checked_add(32))
104            .and_then(|n| n.checked_add(self.metadata_len().unwrap_or(0)))
105            .ok_or(IndexError::Overflow)
106    }
107}
108
109pub(super) fn decode_row<'a>(
110    d: &mut Decoder<'a>,
111    l: IndexReadLimits,
112) -> Result<EntryView<'a>, IndexError> {
113    let fields = checked(d.map())?.ok_or(IndexError::Malformed)?;
114    if !(5..=6).contains(&fields) {
115        return Err(IndexError::MissingField);
116    }
117    let mut row = EntryView {
118        key: "",
119        kind: "",
120        offset: 0,
121        length: 0,
122        sha256: [0; 32],
123        meta: None,
124    };
125    let mut seen = 0u8;
126    for _ in 0..fields {
127        let field = checked(d.str())?;
128        let bit = match field {
129            "key" => 1,
130            "kind" => 2,
131            "offset" => 4,
132            "length" => 8,
133            "sha256" => 16,
134            "meta" => 32,
135            _ => return Err(IndexError::Field),
136        };
137        if seen & bit != 0 {
138            return Err(IndexError::DuplicateField);
139        }
140        seen |= bit;
141        match bit {
142            1 | 2 => {
143                let value = checked(d.str())?;
144                let cap = if bit == 1 { l.key_bytes } else { l.kind_bytes };
145                if value.is_empty() || value.len() > cap {
146                    return Err(IndexError::Text);
147                }
148                if bit == 1 {
149                    row.key = value;
150                } else {
151                    row.kind = value;
152                }
153            }
154            4 => row.offset = checked(d.u64())?,
155            8 => row.length = checked(d.u64())?,
156            16 => {
157                let digest = byte_field(d, 32).map_err(|_| IndexError::Digest)?;
158                if digest.len() != 32 {
159                    return Err(IndexError::Digest);
160                }
161                digest.copy_into(&mut row.sha256)?;
162            }
163            32 => {
164                if checked(d.datatype())? == Type::Null {
165                    checked(d.null())?;
166                } else {
167                    row.meta = Some(byte_field(d, l.meta_bytes)?);
168                }
169            }
170            _ => return Err(IndexError::Field),
171        }
172    }
173    if seen & 31 != 31 {
174        return Err(IndexError::MissingField);
175    }
176    Ok(row)
177}
178
179pub struct EntryIter<'a> {
180    decoder: Decoder<'a>,
181    remaining: usize,
182    limits: IndexReadLimits,
183}
184impl<'a> EntryIter<'a> {
185    pub(super) fn new(bytes: &'a [u8], count: usize, limits: IndexReadLimits) -> Self {
186        let mut decoder = Decoder::new(bytes);
187        // The immutable directory was already admitted; iteration still propagates decode errors.
188        let _ = decoder.array();
189        Self {
190            decoder,
191            remaining: count,
192            limits,
193        }
194    }
195}
196impl<'a> Iterator for EntryIter<'a> {
197    type Item = Result<EntryView<'a>, IndexError>;
198    fn next(&mut self) -> Option<Self::Item> {
199        if self.remaining == 0 {
200            return None;
201        }
202        self.remaining -= 1;
203        let row = decode_row(&mut self.decoder, self.limits);
204        if row.is_err() {
205            self.remaining = 0;
206        }
207        Some(row)
208    }
209    fn size_hint(&self) -> (usize, Option<usize>) {
210        (0, Some(self.remaining))
211    }
212}