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
//#![cfg_attr(feature = "nightly", feature(asm,global_asm, asm_const))]
#[warn(missing_docs)]
extern crate core;

use log::{debug, trace, warn};
use std::path::PathBuf;
///This struct will expose certain module private functions, to actually use the api.
#[derive(Debug, Clone)]
pub struct Injector<'a> {
    pub dll: &'a str,
    pub pid: u32,
}

pub(crate) type Result<T> = std::result::Result<T, error::Error>;

pub mod error;
mod platforms;

impl<'a> Injector<'a> {
    ///Create a new Injector object.
    pub fn new(dll: &'a str, pid: u32) -> Self {
        Injector { dll, pid }
    }
    ///Sets the dll
    pub fn set_dll(&mut self, dll: &'a str) {
        self.dll = dll;
    }
    ///Sets the pid
    pub fn set_pid(&mut self, pid: u32) {
        self.pid = pid;
    }
}

impl<'a> Default for Injector<'a> {
    fn default() -> Self {
        Self::new("", 0)
    }
}
///This takes a string, and returns only the last path element
///Since this uses rust builtins, it should "just work".
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()),
    }
}

///This truncates all 0 from the end of a Vec
///This will keep other 0 entries in the Vec perfectly intact.
///This has a worst case performance of o(n).
//todo: improve performance
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 {
    ///This string contains a bunch of special chars, to test methods operating on strings.
    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");
    }
}