xxd.c 24 KB

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