/src/xxd/xxd.c

https://bitbucket.org/ultra_iter/vim-qt · C · 849 lines · 677 code · 62 blank · 110 comment · 286 complexity · 0190588920561aada1d09a63f29174fe MD5 · raw file

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