From 312d46ca596619f3d81430157c003cc4fb0fcf5e Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Wed, 1 Nov 2017 21:43:20 +0100 Subject: Convert CompressedData to use a Message for any containing Packets - Don't just save the decompressed content. Instead, use Message::deserialize to process the compressed data. --- src/openpgp/openpgp.rs | 15 ++++----------- src/openpgp/parse/parse.rs | 42 ++++++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/openpgp/openpgp.rs b/src/openpgp/openpgp.rs index 863c61a0..01473431 100644 --- a/src/openpgp/openpgp.rs +++ b/src/openpgp/openpgp.rs @@ -333,11 +333,11 @@ impl<'a> Deref for Literal { pub struct CompressedData { common: PacketCommon, algo: u8, - content: Box<[u8]>, + content: Message, } // Allow transparent access of common fields. -impl<'a> Deref for CompressedData { +impl Deref for CompressedData { type Target = PacketCommon; fn deref(&self) -> &Self::Target { @@ -347,16 +347,9 @@ impl<'a> Deref for CompressedData { impl std::fmt::Debug for CompressedData { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let l = std::cmp::min(16, self.content.len()); - let mut content_fmt = format!("{:?}", &self.content[..l]); - if l < self.content.len() { - content_fmt.push_str("..."); - } - content_fmt.push_str(&format!(" ({} bytes)", self.content.len())[..]); - f.debug_struct("CompressedData") .field("algo", &self.algo) - .field("content (decompressed)", &content_fmt) + .field("content", &self.content) .finish() } } @@ -374,7 +367,7 @@ pub enum Packet { } // Allow transparent access of common fields. -impl Deref for Packet { +impl<'a> Deref for Packet { type Target = PacketCommon; fn deref(&self) -> &Self::Target { diff --git a/src/openpgp/parse/parse.rs b/src/openpgp/parse/parse.rs index be888ec4..6f03296a 100644 --- a/src/openpgp/parse/parse.rs +++ b/src/openpgp/parse/parse.rs @@ -335,13 +335,14 @@ pub fn compressed_data_body(bio: &mut T) // 100 to 110 - Private/Experimental algorithm let mut deflater : Option> = match algo { 0 => // Uncompressed. - return Ok(CompressedData { - common: PacketCommon { - tag: Tag::CompressedData, - }, - algo: algo, - content: bio.steal_eof()?, - }), + unimplemented!(), + // return Ok(CompressedData { + // common: PacketCommon { + // tag: Tag::CompressedData, + // }, + // algo: algo, + // children: Message::deserialize(bio), + // }), 1 => // Zip. Some(Box::new(bio.deflate_decode())), 2 => // Zlib @@ -355,14 +356,14 @@ pub fn compressed_data_body(bio: &mut T) }; if let Some(ref mut deflater) = deflater { - let mut bio2 = BufferedReaderGeneric::new(deflater, None); + let bio2 = BufferedReaderGeneric::new(deflater, None); return Ok(CompressedData { common: PacketCommon { tag: Tag::CompressedData, }, algo: algo, - content: bio2.steal_eof()?, + content: Message::deserialize(bio2)?, }); } unreachable!(); @@ -391,18 +392,19 @@ fn compressed_data_body_test () { let p = compressed_data_body(&mut bio).unwrap(); println!("{:?}", p); - let mut bio2 = BufferedReaderMemory::new(&p.content); - let h = header(&mut bio2).unwrap(); - assert_eq!(h.ctb.tag, Tag::Literal); - assert_eq!(h.length, BodyLength::Partial(4096)); - - let mut bio3 = BufferedReaderPartialBodyFilter::new(&mut bio2, 4096); - let p = literal_body(&mut bio3).unwrap(); + assert_eq!(p.content.packets.len(), 1); + match p.content.packets[0] { + Packet::Literal(ref l) => { + assert_eq!(l.filename, None); + assert_eq!(l.format, 'b' as u8); + assert_eq!(l.date, 1509219866); + assert_eq!(&expected[..], &l.content[..]); + }, + _ => { + unreachable!(); + }, + } - assert_eq!(p.filename, None); - assert_eq!(p.format, 'b' as u8); - assert_eq!(p.date, 1509219866); - assert_eq!(&expected[..], &p.content[..]); } } -- cgit v1.2.3