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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#![warn(missing_docs)]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Injector<'a> {
pub dll: &'a str,
pub pid: u32,
}
pub(crate) type Result<T> = std::result::Result<T, error::Error>;
pub(crate) use log::{debug, error, info, trace, warn};
use std::path::{Path, PathBuf};
pub mod error;
mod platforms;
pub trait Inject {
fn inject(&self) -> Result<()>;
fn eject(&self) -> Result<()>;
fn find_pid<P: AsRef<Path>>(name: P) -> Result<Vec<u32>>;
}
impl<'a> Injector<'a> {
pub fn new(dll: &'a str, pid: u32) -> Self {
Injector { dll, pid }
}
pub fn set_dll(&mut self, dll: &'a str) {
self.dll = dll;
}
pub fn set_pid(&mut self, pid: u32) {
self.pid = pid;
}
#[cfg(target_family = "windows")]
pub fn inject(&self, wait: bool) -> impl Inject + '_ {
platforms::windows::InjectWin { inj: self, wait }
}
#[cfg(target_family = "windows")]
pub fn find_pid<P: AsRef<Path>>(name: P) -> Result<Vec<u32>> {
platforms::windows::InjectWin::find_pid(name)
}
}
impl<'a> Default for Injector<'a> {
fn default() -> Self {
Self::new("", 0)
}
}
pub fn strip_path(dll: &str) -> Result<String> {
let pb = PathBuf::from(dll);
match pb.file_name().and_then(|x| x.to_str()) {
None => Err(error::Error::Io(std::io::Error::from(
std::io::ErrorKind::Unsupported,
))),
Some(v) => Ok(v.to_string()),
}
}
pub fn trim_wide_str(mut v: Vec<u16>) -> Vec<u16> {
while v.last().map(|x| *x) == Some(0) {
v.pop();
}
return v;
}
#[cfg(test)]
mod test {
pub const STR:&str = "This is just any string, since we are not testing anything else, other than setting the dll.!'\r\n\t%$§\"{\\[()]}=?´`öäü^°,.-;:_#+*~<>|³²@";
use crate::Result;
#[test]
fn trim_vec() {
let buf: Vec<u16> = (0..u16::MAX).collect();
let mut buf2 = buf.clone();
buf2.append(&mut [0u16; 100].to_vec());
assert_eq!(super::trim_wide_str(buf2), buf);
}
#[test]
fn strip_path() -> Result<()> {
#[cfg(target_family = "windows")]
assert_eq!(
super::strip_path("C:\\this\\is\\a\\test\\path\\with\\a\\dir\\at\\the\\end\\")?,
"end",
"strip path failed to strip the end of a win path, with a dir at the end"
);
assert_eq!(
super::strip_path("/this/is/a/test/path/with/a/dir/at/the/end/")?,
"end",
"strip path failed to strip the end of a rust path, with a dir at the end"
);
#[cfg(target_family = "windows")]
assert_eq!(super::strip_path("C:\\this\\is\\a\\test\\path\\with\\a\\dir\\at\\the\\end")?,"end","strip path failed to strip the end of a win path, with a dir/extensionless file at the end");
assert_eq!(super::strip_path("/this/is/a/test/path/with/a/dir/at/the/end")?,"end","strip path failed to strip the end of a rust path, with a dir/extensionless file at the end");
#[cfg(target_family = "windows")]
assert_eq!(
super::strip_path(
"C:\\this\\is\\a\\test\\path\\with\\a\\file\\at\\the\\end\\file.txt"
)?,
"file.txt",
"strip path failed to strip the end of a win path, with a file at the end"
);
assert_eq!(
super::strip_path("/this/is/a/test/path/with/a/file/at/the/end/file.txt")?,
"file.txt",
"strip path failed to strip the end of a rust path, with a file at the end"
);
Ok(())
}
#[test]
fn set_dll() {
let mut inj = super::Injector::default();
inj.set_dll(STR);
assert_eq!(inj.dll, STR, "Setter did not correctly set the dll string");
}
#[test]
fn set_pid() {
let mut inj = super::Injector::default();
const PID: u32 = 0;
inj.set_pid(PID);
assert_eq!(inj.pid, PID, "Setter did not correctly set the PID");
}
}