From d136b7c5e5efcd9cbd51164cd14d586e758b9c3c Mon Sep 17 00:00:00 2001 From: Jake Probst Date: Tue, 20 Aug 2019 17:57:20 -0700 Subject: [PATCH] different servers have different buffer chunk sizes --- src/common/network.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/common/network.rs b/src/common/network.rs index ba9ce1f..0aa1a29 100644 --- a/src/common/network.rs +++ b/src/common/network.rs @@ -43,11 +43,17 @@ pub fn recv_packet(socket: &mut T, cipher: &mut dyn PSOCipher) -> Resul } let mut dec_size_buf = cipher.decrypt(&size_buf)?; - let size = u16::from_le_bytes([dec_size_buf[0], dec_size_buf[1]]) as usize; + let pkt_size = u16::from_le_bytes([dec_size_buf[0], dec_size_buf[1]]) as usize; - let mut data_buf = vec![0u8; size - cipher.header_size()]; + let mut buf_size = pkt_size; + while buf_size % cipher.block_size() != 0 { + buf_size += 1; + } + buf_size -= cipher.header_size(); + + let mut data_buf = vec![0u8; buf_size]; let mut offset = 0; - while offset < size - cipher.header_size() { + while offset < buf_size { let diff = socket.read(&mut data_buf[offset..])?; if diff == 0 { return Err(PacketNetworkError::ClientDisconnected); @@ -55,21 +61,14 @@ pub fn recv_packet(socket: &mut T, cipher: &mut dyn PSOCipher) -> Resul offset += diff; } - let mut null_data_count = 0; - while data_buf.len() % cipher.header_size() != 0 { - data_buf.push(0); - null_data_count += 1; - } - + //println!("[encbuf]: {:X?}", data_buf); let mut dec_data_buf = cipher.decrypt(&data_buf.to_vec())?; - for _ in 0..null_data_count { - dec_data_buf.pop(); - } - let mut full_buf = Vec::new(); full_buf.append(&mut dec_size_buf); full_buf.append(&mut dec_data_buf); + full_buf = full_buf[..pkt_size].to_vec(); + //println!("[recv]: {:X?}", full_buf); Ok(full_buf) }