pkstrings: strings: slightly more elaborate algo to split on End of String chars

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2021-11-19 20:21:34 +01:00
parent 891120c4e6
commit b824fe6512
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -27,28 +27,64 @@ struct Opt {
files: Vec<PathBuf>,
}
fn print_buffer(offset: usize, buffer: &Vec<String>) {
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<u8>) {
let mut buffer: Vec<String> = 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::<u8>::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() {