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
use crate::state::Sha3State;
use digest::XofReader;
#[cfg(feature = "std")]
use std::io;
pub struct Sha3XofReader {
state: Sha3State,
rate: usize,
pos: usize,
}
impl Sha3XofReader {
pub(crate) fn new(state: Sha3State, rate: usize) -> Self {
Sha3XofReader {
state,
rate,
pos: 0,
}
}
}
impl XofReader for Sha3XofReader {
fn read(&mut self, mut buffer: &mut [u8]) {
let rem = self.rate - self.pos;
let n = buffer.len();
if n >= rem {
let (l, r) = { buffer }.split_at_mut(rem);
buffer = r;
self.state.as_bytes(|state| {
l.copy_from_slice(&state[self.pos..self.rate]);
});
self.state.apply_f();
} else {
self.state.as_bytes(|state| {
buffer.copy_from_slice(&state[self.pos..self.pos + n]);
});
self.pos += n;
return;
}
while buffer.len() >= self.rate {
let (l, r) = { buffer }.split_at_mut(self.rate);
buffer = r;
self.state.as_bytes(|state| {
l.copy_from_slice(&state[..self.rate]);
});
self.state.apply_f();
}
let n = buffer.len();
self.pos = n;
self.state.as_bytes(|state| {
buffer.copy_from_slice(&state[..n]);
});
}
}
#[cfg(feature = "std")]
impl io::Read for Sha3XofReader {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
<Self as XofReader>::read(self, buf);
Ok(buf.len())
}
}