Skip to main content

ssh_encoding/base64/
reader.rs

1//! Base64 reader support (constant-time).
2
3use crate::{Reader, Result};
4use core::fmt::{self, Debug};
5
6/// Inner constant-time Base64 reader type from the `base64ct` crate.
7type Inner<'i> = base64ct::Decoder<'i, base64ct::Base64>;
8
9/// Constant-time Base64 reader implementation.
10pub struct Base64Reader<'i> {
11    /// Inner Base64 reader.
12    inner: Inner<'i>,
13}
14
15impl<'i> Base64Reader<'i> {
16    /// Create a new Base64 reader for a byte slice containing contiguous (non-newline-delimited)
17    /// Base64-encoded data.
18    ///
19    /// # Errors
20    /// Returns [`crate::Error::Base64`] if the `input` buffer is empty.
21    pub fn new(input: &'i [u8]) -> Result<Self> {
22        let inner = Inner::new(input)?;
23
24        Ok(Self { inner })
25    }
26}
27
28impl Debug for Base64Reader<'_> {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        f.debug_struct("Base64Reader")
31            .field("remaining_len", &self.remaining_len())
32            .finish_non_exhaustive()
33    }
34}
35
36impl Reader for Base64Reader<'_> {
37    fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]> {
38        Ok(self.inner.decode(out)?)
39    }
40
41    fn remaining_len(&self) -> usize {
42        self.inner.remaining_len()
43    }
44}