#![cfg(feature = "hook")]
use std::hash::Hash;
use std::mem;
#[derive(Debug)]
pub enum Thunk<'a> {
Ordinal(isize),
Name(&'a str),
}
impl<'a> PartialEq for Thunk<'a> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(&Thunk::Ordinal(s), &Thunk::Ordinal(o)) => s == o,
(&Thunk::Name(s), &Thunk::Name(o)) => s == o,
_ => false,
}
}
}
impl<'a> Eq for Thunk<'a> {}
impl<'a> Hash for Thunk<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
mem::discriminant(self).hash(state);
match *self {
Thunk::Ordinal(o) => o.hash(state),
Thunk::Name(n) => n.hash(state),
};
}
}
#[derive(Debug)]
pub struct ProcDesc<'a> {
dll: String,
thunk: Thunk<'a>,
}
impl<'a> PartialEq for ProcDesc<'a> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.dll == other.dll && self.thunk == other.thunk
}
}
impl<'a> Eq for ProcDesc<'a> {}
impl<'a> Hash for ProcDesc<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.dll.hash(state);
self.thunk.hash(state);
}
}
impl<'a> ProcDesc<'a> {
pub fn new(dll: &str, thunk: Thunk<'a>) -> ProcDesc<'a> {
ProcDesc {
dll: dll.to_ascii_lowercase(),
thunk: thunk,
}
}
}
impl<'a> From<&'a str> for ProcDesc<'a> {
fn from(s: &'a str) -> Self {
match s.find('!') {
Some(i) => {
let (dll, proc_name) = s.split_at(i);
ProcDesc {
dll: dll.to_ascii_lowercase(),
thunk: Thunk::Name(&proc_name[1..]),
}
}
None => match s.find('#') {
Some(i) => {
let (dll, proc_ord) = s.split_at(i);
ProcDesc {
dll: dll.to_ascii_lowercase(),
thunk: Thunk::Ordinal(proc_ord[1..].parse().unwrap_or(0)),
}
}
None => ProcDesc {
dll: String::new(),
thunk: Thunk::Ordinal(0),
},
},
}
}
}