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
use crate::{DecodeError, NestedDecode, NestedDecodeInput};
impl<'a> NestedDecodeInput for &'a [u8] {
fn remaining_len(&self) -> usize {
self.len()
}
fn read_into(&mut self, into: &mut [u8]) -> Result<(), DecodeError> {
if into.len() > self.len() {
return Err(DecodeError::INPUT_TOO_SHORT);
}
let len = into.len();
into.copy_from_slice(&self[..len]);
*self = &self[len..];
Ok(())
}
fn read_into_or_exit<ExitCtx: Clone>(
&mut self,
into: &mut [u8],
c: ExitCtx,
exit: fn(ExitCtx, DecodeError) -> !,
) {
if into.len() > self.len() {
exit(c, DecodeError::INPUT_TOO_SHORT);
}
let len = into.len();
into.copy_from_slice(&self[..len]);
*self = &self[len..];
}
}
pub fn dep_decode_from_byte_slice<D: NestedDecode>(input: &[u8]) -> Result<D, DecodeError> {
let mut_slice = &mut &*input;
let result = D::dep_decode(mut_slice);
if !mut_slice.is_empty() {
return Err(DecodeError::INPUT_TOO_LONG);
}
result
}
pub fn dep_decode_from_byte_slice_or_exit<D: NestedDecode, ExitCtx: Clone>(
input: &[u8],
c: ExitCtx,
exit: fn(ExitCtx, DecodeError) -> !,
) -> D {
let mut_slice = &mut &*input;
let result = D::dep_decode_or_exit(mut_slice, c.clone(), exit);
if !mut_slice.is_empty() {
exit(c, DecodeError::INPUT_TOO_LONG);
}
result
}