diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..c229d79 --- /dev/null +++ b/src/common.rs @@ -0,0 +1,19 @@ +use std::ffi::CString; + +pub(crate) fn read_bool_from + std::cmp::PartialEq>(x: T) -> bool { + x == T::from(1u8) +} + +pub(crate) fn write_bool_as>(x: &bool) -> T { + if *x { T::from(1u8) } else { T::from(0u8) } +} + +pub(crate) fn read_string(byte_stream: Vec) -> String { + let str = String::from_utf8(byte_stream).unwrap(); + str.trim_matches(char::from(0)).to_string() // trim \0 from the end of strings +} + +pub(crate) fn write_string(str: &String) -> Vec { + let c_string = CString::new(&**str).unwrap(); + c_string.as_bytes_with_nul().to_vec() +} diff --git a/src/encryption.rs b/src/encryption.rs index 943aa22..61419e2 100644 --- a/src/encryption.rs +++ b/src/encryption.rs @@ -2,7 +2,21 @@ use std::{io::Cursor, slice}; use binrw::{BinRead, BinResult, BinWrite}; -use crate::packet::{blowfish_decode, blowfish_encode}; +#[link(name = "FFXIVBlowfish")] +unsafe extern "C" { + pub fn blowfish_encode( + key: *const u8, + keybytes: u32, + pInput: *const u8, + lSize: u32, + ) -> *const u8; + pub fn blowfish_decode( + key: *const u8, + keybytes: u32, + pInput: *const u8, + lSize: u32, + ) -> *const u8; +} const GAME_VERSION: u16 = 7000; diff --git a/src/lib.rs b/src/lib.rs index 92bdfd2..4210530 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod config; pub mod encryption; pub mod packet; pub mod patchlist; +mod common; pub fn generate_sid() -> String { let random_id: String = rand::thread_rng() diff --git a/src/packet.rs b/src/packet.rs index b76f0fb..c1c20cc 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,5 +1,4 @@ use std::{ - ffi::CString, fs::write, io::Cursor, time::{SystemTime, UNIX_EPOCH}, @@ -11,41 +10,7 @@ use tokio::{ net::TcpStream, }; -use crate::encryption::{decrypt, encrypt, generate_encryption_key}; - -pub(crate) fn read_bool_from + std::cmp::PartialEq>(x: T) -> bool { - x == T::from(1u8) -} - -pub(crate) fn write_bool_as>(x: &bool) -> T { - if *x { T::from(1u8) } else { T::from(0u8) } -} - -pub(crate) fn read_string(byte_stream: Vec) -> String { - let str = String::from_utf8(byte_stream).unwrap(); - str.trim_matches(char::from(0)).to_string() // trim \0 from the end of strings -} - -pub(crate) fn write_string(str: &String) -> Vec { - let c_string = CString::new(&**str).unwrap(); - c_string.as_bytes_with_nul().to_vec() -} - -#[link(name = "FFXIVBlowfish")] -unsafe extern "C" { - pub fn blowfish_encode( - key: *const u8, - keybytes: u32, - pInput: *const u8, - lSize: u32, - ) -> *const u8; - pub fn blowfish_decode( - key: *const u8, - keybytes: u32, - pInput: *const u8, - lSize: u32, - ) -> *const u8; -} +use crate::{common::{read_bool_from, read_string, write_bool_as, write_string}, encryption::{blowfish_encode, decrypt, encrypt, generate_encryption_key}}; #[binrw] #[brw(repr = u16)]