diff --git a/pkstrings/examples/strings.rs b/pkstrings/examples/strings.rs index dd4b5ae..8ab1154 100644 --- a/pkstrings/examples/strings.rs +++ b/pkstrings/examples/strings.rs @@ -27,28 +27,64 @@ struct Opt { files: Vec, } -fn print_buffer(offset: usize, buffer: &Vec) { - let mut res: String = String::new(); - for buf in buffer { - res.push_str(buf.as_str()); +fn print_buffer(offset: usize, buffer: &String) { + let trimmed = buffer.trim_matches('_'); + if trimmed.len() > 0 { + println!("{:#7x}: {}", offset, buffer); } - println!("{}: {}", format!("{:#7x}", offset), res); } fn read_data(data: Vec) { - let mut buffer: Vec = Vec::new(); + let mut buffer = String::new(); + let mut last_offset_start: usize = 0; + let eos: &[u8] = &[0x50, 0x57, 0x58, 0x5f]; // end of string + let mut last_pkstr: PKString = PKString::try_from(&[0x00][..]).unwrap(); for (offset, ord) in data.iter().enumerate() { // Read current chr - if let Ok(pkstr) = PKString::try_from(&[*ord][..]) { - buffer.push(String::from(&pkstr)); - } else { - if buffer.len() >= 4 { - print_buffer(offset, &buffer); - } - buffer.clear(); + match PKString::try_from(&[*ord][..]) { + Err(_) => { + if buffer.len() >= 4 { + print_buffer(last_offset_start, &buffer); + } + buffer.clear(); + last_offset_start = offset + 1; + }, + Ok(pkstr) => { + if buffer.len() == 0 { + buffer.push_str(String::from(&pkstr).as_str()); + last_pkstr = pkstr; + } else { + let ord = &pkstr.as_slice()[0]; + let last_ord: u8 = { + let tmp = Vec::::from(last_pkstr.clone()); + tmp[tmp.len() - 1] + }; + + if ! eos.contains(ord) && eos.contains(&last_ord) && buffer.len() >= 4 { + print_buffer(last_offset_start, &buffer); + buffer.clear(); + last_offset_start = offset; + } + + buffer.push_str(String::from(&pkstr).as_str()); + + if buffer.len() > 1 && eos.contains(ord) && eos.contains(&last_ord) && buffer.len() >= 4 { + print_buffer(last_offset_start, &buffer); + buffer.clear(); + last_offset_start = offset + 1; + } + + last_pkstr = pkstr; + } + }, } } + + // Print off any remaining buffer + if buffer.len() > 0 { + print_buffer(last_offset_start, &buffer); + } } fn main() {