17 Commits
Author SHA1 Message Date
David Peter e10136d203 Updates for 0.13 2023-05-10 22:19:07 +02:00
David Peter 5f0373184f Add --characters, change meaning of -C
This changes the meaning of `-C` to "show the character panel". The long
version `--no-characters` can still be used to hide the character panel.

closes #187
2023-05-10 22:09:19 +02:00
David Peter 57d18482ba Add Copy and Clone for Endianness 2023-04-26 14:53:37 +02:00
David Peter 816779e968 Merge pull request #189 from RinHizakura/master
Support both little and big endian dump
2023-04-25 09:20:23 +02:00
David Peter 9be018fbb9 Add -e alias for --endianness=little 2023-04-25 09:12:26 +02:00
David Peter cba717b783 Minor doc comment adjustment 2023-04-25 08:51:31 +02:00
David Peter 580f865c4a Rename function 2023-04-25 08:50:55 +02:00
David Peter 20450310c9 Help text adjustments, do not require group-size to be present 2023-04-25 08:48:39 +02:00
RinHizakura b251575d94 Change design for endianness command line option 2023-04-24 23:44:49 +08:00
RinHizakura 58b038af27 Change le to non-abbreviated name little endian 2023-04-24 23:13:28 +08:00
RinHizakura d703cdfd78 Fix comment error and grammarly 2023-04-24 23:10:25 +08:00
RinHizakura 43f8b92502 Fix test for endianness 2023-04-24 22:59:23 +08:00
RinHizakura a966896d3f Fix integration test module name 2023-04-24 22:59:23 +08:00
RinHizakura 08b903270f Change endianness from boolean type to enum 2023-04-24 22:59:19 +08:00
David Peter dd601e4792 Run cargo fmt 2023-04-21 21:28:52 +02:00
RinHizakura 564cf64b1b Support both little and big endian dump 2023-04-21 21:26:43 +02:00
David Peter c24280620a Merge pull request #191 from sharkdp/ci-sync
CI update
2023-04-21 21:25:27 +02:00
6 changed files with 158 additions and 12 deletions

No files matched your search

+15
View File
@@ -1,3 +1,18 @@
# v0.13.0
## Features
- Support both little and big Endian dumps using `--endianness={little,big}`, see #189 and #104 (@RinHizakura)
## Changes
- **Breaking**: Changed the meaning of the short flag `-C` to be consistent with `hexdump -C`. Previously, this would *hide* the character panel, but now `-C` *shows* the character panel, in case it has been previously (e.g. in an `alias`) disabled with `--no-characters`, see #187 (@sharkdp)
## `hexyl` as a library
- New `endianness` method for `PrinterBuilder`
# v0.12.0
## Features
Generated
+1 -1
View File
@@ -200,7 +200,7 @@ dependencies = [
[[package]]
name = "hexyl"
version = "0.12.0"
version = "0.13.0"
dependencies = [
"anyhow",
"assert_cmd",
+1 -1
View File
@@ -7,7 +7,7 @@ license = "MIT/Apache-2.0"
name = "hexyl"
readme = "README.md"
repository = "https://github.com/sharkdp/hexyl"
version = "0.12.0"
version = "0.13.0"
edition = "2021"
rust-version = "1.60"
+47 -5
View File
@@ -17,7 +17,7 @@ use thiserror::Error as ThisError;
use terminal_size::terminal_size;
use hexyl::{Base, BorderStyle, Input, PrinterBuilder};
use hexyl::{Base, BorderStyle, Endianness, Input, PrinterBuilder};
const DEFAULT_BLOCK_SIZE: i64 = 512;
@@ -128,10 +128,17 @@ fn run() -> Result<()> {
))
.arg(
Arg::new("no_chars")
.short('C')
.long("no-characters")
.action(ArgAction::SetFalse)
.help("Whether to display the character panel on the right."),
.help("Do not show the character panel on the right."),
)
.arg(
Arg::new("chars")
.short('C')
.long("characters")
.overrides_with("no_chars")
.action(ArgAction::SetTrue)
.help("Show the character panel on the right. This is the default, unless --no-characters has been specified."),
)
.arg(
Arg::new("no_position")
@@ -174,10 +181,34 @@ fn run() -> Result<()> {
.value_name("N")
.help(
"Number of bytes/octets that should be grouped together. \
Possible group sizes are 1, 2, 4, 8. The default is 1. \
'--groupsize can be used as an alias (xxd-compatibility).",
Possible group sizes are 1, 2, 4, 8. The default is 1. You \
can use the '--endianness' option to control the ordering of \
the bytes within a group. '--groupsize' can be used as an \
alias (xxd-compatibility).",
),
)
.arg(
Arg::new("endianness")
.long("endianness")
.num_args(1)
.value_name("FORMAT")
.value_parser(["big", "little"])
.default_value("big")
.help(
"Whether to print out groups in little-endian or big-endian \
format. This option only has an effect if the '--group-size' \
is larger than 1. '-e' can be used as an alias for \
'--endianness=little'.",
),
)
.arg(
Arg::new("little_endian_format")
.short('e')
.action(ArgAction::SetTrue)
.overrides_with("endianness")
.hide(true)
.help("An alias for '--endianness=little'."),
)
.arg(
Arg::new("base")
.short('b')
@@ -425,6 +456,16 @@ fn run() -> Result<()> {
)
};
let little_endian_format = *matches.get_one::<bool>("little_endian_format").unwrap();
let endianness = matches.get_one::<String>("endianness");
let endianness = match (
endianness.map(|s| s.as_ref()).unwrap(),
little_endian_format,
) {
(_, true) | ("little", _) => Endianness::Little,
("big", _) => Endianness::Big,
_ => unreachable!(),
};
let stdout = io::stdout();
let mut stdout_lock = BufWriter::new(stdout.lock());
@@ -437,6 +478,7 @@ fn run() -> Result<()> {
.num_panels(panels)
.group_size(group_size)
.with_base(base)
.endianness(endianness)
.build();
printer.display_offset(skip_offset + display_offset);
printer.print_all(&mut reader).map_err(|e| anyhow!(e))?;
+42 -1
View File
@@ -21,6 +21,7 @@ const COLOR_ASCII_OTHER: &[u8] = colors::Green::ANSI_FG.as_bytes();
const COLOR_NONASCII: &[u8] = colors::Yellow::ANSI_FG.as_bytes();
const COLOR_RESET: &[u8] = colors::Default::ANSI_FG.as_bytes();
#[derive(Copy, Clone)]
pub enum ByteCategory {
Null,
AsciiPrintable,
@@ -29,6 +30,12 @@ pub enum ByteCategory {
NonAscii,
}
#[derive(Copy, Clone)]
pub enum Endianness {
Little,
Big,
}
#[derive(PartialEq)]
enum Squeezer {
Print,
@@ -159,6 +166,7 @@ pub struct PrinterBuilder<'a, Writer: Write> {
panels: u64,
group_size: u8,
base: Base,
endianness: Endianness,
}
impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
@@ -173,6 +181,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
panels: 2,
group_size: 1,
base: Base::Hexadecimal,
endianness: Endianness::Big,
}
}
@@ -216,6 +225,11 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
self
}
pub fn endianness(mut self, endianness: Endianness) -> Self {
self.endianness = endianness;
self
}
pub fn build(self) -> Printer<'a, Writer> {
Printer::new(
self.writer,
@@ -227,6 +241,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
self.panels,
self.group_size,
self.base,
self.endianness,
)
}
}
@@ -254,6 +269,8 @@ pub struct Printer<'a, Writer: Write> {
group_size: u8,
/// The number of digits used to write the base.
base_digits: u8,
/// Whether to show groups in little or big endian format.
endianness: Endianness,
}
impl<'a, Writer: Write> Printer<'a, Writer> {
@@ -267,6 +284,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
panels: u64,
group_size: u8,
base: Base,
endianness: Endianness,
) -> Printer<'a, Writer> {
Printer {
idx: 0,
@@ -304,6 +322,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
Base::Decimal => 3,
Base::Hexadecimal => 2,
},
endianness,
}
}
@@ -523,8 +542,26 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
Ok(())
}
fn reorder_buffer_to_little_endian(&self, buf: &mut Vec<u8>) {
let n = buf.len();
let group_sz = self.group_size as usize;
for idx in (0..n).step_by(group_sz) {
let remaining = n - idx;
let total = remaining.min(group_sz);
buf[idx..idx + total].reverse();
}
}
pub fn print_bytes(&mut self) -> io::Result<()> {
for (i, &b) in self.line_buf.clone().iter().enumerate() {
let mut buf = self.line_buf.clone();
if matches!(self.endianness, Endianness::Little) {
self.reorder_buffer_to_little_endian(&mut buf);
};
for (i, &b) in buf.iter().enumerate() {
self.print_byte(i, b)?;
}
Ok(())
@@ -682,6 +719,7 @@ mod tests {
2,
1,
Base::Hexadecimal,
Endianness::Big,
);
printer.print_all(input).unwrap();
@@ -736,6 +774,7 @@ mod tests {
2,
1,
Base::Hexadecimal,
Endianness::Big,
);
printer.display_offset(0xdeadbeef);
@@ -769,6 +808,7 @@ mod tests {
4,
1,
Base::Hexadecimal,
Endianness::Big,
);
printer.print_all(input).unwrap();
@@ -828,6 +868,7 @@ mod tests {
3,
1,
Base::Hexadecimal,
Endianness::Big,
);
printer.print_all(input).unwrap();
+52 -4
View File
@@ -292,12 +292,12 @@ mod display_settings {
}
}
mod group {
mod group_and_endianness {
use super::hexyl;
use super::PrettyAssert;
#[test]
fn group_2_bytes() {
fn group_2_bytes_be() {
hexyl()
.arg("ascii")
.arg("--color=never")
@@ -312,7 +312,23 @@ mod group {
}
#[test]
fn group_4_bytes() {
fn group_2_bytes_le() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--group-size=2")
.arg("--endianness=little")
.assert()
.success()
.stdout(
"┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\
│00000000│ 3130 3332 3534 3736 ┊ 3938 6261 6463 0a65 │01234567┊89abcde_│\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n",
);
}
#[test]
fn group_4_bytes_be() {
hexyl()
.arg("ascii")
.arg("--color=never")
@@ -327,7 +343,23 @@ mod group {
}
#[test]
fn group_8_bytes() {
fn group_4_bytes_le() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--group-size=4")
.arg("--endianness=little")
.assert()
.success()
.stdout(
"┌────────┬───────────────────┬───────────────────┬────────┬────────┐\n\
│00000000│ 33323130 37363534 ┊ 62613938 0a656463 │01234567┊89abcde_│\n\
└────────┴───────────────────┴───────────────────┴────────┴────────┘\n",
);
}
#[test]
fn group_8_bytes_be() {
hexyl()
.arg("ascii")
.arg("--color=never")
@@ -341,6 +373,22 @@ mod group {
);
}
#[test]
fn group_8_bytes_le() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--group-size=8")
.arg("--endianness=little")
.assert()
.success()
.stdout(
"┌────────┬──────────────────┬──────────────────┬────────┬────────┐\n\
│00000000│ 3736353433323130 ┊ 0a65646362613938 │01234567┊89abcde_│\n\
└────────┴──────────────────┴──────────────────┴────────┴────────┘\n",
);
}
#[test]
fn group_size_plain() {
hexyl()