1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-04-22 15:27:44 +00:00

Move more code out of packet module

This commit is contained in:
Joshua Goins 2025-03-08 21:56:44 -05:00
parent dd83b335dd
commit 6aed610276
4 changed files with 36 additions and 37 deletions

19
src/common.rs Normal file
View file

@ -0,0 +1,19 @@
use std::ffi::CString;
pub(crate) fn read_bool_from<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
x == T::from(1u8)
}
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(x: &bool) -> T {
if *x { T::from(1u8) } else { T::from(0u8) }
}
pub(crate) fn read_string(byte_stream: Vec<u8>) -> 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<u8> {
let c_string = CString::new(&**str).unwrap();
c_string.as_bytes_with_nul().to_vec()
}

View file

@ -2,7 +2,21 @@ use std::{io::Cursor, slice};
use binrw::{BinRead, BinResult, BinWrite}; 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; const GAME_VERSION: u16 = 7000;

View file

@ -6,6 +6,7 @@ pub mod config;
pub mod encryption; pub mod encryption;
pub mod packet; pub mod packet;
pub mod patchlist; pub mod patchlist;
mod common;
pub fn generate_sid() -> String { pub fn generate_sid() -> String {
let random_id: String = rand::thread_rng() let random_id: String = rand::thread_rng()

View file

@ -1,5 +1,4 @@
use std::{ use std::{
ffi::CString,
fs::write, fs::write,
io::Cursor, io::Cursor,
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
@ -11,41 +10,7 @@ use tokio::{
net::TcpStream, net::TcpStream,
}; };
use crate::encryption::{decrypt, encrypt, generate_encryption_key}; use crate::{common::{read_bool_from, read_string, write_bool_as, write_string}, encryption::{blowfish_encode, decrypt, encrypt, generate_encryption_key}};
pub(crate) fn read_bool_from<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
x == T::from(1u8)
}
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(x: &bool) -> T {
if *x { T::from(1u8) } else { T::from(0u8) }
}
pub(crate) fn read_string(byte_stream: Vec<u8>) -> 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<u8> {
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;
}
#[binrw] #[binrw]
#[brw(repr = u16)] #[brw(repr = u16)]