Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /* fD note: This source file comes from the VIM source package. All credits go to the original authors */ |
| 2 | |
| 3 | /* xxd: my hexdump facility. jw |
| 4 | * |
| 5 | * 2.10.90 changed to word output |
| 6 | * 3.03.93 new indent style, dumb bug inserted and fixed. |
| 7 | * -c option, mls |
| 8 | * 26.04.94 better option parser, -ps, -l, -s added. |
| 9 | * 1.07.94 -r badly needs - as input file. Per default autoskip over |
| 10 | * consecutive lines of zeroes, as unix od does. |
| 11 | * -a shows them too. |
| 12 | * -i dump as c-style #include "file.h" |
| 13 | * 1.11.95 if "xxd -i" knows the filename, an 'unsigned char filename_bits[]' |
| 14 | * array is written in correct c-syntax. |
| 15 | * -s improved, now defaults to absolute seek, relative requires a '+'. |
| 16 | * -r improved, now -r -s -0x... is supported. |
| 17 | * change/suppress leading '\0' bytes. |
| 18 | * -l n improved: stops exactly after n bytes. |
| 19 | * -r improved, better handling of partial lines with trailing garbage. |
| 20 | * -r improved, now -r -p works again! |
| 21 | * -r improved, less flushing, much faster now! (that was silly) |
| 22 | * 3.04.96 Per repeated request of a single person: autoskip defaults to off. |
| 23 | * 15.05.96 -v added. They want to know the version. |
| 24 | * -a fixed, to show last line inf file ends in all zeros. |
| 25 | * -u added: Print upper case hex-letters, as preferred by unix bc. |
| 26 | * -h added to usage message. Usage message extended. |
| 27 | * Now using outfile if specified even in normal mode, aehem. |
| 28 | * No longer mixing of ints and longs. May help doze people. |
| 29 | * Added binify ioctl for same reason. (Enough Doze stress for 1996!) |
| 30 | * 16.05.96 -p improved, removed occasional superfluous linefeed. |
| 31 | * 20.05.96 -l 0 fixed. tried to read anyway. |
| 32 | * 21.05.96 -i fixed. now honours -u, and prepends __ to numeric filenames. |
| 33 | * compile -DWIN32 for NT or W95. George V. Reilly, * -v improved :-) |
| 34 | * support --gnuish-longhorn-options |
| 35 | * 25.05.96 MAC support added: CodeWarrior already uses ``outline'' in Types.h |
| 36 | * which is included by MacHeaders (Axel Kielhorn). Renamed to |
| 37 | * xxdline(). |
| 38 | * 7.06.96 -i printed 'int' instead of 'char'. *blush* |
| 39 | * added Bram's OS2 ifdefs... |
| 40 | * 18.07.96 gcc -Wall @ SunOS4 is now slient. |
| 41 | * Added osver for MSDOS/DJGPP/WIN32. |
| 42 | * 29.08.96 Added size_t to strncmp() for Amiga. |
| 43 | * 24.03.97 Windows NT support (Phil Hanna). Clean exit for Amiga WB (Bram) |
| 44 | * 02.04.97 Added -E option, to have EBCDIC translation instead of ASCII |
| 45 | * (azc10@yahoo.com) |
| 46 | * 22.05.97 added -g (group octets) option (jcook@namerica.kla.com). |
| 47 | * 23.09.98 nasty -p -r misfeature fixed: slightly wrong output, when -c was |
| 48 | * missing or wrong. |
| 49 | * 26.09.98 Fixed: 'xxd -i infile outfile' did not truncate outfile. |
| 50 | * 27.10.98 Fixed: -g option parser required blank. |
| 51 | * option -b added: 01000101 binary output in normal format. |
| 52 | * 16.05.00 Added VAXC changes by Stephen P. Wall |
| 53 | * 16.05.00 Improved MMS file and merge for VMS by Zoltan Arpadffy |
| 54 | * |
| 55 | * (c) 1990-1998 by Juergen Weigert (jnweiger@informatik.uni-erlangen.de) |
| 56 | * |
| 57 | * Small changes made afterwards by Bram Moolenaar et al. |
| 58 | * |
| 59 | * Distribute freely and credit me, |
| 60 | * make money and share with me, |
| 61 | * lose money and don't ask me. |
| 62 | */ |
| 63 | |
| 64 | /* Visual Studio 2005 has 'deprecated' many of the standard CRT functions */ |
| 65 | #if _MSC_VER >= 1400 |
| 66 | # define _CRT_SECURE_NO_DEPRECATE |
| 67 | # define _CRT_NONSTDC_NO_DEPRECATE |
| 68 | #endif |
| 69 | #if !defined(CYGWIN) && (defined(CYGWIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)) |
| 70 | # define CYGWIN |
| 71 | #endif |
| 72 | |
| 73 | #include <stdio.h> |
| 74 | #ifdef VAXC |
| 75 | # include <file.h> |
| 76 | #else |
| 77 | # include <fcntl.h> |
| 78 | #endif |
| 79 | #ifdef __TSC__ |
| 80 | # define MSDOS |
| 81 | #endif |
| 82 | #if !defined(OS2) && defined(__EMX__) |
| 83 | # define OS2 |
| 84 | #endif |
| 85 | #if defined(MSDOS) || defined(WIN32) || defined(OS2) || defined(__BORLANDC__) \ |
| 86 | || defined(CYGWIN) |
| 87 | # include <io.h> /* for setmode() */ |
| 88 | #else |
| 89 | # ifdef UNIX |
| 90 | # include <unistd.h> |
| 91 | # endif |
| 92 | #endif |
| 93 | #include <stdlib.h> |
| 94 | #include <string.h> /* for strncmp() */ |
| 95 | #include <ctype.h> /* for isalnum() */ |
| 96 | #if __MWERKS__ && !defined(BEBOX) |
| 97 | # include <unix.h> /* for fdopen() on MAC */ |
| 98 | #endif |
| 99 | |
| 100 | #if defined(__BORLANDC__) && __BORLANDC__ <= 0x0410 && !defined(fileno) |
| 101 | /* Missing define and prototype grabbed from the BC 4.0 <stdio.h> */ |
| 102 | # define fileno(f) ((f)->fd) |
| 103 | FILE _FAR *_Cdecl _FARFUNC fdopen(int __handle, char _FAR *__type); |
| 104 | #endif |
| 105 | |
| 106 | |
| 107 | /* This corrects the problem of missing prototypes for certain functions |
| 108 | * in some GNU installations (e.g. SunOS 4.1.x). |
| 109 | * Darren Hiebert <darren@hmi.com> (sparc-sun-sunos4.1.3_U1/2.7.2.2) |
| 110 | */ |
| 111 | #if defined(__GNUC__) && defined(__STDC__) |
| 112 | # ifndef __USE_FIXED_PROTOTYPES__ |
| 113 | # define __USE_FIXED_PROTOTYPES__ |
| 114 | # endif |
| 115 | #endif |
| 116 | |
| 117 | #ifndef __USE_FIXED_PROTOTYPES__ |
| 118 | /* |
| 119 | * This is historic and works only if the compiler really has no prototypes: |
| 120 | * |
| 121 | * Include prototypes for Sun OS 4.x, when using an ANSI compiler. |
| 122 | * FILE is defined on OS 4.x, not on 5.x (Solaris). |
| 123 | * if __SVR4 is defined (some Solaris versions), don't include this. |
| 124 | */ |
| 125 | #if defined(sun) && defined(FILE) && !defined(__SVR4) && defined(__STDC__) |
| 126 | # define __P(a) a |
| 127 | /* excerpt from my sun_stdlib.h */ |
| 128 | extern int fprintf __P((FILE *, char *, ...)); |
| 129 | extern int fputs __P((char *, FILE *)); |
| 130 | extern int _flsbuf __P((unsigned char, FILE *)); |
| 131 | extern int _filbuf __P((FILE *)); |
| 132 | extern int fflush __P((FILE *)); |
| 133 | extern int fclose __P((FILE *)); |
| 134 | extern int fseek __P((FILE *, long, int)); |
| 135 | extern int rewind __P((FILE *)); |
| 136 | |
| 137 | extern void perror __P((char *)); |
| 138 | # endif |
| 139 | #endif |
| 140 | |
| 141 | extern long int strtol(); |
| 142 | extern long int ftell(); |
| 143 | |
| 144 | char version[] = "xxd V1.10 27oct98 by Juergen Weigert"; |
| 145 | #ifdef WIN32 |
| 146 | char osver[] = " (Win32)"; |
| 147 | #else |
| 148 | # ifdef DJGPP |
| 149 | char osver[] = " (dos 32 bit)"; |
| 150 | # else |
| 151 | # ifdef MSDOS |
| 152 | char osver[] = " (dos 16 bit)"; |
| 153 | # else |
| 154 | char osver[] = ""; |
| 155 | # endif |
| 156 | # endif |
| 157 | #endif |
| 158 | |
| 159 | #if defined(MSDOS) || defined(WIN32) || defined(OS2) |
| 160 | # define BIN_READ(yes) ((yes) ? "rb" : "rt") |
| 161 | # define BIN_WRITE(yes) ((yes) ? "wb" : "wt") |
| 162 | # define BIN_CREAT(yes) ((yes) ? (O_CREAT|O_BINARY) : O_CREAT) |
| 163 | # define BIN_ASSIGN(fp, yes) setmode(fileno(fp), (yes) ? O_BINARY : O_TEXT) |
| 164 | # define PATH_SEP '\\' |
| 165 | #elif defined(CYGWIN) |
| 166 | # define BIN_READ(yes) ((yes) ? "rb" : "rt") |
| 167 | # define BIN_WRITE(yes) ((yes) ? "wb" : "w") |
| 168 | # define BIN_CREAT(yes) ((yes) ? (O_CREAT|O_BINARY) : O_CREAT) |
| 169 | # define BIN_ASSIGN(fp, yes) ((yes) ? (void) setmode(fileno(fp), O_BINARY) : (void) (fp)) |
| 170 | # define PATH_SEP '/' |
| 171 | #else |
| 172 | # ifdef VMS |
| 173 | # define BIN_READ(dummy) "r" |
| 174 | # define BIN_WRITE(dummy) "w" |
| 175 | # define BIN_CREAT(dummy) O_CREAT |
| 176 | # define BIN_ASSIGN(fp, dummy) fp |
| 177 | # define PATH_SEP ']' |
| 178 | # define FILE_SEP '.' |
| 179 | # else |
| 180 | # define BIN_READ(dummy) "r" |
| 181 | # define BIN_WRITE(dummy) "w" |
| 182 | # define BIN_CREAT(dummy) O_CREAT |
| 183 | # define BIN_ASSIGN(fp, dummy) fp |
| 184 | # define PATH_SEP '/' |
| 185 | # endif |
| 186 | #endif |
| 187 | |
| 188 | /* open has only to arguments on the Mac */ |
| 189 | #if __MWERKS__ |
| 190 | # define OPEN(name, mode, umask) open(name, mode) |
| 191 | #else |
| 192 | # define OPEN(name, mode, umask) open(name, mode, umask) |
| 193 | #endif |
| 194 | |
| 195 | #ifdef AMIGA |
| 196 | # define STRNCMP(s1, s2, l) strncmp(s1, s2, (size_t)l) |
| 197 | #else |
| 198 | # define STRNCMP(s1, s2, l) strncmp(s1, s2, l) |
| 199 | #endif |
| 200 | |
| 201 | #ifndef __P |
| 202 | # if defined(__STDC__) || defined(MSDOS) || defined(WIN32) || defined(OS2) \ |
| 203 | || defined(__BORLANDC__) |
| 204 | # define __P(a) a |
| 205 | # else |
| 206 | # define __P(a) () |
| 207 | # endif |
| 208 | #endif |
| 209 | |
| 210 | /* Let's collect some prototypes */ |
| 211 | /* CodeWarrior is really picky about missing prototypes */ |
| 212 | static void exit_with_usage __P((char *)); |
| 213 | static int huntype __P((FILE *, FILE *, FILE *, char *, int, int, long)); |
| 214 | static void xxdline __P((FILE *, char *, int)); |
| 215 | |
| 216 | #define TRY_SEEK /* attempt to use lseek, or skip forward by reading */ |
| 217 | #define COLS 256 /* change here, if you ever need more columns */ |
| 218 | #define LLEN (11 + (9*COLS-1)/1 + COLS + 2) |
| 219 | |
| 220 | char hexxa[] = "0123456789abcdef0123456789ABCDEF", *hexx = hexxa; |
| 221 | |
| 222 | /* the different hextypes known by this program: */ |
| 223 | #define HEX_NORMAL 0 |
| 224 | #define HEX_POSTSCRIPT 1 |
| 225 | #define HEX_CINCLUDE 2 |
| 226 | #define HEX_BITS 3 /* not hex a dump, but bits: 01111001 */ |
| 227 | |
| 228 | static void |
| 229 | exit_with_usage(pname) |
| 230 | char *pname; |
| 231 | { |
| 232 | fprintf(stderr, "Usage:\n %s [options] [infile [outfile]]\n", pname); |
| 233 | fprintf(stderr, " or\n %s -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]\n", pname); |
| 234 | fprintf(stderr, "Options:\n"); |
| 235 | fprintf(stderr, " -a toggle autoskip: A single '*' replaces nul-lines. Default off.\n"); |
| 236 | fprintf(stderr, " -b binary digit dump (incompatible with -ps,-i,-r). Default hex.\n"); |
| 237 | fprintf(stderr, " -c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30).\n"); |
| 238 | fprintf(stderr, " -E show characters in EBCDIC. Default ASCII.\n"); |
| 239 | fprintf(stderr, " -g number of octets per group in normal output. Default 2.\n"); |
| 240 | fprintf(stderr, " -h print this summary.\n"); |
| 241 | fprintf(stderr, " -i output in C include file style.\n"); |
| 242 | fprintf(stderr, " -l len stop after <len> octets.\n"); |
| 243 | fprintf(stderr, " -ps output in postscript plain hexdump style.\n"); |
| 244 | fprintf(stderr, " -r reverse operation: convert (or patch) hexdump into binary.\n"); |
| 245 | fprintf(stderr, " -r -s off revert with <off> added to file positions found in hexdump.\n"); |
| 246 | fprintf(stderr, " -s %sseek start at <seek> bytes abs. %sinfile offset.\n", |
| 247 | #ifdef TRY_SEEK |
| 248 | "[+][-]", "(or +: rel.) "); |
| 249 | #else |
| 250 | "", ""); |
| 251 | #endif |
| 252 | fprintf(stderr, " -u use upper case hex letters.\n"); |
| 253 | fprintf(stderr, " -v show version: \"%s%s\".\n", version, osver); |
| 254 | exit(1); |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Max. cols binary characters are decoded from the input stream per line. |
| 259 | * Two adjacent garbage characters after evaluated data delimit valid data. |
| 260 | * Everything up to the next newline is discarded. |
| 261 | * |
| 262 | * The name is historic and came from 'undo type opt h'. |
| 263 | */ |
| 264 | static int |
| 265 | huntype(fpi, fpo, fperr, pname, cols, hextype, base_off) |
| 266 | FILE *fpi, *fpo, *fperr; |
| 267 | char *pname; |
| 268 | int cols, hextype; |
| 269 | long base_off; |
| 270 | { |
| 271 | int c, ign_garb = 1, n1 = -1, n2 = 0, n3, p = cols; |
| 272 | long have_off = 0, want_off = 0; |
| 273 | |
| 274 | rewind(fpi); |
| 275 | |
| 276 | while ((c = getc(fpi)) != EOF) |
| 277 | { |
| 278 | if (c == '\r') /* Doze style input file? */ |
| 279 | continue; |
| 280 | |
| 281 | /* Allow multiple spaces. This doesn't work when there is normal text |
| 282 | * after the hex codes in the last line that looks like hex, thus only |
| 283 | * use it for PostScript format. */ |
| 284 | if (hextype == HEX_POSTSCRIPT && (c == ' ' || c == '\n' || c == '\t')) |
| 285 | continue; |
| 286 | |
| 287 | n3 = n2; |
| 288 | n2 = n1; |
| 289 | |
| 290 | if (c >= '0' && c <= '9') |
| 291 | n1 = c - '0'; |
| 292 | else if (c >= 'a' && c <= 'f') |
| 293 | n1 = c - 'a' + 10; |
| 294 | else if (c >= 'A' && c <= 'F') |
| 295 | n1 = c - 'A' + 10; |
| 296 | else |
| 297 | { |
| 298 | n1 = -1; |
| 299 | if (ign_garb) |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | ign_garb = 0; |
| 304 | |
| 305 | if (p >= cols) |
| 306 | { |
| 307 | if (!hextype) |
| 308 | { |
| 309 | if (n1 < 0) |
| 310 | { |
| 311 | p = 0; |
| 312 | continue; |
| 313 | } |
| 314 | want_off = (want_off << 4) | n1; |
| 315 | continue; |
| 316 | } |
| 317 | else |
| 318 | p = 0; |
| 319 | } |
| 320 | |
| 321 | if (base_off + want_off != have_off) |
| 322 | { |
| 323 | fflush(fpo); |
| 324 | #ifdef TRY_SEEK |
| 325 | c = fseek(fpo, base_off + want_off - have_off, 1); |
| 326 | if (c >= 0) |
| 327 | have_off = base_off + want_off; |
| 328 | #endif |
| 329 | if (base_off + want_off < have_off) |
| 330 | { |
| 331 | fprintf(fperr, "%s: sorry, cannot seek backwards.\n", pname); |
| 332 | return 5; |
| 333 | } |
| 334 | for (; have_off < base_off + want_off; have_off++) |
| 335 | putc(0, fpo); |
| 336 | } |
| 337 | |
| 338 | if (n2 >= 0 && n1 >= 0) |
| 339 | { |
| 340 | putc((n2 << 4) | n1, fpo); |
| 341 | have_off++; |
| 342 | want_off++; |
| 343 | n1 = -1; |
| 344 | if ((++p >= cols) && !hextype) |
| 345 | { |
| 346 | /* skip rest of line as garbage */ |
| 347 | want_off = 0; |
| 348 | while ((c = getc(fpi)) != '\n' && c != EOF) |
| 349 | ; |
| 350 | ign_garb = 1; |
| 351 | } |
| 352 | } |
| 353 | else if (n1 < 0 && n2 < 0 && n3 < 0) |
| 354 | { |
| 355 | /* already stumbled into garbage, skip line, wait and see */ |
| 356 | if (!hextype) |
| 357 | want_off = 0; |
| 358 | while ((c = getc(fpi)) != '\n' && c != EOF) |
| 359 | ; |
| 360 | ign_garb = 1; |
| 361 | } |
| 362 | } |
| 363 | fflush(fpo); |
| 364 | #ifdef TRY_SEEK |
| 365 | fseek(fpo, 0L, 2); |
| 366 | #endif |
| 367 | fclose(fpo); |
| 368 | fclose(fpi); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Print line l. If nz is false, xxdline regards the line a line of |
| 374 | * zeroes. If there are three or more consecutive lines of zeroes, |
| 375 | * they are replaced by a single '*' character. |
| 376 | * |
| 377 | * If the output ends with more than two lines of zeroes, you |
| 378 | * should call xxdline again with l being the last line and nz |
| 379 | * negative. This ensures that the last line is shown even when |
| 380 | * it is all zeroes. |
| 381 | * |
| 382 | * If nz is always positive, lines are never suppressed. |
| 383 | */ |
| 384 | static void |
| 385 | xxdline(fp, l, nz) |
| 386 | FILE *fp; |
| 387 | char *l; |
| 388 | int nz; |
| 389 | { |
| 390 | static char z[LLEN+1]; |
| 391 | static int zero_seen = 0; |
| 392 | |
| 393 | if (!nz && zero_seen == 1) |
| 394 | strcpy(z, l); |
| 395 | |
| 396 | if (nz || !zero_seen++) |
| 397 | { |
| 398 | if (nz) |
| 399 | { |
| 400 | if (nz < 0) |
| 401 | zero_seen--; |
| 402 | if (zero_seen == 2) |
| 403 | fputs(z, fp); |
| 404 | if (zero_seen > 2) |
| 405 | fputs("*\n", fp); |
| 406 | } |
| 407 | if (nz >= 0 || zero_seen > 0) |
| 408 | fputs(l, fp); |
| 409 | if (nz) |
| 410 | zero_seen = 0; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* This is an EBCDIC to ASCII conversion table */ |
| 415 | /* from a proposed BTL standard April 16, 1979 */ |
| 416 | static unsigned char etoa64[] = |
| 417 | { |
| 418 | 0040,0240,0241,0242,0243,0244,0245,0246, |
| 419 | 0247,0250,0325,0056,0074,0050,0053,0174, |
| 420 | 0046,0251,0252,0253,0254,0255,0256,0257, |
| 421 | 0260,0261,0041,0044,0052,0051,0073,0176, |
| 422 | 0055,0057,0262,0263,0264,0265,0266,0267, |
| 423 | 0270,0271,0313,0054,0045,0137,0076,0077, |
| 424 | 0272,0273,0274,0275,0276,0277,0300,0301, |
| 425 | 0302,0140,0072,0043,0100,0047,0075,0042, |
| 426 | 0303,0141,0142,0143,0144,0145,0146,0147, |
| 427 | 0150,0151,0304,0305,0306,0307,0310,0311, |
| 428 | 0312,0152,0153,0154,0155,0156,0157,0160, |
| 429 | 0161,0162,0136,0314,0315,0316,0317,0320, |
| 430 | 0321,0345,0163,0164,0165,0166,0167,0170, |
| 431 | 0171,0172,0322,0323,0324,0133,0326,0327, |
| 432 | 0330,0331,0332,0333,0334,0335,0336,0337, |
| 433 | 0340,0341,0342,0343,0344,0135,0346,0347, |
| 434 | 0173,0101,0102,0103,0104,0105,0106,0107, |
| 435 | 0110,0111,0350,0351,0352,0353,0354,0355, |
| 436 | 0175,0112,0113,0114,0115,0116,0117,0120, |
| 437 | 0121,0122,0356,0357,0360,0361,0362,0363, |
| 438 | 0134,0237,0123,0124,0125,0126,0127,0130, |
| 439 | 0131,0132,0364,0365,0366,0367,0370,0371, |
| 440 | 0060,0061,0062,0063,0064,0065,0066,0067, |
| 441 | 0070,0071,0372,0373,0374,0375,0376,0377 |
| 442 | }; |
| 443 | |
| 444 | int |
| 445 | main(argc, argv) |
| 446 | int argc; |
| 447 | char *argv[]; |
| 448 | { |
| 449 | FILE *fp, *fpo; |
| 450 | int c, e, p = 0, relseek = 1, negseek = 0, revert = 0; |
| 451 | int cols = 0, nonzero = 0, autoskip = 0, hextype = HEX_NORMAL; |
| 452 | int ebcdic = 0; |
| 453 | int octspergrp = -1; /* number of octets grouped in output */ |
| 454 | int grplen; /* total chars per octet group */ |
| 455 | long length = -1, n = 0, seekoff = 0; |
| 456 | char l[LLEN+1]; |
| 457 | char *pname, *pp; |
| 458 | |
| 459 | #ifdef AMIGA |
| 460 | /* This program doesn't work when started from the Workbench */ |
| 461 | if (argc == 0) |
| 462 | exit(1); |
| 463 | #endif |
| 464 | |
| 465 | pname = argv[0]; |
| 466 | for (pp = pname; *pp; ) |
| 467 | if (*pp++ == PATH_SEP) |
| 468 | pname = pp; |
| 469 | #ifdef FILE_SEP |
| 470 | for (pp = pname; *pp; pp++) |
| 471 | if (*pp == FILE_SEP) |
| 472 | { |
| 473 | *pp = '\0'; |
| 474 | break; |
| 475 | } |
| 476 | #endif |
| 477 | |
| 478 | while (argc >= 2) |
| 479 | { |
| 480 | pp = argv[1] + (!STRNCMP(argv[1], "--", 2) && argv[1][2]); |
| 481 | if (!STRNCMP(pp, "-a", 2)) autoskip = 1 - autoskip; |
| 482 | else if (!STRNCMP(pp, "-b", 2)) hextype = HEX_BITS; |
| 483 | else if (!STRNCMP(pp, "-u", 2)) hexx = hexxa + 16; |
| 484 | else if (!STRNCMP(pp, "-p", 2)) hextype = HEX_POSTSCRIPT; |
| 485 | else if (!STRNCMP(pp, "-i", 2)) hextype = HEX_CINCLUDE; |
| 486 | else if (!STRNCMP(pp, "-r", 2)) revert++; |
| 487 | else if (!STRNCMP(pp, "-E", 2)) ebcdic++; |
| 488 | else if (!STRNCMP(pp, "-v", 2)) |
| 489 | { |
| 490 | fprintf(stderr, "%s%s\n", version, osver); |
| 491 | exit(0); |
| 492 | } |
| 493 | else if (!STRNCMP(pp, "-c", 2)) |
| 494 | { |
| 495 | if (pp[2] && STRNCMP("ols", pp + 2, 3)) |
| 496 | cols = (int)strtol(pp + 2, NULL, 0); |
| 497 | else |
| 498 | { |
| 499 | if (!argv[2]) |
| 500 | exit_with_usage(pname); |
| 501 | cols = (int)strtol(argv[2], NULL, 0); |
| 502 | argv++; |
| 503 | argc--; |
| 504 | } |
| 505 | } |
| 506 | else if (!STRNCMP(pp, "-g", 2)) |
| 507 | { |
| 508 | if (pp[2] && STRNCMP("group", pp + 2, 5)) |
| 509 | octspergrp = (int)strtol(pp + 2, NULL, 0); |
| 510 | else |
| 511 | { |
| 512 | if (!argv[2]) |
| 513 | exit_with_usage(pname); |
| 514 | octspergrp = (int)strtol(argv[2], NULL, 0); |
| 515 | argv++; |
| 516 | argc--; |
| 517 | } |
| 518 | } |
| 519 | else if (!STRNCMP(pp, "-s", 2)) |
| 520 | { |
| 521 | relseek = 0; |
| 522 | negseek = 0; |
| 523 | if (pp[2] && STRNCMP("kip", pp+2, 3) && STRNCMP("eek", pp+2, 3)) |
| 524 | { |
| 525 | #ifdef TRY_SEEK |
| 526 | if (pp[2] == '+') |
| 527 | relseek++; |
| 528 | if (pp[2+relseek] == '-') |
| 529 | negseek++; |
| 530 | #endif |
| 531 | seekoff = strtol(pp + 2+relseek+negseek, (char **)NULL, 0); |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | if (!argv[2]) |
| 536 | exit_with_usage(pname); |
| 537 | #ifdef TRY_SEEK |
| 538 | if (argv[2][0] == '+') |
| 539 | relseek++; |
| 540 | if (argv[2][relseek] == '-') |
| 541 | negseek++; |
| 542 | #endif |
| 543 | seekoff = strtol(argv[2] + relseek+negseek, (char **)NULL, 0); |
| 544 | argv++; |
| 545 | argc--; |
| 546 | } |
| 547 | } |
| 548 | else if (!STRNCMP(pp, "-l", 2)) |
| 549 | { |
| 550 | if (pp[2] && STRNCMP("en", pp + 2, 2)) |
| 551 | length = strtol(pp + 2, (char **)NULL, 0); |
| 552 | else |
| 553 | { |
| 554 | if (!argv[2]) |
| 555 | exit_with_usage(pname); |
| 556 | length = strtol(argv[2], (char **)NULL, 0); |
| 557 | argv++; |
| 558 | argc--; |
| 559 | } |
| 560 | } |
| 561 | else if (!strcmp(pp, "--")) /* end of options */ |
| 562 | { |
| 563 | argv++; |
| 564 | argc--; |
| 565 | break; |
| 566 | } |
| 567 | else if (pp[0] == '-' && pp[1]) /* unknown option */ |
| 568 | exit_with_usage(pname); |
| 569 | else |
| 570 | break; /* not an option */ |
| 571 | |
| 572 | argv++; /* advance to next argument */ |
| 573 | argc--; |
| 574 | } |
| 575 | |
| 576 | if (!cols) |
| 577 | switch (hextype) |
| 578 | { |
| 579 | case HEX_POSTSCRIPT: cols = 30; break; |
| 580 | case HEX_CINCLUDE: cols = 12; break; |
| 581 | case HEX_BITS: cols = 6; break; |
| 582 | case HEX_NORMAL: |
| 583 | default: cols = 16; break; |
| 584 | } |
| 585 | |
| 586 | if (octspergrp < 0) |
| 587 | switch (hextype) |
| 588 | { |
| 589 | case HEX_BITS: octspergrp = 1; break; |
| 590 | case HEX_NORMAL: octspergrp = 2; break; |
| 591 | case HEX_POSTSCRIPT: |
| 592 | case HEX_CINCLUDE: |
| 593 | default: octspergrp = 0; break; |
| 594 | } |
| 595 | |
| 596 | if (cols < 1 || ((hextype == HEX_NORMAL || hextype == HEX_BITS) |
| 597 | && (cols > COLS))) |
| 598 | { |
| 599 | fprintf(stderr, "%s: invalid number of columns (max. %d).\n", pname, COLS); |
| 600 | exit(1); |
| 601 | } |
| 602 | |
| 603 | if (octspergrp < 1) |
| 604 | octspergrp = cols; |
| 605 | |
| 606 | if (argc > 3) |
| 607 | exit_with_usage(pname); |
| 608 | |
| 609 | if (argc == 1 || (argv[1][0] == '-' && !argv[1][1])) |
| 610 | BIN_ASSIGN(fp = stdin, !revert); |
| 611 | else |
| 612 | { |
| 613 | if ((fp = fopen(argv[1], BIN_READ(!revert))) == NULL) |
| 614 | { |
| 615 | fprintf(stderr,"%s: ", pname); |
| 616 | perror(argv[1]); |
| 617 | return 2; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (argc < 3 || (argv[2][0] == '-' && !argv[2][1])) |
| 622 | BIN_ASSIGN(fpo = stdout, revert); |
| 623 | else |
| 624 | { |
| 625 | int fd; |
| 626 | int mode = revert ? O_WRONLY : (O_TRUNC|O_WRONLY); |
| 627 | |
| 628 | if (((fd = OPEN(argv[2], mode | BIN_CREAT(revert), 0666)) < 0) || |
| 629 | (fpo = fdopen(fd, BIN_WRITE(revert))) == NULL) |
| 630 | { |
| 631 | fprintf(stderr, "%s: ", pname); |
| 632 | perror(argv[2]); |
| 633 | return 3; |
| 634 | } |
| 635 | rewind(fpo); |
| 636 | } |
| 637 | |
| 638 | if (revert) |
| 639 | { |
| 640 | if (hextype && (hextype != HEX_POSTSCRIPT)) |
| 641 | { |
| 642 | fprintf(stderr, "%s: sorry, cannot revert this type of hexdump\n", pname); |
| 643 | return -1; |
| 644 | } |
| 645 | return huntype(fp, fpo, stderr, pname, cols, hextype, |
| 646 | negseek ? -seekoff : seekoff); |
| 647 | } |
| 648 | |
| 649 | if (seekoff || negseek || !relseek) |
| 650 | { |
| 651 | #ifdef TRY_SEEK |
| 652 | if (relseek) |
| 653 | e = fseek(fp, negseek ? -seekoff : seekoff, 1); |
| 654 | else |
| 655 | e = fseek(fp, negseek ? -seekoff : seekoff, negseek ? 2 : 0); |
| 656 | if (e < 0 && negseek) |
| 657 | { |
| 658 | fprintf(stderr, "%s: sorry cannot seek.\n", pname); |
| 659 | return 4; |
| 660 | } |
| 661 | if (e >= 0) |
| 662 | seekoff = ftell(fp); |
| 663 | else |
| 664 | #endif |
| 665 | { |
| 666 | long s = seekoff; |
| 667 | |
| 668 | while (s--) |
| 669 | (void)getc(fp); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if (hextype == HEX_CINCLUDE) |
| 674 | { |
| 675 | if (fp != stdin) |
| 676 | { |
| 677 | fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : ""); |
| 678 | for (e = 0; (c = argv[1][e]) != 0; e++) |
| 679 | putc(isalnum(c) ? c : '_', fpo); |
| 680 | fputs("[] = {\n", fpo); |
| 681 | } |
| 682 | |
| 683 | p = 0; |
| 684 | while ((length < 0 || p < length) && (c = getc(fp)) != EOF) |
| 685 | { |
| 686 | fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", |
| 687 | (p % cols) ? ", " : ",\n "+2*!p, c); |
| 688 | p++; |
| 689 | } |
| 690 | |
| 691 | if (p) |
| 692 | fputs("\n};\n"+3*(fp == stdin), fpo); |
| 693 | |
| 694 | if (fp != stdin) |
| 695 | { |
| 696 | fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : ""); |
| 697 | for (e = 0; (c = argv[1][e]) != 0; e++) |
| 698 | putc(isalnum(c) ? c : '_', fpo); |
| 699 | fprintf(fpo, "_len = %d;\n", p); |
| 700 | } |
| 701 | |
| 702 | fclose(fp); |
| 703 | fclose(fpo); |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | if (hextype == HEX_POSTSCRIPT) |
| 708 | { |
| 709 | p = cols; |
| 710 | while ((length < 0 || n < length) && (e = getc(fp)) != EOF) |
| 711 | { |
| 712 | putchar(hexx[(e >> 4) & 0xf]); |
| 713 | putchar(hexx[(e ) & 0xf]); |
| 714 | n++; |
| 715 | if (!--p) |
| 716 | { |
| 717 | putchar('\n'); |
| 718 | p = cols; |
| 719 | } |
| 720 | } |
| 721 | if (p < cols) |
| 722 | putchar('\n'); |
| 723 | fclose(fp); |
| 724 | fclose(fpo); |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | /* hextype: HEX_NORMAL or HEX_BITS */ |
| 729 | |
| 730 | if (hextype == HEX_NORMAL) |
| 731 | grplen = octspergrp + octspergrp + 1; /* chars per octet group */ |
| 732 | else /* hextype == HEX_BITS */ |
| 733 | grplen = 8 * octspergrp + 1; |
| 734 | |
| 735 | while ((length < 0 || n < length) && (e = getc(fp)) != EOF) |
| 736 | { |
| 737 | if (p == 0) |
| 738 | { |
| 739 | sprintf(l, "%07lx: ", n + seekoff); |
| 740 | for (c = 9; c < LLEN; l[c++] = ' '); |
| 741 | } |
| 742 | if (hextype == HEX_NORMAL) |
| 743 | { |
| 744 | l[c = (9 + (grplen * p) / octspergrp)] = hexx[(e >> 4) & 0xf]; |
| 745 | l[++c] = hexx[ e & 0xf]; |
| 746 | } |
| 747 | else /* hextype == HEX_BITS */ |
| 748 | { |
| 749 | int i; |
| 750 | |
| 751 | c = (9 + (grplen * p) / octspergrp) - 1; |
| 752 | for (i = 7; i >= 0; i--) |
| 753 | l[++c] = (e & (1 << i)) ? '1' : '0'; |
| 754 | } |
| 755 | if (ebcdic) |
| 756 | e = (e < 64) ? '.' : etoa64[e-64]; |
| 757 | /* When changing this update definition of LLEN above. */ |
| 758 | l[11 + (grplen * cols - 1)/octspergrp + p] = |
| 759 | #ifdef __MVS__ |
| 760 | (e >= 64) |
| 761 | #else |
| 762 | (e > 31 && e < 127) |
| 763 | #endif |
| 764 | ? e : '.'; |
| 765 | if (e) |
| 766 | nonzero++; |
| 767 | n++; |
| 768 | if (++p == cols) |
| 769 | { |
| 770 | l[c = (11 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; |
| 771 | xxdline(fpo, l, autoskip ? nonzero : 1); |
| 772 | nonzero = 0; |
| 773 | p = 0; |
| 774 | } |
| 775 | } |
| 776 | if (p) |
| 777 | { |
| 778 | l[c = (11 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; |
| 779 | xxdline(fpo, l, 1); |
| 780 | } |
| 781 | else if (autoskip) |
| 782 | xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ |
| 783 | |
| 784 | fclose(fp); |
| 785 | fclose(fpo); |
| 786 | return 0; |
| 787 | } |