diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-09-11 16:28:54 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-09-17 13:44:06 +0200 |
commit | 0227b4e9c87820b01f4bd3f6969ade8e511e637c (patch) | |
tree | 4128f3442ea34ec51fbbc1ed3049b0d4cce7679c /openpgp | |
parent | aae0f0fecbfa688dbbdf8f5012088220af753737 (diff) |
openpgp: Make packet::Common::body private.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/message/mod.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/mod.rs | 7 | ||||
-rw-r--r-- | openpgp/src/packet_pile.rs | 20 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 11 | ||||
-rw-r--r-- | openpgp/src/serialize/mod.rs | 30 |
5 files changed, 37 insertions, 33 deletions
diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index a2f8b7ce..0efd6bef 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -380,7 +380,7 @@ impl Message { // we treat the content as an opaque message. path.push(0); - if packet.children.is_none() && packet.body.is_some() { + if packet.children.is_none() && packet.body().is_some() { v.push_token(Token::OpaqueContent, &path); } } diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs index 1f65ac04..b048e271 100644 --- a/openpgp/src/packet/mod.rs +++ b/openpgp/src/packet/mod.rs @@ -163,7 +163,7 @@ pub struct Common { /// `PacketParser` is configured to buffer unread content, then /// this is not the packet's entire content; it is just the unread /// content. - pub body: Option<Vec<u8>>, + body: Option<Vec<u8>>, } impl fmt::Debug for Common { @@ -221,6 +221,11 @@ impl Common { if data.len() == 0 { None } else { Some(data) }) .unwrap_or(Vec::new()) } + + pub(crate) // For parse.rs + fn body_mut(&mut self) -> Option<&mut Vec<u8>> { + self.body.as_mut() + } } /// Holds zero or more OpenPGP packets. diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index ba4b5c42..76c8b611 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -602,12 +602,12 @@ mod test { // recurse should now not recurse. Since there is nothing // following the compressed packet, ppr should be EOF. - let (mut packet, ppr) = pp.next().unwrap(); + let (packet, ppr) = pp.next().unwrap(); assert!(ppr.is_none()); // Get the rest of the content and put the initial byte that // we stole back. - let mut content = packet.body.take().unwrap(); + let mut content = packet.body().unwrap().to_vec(); content.insert(0, data[0]); let content = &content.into_boxed_slice()[..]; @@ -659,16 +659,16 @@ mod test { assert_eq!(pile.path_ref_mut(&[ 0, 0 ]).unwrap().tag(), Tag::CompressedData); - for (i, t) in text.iter().enumerate() { + for (i, t) in text.into_iter().enumerate() { assert_eq!(pile.path_ref(&[ 0, 0, i ]).unwrap().tag(), Tag::Literal); assert_eq!(pile.path_ref_mut(&[ 0, 0, i ]).unwrap().tag(), Tag::Literal); - assert_eq!(pile.path_ref(&[ 0, 0, i ]).unwrap().body, - Some(t.to_vec())); - assert_eq!(pile.path_ref_mut(&[ 0, 0, i ]).unwrap().body, - Some(t.to_vec())); + assert_eq!(pile.path_ref(&[ 0, 0, i ]).unwrap().body(), + Some(t)); + assert_eq!(pile.path_ref_mut(&[ 0, 0, i ]).unwrap().body(), + Some(t)); } // Try a few out of bounds accesses. @@ -721,7 +721,7 @@ mod test { let children = pile.into_children().collect::<Vec<Packet>>(); assert_eq!(children.len(), 1, "{:#?}", children); if let Packet::Literal(ref literal) = children[0] { - assert_eq!(literal.common.body, Some(b"two".to_vec()), + assert_eq!(literal.body(), Some(&b"two"[..]), "{:#?}", literal); } else { panic!("WTF"); @@ -759,7 +759,7 @@ mod test { .children() .map(|p| { if let Packet::Literal(ref literal) = p { - &literal.common.body.as_ref().unwrap()[..] + literal.body().unwrap() } else { panic!("Expected a literal packet, got: {:?}", p); } @@ -816,7 +816,7 @@ mod test { .children.as_ref().unwrap().children() .map(|p| { if let Packet::Literal(ref literal) = p { - &literal.common.body.as_ref().unwrap()[..] + literal.body().unwrap() } else { panic!("Expected a literal packet, got: {:?}", p); } diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index 9161c0a8..896b4cf9 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -3257,15 +3257,14 @@ 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(mut body) = self.packet.body.take() { + if let Some(body) = self.packet.body_mut() { body.append(&mut rest); - self.packet.body = Some(body); } else { - self.packet.body = Some(rest); + self.packet.set_body(rest); } } - if let Some(body) = self.packet.body.as_ref() { + if let Some(body) = self.packet.body() { Ok(&body[..]) } else { Ok(&b""[..]) @@ -3503,7 +3502,7 @@ fn packet_parser_reader_interface() { let (packet, ppr) = pp.recurse().unwrap(); assert!(ppr.is_none()); // Since we read all of the data, we expect content to be None. - assert!(packet.body.is_none()); + assert!(packet.body().is_none()); } impl<'a> PacketParser<'a> { @@ -3895,7 +3894,7 @@ mod test { "{:?}", pp.packet); } else { pp.buffer_unread_content().unwrap(); - assert_eq!(&pp.packet.body.as_ref().unwrap()[..], + assert_eq!(pp.packet.body().unwrap(), &test.plaintext.content()[..], "{:?}", pp.packet); } diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index 2eaa6863..82b68bd8 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -923,7 +923,7 @@ impl SerializeInto for S2K { impl Serialize for Unknown { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { - let body = if let Some(ref body) = self.common.body { + let body = if let Some(body) = self.body() { &body[..] } else { &b""[..] @@ -1613,7 +1613,7 @@ impl Literal { if write_tag { let len = 1 + (1 + filename.len()) + 4 - + self.common.body.as_ref().map(|b| b.len()).unwrap_or(0); + + self.body().as_ref().map(|b| b.len()).unwrap_or(0); CTB::new(Tag::Literal).serialize(o)?; BodyLength::Full(len as u32).serialize(o)?; } @@ -1627,7 +1627,7 @@ impl Literal { impl Serialize for Literal { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { - let body = if let Some(ref body) = self.common.body { + let body = if let Some(body) = self.body() { &body[..] } else { &b""[..] @@ -1651,7 +1651,7 @@ impl Serialize for Literal { impl NetLength for Literal { fn net_len(&self) -> usize { 1 + (1 + self.filename().map(|f| f.len()).unwrap_or(0)) + 4 - + self.common.body.as_ref().map(|b| b.len()).unwrap_or(0) + + self.body().as_ref().map(|b| b.len()).unwrap_or(0) } } @@ -1678,7 +1678,7 @@ impl Serialize for CompressedData { self.algorithm(), self.common.children.as_ref().map( |cont| cont.children().len()), - self.common.body.as_ref().map(|body| body.len())); + self.body().as_ref().map(|body| body.len())); } let o = stream::Message::new(o); @@ -1693,7 +1693,7 @@ impl Serialize for CompressedData { } // Append the data. - if let Some(ref data) = self.common.body { + if let Some(data) = self.body() { o.write_all(data)?; } @@ -1708,7 +1708,7 @@ impl NetLength for CompressedData { children.packets.iter().map(|p| p.serialized_len()) .sum() }).unwrap_or(0) - + self.common.body.as_ref().map(|body| body.len()).unwrap_or(0); + + self.body().as_ref().map(|body| body.len()).unwrap_or(0); // Worst case, the data gets larger. Account for that. let inner_length = inner_length + cmp::max(inner_length / 2, 128); @@ -1912,7 +1912,7 @@ impl Serialize for SEIP { .into()); } else { o.write_all(&[self.version()])?; - if let Some(ref body) = self.common.body { + if let Some(body) = self.body() { o.write_all(&body[..])?; } } @@ -1924,7 +1924,7 @@ impl Serialize for SEIP { impl NetLength for SEIP { fn net_len(&self) -> usize { 1 // Version. - + self.common.body.as_ref().map(|b| b.len()).unwrap_or(0) + + self.body().as_ref().map(|b| b.len()).unwrap_or(0) } } @@ -2016,7 +2016,7 @@ impl Serialize for AED1 { } else { self.serialize_headers(o)?; - if let Some(ref body) = self.common.body { + if let Some(body) = self.body() { o.write_all(&body[..])?; } } @@ -2032,7 +2032,7 @@ impl NetLength for AED1 { } else { 4 // Headers. + self.iv().len() - + self.common.body.as_ref().map(|b| b.len()).unwrap_or(0) + + self.body().as_ref().map(|b| b.len()).unwrap_or(0) } } } @@ -2541,12 +2541,12 @@ 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.common.body { + let expected_body = if let Some(ref data) = expected.body() { &data[..] } else { &b""[..] }; - let got_body = if let Some(ref data) = got.common.body { + let got_body = if let Some(ref data) = got.body() { &data[..] } else { &b""[..] @@ -2727,14 +2727,14 @@ mod test { eprintln!("Orig:"); let p = pile.children().next().unwrap(); eprintln!("{:?}", p); - let body = &p.body.as_ref().unwrap()[..]; + let body = p.body().unwrap(); eprintln!("Body: {}", body.len()); eprintln!("{}", binary_pp(body)); eprintln!("Reparsed:"); let p = pile2.children().next().unwrap(); eprintln!("{:?}", p); - let body = &p.body.as_ref().unwrap()[..]; + let body = p.body().unwrap(); eprintln!("Body: {}", body.len()); eprintln!("{}", binary_pp(body)); |