hexdump_xxd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * xxd implementation for busybox
  4. *
  5. * Copyright (c) 2017 Denys Vlasenko <vda.linux@gmail.com>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config XXD
  10. //config: bool "xxd (8.9 kb)"
  11. //config: default y
  12. //config: help
  13. //config: The xxd utility is used to display binary data in a readable
  14. //config: way that is comparable to the output from most hex editors.
  15. //applet:IF_XXD(APPLET_NOEXEC(xxd, xxd, BB_DIR_USR_BIN, BB_SUID_DROP, xxd))
  16. //kbuild:lib-$(CONFIG_XXD) += hexdump_xxd.o
  17. // $ xxd --version
  18. // xxd V1.10 27oct98 by Juergen Weigert
  19. // $ xxd --help
  20. // Usage:
  21. // xxd [options] [infile [outfile]]
  22. // or
  23. // xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
  24. // Options:
  25. // -a toggle autoskip: A single '*' replaces nul-lines. Default off.
  26. // -b binary digit dump (incompatible with -ps,-i,-r). Default hex.
  27. // -c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
  28. // -E show characters in EBCDIC. Default ASCII.
  29. // -e little-endian dump (incompatible with -ps,-i,-r).
  30. // -g number of octets per group in normal output. Default 2 (-e: 4).
  31. // -i output in C include file style.
  32. // -l len stop after <len> octets.
  33. // -o off add <off> to the displayed file position.
  34. // -ps output in postscript plain hexdump style.
  35. // -r reverse operation: convert (or patch) hexdump into binary.
  36. // -r -s off revert with <off> added to file positions found in hexdump.
  37. // -s [+][-]seek start at <seek> bytes abs. (or +: rel.) infile offset.
  38. // -u use upper case hex letters.
  39. //usage:#define xxd_trivial_usage
  40. //usage: "[OPTIONS] [FILE]"
  41. //usage:#define xxd_full_usage "\n\n"
  42. //usage: "Hex dump FILE (or stdin)\n"
  43. //usage: "\n -g N Bytes per group"
  44. //usage: "\n -c N Bytes per line"
  45. //usage: "\n -p Show only hex bytes, assumes -c30"
  46. // exactly the same help text lines in hexdump and xxd:
  47. //usage: "\n -l LENGTH Show only first LENGTH bytes"
  48. //usage: "\n -s OFFSET Skip OFFSET bytes"
  49. // TODO: implement -r (see hexdump -R)
  50. #include "libbb.h"
  51. #include "dump.h"
  52. /* This is a NOEXEC applet. Be very careful! */
  53. int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  54. int xxd_main(int argc UNUSED_PARAM, char **argv)
  55. {
  56. char buf[80];
  57. dumper_t *dumper;
  58. char *opt_l, *opt_s;
  59. unsigned bytes = 2;
  60. unsigned cols = 0;
  61. unsigned opt;
  62. dumper = alloc_dumper();
  63. #define OPT_l (1 << 0)
  64. #define OPT_s (1 << 1)
  65. #define OPT_a (1 << 2)
  66. #define OPT_p (1 << 3)
  67. opt = getopt32(argv, "^" "l:s:apg:+c:+" "\0" "?1" /* 1 argument max */,
  68. &opt_l, &opt_s, &bytes, &cols
  69. );
  70. argv += optind;
  71. dumper->dump_vflag = ALL;
  72. // if (opt & OPT_a)
  73. // dumper->dump_vflag = SKIPNUL; ..does not exist
  74. if (opt & OPT_l) {
  75. dumper->dump_length = xstrtou_range(
  76. opt_l,
  77. /*base:*/ 0,
  78. /*lo:*/ 0, /*hi:*/ INT_MAX
  79. );
  80. }
  81. if (opt & OPT_s) {
  82. dumper->dump_skip = xstrtoull_range(
  83. opt_s,
  84. /*base:*/ 0,
  85. /*lo:*/ 0, /*hi:*/ OFF_T_MAX
  86. );
  87. //BUGGY for /proc/version (unseekable?)
  88. }
  89. if (opt & OPT_p) {
  90. if (cols == 0)
  91. cols = 30;
  92. bytes = cols; /* -p ignores -gN */
  93. } else {
  94. if (cols == 0)
  95. cols = 16;
  96. bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
  97. }
  98. if (bytes < 1 || bytes >= cols) {
  99. sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx"
  100. bb_dump_add(dumper, buf);
  101. }
  102. else if (bytes == 1) {
  103. sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx "
  104. bb_dump_add(dumper, buf);
  105. }
  106. else {
  107. /* Format "print byte" with and without trailing space */
  108. #define BS "/1 \"%02x \""
  109. #define B "/1 \"%02x\""
  110. unsigned i;
  111. char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
  112. char *p = bigbuf;
  113. for (i = 1; i <= cols; i++) {
  114. if (i == cols || i % bytes)
  115. p = stpcpy(p, B);
  116. else
  117. p = stpcpy(p, BS);
  118. }
  119. // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx"
  120. // todo: can be more clever and use
  121. // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats
  122. //bb_error_msg("ADDED:'%s'", bigbuf);
  123. bb_dump_add(dumper, bigbuf);
  124. free(bigbuf);
  125. }
  126. if (!(opt & OPT_p)) {
  127. sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
  128. bb_dump_add(dumper, buf);
  129. } else {
  130. bb_dump_add(dumper, "\"\n\"");
  131. }
  132. return bb_dump_dump(dumper, argv);
  133. }