summaryrefslogtreecommitdiffstats
path: root/openpgp/src/parse.rs
diff options
context:
space:
mode:
Diffstat (limited to 'openpgp/src/parse.rs')
-rw-r--r--openpgp/src/parse.rs185
1 files changed, 127 insertions, 58 deletions
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 {
+ if let PacketParserResult::Some(pp) = ppr {
let err = pp.next().err().unwrap();
assert_match!(Some(&Error::MalformedPacket(_))
= err.downcast_ref());
@@ -4667,7 +4736,7 @@ mod test {
// Make sure we can read it.
let ppr = PacketParserBuilder::from_bytes(&buf).unwrap()
.build().unwrap();
- if let Ok(pp) = ppr {
+ if let PacketParserResult::Some(pp) = ppr {
assert_eq!(Packet::UserID("foobar".into()), pp.packet);
} else {
panic!("failed to parse userid");
@@ -4678,7 +4747,7 @@ mod test {
let ppr = PacketParserBuilder::from_bytes(&buf).unwrap()
.max_packet_size(5)
.build().unwrap();
- if let Ok(pp) = ppr {
+ if let PacketParserResult::Some(pp) = ppr {
if let Packet::Unknown(ref u) = pp.packet {
assert_eq!(u.tag(), Tag::UserID);
assert_match!(Some(&Error::PacketTooLarge(_, _, _))