generated from PlexSheep/baserepo
Compare commits
3 commits
d8fd70aae4
...
3d727c74d0
Author | SHA1 | Date | |
---|---|---|---|
3d727c74d0 | |||
a150304d01 | |||
0cf8ab78dc |
5 changed files with 7 additions and 447 deletions
|
@ -70,12 +70,6 @@ name = "ccc"
|
|||
path = "src/ccc/mod.rs"
|
||||
required-features = ["bin", "math"]
|
||||
|
||||
[[bin]]
|
||||
name = "hedu"
|
||||
path = "src/hedu/mod.rs"
|
||||
required-features = ["bin", "bintols"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
libpt-core = { workspace = true }
|
||||
libpt-bintols = { workspace = true }
|
||||
|
|
|
@ -1,239 +0,0 @@
|
|||
//! # 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 crate::display::humanbytes;
|
||||
use anyhow::{bail, Result};
|
||||
use libpt_log::{debug, error, trace, warn};
|
||||
use std::io::{prelude::*, Read, SeekFrom};
|
||||
|
||||
pub const BYTES_PER_LINE: usize = 16;
|
||||
pub const LINE_SEP_HORIZ: char = '─';
|
||||
pub const LINE_SEP_VERT: char = '│';
|
||||
pub const CHAR_BORDER: &'static str = "|";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Hedu {
|
||||
pub chars: bool,
|
||||
pub skip: usize,
|
||||
pub show_identical: bool,
|
||||
pub limit: usize,
|
||||
stop: bool,
|
||||
len: usize,
|
||||
data_idx: usize,
|
||||
rd_counter: usize,
|
||||
buf: [[u8; BYTES_PER_LINE]; 2],
|
||||
alt_buf: usize,
|
||||
pub display_buf: String,
|
||||
first_iter: bool,
|
||||
}
|
||||
|
||||
impl Hedu {
|
||||
pub fn new(chars: bool, skip: usize, show_identical: bool, limit: usize) -> Self {
|
||||
Hedu {
|
||||
chars,
|
||||
skip,
|
||||
show_identical,
|
||||
limit,
|
||||
stop: false,
|
||||
len: 0,
|
||||
data_idx: 0,
|
||||
rd_counter: 0,
|
||||
buf: [[0; BYTES_PER_LINE]; 2],
|
||||
alt_buf: 0,
|
||||
display_buf: String::new(),
|
||||
first_iter: true,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn display(&mut self) {
|
||||
println!("{}", self.display_buf);
|
||||
self.display_buf = String::new();
|
||||
}
|
||||
#[inline]
|
||||
pub fn sep(&mut self) {
|
||||
if self.chars {
|
||||
self.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(80);
|
||||
} else {
|
||||
self.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(59);
|
||||
}
|
||||
self.display();
|
||||
}
|
||||
#[inline]
|
||||
pub fn newline(&mut self) {
|
||||
self.display_buf += "\n";
|
||||
self.display();
|
||||
}
|
||||
fn dump_a_line(&mut self) {
|
||||
self.display_buf += &format!("{:08X} {LINE_SEP_VERT} ", self.data_idx);
|
||||
if self.len != 0 {
|
||||
for i in 0..self.len {
|
||||
if i as usize % BYTES_PER_LINE == BYTES_PER_LINE / 2 {
|
||||
self.display_buf += " ";
|
||||
}
|
||||
self.display_buf += &format!("{:02X} ", self.buf[self.alt_buf][i]);
|
||||
}
|
||||
if self.len == BYTES_PER_LINE / 2 {
|
||||
self.display_buf += " "
|
||||
}
|
||||
for i in 0..(BYTES_PER_LINE - self.len) {
|
||||
if i as usize % BYTES_PER_LINE == BYTES_PER_LINE / 2 {
|
||||
self.display_buf += " ";
|
||||
}
|
||||
self.display_buf += " ";
|
||||
}
|
||||
} else {
|
||||
self.display_buf += &format!("{:49}", "");
|
||||
}
|
||||
if self.chars {
|
||||
self.display_buf += &format!("{LINE_SEP_VERT} ");
|
||||
if self.len != 0 {
|
||||
self.display_buf += CHAR_BORDER;
|
||||
for i in 0..self.len {
|
||||
self.display_buf +=
|
||||
&format!("{}", mask_chars(self.buf[self.alt_buf][i] as char));
|
||||
}
|
||||
self.display_buf += CHAR_BORDER;
|
||||
} else {
|
||||
self.display_buf += &format!("{:^8}", "");
|
||||
}
|
||||
}
|
||||
self.display();
|
||||
}
|
||||
|
||||
fn skip_lines(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
||||
trace!(buf = format!("{:?}", self.buf), "found a duplicating line");
|
||||
let start_line = self.data_idx;
|
||||
while self.buf[0] == self.buf[1] && self.len == BYTES_PER_LINE {
|
||||
self.rd_data(data)?;
|
||||
}
|
||||
self.display_buf += &format!(
|
||||
"******** {LINE_SEP_VERT} {:<49}",
|
||||
format!(
|
||||
"(repeats {} lines)",
|
||||
self.data_idx - start_line / (BYTES_PER_LINE) + 1
|
||||
)
|
||||
);
|
||||
if self.chars {
|
||||
self.display_buf += &format!("{LINE_SEP_VERT}");
|
||||
}
|
||||
trace!(
|
||||
buf = format!("{:X?}", self.buf),
|
||||
"dumping buf after line skip"
|
||||
);
|
||||
self.alt_buf ^= 1; // read into the other buf, so we can check for sameness
|
||||
self.display();
|
||||
Ok(())
|
||||
}
|
||||
pub fn dump(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
||||
// skip a given number of bytes
|
||||
if self.skip > 0 {
|
||||
data.skip(self.skip)?;
|
||||
self.rd_counter += self.skip;
|
||||
debug!(
|
||||
data_idx = self.data_idx,
|
||||
"Skipped {}",
|
||||
humanbytes(self.skip)
|
||||
);
|
||||
}
|
||||
|
||||
// print the head
|
||||
self.display_buf += &format!("DATA IDX {LINE_SEP_VERT} DATA AS HEX");
|
||||
if self.chars {
|
||||
self.display_buf += &format!("{:width$} {LINE_SEP_VERT} DATA AS CHAR", "", width = 37);
|
||||
}
|
||||
self.display();
|
||||
self.sep();
|
||||
|
||||
// data dump loop
|
||||
self.rd_data(data)?;
|
||||
self.data_idx = 0;
|
||||
while self.len > 0 || self.first_iter {
|
||||
self.first_iter = false;
|
||||
|
||||
self.dump_a_line();
|
||||
|
||||
// loop breaker logic
|
||||
if self.stop || self.len < BYTES_PER_LINE {
|
||||
break;
|
||||
}
|
||||
self.rd_data(data)?;
|
||||
|
||||
// after line logic
|
||||
if self.buf[0] == self.buf[1] && self.len == BYTES_PER_LINE && !self.show_identical {
|
||||
self.skip_lines(data)?;
|
||||
}
|
||||
}
|
||||
self.data_idx += BYTES_PER_LINE;
|
||||
|
||||
self.sep();
|
||||
self.display_buf += &format!(
|
||||
"{:08X} {LINE_SEP_VERT} read total:\t\t {:<8} {:<15}",
|
||||
self.rd_counter,
|
||||
humanbytes(self.rd_counter),
|
||||
format!("({} B)", self.rd_counter)
|
||||
);
|
||||
if self.chars {
|
||||
self.display_buf += &format!("{LINE_SEP_VERT}");
|
||||
}
|
||||
self.display();
|
||||
Ok(())
|
||||
}
|
||||
#[inline]
|
||||
fn adjust_counters(&mut self) {
|
||||
self.rd_counter += self.len;
|
||||
self.data_idx += self.len;
|
||||
}
|
||||
|
||||
fn rd_data(&mut self, data: &mut dyn DataSource) -> Result<()> {
|
||||
match data.read(&mut self.buf[self.alt_buf]) {
|
||||
Ok(mut len) => {
|
||||
if self.limit != 0 && self.rd_counter + (BYTES_PER_LINE - 1) >= self.limit {
|
||||
len = self.limit % BYTES_PER_LINE;
|
||||
self.stop = true;
|
||||
}
|
||||
self.len = len;
|
||||
self.adjust_counters();
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => {
|
||||
error!("error while reading data: {err}");
|
||||
bail!(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if c.is_ascii_graphic() {
|
||||
return c;
|
||||
} else if c == '\n' {
|
||||
return '↩';
|
||||
} else if c == ' ' {
|
||||
return '␣';
|
||||
} else if c == '\t' {
|
||||
return '⭾';
|
||||
} else {
|
||||
return '<27>';
|
||||
}
|
||||
}
|
|
@ -25,4 +25,3 @@ pub const YOBI: u128 = 2u128.pow(80);
|
|||
// use libpt_core;
|
||||
pub mod datalayout;
|
||||
pub mod display;
|
||||
pub mod hedu;
|
||||
|
|
|
@ -26,45 +26,25 @@ use tracing::subscriber::SetGlobalDefaultError;
|
|||
|
||||
//// ENUMS /////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// ## Errors for the [Logger](super::Logger)
|
||||
#[derive(Error)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
/// Bad IO operation
|
||||
#[error("Bad IO operation")]
|
||||
IO(std::io::Error),
|
||||
IO(#[from] std::io::Error),
|
||||
/// Various errors raised when the messenger is used in a wrong way
|
||||
#[error("Bad usage")]
|
||||
Usage(String),
|
||||
/// Could not assign logger as the global default
|
||||
#[error("Could not assign as global default")] // TODO: make this more clear
|
||||
SetGlobalDefaultFail(SetGlobalDefaultError),
|
||||
#[error("Could not assign logger as global default")]
|
||||
SetGlobalDefaultFail(#[from] SetGlobalDefaultError),
|
||||
/// any other error type, wrapped in [anyhow::Error](anyhow::Error)
|
||||
#[error(transparent)]
|
||||
Other(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
Error::IO(value)
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
impl From<SetGlobalDefaultError> for Error {
|
||||
fn from(value: SetGlobalDefaultError) -> Self {
|
||||
Error::SetGlobalDefaultFail(value)
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
impl std::fmt::Debug for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::IO(e) => write!(f, "<IO Error {e:?}>"),
|
||||
Error::Usage(e) => write!(f, "<Usage Error {e:?}>"),
|
||||
Error::SetGlobalDefaultFail(e) => write!(f, "<SetGlobalDefaultFail {e:?}>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
|
174
src/hedu/mod.rs
174
src/hedu/mod.rs
|
@ -1,174 +0,0 @@
|
|||
//! # Executable for the hedu submodule
|
||||
//!
|
||||
//! Dump data to a fancy format.
|
||||
|
||||
//// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
|
||||
// we want docs
|
||||
#![warn(missing_docs)]
|
||||
#![warn(rustdoc::missing_crate_level_docs)]
|
||||
// we want Debug everywhere.
|
||||
#![warn(missing_debug_implementations)]
|
||||
// enable clippy's extra lints, the pedantic version
|
||||
#![warn(clippy::pedantic)]
|
||||
|
||||
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
use libpt::{bintols::hedu::*, log::*};
|
||||
|
||||
use clap::Parser;
|
||||
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
||||
|
||||
use std::{fs::File, io::IsTerminal, path::PathBuf};
|
||||
|
||||
//// TYPES /////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
|
||||
/// short about section displayed in help
|
||||
const ABOUT_ROOT: &'static str = r##"
|
||||
Dumps data in fancy formats.
|
||||
"##;
|
||||
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
|
||||
static LONG_ABOUT_ROOT: &'static str = r##"
|
||||
|
||||
libpt is a personal general purpose library, offering this executable, a python module and a
|
||||
dynamic library.
|
||||
"##;
|
||||
|
||||
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// MACROS ////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// ENUMS /////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
|
||||
/// defines CLI interface
|
||||
#[derive(Debug, Clone, Parser)]
|
||||
#[command(
|
||||
author,
|
||||
version,
|
||||
about = ABOUT_ROOT,
|
||||
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
|
||||
help_template =
|
||||
r#"{about-section}
|
||||
{usage-heading} {usage}
|
||||
{all-args}{tab}
|
||||
|
||||
libpt: {version}
|
||||
Author: {author-with-newline}
|
||||
"#
|
||||
)]
|
||||
pub struct Cli {
|
||||
// clap_verbosity_flag seems to make this a global option implicitly
|
||||
/// set a verbosity, multiple allowed (f.e. -vvv)
|
||||
#[command(flatten)]
|
||||
pub verbose: Verbosity<InfoLevel>,
|
||||
|
||||
/// show additional logging meta data
|
||||
#[arg(long)]
|
||||
pub meta: bool,
|
||||
|
||||
/// show character representation
|
||||
#[arg(short, long)]
|
||||
pub chars: bool,
|
||||
|
||||
/// skip first N bytes
|
||||
#[arg(short, long, default_value_t = 0)]
|
||||
pub skip: usize,
|
||||
|
||||
/// only interpret N bytes (end after N)
|
||||
#[arg(short, long, default_value_t = 0)]
|
||||
pub limit: usize,
|
||||
|
||||
/// show identical lines
|
||||
#[arg(short = 'i', long)]
|
||||
pub show_identical: bool,
|
||||
|
||||
/// a data source, probably a file.
|
||||
///
|
||||
/// If left empty or set as "-", the program will read from stdin.
|
||||
// TODO: take many sources #60
|
||||
pub data_source: Vec<String>,
|
||||
}
|
||||
|
||||
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
|
||||
fn main() {
|
||||
let mut cli = cli_parse();
|
||||
let mut sources: Vec<Box<dyn DataSource>> = Vec::new();
|
||||
if cli.data_source.len() > 0 && cli.data_source[0] != "-" {
|
||||
for data_source in &cli.data_source {
|
||||
let data_source: PathBuf = PathBuf::from(data_source);
|
||||
if data_source.is_dir() {
|
||||
warn!("Not a file {:?}, skipping", data_source);
|
||||
// std::process::exit(1);
|
||||
continue;
|
||||
}
|
||||
trace!("Trying to open '{:?}'", data_source);
|
||||
match File::open(&data_source) {
|
||||
Ok(file) => sources.push(Box::new(file)),
|
||||
Err(err) => {
|
||||
error!("Could not open '{:?}': {err}", data_source);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
} else {
|
||||
trace!("Trying to open stdin");
|
||||
let stdin = std::io::stdin();
|
||||
if stdin.is_terminal() {
|
||||
warn!("Refusing to dump from interactive terminal");
|
||||
std::process::exit(2)
|
||||
}
|
||||
// just for the little header
|
||||
cli.data_source = Vec::new();
|
||||
cli.data_source.push(format!("stdin"));
|
||||
sources.push(Box::new(stdin));
|
||||
}
|
||||
for (i, source) in sources.iter_mut().enumerate() {
|
||||
let mut config = Hedu::new(cli.chars, cli.skip, cli.show_identical, cli.limit);
|
||||
// FIXME: find a better way to get the file name
|
||||
// Currently, skipped sources make an extra newline here.
|
||||
match config.chars {
|
||||
false => {
|
||||
println!("{:─^59}", format!(" {} ", cli.data_source[i]));
|
||||
}
|
||||
true => {
|
||||
println!("{:─^80}", format!(" {} ", cli.data_source[i]));
|
||||
}
|
||||
}
|
||||
match config.dump(&mut **source) {
|
||||
Ok(_) => (),
|
||||
Err(err) => {
|
||||
error!("Could not dump data of file: {err}");
|
||||
std::process::exit(3);
|
||||
}
|
||||
}
|
||||
if i < cli.data_source.len() - 1 {
|
||||
config.newline();
|
||||
}
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
fn cli_parse() -> Cli {
|
||||
let cli = Cli::parse();
|
||||
let ll: Level = match cli.verbose.log_level().unwrap().as_str() {
|
||||
"TRACE" => Level::TRACE,
|
||||
"DEBUG" => Level::DEBUG,
|
||||
"INFO" => Level::INFO,
|
||||
"WARN" => Level::WARN,
|
||||
"ERROR" => Level::ERROR,
|
||||
_ => {
|
||||
unreachable!();
|
||||
}
|
||||
};
|
||||
if cli.meta {
|
||||
Logger::init(None, Some(ll), false).expect("could not initialize Logger");
|
||||
} else {
|
||||
// less verbose version
|
||||
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
|
||||
}
|
||||
return cli;
|
||||
}
|
Reference in a new issue