Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
godzie44 committed Oct 24, 2024
1 parent 50843ee commit 803731d
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 191 deletions.
39 changes: 15 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A simple demo of how to use the binsider as a library.
use std::{env, fs, path::PathBuf, sync::mpsc, time::Duration};
use std::{fs, path::PathBuf, sync::mpsc, time::Duration};

use binsider::{prelude::*, tui::ui::Tab};

Expand All @@ -11,7 +11,11 @@ use ratatui::{

fn main() -> Result<()> {
// Create an analyzer.
let path = PathBuf::from(env::args().next().expect("no file given"));
let mut path = PathBuf::from("ls");
if !path.exists() {
let resolved_path = which::which(path.to_string_lossy().to_string()).unwrap();
path = resolved_path;
}
let file_data = fs::read(&path)?;
let file_info = FileInfo::new(
path.to_str().unwrap_or_default(),
Expand Down
64 changes: 64 additions & 0 deletions examples/widget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::path::PathBuf;
use ratatui::{
crossterm::event::{KeyCode},
Frame,
};
use binsider::prelude::*;

fn main() -> Result<()> {
let mut path = PathBuf::from("ls");
if !path.exists() {
let resolved_path = which::which(path.to_string_lossy().to_string())?;
path = resolved_path;
}

let file_data = std::fs::read(&path)?;
let bytes = file_data.as_slice();
let file_info = FileInfo::new(path.to_str().expect("should be valid string"), Some(vec![]), bytes)?;
let analyzer =
Analyzer::new(file_info, 15, vec![])?;

// Create an application.
let mut state = State::new(analyzer)?;
let events = EventHandler::new(250);
state.analyzer.extract_strings(events.sender.clone());

let mut terminal = ratatui::init();
loop {
terminal
.draw(|frame: &mut Frame| {
render(&mut state, frame);
})
.expect("failed to draw frame");

let event = events.next()?;
match event {
Event::Key(key_event) => {
if key_event.code == KeyCode::Char('q') {
break;
}
handle_event(Event::Key(key_event), &events, &mut state)?;
}
Event::Restart(None) => {
break;
}
Event::Restart(Some(path)) => {
let file_data = std::fs::read(&path)?;
let bytes = file_data.as_slice();
let file_info = FileInfo::new(path.to_str().expect("should be valid string"), Some(vec![]), bytes)?;
let analyzer =
Analyzer::new(file_info, 15, vec![])?;
state.analyzer = analyzer;
state.handle_tab()?;
state.analyzer.extract_strings(events.sender.clone());
}
_ => {
handle_event(event, &events, &mut state)?;
}
}
}
events.stop();
ratatui::restore();

Ok(())
}
10 changes: 5 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Analyzer<'a> {
/// List of files that are being analyzed.
pub files: Vec<PathBuf>,
/// Current file information.
pub file: FileInfo<'a>,
pub file: FileInfo,
/// Elf properties.
pub elf: Elf,
/// Strings.
Expand Down Expand Up @@ -59,11 +59,11 @@ impl Debug for Analyzer<'_> {
impl<'a> Analyzer<'a> {
/// Constructs a new instance.
pub fn new(
mut file_info: FileInfo<'a>,
mut file_info: FileInfo,
strings_len: usize,
files: Vec<PathBuf>,
) -> Result<Self> {
let elf_bytes = ElfBytes::<AnyEndian>::minimal_parse(file_info.bytes)?;
let elf_bytes = ElfBytes::<AnyEndian>::minimal_parse(file_info.bytes.as_ref())?;
let elf = Elf::try_from(elf_bytes)?;
let heh = Heh::new(file_info.open_file()?, Encoding::Ascii, 0)
.map_err(|e| Error::HexdumpError(e.to_string()))?;
Expand All @@ -81,9 +81,9 @@ impl<'a> Analyzer<'a> {
}

/// Extracts the library dependencies.
pub fn extract_libs(file_info: &FileInfo<'a>) -> Result<Vec<(String, String)>> {
pub fn extract_libs(file_info: &FileInfo) -> Result<Vec<(String, String)>> {
let mut dependencies = DependencyAnalyzer::default()
.analyze(file_info.path)?
.analyze(&file_info.path)?
.libraries
.clone()
.into_iter()
Expand Down
20 changes: 10 additions & 10 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use std::os::windows::fs::MetadataExt;

/// General file information.
#[derive(Debug)]
pub struct FileInfo<'a> {
pub struct FileInfo {
/// Path of the file.
pub path: &'a str,
pub path: String,
/// Arguments of the file.
pub arguments: Option<Vec<&'a str>>,
pub arguments: Option<Vec<String>>,
/// Bytes of the file.
pub bytes: &'a [u8],
pub bytes: Box<[u8]>,
/// Whether if the file is read only.
pub is_read_only: bool,
/// Name of the file.
Expand Down Expand Up @@ -69,19 +69,19 @@ pub struct FileDateInfo {
pub birth: String,
}

impl<'a> FileInfo<'a> {
impl FileInfo {
/// Constructs a new instance.
#[cfg(not(target_os = "windows"))]
pub fn new(path: &'a str, arguments: Option<Vec<&'a str>>, bytes: &'a [u8]) -> Result<Self> {
pub fn new(path: &str, arguments: Option<Vec<String>>, bytes: impl Into<Box<[u8]>>) -> Result<Self> {
let metadata = fs::metadata(path)?;
let mode = metadata.permissions().mode();

let users = Users::new_with_refreshed_list();
let groups = Groups::new_with_refreshed_list();
Ok(Self {
path,
path: path.to_string(),
arguments,
bytes,
bytes: bytes.into(),
is_read_only: false,
name: PathBuf::from(path)
.file_name()
Expand Down Expand Up @@ -160,11 +160,11 @@ impl<'a> FileInfo<'a> {
/// Opens the file (with R/W if possible) and returns it.
pub fn open_file(&mut self) -> Result<File> {
Ok(
match OpenOptions::new().write(true).read(true).open(self.path) {
match OpenOptions::new().write(true).read(true).open(&self.path) {
Ok(v) => v,
Err(_) => {
self.is_read_only = true;
File::open(self.path)?
File::open(&self.path)?
}
},
)
Expand Down
Loading

0 comments on commit 803731d

Please sign in to comment.