Coverage Report

Created: 2025-07-11 07:02

/src/spdm-rs/spdmlib/src/crypto/bytes_mut_scrubbed.rs
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2023 Intel Corporation
2
//
3
// SPDX-License-Identifier: Apache-2.0 or MIT
4
5
use bytes::{buf::IntoIter, Buf, BufMut, Bytes, BytesMut};
6
use core::{
7
    borrow::{Borrow, BorrowMut},
8
    cmp, hash,
9
    iter::FromIterator,
10
    ops::{Deref, DerefMut},
11
};
12
use zeroize::Zeroize;
13
14
#[derive(Default)]
15
pub struct BytesMutStrubbed {
16
    bytes_mut: BytesMut,
17
}
18
19
impl BytesMutStrubbed {
20
    #[inline]
21
0
    pub fn with_capacity(capacity: usize) -> BytesMutStrubbed {
22
0
        BytesMutStrubbed {
23
0
            bytes_mut: BytesMut::with_capacity(capacity),
24
0
        }
25
0
    }
26
27
    #[inline]
28
0
    pub fn new() -> BytesMutStrubbed {
29
0
        BytesMutStrubbed {
30
0
            bytes_mut: BytesMut::new(),
31
0
        }
32
0
    }
33
34
    #[inline]
35
0
    pub fn len(&self) -> usize {
36
0
        self.bytes_mut.len()
37
0
    }
38
39
    #[inline]
40
0
    pub fn is_empty(&self) -> bool {
41
0
        self.bytes_mut.is_empty()
42
0
    }
43
44
    #[inline]
45
0
    pub fn capacity(&self) -> usize {
46
0
        self.bytes_mut.capacity()
47
0
    }
48
49
0
    pub fn extend_from_slice(&mut self, extend: &[u8]) {
50
0
        self.bytes_mut.extend_from_slice(extend)
51
0
    }
52
53
    #[inline]
54
0
    pub fn reserve(&mut self, additional: usize) {
55
0
        self.bytes_mut.reserve(additional)
56
0
    }
57
58
0
    pub fn resize(&mut self, new_len: usize, value: u8) {
59
0
        self.bytes_mut.resize(new_len, value)
60
0
    }
61
62
0
    pub fn clear(&mut self) {
63
0
        self.bytes_mut.clear()
64
0
    }
65
66
0
    pub fn truncate(&mut self, len: usize) {
67
0
        self.bytes_mut.truncate(len)
68
0
    }
69
70
0
    pub fn zeroed(len: usize) -> BytesMutStrubbed {
71
0
        BytesMutStrubbed {
72
0
            bytes_mut: BytesMut::zeroed(len),
73
0
        }
74
0
    }
75
76
0
    pub fn put_u8(&mut self, n: u8) {
77
0
        self.bytes_mut.put_u8(n)
78
0
    }
79
}
80
81
impl Drop for BytesMutStrubbed {
82
0
    fn drop(&mut self) {
83
0
        self.bytes_mut[..].zeroize()
84
0
    }
85
}
86
87
impl Buf for BytesMutStrubbed {
88
    #[inline]
89
0
    fn remaining(&self) -> usize {
90
0
        self.bytes_mut.remaining()
91
0
    }
92
93
    #[inline]
94
0
    fn chunk(&self) -> &[u8] {
95
0
        self.bytes_mut.chunk()
96
0
    }
97
98
    #[inline]
99
0
    fn advance(&mut self, cnt: usize) {
100
0
        self.bytes_mut.advance(cnt)
101
0
    }
102
103
0
    fn copy_to_bytes(&mut self, len: usize) -> Bytes {
104
0
        self.bytes_mut.copy_to_bytes(len)
105
0
    }
106
}
107
108
impl AsRef<[u8]> for BytesMutStrubbed {
109
    #[inline]
110
0
    fn as_ref(&self) -> &[u8] {
111
0
        self.bytes_mut.as_ref()
112
0
    }
113
}
114
115
impl Deref for BytesMutStrubbed {
116
    type Target = [u8];
117
118
    #[inline]
119
0
    fn deref(&self) -> &[u8] {
120
0
        self.bytes_mut.deref()
121
0
    }
122
}
123
124
impl AsMut<[u8]> for BytesMutStrubbed {
125
    #[inline]
126
0
    fn as_mut(&mut self) -> &mut [u8] {
127
0
        self.bytes_mut.as_mut()
128
0
    }
129
}
130
131
impl DerefMut for BytesMutStrubbed {
132
    #[inline]
133
0
    fn deref_mut(&mut self) -> &mut [u8] {
134
0
        self.bytes_mut.deref_mut()
135
0
    }
136
}
137
138
impl<'a> From<&'a [u8]> for BytesMutStrubbed {
139
0
    fn from(src: &'a [u8]) -> BytesMutStrubbed {
140
0
        BytesMutStrubbed {
141
0
            bytes_mut: BytesMut::from(src),
142
0
        }
143
0
    }
144
}
145
146
impl<'a> From<&'a str> for BytesMutStrubbed {
147
0
    fn from(src: &'a str) -> BytesMutStrubbed {
148
0
        BytesMutStrubbed {
149
0
            bytes_mut: BytesMut::from(src),
150
0
        }
151
0
    }
152
}
153
154
impl PartialEq for BytesMutStrubbed {
155
0
    fn eq(&self, other: &BytesMutStrubbed) -> bool {
156
0
        self.bytes_mut.eq(&other.bytes_mut)
157
0
    }
158
}
159
160
impl PartialOrd for BytesMutStrubbed {
161
0
    fn partial_cmp(&self, other: &BytesMutStrubbed) -> Option<cmp::Ordering> {
162
0
        Some(self.cmp(other))
163
0
    }
164
}
165
166
impl Ord for BytesMutStrubbed {
167
0
    fn cmp(&self, other: &BytesMutStrubbed) -> cmp::Ordering {
168
0
        self.bytes_mut.cmp(&other.bytes_mut)
169
0
    }
170
}
171
172
impl Eq for BytesMutStrubbed {}
173
174
impl hash::Hash for BytesMutStrubbed {
175
0
    fn hash<H>(&self, state: &mut H)
176
0
    where
177
0
        H: hash::Hasher,
178
0
    {
179
0
        self.bytes_mut.hash(state)
180
0
    }
181
}
182
183
impl Borrow<[u8]> for BytesMutStrubbed {
184
0
    fn borrow(&self) -> &[u8] {
185
0
        self.bytes_mut.borrow()
186
0
    }
187
}
188
189
impl BorrowMut<[u8]> for BytesMutStrubbed {
190
0
    fn borrow_mut(&mut self) -> &mut [u8] {
191
0
        self.bytes_mut.borrow_mut()
192
0
    }
193
}
194
195
impl Clone for BytesMutStrubbed {
196
0
    fn clone(&self) -> BytesMutStrubbed {
197
0
        BytesMutStrubbed {
198
0
            bytes_mut: self.bytes_mut.clone(),
199
0
        }
200
0
    }
201
}
202
203
impl IntoIterator for BytesMutStrubbed {
204
    type Item = u8;
205
    type IntoIter = IntoIter<BytesMutStrubbed>;
206
207
0
    fn into_iter(self) -> Self::IntoIter {
208
0
        IntoIter::new(self)
209
0
    }
210
}
211
212
impl<'a> IntoIterator for &'a BytesMutStrubbed {
213
    type Item = &'a u8;
214
    type IntoIter = core::slice::Iter<'a, u8>;
215
216
0
    fn into_iter(self) -> Self::IntoIter {
217
0
        self.as_ref().iter()
218
0
    }
219
}
220
221
impl Extend<u8> for BytesMutStrubbed {
222
0
    fn extend<T>(&mut self, iter: T)
223
0
    where
224
0
        T: IntoIterator<Item = u8>,
225
0
    {
226
0
        self.bytes_mut.extend(iter)
227
0
    }
228
}
229
230
impl<'a> Extend<&'a u8> for BytesMutStrubbed {
231
0
    fn extend<T>(&mut self, iter: T)
232
0
    where
233
0
        T: IntoIterator<Item = &'a u8>,
234
0
    {
235
0
        self.bytes_mut.extend(iter)
236
0
    }
237
}
238
239
impl Extend<Bytes> for BytesMutStrubbed {
240
0
    fn extend<T>(&mut self, iter: T)
241
0
    where
242
0
        T: IntoIterator<Item = Bytes>,
243
0
    {
244
0
        self.bytes_mut.extend(iter)
245
0
    }
246
}
247
248
impl FromIterator<u8> for BytesMutStrubbed {
249
0
    fn from_iter<T: IntoIterator<Item = u8>>(into_iter: T) -> Self {
250
0
        BytesMutStrubbed {
251
0
            bytes_mut: BytesMut::from_iter(into_iter),
252
0
        }
253
0
    }
254
}
255
256
impl<'a> FromIterator<&'a u8> for BytesMutStrubbed {
257
0
    fn from_iter<T: IntoIterator<Item = &'a u8>>(into_iter: T) -> Self {
258
0
        BytesMutStrubbed {
259
0
            bytes_mut: BytesMut::from_iter(into_iter),
260
0
        }
261
0
    }
262
}