Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Update deps + 2021 edition #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
584 changes: 150 additions & 434 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "xxv"
version = "0.1.3"
authors = ["Chris Vest <mr.chrisvest@gmail.com>"]
edition = "2018"
edition = "2021"
description = "A hex viewer for the terminal, with a visual text user interface."
keywords = ["hex", "binary", "ncurses"]
categories = ["command-line-utilities", "development-tools"]
Expand All @@ -27,22 +27,22 @@ opt-level = 's'
panic = "abort"

[dependencies]
cursive = { version = "0.16.3", default-features = false }
unicode-width = "0.1.8"
directories = "3.0.2"
rmp-serde = "0.15.4"
serde = "1.0.125"
serde_derive = "1.0.125"
backtrace = "0.3.58"
time = "0.2.26"
bstr = { version = "0.2.16", default-features = false, features = ["std"] }
cursive = { version = "0.17", default-features = false }
unicode-width = "0.1"
directories = "4.0"
rmp-serde = "1.0"
serde = "1.0"
serde_derive = "1.0"
backtrace = "0.3"
time = "0.3"
bstr = { version = "0.2", default-features = false, features = ["std"] }

[target.'cfg(target_os = "linux")'.dependencies]
rio = "0.9.4"
#wl-clipboard-rs = "0.2.0" # Wayland clipboard integration.

[dev-dependencies]
tempfile = "3.2.0"
tempfile = "3.2"

[features]
default = ["cursive/ncurses-backend"]
Expand Down
41 changes: 24 additions & 17 deletions src/byte_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct TilingByteReader {
path: PathBuf,
length: u64,
use_large_addresses: bool,
display_name: String
display_name: String,
}

pub type Window = (u64, u64, u16, u16);
Expand All @@ -28,10 +28,10 @@ impl TilingByteReader {
path: path_buf,
length: file_len,
use_large_addresses: file_len > u64::from(std::u32::MAX),
display_name
display_name,
})
}

pub fn reopen(&mut self) -> Result<()> {
self.file = self.open_file()?;
self.length = self.file.metadata()?.len();
Expand All @@ -45,12 +45,17 @@ impl TilingByteReader {
pub fn file_name(&self) -> &str {
&self.display_name
}

pub fn get_path_clone(&self) -> PathBuf {
self.path.clone()
}

pub fn get_window(&mut self, window: Window, line_length: u64, buf: &mut Vec<u8>) -> Result<()> {
pub fn get_window(
&mut self,
window: Window,
line_length: u64,
buf: &mut Vec<u8>,
) -> Result<()> {
// The binary file is viewed in terms of lines.
// The lines turn the linear byte sequence into a 2D byte grid.
// Each line may be significantly longer than what can fit in the window.
Expand All @@ -71,11 +76,11 @@ impl TilingByteReader {
}
Ok(())
}

pub fn get_length(&self) -> u64 {
self.length
}

pub fn use_large_addresses(&self) -> bool {
self.use_large_addresses
}
Expand All @@ -96,40 +101,42 @@ mod tests {

let mut reader = TilingByteReader::new(tmpf.path()).unwrap();
let mut buf = Vec::new();
reader.get_window((0,0,16,16).into(), 16, &mut buf).unwrap();
reader
.get_window((0, 0, 16, 16).into(), 16, &mut buf)
.unwrap();
assert_eq!(buf, b"01234567")
}

#[test]
fn getting_multi_line_string_top_left() {
let mut tmpf = tempfile::NamedTempFile::new().unwrap();
tmpf.write(b"0123456789abcdef").unwrap();

let mut reader = TilingByteReader::new(tmpf.path()).unwrap();
let mut buf = Vec::new();
reader.get_window((0,0,4,2), 8, &mut buf).unwrap();
reader.get_window((0, 0, 4, 2), 8, &mut buf).unwrap();
assert_eq!(buf, b"012389ab")
}

#[test]
fn getting_multi_line_string_top_right() {
let mut tmpf = tempfile::NamedTempFile::new().unwrap();
tmpf.write(b"0123456789abcdef").unwrap();

let mut reader = TilingByteReader::new(tmpf.path()).unwrap();
let mut buf = Vec::new();
reader.get_window((4,0,4,2), 8, &mut buf).unwrap();
reader.get_window((4, 0, 4, 2), 8, &mut buf).unwrap();
assert_eq!(buf, b"4567cdef")
}

#[test]
fn getting_multi_line_string_bottom_left() {
let mut tmpf = tempfile::NamedTempFile::new().unwrap();
tmpf.write(b"0123456789abcdef").unwrap();

let mut reader = TilingByteReader::new(tmpf.path()).unwrap();
let mut buf = Vec::new();
reader.get_window((0,1,4,2), 8, &mut buf).unwrap();
reader.get_window((0, 1, 4, 2), 8, &mut buf).unwrap();
assert_eq!(buf, b"89ab")
}
}
37 changes: 23 additions & 14 deletions src/file_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,43 @@ const BUFFER_SIZE: usize = 1024 * 1024;

#[cfg(target_os = "linux")]
pub fn search<F>(mut file: File, bytes: &[u8], mut consumer: F)
where F: FnMut(u64) {
if async_io_search(&mut file, &bytes, &mut consumer).is_err() {
sync_io_search(&mut file, &bytes, &mut consumer);
where
F: FnMut(u64),
{
if async_io_search(&mut file, bytes, &mut consumer).is_err() {
sync_io_search(&mut file, bytes, &mut consumer);
}
}

#[cfg(not(target_os = "linux"))]
pub fn search<F>(mut file: File, bytes: &[u8], mut consumer: F)
where F: FnMut(u64) {
where
F: FnMut(u64),
{
sync_io_search(&mut file, &bytes, &mut consumer);
}

#[cfg(target_os = "linux")]
fn async_io_search<F>(file: &mut File, bytes: &[u8], consumer: &mut F) -> Result<()>
where F: FnMut(u64) {
where
F: FnMut(u64),
{
use std::collections::VecDeque;

let file_len = file.metadata()?.len();
if file_len <= u64::try_from(BUFFER_SIZE).unwrap() {
sync_io_search(file, bytes, consumer);
return Ok(());
}

let finder = Finder::new(bytes);
let needle_size = bytes.len();
let queue_depth = 32;
let mut read_pos = 0;
let config = rio::Config::default();
let io = config.start()?;
let buffers = vec![vec![0; BUFFER_SIZE]; queue_depth];

let mut queue = VecDeque::with_capacity(queue_depth);
for buf in &buffers {
let cqe = io.read_at(file, buf, read_pos);
Expand All @@ -59,18 +65,20 @@ fn async_io_search<F>(file: &mut File, bytes: &[u8], consumer: &mut F) -> Result
read_pos += u64::try_from(BUFFER_SIZE - needle_size + 1).unwrap();
}
}

Ok(())
}

fn sync_io_search<F>(file: &mut File, bytes: &[u8], consumer: &mut F)
where F: FnMut(u64) {
where
F: FnMut(u64),
{
let finder = Finder::new(bytes);
let needle_size = bytes.len();
let mut buf = vec![0; BUFFER_SIZE];
let mut num_bytes = file.read(&mut buf).unwrap();
let mut pos = 0;

while num_bytes > needle_size {
let mut offset = 0;
while let Some(p) = finder.find(&buf[offset..num_bytes]) {
Expand Down Expand Up @@ -99,7 +107,7 @@ mod tests {
search(file, b"aba", |hit| output.push(hit));
assert_eq!(output, vec![0, 2]);
}

#[test]
fn sync_io_search_in_big_file() {
let mut file = tempfile::tempfile().unwrap();
Expand All @@ -110,7 +118,7 @@ mod tests {

assert_eq!(counter, 3);
}

#[cfg(target_os = "linux")]
#[test]
fn async_io_search_in_big_file() {
Expand All @@ -128,7 +136,8 @@ mod tests {
fn prepare_big_file(file: &mut File) {
let file_len = u64::try_from(BUFFER_SIZE * 2 + (BUFFER_SIZE >> 1)).unwrap();
file.set_len(file_len).unwrap();
file.seek(SeekFrom::Start(u64::try_from(BUFFER_SIZE - 3).unwrap())).unwrap();
file.seek(SeekFrom::Start(u64::try_from(BUFFER_SIZE - 3).unwrap()))
.unwrap();
file.write(b"Pokemon PokPokemon").unwrap();
file.seek(SeekFrom::Start(file_len - 7)).unwrap();
file.write(b"Pokemon").unwrap();
Expand Down
11 changes: 8 additions & 3 deletions src/goto_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use cursive::event::Key;
use cursive::traits::{Resizable, Nameable};
use cursive::traits::{Nameable, Resizable};
use cursive::views::{Dialog, DummyView, EditView, LinearLayout, OnEventView, TextView};
use cursive::Cursive;

use crate::hex_view::HexView;
use crate::utilities::{get_content, parse_number_or_zero};
use crate::xxv_tui::{OBJ_HEX_VIEW, OBJ_GOTO_OFFSET, OBJ_GOTO_MUL1, OBJ_GOTO_MUL2};
use crate::xxv_tui::{OBJ_GOTO_MUL1, OBJ_GOTO_MUL2, OBJ_GOTO_OFFSET, OBJ_HEX_VIEW};

pub fn open_goto_dialog(s: &mut Cursive) {
let (line_width, length) = s
Expand All @@ -23,7 +23,12 @@ pub fn open_goto_dialog(s: &mut Cursive) {
.min_width(18),
)
.child(TextView::new(" + "))
.child(EditView::new().content("0").with_name(OBJ_GOTO_MUL1).min_width(18))
.child(
EditView::new()
.content("0")
.with_name(OBJ_GOTO_MUL1)
.min_width(18),
)
.child(TextView::new(" * "))
.child(
EditView::new()
Expand Down
14 changes: 6 additions & 8 deletions src/help_text.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use cursive::Cursive;
use cursive::views::{TextView, Dialog, ScrollView, OnEventView};
use cursive::event::Key;
use cursive::views::{Dialog, OnEventView, ScrollView, TextView};
use cursive::Cursive;

const HELP_TEXT: &str = include_str!("help_text.md");

pub fn show_help(s: &mut Cursive) {
let text_view = TextView::new(HELP_TEXT);
let dialog = Dialog::around(ScrollView::new(text_view))
.dismiss_button("Ok");
let esc_view = OnEventView::new(dialog)
.on_event(Key::Esc, |s| {
s.pop_layer();
});
let dialog = Dialog::around(ScrollView::new(text_view)).dismiss_button("Ok");
let esc_view = OnEventView::new(dialog).on_event(Key::Esc, |s| {
s.pop_layer();
});
s.add_layer(esc_view);
}
Loading