Browse Source

check that the entire packet was read

pull/3/head
Jake Probst 5 years ago
parent
commit
8caa48a1e6
  1. 17
      psopacket/src/lib.rs

17
psopacket/src/lib.rs

@ -206,18 +206,29 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
impl PSOPacket for #this_struct { impl PSOPacket for #this_struct {
fn from_bytes(data: &Vec<u8>) -> Result<#this_struct, PacketParseError> { fn from_bytes(data: &Vec<u8>) -> Result<#this_struct, PacketParseError> {
let mut cur = std::io::Cursor::new(data); let mut cur = std::io::Cursor::new(data);
cur.seek(SeekFrom::Start(2)).unwrap();
let mut b: [u8; 2] = [0; 2]; let mut b: [u8; 2] = [0; 2];
cur.read(&mut b).unwrap(); cur.read(&mut b).unwrap();
let len = u16::from_le_bytes(b);
cur.read(&mut b).unwrap();
let cmd = u16::from_le_bytes(b); let cmd = u16::from_le_bytes(b);
if cmd != #pkt_cmd { if cmd != #pkt_cmd {
return Err(PacketParseError::WrongPacketCommand); return Err(PacketParseError::WrongPacketCommand);
} }
if len as usize != data.len() {
return Err(PacketParseError::WrongPacketSize(len, data.len()));
}
Ok(#this_struct {
let result = Ok(#this_struct {
#(#from_bytes)* #(#from_bytes)*
})
});
if cur.position() as usize != data.len() {
return Err(PacketParseError::DataStructNotLargeEnough(cur.position(), data.len()));
}
result
} }
fn as_bytes(&self) -> Vec<u8> { fn as_bytes(&self) -> Vec<u8> {
let mut buf: Vec<u8> = Vec::new(); let mut buf: Vec<u8> = Vec::new();

Loading…
Cancel
Save