diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-04-16 11:58:32 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-04-16 11:58:32 +0200 |
commit | 350320a32ce569d3e86352ba5a79ee969e9595af (patch) | |
tree | 87fe70395f62566d7d7e0cef3e93d62f6288a4e4 /openpgp/src | |
parent | e1ae4a79ada96a97f604d3f3f172b9b02a6da8bb (diff) |
Revert "openpgp: Make PacketParserResult a std::result::Result."
This reverts commit 2e1eec5fe4157a391a13554ff7df3075cfe043cc.
Diffstat (limited to 'openpgp/src')
-rw-r--r-- | openpgp/src/cert/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/cert/parser/mod.rs | 6 | ||||
-rw-r--r-- | openpgp/src/crypto/s2k.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/key.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/signature/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/packet/signature/subpacket.rs | 4 | ||||
-rw-r--r-- | openpgp/src/packet_pile.rs | 18 | ||||
-rw-r--r-- | openpgp/src/parse.rs | 185 | ||||
-rw-r--r-- | openpgp/src/parse/packet_parser_builder.rs | 12 | ||||
-rw-r--r-- | openpgp/src/parse/packet_pile_parser.rs | 24 | ||||
-rw-r--r-- | openpgp/src/parse/stream.rs | 18 | ||||
-rw-r--r-- | openpgp/src/serialize/stream.rs | 10 |
12 files changed, 179 insertions, 110 deletions
diff --git a/openpgp/src/cert/mod.rs b/openpgp/src/cert/mod.rs index 4ce0811b..2bd2e635 100644 --- a/openpgp/src/cert/mod.rs +++ b/openpgp/src/cert/mod.rs @@ -518,7 +518,7 @@ use super::*; /// ```rust /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::Result; -/// # use openpgp::parse::{Parse, PacketParser}; +/// # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; /// use openpgp::cert::prelude::*; /// /// # fn main() { f().unwrap(); } @@ -593,7 +593,7 @@ use super::*; /// # extern crate sequoia_openpgp as openpgp; /// # use std::convert::TryFrom; /// # use openpgp::Result; -/// # use openpgp::parse::{Parse, PacketParser}; +/// # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; /// use openpgp::Cert; /// /// # fn main() { f().unwrap(); } diff --git a/openpgp/src/cert/parser/mod.rs b/openpgp/src/cert/parser/mod.rs index da0194bc..50cc7e5a 100644 --- a/openpgp/src/cert/parser/mod.rs +++ b/openpgp/src/cert/parser/mod.rs @@ -394,7 +394,7 @@ enum PacketSource<'a, I: Iterator<Item=Packet>> { /// ```rust /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::Result; -/// # use openpgp::parse::{Parse, PacketParser}; +/// # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; /// use openpgp::cert::prelude::*; /// /// # fn main() { f().unwrap(); } @@ -441,7 +441,7 @@ impl<'a> From<PacketParserResult<'a>> for CertParser<'a, vec::IntoIter<Packet>> /// Initializes a `CertParser` from a `PacketParser`. fn from(ppr: PacketParserResult<'a>) -> Self { let mut parser : Self = Default::default(); - if let Ok(pp) = ppr { + if let PacketParserResult::Some(pp) = ppr { parser.source = PacketSource::PacketParser(pp); } parser @@ -731,7 +731,7 @@ impl<'a, I: Iterator<Item=Packet>> Iterator for CertParser<'a, I> { match pp.next() { Ok((packet, ppr)) => { - if let Ok(pp) = ppr { + if let PacketParserResult::Some(pp) = ppr { self.source = PacketSource::PacketParser(pp); } diff --git a/openpgp/src/crypto/s2k.rs b/openpgp/src/crypto/s2k.rs index 40a26bda..8dff4693 100644 --- a/openpgp/src/crypto/s2k.rs +++ b/openpgp/src/crypto/s2k.rs @@ -416,7 +416,7 @@ mod tests { // Get the next packet. let (_, ppr) = pp.next().unwrap(); - assert!(ppr.is_err()); + assert!(ppr.is_none()); } } diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index 29409ace..d06ac07e 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -29,7 +29,7 @@ //! ```rust //! # extern crate sequoia_openpgp as openpgp; //! # use openpgp::Result; -//! # use openpgp::parse::{Parse, PacketParser}; +//! # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; //! # use openpgp::cert::prelude::*; //! use openpgp::packet::{Key, key}; //! diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs index b294ae5a..66c0065a 100644 --- a/openpgp/src/packet/signature/mod.rs +++ b/openpgp/src/packet/signature/mod.rs @@ -1283,7 +1283,7 @@ mod test { use super::*; use crate::Cert; - use crate::parse::PacketParser; + use crate::parse::{PacketParserResult, PacketParser}; struct Test<'a> { key: &'a str, @@ -1369,7 +1369,7 @@ mod test { let mut good = 0; let mut ppr = PacketParser::from_bytes( crate::tests::message(test.data)).unwrap(); - while let Ok(pp) = ppr { + while let PacketParserResult::Some(pp) = ppr { if let Packet::Signature(ref sig) = pp.packet { let result = sig.verify(cert.primary_key().key()) .map(|_| true).unwrap_or(false); diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs index ac000842..605d48d2 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -34,13 +34,13 @@ //! # extern crate sequoia_openpgp as openpgp; //! # use openpgp::Result; //! # use openpgp::Packet; -//! # use openpgp::parse::{Parse, PacketParser}; +//! # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; //! # //! # f(include_bytes!("../../../tests/data/messages/signed.gpg")); //! # //! # fn f(message_data: &[u8]) -> Result<()> { //! let mut ppr = PacketParser::from_bytes(message_data)?; -//! while let Ok(mut pp) = ppr { +//! while let PacketParserResult::Some(mut pp) = ppr { //! if let Packet::Signature(ref sig) = pp.packet { //! if let Some(fp) = sig.issuer_fingerprint() { //! eprintln!("Signature issued by: {}", fp.to_string()); diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index 4218a7bf..bc1bc9e3 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -528,7 +528,7 @@ impl<'a> TryFrom<PacketParserResult<'a>> for PacketPile { // ppo.recursion_depth and leave the rest of the message, but // it is hard to imagine that that is what the caller wants. // Instead of hiding that error, fail fast. - if let Ok(ref pp) = ppr { + if let PacketParserResult::Some(ref pp) = ppr { if pp.recursion_depth() != 0 { return Err(Error::InvalidOperation( format!("Expected top-level packet, \ @@ -542,7 +542,7 @@ impl<'a> TryFrom<PacketParserResult<'a>> for PacketPile { let mut last_position = 0; - if ppr.is_err() { + if ppr.is_none() { // Empty message. return Ok(PacketPile::from(Vec::new())); } @@ -593,7 +593,7 @@ impl<'a> TryFrom<PacketParserResult<'a>> for PacketPile { container.children_mut().unwrap().push(packet); - if ppr.is_err() { + if ppr.is_none() { break 'outer; } @@ -740,7 +740,7 @@ mod test { .try_into().unwrap(); let mut ppr = ppp.recurse().unwrap(); - while ppr.is_ok() { + while ppr.is_some() { ppr = ppp.recurse().unwrap(); } let pile = ppp.finish(); @@ -755,7 +755,7 @@ mod test { .try_into().unwrap(); let mut ppr = ppp.recurse().unwrap(); - while let Ok(pp) = ppr.as_mut() { + while let Some(pp) = ppr.as_mut() { eprintln!("{:?}", pp); ppr = ppp.recurse().unwrap(); } @@ -799,13 +799,13 @@ mod test { let mut count = 0; loop { - if let Ok(pp2) = ppr { + if let PacketParserResult::Some(pp2) = ppr { count += 1; let packet_depth = pp2.recursion_depth(); let pp2 = pp2.recurse().unwrap().1; assert_eq!(packet_depth, count - 1); - if pp2.is_ok() { + if pp2.is_some() { assert_eq!(pp2.as_ref().unwrap().recursion_depth(), count); } ppr = pp2; @@ -843,7 +843,7 @@ mod test { // recurse should now not recurse. Since there is nothing // following the compressed packet, ppr should be EOF. let (packet, ppr) = pp.next().unwrap(); - assert!(ppr.is_err()); + assert!(ppr.is_none()); // Get the rest of the content and put the initial byte that // we stole back. @@ -859,7 +859,7 @@ mod test { // And we're done... let ppr = pp.next().unwrap().1; - assert!(ppr.is_err()); + assert!(ppr.is_none()); } #[test] diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index fb556ad2..c8c42a29 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -212,13 +212,13 @@ macro_rules! impl_parse_generic_packet { pp.buffer_unread_content()?; match pp.next()? { - (Packet::$typ(o), Err(_)) + (Packet::$typ(o), PacketParserResult::EOF(_)) => Ok(o), - (p, Err(_)) => + (p, PacketParserResult::EOF(_)) => Err(Error::InvalidOperation( format!("Not a {} packet: {:?}", stringify!($typ), p)).into()), - (_, Ok(_)) => + (_, PacketParserResult::Some(_)) => Err(Error::InvalidOperation( "Excess data after packet".into()).into()), } @@ -1821,7 +1821,7 @@ fn one_pass_sig_test () { let mut one_pass_sigs = 0; let mut sigs = 0; - while let Ok(pp) = ppr { + while let PacketParserResult::Some(pp) = ppr { if let Packet::OnePassSig(_) = pp.packet { one_pass_sigs += 1; } else if let Packet::Signature(ref sig) = pp.packet { @@ -2065,14 +2065,14 @@ impl<'a> Parse<'a, key::UnspecifiedKey> for key::UnspecifiedKey { pp.buffer_unread_content()?; match pp.next()? { - (Packet::PublicKey(o), Err(_)) => Ok(o.into()), - (Packet::PublicSubkey(o), Err(_)) => Ok(o.into()), - (Packet::SecretKey(o), Err(_)) => Ok(o.into()), - (Packet::SecretSubkey(o), Err(_)) => Ok(o.into()), - (p, Err(_)) => + (Packet::PublicKey(o), PacketParserResult::EOF(_)) => Ok(o.into()), + (Packet::PublicSubkey(o), PacketParserResult::EOF(_)) => Ok(o.into()), + (Packet::SecretKey(o), PacketParserResult::EOF(_)) => Ok(o.into()), + (Packet::SecretSubkey(o), PacketParserResult::EOF(_)) => Ok(o.into()), + (p, PacketParserResult::EOF(_)) => Err(Error::InvalidOperation( format!("Not a Key packet: {:?}", p)).into()), - (_, Ok(_)) => + (_, PacketParserResult::Some(_)) => Err(Error::InvalidOperation( "Excess data after packet".into()).into()), } @@ -2331,7 +2331,7 @@ fn compressed_data_parser_test () { } // And, we're done... - assert!(ppr.is_err()); + assert!(ppr.is_none()); } } @@ -2673,18 +2673,18 @@ impl<'a> Parse<'a, Packet> for Packet { ?.buffer_unread_content().build()?; let (p, ppr) = match ppr { - Ok(pp) => { + PacketParserResult::Some(pp) => { pp.next()? }, - Err(_) => + PacketParserResult::EOF(_) => return Err(Error::InvalidOperation( "Unexpected EOF".into()).into()), }; match (p, ppr) { - (p, Err(_)) => + (p, PacketParserResult::EOF(_)) => Ok(p), - (_, Ok(_)) => + (_, PacketParserResult::Some(_)) => Err(Error::InvalidOperation( "Excess data after packet".into()).into()), } @@ -2754,12 +2754,12 @@ impl PacketParserState { /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::Result; /// # use openpgp::Packet; -/// # use openpgp::parse::{Parse, PacketParser}; +/// # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; /// # let _ = f(include_bytes!("../tests/data/keys/public-key.gpg")); /// # /// # fn f(message_data: &[u8]) -> Result<()> { /// let mut ppr = PacketParser::from_bytes(message_data)?; -/// while let Ok(mut pp) = ppr { +/// while let PacketParserResult::Some(mut pp) = ppr { /// // Process the packet. /// /// if let Packet::Literal(_) = pp.packet { @@ -2916,20 +2916,89 @@ impl PacketParserEOF { /// We don't use an `Option`, because when we reach the end of the /// packet sequence, some information about the message needs to /// remain accessible. -pub type PacketParserResult<'a> = - std::result::Result<PacketParser<'a>, PacketParserEOF>; - -/// Like `Option::take`() for PacketParserResult. -/// -/// `self` is replaced with a `PacketParserEOF` with default values. -fn packet_parser_result_take<'a>(ppr: &mut PacketParserResult<'a>) - -> PacketParserResult<'a> { - mem::replace( - ppr, - Err(PacketParserEOF::new( - PacketParserState::new(Default::default())))) +#[derive(Debug)] +pub enum PacketParserResult<'a> { + /// A `PacketParser` for the next packet. + Some(PacketParser<'a>), + /// Information about a fully parsed packet sequence. + EOF(PacketParserEOF), } +impl<'a> PacketParserResult<'a> { + /// Like `Option::is_none`(). + pub fn is_none(&self) -> bool { + if let PacketParserResult::EOF(_) = self { + true + } else { + false + } + } + + /// An alias for `is_none`(). + pub fn is_eof(&self) -> bool { + Self::is_none(self) + } + + /// Like `Option::is_some`(). + pub fn is_some(&self) -> bool { + ! Self::is_none(self) + } + + /// Like `Option::expect`(). + pub fn expect(self, msg: &str) -> PacketParser<'a> { + if let PacketParserResult::Some(pp) = self { + return pp; + } else { + panic!("{}", msg); + } + } + + /// Like `Option::unwrap`(). + pub fn unwrap(self) -> PacketParser<'a> { + self.expect("called `PacketParserResult::unwrap()` on a \ + `PacketParserResult::PacketParserEOF` value") + } + + /// Like `Option::as_ref`(). + pub fn as_ref(&self) -> Option<&PacketParser<'a>> { + if let PacketParserResult::Some(ref pp) = self { + Some(pp) + } else { + None + } + } + + /// Like `Option::as_mut`(). + pub fn as_mut(&mut self) -> Option<&mut PacketParser<'a>> { + if let PacketParserResult::Some(ref mut pp) = self { + Some(pp) + } else { + None + } + } + + /// Like `Option::take`(). + /// + /// `self` is replaced with a `PacketParserEOF` with default + /// values. + pub fn take(&mut self) -> Self { + mem::replace( + self, + PacketParserResult::EOF( + PacketParserEOF::new( + PacketParserState::new(Default::default())))) + } + + /// Like `Option::map`(). + pub fn map<U, F>(self, f: F) -> Option<U> + where F: FnOnce(PacketParser<'a>) -> U + { + match self { + PacketParserResult::Some(x) => Some(f(x)), + PacketParserResult::EOF(_) => None, + } + } +} impl<'a> Parse<'a, PacketParserResult<'a>> for PacketParser<'a> { /// Starts parsing an OpenPGP message stored in a `std::io::Read` object. @@ -3464,7 +3533,7 @@ impl <'a> PacketParser<'a> { let mut eof = PacketParserEOF::new(state_); eof.last_path = self.last_path; return Ok((self.packet, - Err(eof))); + PacketParserResult::EOF(eof))); } else { self.state = state_; self.finish()?; @@ -3487,7 +3556,7 @@ impl <'a> PacketParser<'a> { pp.last_path = self.last_path; - return Ok((self.packet, Ok(pp))); + return Ok((self.packet, PacketParserResult::Some(pp))); } } } @@ -3557,7 +3626,7 @@ impl <'a> PacketParser<'a> { pp.last_path = last_path; return Ok((self.packet, - Ok(pp))); + PacketParserResult::Some(pp))); }, ParserResult::EOF(_) => { return Err(Error::MalformedPacket( @@ -3598,13 +3667,13 @@ impl <'a> PacketParser<'a> { /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::Result; /// # use openpgp::Packet; - /// # use openpgp::parse::{Parse, PacketParser}; + /// # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; /// # use std::string::String; /// # f(include_bytes!("../tests/data/messages/literal-mode-t-partial-body.gpg")); /// # /// # fn f(message_data: &[u8]) -> Result<()> { /// let mut ppr = PacketParser::from_bytes(message_data)?; - /// while let Ok(mut pp) = ppr { + /// while let PacketParserResult::Some(mut pp) = ppr { /// // Process the packet. /// /// if let Packet::Literal(_) = pp.packet { @@ -3950,7 +4019,7 @@ fn packet_parser_reader_interface() { // Make sure we can still get the next packet (which in this case // is just EOF). let (packet, ppr) = pp.recurse().unwrap(); - assert!(ppr.is_err()); + assert!(ppr.is_none()); // Since we read all of the data, we expect content to be None. assert_eq!(packet.unprocessed_body().unwrap().len(), 0); } @@ -4284,11 +4353,11 @@ mod test { ppr = ppr.unwrap().recurse().unwrap().1; } - while let Ok(pp) = ppr { + while let PacketParserResult::Some(pp) = ppr { let tag = pp.packet.tag(); for t in keep.iter() { if *t == tag { - return Ok(pp); + return PacketParserResult::Some(pp); } } @@ -4332,7 +4401,7 @@ mod test { let mut ppr = consume_until( ppr, false, &[ Tag::SEIP, Tag::AED ][..], &[ Tag::SKESK, Tag::PKESK ][..] ); - if let Ok(ref mut pp) = ppr { + if let PacketParserResult::Some(ref mut pp) = ppr { let key = crate::fmt::from_hex(test.key_hex, false) .unwrap().into(); @@ -4344,7 +4413,7 @@ mod test { let mut ppr = consume_until( ppr, true, &[ Tag::Literal ][..], &[ Tag::OnePassSig, Tag::CompressedData ][..]); - if let Ok(ref mut pp) = ppr { + if let PacketParserResult::Some(ref mut pp) = ppr { if stream { let mut body = Vec::new(); loop { @@ -4373,20 +4442,20 @@ mod test { let ppr = consume_until( ppr, true, &[ Tag::MDC ][..], &[ Tag::Signature ][..]); - if let Ok( + if let PacketParserResult::Some( PacketParser { packet: Packet::MDC(ref mdc), .. }) = ppr { assert_eq!(mdc.computed_digest(), mdc.digest(), "MDC doesn't match"); } - if ppr.is_err() { + if ppr.is_none() { // AED packets don't have an MDC packet. continue; } let ppr = consume_until( ppr, true, &[][..], &[][..]); - assert!(ppr.is_err()); + assert!(ppr.is_none()); } } @@ -4400,7 +4469,7 @@ mod test { // Make sure we actually decrypted... let mut saw_literal = false; - while let Ok(mut pp) = ppr { + while let PacketParserResult::Some(mut pp) = ppr { assert!(pp.possible_message().is_ok()); match pp.packet { @@ -4419,7 +4488,7 @@ mod test { ppr = pp.recurse().unwrap().1; } assert!(saw_literal); - if let Err(eof) = ppr { + if let PacketParserResult::EOF(eof) = ppr { assert!(eof.is_message().is_ok()); } else { unreachable!(); @@ -4441,11 +4510,11 @@ mod test { .build() .expect(&format!("Error reading {:?}", test)); - while let Ok(pp) = ppr { + while let PacketParserResult::Some(pp) = ppr { assert!(pp.possible_keyring().is_ok()); ppr = pp.recurse().unwrap().1; } - if let Err(eof) = ppr { + if let PacketParserResult::EOF(eof) = ppr { assert!(eof.is_keyring().is_ok()); assert!(eof.is_cert().is_err()); } else { @@ -4466,12 +4535,12 @@ mod test { .build() .expect(&format!("Error reading {:?}", test)); - while let Ok(pp) = ppr { + while let PacketParserResult::Some(pp) = ppr { assert!(pp.possible_keyring().is_ok()); assert!(pp.possible_cert().is_ok()); ppr = pp.recurse().unwrap().1; } - if let Err(eof) = ppr { + if let PacketParserResult::EOF(eof) = ppr { assert!(eof.is_keyring().is_ok()); assert!(eof.is_cert().is_ok()); } else { @@ -4491,7 +4560,7 @@ mod test { .expect(&format!("Error reading {}", test.filename)[..]); let mut saw_literal = false; - while let Ok(pp) = ppr { + while let PacketParserResult::Some(pp) = ppr { assert!(pp.possible_message().is_ok()); match pp.packet { @@ -4505,7 +4574,7 @@ mod test { ppr = pp.recurse().unwrap().1; } assert!(! saw_literal); - if let Err(eof) = ppr { + if let PacketParserResult::EOF(eof) = ppr { eprintln!("eof: {:?}; message: {:?}", eof, eof.is_message()); assert!(eof.is_message().is_ok()); } else { @@ -4530,7 +4599,7 @@ mod test { // We pop from the end. paths.reverse(); - while let Ok(mut pp) = ppr { + while let PacketParserResult::Some(mut pp) = ppr { let path = paths.pop().expect("Message longer than expect"); assert_eq!(path.0, pp.packet.tag()); assert_eq!(path.1, pp.path()); @@ -4557,7 +4626,7 @@ mod test { "Message shorter than expected (expecting: {:?})", paths); - if let Err(eof) = ppr { + if let PacketParserResult::EOF(eof) = ppr { assert_eq!(last_path, eof.last_path()); } else { panic!("Expect an EOF"); @@ -4581,7 +4650,7 @@ mod test { let mut userids = 0; let mut uas = 0; let mut unknown = 0; - while let Ok(pp) = ppr { + while let PacketParserResult::Some(pp) = ppr { match pp.packet { Packet::Signature(_) => sigs += 1, Packet::PublicSubkey(_) => subkeys += 1, @@ -4613,7 +4682,7 @@ mod test { let ppr = PacketParserBuilder::from_bytes(msg).unwrap() .dearmor(packet_parser_builder::Dearmor::Disabled) .build(); - assert_match!(Ok(Ok(ref _pp)) = ppr); + assert_match!(Ok(PacketParserResult::Some(ref _pp)) = ppr); // Prepend an invalid byte and make sure we fail. Note: we @@ -4640,14 +4709,14 @@ mod test { let ppr = PacketParserBuilder::from_bytes(msg).unwrap() .dearmor(packet_parser_builder::Dearmor::Disabled) .build(); - assert_match!(Ok(Ok(ref _pp)) = ppr); + assert_match!(Ok(PacketParserResult::Some(ref _pp)) = ppr); // Now truncate the packet. let msg2 = &msg[..msg.len() - 1]; let ppr = PacketParserBuilder::from_bytes(msg2).unwrap() .dearmor(packet_parser_builder::Dearmor::Disabled) .build().unwrap(); - if let Ok(pp) = ppr { |