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
use crate::{DecodeError, DecodeResult};
use std::ops::Deref;
#[cfg(target_family = "unix")]
use std::os::unix::io::RawFd;
pub struct Decoder<'a, T>
where
T: Deref<Target = [u8]>,
{
pub(crate) buf: &'a T,
pub(crate) offset: usize,
#[cfg(target_family = "unix")]
pub(crate) fds: &'a [RawFd],
#[cfg(target_family = "unix")]
pub(crate) offset_fds: usize,
}
impl<'a, T> Decoder<'a, T>
where
T: Deref<Target = [u8]>,
{
pub(crate) fn algin(&mut self, a: usize) -> DecodeResult<()> {
while self.offset % a != 0 {
if let Some(b) = self.buf.get(self.offset) {
if *b != 0 {
return Err(DecodeError::Padding);
}
} else {
return Err(DecodeError::TooShort);
}
self.offset += 1;
}
Ok(())
}
pub fn new(buf: &'a T) -> Decoder<'a, T> {
Decoder {
buf,
offset: 0,
#[cfg(target_family = "unix")]
fds: &[],
#[cfg(target_family = "unix")]
offset_fds: 0,
}
}
pub fn with_offset(buf: &'a T, offset: usize) -> Decoder<'a, T> {
Decoder {
buf,
offset,
#[cfg(target_family = "unix")]
fds: &[],
#[cfg(target_family = "unix")]
offset_fds: 0,
}
}
#[cfg(target_family = "unix")]
pub fn with_fds(
buf: &'a T,
offset: usize,
fds: &'a [RawFd],
offset_fds: usize,
) -> Decoder<'a, T> {
Decoder {
buf,
offset,
fds,
offset_fds,
}
}
pub fn get_offset(&self) -> usize {
self.offset
}
#[cfg(target_family = "unix")]
pub fn get_offset_fds(&self) -> usize {
self.offset_fds
}
}