hexdump.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * hexdump implementation for busybox
  4. * Based on code from util-linux v 2.11l
  5. *
  6. * Copyright (c) 1989
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. //config:config HEXDUMP
  12. //config: bool "hexdump"
  13. //config: default y
  14. //config: help
  15. //config: The hexdump utility is used to display binary data in a readable
  16. //config: way that is comparable to the output from most hex editors.
  17. //config:
  18. //config:config FEATURE_HEXDUMP_REVERSE
  19. //config: bool "Support -R, reverse of 'hexdump -Cv'"
  20. //config: default y
  21. //config: depends on HEXDUMP
  22. //config: help
  23. //config: The hexdump utility is used to display binary data in an ascii
  24. //config: readable way. This option creates binary data from an ascii input.
  25. //config: NB: this option is non-standard. It's unwise to use it in scripts
  26. //config: aimed to be portable.
  27. //config:
  28. //config:config HD
  29. //config: bool "hd"
  30. //config: default y
  31. //config: help
  32. //config: hd is an alias to hexdump -C.
  33. //applet:IF_HEXDUMP(APPLET_NOEXEC(hexdump, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hexdump))
  34. //applet:IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
  35. //kbuild:lib-$(CONFIG_HEXDUMP) += hexdump.o
  36. //kbuild:lib-$(CONFIG_HD) += hexdump.o
  37. //usage:#define hexdump_trivial_usage
  38. //usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..."
  39. //usage:#define hexdump_full_usage "\n\n"
  40. //usage: "Display FILEs (or stdin) in a user specified format\n"
  41. //usage: "\n -b One-byte octal display"
  42. //usage: "\n -c One-byte character display"
  43. //usage: "\n -C Canonical hex+ASCII, 16 bytes per line"
  44. //usage: "\n -d Two-byte decimal display"
  45. //usage: "\n -e FORMAT_STRING"
  46. //usage: "\n -f FORMAT_FILE"
  47. //usage: "\n -n LENGTH Interpret only LENGTH bytes of input"
  48. //usage: "\n -o Two-byte octal display"
  49. //usage: "\n -s OFFSET Skip OFFSET bytes"
  50. //usage: "\n -v Display all input data"
  51. //usage: "\n -x Two-byte hexadecimal display"
  52. //usage: IF_FEATURE_HEXDUMP_REVERSE(
  53. //usage: "\n -R Reverse of 'hexdump -Cv'")
  54. //usage:
  55. //usage:#define hd_trivial_usage
  56. //usage: "FILE..."
  57. //usage:#define hd_full_usage "\n\n"
  58. //usage: "hd is an alias for hexdump -C"
  59. #include "libbb.h"
  60. #include "dump.h"
  61. /* This is a NOEXEC applet. Be very careful! */
  62. static void bb_dump_addfile(dumper_t *dumper, char *name)
  63. {
  64. char *p;
  65. FILE *fp;
  66. char *buf;
  67. fp = xfopen_for_read(name);
  68. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  69. p = skip_whitespace(buf);
  70. if (*p && (*p != '#')) {
  71. bb_dump_add(dumper, p);
  72. }
  73. free(buf);
  74. }
  75. fclose(fp);
  76. }
  77. static const char *const add_strings[] = {
  78. "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
  79. "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
  80. "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
  81. "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
  82. "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
  83. };
  84. static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
  85. static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
  86. int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  87. int hexdump_main(int argc, char **argv)
  88. {
  89. dumper_t *dumper = alloc_dumper();
  90. const char *p;
  91. int ch;
  92. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  93. FILE *fp;
  94. smallint rdump = 0;
  95. #endif
  96. if (ENABLE_HD
  97. && (!ENABLE_HEXDUMP || !applet_name[2])
  98. ) { /* we are "hd" */
  99. ch = 'C';
  100. goto hd_applet;
  101. }
  102. /* We cannot use getopt32: in hexdump options are cumulative.
  103. * E.g. "hexdump -C -C file" should dump each line twice */
  104. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  105. p = strchr(hexdump_opts, ch);
  106. if (!p)
  107. bb_show_usage();
  108. if ((p - hexdump_opts) < 5) {
  109. bb_dump_add(dumper, add_first);
  110. bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
  111. }
  112. /* Save a little bit of space below by omitting the 'else's. */
  113. if (ch == 'C') {
  114. hd_applet:
  115. bb_dump_add(dumper, "\"%08.8_Ax\n\"");
  116. bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
  117. bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
  118. }
  119. if (ch == 'e') {
  120. bb_dump_add(dumper, optarg);
  121. } /* else */
  122. if (ch == 'f') {
  123. bb_dump_addfile(dumper, optarg);
  124. } /* else */
  125. if (ch == 'n') {
  126. dumper->dump_length = xatoi_positive(optarg);
  127. } /* else */
  128. if (ch == 's') { /* compat: -s accepts hex numbers too */
  129. dumper->dump_skip = xstrtoull_range_sfx(
  130. optarg,
  131. /*base:*/ 0,
  132. /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
  133. bkm_suffixes
  134. );
  135. } /* else */
  136. if (ch == 'v') {
  137. dumper->dump_vflag = ALL;
  138. }
  139. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  140. if (ch == 'R') {
  141. rdump = 1;
  142. }
  143. #endif
  144. }
  145. if (!dumper->fshead) {
  146. bb_dump_add(dumper, add_first);
  147. bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
  148. }
  149. argv += optind;
  150. #if !ENABLE_FEATURE_HEXDUMP_REVERSE
  151. return bb_dump_dump(dumper, argv);
  152. #else
  153. if (!rdump) {
  154. return bb_dump_dump(dumper, argv);
  155. }
  156. /* -R: reverse of 'hexdump -Cv' */
  157. fp = stdin;
  158. if (!*argv) {
  159. argv--;
  160. goto jump_in;
  161. }
  162. do {
  163. char *buf;
  164. fp = xfopen_for_read(*argv);
  165. jump_in:
  166. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  167. p = buf;
  168. while (1) {
  169. /* skip address or previous byte */
  170. while (isxdigit(*p)) p++;
  171. while (*p == ' ') p++;
  172. /* '|' char will break the line */
  173. if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
  174. break;
  175. putchar(ch);
  176. }
  177. free(buf);
  178. }
  179. fclose(fp);
  180. } while (*++argv);
  181. fflush_stdout_and_exit(EXIT_SUCCESS);
  182. #endif
  183. }