mirror of
https://git.intege.rs/xlib/x.git
synced 2025-12-06 04:45:02 +00:00
initial
This commit is contained in:
12
proc_macro/src/lib.rs
Normal file
12
proc_macro/src/lib.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
#![feature(proc_macro_quote)]
|
||||
#![feature(proc_macro_span)]
|
||||
#![feature(custom_inner_attributes)]
|
||||
|
||||
use proc_macro::*;
|
||||
|
||||
mod sigscan;
|
||||
|
||||
#[proc_macro]
|
||||
pub fn signature(input: TokenStream) -> TokenStream {
|
||||
sigscan::macro_proc(input)
|
||||
}
|
||||
50
proc_macro/src/sigscan.rs
Normal file
50
proc_macro/src/sigscan.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
extern crate proc_macro;
|
||||
use proc_macro::*;
|
||||
use syn::*;
|
||||
|
||||
enum Operation {
|
||||
Byte(u8),
|
||||
Wildcard,
|
||||
}
|
||||
|
||||
fn generate_scanner(operations: Vec<Operation>) -> TokenStream {
|
||||
let mut output: String = "| b: &[u8] | -> Option<usize> {\n".to_string();
|
||||
|
||||
output.push_str(format!("for i in 0..b.len()-{} {{",operations.len()).as_str());
|
||||
for (i,op) in operations.iter().enumerate() {
|
||||
if let Operation::Byte(byte) = op {
|
||||
output.push_str(format!("if b[i+{}] != 0x{:x} {{ continue; }}", i, byte).as_str());
|
||||
}
|
||||
}
|
||||
|
||||
output.push_str("return Some(i);");
|
||||
output.push_str("};");
|
||||
|
||||
output.push_str("None}");
|
||||
output.parse().unwrap()
|
||||
}
|
||||
|
||||
pub fn macro_proc(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as LitStr);
|
||||
let input: String = input.value();
|
||||
|
||||
let mut operations = vec![];
|
||||
|
||||
for hex in input.split(" ") {
|
||||
let op = if hex.eq("?") {
|
||||
Operation::Wildcard
|
||||
} else {
|
||||
match u8::from_str_radix(hex, 16) {
|
||||
Ok(hex) => {
|
||||
Operation::Byte(hex)
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("{}", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
operations.push(op);
|
||||
}
|
||||
|
||||
return generate_scanner(operations);
|
||||
}
|
||||
Reference in New Issue
Block a user