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
#[macro_export]
/// Creates a function from an address where:
/// $addy = function address
/// $res = return type
/// $(arg)* = argument types
/// Example:
/// ```rust
/// // in C: int foo() { return 1; }
/// make_func!(address_of_foo, isize, )
/// // in C: int bar(int baz, int foobar) { return baz + foobar; }
/// make_func!(address_of_bar, isize, isize, isize)
/// ```
macro_rules! make_func {
    ($addy:expr, $res:ty, $($arg:ty),*) => {
        std::mem::transmute::<*const usize, fn($($arg,)*) -> $res>($addy as *const usize)
    }
}

// an enum for endianess
pub enum Endian { Big, Little }

pub struct Memory {
    end: Endian
}

impl Memory {
    /// Returns a struct thing with all the functions, prepared w/ endianess.
    pub fn new(endian: Endian) -> Memory {
        Memory {end: endian}
    }
    // Reads "length" amount of bytes starting at address.
    pub fn read_bytes(&self, address: usize, length: usize) -> Vec<u8> {
        let mut result = Vec::<u8>::new(); //TURBO
        let mut i; // we know we're going to index it rn.
        match self.end {
            Endian::Big => {
                i = 0;
                while i <= length {
                    unsafe { result.push(*((address + i) as *const u8)) };
                    i += 1;
                }
            },
            Endian::Little => {
                i = 1;
                while i <= length {
                    unsafe { result.push(*((address + length - i) as *const u8)) };
                    i += 1;
                }
            }
        }
        result
    }
    // Writes bytes, starting at address.
    pub fn write_bytes(&self, address: usize, bytes: &[u8]) {
        let length = bytes.len();
        let mut i;
        match self.end {
            Endian::Big => {
                i = 1; // this should fix it LOL
                for byte in bytes.into_iter() {
                    println!("GenAddr: {:x}", unsafe { *((address + i) as *mut u8) });
                    println!("GenByte: {:x}", *byte);
                    unsafe { *((address + i) as *mut u8) = *byte };
                    i += 1;
                }
            },
            Endian::Little => {
                i = 1;
                for byte in bytes.into_iter() {
                    unsafe { *((address + length - i) as *mut u8) = *byte };
                    i += 1;
                }
            }
        }
    }
    /// Searches for a matching pattern between the addresses "from" and "to".
    pub fn search(&self, pattern: &[u8], from: usize, to: usize, wildcard: u8) -> usize {
        if from >= to {
            panic!("the from address is higher than the to address");
        }
        let length = pattern.len() - 1;
        for position in from..to {
            let bytes = self.read_bytes(position, length);
            let mut p = 0;
            for byte in &bytes {
                if pattern[p] == wildcard {
                    p += 1;
                    continue
                }
                if byte != &pattern[p] {
                    break
                } else if p == length {
                    return position - 1
                }
                p += 1;
            }
        }
        0
    }
}