diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-12-19 15:42:20 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-12-20 18:04:56 +0100 |
commit | 56c114e51e6cd74719bdfd90f160eb6220f63ae6 (patch) | |
tree | 93e8b518633e42f5eace189a0c796825934f736c /openpgp | |
parent | 046cbc7837cfae8505cb77cdc0352eeaac023a85 (diff) |
openpgp: Store unknown packet data in struct Unknown.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/packet/unknown.rs | 23 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 11 | ||||
-rw-r--r-- | openpgp/src/serialize/mod.rs | 23 |
3 files changed, 37 insertions, 20 deletions
diff --git a/openpgp/src/packet/unknown.rs b/openpgp/src/packet/unknown.rs index d9bfbfd0..1d2d8c96 100644 --- a/openpgp/src/packet/unknown.rs +++ b/openpgp/src/packet/unknown.rs @@ -21,6 +21,11 @@ pub struct Unknown { tag: Tag, /// Error that caused parsing or processing to abort. error: failure::Error, + /// Body data. + /// + /// This is written when serialized, and set by the packet parser + /// if `buffer_unread_content` is used. + body: Vec<u8>, } impl Eq for Unknown {} @@ -42,7 +47,7 @@ impl Ord for Unknown { fn cmp(&self, other: &Unknown) -> Ordering { match self.tag.cmp(&other.tag) { - Ordering::Equal => self.common.body().cmp(&other.common.body()), + Ordering::Equal => self.body().cmp(&other.body()), o => o, } } @@ -52,6 +57,7 @@ impl Hash for Unknown { fn hash<H: Hasher>(&self, state: &mut H) { self.common.hash(state); self.tag.hash(state); + self.body.hash(state); } } @@ -76,6 +82,21 @@ impl Unknown { } } + /// Gets a reference to the unknown packet's body. + pub fn body(&self) -> &[u8] { + &self.body + } + + /// Gets a mutable reference to the unknown packet's body. + pub fn body_mut(&mut self) -> &mut Vec<u8> { + &mut self.body + } + + /// Sets the unknown packet's body. + pub fn set_body(&mut self, data: Vec<u8>) -> Vec<u8> { + std::mem::replace(&mut self.body, data) + } + /// Gets the unknown packet's tag. pub fn tag(&self) -> Tag { self.tag diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index e7b1e293..3a3686a8 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -3613,6 +3613,17 @@ impl <'a> PacketParser<'a> { Ok(p.body()) }, + Packet::Unknown(p) => { + if rest.len() > 0 { + if p.body().len() > 0 { + p.body_mut().append(&mut rest); + } else { + p.set_body(rest); + } + } + + Ok(p.body()) + }, p => { if rest.len() > 0 { if let Some(body) = p.body_mut() { diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index 3bbd9000..9d361d9f 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -923,21 +923,14 @@ impl SerializeInto for S2K { impl Serialize for Unknown { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { - let body = if let Some(body) = self.body() { - &body[..] - } else { - &b""[..] - }; - - o.write_all(&body[..])?; - + o.write_all(self.body())?; Ok(()) } } impl NetLength for Unknown { fn net_len(&self) -> usize { - self.body().unwrap_or(&b""[..]).len() + self.body().len() } } @@ -2545,16 +2538,8 @@ mod test { let expected = to_unknown_packet(expected).unwrap(); let got = to_unknown_packet(got).unwrap(); - let expected_body = if let Some(ref data) = expected.body() { - &data[..] - } else { - &b""[..] - }; - let got_body = if let Some(ref data) = got.body() { - &data[..] - } else { - &b""[..] - }; + let expected_body = expected.body(); + let got_body = got.body(); let mut fail = false; if expected.tag() != got.tag() { |