askar_crypto/buffer/
mod.rs

1//! Structures and traits for representing byte ranges in memory
2
3#[cfg(feature = "alloc")]
4use alloc::vec::Vec;
5use core::{fmt::Debug, ops::Range};
6
7use crate::error::Error;
8
9mod array;
10pub use self::array::ArrayKey;
11
12mod hash;
13pub use self::hash::HashBuffer;
14
15#[cfg(feature = "alloc")]
16mod secret;
17#[cfg(feature = "alloc")]
18#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
19pub use self::secret::SecretBytes;
20
21mod string;
22pub use self::string::HexRepr;
23
24mod writer;
25pub use self::writer::Writer;
26
27/// Support for writing to a byte buffer
28pub trait WriteBuffer: Debug {
29    /// Append a slice to the buffer
30    fn buffer_write(&mut self, data: &[u8]) -> Result<(), Error>;
31}
32
33/// Support for writing to, accessing, and resizing a byte buffer
34pub trait ResizeBuffer: WriteBuffer + AsRef<[u8]> + AsMut<[u8]> {
35    /// Insert a slice at the given position in the buffer
36    fn buffer_insert(&mut self, pos: usize, data: &[u8]) -> Result<(), Error>;
37
38    /// Remove an exclusive range from the buffer
39    fn buffer_remove(&mut self, range: Range<usize>) -> Result<(), Error>;
40
41    /// Resize the buffer, truncating or padding it with zeroes
42    fn buffer_resize(&mut self, len: usize) -> Result<(), Error>;
43
44    /// Extend the buffer with `len` bytes of zeroes and return
45    /// a mutable reference to the slice
46    fn buffer_extend(&mut self, len: usize) -> Result<&mut [u8], Error> {
47        let pos = self.as_ref().len();
48        let end = pos + len;
49        self.buffer_resize(end)?;
50        Ok(&mut self.as_mut()[pos..end])
51    }
52}
53
54#[cfg(feature = "alloc")]
55#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
56impl WriteBuffer for Vec<u8> {
57    fn buffer_write(&mut self, data: &[u8]) -> Result<(), Error> {
58        self.extend_from_slice(data);
59        Ok(())
60    }
61}
62
63#[cfg(feature = "alloc")]
64#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
65impl ResizeBuffer for Vec<u8> {
66    fn buffer_insert(&mut self, pos: usize, data: &[u8]) -> Result<(), Error> {
67        self.splice(pos..pos, data.iter().cloned());
68        Ok(())
69    }
70
71    fn buffer_remove(&mut self, range: Range<usize>) -> Result<(), Error> {
72        self.drain(range);
73        Ok(())
74    }
75
76    fn buffer_resize(&mut self, len: usize) -> Result<(), Error> {
77        self.resize(len, 0u8);
78        Ok(())
79    }
80}
81
82#[cfg(feature = "alloc")]
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    pub(crate) fn test_write_buffer<B: WriteBuffer + AsRef<[u8]>>(mut w: B) {
88        w.buffer_write(b"he").unwrap();
89        w.buffer_write(b"y").unwrap();
90        assert_eq!(w.as_ref(), b"hey");
91    }
92
93    pub(crate) fn test_resize_buffer<B: ResizeBuffer>(mut w: B) {
94        w.buffer_write(b"hello").unwrap();
95        w.buffer_insert(1, b"world").unwrap();
96        assert_eq!(w.as_ref(), b"hworldello");
97        w.buffer_resize(12).unwrap();
98        assert_eq!(w.as_ref(), b"hworldello\0\0");
99        w.buffer_resize(6).unwrap();
100        assert_eq!(w.as_ref(), b"hworld");
101        w.buffer_insert(1, b"ello").unwrap();
102        assert_eq!(w.as_ref(), b"helloworld");
103    }
104
105    #[test]
106    fn write_buffer_vec() {
107        test_write_buffer(Vec::new());
108    }
109
110    #[test]
111    fn resize_buffer_vec() {
112        test_resize_buffer(Vec::new());
113    }
114}