Skip to content

Commit

Permalink
#54 Ported new string algorithm to virt_mem
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1N committed May 23, 2021
1 parent 4f33e8d commit 63cb831
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions memflow/src/mem/virt_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,31 +218,50 @@ where
Ok(String::from_utf8_lossy(&buf).to_string())
}

/// Reads a variable length string with a length of up to 4kb from the target.
/// Reads a variable length string with a length of up to specified amount from the target.
///
/// # Arguments
///
/// * `addr` - target address to read from
/// * `n` - maximum number of bytes to read
///
/// # Remarks:
///
/// The string must be null-terminated.
/// If no null terminator is found the this function will return an error.
///
/// For reading fixed-size char arrays the [`virt_read_char_array`] should be used.
fn virt_read_char_string(&mut self, addr: Address) -> PartialResult<String> {
fn virt_read_char_string_n(&mut self, addr: Address, n: usize) -> Result<String> {
let mut buf = vec![0; 32];

let mut last_n = 0;

loop {
self.virt_read_raw_into(addr, &mut buf).data_part()?;
let (_, right) = buf.split_at_mut(last_n);

self.virt_read_raw_into(addr, right)?;
if let Some((n, _)) = buf.iter().enumerate().find(|(_, c)| **c == 0_u8) {
buf.truncate(n);
buf.truncate(last_n + n);
return Ok(String::from_utf8_lossy(&buf).to_string());
}
if buf.len() * 2 > 4096 {
if buf.len() >= n {
break;
}
buf.extend(vec![0; buf.len()]);
last_n = buf.len();

buf.extend((0..buf.len()).map(|_| 0));
}
Err(PartialError::Error(Error(
ErrorOrigin::VirtualMemory,
ErrorKind::OutOfBounds,
)))

Err(Error(ErrorOrigin::VirtualMemory, ErrorKind::OutOfBounds))
}

/// Reads a variable length string with up to 4kb length from the target.
///
/// # Arguments
///
/// * `addr` - target address to read from
fn virt_read_char_string(&mut self, addr: Address) -> Result<String> {
self.virt_read_char_string_n(addr, 4096)
}

fn virt_batcher(&mut self) -> VirtualMemoryBatcher<Self>
Expand Down

0 comments on commit 63cb831

Please sign in to comment.