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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#![forbid(unsafe_code)]
use binfarce;
use binfarce::Format;
pub fn raw_auditable_data<'a>(data: &'a [u8]) -> Result<&'a [u8], Error> {
match binfarce::detect_format(data) {
Format::Elf32{byte_order} => {
let section = binfarce::elf32::parse(data, byte_order)?
.section_with_name(".rust-deps-v0")?.ok_or(Error::NoAuditData)?;
Ok(data.get(section.range()?).ok_or(Error::UnexpectedEof)?)
},
Format::Elf64{byte_order} => {
let section = binfarce::elf64::parse(data, byte_order)?
.section_with_name(".rust-deps-v0")?.ok_or(Error::NoAuditData)?;
Ok(data.get(section.range()?).ok_or(Error::UnexpectedEof)?)
},
Format::Macho => {
let parsed = binfarce::macho::parse(data)?;
let section = parsed.section_with_name("__TEXT", "rust-deps-v0")?;
let section = section.ok_or(Error::NoAuditData)?;
Ok(data.get(section.range()?).ok_or(Error::UnexpectedEof)?)
},
Format::PE => {
let parsed = binfarce::pe::parse(data)?;
let section = parsed.section_with_name("rdep-v0")?.ok_or(Error::NoAuditData)?;
Ok(data.get(section.range()?).ok_or(Error::UnexpectedEof)?)
}
_ => Err(Error::NotAnExecutable)
}
}
#[derive(Debug, Copy, Clone)]
pub enum Error {
NoAuditData,
NotAnExecutable,
UnexpectedEof,
MalformedFile,
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let message = match self {
Error::NoAuditData => "No audit data found in the executable",
Error::NotAnExecutable => "Not an executable file",
Error::UnexpectedEof => "Unexpected end of file",
Error::MalformedFile => "Malformed executable file",
};
write!(f, "{}", message)
}
}
impl From<binfarce::ParseError> for Error {
fn from(e: binfarce::ParseError) -> Self {
match e {
binfarce::ParseError::MalformedInput => Error::MalformedFile,
binfarce::ParseError::UnexpectedEof => Error::UnexpectedEof,
}
}
}