mem_rs/
helpers.rs

1// This file is part of the mem-rs distribution (https://github.com/FrankvdStam/mem-rs).
2// Copyright (c) 2022 Frank van der Stam.
3// https://github.com/FrankvdStam/mem-rs/blob/main/LICENSE
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, version 3.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17use std::path::Path;
18use windows::core::{PCSTR, PCWSTR};
19
20/// Naive linear search for a needle in a haystack with wildcards
21pub fn scan(haystack: &[u8], needle: &[Option<u8>]) -> Option<usize>
22{
23    if haystack.len() == 0
24    {
25        return None;
26    }
27
28    for i in 0..haystack.len() - needle.len()
29    {
30        let mut found = true;
31        for j in 0..needle.len()
32        {
33            if let Some(byte) = needle[j]
34            {
35                if byte != haystack[i + j]
36                {
37                    found = false;
38                    break;
39                }
40            }
41        }
42        if found
43        {
44            return Some(i);
45        }
46    }
47    return None;
48}
49
50/// Converts a string of hex characters into a byte pattern with wildcards.
51/// ? is the character used for wildcards.
52/// Hex characters don't have to be prefixed with 0x
53pub fn to_pattern(str: &str) -> Vec<Option<u8>>
54{
55    let mut vec = Vec::new();
56    for substr in str.split(" ")
57    {
58        if substr == "?"
59        {
60            vec.push(None);
61        }
62        else
63        {
64            vec.push(Some(u8::from_str_radix(substr, 16).expect("invalid hex string in pattern string")));
65        }
66    }
67    return vec;
68}
69
70/// Retrieve only the filename portion from a filepath.
71pub fn get_file_name_from_string(str: &String) -> String
72{
73    return String::from(Path::new(&str).file_name().unwrap().to_str().unwrap());
74}
75
76/// Win32 memes. Use with caution.
77pub fn vec_u16_to_u8(vec_u16: &Vec<u16>) -> Vec<u8>
78{
79    return unsafe { vec_u16.align_to::<u8>().1.to_vec() };
80}
81
82/// Win32 memes. Use with caution.
83pub fn w32str_to_string(w32str: &Vec<u16>) -> String
84{
85    return w32str.iter().map(|&v| (v & 0xFF) as u8).take_while(|&c| c != 0).map(|c| c as char).collect();
86}
87
88/// Win32 memes. Use with caution.
89pub fn get_w32str_from_str(str: &str) -> Vec<u16>
90{
91    return str.encode_utf16().collect();
92}
93
94/// Win32 memes. Use with caution.
95pub fn get_pcwstr_from_str(str: &str) -> PCWSTR
96{
97    let vec: Vec<u16> = str.encode_utf16().collect();
98    return PCWSTR(vec.as_ptr());
99}
100
101/// Win32 memes. Use with caution.
102pub fn get_pcstr_from_str(str: &str) -> PCSTR
103{
104    return PCSTR(str.as_ptr());
105}