summaryrefslogtreecommitdiffstats
path: root/openpgp/src/types/bitfield.rs
blob: d4a896bc1ad14179cf01ba40b64c1859360eb01b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/// A bitfield.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Bitfield {
    pub(crate) raw: Vec<u8>,
}

impl From<Vec<u8>> for Bitfield {
    fn from(raw: Vec<u8>) -> Self {
        Self { raw }
    }
}

impl Bitfield {
    pub fn iter<'a>(&'a self) -> impl Iterator<Item = usize> + 'a {
        self.raw.iter()
            .flat_map(|b| {
                (0..8).into_iter().map(move |i| {
                    b & (1 << i) != 0
                })
            })
            .enumerate()
            .filter_map(|(i, v)| if v { Some(i) } else { None })
    }

    pub fn padding_len(&self) -> usize {
        let mut padding = 0;
        for i in (0..self.raw.len()).rev() {
            if self.raw[i] == 0 {
                padding += 1;
            } else {
                break;
            }
        }
        padding
    }

    /// Compares two feature sets for semantic equality.
    pub fn normalized_eq(&self, other: &Self) -> bool {
        let (small, big) = if self.raw.len() < other.raw.len() {
            (self, other)
        } else {
            (other, self)
        };

        for (s, b) in small.raw.iter().zip(big.raw.iter()) {
            if s != b {
                return false;
            }
        }

        for &b in &big.raw[small.raw.len()..] {
            if b != 0 {
                return false;
            }
        }

        true
    }

    /// Returns a slice containing the raw values.
    pub(crate) fn as_slice(&self) -> &[u8] {
        &self.raw
    }

    /// Returns whether the specified flag is set.
    pub fn get(&self, bit: usize) -> bool {
        let byte = bit / 8;

        if byte >= self.raw.len() {
            // Unset bits are false.
            false
        } else {
            (self.raw[byte] & (1 << (bit % 8))) != 0
        }
    }

    /// Remove any trailing padding.
    fn clear_padding(mut self) -> Self {
        while self.raw.len() > 0 && self.raw[self.raw.len() - 1] == 0 {
            self.raw.truncate(self.raw.len() - 1);
        }

        self
    }

    /// 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()
    }

    /// Clears the specified flag.
    ///
    /// This also clears any padding (trailing NUL bytes).
    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()
    }
}