Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions der/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ use crate::{
use alloc::vec::Vec;

/// Reader trait which reads DER-encoded input.
pub trait Reader<'r>: Sized {
pub trait Reader<'r>: Clone {
/// Get the [`EncodingRules`] which should be applied when decoding the input.
fn encoding_rules(&self) -> EncodingRules;

/// Get the length of the input.
fn input_len(&self) -> Length;

/// Peek at the decoded PEM without updating the internal state, writing into the provided
/// output buffer.
///
/// Attempts to fill the entire buffer, returning an error if there is not enough data.
fn peek_into(&self, buf: &mut [u8]) -> crate::Result<()>;

/// Get the position within the buffer.
fn position(&self) -> Length;

Expand Down Expand Up @@ -121,6 +115,16 @@ pub trait Reader<'r>: Sized {
self.peek_into(&mut byte).ok().map(|_| byte[0])
}

/// Peek at the decoded data without updating the internal state, writing into the provided
/// output buffer.
///
/// Attempts to fill the entire buffer, returning an error if there is not enough data.
fn peek_into(&self, buf: &mut [u8]) -> Result<(), Error> {
let mut reader = self.clone();
reader.read_into(buf)?;
Ok(())
}

/// Peek forward in the input data, attempting to decode a [`Header`] from
/// the data at the current position in the decoder.
///
Expand Down
5 changes: 0 additions & 5 deletions der/src/reader/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ impl<'i> Reader<'i> for PemReader<'i> {
self.position.input_len()
}

fn peek_into(&self, buf: &mut [u8]) -> Result<()> {
self.clone().read_into(buf)?;
Ok(())
}

fn position(&self) -> Length {
self.position.current()
}
Expand Down
5 changes: 0 additions & 5 deletions der/src/reader/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ impl<'a> Reader<'a> for SliceReader<'a> {
self.bytes.len()
}

fn peek_into(&self, buf: &mut [u8]) -> crate::Result<()> {
self.clone().read_into(buf)?;
Ok(())
}

fn position(&self) -> Length {
self.position
}
Expand Down