xxd patches

1,277 views
Skip to first unread message

Vadim Vygonets

unread,
Nov 11, 2013, 3:12:12 PM11/11/13
to vim...@googlegroups.com
Hello vimers,

I needed a couple of features that no hexdump utility had, so I
patched xxd. The added features are:

* -e little endian
* -o add offset to the displayed address
* change address width to 8 digits

You can view the patches here:

https://github.com/unixdj/xxd/commits/for-vim

If you're interested, I can prepare them in the prefered format
(hg?).

Best,
Vadim.

--
What sane person could live in this world and not be crazy?
-- Ursula K. LeGuin

Tony Mechelynck

unread,
Nov 11, 2013, 5:31:49 PM11/11/13
to vim...@googlegroups.com, Bram Moolenaar
On 11/11/13 21:12, Vadim Vygonets wrote:
> Hello vimers,
>
> I needed a couple of features that no hexdump utility had, so I
> patched xxd. The added features are:
>
> * -e little endian
> * -o add offset to the displayed address
> * change address width to 8 digits
>
> You can view the patches here:
>
> https://github.com/unixdj/xxd/commits/for-vim
>
> If you're interested, I can prepare them in the prefered format
> (hg?).
>
> Best,
> Vadim.
>
The preferred format is context diff (with unified diff a close second)
and such that it can be applied by starting at the top of the Vim source
tree. A typical "hg diff" patch in git format would compare, for
instance, a/src/xxd/xxd.c with b/src/xxd/xxd.c and similarly for xxd.1

See also ":help develop.txt".

Note that submitting a patch to this mailing list will bring it onto the
Vim community's radar; there's of course no guarantee that it will be
accepted, especially since in the case of xxd, that program is (IIUC) ©
Juergen Weigert <jnwe...@informatik.uni-erlangen.de> "with small
changes by Bram Moolenaar", so he (Juergen) should probably be CC-ed on
your post. I'm not sure of the details though, because when I invoke the
executable I get version 1.10 "27oct98 by Juergen Weigert" but the
manpage I have access to (and which mentions Bram) is the one for
version 1.7 (dated "August 1996"). After looking at runtime/doc/xxd.man
in the Vim source tree, it seems that "man 1 xxd" gives me the manpage
which came with Vim and that the version number in the man page was left
at 1.7 (an oversight, Bram?)

Best regards,
Tony.
--
<rcw> liiwi: printk("CPU0 on fire\n");

Vadim Vygonets

unread,
Nov 13, 2013, 4:56:09 AM11/13/13
to vim...@googlegroups.com
Quoth Tony Mechelynck on Mon, Nov 11, 2013:
> Note that submitting a patch to this mailing list will bring it onto the
> Vim community's radar; there's of course no guarantee that it will be
> accepted, especially since in the case of xxd, that program is (IIUC) ©
> Juergen Weigert <jnwe...@informatik.uni-erlangen.de> "with small
> changes by Bram Moolenaar", so he (Juergen) should probably be CC-ed on
> your post.

When I looked for sources of xxd (in the OS I use and online), I
only found them in vim, and thus assumed that xxd has been
adopted by the vim team. But sure, I'll try to contact Juergen
at his 15 year old academic e-mail address :)

Thanks a lot for your reply, I've read the help file and will
post proper patches in the next days.

Vadim.

--
Draft beer, not people

Vadim Vygonets

unread,
Nov 19, 2013, 6:23:23 AM11/19/13
to vim...@googlegroups.com
Hello vim_devils,

Juergen Weigert, the original author of xxd, didn't answer my
e-mail, so I'm submitting the patches here.

Vadim.

runtime/doc/xxd.1 | 12 +++++++++++-
src/xxd/xxd.c | 29 ++++++++++++++++++++++++-----
runtime/doc/xxd.1 | 5 +++++
src/xxd/xxd.c | 20 +++++++++++++++++---
src/xxd/xxd.c | 21 +++++++++++----------
5 files changed, 68 insertions(+), 19 deletions(-)


Vadim Vygonets

unread,
Nov 19, 2013, 6:23:24 AM11/19/13
to vim...@googlegroups.com
runtime/doc/xxd.1 | 12 +++++++++++-
src/xxd/xxd.c | 29 ++++++++++++++++++++++++-----
2 files changed, 35 insertions(+), 6 deletions(-)


# HG changeset patch
# User Vadim Vygonets <va...@vygo.net>
# Date 1384858885 -3600
# Node ID e5b9ff009e25d661ad1f71bba70c3f7ac71d2e80
# Parent 58bcf8fa172f6bbb254a90d1b9cec91d0a040157
xxd: Add -e: little endian hexdump

diff -r 58bcf8fa172f -r e5b9ff009e25 runtime/doc/xxd.1
--- a/runtime/doc/xxd.1 Sun Nov 17 20:32:54 2013 +0100
+++ b/runtime/doc/xxd.1 Tue Nov 19 12:01:25 2013 +0100
@@ -76,6 +76,16 @@
This does not change the hexadecimal representation. The option is
meaningless in combinations with \-r, \-p or \-i.
.TP
+.IR \-e
+Switch to little-endian hexdump.
+This option treats byte groups as words in little-endian byte order.
+The default grouping of 4 bytes may be changed using
+.RI "" \-g .
+This option only applies to hexdump, leaving the ASCII (or EBCDIC)
+representation unchanged.
+The command line switches
+\-r, \-p, \-i do not work with this mode.
+.TP
.IR "\-g bytes " | " \-groupsize bytes"
separate the output of every
.RI < bytes >
@@ -84,7 +94,7 @@
.I \-g 0
to suppress grouping.
.RI < Bytes "> defaults to " 2
-in normal mode and \fI1\fP in bits mode.
+in normal mode, \fI4\fP in little-endian mode and \fI1\fP in bits mode.
Grouping does not apply to postscript or include style.
.TP
.IR \-h " | " \-help
diff -r 58bcf8fa172f -r e5b9ff009e25 src/xxd/xxd.c
--- a/src/xxd/xxd.c Sun Nov 17 20:32:54 2013 +0100
+++ b/src/xxd/xxd.c Tue Nov 19 12:01:25 2013 +0100
@@ -51,6 +51,7 @@
* 16.05.00 Improved MMS file and merge for VMS by Zoltan Arpadffy
* 2011 March Better error handling by Florian Zumbiehl.
* 2011 April Formatting by Bram Moolenaar
+ * 08.06.2013 Little-endian hexdump (-e) by Vadim Vygonets.
*
* (c) 1990-1998 by Juergen Weigert (jnwe...@informatik.uni-erlangen.de)
*
@@ -225,6 +226,7 @@
#define HEX_POSTSCRIPT 1
#define HEX_CINCLUDE 2
#define HEX_BITS 3 /* not hex a dump, but bits: 01111001 */
+#define HEX_LITTLEENDIAN 4

static char *pname;

@@ -238,7 +240,8 @@
fprintf(stderr, " -b binary digit dump (incompatible with -ps,-i,-r). Default hex.\n");
fprintf(stderr, " -c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30).\n");
fprintf(stderr, " -E show characters in EBCDIC. Default ASCII.\n");
- fprintf(stderr, " -g number of octets per group in normal output. Default 2.\n");
+ fprintf(stderr, " -e little-endian dump (incompatible with -ps,-i,-r).\n");
+ fprintf(stderr, " -g number of octets per group in normal output. Default 2 (-e: 4).\n");
fprintf(stderr, " -h print this summary.\n");
fprintf(stderr, " -i output in C include file style.\n");
fprintf(stderr, " -l len stop after <len> octets.\n");
@@ -503,6 +506,7 @@
pp = argv[1] + (!STRNCMP(argv[1], "--", 2) && argv[1][2]);
if (!STRNCMP(pp, "-a", 2)) autoskip = 1 - autoskip;
else if (!STRNCMP(pp, "-b", 2)) hextype = HEX_BITS;
+ else if (!STRNCMP(pp, "-e", 2)) hextype = HEX_LITTLEENDIAN;
else if (!STRNCMP(pp, "-u", 2)) hexx = hexxa + 16;
else if (!STRNCMP(pp, "-p", 2)) hextype = HEX_POSTSCRIPT;
else if (!STRNCMP(pp, "-i", 2)) hextype = HEX_CINCLUDE;
@@ -603,6 +607,7 @@
case HEX_CINCLUDE: cols = 12; break;
case HEX_BITS: cols = 6; break;
case HEX_NORMAL:
+ case HEX_LITTLEENDIAN:
default: cols = 16; break;
}

@@ -611,20 +616,28 @@
{
case HEX_BITS: octspergrp = 1; break;
case HEX_NORMAL: octspergrp = 2; break;
+ case HEX_LITTLEENDIAN: octspergrp = 4; break;
case HEX_POSTSCRIPT:
case HEX_CINCLUDE:
default: octspergrp = 0; break;
}

- if (cols < 1 || ((hextype == HEX_NORMAL || hextype == HEX_BITS)
+ if (cols < 1 || ((hextype == HEX_NORMAL || hextype == HEX_BITS || hextype == HEX_LITTLEENDIAN)
&& (cols > COLS)))
{
fprintf(stderr, "%s: invalid number of columns (max. %d).\n", pname, COLS);
exit(1);
}

- if (octspergrp < 1)
+ if (octspergrp < 1 || octspergrp > cols)
octspergrp = cols;
+ else if (hextype == HEX_LITTLEENDIAN && (octspergrp & (octspergrp-1)))
+ {
+ fprintf(stderr,
+ "%s: number of octets per group must be a power of 2 with -e.\n",
+ pname);
+ exit(1);
+ }

if (argc > 3)
exit_with_usage();
@@ -781,9 +794,9 @@
return 0;
}

- /* hextype: HEX_NORMAL or HEX_BITS */
+ /* hextype: HEX_NORMAL or HEX_BITS or HEX_LITTLEENDIAN */

- if (hextype == HEX_NORMAL)
+ if (hextype != HEX_BITS)
grplen = octspergrp + octspergrp + 1; /* chars per octet group */
else /* hextype == HEX_BITS */
grplen = 8 * octspergrp + 1;
@@ -801,6 +814,12 @@
l[c = (9 + (grplen * p) / octspergrp)] = hexx[(e >> 4) & 0xf];
l[++c] = hexx[ e & 0xf];
}
+ else if (hextype == HEX_LITTLEENDIAN)
+ {
+ int x = p ^ (octspergrp-1);
+ l[c = (9 + (grplen * x) / octspergrp)] = hexx[(e >> 4) & 0xf];
+ l[++c] = hexx[ e & 0xf];
+ }
else /* hextype == HEX_BITS */
{
int i;

Vadim Vygonets

unread,
Nov 19, 2013, 6:23:25 AM11/19/13
to vim...@googlegroups.com
runtime/doc/xxd.1 | 5 +++++
src/xxd/xxd.c | 20 +++++++++++++++++---
2 files changed, 22 insertions(+), 3 deletions(-)


# HG changeset patch
# User Vadim Vygonets <va...@vygo.net>
# Date 1384858930 -3600
# Node ID f5e9ce53c1d8e9cfb73fdf3932681262e6be6707
# Parent e5b9ff009e25d661ad1f71bba70c3f7ac71d2e80
xxd: Add -o: add offset to displayed position

diff -r e5b9ff009e25 -r f5e9ce53c1d8 runtime/doc/xxd.1
--- a/runtime/doc/xxd.1 Tue Nov 19 12:01:25 2013 +0100
+++ b/runtime/doc/xxd.1 Tue Nov 19 12:02:10 2013 +0100
@@ -109,6 +109,11 @@
.RI < len >
octets.
.TP
+.I \-o offset
+add
+.RI < offset >
+to the displayed file position.
+.TP
.IR \-p " | " \-ps " | " \-postscript " | " \-plain
output in postscript continuous hexdump style. Also known as plain hexdump
style.
diff -r e5b9ff009e25 -r f5e9ce53c1d8 src/xxd/xxd.c
--- a/src/xxd/xxd.c Tue Nov 19 12:01:25 2013 +0100
+++ b/src/xxd/xxd.c Tue Nov 19 12:02:10 2013 +0100
@@ -51,7 +51,7 @@
* 16.05.00 Improved MMS file and merge for VMS by Zoltan Arpadffy
* 2011 March Better error handling by Florian Zumbiehl.
* 2011 April Formatting by Bram Moolenaar
- * 08.06.2013 Little-endian hexdump (-e) by Vadim Vygonets.
+ * 08.06.2013 Little-endian hexdump (-e) and offset (-o) by Vadim Vygonets.
*
* (c) 1990-1998 by Juergen Weigert (jnwe...@informatik.uni-erlangen.de)
*
@@ -245,6 +245,7 @@
fprintf(stderr, " -h print this summary.\n");
fprintf(stderr, " -i output in C include file style.\n");
fprintf(stderr, " -l len stop after <len> octets.\n");
+ fprintf(stderr, " -o off add <off> to the displayed file position.\n");
fprintf(stderr, " -ps output in postscript plain hexdump style.\n");
fprintf(stderr, " -r reverse operation: convert (or patch) hexdump into binary.\n");
fprintf(stderr, " -r -s off revert with <off> added to file positions found in hexdump.\n");
@@ -478,7 +479,7 @@
int ebcdic = 0;
int octspergrp = -1; /* number of octets grouped in output */
int grplen; /* total chars per octet group */
- long length = -1, n = 0, seekoff = 0;
+ long length = -1, n = 0, seekoff = 0, displayoff = 0;
static char l[LLEN+1]; /* static because it may be too big for stack */
char *pp;

@@ -543,6 +544,19 @@
argc--;
}
}
+ else if (!STRNCMP(pp, "-o", 2))
+ {
+ if (pp[2] && STRNCMP("ffset", pp + 2, 5))
+ displayoff = (int)strtol(pp + 2, NULL, 0);
+ else
+ {
+ if (!argv[2])
+ exit_with_usage();
+ displayoff = (int)strtol(argv[2], NULL, 0);
+ argv++;
+ argc--;
+ }
+ }
else if (!STRNCMP(pp, "-s", 2))
{
relseek = 0;
@@ -806,7 +820,7 @@
{
if (p == 0)
{
- sprintf(l, "%07lx: ", n + seekoff);
+ sprintf(l, "%07lx: ", n + seekoff + displayoff);
for (c = 9; c < LLEN; l[c++] = ' ');
}
if (hextype == HEX_NORMAL)

Bram Moolenaar

unread,
Nov 19, 2013, 4:42:28 PM11/19/13
to Vadim Vygonets, vim...@googlegroups.com

Vadim Vygonets wrote:

> runtime/doc/xxd.1 | 12 +++++++++++-
> src/xxd/xxd.c | 29 ++++++++++++++++++++++++-----
> 2 files changed, 35 insertions(+), 6 deletions(-)
>
>
> # HG changeset patch
> # User Vadim Vygonets <va...@vygo.net>
> # Date 1384858885 -3600
> # Node ID e5b9ff009e25d661ad1f71bba70c3f7ac71d2e80
> # Parent 58bcf8fa172f6bbb254a90d1b9cec91d0a040157
> xxd: Add -e: little endian hexdump

Thanks, I'll add your patches to the todo list.


--
If you had to identify, in one word, the reason why the
human race has not achieved, and never will achieve, its
full potential, that word would be "meetings."

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Álvaro GR

unread,
Jan 29, 2015, 6:11:25 AM1/29/15
to vim...@googlegroups.com, vadi...@vygo.net
El martes, 19 de noviembre de 2013, 22:42:28 (UTC+1), Bram Moolenaar escribió:
> Vadim Vygonets wrote:
>
>
> Thanks, I'll add your patches to the todo list.


I'd be grateful if that endianness patch was finally added to the source. Using hexdump to work around this is wasteful...

Regards,
Álvaro.
Reply all
Reply to author
Forward
0 new messages