From 28619a95c38537c586a45cf26b81d6bc0f294bd6 Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 4 Nov 2019 20:33:10 -0800 Subject: [PATCH] move util to libpso --- src/common/util.rs | 63 ---------------------------------------------- src/login/login.rs | 2 +- 2 files changed, 1 insertion(+), 64 deletions(-) delete mode 100644 src/common/util.rs diff --git a/src/common/util.rs b/src/common/util.rs deleted file mode 100644 index 0c85032..0000000 --- a/src/common/util.rs +++ /dev/null @@ -1,63 +0,0 @@ - -pub fn array_to_utf8(array: [u8; X]) -> Result { - String::from_utf8(array.to_vec()) - .map(|mut s| { - if let Some(index) = s.find("\u{0}") { - s.truncate(index); - } - s - }) -} - -#[macro_export] -macro_rules! utf8_to_array { - ($s: expr, $size: expr) => { - { - let mut array = [0u8; $size]; - let bytes = $s.as_bytes(); - array[..bytes.len()].clone_from_slice(&bytes); - array - } - } -} - -#[macro_export] -macro_rules! utf8_to_utf16_array { - ($s: expr, $size: expr) => { - { - let mut array = [0u16; $size]; - //let bytes = $s.as_bytes(); - let bytes = $s.encode_utf16().collect::>(); - array[..bytes.len()].clone_from_slice(&bytes); - array - } - } -} - - - - -#[cfg(test)] -mod test { - #[test] - fn test_utf8_to_array() { - let s = "asdf".to_owned(); - let a = utf8_to_array!(s, 8); - - let mut e = [0u8; 8]; - e[..4].clone_from_slice(b"asdf"); - assert!(a == e); - } - - #[test] - fn utf8_to_utf16_array() { - let utf16 = utf8_to_utf16_array!("asdf", 16); - assert!(utf16 == [97, 115, 100, 102, 0,0,0,0,0,0,0,0,0,0,0,0]) - } - - #[test] - fn utf8_to_utf16_array_unicode() { - let utf16 = utf8_to_utf16_array!("あいうえお", 16); - assert!(utf16 == [0x3042 , 0x3044, 0x3046, 0x3048, 0x304A, 0,0,0,0,0,0,0,0,0,0,0]) - } -} diff --git a/src/login/login.rs b/src/login/login.rs index b47afc8..a2c1c85 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -8,10 +8,10 @@ use bcrypt; use libpso::packet::login::*; use libpso::{PacketParseError, PSOPacket}; use libpso::crypto::bb::PSOBBCipher; +use libpso::util::array_to_utf8; use crate::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY}; use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ServerState, OnConnect, ClientId}; -use crate::common::util::array_to_utf8; use crate::entity::gateway::EntityGateway; use crate::entity::account::UserAccount;