This commit is contained in:
Daniel Akhterov 2019-06-14 19:47:33 -07:00
parent cb53559bce
commit 17c86ed3ec
2 changed files with 41 additions and 36 deletions

View file

@ -13,9 +13,14 @@ pub fn deserialize_int_lenenc(buf: &Vec<u8>, index: &usize) -> (Option<usize>, u
} }
} }
#[inline]
pub fn deserialize_int_8(buf: &Vec<u8>, index: &usize) -> (u64, usize) {
(LittleEndian::read_u64(&buf[*index..]), index + 8)
}
#[inline] #[inline]
pub fn deserialize_int_4(buf: &Vec<u8>, index: &usize) -> (u32, usize) { pub fn deserialize_int_4(buf: &Vec<u8>, index: &usize) -> (u32, usize) {
(LittleEndian::read_u32(&buf[*index..]), index + 3) (LittleEndian::read_u32(&buf[*index..]), index + 4)
} }
#[inline] #[inline]

View file

@ -90,7 +90,7 @@ pub struct InitialHandshakePacket {
pub collation: u8, pub collation: u8,
pub status: u16, pub status: u16,
pub plugin_data_length: u8, pub plugin_data_length: u8,
pub scramble2: Option<Bytes>, pub scramble: Option<Bytes>,
pub auth_plugin_name: Option<Bytes>, pub auth_plugin_name: Option<Bytes>,
} }
@ -129,70 +129,70 @@ impl Deserialize for InitialHandshakePacket {
fn deserialize(buf: &mut Vec<u8>) -> Result<Self, Error> { fn deserialize(buf: &mut Vec<u8>) -> Result<Self, Error> {
let mut index = 0; let mut index = 0;
let length = (buf[0] + (buf[1]<<8) + (buf[2]<<16)) as u32; let (length, index) = deserialize_int_3(&buf, &index);
index += 3;
if buf.len() != length as usize { if buf.len() != length as usize {
return Err(err_msg("Lengths to do not match")); return Err(err_msg("Lengths to do not match"));
} }
let sequence_number = buf[index]; let mut sequence_number = 0;
index += 1; (sequence_number, index) = deserialize_int_1(&buf, &index);
if sequence_number != 0 { if sequence_number != 0 {
return Err(err_msg("Squence Number of Initial Handshake Packet is not 0")); return Err(err_msg("Squence Number of Initial Handshake Packet is not 0"));
} }
let protocol_version = buf[index] as u8; let (protocol_version, index) = deserialize_int_1(&buf, &index);
index += 1; let (server_version, index) = deserialize_string_null(&buf, &index);
let (connection_id, index) = deserialize_int_4(&buf, &index);
let null_index = memchr::memchr(b'\0', &buf[index..]).unwrap(); let (auth_seed, index) = deserialize_string_fix(&buf, &index, 8);
let server_version = Bytes::from(buf[index..null_index].to_vec());
index = null_index + 1;
let connection_id = LittleEndian::read_u32(&buf);
index += 4;
let auth_seed = Bytes::from(buf[index..index + 8].to_vec());
index += 8;
// Skip reserved byte // Skip reserved byte
index += 1; index += 1;
let mut capabilities = Capabilities::from_bits(LittleEndian::read_u16(&buf[index..]).into()).unwrap(); let (cap, index) = deserialize_int_2(&buf, &index);
index += 2; let mut capabilities = Capabilities::from_bits(cap.into()).unwrap();
let collation = buf[index]; let (collation, index) = deserialize_int_1(&buf, &index);
index += 1;
let status = LittleEndian::read_u16(&buf[index..]); let (status, index) = deserialize_int_2(&buf, &index);
index += 2;
capabilities |= Capabilities::from_bits(LittleEndian::read_u16(&buf[index..]).into()).unwrap(); let (cap, index) = deserialize_int_2(&buf, &index);
index += 2; capabilities |= Capabilities::from_bits(cap.into()).unwrap();
let mut plugin_data_length = 0; let mut plugin_data_length = 0;
if !(capabilities & Capabilities::PLUGIN_AUTH).is_empty() { if !(capabilities & Capabilities::PLUGIN_AUTH).is_empty() {
plugin_data_length = buf[index] as u8; let (plugin, i) = deserialize_int_1(&buf, &index);
plugin_data_length = plugin;
index = i;
} else {
// Skip reserve byte
index += 1;
} }
index += 1;
// Skip filler // Skip filler
index += 6; index += 6;
if (capabilities & Capabilities::CLIENT_MYSQL).is_empty() { if (capabilities & Capabilities::CLIENT_MYSQL).is_empty() {
capabilities |= Capabilities::from_bits(LittleEndian::read_u32(&buf[index..]).into()).unwrap(); let (cap, i) = deserialize_int_4(&buf, &index);
capabilities |= Capabilities::from_bits(cap.into()).unwrap();
index = i;
} else {
// Skip filler
index += 4;
} }
index += 4;
let mut scramble2: Option<Bytes> = None; let mut scramble: Option<Bytes> = None;
let mut auth_plugin_name: Option<Bytes> = None; let mut auth_plugin_name: Option<Bytes> = None;
if !(capabilities & Capabilities::SECURE_CONNECTION).is_empty() { if !(capabilities & Capabilities::SECURE_CONNECTION).is_empty() {
let len = std::cmp::max(12, plugin_data_length - 9); let len = std::cmp::max(12, plugin_data_length as usize - 9);
scramble2 = Some(Bytes::from(buf[index..index + len as usize].to_vec())); let (scram, i) = deserialize_string_fix(&buf, &index, len);
scramble = Some(scram);
index = i;
} else { } else {
let null_index = memchr::memchr(b'\0', &buf[index..]).unwrap(); let (plugin, i) = deserialize_string_null(&buf, &index);
auth_plugin_name = Some(Bytes::from(buf[index..null_index].to_vec())); let auth_plugin_name = Some(plugin);
index = i;
} }
Ok(InitialHandshakePacket { Ok(InitialHandshakePacket {
@ -206,7 +206,7 @@ impl Deserialize for InitialHandshakePacket {
collation, collation,
status, status,
plugin_data_length, plugin_data_length,
scramble2, scramble,
auth_plugin_name, auth_plugin_name,
}) })
} }