summaryrefslogtreecommitdiffstats
path: root/openpgp/src/parse
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-12-19 13:23:53 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-12-20 18:04:56 +0100
commit103379888e9d0ccc701a24f2adb539bc9095f10b (patch)
treea620c1788f82c9f4bda9a0a1e94aec9ccfc32476 /openpgp/src/parse
parentdc578999ea706417f4fd1a877ecc06840e58d5b0 (diff)
openpgp: Store literal data in struct Literal.
- With this change, packet::Common is only used by container packets.
Diffstat (limited to 'openpgp/src/parse')
-rw-r--r--openpgp/src/parse/parse.rs55
1 files changed, 38 insertions, 17 deletions
diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs
index 33b27d1a..e7b1e293 100644
--- a/openpgp/src/parse/parse.rs
+++ b/openpgp/src/parse/parse.rs
@@ -3586,9 +3586,12 @@ impl <'a> PacketParser<'a> {
/// .starts_with(b"A Cypherpunk's Manifesto"));
/// # assert!(pp.buffer_unread_content()?
/// # .starts_with(b"A Cypherpunk's Manifesto"));
- /// assert!(pp.packet.body().unwrap()
- /// .starts_with(b"A Cypherpunk's Manifesto"));
- /// assert_eq!(pp.packet.body().unwrap().len(), 5158);
+ /// if let Packet::Literal(l) = &pp.packet {
+ /// assert!(l.body().starts_with(b"A Cypherpunk's Manifesto"));
+ /// assert_eq!(l.body().len(), 5158);
+ /// } else {
+ /// unreachable!();
+ /// }
/// }
///
/// // Start parsing the next packet.
@@ -3598,18 +3601,33 @@ impl <'a> PacketParser<'a> {
/// # }
pub fn buffer_unread_content(&mut self) -> Result<&[u8]> {
let mut rest = self.steal_eof()?;
- if rest.len() > 0 {
- if let Some(body) = self.packet.body_mut() {
- body.append(&mut rest);
- } else {
- self.packet.set_body(rest);
- }
- }
+ match &mut self.packet {
+ Packet::Literal(p) => {
+ if rest.len() > 0 {
+ if p.body().len() > 0 {
+ p.body_mut().append(&mut rest);
+ } else {
+ p.set_body(rest);
+ }
+ }
- if let Some(body) = self.packet.body() {
- Ok(&body[..])
- } else {
- Ok(&b""[..])
+ Ok(p.body())
+ },
+ p => {
+ if rest.len() > 0 {
+ if let Some(body) = p.body_mut() {
+ body.append(&mut rest);
+ } else {
+ p.set_body(rest);
+ }
+ }
+
+ if let Some(body) = p.body() {
+ Ok(&body[..])
+ } else {
+ Ok(&b""[..])
+ }
+ },
}
}
@@ -4252,9 +4270,12 @@ mod test {
"{:?}", pp.packet);
} else {
pp.buffer_unread_content().unwrap();
- assert_eq!(pp.packet.body().unwrap(),
- &test.plaintext.content()[..],
- "{:?}", pp.packet);
+ if let Packet::Literal(l) = &pp.packet {
+ assert_eq!(l.body(), &test.plaintext.content()[..],
+ "{:?}", pp.packet);
+ } else {
+ panic!("Expected literal, got: {:?}", pp.packet);
+ }
}
} else {
panic!("Expected a Literal packet. Got: {:?}", ppr);