hexl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Convert files for Emacs Hexl mode.
  2. Copyright (C) 1989, 2001-2012 Free Software Foundation, Inc.
  3. Author: Keith Gabryelski
  4. (according to authors.el)
  5. This file is not considered part of GNU Emacs.
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #ifdef DOS_NT
  22. #include <fcntl.h>
  23. #if __DJGPP__ >= 2
  24. #include <io.h>
  25. #endif
  26. #endif
  27. #ifdef WINDOWSNT
  28. #include <io.h>
  29. #endif
  30. #define DEFAULT_GROUPING 0x01
  31. #define DEFAULT_BASE 16
  32. #undef TRUE
  33. #undef FALSE
  34. #define TRUE (1)
  35. #define FALSE (0)
  36. int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
  37. int group_by = DEFAULT_GROUPING;
  38. char *progname;
  39. void usage (void) NO_RETURN;
  40. int
  41. main (int argc, char **argv)
  42. {
  43. register long address;
  44. char string[18];
  45. FILE *fp;
  46. progname = *argv++; --argc;
  47. /*
  48. ** -hex hex dump
  49. ** -oct Octal dump
  50. ** -group-by-8-bits
  51. ** -group-by-16-bits
  52. ** -group-by-32-bits
  53. ** -group-by-64-bits
  54. ** -iso iso character set.
  55. ** -big-endian Big Endian
  56. ** -little-endian Little Endian
  57. ** -un || -de from hexl format to binary.
  58. ** -- End switch list.
  59. ** <filename> dump filename
  60. ** - (as filename == stdin)
  61. */
  62. while (*argv && *argv[0] == '-' && (*argv)[1])
  63. {
  64. /* A switch! */
  65. if (!strcmp (*argv, "--"))
  66. {
  67. --argc; argv++;
  68. break;
  69. }
  70. else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
  71. {
  72. un_flag = TRUE;
  73. --argc; argv++;
  74. }
  75. else if (!strcmp (*argv, "-hex"))
  76. {
  77. base = 16;
  78. --argc; argv++;
  79. }
  80. else if (!strcmp (*argv, "-iso"))
  81. {
  82. iso_flag = TRUE;
  83. --argc; argv++;
  84. }
  85. else if (!strcmp (*argv, "-oct"))
  86. {
  87. base = 8;
  88. --argc; argv++;
  89. }
  90. else if (!strcmp (*argv, "-big-endian"))
  91. {
  92. endian = 1;
  93. --argc; argv++;
  94. }
  95. else if (!strcmp (*argv, "-little-endian"))
  96. {
  97. endian = 0;
  98. --argc; argv++;
  99. }
  100. else if (!strcmp (*argv, "-group-by-8-bits"))
  101. {
  102. group_by = 0x00;
  103. --argc; argv++;
  104. }
  105. else if (!strcmp (*argv, "-group-by-16-bits"))
  106. {
  107. group_by = 0x01;
  108. --argc; argv++;
  109. }
  110. else if (!strcmp (*argv, "-group-by-32-bits"))
  111. {
  112. group_by = 0x03;
  113. --argc; argv++;
  114. }
  115. else if (!strcmp (*argv, "-group-by-64-bits"))
  116. {
  117. group_by = 0x07;
  118. endian = 0;
  119. --argc; argv++;
  120. }
  121. else
  122. {
  123. fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
  124. *argv);
  125. usage ();
  126. }
  127. }
  128. do
  129. {
  130. if (*argv == NULL)
  131. fp = stdin;
  132. else
  133. {
  134. char *filename = *argv++;
  135. if (!strcmp (filename, "-"))
  136. fp = stdin;
  137. else if ((fp = fopen (filename, "r")) == NULL)
  138. {
  139. perror (filename);
  140. continue;
  141. }
  142. }
  143. if (un_flag)
  144. {
  145. char buf[18];
  146. #ifdef DOS_NT
  147. #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
  148. if (!isatty (fileno (stdout)))
  149. setmode (fileno (stdout), O_BINARY);
  150. #else
  151. (stdout)->_flag &= ~_IOTEXT; /* print binary */
  152. _setmode (fileno (stdout), O_BINARY);
  153. #endif
  154. #endif
  155. for (;;)
  156. {
  157. register int i, c = 0, d;
  158. #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
  159. /* Skip 10 bytes. */
  160. if (fread (buf, 1, 10, fp) != 10)
  161. break;
  162. for (i=0; i < 16; ++i)
  163. {
  164. if ((c = getc (fp)) == ' ' || c == EOF)
  165. break;
  166. d = getc (fp);
  167. c = hexchar (c) * 0x10 + hexchar (d);
  168. putchar (c);
  169. if ((i&group_by) == group_by)
  170. getc (fp);
  171. }
  172. if (c == ' ')
  173. {
  174. while ((c = getc (fp)) != '\n' && c != EOF)
  175. ;
  176. if (c == EOF)
  177. break;
  178. }
  179. else
  180. {
  181. if (i < 16)
  182. break;
  183. /* Skip 18 bytes. */
  184. if (fread (buf, 1, 18, fp) != 18)
  185. break;
  186. }
  187. }
  188. }
  189. else
  190. {
  191. #ifdef DOS_NT
  192. #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
  193. if (!isatty (fileno (fp)))
  194. setmode (fileno (fp), O_BINARY);
  195. #else
  196. (fp)->_flag &= ~_IOTEXT; /* read binary */
  197. _setmode (fileno (fp), O_BINARY);
  198. #endif
  199. #endif
  200. address = 0;
  201. string[0] = ' ';
  202. string[17] = '\0';
  203. for (;;)
  204. {
  205. register int i, c = 0;
  206. for (i=0; i < 16; ++i)
  207. {
  208. if ((c = getc (fp)) == EOF)
  209. {
  210. if (!i)
  211. break;
  212. fputs (" ", stdout);
  213. string[i+1] = '\0';
  214. }
  215. else
  216. {
  217. if (!i)
  218. printf ("%08lx: ", address);
  219. if (iso_flag)
  220. string[i+1] =
  221. (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
  222. else
  223. string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
  224. printf ("%02x", c);
  225. }
  226. if ((i&group_by) == group_by)
  227. putchar (' ');
  228. }
  229. if (i)
  230. puts (string);
  231. if (c == EOF)
  232. break;
  233. address += 0x10;
  234. }
  235. }
  236. if (fp != stdin)
  237. fclose (fp);
  238. } while (*argv != NULL);
  239. return EXIT_SUCCESS;
  240. }
  241. void
  242. usage (void)
  243. {
  244. fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
  245. exit (EXIT_FAILURE);
  246. }
  247. /* hexl.c ends here */