// Copyright (C) 2020 "Maxime “pep” Buquet " // // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU Affero General Public License as published by the // Free Software Foundation, either version 3 of the License, or (at your // option) any later version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License // for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . use std::convert::TryFrom; use std::fs; use std::path::PathBuf; use pkstrings::PKString; use structopt::StructOpt; #[derive(Debug, StructOpt)] #[structopt(name = "strings", about = "Display printable strings in [file(s)]")] struct Opt { #[structopt(parse(from_os_str), long, short)] 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()); } println!("{}: {}", format!("{:#7x}", offset), res); } fn read_data(data: Vec) { let mut buffer: Vec = Vec::new(); for (offset, ord) in data.iter().enumerate() { // Read current chr if let Ok(pkstr) = PKString::try_from(*ord) { buffer.push(pkstr.into_string()); } else { if buffer.len() >= 4 { print_buffer(offset, &buffer); } buffer.clear(); } } } fn main() { let opt = Opt::from_args(); for file in opt.files { let data: Vec = fs::read(file).unwrap(); read_data(data) } }