From 7ba559b0ee2da46bcad9627377e8a86a52313777 Mon Sep 17 00:00:00 2001 From: "S. Gilles" Date: Sat, 18 Dec 2021 12:35:18 -0500 Subject: [PATCH] app-text/xxd: add (back?) --- app-text/xxd/Manifest | 4 + app-text/xxd/files/Makefile | 7 + app-text/xxd/files/xxd.1 | 388 +++++++++++++++++ app-text/xxd/files/xxd.c | 879 +++++++++++++++++++++++++++++++++++++++ app-text/xxd/xxd-8.2.3533.ebuild | 25 ++ 5 files changed, 1303 insertions(+) create mode 100644 app-text/xxd/Manifest create mode 100644 app-text/xxd/files/Makefile create mode 100644 app-text/xxd/files/xxd.1 create mode 100644 app-text/xxd/files/xxd.c create mode 100644 app-text/xxd/xxd-8.2.3533.ebuild diff --git a/app-text/xxd/Manifest b/app-text/xxd/Manifest new file mode 100644 index 0000000..db2400f --- /dev/null +++ b/app-text/xxd/Manifest @@ -0,0 +1,4 @@ +AUX Makefile 116 BLAKE2B e1274f5e146c2ca844289dc89636e9e26462767a93ab8fdfc9931f4c68e436426ec55f4f7c659596b86751f3f27f8d89a6e269aac1b2aad929ae653a4820826c SHA512 5f847d62ccd75a36471145a316b9ab77d5ade46d86d37c625989a74cb91cf713e0b0f8a89aa4d07ec253154a5fefb4487b4dc51d57635d0b083404f4d5d9777a +AUX xxd.1 10501 BLAKE2B 0eafbbc8775f135d316d756cf967f8ba4b15b0c8874654e87de0781c2022282a9f67edcc437d4facaf06392e6da0202939d2035504cf274927f6187500aa3dc9 SHA512 2f6c1d51c996882138a3980514fdfb1b3d22ae4592e436065b4c31c646111c0f009acd28f8ace6105115890afbc6450b434b3abf31d7c87be2bf4b47402d7f10 +AUX xxd.c 24405 BLAKE2B 01e4bfcb8ca7edeba9e48278bfc742823c23bf89cca5c9770d11e4ff749c4628a2ef6cde3fb716b796d0b12e71a770dc7f9020c99cef03816b892aa1134d6eb8 SHA512 e71a8dcbfd008a699f4ed215970b1b631eb8d16c920888abd21127ceb6c0b82e82f80047795c01212cc1cd2c316e062c261f1c5b5bc8d92f872ac46c1357cd69 +EBUILD xxd-8.2.3533.ebuild 517 BLAKE2B 5abcc8b204ec463aa982e08d2435f363d8b8b55d0f06ef647f53ee8029dfbfcceebf839fb970dec538dce387e1df3010600ff5708d2309ffafc86245fe8f298d SHA512 038124ac49e7a6697bbfef8bf9e0062cc4c242c93c472c6450abdf52d2c7ca0809551c0f2c5050398085d1075466162d9b942d52f19e34e77753700edfa3697e diff --git a/app-text/xxd/files/Makefile b/app-text/xxd/files/Makefile new file mode 100644 index 0000000..97bbcc7 --- /dev/null +++ b/app-text/xxd/files/Makefile @@ -0,0 +1,7 @@ +# The most simplistic Makefile + +xxd: xxd.c + $(CC) $(CFLAGS) $(LDFLAGS) -DUNIX -o xxd xxd.c + +clean: + rm -f xxd xxd.o diff --git a/app-text/xxd/files/xxd.1 b/app-text/xxd/files/xxd.1 new file mode 100644 index 0000000..46bfedc --- /dev/null +++ b/app-text/xxd/files/xxd.1 @@ -0,0 +1,388 @@ +.TH XXD 1 "August 1996" "Manual page for xxd" +.\" +.\" 21st May 1996 +.\" Man page author: +.\" Tony Nugent +.\" Changes by Bram Moolenaar +.SH NAME +.I xxd +\- make a hexdump or do the reverse. +.SH SYNOPSIS +.B xxd +\-h[elp] +.br +.B xxd +[options] [infile [outfile]] +.br +.B xxd +\-r[evert] [options] [infile [outfile]] +.SH DESCRIPTION +.I xxd +creates a hex dump of a given file or standard input. +It can also convert a hex dump back to its original binary form. +Like +.BR uuencode (1) +and +.BR uudecode (1) +it allows the transmission of binary data in a `mail-safe' ASCII representation, +but has the advantage of decoding to standard output. +Moreover, it can be used to perform binary file patching. +.SH OPTIONS +If no +.I infile +is given, standard input is read. +If +.I infile +is specified as a +.RB \` \- ' +character, then input is taken from standard input. +If no +.I outfile +is given (or a +.RB \` \- ' +character is in its place), results are sent to standard output. +.PP +Note that a "lazy" parser is used which does not check for more than the first +option letter, unless the option is followed by a parameter. +Spaces between a single option letter and its parameter are optional. +Parameters to options can be specified in decimal, hexadecimal or octal +notation. +Thus +.BR \-c8 , +.BR "\-c 8" , +.B \-c 010 +and +.B \-cols 8 +are all equivalent. +.PP +.TP +.IR \-a " | " \-autoskip +Toggle autoskip: A single '*' replaces nul-lines. Default off. +.TP +.IR \-b " | " \-bits +Switch to bits (binary digits) dump, rather than hexdump. +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 +followed by an ascii (or ebcdic) representation. The command line switches +\-r, \-p, \-i do not work with this mode. +.TP +.IR "\-c cols " | " \-cols cols" +Format +.RI < cols > +octets per line. Default 16 (\-i: 12, \-ps: 30, \-b: 6). Max 256. +.TP +.IR \-C " | " \-capitalize +Capitalize variable names in C include file style, when using \-i. +.TP +.IR \-E " | " \-EBCDIC +Change the character encoding in the righthand column from ASCII to EBCDIC. +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 > +bytes (two hex characters or eight bit-digits each) by a whitespace. +Specify +.I \-g 0 +to suppress grouping. +.RI < Bytes "> defaults to " 2 +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 +Print a summary of available commands and exit. No hex dumping is performed. +.TP +.IR \-i " | " \-include +Output in C include file style. A complete static array definition is written +(named after the input file), unless xxd reads from stdin. +.TP +.IR "\-l len " | " \-len len" +Stop after writing +.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. +.TP +.IR \-r " | " \-revert +Reverse operation: convert (or patch) hexdump into binary. +If not writing to stdout, xxd writes into its output file without truncating +it. Use the combination +.I \-r \-p +to read plain hexadecimal dumps without line number information and without a +particular column layout. Additional Whitespace and line-breaks are allowed +anywhere. +.TP +.I \-seek offset +When used after +.IR \-r : +revert with +.RI < offset > +added to file positions found in hexdump. +.TP +.I \-s [+][\-]seek +Start at +.RI < seek > +bytes abs. (or rel.) infile offset. +\fI+ \fRindicates that the seek is relative to the current stdin file position +(meaningless when not reading from stdin). \fI\- \fRindicates that the seek +should be that many characters from the end of the input (or if combined with +\fI+\fR: before the current stdin file position). +Without \-s option, xxd starts at the current file position. +.TP +.I \-u +Use upper case hex letters. Default is lower case. +.TP +.IR \-v " | " \-version +Show version string. +.SH CAVEATS +.PP +.I xxd \-r +has some builtin magic while evaluating line number information. +If the output file is seekable, then the linenumbers at the start of each +hexdump line may be out of order, lines may be missing, or overlapping. In +these cases xxd will lseek(2) to the next position. If the output file is not +seekable, only gaps are allowed, which will be filled by null-bytes. +.PP +.I xxd \-r +never generates parse errors. Garbage is silently skipped. +.PP +When editing hexdumps, please note that +.I xxd \-r +skips everything on the input line after reading enough columns of hexadecimal +data (see option \-c). This also means, that changes to the printable ascii (or +ebcdic) columns are always ignored. Reverting a plain (or postscript) style +hexdump with xxd \-r \-p does not depend on the correct number of columns. Here anything that looks like a pair of hex-digits is interpreted. +.PP +Note the difference between +.br +\fI% xxd \-i file\fR +.br +and +.br +\fI% xxd \-i < file\fR +.PP +.I xxd \-s +seek +may be different from +.IR "xxd \-s seek" , +as lseek(2) is used to "rewind" input. A '+' +makes a difference if the input source is stdin, and if stdin's file position +is not at the start of the file by the time xxd is started and given its input. +The following examples may help to clarify (or further confuse!)... +.PP +Rewind stdin before reading; needed because the `cat' has already read to the +end of stdin. +.br +\fI% sh \-c "cat > plain_copy; xxd \-s 0 > hex_copy" < file\fR +.PP +Hexdump from file position 0x480 (=1024+128) onwards. +The `+' sign means "relative to the current position", thus the `128' adds to +the 1k where dd left off. +.br +\fI% sh \-c "dd of=plain_snippet bs=1k count=1; xxd \-s +128 > hex_snippet" < file\fR +.PP +Hexdump from file position 0x100 ( = 1024\-768) on. +.br +\fI% sh \-c "dd of=plain_snippet bs=1k count=1; xxd \-s +\-768 > hex_snippet" < file\fR +.PP +However, this is a rare situation and the use of `+' is rarely needed. +The author prefers to monitor the effect of xxd with strace(1) or truss(1), whenever \-s is used. +.SH EXAMPLES +.PP +.br +Print everything but the first three lines (hex 0x30 bytes) of +.BR file . +.br +\fI% xxd \-s 0x30 file\fR +.PP +.br +Print 3 lines (hex 0x30 bytes) from the end of +.BR file . +.br +\fI% xxd \-s \-0x30 file\fR +.PP +.br +Print 120 bytes as continuous hexdump with 20 octets per line. +.br +\fI% xxd \-l 120 \-ps \-c 20 xxd.1\fR +.br +2e54482058584420312022417567757374203139 +.br +39362220224d616e75616c207061676520666f72 +.br +20787864220a2e5c220a2e5c222032317374204d +.br +617920313939360a2e5c22204d616e2070616765 +.br +20617574686f723a0a2e5c2220202020546f6e79 +.br +204e7567656e74203c746f6e79407363746e7567 +.br + +.br +Hexdump the first 120 bytes of this man page with 12 octets per line. +.br +\fI% xxd \-l 120 \-c 12 xxd.1\fR +.br +0000000: 2e54 4820 5858 4420 3120 2241 .TH XXD 1 "A +.br +000000c: 7567 7573 7420 3139 3936 2220 ugust 1996" +.br +0000018: 224d 616e 7561 6c20 7061 6765 "Manual page +.br +0000024: 2066 6f72 2078 7864 220a 2e5c for xxd"..\\ +.br +0000030: 220a 2e5c 2220 3231 7374 204d "..\\" 21st M +.br +000003c: 6179 2031 3939 360a 2e5c 2220 ay 1996..\\" +.br +0000048: 4d61 6e20 7061 6765 2061 7574 Man page aut +.br +0000054: 686f 723a 0a2e 5c22 2020 2020 hor:..\\" +.br +0000060: 546f 6e79 204e 7567 656e 7420 Tony Nugent +.br +000006c: 3c74 6f6e 7940 7363 746e 7567 output_file\fR +.br + +.br +Patch the date in the file xxd.1 +.br +\fI% echo "0000037: 3574 68" | xxd \-r \- xxd.1\fR +.br +\fI% xxd \-s 0x36 \-l 13 \-c 13 xxd.1\fR +.br +0000036: 3235 7468 204d 6179 2031 3939 36 25th May 1996 +.PP +.br +Create a 65537 byte file with all bytes 0x00, +except for the last one which is 'A' (hex 0x41). +.br +\fI% echo "010000: 41" | xxd \-r > file\fR +.PP +.br +Hexdump this file with autoskip. +.br +\fI% xxd \-a \-c 12 file\fR +.br +0000000: 0000 0000 0000 0000 0000 0000 ............ +.br +* +.br +000fffc: 0000 0000 40 ....A +.PP +Create a 1 byte file containing a single 'A' character. +The number after '\-r \-s' adds to the linenumbers found in the file; +in effect, the leading bytes are suppressed. +.br +\fI% echo "010000: 41" | xxd \-r \-s \-0x10000 > file\fR +.PP +Use xxd as a filter within an editor such as +.B vim(1) +to hexdump a region marked between `a' and `z'. +.br +\fI:'a,'z!xxd\fR +.PP +Use xxd as a filter within an editor such as +.B vim(1) +to recover a binary hexdump marked between `a' and `z'. +.br +\fI:'a,'z!xxd \-r\fR +.PP +Use xxd as a filter within an editor such as +.B vim(1) +to recover one line of a hexdump. Move the cursor over the line and type: +.br +\fI!!xxd \-r\fR +.PP +Read single characters from a serial line +.br +\fI% xxd \-c1 < /dev/term/b &\fR +.br +\fI% stty < /dev/term/b \-echo \-opost \-isig \-icanon min 1\fR +.br +\fI% echo \-n foo > /dev/term/b\fR +.PP +.SH "RETURN VALUES" +The following error values are returned: +.TP +0 +no errors encountered. +.TP +\-1 +operation not supported ( +.I xxd \-r \-i +still impossible). +.TP +1 +error while parsing options. +.TP +2 +problems with input file. +.TP +3 +problems with output file. +.TP +4,5 +desired seek position is unreachable. +.SH "SEE ALSO" +uuencode(1), uudecode(1), patch(1) +.br +.SH WARNINGS +The tools weirdness matches its creators brain. +Use entirely at your own risk. Copy files. Trace it. Become a wizard. +.br +.SH VERSION +This manual page documents xxd version 1.7 +.SH AUTHOR +.br +(c) 1990-1997 by Juergen Weigert +.br + +.LP +Distribute freely and credit me, +.br +make money and share with me, +.br +lose money and don't ask me. +.PP +Manual page started by Tony Nugent +.br + +.br +Small changes by Bram Moolenaar. +Edited by Juergen Weigert. +.PP diff --git a/app-text/xxd/files/xxd.c b/app-text/xxd/files/xxd.c new file mode 100644 index 0000000..749df43 --- /dev/null +++ b/app-text/xxd/files/xxd.c @@ -0,0 +1,879 @@ +/* xxd: my hexdump facility. jw + * + * 2.10.90 changed to word output + * 3.03.93 new indent style, dumb bug inserted and fixed. + * -c option, mls + * 26.04.94 better option parser, -ps, -l, -s added. + * 1.07.94 -r badly needs - as input file. Per default autoskip over + * consecutive lines of zeroes, as unix od does. + * -a shows them too. + * -i dump as c-style #include "file.h" + * 1.11.95 if "xxd -i" knows the filename, an 'unsigned char filename_bits[]' + * array is written in correct c-syntax. + * -s improved, now defaults to absolute seek, relative requires a '+'. + * -r improved, now -r -s -0x... is supported. + * change/suppress leading '\0' bytes. + * -l n improved: stops exactly after n bytes. + * -r improved, better handling of partial lines with trailing garbage. + * -r improved, now -r -p works again! + * -r improved, less flushing, much faster now! (that was silly) + * 3.04.96 Per repeated request of a single person: autoskip defaults to off. + * 15.05.96 -v added. They want to know the version. + * -a fixed, to show last line inf file ends in all zeros. + * -u added: Print upper case hex-letters, as preferred by unix bc. + * -h added to usage message. Usage message extended. + * Now using outfile if specified even in normal mode, aehem. + * No longer mixing of ints and longs. May help doze people. + * Added binify ioctl for same reason. (Enough Doze stress for 1996!) + * 16.05.96 -p improved, removed occasional superfluous linefeed. + * 20.05.96 -l 0 fixed. tried to read anyway. + * 21.05.96 -i fixed. now honours -u, and prepends __ to numeric filenames. + * compile -DWIN32 for NT or W95. George V. Reilly, * -v improved :-) + * support --gnuish-longhorn-options + * 25.05.96 MAC support added: CodeWarrior already uses ``outline'' in Types.h + * which is included by MacHeaders (Axel Kielhorn). Renamed to + * xxdline(). + * 7.06.96 -i printed 'int' instead of 'char'. *blush* + * added Bram's OS2 ifdefs... + * 18.07.96 gcc -Wall @ SunOS4 is now silent. + * Added osver for MSDOS/DJGPP/WIN32. + * 29.08.96 Added size_t to strncmp() for Amiga. + * 24.03.97 Windows NT support (Phil Hanna). Clean exit for Amiga WB (Bram) + * 02.04.97 Added -E option, to have EBCDIC translation instead of ASCII + * (azc10@yahoo.com) + * 22.05.97 added -g (group octets) option (jcook@namerica.kla.com). + * 23.09.98 nasty -p -r misfeature fixed: slightly wrong output, when -c was + * missing or wrong. + * 26.09.98 Fixed: 'xxd -i infile outfile' did not truncate outfile. + * 27.10.98 Fixed: -g option parser required blank. + * option -b added: 01000101 binary output in normal format. + * 16.05.00 Added VAXC changes by Stephen P. Wall + * 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) and offset (-o) by Vadim Vygonets. + * 11.01.2019 Add full 64/32 bit range to -o and output by Christer Jensen. + * 04.02.2020 Add -d for decimal offsets by Aapo Rantalainen + * + * (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com) + * + * I hereby grant permission to distribute and use xxd + * under X11-MIT or GPL-2.0 (at the user's choice). + * + * Contributions by Bram Moolenaar et al. + */ + +/* Visual Studio 2005 has 'deprecated' many of the standard CRT functions */ +#if _MSC_VER >= 1400 +# define _CRT_SECURE_NO_DEPRECATE +# define _CRT_NONSTDC_NO_DEPRECATE +#endif +#if !defined(CYGWIN) && defined(__CYGWIN__) +# define CYGWIN +#endif + +#include +#ifdef VAXC +# include +#else +# include +#endif +#if defined(WIN32) || defined(CYGWIN) +# include /* for setmode() */ +#else +# ifdef UNIX +# include +# endif +#endif +#include +#include /* for strncmp() */ +#include /* for isalnum() */ +#include +#if __MWERKS__ && !defined(BEBOX) +# include /* for fdopen() on MAC */ +#endif + + +/* This corrects the problem of missing prototypes for certain functions + * in some GNU installations (e.g. SunOS 4.1.x). + * Darren Hiebert (sparc-sun-sunos4.1.3_U1/2.7.2.2) + */ +#if defined(__GNUC__) && defined(__STDC__) +# ifndef __USE_FIXED_PROTOTYPES__ +# define __USE_FIXED_PROTOTYPES__ +# endif +#endif + +#ifndef __USE_FIXED_PROTOTYPES__ +/* + * This is historic and works only if the compiler really has no prototypes: + * + * Include prototypes for Sun OS 4.x, when using an ANSI compiler. + * FILE is defined on OS 4.x, not on 5.x (Solaris). + * if __SVR4 is defined (some Solaris versions), don't include this. + */ +#if defined(sun) && defined(FILE) && !defined(__SVR4) && defined(__STDC__) +# define __P(a) a +/* excerpt from my sun_stdlib.h */ +extern int fprintf __P((FILE *, char *, ...)); +extern int fputs __P((char *, FILE *)); +extern int _flsbuf __P((unsigned char, FILE *)); +extern int _filbuf __P((FILE *)); +extern int fflush __P((FILE *)); +extern int fclose __P((FILE *)); +extern int fseek __P((FILE *, long, int)); +extern int rewind __P((FILE *)); + +extern void perror __P((char *)); +# endif +#endif + +extern long int strtol(); +extern long int ftell(); + +char version[] = "xxd 2020-02-04 by Juergen Weigert et al."; +#ifdef WIN32 +char osver[] = " (Win32)"; +#else +char osver[] = ""; +#endif + +#if defined(WIN32) +# define BIN_READ(yes) ((yes) ? "rb" : "rt") +# define BIN_WRITE(yes) ((yes) ? "wb" : "wt") +# define BIN_CREAT(yes) ((yes) ? (O_CREAT|O_BINARY) : O_CREAT) +# define BIN_ASSIGN(fp, yes) setmode(fileno(fp), (yes) ? O_BINARY : O_TEXT) +# define PATH_SEP '\\' +#elif defined(CYGWIN) +# define BIN_READ(yes) ((yes) ? "rb" : "rt") +# define BIN_WRITE(yes) ((yes) ? "wb" : "w") +# define BIN_CREAT(yes) ((yes) ? (O_CREAT|O_BINARY) : O_CREAT) +# define BIN_ASSIGN(fp, yes) ((yes) ? (void) setmode(fileno(fp), O_BINARY) : (void) (fp)) +# define PATH_SEP '/' +#else +# ifdef VMS +# define BIN_READ(dummy) "r" +# define BIN_WRITE(dummy) "w" +# define BIN_CREAT(dummy) O_CREAT +# define BIN_ASSIGN(fp, dummy) fp +# define PATH_SEP ']' +# define FILE_SEP '.' +# else +# define BIN_READ(dummy) "r" +# define BIN_WRITE(dummy) "w" +# define BIN_CREAT(dummy) O_CREAT +# define BIN_ASSIGN(fp, dummy) fp +# define PATH_SEP '/' +# endif +#endif + +/* open has only to arguments on the Mac */ +#if __MWERKS__ +# define OPEN(name, mode, umask) open(name, mode) +#else +# define OPEN(name, mode, umask) open(name, mode, umask) +#endif + +#ifdef AMIGA +# define STRNCMP(s1, s2, l) strncmp(s1, s2, (size_t)l) +#else +# define STRNCMP(s1, s2, l) strncmp(s1, s2, l) +#endif + +#ifndef __P +# if defined(__STDC__) || defined(WIN32) +# define __P(a) a +# else +# define __P(a) () +# endif +#endif + +/* Let's collect some prototypes */ +/* CodeWarrior is really picky about missing prototypes */ +static void exit_with_usage __P((void)); +static void die __P((int)); +static int huntype __P((FILE *, FILE *, FILE *, int, int, long)); +static void xxdline __P((FILE *, char *, int)); + +#define TRY_SEEK /* attempt to use lseek, or skip forward by reading */ +#define COLS 256 /* change here, if you ever need more columns */ +#define LLEN ((2*(int)sizeof(unsigned long)) + 4 + (9*COLS-1) + COLS + 2) + +char hexxa[] = "0123456789abcdef0123456789ABCDEF", *hexx = hexxa; + +/* the different hextypes known by this program: */ +#define HEX_NORMAL 0 +#define HEX_POSTSCRIPT 1 +#define HEX_CINCLUDE 2 +#define HEX_BITS 3 /* not hex a dump, but bits: 01111001 */ +#define HEX_LITTLEENDIAN 4 + +#define CONDITIONAL_CAPITALIZE(c) (capitalize ? toupper((int)c) : c) + +static char *pname; + + static void +exit_with_usage(void) +{ + fprintf(stderr, "Usage:\n %s [options] [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, " -a toggle autoskip: A single '*' replaces nul-lines. Default off.\n"); + fprintf(stderr, " -b binary digit dump (incompatible with -ps,-i,-r). Default hex.\n"); + fprintf(stderr, " -C capitalize variable names in C include file style (-i).\n"); + fprintf(stderr, " -c cols format octets per line. Default 16 (-i: 12, -ps: 30).\n"); + fprintf(stderr, " -E show characters in EBCDIC. Default ASCII.\n"); + fprintf(stderr, " -e little-endian dump (incompatible with -ps,-i,-r).\n"); + fprintf(stderr, " -g bytes 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 octets.\n"); + fprintf(stderr, " -o off add 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 added to file positions found in hexdump.\n"); + fprintf(stderr, " -d show offset in decimal instead of hex.\n"); + fprintf(stderr, " -s %sseek start at bytes abs. %sinfile offset.\n", +#ifdef TRY_SEEK + "[+][-]", "(or +: rel.) "); +#else + "", ""); +#endif + fprintf(stderr, " -u use upper case hex letters.\n"); + fprintf(stderr, " -v show version: \"%s%s\".\n", version, osver); + exit(1); +} + + static void +die(int ret) +{ + fprintf(stderr, "%s: ", pname); + perror(NULL); + exit(ret); +} + +/* + * Max. cols binary characters are decoded from the input stream per line. + * Two adjacent garbage characters after evaluated data delimit valid data. + * Everything up to the next newline is discarded. + * + * The name is historic and came from 'undo type opt h'. + */ + static int +huntype( + FILE *fpi, + FILE *fpo, + FILE *fperr, + int cols, + int hextype, + long base_off) +{ + int c, ign_garb = 1, n1 = -1, n2 = 0, n3, p = cols; + long have_off = 0, want_off = 0; + + rewind(fpi); + + while ((c = getc(fpi)) != EOF) + { + if (c == '\r') /* Doze style input file? */ + continue; + + /* Allow multiple spaces. This doesn't work when there is normal text + * after the hex codes in the last line that looks like hex, thus only + * use it for PostScript format. */ + if (hextype == HEX_POSTSCRIPT && (c == ' ' || c == '\n' || c == '\t')) + continue; + + n3 = n2; + n2 = n1; + + if (c >= '0' && c <= '9') + n1 = c - '0'; + else if (c >= 'a' && c <= 'f') + n1 = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + n1 = c - 'A' + 10; + else + { + n1 = -1; + if (ign_garb) + continue; + } + + ign_garb = 0; + + if (!hextype && (p >= cols)) + { + if (n1 < 0) + { + p = 0; + continue; + } + want_off = (want_off << 4) | n1; + continue; + } + + if (base_off + want_off != have_off) + { + if (fflush(fpo) != 0) + die(3); +#ifdef TRY_SEEK + c = fseek(fpo, base_off + want_off - have_off, 1); + if (c >= 0) + have_off = base_off + want_off; +#endif + if (base_off + want_off < have_off) + { + fprintf(fperr, "%s: sorry, cannot seek backwards.\n", pname); + return 5; + } + for (; have_off < base_off + want_off; have_off++) + if (putc(0, fpo) == EOF) + die(3); + } + + if (n2 >= 0 && n1 >= 0) + { + if (putc((n2 << 4) | n1, fpo) == EOF) + die(3); + have_off++; + want_off++; + n1 = -1; + if (!hextype && (++p >= cols)) + { + /* skip the rest of the line as garbage */ + n2 = -1; + n3 = -1; + } + } + if (n1 < 0 && n2 < 0 && n3 < 0) + { + /* already stumbled into garbage, skip line, wait and see */ + if (!hextype) + want_off = 0; + while ((c = getc(fpi)) != '\n' && c != EOF) + ; + if (c == EOF && ferror(fpi)) + die(2); + ign_garb = 1; + } + } + if (fflush(fpo) != 0) + die(3); +#ifdef TRY_SEEK + fseek(fpo, 0L, 2); +#endif + if (fclose(fpo) != 0) + die(3); + if (fclose(fpi) != 0) + die(2); + return 0; +} + +/* + * Print line l. If nz is false, xxdline regards the line a line of + * zeroes. If there are three or more consecutive lines of zeroes, + * they are replaced by a single '*' character. + * + * If the output ends with more than two lines of zeroes, you + * should call xxdline again with l being the last line and nz + * negative. This ensures that the last line is shown even when + * it is all zeroes. + * + * If nz is always positive, lines are never suppressed. + */ + static void +xxdline(FILE *fp, char *l, int nz) +{ + static char z[LLEN+1]; + static int zero_seen = 0; + + if (!nz && zero_seen == 1) + strcpy(z, l); + + if (nz || !zero_seen++) + { + if (nz) + { + if (nz < 0) + zero_seen--; + if (zero_seen == 2) + if (fputs(z, fp) == EOF) + die(3); + if (zero_seen > 2) + if (fputs("*\n", fp) == EOF) + die(3); + } + if (nz >= 0 || zero_seen > 0) + if (fputs(l, fp) == EOF) + die(3); + if (nz) + zero_seen = 0; + } +} + +/* This is an EBCDIC to ASCII conversion table */ +/* from a proposed BTL standard April 16, 1979 */ +static unsigned char etoa64[] = +{ + 0040,0240,0241,0242,0243,0244,0245,0246, + 0247,0250,0325,0056,0074,0050,0053,0174, + 0046,0251,0252,0253,0254,0255,0256,0257, + 0260,0261,0041,0044,0052,0051,0073,0176, + 0055,0057,0262,0263,0264,0265,0266,0267, + 0270,0271,0313,0054,0045,0137,0076,0077, + 0272,0273,0274,0275,0276,0277,0300,0301, + 0302,0140,0072,0043,0100,0047,0075,0042, + 0303,0141,0142,0143,0144,0145,0146,0147, + 0150,0151,0304,0305,0306,0307,0310,0311, + 0312,0152,0153,0154,0155,0156,0157,0160, + 0161,0162,0136,0314,0315,0316,0317,0320, + 0321,0345,0163,0164,0165,0166,0167,0170, + 0171,0172,0322,0323,0324,0133,0326,0327, + 0330,0331,0332,0333,0334,0335,0336,0337, + 0340,0341,0342,0343,0344,0135,0346,0347, + 0173,0101,0102,0103,0104,0105,0106,0107, + 0110,0111,0350,0351,0352,0353,0354,0355, + 0175,0112,0113,0114,0115,0116,0117,0120, + 0121,0122,0356,0357,0360,0361,0362,0363, + 0134,0237,0123,0124,0125,0126,0127,0130, + 0131,0132,0364,0365,0366,0367,0370,0371, + 0060,0061,0062,0063,0064,0065,0066,0067, + 0070,0071,0372,0373,0374,0375,0376,0377 +}; + + int +main(int argc, char *argv[]) +{ + FILE *fp, *fpo; + int c, e, p = 0, relseek = 1, negseek = 0, revert = 0; + int cols = 0, nonzero = 0, autoskip = 0, hextype = HEX_NORMAL; + int capitalize = 0, decimal_offset = 0; + 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; + unsigned long displayoff = 0; + static char l[LLEN+1]; /* static because it may be too big for stack */ + char *pp; + int addrlen = 9; + +#ifdef AMIGA + /* This program doesn't work when started from the Workbench */ + if (argc == 0) + exit(1); +#endif + + pname = argv[0]; + for (pp = pname; *pp; ) + if (*pp++ == PATH_SEP) + pname = pp; +#ifdef FILE_SEP + for (pp = pname; *pp; pp++) + if (*pp == FILE_SEP) + { + *pp = '\0'; + break; + } +#endif + + while (argc >= 2) + { + 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; + else if (!STRNCMP(pp, "-C", 2)) capitalize = 1; + else if (!STRNCMP(pp, "-d", 2)) decimal_offset = 1; + else if (!STRNCMP(pp, "-r", 2)) revert++; + else if (!STRNCMP(pp, "-E", 2)) ebcdic++; + else if (!STRNCMP(pp, "-v", 2)) + { + fprintf(stderr, "%s%s\n", version, osver); + exit(0); + } + else if (!STRNCMP(pp, "-c", 2)) + { + if (pp[2] && !STRNCMP("apitalize", pp + 2, 9)) + capitalize = 1; + else if (pp[2] && STRNCMP("ols", pp + 2, 3)) + cols = (int)strtol(pp + 2, NULL, 0); + else + { + if (!argv[2]) + exit_with_usage(); + cols = (int)strtol(argv[2], NULL, 0); + argv++; + argc--; + } + } + else if (!STRNCMP(pp, "-g", 2)) + { + if (pp[2] && STRNCMP("roup", pp + 2, 4)) + octspergrp = (int)strtol(pp + 2, NULL, 0); + else + { + if (!argv[2]) + exit_with_usage(); + octspergrp = (int)strtol(argv[2], NULL, 0); + argv++; + argc--; + } + } + else if (!STRNCMP(pp, "-o", 2)) + { + int reloffset = 0; + int negoffset = 0; + if (pp[2] && STRNCMP("ffset", pp + 2, 5)) + displayoff = strtoul(pp + 2, NULL, 0); + else + { + if (!argv[2]) + exit_with_usage(); + + if (argv[2][0] == '+') + reloffset++; + if (argv[2][reloffset] == '-') + negoffset++; + + if (negoffset) + displayoff = ULONG_MAX - strtoul(argv[2] + reloffset+negoffset, NULL, 0) + 1; + else + displayoff = strtoul(argv[2] + reloffset+negoffset, NULL, 0); + + argv++; + argc--; + } + } + else if (!STRNCMP(pp, "-s", 2)) + { + relseek = 0; + negseek = 0; + if (pp[2] && STRNCMP("kip", pp+2, 3) && STRNCMP("eek", pp+2, 3)) + { +#ifdef TRY_SEEK + if (pp[2] == '+') + relseek++; + if (pp[2+relseek] == '-') + negseek++; +#endif + seekoff = strtol(pp + 2+relseek+negseek, (char **)NULL, 0); + } + else + { + if (!argv[2]) + exit_with_usage(); +#ifdef TRY_SEEK + if (argv[2][0] == '+') + relseek++; + if (argv[2][relseek] == '-') + negseek++; +#endif + seekoff = strtol(argv[2] + relseek+negseek, (char **)NULL, 0); + argv++; + argc--; + } + } + else if (!STRNCMP(pp, "-l", 2)) + { + if (pp[2] && STRNCMP("en", pp + 2, 2)) + length = strtol(pp + 2, (char **)NULL, 0); + else + { + if (!argv[2]) + exit_with_usage(); + length = strtol(argv[2], (char **)NULL, 0); + argv++; + argc--; + } + } + else if (!strcmp(pp, "--")) /* end of options */ + { + argv++; + argc--; + break; + } + else if (pp[0] == '-' && pp[1]) /* unknown option */ + exit_with_usage(); + else + break; /* not an option */ + + argv++; /* advance to next argument */ + argc--; + } + + if (!cols) + switch (hextype) + { + case HEX_POSTSCRIPT: cols = 30; break; + case HEX_CINCLUDE: cols = 12; break; + case HEX_BITS: cols = 6; break; + case HEX_NORMAL: + case HEX_LITTLEENDIAN: + default: cols = 16; break; + } + + if (octspergrp < 0) + switch (hextype) + { + 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 || hextype == HEX_LITTLEENDIAN) + && (cols > COLS))) + { + fprintf(stderr, "%s: invalid number of columns (max. %d).\n", pname, COLS); + exit(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(); + + if (argc == 1 || (argv[1][0] == '-' && !argv[1][1])) + BIN_ASSIGN(fp = stdin, !revert); + else + { + if ((fp = fopen(argv[1], BIN_READ(!revert))) == NULL) + { + fprintf(stderr,"%s: ", pname); + perror(argv[1]); + return 2; + } + } + + if (argc < 3 || (argv[2][0] == '-' && !argv[2][1])) + BIN_ASSIGN(fpo = stdout, revert); + else + { + int fd; + int mode = revert ? O_WRONLY : (O_TRUNC|O_WRONLY); + + if (((fd = OPEN(argv[2], mode | BIN_CREAT(revert), 0666)) < 0) || + (fpo = fdopen(fd, BIN_WRITE(revert))) == NULL) + { + fprintf(stderr, "%s: ", pname); + perror(argv[2]); + return 3; + } + rewind(fpo); + } + + if (revert) + { + if (hextype && (hextype != HEX_POSTSCRIPT)) + { + fprintf(stderr, "%s: sorry, cannot revert this type of hexdump\n", pname); + return -1; + } + return huntype(fp, fpo, stderr, cols, hextype, + negseek ? -seekoff : seekoff); + } + + if (seekoff || negseek || !relseek) + { +#ifdef TRY_SEEK + if (relseek) + e = fseek(fp, negseek ? -seekoff : seekoff, 1); + else + e = fseek(fp, negseek ? -seekoff : seekoff, negseek ? 2 : 0); + if (e < 0 && negseek) + { + fprintf(stderr, "%s: sorry cannot seek.\n", pname); + return 4; + } + if (e >= 0) + seekoff = ftell(fp); + else +#endif + { + long s = seekoff; + + while (s--) + if (getc(fp) == EOF) + { + if (ferror(fp)) + { + die(2); + } + else + { + fprintf(stderr, "%s: sorry cannot seek.\n", pname); + return 4; + } + } + } + } + + if (hextype == HEX_CINCLUDE) + { + if (fp != stdin) + { + if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) + die(3); + for (e = 0; (c = argv[1][e]) != 0; e++) + if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) + die(3); + if (fputs("[] = {\n", fpo) == EOF) + die(3); + } + + p = 0; + c = 0; + while ((length < 0 || p < length) && (c = getc(fp)) != EOF) + { + if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", + (p % cols) ? ", " : &",\n "[2*!p], c) < 0) + die(3); + p++; + } + if (c == EOF && ferror(fp)) + die(2); + + if (p && fputs("\n", fpo) == EOF) + die(3); + if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF) + die(3); + + if (fp != stdin) + { + if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) + die(3); + for (e = 0; (c = argv[1][e]) != 0; e++) + if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) + die(3); + if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0) + die(3); + } + + if (fclose(fp)) + die(2); + if (fclose(fpo)) + die(3); + return 0; + } + + if (hextype == HEX_POSTSCRIPT) + { + p = cols; + e = 0; + while ((length < 0 || n < length) && (e = getc(fp)) != EOF) + { + if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF + || putc(hexx[e & 0xf], fpo) == EOF) + die(3); + n++; + if (!--p) + { + if (putc('\n', fpo) == EOF) + die(3); + p = cols; + } + } + if (e == EOF && ferror(fp)) + die(2); + if (p < cols) + if (putc('\n', fpo) == EOF) + die(3); + if (fclose(fp)) + die(2); + if (fclose(fpo)) + die(3); + return 0; + } + + /* hextype: HEX_NORMAL or HEX_BITS or HEX_LITTLEENDIAN */ + + if (hextype != HEX_BITS) + grplen = octspergrp + octspergrp + 1; /* chars per octet group */ + else /* hextype == HEX_BITS */ + grplen = 8 * octspergrp + 1; + + e = 0; + while ((length < 0 || n < length) && (e = getc(fp)) != EOF) + { + if (p == 0) + { + if (decimal_offset) + addrlen = sprintf(l, "%08ld:", + ((unsigned long)(n + seekoff + displayoff))); + else + addrlen = sprintf(l, "%08lx:", + ((unsigned long)(n + seekoff + displayoff))); + for (c = addrlen; c < LLEN; l[c++] = ' '); + } + if (hextype == HEX_NORMAL) + { + l[c = (addrlen + 1 + (grplen * p) / octspergrp)] = hexx[(e >> 4) & 0xf]; + l[++c] = hexx[ e & 0xf]; + } + else if (hextype == HEX_LITTLEENDIAN) + { + int x = p ^ (octspergrp-1); + l[c = (addrlen + 1 + (grplen * x) / octspergrp)] = hexx[(e >> 4) & 0xf]; + l[++c] = hexx[ e & 0xf]; + } + else /* hextype == HEX_BITS */ + { + int i; + + c = (addrlen + 1 + (grplen * p) / octspergrp) - 1; + for (i = 7; i >= 0; i--) + l[++c] = (e & (1 << i)) ? '1' : '0'; + } + if (e) + nonzero++; + if (ebcdic) + e = (e < 64) ? '.' : etoa64[e-64]; + /* When changing this update definition of LLEN above. */ + l[addrlen + 3 + (grplen * cols - 1)/octspergrp + p] = +#ifdef __MVS__ + (e >= 64) +#else + (e > 31 && e < 127) +#endif + ? e : '.'; + n++; + if (++p == cols) + { + l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; + xxdline(fpo, l, autoskip ? nonzero : 1); + nonzero = 0; + p = 0; + } + } + if (e == EOF && ferror(fp)) + die(2); + if (p) + { + l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; + xxdline(fpo, l, 1); + } + else if (autoskip) + xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ + + if (fclose(fp)) + die(2); + if (fclose(fpo)) + die(3); + return 0; +} + +/* vi:set ts=8 sw=4 sts=2 cino+={2 cino+=n-2 : */ diff --git a/app-text/xxd/xxd-8.2.3533.ebuild b/app-text/xxd/xxd-8.2.3533.ebuild new file mode 100644 index 0000000..549bb27 --- /dev/null +++ b/app-text/xxd/xxd-8.2.3533.ebuild @@ -0,0 +1,25 @@ +# Copyright 1999-2021 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +EAPI=7 + +inherit eutils + +DESCRIPTION="Standalone xxd program from vim-8.2.3533" +HOMEPAGE="https://github.com/vim/vim" + +LICENSE="X11-MIT" +SLOT="0" +KEYWORDS="alpha amd64 arm hppa ppc ppc64 sparc x86 amd64-fbsd x86-fbsd" + +src_unpack() { + mkdir -p "${P}" + cp "${FILESDIR}"/Makefile "${P}"/Makefile + cp "${FILESDIR}"/xxd.1 "${P}"/xxd.1 + cp "${FILESDIR}"/xxd.c "${P}"/xxd.c +} + +src_install() { + doman xxd.1 + dobin xxd +} -- 2.11.4.GIT