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