hexdump.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 1-byte octal display"
  42. //usage: "\n -c 1-byte character display"
  43. //usage: "\n -d 2-byte decimal display"
  44. //usage: "\n -o 2-byte octal display"
  45. //usage: "\n -x 2-byte hex display"
  46. //usage: "\n -C hex+ASCII 16 bytes per line"
  47. //usage: "\n -v Show all (no dup folding)"
  48. //usage: "\n -e FORMAT_STR Example: '16/1 \"%02x|\"\"\\n\"'"
  49. //usage: "\n -f FORMAT_FILE"
  50. // exactly the same help text lines in hexdump and xxd:
  51. //usage: "\n -n LENGTH Show only first LENGTH bytes"
  52. //usage: "\n -s OFFSET Skip OFFSET bytes"
  53. //usage: IF_FEATURE_HEXDUMP_REVERSE(
  54. //usage: "\n -R Reverse of 'hexdump -Cv'")
  55. // TODO: NONCOMPAT!!! move -R to xxd -r
  56. //usage:
  57. //usage:#define hd_trivial_usage
  58. //usage: "FILE..."
  59. //usage:#define hd_full_usage "\n\n"
  60. //usage: "hd is an alias for hexdump -C"
  61. #include "libbb.h"
  62. #include "dump.h"
  63. /* This is a NOEXEC applet. Be very careful! */
  64. static void bb_dump_addfile(dumper_t *dumper, char *name)
  65. {
  66. char *p;
  67. FILE *fp;
  68. char *buf;
  69. fp = xfopen_for_read(name);
  70. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  71. p = skip_whitespace(buf);
  72. if (*p && (*p != '#')) {
  73. bb_dump_add(dumper, p);
  74. }
  75. free(buf);
  76. }
  77. fclose(fp);
  78. }
  79. static const char *const add_strings[] = {
  80. "\"%07.7_ax \"16/1 \"%03o \"\"\n\"", /* b */
  81. "\"%07.7_ax \"16/1 \"%3_c \"\"\n\"", /* c */
  82. "\"%07.7_ax \"8/2 \" %05u \"\"\n\"", /* d */
  83. "\"%07.7_ax \"8/2 \" %06o \"\"\n\"", /* o */
  84. "\"%07.7_ax \"8/2 \" %04x \"\"\n\"", /* x */
  85. };
  86. static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
  87. static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
  88. int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  89. int hexdump_main(int argc, char **argv)
  90. {
  91. dumper_t *dumper = alloc_dumper();
  92. const char *p;
  93. int ch;
  94. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  95. FILE *fp;
  96. smallint rdump = 0;
  97. #endif
  98. if (ENABLE_HD
  99. && (!ENABLE_HEXDUMP || !applet_name[2])
  100. ) { /* we are "hd" */
  101. ch = 'C';
  102. goto hd_applet;
  103. }
  104. /* We cannot use getopt32: in hexdump options are cumulative.
  105. * E.g. "hexdump -C -C file" should dump each line twice */
  106. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  107. p = strchr(hexdump_opts, ch);
  108. if (!p)
  109. bb_show_usage();
  110. if ((p - hexdump_opts) < 5) {
  111. bb_dump_add(dumper, add_first);
  112. bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
  113. }
  114. /* Save a little bit of space below by omitting the 'else's. */
  115. if (ch == 'C') {
  116. hd_applet:
  117. bb_dump_add(dumper, "\"%08.8_Ax\n\""); // final address line after dump
  118. //------------------- "address " 8 * "xx " " " 8 * "xx "
  119. bb_dump_add(dumper, "\"%08.8_ax \"8/1 \"%02x \"\" \"8/1 \"%02x \"");
  120. //------------------- " |ASCII...........|\n"
  121. bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
  122. }
  123. if (ch == 'e') {
  124. bb_dump_add(dumper, optarg);
  125. } /* else */
  126. if (ch == 'f') {
  127. bb_dump_addfile(dumper, optarg);
  128. } /* else */
  129. if (ch == 'n') {
  130. dumper->dump_length = xatoi_positive(optarg);
  131. } /* else */
  132. if (ch == 's') { /* compat: -s accepts hex numbers too */
  133. dumper->dump_skip = xstrtoull_range_sfx(
  134. optarg,
  135. /*base:*/ 0,
  136. /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
  137. bkm_suffixes
  138. );
  139. } /* else */
  140. if (ch == 'v') {
  141. dumper->dump_vflag = ALL;
  142. }
  143. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  144. if (ch == 'R') {
  145. rdump = 1;
  146. }
  147. #endif
  148. }
  149. if (!dumper->fshead) {
  150. bb_dump_add(dumper, add_first);
  151. bb_dump_add(dumper, "\"%07.7_ax \"8/2 \"%04x \"\"\n\"");
  152. }
  153. argv += optind;
  154. #if !ENABLE_FEATURE_HEXDUMP_REVERSE
  155. return bb_dump_dump(dumper, argv);
  156. #else
  157. if (!rdump) {
  158. return bb_dump_dump(dumper, argv);
  159. }
  160. /* -R: reverse of 'hexdump -Cv' */
  161. fp = stdin;
  162. if (!*argv) {
  163. argv--;
  164. goto jump_in;
  165. }
  166. do {
  167. char *buf;
  168. fp = xfopen_for_read(*argv);
  169. jump_in:
  170. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  171. p = buf;
  172. while (1) {
  173. /* skip address or previous byte */
  174. while (isxdigit(*p)) p++;
  175. while (*p == ' ') p++;
  176. /* '|' char will break the line */
  177. if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
  178. break;
  179. putchar(ch);
  180. }
  181. free(buf);
  182. }
  183. fclose(fp);
  184. } while (*++argv);
  185. fflush_stdout_and_exit(EXIT_SUCCESS);
  186. #endif
  187. }