0
0
mirror of https://github.com/vim/vim.git synced 2025-10-18 07:54:29 -04:00

patch 9.1.0732: xxd: cannot use -b and -i together

Problem:  xxd: cannot use -b and -i together
          (Irgendwer)
Solution: implement the missing changes
          (Andre Chang)

fixes: #15362
closes: #15661

Signed-off-by: Andre Chang <andre@augmentcode.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Andre Chang
2024-09-15 20:03:05 +02:00
committed by Christian Brabandt
parent 5e95c8f637
commit 150227258d
5 changed files with 95 additions and 50 deletions

View File

@@ -63,8 +63,8 @@ Toggle autoskip: A single '*' replaces NUL-lines. Default off.
Switch to bits (binary digits) dump, rather than hex dump. Switch to bits (binary digits) dump, rather than hex dump.
This option writes octets as eight digits "1"s and "0"s instead of a normal This option writes octets as eight digits "1"s and "0"s instead of a normal
hexadecimal dump. Each line is preceded by a line number in hexadecimal and hexadecimal dump. Each line is preceded by a line number in hexadecimal and
followed by an ASCII (or EBCDIC) representation. The command line switches followed by an ASCII (or EBCDIC) representation. The command line switch
\-p, \-i do not work with this mode. \-p does not work with this mode. Can be combined with \-i.
.TP .TP
.IR "\-c cols " | " \-cols cols" .IR "\-c cols " | " \-cols cols"
Format Format
@@ -109,7 +109,8 @@ Print a summary of available commands and exit. No hex dumping is performed.
.TP .TP
.IR \-i " | " \-include .IR \-i " | " \-include
Output in C include file style. A complete static array definition is written Output in C include file style. A complete static array definition is written
(named after the input file), unless xxd reads from stdin. (named after the input file), unless xxd reads from stdin. Can be combined
with \-b.
.TP .TP
.IR "\-l len " | " \-len len" .IR "\-l len " | " \-len len"
Stop after writing Stop after writing

View File

@@ -1,7 +1,5 @@
XXD(1) General Commands Manual XXD(1) XXD(1) General Commands Manual XXD(1)
NAME NAME
xxd - make a hex dump or do the reverse. xxd - make a hex dump or do the reverse.
@@ -37,8 +35,8 @@ OPTIONS
option writes octets as eight digits "1"s and "0"s instead of a option writes octets as eight digits "1"s and "0"s instead of a
normal hexadecimal dump. Each line is preceded by a line number normal hexadecimal dump. Each line is preceded by a line number
in hexadecimal and followed by an ASCII (or EBCDIC) representa in hexadecimal and followed by an ASCII (or EBCDIC) representa
tion. The command line switches -p, -i do not work with this tion. The command line switch -p does not work with this mode.
mode. Can be combined with -i.
-c cols | -cols cols -c cols | -cols cols
Format <cols> octets per line. Default 16 (-i: 12, -ps: 30, -b: Format <cols> octets per line. Default 16 (-i: 12, -ps: 30, -b:
@@ -77,7 +75,7 @@ OPTIONS
-i | -include -i | -include
Output in C include file style. A complete static array defini Output in C include file style. A complete static array defini
tion is written (named after the input file), unless xxd reads tion is written (named after the input file), unless xxd reads
from stdin. from stdin. Can be combined with -b.
-l len | -len len -l len | -len len
Stop after writing <len> octets. Stop after writing <len> octets.

View File

@@ -269,6 +269,23 @@ func Test_xxd()
endfor endfor
" Test 19: Print C include in binary format
let s:test += 1
call writefile(['TESTabcd09'], 'XXDfile')
%d
exe '0r! ' . s:xxd_cmd . ' -i -b XXDfile'
$d
let expected =<< trim [CODE]
unsigned char XXDfile[] = {
0b01010100, 0b01000101, 0b01010011, 0b01010100, 0b01100001, 0b01100010,
0b01100011, 0b01100100, 0b00110000, 0b00111001, 0b00001010
};
unsigned int XXDfile_len = 11;
[CODE]
call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
%d %d
bwipe! bwipe!
call delete('XXDfile') call delete('XXDfile')

View File

@@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
732,
/**/ /**/
731, 731,
/**/ /**/

View File

@@ -63,6 +63,7 @@
* 25.01.2024 revert the previous patch (size_t instead of unsigned int) * 25.01.2024 revert the previous patch (size_t instead of unsigned int)
* 10.02.2024 fix buffer-overflow when writing color output to buffer, #14003 * 10.02.2024 fix buffer-overflow when writing color output to buffer, #14003
* 10.05.2024 fix another buffer-overflow when writing colored output to buffer, #14738 * 10.05.2024 fix another buffer-overflow when writing colored output to buffer, #14738
* 10.09.2024 Support -b and -i together, #15661
* *
* (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com) * (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com)
* *
@@ -143,7 +144,7 @@ extern void perror __P((char *));
# endif # endif
#endif #endif
char version[] = "xxd 2024-05-10 by Juergen Weigert et al."; char version[] = "xxd 2024-09-15 by Juergen Weigert et al.";
#ifdef WIN32 #ifdef WIN32
char osver[] = " (Win32)"; char osver[] = " (Win32)";
#else #else
@@ -220,11 +221,11 @@ char osver[] = "";
char hexxa[] = "0123456789abcdef0123456789ABCDEF", *hexx = hexxa; char hexxa[] = "0123456789abcdef0123456789ABCDEF", *hexx = hexxa;
/* the different hextypes known by this program: */ /* the different hextypes known by this program: */
#define HEX_NORMAL 0 #define HEX_NORMAL 0x00 /* no flags set */
#define HEX_POSTSCRIPT 1 #define HEX_POSTSCRIPT 0x01
#define HEX_CINCLUDE 2 #define HEX_CINCLUDE 0x02
#define HEX_BITS 3 /* not hex a dump, but bits: 01111001 */ #define HEX_BITS 0x04 /* not hex a dump, but bits: 01111001 */
#define HEX_LITTLEENDIAN 4 #define HEX_LITTLEENDIAN 0x08
#define CONDITIONAL_CAPITALIZE(c) (capitalize ? toupper((unsigned char)(c)) : (c)) #define CONDITIONAL_CAPITALIZE(c) (capitalize ? toupper((unsigned char)(c)) : (c))
@@ -255,7 +256,7 @@ exit_with_usage(void)
fprintf(stderr, " or\n %s -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]\n", pname); fprintf(stderr, " or\n %s -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]\n", pname);
fprintf(stderr, "Options:\n"); fprintf(stderr, "Options:\n");
fprintf(stderr, " -a toggle autoskip: A single '*' replaces nul-lines. Default off.\n"); fprintf(stderr, " -a toggle autoskip: A single '*' replaces nul-lines. Default off.\n");
fprintf(stderr, " -b binary digit dump (incompatible with -ps,-i). Default hex.\n"); fprintf(stderr, " -b binary digit dump (incompatible with -ps). Default hex.\n");
fprintf(stderr, " -C capitalize variable names in C include file style (-i).\n"); fprintf(stderr, " -C capitalize variable names in C include file style (-i).\n");
fprintf(stderr, " -c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30).\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, " -E show characters in EBCDIC. Default ASCII.\n");
@@ -692,11 +693,11 @@ main(int argc, char *argv[])
{ {
pp = argv[1] + (!STRNCMP(argv[1], "--", 2) && argv[1][2]); pp = argv[1] + (!STRNCMP(argv[1], "--", 2) && argv[1][2]);
if (!STRNCMP(pp, "-a", 2)) autoskip = 1 - autoskip; if (!STRNCMP(pp, "-a", 2)) autoskip = 1 - autoskip;
else if (!STRNCMP(pp, "-b", 2)) hextype = HEX_BITS; else if (!STRNCMP(pp, "-b", 2)) hextype |= HEX_BITS;
else if (!STRNCMP(pp, "-e", 2)) hextype = HEX_LITTLEENDIAN; else if (!STRNCMP(pp, "-e", 2)) hextype |= HEX_LITTLEENDIAN;
else if (!STRNCMP(pp, "-u", 2)) hexx = hexxa + 16; else if (!STRNCMP(pp, "-u", 2)) hexx = hexxa + 16;
else if (!STRNCMP(pp, "-p", 2)) hextype = HEX_POSTSCRIPT; else if (!STRNCMP(pp, "-p", 2)) hextype |= HEX_POSTSCRIPT;
else if (!STRNCMP(pp, "-i", 2)) hextype = HEX_CINCLUDE; else if (!STRNCMP(pp, "-i", 2)) hextype |= HEX_CINCLUDE;
else if (!STRNCMP(pp, "-C", 2)) capitalize = 1; else if (!STRNCMP(pp, "-C", 2)) capitalize = 1;
else if (!STRNCMP(pp, "-d", 2)) decimal_offset = 1; else if (!STRNCMP(pp, "-d", 2)) decimal_offset = 1;
else if (!STRNCMP(pp, "-r", 2)) revert++; else if (!STRNCMP(pp, "-r", 2)) revert++;
@@ -856,11 +857,19 @@ main(int argc, char *argv[])
argc--; argc--;
} }
if (hextype != (HEX_CINCLUDE | HEX_BITS))
{
/* Allow at most one bit to be set in hextype */
if (hextype & (hextype - 1))
error_exit(1, "only one of -b, -e, -u, -p, -i can be used");
}
if (!colsgiven || (!cols && hextype != HEX_POSTSCRIPT)) if (!colsgiven || (!cols && hextype != HEX_POSTSCRIPT))
switch (hextype) switch (hextype)
{ {
case HEX_POSTSCRIPT: cols = 30; break; case HEX_POSTSCRIPT: cols = 30; break;
case HEX_CINCLUDE: cols = 12; break; case HEX_CINCLUDE: cols = 12; break;
case HEX_CINCLUDE | HEX_BITS:
case HEX_BITS: cols = 6; break; case HEX_BITS: cols = 6; break;
case HEX_NORMAL: case HEX_NORMAL:
case HEX_LITTLEENDIAN: case HEX_LITTLEENDIAN:
@@ -870,6 +879,7 @@ main(int argc, char *argv[])
if (octspergrp < 0) if (octspergrp < 0)
switch (hextype) switch (hextype)
{ {
case HEX_CINCLUDE | HEX_BITS:
case HEX_BITS: octspergrp = 1; break; case HEX_BITS: octspergrp = 1; break;
case HEX_NORMAL: octspergrp = 2; break; case HEX_NORMAL: octspergrp = 2; break;
case HEX_LITTLEENDIAN: octspergrp = 4; break; case HEX_LITTLEENDIAN: octspergrp = 4; break;
@@ -966,7 +976,7 @@ main(int argc, char *argv[])
} }
} }
if (hextype == HEX_CINCLUDE) if (hextype & HEX_CINCLUDE)
{ {
/* A user-set variable name overrides fp == stdin */ /* A user-set variable name overrides fp == stdin */
if (varname == NULL && fp != stdin) if (varname == NULL && fp != stdin)
@@ -982,11 +992,28 @@ main(int argc, char *argv[])
p = 0; p = 0;
while ((length < 0 || p < length) && (c = getc_or_die(fp)) != EOF) while ((length < 0 || p < length) && (c = getc_or_die(fp)) != EOF)
{
if (hextype & HEX_BITS)
{
if (p == 0)
fputs_or_die(" ", fpo);
else if (p % cols == 0)
fputs_or_die(",\n ", fpo);
else
fputs_or_die(", ", fpo);
FPRINTF_OR_DIE((fpo, "0b"));
for (int j = 7; j >= 0; j--)
putc_or_die((c & (1 << j)) ? '1' : '0', fpo);
p++;
}
else
{ {
FPRINTF_OR_DIE((fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", FPRINTF_OR_DIE((fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
(p % cols) ? ", " : (!p ? " " : ",\n "), c)); (p % cols) ? ", " : (!p ? " " : ",\n "), c));
p++; p++;
} }
}
if (p) if (p)
fputs_or_die("\n", fpo); fputs_or_die("\n", fpo);