Skip to content

Commit

Permalink
change dictionary key to raw bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Oct 2, 2023
1 parent 87e6321 commit d3e9e76
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
12 changes: 12 additions & 0 deletions src/cxxstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ impl CxxString {
}
}

pub unsafe fn to_bytes_without_nul(&mut self) -> &[u8] {
std::slice::from_raw_parts(self.ptr, self.len)
}

pub fn size(&self) -> usize {
self.len
}
Expand Down Expand Up @@ -225,6 +229,14 @@ impl CxxString {
}
}

pub unsafe fn to_bytes_without_nul(&mut self) -> &[u8] {
let mut data: *const u8 = self.data.buf.as_ptr();
if self.capa >= 16 {
data = self.data.ptr;
}
std::slice::from_raw_parts(data, self.len)
}

pub unsafe fn as_ptr(&mut self) -> *const u8 {
std::mem::transmute(self)
}
Expand Down
18 changes: 9 additions & 9 deletions src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub static DICTIONARY: Dictionary = Dictionary::new(&CONFIG.settings.dictionary)

#[allow(dead_code)]
pub struct Dictionary {
map: HashMap<String, Vec<u8>>,
map: HashMap<Vec<u8>, Vec<u8>>,
path: &'static str,
}

Expand All @@ -23,21 +23,21 @@ impl Dictionary {
format!("Unable to load dictionary {}", path).as_str(),
utils::MessageIconType::Warning,
);
HashMap::<String, Vec<u8>>::new()
HashMap::<Vec<u8>, Vec<u8>>::new()
}),
path,
}
}

pub fn get(&self, key: &str) -> Option<&Vec<u8>> {
pub fn get(&self, key: &[u8]) -> Option<&Vec<u8>> {
self.map.get(key)
}

pub fn _size(&self) -> usize {
pub fn size(&self) -> usize {
self.map.len()
}

pub fn _data(&self) -> &HashMap<String, Vec<u8>> {
pub fn _data(&self) -> &HashMap<Vec<u8>, Vec<u8>> {
&self.map
}

Expand All @@ -47,15 +47,15 @@ impl Dictionary {
}

#[allow(unused_must_use)]
fn load(path: &str) -> Result<HashMap<String, Vec<u8>>, Box<dyn std::error::Error>> {
fn load(path: &str) -> Result<HashMap<Vec<u8>, Vec<u8>>, Box<dyn std::error::Error>> {
let mut file = std::fs::File::open(path)?;
let mut contents: Vec<u8> = Vec::new();
file.read_to_end(&mut contents);
let mut map = HashMap::<String, Vec<u8>>::new();
let mut map = HashMap::<Vec<u8>, Vec<u8>>::new();
for item in regex::bytes::Regex::new(r#"(?-u)"(.+)","(.+)""#)?.captures_iter(&contents) {
let mut v = Vec::<u8>::from(&item[2]);
let mut v = item[2].to_vec();
v.push(0);
map.insert(String::from_utf8_lossy(&item[1]).to_string(), v);
map.insert(item[1].to_vec(), v);
}
Ok(map)
}
Expand Down
44 changes: 22 additions & 22 deletions src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ pub unsafe fn disable_all() -> Result<(), Box<dyn std::error::Error>> {

#[cfg_attr(target_os = "windows", hook(by_offset))]
#[cfg_attr(target_os = "linux", hook(bypass))]
fn string_copy_n(dst: *mut c_char, src: *const c_char, size: usize) -> *mut c_char {
fn string_copy_n(dst: *mut c_char, src: *const u8, size: usize) -> *mut c_char {
unsafe {
match (utils::cstr(src, size + 1), size > 1) {
(Ok(value), true) => match DICTIONARY.get(value) {
match (std::slice::from_raw_parts(src, size), size > 1) {
(value, true) => match DICTIONARY.get(value) {
Some(translate) => {
let (ptr, len, _) = translate.to_owned().into_raw_parts();
original!(dst, ptr as *const c_char, len - 1)
original!(dst, ptr, len - 1)
}
_ => original!(dst, src, size),
},
Expand All @@ -93,13 +93,13 @@ fn string_copy_n(dst: *mut c_char, src: *const c_char, size: usize) -> *mut c_ch

#[cfg_attr(target_os = "windows", hook(by_offset))]
#[cfg_attr(target_os = "linux", hook(bypass))]
fn string_append_n(dst: *mut c_char, src: *const c_char, size: usize) -> *mut c_char {
fn string_append_n(dst: *mut c_char, src: *const u8, size: usize) -> *mut c_char {
unsafe {
match (utils::cstr(src, size + 1), size > 1) {
(Ok(value), true) => match DICTIONARY.get(value) {
match (std::slice::from_raw_parts(src, size), size > 1) {
(value, true) => match DICTIONARY.get(value) {
Some(translate) => {
let (ptr, len, _) = translate.to_owned().into_raw_parts();
original!(dst, ptr as *const c_char, len - 1)
original!(dst, ptr, len - 1)
}
_ => original!(dst, src, size),
},
Expand All @@ -110,13 +110,13 @@ fn string_append_n(dst: *mut c_char, src: *const c_char, size: usize) -> *mut c_

#[cfg_attr(target_os = "windows", hook(bypass))]
#[cfg_attr(target_os = "linux", hook(by_symbol))]
fn std_string_append(dst: *const u8, src: *const c_char) -> *const u8 {
fn std_string_append(dst: *const u8, src: *const u8) -> *const u8 {
unsafe {
match std::ffi::CStr::from_ptr(src).to_str() {
(Ok(value)) => match DICTIONARY.get(value) {
match std::ffi::CStr::from_ptr(src as *const c_char).to_bytes() {
(value) => match DICTIONARY.get(value) {
Some(translate) => {
let (ptr, _, _) = translate.to_owned().into_raw_parts();
original!(dst, ptr as *const c_char)
original!(dst, ptr)
}
_ => original!(dst, src),
},
Expand All @@ -127,13 +127,13 @@ fn std_string_append(dst: *const u8, src: *const c_char) -> *const u8 {

#[cfg_attr(target_os = "windows", hook(bypass))]
#[cfg_attr(target_os = "linux", hook(by_symbol))]
fn std_string_assign(dst: *const u8, src: *const c_char) -> *const u8 {
fn std_string_assign(dst: *const u8, src: *const u8) -> *const u8 {
unsafe {
match std::ffi::CStr::from_ptr(src).to_str() {
(Ok(value)) => match DICTIONARY.get(value) {
match std::ffi::CStr::from_ptr(src as *const c_char).to_bytes() {
(value) => match DICTIONARY.get(value) {
Some(translate) => {
let (ptr, _, _) = translate.to_owned().into_raw_parts();
original!(dst, ptr as *const c_char)
original!(dst, ptr)
}
_ => original!(dst, src),
},
Expand All @@ -147,8 +147,8 @@ fn std_string_assign(dst: *const u8, src: *const c_char) -> *const u8 {
fn addst(gps: usize, src: *const u8, justify: u8, space: u32) {
unsafe {
let s = CxxString::from_ptr(src);
match s.to_str() {
Ok(converted) => match DICTIONARY.get(converted) {
match s.to_bytes_without_nul() {
converted => match DICTIONARY.get(converted) {
Some(translate) => {
let (ptr, len, _) = translate.to_owned().into_raw_parts();
let mut cxxstr = CxxString::new(ptr, len - 1);
Expand All @@ -172,8 +172,8 @@ fn addst(gps: usize, src: *const u8, justify: u8, space: u32) {
fn addst_top(gps: usize, src: *const u8, justify: u8, space: u32) {
unsafe {
let s = CxxString::from_ptr(src);
match s.to_str() {
Ok(converted) => match DICTIONARY.get(converted) {
match s.to_bytes_without_nul() {
converted => match DICTIONARY.get(converted) {
Some(translate) => {
let (ptr, len, _) = translate.to_owned().into_raw_parts();
let mut cxxstr = CxxString::new(ptr, len - 1);
Expand All @@ -197,8 +197,8 @@ fn addst_top(gps: usize, src: *const u8, justify: u8, space: u32) {
fn addst_flag(gps: usize, src: *const u8, a3: usize, a4: usize, flag: u32) {
unsafe {
let s = CxxString::from_ptr(src);
match s.to_str() {
Ok(converted) => match DICTIONARY.get(converted) {
match s.to_bytes_without_nul() {
converted => match DICTIONARY.get(converted) {
Some(translate) => {
let (ptr, len, _) = translate.to_owned().into_raw_parts();
let mut cxxstr = CxxString::new(ptr, len - 1);
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use log::LevelFilter;
use log::{error, info, trace};

use crate::config::CONFIG;
use crate::dictionary::DICTIONARY;

#[static_init::constructor]
#[no_mangle]
Expand All @@ -41,6 +42,11 @@ extern "C" fn attach() {
info!("pe checksum: 0x{:x}", CONFIG.offset_metadata.checksum);
info!("offsets version: {}", CONFIG.offset_metadata.version);
info!("hook version: {}", CONFIG.hook_version);
info!(
"dictionary \"{}\", items {}",
CONFIG.settings.dictionary,
DICTIONARY.size()
);
unsafe {
hooks::attach_all().unwrap();
}
Expand Down

0 comments on commit d3e9e76

Please sign in to comment.