hexdump.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 (8.6 kb)"
  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 HD
  19. //config: bool "hd (7.8 kb)"
  20. //config: default y
  21. //config: help
  22. //config: hd is an alias to hexdump -C.
  23. //applet:IF_HEXDUMP(APPLET_NOEXEC(hexdump, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hexdump))
  24. //applet:IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
  25. //kbuild:lib-$(CONFIG_HEXDUMP) += hexdump.o
  26. //kbuild:lib-$(CONFIG_HD) += hexdump.o
  27. //usage:#define hexdump_trivial_usage
  28. //usage: "[-bcdoxCv] [-e FMT] [-f FMT_FILE] [-n LEN] [-s OFS] [FILE]..."
  29. //usage:#define hexdump_full_usage "\n\n"
  30. //usage: "Display FILEs (or stdin) in a user specified format\n"
  31. //usage: "\n -b 1-byte octal display"
  32. //usage: "\n -c 1-byte character display"
  33. //usage: "\n -d 2-byte decimal display"
  34. //usage: "\n -o 2-byte octal display"
  35. //usage: "\n -x 2-byte hex display"
  36. //usage: "\n -C hex+ASCII 16 bytes per line"
  37. //usage: "\n -v Show all (no dup folding)"
  38. //usage: "\n -e FORMAT_STR Example: '16/1 \"%02x|\"\"\\n\"'"
  39. //usage: "\n -f FORMAT_FILE"
  40. // exactly the same help text lines in hexdump and xxd:
  41. //usage: "\n -n LENGTH Show only first LENGTH bytes"
  42. //usage: "\n -s OFFSET Skip OFFSET bytes"
  43. //usage:
  44. //usage:#define hd_trivial_usage
  45. //usage: "FILE..."
  46. //usage:#define hd_full_usage "\n\n"
  47. //usage: "hd is an alias for hexdump -C"
  48. #include "libbb.h"
  49. #include "dump.h"
  50. /* This is a NOEXEC applet. Be very careful! */
  51. static void bb_dump_addfile(dumper_t *dumper, char *name)
  52. {
  53. char *p;
  54. FILE *fp;
  55. char *buf;
  56. fp = xfopen_for_read(name);
  57. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  58. p = skip_whitespace(buf);
  59. if (*p && (*p != '#')) {
  60. bb_dump_add(dumper, p);
  61. }
  62. free(buf);
  63. }
  64. fclose(fp);
  65. }
  66. static const char *const add_strings[] ALIGN_PTR = {
  67. "\"%07.7_ax \"16/1 \"%03o \"\"\n\"", /* b */
  68. "\"%07.7_ax \"16/1 \"%3_c \"\"\n\"", /* c */
  69. "\"%07.7_ax \"8/2 \" %05u \"\"\n\"", /* d */
  70. "\"%07.7_ax \"8/2 \" %06o \"\"\n\"", /* o */
  71. "\"%07.7_ax \"8/2 \" %04x \"\"\n\"", /* x */
  72. };
  73. static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
  74. static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v";
  75. int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  76. int hexdump_main(int argc, char **argv)
  77. {
  78. dumper_t *dumper = alloc_dumper();
  79. const char *p;
  80. int ch;
  81. if (ENABLE_HD
  82. && (!ENABLE_HEXDUMP || !applet_name[2])
  83. ) { /* we are "hd" */
  84. ch = 'C';
  85. goto hd_applet;
  86. }
  87. /* We cannot use getopt32: in hexdump options are cumulative.
  88. * E.g. "hexdump -C -C file" should dump each line twice */
  89. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  90. p = strchr(hexdump_opts, ch);
  91. if (!p)
  92. bb_show_usage();
  93. if ((p - hexdump_opts) < 5) {
  94. bb_dump_add(dumper, add_first);
  95. bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
  96. }
  97. /* Save a little bit of space below by omitting the 'else's. */
  98. if (ch == 'C') {
  99. hd_applet:
  100. bb_dump_add(dumper, "\"%08.8_Ax\n\""); // final address line after dump
  101. //------------------- "address " 8 * "xx " " " 8 * "xx "
  102. bb_dump_add(dumper, "\"%08.8_ax \"8/1 \"%02x \"\" \"8/1 \"%02x \"");
  103. //------------------- " |ASCII...........|\n"
  104. bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
  105. }
  106. if (ch == 'e') {
  107. bb_dump_add(dumper, optarg);
  108. } /* else */
  109. if (ch == 'f') {
  110. bb_dump_addfile(dumper, optarg);
  111. } /* else */
  112. if (ch == 'n') {
  113. dumper->dump_length = xatoi_positive(optarg);
  114. } /* else */
  115. if (ch == 's') { /* compat: -s accepts hex numbers too */
  116. dumper->dump_skip = xstrtoull_range_sfx(
  117. optarg,
  118. /*base:*/ 0,
  119. /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
  120. bkm_suffixes
  121. );
  122. } /* else */
  123. if (ch == 'v') {
  124. dumper->dump_vflag = ALL;
  125. }
  126. }
  127. if (!dumper->fshead) {
  128. bb_dump_add(dumper, add_first);
  129. bb_dump_add(dumper, "\"%07.7_ax \"8/2 \"%04x \"\"\n\"");
  130. }
  131. argv += optind;
  132. return bb_dump_dump(dumper, argv);
  133. }