generated from PlexSheep/rs-base
Compare commits
No commits in common. "c9782ef6dcc68af27e40efa4b3676bd1547ad9b1" and "f5ec931b33afe2b1407322970267868dfb74dba9" have entirely different histories.
c9782ef6dc
...
f5ec931b33
2 changed files with 31 additions and 41 deletions
|
@ -1,8 +1,13 @@
|
||||||
//! # Underlying mechanisms of how hedu works with data
|
//! # Dump data
|
||||||
|
//!
|
||||||
|
//! This crate is part of [`pt`](../libpt/index.html), but can also be used as a standalone
|
||||||
|
//! module.
|
||||||
|
//!
|
||||||
|
//! Hedu is made for hexdumping data. `libpt` offers a cli application using this module.
|
||||||
|
|
||||||
use std::io::{prelude::*, Read, SeekFrom};
|
use std::io::{prelude::*, Read, SeekFrom};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{bail, Result};
|
||||||
use libpt::bintols::display::humanbytes;
|
use libpt::bintols::display::humanbytes;
|
||||||
use libpt::log::{debug, error, trace, warn};
|
use libpt::log::{debug, error, trace, warn};
|
||||||
|
|
||||||
|
@ -11,23 +16,6 @@ pub const LINE_SEP_HORIZ: char = '─';
|
||||||
pub const LINE_SEP_VERT: char = '│';
|
pub const LINE_SEP_VERT: char = '│';
|
||||||
pub const CHAR_BORDER: &'static str = "|";
|
pub const CHAR_BORDER: &'static str = "|";
|
||||||
|
|
||||||
pub trait DataSource: Read {
|
|
||||||
fn skip(&mut self, length: usize) -> std::io::Result<()>;
|
|
||||||
}
|
|
||||||
impl DataSource for std::io::Stdin {
|
|
||||||
fn skip(&mut self, _length: usize) -> std::io::Result<()> {
|
|
||||||
warn!("can't skip bytes for the stdin!");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl DataSource for std::fs::File {
|
|
||||||
fn skip(&mut self, length: usize) -> std::io::Result<()> {
|
|
||||||
self.seek(SeekFrom::Current(length as i64))?;
|
|
||||||
// returns the new position from the start, we don't need it here.
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Hedu {
|
pub struct Hedu {
|
||||||
pub chars: bool,
|
pub chars: bool,
|
||||||
|
@ -45,7 +33,6 @@ pub struct Hedu {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hedu {
|
impl Hedu {
|
||||||
/// create a new hedu struct, initializing all kinds of configs and values needed while running
|
|
||||||
pub fn new(chars: bool, skip: usize, show_identical: bool, limit: usize) -> Self {
|
pub fn new(chars: bool, skip: usize, show_identical: bool, limit: usize) -> Self {
|
||||||
Hedu {
|
Hedu {
|
||||||
chars,
|
chars,
|
||||||
|
@ -62,15 +49,11 @@ impl Hedu {
|
||||||
first_iter: true,
|
first_iter: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// print the display buffer to the stdout
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn display(&mut self) {
|
pub fn display(&mut self) {
|
||||||
println!("{}", self.display_buf);
|
println!("{}", self.display_buf);
|
||||||
self.display_buf = String::new();
|
self.display_buf = String::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// display a separator line
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn sep(&mut self) {
|
pub fn sep(&mut self) {
|
||||||
if self.chars {
|
if self.chars {
|
||||||
|
@ -80,15 +63,11 @@ impl Hedu {
|
||||||
}
|
}
|
||||||
self.display();
|
self.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// display a newline
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn newline(&mut self) {
|
pub fn newline(&mut self) {
|
||||||
self.display_buf += "\n";
|
self.display_buf += "\n";
|
||||||
self.display();
|
self.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// display the dump of a single line of data
|
|
||||||
fn dump_a_line(&mut self) {
|
fn dump_a_line(&mut self) {
|
||||||
self.display_buf += &format!("{:08X} {LINE_SEP_VERT} ", self.data_idx);
|
self.display_buf += &format!("{:08X} {LINE_SEP_VERT} ", self.data_idx);
|
||||||
if self.len != 0 {
|
if self.len != 0 {
|
||||||
|
@ -126,7 +105,6 @@ impl Hedu {
|
||||||
self.display();
|
self.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// skip duplicate lines and display skip info
|
|
||||||
fn skip_lines(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
fn skip_lines(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
||||||
trace!(buf = format!("{:?}", self.buf), "found a duplicating line");
|
trace!(buf = format!("{:?}", self.buf), "found a duplicating line");
|
||||||
let start_line = self.data_idx;
|
let start_line = self.data_idx;
|
||||||
|
@ -137,7 +115,7 @@ impl Hedu {
|
||||||
"******** {LINE_SEP_VERT} {:<49}",
|
"******** {LINE_SEP_VERT} {:<49}",
|
||||||
format!(
|
format!(
|
||||||
"(repeats {} lines)",
|
"(repeats {} lines)",
|
||||||
(self.data_idx - start_line / (BYTES_PER_LINE) + 1) / BYTES_PER_LINE
|
self.data_idx - start_line / (BYTES_PER_LINE) + 1
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
if self.chars {
|
if self.chars {
|
||||||
|
@ -147,11 +125,10 @@ impl Hedu {
|
||||||
buf = format!("{:X?}", self.buf),
|
buf = format!("{:X?}", self.buf),
|
||||||
"dumping buf after line skip"
|
"dumping buf after line skip"
|
||||||
);
|
);
|
||||||
|
self.alt_buf ^= 1; // read into the other buf, so we can check for sameness
|
||||||
self.display();
|
self.display();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// dump a data sources contents according to the set configs
|
|
||||||
pub fn dump(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
pub fn dump(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
||||||
// skip a given number of bytes
|
// skip a given number of bytes
|
||||||
if self.skip > 0 {
|
if self.skip > 0 {
|
||||||
|
@ -206,15 +183,12 @@ impl Hedu {
|
||||||
self.display();
|
self.display();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// adjust the counters of the hedu struct, adding self.len
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn adjust_counters(&mut self) {
|
fn adjust_counters(&mut self) {
|
||||||
self.rd_counter += self.len;
|
self.rd_counter += self.len;
|
||||||
self.data_idx += self.len;
|
self.data_idx += self.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// read data from the data source
|
|
||||||
fn rd_data(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
fn rd_data(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
||||||
match data.read(&mut self.buf[self.alt_buf]) {
|
match data.read(&mut self.buf[self.alt_buf]) {
|
||||||
Ok(mut len) => {
|
Ok(mut len) => {
|
||||||
|
@ -224,17 +198,33 @@ impl Hedu {
|
||||||
}
|
}
|
||||||
self.len = len;
|
self.len = len;
|
||||||
self.adjust_counters();
|
self.adjust_counters();
|
||||||
Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("error while reading data: {err}");
|
error!("error while reading data: {err}");
|
||||||
Err(err.into())
|
bail!(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// interpret characters for the --chars option
|
pub trait DataSource: Read {
|
||||||
|
fn skip(&mut self, length: usize) -> std::io::Result<()>;
|
||||||
|
}
|
||||||
|
impl DataSource for std::io::Stdin {
|
||||||
|
fn skip(&mut self, _length: usize) -> std::io::Result<()> {
|
||||||
|
warn!("can't skip bytes for the stdin!");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl DataSource for std::fs::File {
|
||||||
|
fn skip(&mut self, length: usize) -> std::io::Result<()> {
|
||||||
|
self.seek(SeekFrom::Current(length as i64))?;
|
||||||
|
// returns the new position from the start, we don't need it here.
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn mask_chars(c: char) -> char {
|
fn mask_chars(c: char) -> char {
|
||||||
if c.is_ascii_graphic() {
|
if c.is_ascii_graphic() {
|
||||||
return c;
|
return c;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! # A simple hexdumper with a somewhat fancy format
|
//! # Executable for the hedu submodule
|
||||||
//!
|
//!
|
||||||
//! Dump data from any readable source, such as the stdin or a file
|
//! Dump data to a fancy format.
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
use std::{fs::File, io::IsTerminal, path::PathBuf};
|
use std::{fs::File, io::IsTerminal, path::PathBuf};
|
||||||
|
@ -23,7 +23,7 @@ use dumper::*;
|
||||||
{usage-heading} {usage}
|
{usage-heading} {usage}
|
||||||
{all-args}{tab}
|
{all-args}{tab}
|
||||||
|
|
||||||
{name}: {version}
|
autocrate: {version}
|
||||||
Author: {author-with-newline}
|
Author: {author-with-newline}
|
||||||
"#
|
"#
|
||||||
)]
|
)]
|
||||||
|
|
Reference in a new issue