diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-02-20 11:58:46 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-02-20 11:58:46 +0100 |
commit | 2d92addba5e985994cebd8a41b8e921ba6137bc9 (patch) | |
tree | 1d5b396bb9c8dbcde1af9e642dabe1845234a49e | |
parent | caa8e0df9bdfe13c8c43062fdc05cf2fa7ebbf2d (diff) |
openpgp: Improve debug formatting of the bitfield types.
- Do not erroneously report padding for KeyFlags.
- Print unknown flags and padding for KeyServerPreferences.
- Provide nicer output for Features.
-rw-r--r-- | openpgp/src/types/features.rs | 34 | ||||
-rw-r--r-- | openpgp/src/types/key_flags.rs | 5 | ||||
-rw-r--r-- | openpgp/src/types/server_preferences.rs | 18 |
3 files changed, 55 insertions, 2 deletions
diff --git a/openpgp/src/types/features.rs b/openpgp/src/types/features.rs index ca4ac999..c17c9676 100644 --- a/openpgp/src/types/features.rs +++ b/openpgp/src/types/features.rs @@ -1,7 +1,8 @@ +use std::fmt; use std::hash::{Hash, Hasher}; /// Describes features supported by an OpenPGP implementation. -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct Features{ mdc: bool, aead: bool, @@ -21,6 +22,34 @@ impl Default for Features { } } +impl fmt::Debug for Features { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut dirty = false; + if self.supports_mdc() { + f.write_str("MDC")?; + dirty = true; + } + if self.supports_aead() { + if dirty { f.write_str(", ")?; } + f.write_str("AEAD")?; + dirty = true; + } + if ! self.unknown.is_empty() { + if dirty { f.write_str(", ")?; } + f.write_str("+0x")?; + f.write_str( + &crate::fmt::hex::encode_pretty(&self.unknown))?; + dirty = true; + } + if self.pad_to > FEATURE_FLAGS_N_KNOWN_BYTES + self.unknown.len() { + if dirty { f.write_str(", ")?; } + write!(f, "+padding({} bytes)", self.pad_to - self.unknown.len())?; + } + + Ok(()) + } +} + impl PartialEq for Features { fn eq(&self, other: &Self) -> bool { self.mdc == other.mdc @@ -131,6 +160,9 @@ const FEATURE_FLAG_MDC: u8 = 0x01; /// Encrypted Session Key Packets (packet 3). const FEATURE_FLAG_AEAD: u8 = 0x02; +/// Number of bytes with known flags. +const FEATURE_FLAGS_N_KNOWN_BYTES: usize = 1; + #[cfg(test)] mod tests { use super::*; diff --git a/openpgp/src/types/key_flags.rs b/openpgp/src/types/key_flags.rs index 51e8a9f6..9109abe9 100644 --- a/openpgp/src/types/key_flags.rs +++ b/openpgp/src/types/key_flags.rs @@ -53,7 +53,7 @@ impl fmt::Debug for KeyFlags { f.write_str( &crate::fmt::hex::encode_pretty(&self.unknown))?; } - if self.pad_to > self.unknown.len() { + if self.pad_to > KEY_FLAGS_N_KNOWN_BYTES + self.unknown.len() { write!(f, "+padding({} bytes)", self.pad_to - self.unknown.len())?; } @@ -327,6 +327,9 @@ const KEY_FLAG_AUTHENTICATE: u8 = 0x20; /// than one person. const KEY_FLAG_GROUP_KEY: u8 = 0x80; +/// Number of bytes with known flags. +const KEY_FLAGS_N_KNOWN_BYTES: usize = 1; + #[cfg(test)] mod tests { use super::*; diff --git a/openpgp/src/types/server_preferences.rs b/openpgp/src/types/server_preferences.rs index 549ae07e..42c0a74d 100644 --- a/openpgp/src/types/server_preferences.rs +++ b/openpgp/src/types/server_preferences.rs @@ -18,8 +18,23 @@ impl Default for KeyServerPreferences { impl fmt::Debug for KeyServerPreferences { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut dirty = false; if self.no_modify() { f.write_str("no modify")?; + dirty = true; + } + if ! self.unknown.is_empty() { + if dirty { f.write_str(", ")?; } + f.write_str("+0x")?; + f.write_str( + &crate::fmt::hex::encode_pretty(&self.unknown))?; + dirty = true; + } + if self.pad_to > + KEYSERVER_PREFERENCES_N_KNOWN_BYTES + self.unknown.len() + { + if dirty { f.write_str(", ")?; } + write!(f, "+padding({} bytes)", self.pad_to - self.unknown.len())?; } Ok(()) @@ -105,6 +120,9 @@ impl KeyServerPreferences { /// than one person. const KEYSERVER_PREFERENCE_NO_MODIFY: u8 = 0x80; +/// Number of bytes with known flags. +const KEYSERVER_PREFERENCES_N_KNOWN_BYTES: usize = 1; + #[cfg(test)] mod tests { use super::*; |