diff --git a/der/src/reader.rs b/der/src/reader.rs index 9e2b2fad4..10576be29 100644 --- a/der/src/reader.rs +++ b/der/src/reader.rs @@ -23,16 +23,16 @@ pub trait Reader<'r>: Sized { /// Get the length of the input. fn input_len(&self) -> Length; - /// Peek at the next byte of input without modifying the position within the reader. - fn peek_byte(&mut self) -> Option; + /// Peek at the next byte of input without modifying the cursor. + fn peek_byte(&self) -> Option; /// Peek forward in the input data, attempting to decode a [`Header`] from /// the data at the current position in the decoder. /// - /// Does not modify the position within the reader. - fn peek_header(&mut self) -> Result; + /// Does not modify the decoder's state. + fn peek_header(&self) -> Result; - /// Get the position within the reader in bytes. + /// Get the position within the buffer. fn position(&self) -> Length; /// Attempt to read data borrowed directly from the input as a slice, @@ -103,8 +103,8 @@ pub trait Reader<'r>: Sized { /// Peek at the next byte in the decoder and attempt to decode it as a /// [`Tag`] value. /// - /// Does not modify the position within the reader. - fn peek_tag(&mut self) -> Result { + /// Does not modify the decoder's state. + fn peek_tag(&self) -> Result { match self.peek_byte() { Some(byte) => byte.try_into(), None => Err(Error::incomplete(self.input_len())), diff --git a/der/src/reader/nested.rs b/der/src/reader/nested.rs index 03d21447b..77c6cdc45 100644 --- a/der/src/reader/nested.rs +++ b/der/src/reader/nested.rs @@ -59,7 +59,7 @@ impl<'i, 'r, R: Reader<'r>> Reader<'r> for NestedReader<'i, R> { self.input_len } - fn peek_byte(&mut self) -> Option { + fn peek_byte(&self) -> Option { if self.is_finished() { None } else { @@ -67,7 +67,7 @@ impl<'i, 'r, R: Reader<'r>> Reader<'r> for NestedReader<'i, R> { } } - fn peek_header(&mut self) -> Result
{ + fn peek_header(&self) -> Result
{ if self.is_finished() { Err(Error::incomplete(self.offset())) } else { diff --git a/der/src/reader/pem.rs b/der/src/reader/pem.rs index 7bf5f9cdd..ed619b30a 100644 --- a/der/src/reader/pem.rs +++ b/der/src/reader/pem.rs @@ -2,6 +2,7 @@ use super::Reader; use crate::{Decode, EncodingRules, Error, ErrorKind, Header, Length, Result}; +use core::cell::RefCell; #[allow(clippy::arithmetic_side_effects)] mod utils { @@ -125,7 +126,7 @@ mod utils { #[derive(Clone)] pub struct PemReader<'i> { /// Inner PEM decoder wrapped in a BufReader. - reader: utils::BufReader<'i>, + reader: RefCell>, /// Encoding rules to apply when decoding the input. encoding_rules: EncodingRules, @@ -147,7 +148,7 @@ impl<'i> PemReader<'i> { let input_len = Length::try_from(reader.remaining_len())?; Ok(Self { - reader, + reader: RefCell::new(reader), encoding_rules: EncodingRules::default(), input_len, position: Length::ZERO, @@ -157,7 +158,7 @@ impl<'i> PemReader<'i> { /// Get the PEM label which will be used in the encapsulation boundaries /// for this document. pub fn type_label(&self) -> &'i str { - self.reader.type_label() + self.reader.borrow().type_label() } } @@ -171,15 +172,15 @@ impl<'i> Reader<'i> for PemReader<'i> { self.input_len } - fn peek_byte(&mut self) -> Option { + fn peek_byte(&self) -> Option { if self.is_finished() { None } else { - self.reader.peek_byte() + self.reader.borrow().peek_byte() } } - fn peek_header(&mut self) -> Result
{ + fn peek_header(&self) -> Result
{ if self.is_finished() { Err(Error::incomplete(self.offset())) } else { @@ -197,12 +198,13 @@ impl<'i> Reader<'i> for PemReader<'i> { } fn read_into<'o>(&mut self, buf: &'o mut [u8]) -> Result<&'o [u8]> { - let bytes = self.reader.copy_to_slice(buf)?; + let bytes = self.reader.borrow_mut().copy_to_slice(buf)?; + self.position = (self.position + bytes.len())?; debug_assert_eq!( self.position, - (self.input_len - Length::try_from(self.reader.remaining_len())?)? + (self.input_len - Length::try_from(self.reader.borrow().remaining_len())?)? ); Ok(bytes) diff --git a/der/src/reader/slice.rs b/der/src/reader/slice.rs index 957ae9c37..a9e1cabe7 100644 --- a/der/src/reader/slice.rs +++ b/der/src/reader/slice.rs @@ -77,13 +77,13 @@ impl<'a> Reader<'a> for SliceReader<'a> { self.bytes.len() } - fn peek_byte(&mut self) -> Option { + fn peek_byte(&self) -> Option { self.remaining() .ok() .and_then(|bytes| bytes.first().cloned()) } - fn peek_header(&mut self) -> Result { + fn peek_header(&self) -> Result { Header::decode(&mut self.clone()) } @@ -212,7 +212,7 @@ mod tests { #[test] fn peek_tag() { - let mut reader = SliceReader::new(EXAMPLE_MSG).unwrap(); + let reader = SliceReader::new(EXAMPLE_MSG).unwrap(); assert_eq!(reader.position(), Length::ZERO); assert_eq!(reader.peek_tag().unwrap(), Tag::Integer); assert_eq!(reader.position(), Length::ZERO); // Position unchanged @@ -220,7 +220,7 @@ mod tests { #[test] fn peek_header() { - let mut reader = SliceReader::new(EXAMPLE_MSG).unwrap(); + let reader = SliceReader::new(EXAMPLE_MSG).unwrap(); assert_eq!(reader.position(), Length::ZERO); let header = reader.peek_header().unwrap();