use crate::error;
#[derive(Clone, Debug, Default)]
pub struct MemoryObject {
pub address: u64,
pub data: Vec<u8>,
}
impl MemoryObject {
pub fn from_gdb<T: AsRef<str>>(memory: T) -> error::Result<MemoryObject> {
let mut mem = MemoryObject {
address: 0,
data: Vec::new(),
};
let mut lines = memory.as_ref().lines();
if let Some(first) = lines.next() {
if let Some((address, data)) = first.split_once(':') {
if let Some(address_part) = address.split_whitespace().next() {
mem.address = u64::from_str_radix(address_part.get(2..).unwrap_or(""), 16)?;
for b in data.split_whitespace() {
mem.data
.push(u8::from_str_radix(b.get(2..).unwrap_or(""), 16)?);
}
} else {
return Err(error::Error::MemoryObjectParse(format!(
"Coudn't parse memory string: {first}"
)));
}
} else {
return Err(error::Error::MemoryObjectParse(format!(
"Coudn't parse memory string: {first}"
)));
}
for line in lines {
if let Some((_, data)) = line.split_once(':') {
for b in data.split_whitespace() {
mem.data
.push(u8::from_str_radix(b.get(2..).unwrap_or(""), 16)?);
}
} else {
return Err(error::Error::MemoryObjectParse(format!(
"No memory values: {line}"
)));
}
}
}
Ok(mem)
}
}