3
0

hexdump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  61. int hexdump_main(int argc, char **argv)
  62. {
  63. dumper_t *dumper = alloc_dumper();
  64. const char *p;
  65. int ch;
  66. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  67. FILE *fp;
  68. smallint rdump = 0;
  69. #endif
  70. if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
  71. ch = 'C';
  72. goto hd_applet;
  73. }
  74. /* We cannot use getopt32: in hexdump options are cumulative.
  75. * E.g. "hexdump -C -C file" should dump each line twice */
  76. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  77. p = strchr(hexdump_opts, ch);
  78. if (!p)
  79. bb_show_usage();
  80. if ((p - hexdump_opts) < 5) {
  81. bb_dump_add(dumper, add_first);
  82. bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
  83. }
  84. /* Save a little bit of space below by omitting the 'else's. */
  85. if (ch == 'C') {
  86. hd_applet:
  87. bb_dump_add(dumper, "\"%08.8_Ax\n\"");
  88. bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
  89. bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
  90. }
  91. if (ch == 'e') {
  92. bb_dump_add(dumper, optarg);
  93. } /* else */
  94. if (ch == 'f') {
  95. bb_dump_addfile(dumper, optarg);
  96. } /* else */
  97. if (ch == 'n') {
  98. dumper->dump_length = xatoi_positive(optarg);
  99. } /* else */
  100. if (ch == 's') { /* compat: -s accepts hex numbers too */
  101. dumper->dump_skip = xstrtoull_range_sfx(
  102. optarg,
  103. /*base:*/ 0,
  104. /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
  105. bkm_suffixes
  106. );
  107. } /* else */
  108. if (ch == 'v') {
  109. dumper->dump_vflag = ALL;
  110. }
  111. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  112. if (ch == 'R') {
  113. rdump = 1;
  114. }
  115. #endif
  116. }
  117. if (!dumper->fshead) {
  118. bb_dump_add(dumper, add_first);
  119. bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
  120. }
  121. argv += optind;
  122. #if !ENABLE_FEATURE_HEXDUMP_REVERSE
  123. return bb_dump_dump(dumper, argv);
  124. #else
  125. if (!rdump) {
  126. return bb_dump_dump(dumper, argv);
  127. }
  128. /* -R: reverse of 'hexdump -Cv' */
  129. fp = stdin;
  130. if (!*argv) {
  131. argv--;
  132. goto jump_in;
  133. }
  134. do {
  135. char *buf;
  136. fp = xfopen_for_read(*argv);
  137. jump_in:
  138. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  139. p = buf;
  140. while (1) {
  141. /* skip address or previous byte */
  142. while (isxdigit(*p)) p++;
  143. while (*p == ' ') p++;
  144. /* '|' char will break the line */
  145. if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
  146. break;
  147. putchar(ch);
  148. }
  149. free(buf);
  150. }
  151. fclose(fp);
  152. } while (*++argv);
  153. fflush_stdout_and_exit(EXIT_SUCCESS);
  154. #endif
  155. }