From 28e3c38e3c0490ac8bcb581feca44a2682280df2 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sat, 11 Feb 2023 08:59:25 -0800 Subject: [PATCH] xxd: add -e (little endian). Useful when trying to read tables of addresses/offsets (in ELF files, for example). --- tests/xxd.test | 7 +++++++ toys/other/xxd.c | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/xxd.test b/tests/xxd.test index 3897623e..3b350a46 100755 --- a/tests/xxd.test +++ b/tests/xxd.test @@ -61,4 +61,11 @@ testcmd "-r unnecessary output seeks" '-r | xxd' \ "00000000: 0100 0000 0000 0000 0000 0000 0000 00ff ................\n" '' \ '00000000: 0100 0000 0000 0000 0000 0000 0000 00ff deadbeef........\n' +# Little-endian, testing both the "EOF in first word on line" and "EOF in word +# mid-line" cases. +testcmd "LE partial" "-e -" \ + "00000000: 6568 he\n" "" "he" +testcmd "LE partial mid-line" "-e -" \ + "00000000: 6c6c6568 6f hello\n" "" "hello" + rm file1 file2 diff --git a/toys/other/xxd.c b/toys/other/xxd.c index bb0a8e26..65198ce9 100644 --- a/toys/other/xxd.c +++ b/toys/other/xxd.c @@ -10,18 +10,19 @@ * xxd -p "plain" output: * "4c696e75782076657273696f6e20342e392e302d342d616d643634202864" -USE_XXD(NEWTOY(xxd, ">1c#<0>256l#o#g#<0=2iprs#[!rs]", TOYFLAG_USR|TOYFLAG_BIN)) +USE_XXD(NEWTOY(xxd, ">1c#<0>256l#o#g#<0=2eiprs#[!rs][!re]", TOYFLAG_USR|TOYFLAG_BIN)) config XXD bool "xxd" default y help - usage: xxd [-c n] [-g n] [-i] [-l n] [-o n] [-p] [-r] [-s n] [file] + usage: xxd [-c n] [-g n] [-eipr] [-l n] [-o n] [-s n] [file] Hexdump a file to stdout. If no file is listed, copy from stdin. Filename "-" is a synonym for stdin. -c n Show n bytes per line (default 16) + -e Little-endian -g n Group bytes by adding a ' ' every n bytes (default 2) -i Output include file (CSV hex bytes, plus C header/footer if not stdin) -l n Limit of n bytes before stopping (default is no limit) @@ -42,7 +43,7 @@ static void do_xxd(int fd, char *name) { long long pos = 0; long long limit = TT.l; - int i, len, space, c = TT.c ? : sizeof(toybuf); + int i, j, k, len, space, c = TT.c ? : sizeof(toybuf); if (FLAG(s)) { xlseek(fd, TT.s, SEEK_SET); @@ -58,11 +59,21 @@ static void do_xxd(int fd, char *name) space = 2*TT.c; space += TT.g ? (TT.c+TT.g-1)/TT.g+1 : 2; - for (i=0; i