comparison src/xxd/xxd.c @ 15693:e00983850550 v8.1.0854

patch 8.1.0854: xxd does not work with more than 32 bit addresses commit https://github.com/vim/vim/commit/d8c56a0d29ccb53e12c6dec73b9a0e8a6dad99c2 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jan 30 23:02:25 2019 +0100 patch 8.1.0854: xxd does not work with more than 32 bit addresses Problem: xxd does not work with more than 32 bit addresses. Solution: Add support for 64 bit addresses. (Christer Jensen, closes https://github.com/vim/vim/issues/3791)
author Bram Moolenaar <Bram@vim.org>
date Wed, 30 Jan 2019 23:15:05 +0100
parents 11879b89bb69
children ac05f8fb4200
comparison
equal deleted inserted replaced
15692:62b5f69be0d4 15693:e00983850550
50 * 16.05.00 Added VAXC changes by Stephen P. Wall 50 * 16.05.00 Added VAXC changes by Stephen P. Wall
51 * 16.05.00 Improved MMS file and merge for VMS by Zoltan Arpadffy 51 * 16.05.00 Improved MMS file and merge for VMS by Zoltan Arpadffy
52 * 2011 March Better error handling by Florian Zumbiehl. 52 * 2011 March Better error handling by Florian Zumbiehl.
53 * 2011 April Formatting by Bram Moolenaar 53 * 2011 April Formatting by Bram Moolenaar
54 * 08.06.2013 Little-endian hexdump (-e) and offset (-o) by Vadim Vygonets. 54 * 08.06.2013 Little-endian hexdump (-e) and offset (-o) by Vadim Vygonets.
55 * 11.01.2019 Add full 64/32 bit range to -o and output by Christer Jensen.
55 * 56 *
56 * (c) 1990-1998 by Juergen Weigert (jnweiger@informatik.uni-erlangen.de) 57 * (c) 1990-1998 by Juergen Weigert (jnweiger@informatik.uni-erlangen.de)
57 * 58 *
58 * I hereby grant permission to distribute and use xxd 59 * I hereby grant permission to distribute and use xxd
59 * under X11-MIT or GPL-2.0 (at the user's choice). 60 * under X11-MIT or GPL-2.0 (at the user's choice).
88 # endif 89 # endif
89 #endif 90 #endif
90 #include <stdlib.h> 91 #include <stdlib.h>
91 #include <string.h> /* for strncmp() */ 92 #include <string.h> /* for strncmp() */
92 #include <ctype.h> /* for isalnum() */ 93 #include <ctype.h> /* for isalnum() */
94 #include <limits.h>
93 #if __MWERKS__ && !defined(BEBOX) 95 #if __MWERKS__ && !defined(BEBOX)
94 # include <unix.h> /* for fdopen() on MAC */ 96 # include <unix.h> /* for fdopen() on MAC */
95 #endif 97 #endif
96 98
97 #if defined(__BORLANDC__) && __BORLANDC__ <= 0x0410 && !defined(fileno) 99 #if defined(__BORLANDC__) && __BORLANDC__ <= 0x0410 && !defined(fileno)
202 static int huntype __P((FILE *, FILE *, FILE *, int, int, long)); 204 static int huntype __P((FILE *, FILE *, FILE *, int, int, long));
203 static void xxdline __P((FILE *, char *, int)); 205 static void xxdline __P((FILE *, char *, int));
204 206
205 #define TRY_SEEK /* attempt to use lseek, or skip forward by reading */ 207 #define TRY_SEEK /* attempt to use lseek, or skip forward by reading */
206 #define COLS 256 /* change here, if you ever need more columns */ 208 #define COLS 256 /* change here, if you ever need more columns */
207 #define LLEN (12 + (9*COLS-1) + COLS + 2) 209 #define LLEN ((2*(int)sizeof(unsigned long)) + 4 + (9*COLS-1) + COLS + 2)
208 210
209 char hexxa[] = "0123456789abcdef0123456789ABCDEF", *hexx = hexxa; 211 char hexxa[] = "0123456789abcdef0123456789ABCDEF", *hexx = hexxa;
210 212
211 /* the different hextypes known by this program: */ 213 /* the different hextypes known by this program: */
212 #define HEX_NORMAL 0 214 #define HEX_NORMAL 0
464 int c, e, p = 0, relseek = 1, negseek = 0, revert = 0; 466 int c, e, p = 0, relseek = 1, negseek = 0, revert = 0;
465 int cols = 0, nonzero = 0, autoskip = 0, hextype = HEX_NORMAL, capitalize = 0; 467 int cols = 0, nonzero = 0, autoskip = 0, hextype = HEX_NORMAL, capitalize = 0;
466 int ebcdic = 0; 468 int ebcdic = 0;
467 int octspergrp = -1; /* number of octets grouped in output */ 469 int octspergrp = -1; /* number of octets grouped in output */
468 int grplen; /* total chars per octet group */ 470 int grplen; /* total chars per octet group */
469 long length = -1, n = 0, seekoff = 0, displayoff = 0; 471 long length = -1, n = 0, seekoff = 0;
472 unsigned long displayoff = 0;
470 static char l[LLEN+1]; /* static because it may be too big for stack */ 473 static char l[LLEN+1]; /* static because it may be too big for stack */
471 char *pp; 474 char *pp;
472 475
473 #ifdef AMIGA 476 #ifdef AMIGA
474 /* This program doesn't work when started from the Workbench */ 477 /* This program doesn't work when started from the Workbench */
534 argc--; 537 argc--;
535 } 538 }
536 } 539 }
537 else if (!STRNCMP(pp, "-o", 2)) 540 else if (!STRNCMP(pp, "-o", 2))
538 { 541 {
542 int reloffset = 0;
543 int negoffset = 0;
539 if (pp[2] && STRNCMP("ffset", pp + 2, 5)) 544 if (pp[2] && STRNCMP("ffset", pp + 2, 5))
540 displayoff = (int)strtol(pp + 2, NULL, 0); 545 displayoff = strtoul(pp + 2, NULL, 0);
541 else 546 else
542 { 547 {
543 if (!argv[2]) 548 if (!argv[2])
544 exit_with_usage(); 549 exit_with_usage();
545 displayoff = (int)strtol(argv[2], NULL, 0); 550
551 if (argv[2][0] == '+')
552 reloffset++;
553 if (argv[2][reloffset] == '-')
554 negoffset++;
555
556 if (negoffset)
557 displayoff = ULONG_MAX - strtoul(argv[2] + reloffset+negoffset, NULL, 0) + 1;
558 else
559 displayoff = strtoul(argv[2] + reloffset+negoffset, NULL, 0);
560
546 argv++; 561 argv++;
547 argc--; 562 argc--;
548 } 563 }
549 } 564 }
550 else if (!STRNCMP(pp, "-s", 2)) 565 else if (!STRNCMP(pp, "-s", 2))
803 if (hextype != HEX_BITS) 818 if (hextype != HEX_BITS)
804 grplen = octspergrp + octspergrp + 1; /* chars per octet group */ 819 grplen = octspergrp + octspergrp + 1; /* chars per octet group */
805 else /* hextype == HEX_BITS */ 820 else /* hextype == HEX_BITS */
806 grplen = 8 * octspergrp + 1; 821 grplen = 8 * octspergrp + 1;
807 822
823 int addrlen = 9;
808 e = 0; 824 e = 0;
809 while ((length < 0 || n < length) && (e = getc(fp)) != EOF) 825 while ((length < 0 || n < length) && (e = getc(fp)) != EOF)
810 { 826 {
811 if (p == 0) 827 if (p == 0)
812 { 828 {
813 sprintf(l, "%08lx:", 829 addrlen = sprintf(l, "%08lx:",
814 ((unsigned long)(n + seekoff + displayoff)) & 0xffffffff); 830 ((unsigned long)(n + seekoff + displayoff)));
815 for (c = 9; c < LLEN; l[c++] = ' '); 831 for (c = addrlen; c < LLEN; l[c++] = ' ');
816 } 832 }
817 if (hextype == HEX_NORMAL) 833 if (hextype == HEX_NORMAL)
818 { 834 {
819 l[c = (10 + (grplen * p) / octspergrp)] = hexx[(e >> 4) & 0xf]; 835 l[c = (addrlen + 1 + (grplen * p) / octspergrp)] = hexx[(e >> 4) & 0xf];
820 l[++c] = hexx[ e & 0xf]; 836 l[++c] = hexx[ e & 0xf];
821 } 837 }
822 else if (hextype == HEX_LITTLEENDIAN) 838 else if (hextype == HEX_LITTLEENDIAN)
823 { 839 {
824 int x = p ^ (octspergrp-1); 840 int x = p ^ (octspergrp-1);
825 l[c = (10 + (grplen * x) / octspergrp)] = hexx[(e >> 4) & 0xf]; 841 l[c = (addrlen + 1 + (grplen * x) / octspergrp)] = hexx[(e >> 4) & 0xf];
826 l[++c] = hexx[ e & 0xf]; 842 l[++c] = hexx[ e & 0xf];
827 } 843 }
828 else /* hextype == HEX_BITS */ 844 else /* hextype == HEX_BITS */
829 { 845 {
830 int i; 846 int i;
831 847
832 c = (10 + (grplen * p) / octspergrp) - 1; 848 c = (addrlen + 1 + (grplen * p) / octspergrp) - 1;
833 for (i = 7; i >= 0; i--) 849 for (i = 7; i >= 0; i--)
834 l[++c] = (e & (1 << i)) ? '1' : '0'; 850 l[++c] = (e & (1 << i)) ? '1' : '0';
835 } 851 }
836 if (e) 852 if (e)
837 nonzero++; 853 nonzero++;
838 if (ebcdic) 854 if (ebcdic)
839 e = (e < 64) ? '.' : etoa64[e-64]; 855 e = (e < 64) ? '.' : etoa64[e-64];
840 /* When changing this update definition of LLEN above. */ 856 /* When changing this update definition of LLEN above. */
841 l[12 + (grplen * cols - 1)/octspergrp + p] = 857 l[addrlen + 3 + (grplen * cols - 1)/octspergrp + p] =
842 #ifdef __MVS__ 858 #ifdef __MVS__
843 (e >= 64) 859 (e >= 64)
844 #else 860 #else
845 (e > 31 && e < 127) 861 (e > 31 && e < 127)
846 #endif 862 #endif
847 ? e : '.'; 863 ? e : '.';
848 n++; 864 n++;
849 if (++p == cols) 865 if (++p == cols)
850 { 866 {
851 l[c = (12 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; 867 l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0';
852 xxdline(fpo, l, autoskip ? nonzero : 1); 868 xxdline(fpo, l, autoskip ? nonzero : 1);
853 nonzero = 0; 869 nonzero = 0;
854 p = 0; 870 p = 0;
855 } 871 }
856 } 872 }
857 if (e == EOF && ferror(fp)) 873 if (e == EOF && ferror(fp))
858 die(2); 874 die(2);
859 if (p) 875 if (p)
860 { 876 {
861 l[c = (12 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; 877 l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0';
862 xxdline(fpo, l, 1); 878 xxdline(fpo, l, 1);
863 } 879 }
864 else if (autoskip) 880 else if (autoskip)
865 xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ 881 xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */
866 882