From b9b5c57f9d8bd369ec71fcfd381000093cd28dee Mon Sep 17 00:00:00 2001 From: Daerckdev Date: Thu, 20 Jun 2024 14:28:49 +0200 Subject: [PATCH] fix: get_body_length needs to handle hex length too --- src/adb_tcp_connection.rs | 12 ++++++++---- src/commands/devices.rs | 2 +- src/commands/recv.rs | 4 ++-- src/commands/send.rs | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/adb_tcp_connection.rs b/src/adb_tcp_connection.rs index f58a99e..ee00f62 100644 --- a/src/adb_tcp_connection.rs +++ b/src/adb_tcp_connection.rs @@ -46,7 +46,7 @@ impl AdbTcpConnection { self.send_adb_request(adb_command)?; if with_response { - let length = self.get_body_length()?; + let length = self.get_body_length(true)?; let mut body = vec![ 0; length @@ -78,7 +78,7 @@ impl AdbTcpConnection { match AdbRequestStatus::from_str(str::from_utf8(request_status.as_ref())?)? { AdbRequestStatus::Fail => { // We can keep reading to get further details - let length = self.get_body_length()?; + let length = self.get_body_length(false)?; let mut body = vec![ 0; @@ -103,9 +103,13 @@ impl AdbTcpConnection { Ok(self.tcp_stream.write_all(command.to_string().as_bytes())?) } - pub(crate) fn get_body_length(&mut self) -> Result { + pub(crate) fn get_body_length(&mut self, hex: bool) -> Result { let mut len_buf = [0; 4]; self.tcp_stream.read_exact(&mut len_buf)?; - Ok(LittleEndian::read_u32(&len_buf)) + if hex { + Ok(u32::from_str_radix(str::from_utf8(&len_buf)?, 16)?) + } else { + Ok(LittleEndian::read_u32(&len_buf)) + } } } diff --git a/src/commands/devices.rs b/src/commands/devices.rs index c7a4a43..e899e28 100644 --- a/src/commands/devices.rs +++ b/src/commands/devices.rs @@ -41,7 +41,7 @@ impl AdbTcpConnection { self.send_adb_request(AdbCommand::TrackDevices)?; loop { - let length = self.get_body_length()?; + let length = self.get_body_length(true)?; if length > 0 { let mut body = vec![ diff --git a/src/commands/recv.rs b/src/commands/recv.rs index b06ab47..2f1140d 100644 --- a/src/commands/recv.rs +++ b/src/commands/recv.rs @@ -52,14 +52,14 @@ impl AdbTcpConnection { match &data_header { b"DATA" => { // Handle received data - let length: usize = self.get_body_length()?.try_into().unwrap(); + let length: usize = self.get_body_length(false)?.try_into().unwrap(); self.tcp_stream.read_exact(&mut buffer[..length])?; output.write_all(&buffer)?; } b"DONE" => break, // We're done here b"FAIL" => { // Handle fail - let length: usize = self.get_body_length()?.try_into().unwrap(); + let length: usize = self.get_body_length(false)?.try_into().unwrap(); self.tcp_stream.read_exact(&mut buffer[..length])?; Err(RustADBError::ADBRequestFailed(String::from_utf8( buffer[..length].to_vec(), diff --git a/src/commands/send.rs b/src/commands/send.rs index e82db16..8d616c2 100644 --- a/src/commands/send.rs +++ b/src/commands/send.rs @@ -80,7 +80,7 @@ impl AdbTcpConnection { match AdbRequestStatus::from_str(str::from_utf8(request_status.as_ref())?)? { AdbRequestStatus::Fail => { // We can keep reading to get further details - let length = self.get_body_length()?; + let length = self.get_body_length(false)?; let mut body = vec![ 0;