summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2023-07-03 12:26:12 +0200
committerJustus Winter <justus@sequoia-pgp.org>2023-07-18 17:06:52 +0200
commitdc8f39503894b282d0a98e5012e512768cabb49f (patch)
tree28e240371a483bd899fab194d75381451af6521d
parent05f4e30e72e4aff547fd3496a48c42359e73c02f (diff)
openpgp: Don't implicitly canonicalize Bitfields.
-rw-r--r--openpgp/src/types/bitfield.rs15
-rw-r--r--openpgp/src/types/features.rs4
-rw-r--r--openpgp/src/types/key_flags.rs4
-rw-r--r--openpgp/src/types/server_preferences.rs4
4 files changed, 12 insertions, 15 deletions
diff --git a/openpgp/src/types/bitfield.rs b/openpgp/src/types/bitfield.rs
index 5fccbbaa..d54ba2be 100644
--- a/openpgp/src/types/bitfield.rs
+++ b/openpgp/src/types/bitfield.rs
@@ -92,8 +92,8 @@ impl Bitfield {
}
}
- /// Remove any trailing padding.
- fn clear_padding(mut self) -> Self {
+ /// Canonicalize by removing any trailing zero bytes.
+ pub fn canonicalize(mut self) -> Self {
while !self.raw.is_empty() && self.raw[self.raw.len() - 1] == 0 {
self.raw.truncate(self.raw.len() - 1);
}
@@ -102,27 +102,24 @@ impl Bitfield {
}
/// Sets the specified flag.
- ///
- /// This also clears any padding (trailing NUL bytes).
pub fn set(mut self, bit: usize) -> Self {
let byte = bit / 8;
while self.raw.len() <= byte {
self.raw.push(0);
}
self.raw[byte] |= 1 << (bit % 8);
-
- self.clear_padding()
+ self
}
/// Clears the specified flag.
///
- /// This also clears any padding (trailing NUL bytes).
+ /// Note: This does not implicitly canonicalize the bit field. To
+ /// do that, invoke [`Bitfield::canonicalize`].
pub fn clear(mut self, bit: usize) -> Self {
let byte = bit / 8;
if byte < self.raw.len() {
self.raw[byte] &= !(1 << (bit % 8));
}
-
- self.clear_padding()
+ self
}
}
diff --git a/openpgp/src/types/features.rs b/openpgp/src/types/features.rs
index b085a000..856ccf2f 100644
--- a/openpgp/src/types/features.rs
+++ b/openpgp/src/types/features.rs
@@ -199,7 +199,7 @@ impl Features {
/// # Ok(()) }
/// ```
pub fn set(self, bit: usize) -> Self {
- Self(self.0.set(bit))
+ Self(self.0.set(bit).canonicalize())
}
/// Clears the specified feature flag.
@@ -225,7 +225,7 @@ impl Features {
/// # Ok(()) }
/// ```
pub fn clear(self, bit: usize) -> Self {
- Self(self.0.clear(bit))
+ Self(self.0.clear(bit).canonicalize())
}
/// Returns whether the MDC feature flag is set.
diff --git a/openpgp/src/types/key_flags.rs b/openpgp/src/types/key_flags.rs
index a71cff03..a1867bad 100644
--- a/openpgp/src/types/key_flags.rs
+++ b/openpgp/src/types/key_flags.rs
@@ -238,7 +238,7 @@ impl KeyFlags {
/// # Ok(()) }
/// ```
pub fn set(self, bit: usize) -> Self {
- Self(self.0.set(bit))
+ Self(self.0.set(bit).canonicalize())
}
/// Clears the specified key flag.
@@ -262,7 +262,7 @@ impl KeyFlags {
/// # Ok(()) }
/// ```
pub fn clear(self, bit: usize) -> Self {
- Self(self.0.clear(bit))
+ Self(self.0.clear(bit).canonicalize())
}
/// This key may be used to certify other keys.
diff --git a/openpgp/src/types/server_preferences.rs b/openpgp/src/types/server_preferences.rs
index 4708fd3c..f2a8e637 100644
--- a/openpgp/src/types/server_preferences.rs
+++ b/openpgp/src/types/server_preferences.rs
@@ -176,7 +176,7 @@ impl KeyServerPreferences {
/// # Ok(()) }
/// ```
pub fn set(self, bit: usize) -> Self {
- Self(self.0.set(bit))
+ Self(self.0.set(bit).canonicalize())
}
/// Clears the specified keyserver preference flag.
@@ -200,7 +200,7 @@ impl KeyServerPreferences {
/// # Ok(()) }
/// ```
pub fn clear(self, bit: usize) -> Self {
- Self(self.0.clear(bit))
+ Self(self.0.clear(bit).canonicalize())
}
/// Returns whether the certificate's owner requests that the