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
use super::*;
#[derive(Debug)]
pub struct ByteSliceDriver<'a> {
mode: DriverMode,
input: &'a [u8],
}
impl<'a> ByteSliceDriver<'a> {
pub fn new(input: &'a [u8], mode: Option<DriverMode>) -> Self {
let mode = mode.unwrap_or(DriverMode::Direct);
Self { input, mode }
}
#[inline]
pub fn new_direct(input: &'a [u8]) -> Self {
Self::new(input, Some(DriverMode::Direct))
}
#[inline]
pub fn new_forced(input: &'a [u8]) -> Self {
Self::new(input, Some(DriverMode::Forced))
}
}
impl<'a> FillBytes for ByteSliceDriver<'a> {
#[inline]
fn mode(&self) -> DriverMode {
self.mode
}
#[inline]
fn fill_bytes(&mut self, bytes: &mut [u8]) -> Option<()> {
match self.mode {
DriverMode::Forced => {
let offset = self.input.len().min(bytes.len());
let (current, remaining) = self.input.split_at(offset);
let (bytes_to_fill, bytes_to_zero) = bytes.split_at_mut(offset);
bytes_to_fill.copy_from_slice(current);
for byte in bytes_to_zero.iter_mut() {
*byte = 0;
}
self.input = remaining;
Some(())
}
DriverMode::Direct => {
if bytes.len() > self.input.len() {
return None;
}
let (current, remaining) = self.input.split_at(bytes.len());
bytes.copy_from_slice(current);
self.input = remaining;
Some(())
}
}
}
}
impl<'a> Driver for ByteSliceDriver<'a> {
gen_from_bytes!();
}