Skip to main content

oxabl_cache_format/
cursor.rs

1use crate::error::{FormatError, Result};
2use crate::limits::SEALED_RECORD_BYTES;
3
4/// Bounded big-endian reader over untrusted bytes. Every read checks the remaining length.
5pub struct Cursor<'a> {
6    buf: &'a [u8],
7}
8
9impl<'a> Cursor<'a> {
10    pub fn new(buf: &'a [u8]) -> Self {
11        Self { buf }
12    }
13
14    pub fn remaining(&self) -> usize {
15        self.buf.len()
16    }
17
18    pub fn take(&mut self, n: usize) -> Result<&'a [u8]> {
19        if n > self.buf.len() {
20            return Err(FormatError::Truncated);
21        }
22        let (head, tail) = self.buf.split_at(n);
23        self.buf = tail;
24        Ok(head)
25    }
26
27    pub fn array<const N: usize>(&mut self) -> Result<[u8; N]> {
28        let mut out = [0u8; N];
29        out.copy_from_slice(self.take(N)?);
30        Ok(out)
31    }
32
33    pub fn u8(&mut self) -> Result<u8> {
34        Ok(self.array::<1>()?[0])
35    }
36
37    pub fn u16(&mut self) -> Result<u16> {
38        Ok(u16::from_be_bytes(self.array()?))
39    }
40
41    pub fn u32(&mut self) -> Result<u32> {
42        Ok(u32::from_be_bytes(self.array()?))
43    }
44
45    pub fn u64(&mut self) -> Result<u64> {
46        Ok(u64::from_be_bytes(self.array()?))
47    }
48
49    pub fn i64(&mut self) -> Result<i64> {
50        Ok(i64::from_be_bytes(self.array()?))
51    }
52
53    pub fn str16(&mut self) -> Result<&'a str> {
54        let len = usize::from(self.u16()?);
55        std::str::from_utf8(self.take(len)?).map_err(|_| FormatError::InvalidUtf8)
56    }
57
58    /// Reads a `u16` length and that many bytes, at most one sealed record.
59    pub fn record(&mut self) -> Result<&'a [u8]> {
60        let len = usize::from(self.u16()?);
61        if len > SEALED_RECORD_BYTES {
62            return Err(FormatError::LimitExceeded("sealed record"));
63        }
64        self.take(len)
65    }
66
67    pub fn reserved_u8(&mut self) -> Result<()> {
68        match self.u8()? {
69            0 => Ok(()),
70            _ => Err(FormatError::NonZeroReserved),
71        }
72    }
73
74    pub fn reserved_u16(&mut self) -> Result<()> {
75        match self.u16()? {
76            0 => Ok(()),
77            _ => Err(FormatError::NonZeroReserved),
78        }
79    }
80
81    /// Checks a declared element count against `limit` and against the bytes left, so a
82    /// count can never drive an allocation larger than the input.
83    pub fn check_count(
84        &self,
85        count: u64,
86        limit: usize,
87        what: &'static str,
88        min_element: usize,
89    ) -> Result<usize> {
90        let count = usize::try_from(count).map_err(|_| FormatError::LimitExceeded(what))?;
91        if count > limit {
92            return Err(FormatError::LimitExceeded(what));
93        }
94        match count.checked_mul(min_element) {
95            Some(need) if need <= self.buf.len() => Ok(count),
96            _ => Err(FormatError::Truncated),
97        }
98    }
99
100    pub fn count32(
101        &mut self,
102        limit: usize,
103        what: &'static str,
104        min_element: usize,
105    ) -> Result<usize> {
106        let n = self.u32()?;
107        self.check_count(u64::from(n), limit, what, min_element)
108    }
109
110    pub fn count16(
111        &mut self,
112        limit: usize,
113        what: &'static str,
114        min_element: usize,
115    ) -> Result<usize> {
116        let n = self.u16()?;
117        self.check_count(u64::from(n), limit, what, min_element)
118    }
119
120    /// Succeeds only when every byte has been read.
121    pub fn finish(self) -> Result<()> {
122        if self.buf.is_empty() {
123            Ok(())
124        } else {
125            Err(FormatError::TrailingBytes)
126        }
127    }
128}