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
#[macro_export]
macro_rules! make_func {
    ($addy:expr, $res:ty, $($arg:ty),*) => {
        std::mem::transmute::<*const usize, fn($($arg,)*) -> $res>($addy as *const usize)
    }
}

pub fn read_bytes(address: usize, length: usize) -> Vec<u8> {
    let mut res = Vec::<u8>::new(); //T-T-TURBOFISH
    let mut i = 1;
    while i <= length {
        unsafe{res.push(*((address + (length - i)) as *const u8))};
        i += 1;
    };
    res
}

pub fn read_usize(address: usize) -> usize {
    let res = unsafe { *((address as usize) as *mut usize) };
    res
}

pub fn write_bytes(address: usize, bytes: &[u8]) {
    let mut i = 1;
    let length = bytes.len();
    for byte in bytes.into_iter() {
        unsafe { *((address + (length - i) as usize) as *mut u8) = *byte };
        i += 1;
    }
}

pub fn write_usize(address: usize, writeme: usize) {
    unsafe { *((address as usize) as *mut usize) = writeme };
}


pub fn search(pattern: &[u8], from: usize, to: usize, wildcard: u8) -> usize {
    if from >= to {
        panic!("the from address is higher than the to address");
    }
    for position in from..to {
        let ressy = read_bytes(position, pattern.len());
        let mut p = 0;
        for res_byte in &ressy {
            if pattern[p] == wildcard {
                p += 1;
                continue // Wildcard, just skip.
            }
            if res_byte != &pattern[p] {
                break // Pattern does not match.
            } else if p == pattern.len() - 1 {
                return position // We found it!
            }
            p += 1;
        }
    }
    0 // Return 0 if we don't find anything :c
}