hexdump.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. //usage:#define hexdump_trivial_usage
  12. //usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..."
  13. //usage:#define hexdump_full_usage "\n\n"
  14. //usage: "Display FILEs (or stdin) in a user specified format\n"
  15. //usage: "\n -b One-byte octal display"
  16. //usage: "\n -c One-byte character display"
  17. //usage: "\n -C Canonical hex+ASCII, 16 bytes per line"
  18. //usage: "\n -d Two-byte decimal display"
  19. //usage: "\n -e FORMAT_STRING"
  20. //usage: "\n -f FORMAT_FILE"
  21. //usage: "\n -n LENGTH Interpret only LENGTH bytes of input"
  22. //usage: "\n -o Two-byte octal display"
  23. //usage: "\n -s OFFSET Skip OFFSET bytes"
  24. //usage: "\n -v Display all input data"
  25. //usage: "\n -x Two-byte hexadecimal display"
  26. //usage: IF_FEATURE_HEXDUMP_REVERSE(
  27. //usage: "\n -R Reverse of 'hexdump -Cv'")
  28. //usage:
  29. //usage:#define hd_trivial_usage
  30. //usage: "FILE..."
  31. //usage:#define hd_full_usage "\n\n"
  32. //usage: "hd is an alias for hexdump -C"
  33. #include "libbb.h"
  34. #include "dump.h"
  35. /* This is a NOEXEC applet. Be very careful! */
  36. static void bb_dump_addfile(dumper_t *dumper, char *name)
  37. {
  38. char *p;
  39. FILE *fp;
  40. char *buf;
  41. fp = xfopen_for_read(name);
  42. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  43. p = skip_whitespace(buf);
  44. if (*p && (*p != '#')) {
  45. bb_dump_add(dumper, p);
  46. }
  47. free(buf);
  48. }
  49. fclose(fp);
  50. }
  51. static const char *const add_strings[] = {
  52. "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
  53. "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
  54. "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
  55. "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
  56. "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
  57. };
  58. static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
  59. static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
  60. static const struct suffix_mult suffixes[] = {
  61. { "b", 512 },
  62. { "k", 1024 },
  63. { "m", 1024*1024 },
  64. { "", 0 }
  65. };
  66. int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  67. int hexdump_main(int argc, char **argv)
  68. {
  69. dumper_t *dumper = alloc_dumper();
  70. const char *p;
  71. int ch;
  72. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  73. FILE *fp;
  74. smallint rdump = 0;
  75. #endif
  76. if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
  77. ch = 'C';
  78. goto hd_applet;
  79. }
  80. /* We cannot use getopt32: in hexdump options are cumulative.
  81. * E.g. "hexdump -C -C file" should dump each line twice */
  82. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  83. p = strchr(hexdump_opts, ch);
  84. if (!p)
  85. bb_show_usage();
  86. if ((p - hexdump_opts) < 5) {
  87. bb_dump_add(dumper, add_first);
  88. bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
  89. }
  90. /* Save a little bit of space below by omitting the 'else's. */
  91. if (ch == 'C') {
  92. hd_applet:
  93. bb_dump_add(dumper, "\"%08.8_Ax\n\"");
  94. bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
  95. bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
  96. }
  97. if (ch == 'e') {
  98. bb_dump_add(dumper, optarg);
  99. } /* else */
  100. if (ch == 'f') {
  101. bb_dump_addfile(dumper, optarg);
  102. } /* else */
  103. if (ch == 'n') {
  104. dumper->dump_length = xatoi_positive(optarg);
  105. } /* else */
  106. if (ch == 's') { /* compat: -s accepts hex numbers too */
  107. dumper->dump_skip = xstrtoul_range_sfx(optarg, /*base:*/ 0, /*lo:*/ 0, /*hi:*/ LONG_MAX, suffixes);
  108. } /* else */
  109. if (ch == 'v') {
  110. dumper->dump_vflag = ALL;
  111. }
  112. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  113. if (ch == 'R') {
  114. rdump = 1;
  115. }
  116. #endif
  117. }
  118. if (!dumper->fshead) {
  119. bb_dump_add(dumper, add_first);
  120. bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
  121. }
  122. argv += optind;
  123. #if !ENABLE_FEATURE_HEXDUMP_REVERSE
  124. return bb_dump_dump(dumper, argv);
  125. #else
  126. if (!rdump) {
  127. return bb_dump_dump(dumper, argv);
  128. }
  129. /* -R: reverse of 'hexdump -Cv' */
  130. fp = stdin;
  131. if (!*argv) {
  132. argv--;
  133. goto jump_in;
  134. }
  135. do {
  136. char *buf;
  137. fp = xfopen_for_read(*argv);
  138. jump_in:
  139. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  140. p = buf;
  141. while (1) {
  142. /* skip address or previous byte */
  143. while (isxdigit(*p)) p++;
  144. while (*p == ' ') p++;
  145. /* '|' char will break the line */
  146. if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
  147. break;
  148. putchar(ch);
  149. }
  150. free(buf);
  151. }
  152. fclose(fp);
  153. } while (*++argv);
  154. fflush_stdout_and_exit(EXIT_SUCCESS);
  155. #endif
  156. }