hexdump.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.7 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 (8.3 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. "16/1 \" %03o" , /* b */
  68. "16/1 \" %3_c" , /* c */
  69. "8/2 \" %05u" , /* d */
  70. "8/2 \" %06o" , /* o */
  71. "8/2 \" %04x", /* x */
  72. };
  73. static void add_format(dumper_t *dumper, const char *fmt)
  74. {
  75. char fmtbuf[sizeof("\"%07_ax\"" "%s\"" "\"\n\"") + 16];
  76. sprintf(fmtbuf, "\"%%07_ax\"" "%s\"" "\"\n\"", fmt);
  77. bb_dump_add(dumper, "\"%07_Ax\n\"");
  78. bb_dump_add(dumper, fmtbuf);
  79. }
  80. static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v";
  81. int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  82. int hexdump_main(int argc, char **argv)
  83. {
  84. dumper_t *dumper = alloc_dumper();
  85. const char *p;
  86. int ch;
  87. if (ENABLE_HD
  88. && (!ENABLE_HEXDUMP || !applet_name[2])
  89. ) { /* we are "hd" */
  90. ch = 'C';
  91. goto hd_applet;
  92. }
  93. /* We cannot use getopt32: in hexdump options are cumulative.
  94. * E.g. "hexdump -C -C file" should dump each line twice */
  95. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  96. p = strchr(hexdump_opts, ch);
  97. if (!p)
  98. bb_show_usage();
  99. if ((p - hexdump_opts) < 5) {
  100. add_format(dumper, add_strings[(int)(p - hexdump_opts)]);
  101. }
  102. /* Save a little bit of space below by omitting the 'else's. */
  103. if (ch == 'C') {
  104. hd_applet:
  105. bb_dump_add(dumper, "\"%08_Ax\n\""); // final address line after dump
  106. //------------------- "address " 8 * " xx" " " 8 * " xx"
  107. bb_dump_add(dumper, "\"%08_ax \"8/1 \" %02x\"\" \"8/1 \" %02x\"");
  108. //------------------- " |ASCII...........|\n"
  109. bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
  110. }
  111. if (ch == 'e') {
  112. bb_dump_add(dumper, optarg);
  113. } /* else */
  114. if (ch == 'f') {
  115. bb_dump_addfile(dumper, optarg);
  116. } /* else */
  117. if (ch == 'n') {
  118. dumper->dump_length = xatoi_positive(optarg);
  119. } /* else */
  120. if (ch == 's') { /* compat: -s accepts hex numbers too */
  121. dumper->dump_skip = xstrtoull_range_sfx(
  122. optarg,
  123. /*base:*/ 0,
  124. /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
  125. bkm_suffixes
  126. );
  127. } /* else */
  128. if (ch == 'v') {
  129. dumper->dump_vflag = ALL;
  130. }
  131. }
  132. if (!dumper->fshead) {
  133. add_format(dumper, "8/2 \" %04x");
  134. }
  135. argv += optind;
  136. return bb_dump_dump(dumper, argv);
  137. }