diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2020-07-07 15:47:17 +0200 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2020-07-07 16:00:52 +0200 |
commit | 859fd96cf006cfb0f05ad606d514a7eceb7e2724 (patch) | |
tree | 517deffc8364ed0f7e42f15578f2d59ea807633f /openpgp/src | |
parent | e6d6bcc215d4a8abaf3fef791f38031b3ca87992 (diff) |
openpgp: Implement Marshall traits for SubpacketLength.
Diffstat (limited to 'openpgp/src')
-rw-r--r-- | openpgp/src/packet/signature/subpacket.rs | 40 | ||||
-rw-r--r-- | openpgp/src/parse.rs | 4 | ||||
-rw-r--r-- | openpgp/src/serialize.rs | 33 |
3 files changed, 45 insertions, 32 deletions
diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs index 1cdfc88d..169a2c14 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -80,7 +80,7 @@ use crate::{ Fingerprint, KeyID, SignatureType, - serialize::{Marshal, MarshalInto}, + serialize::MarshalInto, }; use crate::types::{ AEADAlgorithm, @@ -973,10 +973,10 @@ impl Subpacket { #[derive(Clone, Debug, Hash, Eq)] pub(crate) struct SubpacketLength { /// The length. - len: u32, + pub(crate) len: u32, /// The length encoding used in the serialized form. /// If this is `None`, optimal encoding will be used. - raw: Option<Vec<u8>>, + pub(crate) raw: Option<Vec<u8>>, } impl From<u32> for SubpacketLength { @@ -998,14 +998,14 @@ impl PartialEq for SubpacketLength { self_raw == other_raw }, (Some(self_raw), None) => { - let mut other_raw: Vec<u8> = Vec::with_capacity(5); - other.serialize(&mut other_raw).unwrap(); - self_raw == &other_raw + let mut other_raw = [0; 5]; + other.serialize_into(&mut other_raw[..self.serialized_len()]).unwrap(); + &self_raw[..] == &other_raw[..self.serialized_len()] }, (None, Some(other_raw)) => { - let mut self_raw: Vec<u8> = Vec::with_capacity(5); - self.serialize(&mut self_raw).unwrap(); - &self_raw == other_raw + let mut self_raw = [0; 5]; + self.serialize_into(&mut self_raw[..self.serialized_len()]).unwrap(); + &self_raw[..self.serialized_len()] == &other_raw[..] }, } } @@ -1016,33 +1016,11 @@ impl SubpacketLength { Self { len, raw } } - /// Writes the subpacket length to `sink`. - pub(crate) fn serialize(&self, sink: &mut dyn std::io::Write) - -> Result<()> { - match self.raw { - Some(ref raw) => sink.write_all(raw)?, - None => { - BodyLength::serialize(&BodyLength::Full(self.len), sink)? - } - }; - - Ok(()) - } - /// Returns the length. pub(crate) fn len(&self) -> usize { self.len as usize } - /// Returns the length of the serialized subpacket length. - pub(crate) fn serialized_len(&self) -> usize { - if let Some(ref raw) = self.raw { - raw.len() - } else { - Self::len_optimal_encoding(self.len) - } - } - /// Returns the length of the optimal encoding of `len`. pub(crate) fn len_optimal_encoding(len: u32) -> usize { BodyLength::serialized_len(&BodyLength::Full(len)) diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index c2def345..75e59da9 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -233,6 +233,8 @@ use crate::packet::signature::subpacket::{ SubpacketValue, }; +use crate::serialize::MarshalInto; + mod packet_pile_parser; pub use self::packet_pile_parser::PacketPileParser; @@ -1674,6 +1676,8 @@ impl SubpacketLength { #[cfg(test)] quickcheck! { fn length_roundtrip(l: u32) -> bool { + use crate::serialize::Marshal; + let length = SubpacketLength::from(l); let mut encoded = Vec::new(); length.serialize(&mut encoded).unwrap(); diff --git a/openpgp/src/serialize.rs b/openpgp/src/serialize.rs index 9c0c79d9..3b04a085 100644 --- a/openpgp/src/serialize.rs +++ b/openpgp/src/serialize.rs @@ -153,7 +153,7 @@ use crate::packet::header::{ CTBOld, }; use crate::packet::signature::subpacket::{ - SubpacketArea, Subpacket, SubpacketValue, + SubpacketArea, Subpacket, SubpacketValue, SubpacketLength }; use crate::packet::prelude::*; use crate::types::{ @@ -1477,6 +1477,37 @@ impl MarshalInto for SubpacketValue { } } +impl Marshal for SubpacketLength { + /// Writes the subpacket length to `sink`. + fn serialize(&self, sink: &mut dyn std::io::Write) + -> Result<()> { + match self.raw { + Some(ref raw) => sink.write_all(raw)?, + None => { + BodyLength::serialize(&BodyLength::Full(self.len() as u32), sink)? + } + }; + + Ok(()) + } +} + +impl MarshalInto for SubpacketLength { + /// Returns the length of the serialized subpacket length. + fn serialized_len(&self) -> usize { + if let Some(ref raw) = self.raw { + raw.len() + } else { + Self::len_optimal_encoding(self.len() as u32) + } + } + + fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> { + generic_serialize_into(self, buf) + } +} + + impl Marshal for RevocationKey { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { let (pk_algo, fp) = self.revoker(); |