Skip to main content

ssh_encoding/base64/
reader.rs

1//! Base64 reader support (constant-time).
2
3use crate::{Decode, Error, 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    /// Custom length of remaining data, used for nested length-prefixed reading.
15    remaining_len: usize,
16}
17
18impl<'i> Base64Reader<'i> {
19    /// Create a new Base64 reader for a byte slice containing contiguous (non-newline-delimited)
20    /// Base64-encoded data.
21    ///
22    /// # Errors
23    /// Returns [`Error::Base64`] if the `input` buffer is empty.
24    pub fn new(input: &'i [u8]) -> Result<Self> {
25        let inner = Inner::new(input)?;
26        let remaining_len = inner.remaining_len();
27
28        Ok(Self {
29            inner,
30            remaining_len,
31        })
32    }
33}
34
35impl Debug for Base64Reader<'_> {
36    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37        f.debug_struct("Base64Reader")
38            .field("remaining_len", &self.remaining_len)
39            .finish_non_exhaustive()
40    }
41}
42
43impl_reader_for_newtype!(Base64Reader<'_>);