diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2024-01-15 18:35:00 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2024-01-22 13:48:32 +0100 |
commit | b838d4b1bfc4b432e908741c94b336df19d201ad (patch) | |
tree | af9d9a77b046f8de9d9887aeae99a683b2c16565 /openpgp/src/cert | |
parent | 96d829d681ea1b860bf4b16439082929f4636147 (diff) |
openpgp: Add Parse::from_buffered_reader.
- Add a buffered-reader-based function to trait Parse. This allows
us to manipulate the buffered reader stack before and after
parsing, e.g. to parse several armored objects in one stream. The
CertParser also does this, but uses internal interfaces for that.
Diffstat (limited to 'openpgp/src/cert')
-rw-r--r-- | openpgp/src/cert/parser/mod.rs | 10 | ||||
-rw-r--r-- | openpgp/src/cert/raw.rs | 23 |
2 files changed, 31 insertions, 2 deletions
diff --git a/openpgp/src/cert/parser/mod.rs b/openpgp/src/cert/parser/mod.rs index 147f32de..e8963d94 100644 --- a/openpgp/src/cert/parser/mod.rs +++ b/openpgp/src/cert/parser/mod.rs @@ -3,6 +3,7 @@ use std::mem; use std::vec; use std::path::Path; +use buffered_reader::BufferedReader; use lalrpop_util::ParseError; use crate::{ @@ -11,6 +12,7 @@ use crate::{ packet::Tag, Packet, parse::{ + Cookie, Parse, PacketParserResult, PacketParser @@ -594,6 +596,14 @@ impl<'a> From<Vec<Packet>> for CertParser<'a> { impl<'a> Parse<'a, CertParser<'a>> for CertParser<'a> { + /// Initializes a `CertParser` from a `BufferedReader`. + fn from_buffered_reader<R>(reader: R) -> Result<CertParser<'a>> + where + R: BufferedReader<Cookie> + 'a, + { + Ok(Self::from(PacketParser::from_buffered_reader(reader)?)) + } + /// Initializes a `CertParser` from a `Read`er. fn from_reader<R: 'a + io::Read + Send + Sync>(reader: R) -> Result<Self> { Ok(Self::from(PacketParser::from_reader(reader)?)) diff --git a/openpgp/src/cert/raw.rs b/openpgp/src/cert/raw.rs index e9610818..bd981c24 100644 --- a/openpgp/src/cert/raw.rs +++ b/openpgp/src/cert/raw.rs @@ -459,8 +459,11 @@ impl<'a> Parse<'a, RawCert<'a>> for RawCert<'a> { /// Returns the first RawCert encountered in the reader. /// /// Returns an error if there are multiple certificates. - fn from_reader<R: 'a + Read + Send + Sync>(reader: R) -> Result<Self> { - let mut parser = RawCertParser::from_reader(reader)?; + fn from_buffered_reader<R>(reader: R) -> Result<RawCert<'a>> + where + R: BufferedReader<Cookie> + 'a + { + let mut parser = RawCertParser::from_buffered_reader(reader)?; if let Some(cert_result) = parser.next() { if parser.next().is_some() { Err(Error::MalformedCert( @@ -473,6 +476,14 @@ impl<'a> Parse<'a, RawCert<'a>> for RawCert<'a> { Err(Error::MalformedCert("No data".into()).into()) } } + + /// Returns the first RawCert encountered in the reader. + /// + /// Returns an error if there are multiple certificates. + fn from_reader<R: 'a + Read + Send + Sync>(reader: R) -> Result<Self> { + let br = Generic::with_cookie(reader, None, Cookie::default()); + Self::from_buffered_reader(br) + } } impl<'a> crate::seal::Sealed for RawCert<'a> {} @@ -655,6 +666,14 @@ impl<'a> RawCertParser<'a> { impl<'a> Parse<'a, RawCertParser<'a>> for RawCertParser<'a> { + /// Initializes a `RawCertParser` from a `BufferedReader`. + fn from_buffered_reader<R>(reader: R) -> Result<RawCertParser<'a>> + where + R: BufferedReader<Cookie> + 'a + { + RawCertParser::new(reader) + } + /// Initializes a `RawCertParser` from a `Read`er. fn from_reader<R: 'a + Read + Send + Sync>(reader: R) -> Result<Self> { RawCertParser::new(Generic::with_cookie(reader, None, Default::default())) |