mirror of
https://git.intege.rs/xlib/x.git
synced 2025-12-05 20:35:01 +00:00
initial
This commit is contained in:
53
proc_macro/Cargo.lock
generated
Normal file
53
proc_macro/Cargo.lock
generated
Normal file
@@ -0,0 +1,53 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "xgen"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
16
proc_macro/Cargo.toml
Normal file
16
proc_macro/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "xgen"
|
||||
version = "0.0.0"
|
||||
authors = [""]
|
||||
description = "common macros"
|
||||
keywords = []
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "1"
|
||||
proc-macro2 = "1.0.66"
|
||||
syn = { version = "1.0", features = ["full"] }
|
||||
|
||||
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