hexdump_xxd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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: "[-pr] [-g N] [-c N] [-n LEN] [-s OFS] [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. //usage: "\n -r Reverse (with -p, assumes no offsets in input)"
  50. #include "libbb.h"
  51. #include "dump.h"
  52. /* This is a NOEXEC applet. Be very careful! */
  53. #define OPT_l (1 << 0)
  54. #define OPT_s (1 << 1)
  55. #define OPT_a (1 << 2)
  56. #define OPT_p (1 << 3)
  57. #define OPT_r (1 << 4)
  58. static void reverse(unsigned opt, unsigned cols, const char *filename)
  59. {
  60. FILE *fp;
  61. char *buf;
  62. fp = filename ? xfopen_for_read(filename) : stdin;
  63. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  64. char *p = buf;
  65. unsigned cnt = cols;
  66. if (!(opt & OPT_p)) {
  67. /* skip address */
  68. while (isxdigit(*p)) p++;
  69. /* NB: for xxd -r, first hex portion is address even without colon */
  70. /* If it's there, skip it: */
  71. if (*p == ':') p++;
  72. //TODO: seek (or zero-pad if unseekable) to the address position
  73. //NOTE: -s SEEK value should be added to the address before seeking
  74. }
  75. /* Process hex bytes optionally separated by whitespace */
  76. do {
  77. uint8_t val, c;
  78. p = skip_whitespace(p);
  79. c = *p++;
  80. if (isdigit(c))
  81. val = c - '0';
  82. else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
  83. val = (c|0x20) - ('a' - 10);
  84. else
  85. break;
  86. val <<= 4;
  87. /* Works the same with xxd V1.10:
  88. * echo "31 09 32 0a" | xxd -r -p
  89. * echo "31 0 9 32 0a" | xxd -r -p
  90. * thus allow whitespace even within the byte:
  91. */
  92. p = skip_whitespace(p);
  93. c = *p++;
  94. if (isdigit(c))
  95. val |= c - '0';
  96. else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
  97. val |= (c|0x20) - ('a' - 10);
  98. else
  99. break;
  100. putchar(val);
  101. } while (!(opt & OPT_p) || --cnt != 0);
  102. free(buf);
  103. }
  104. //fclose(fp);
  105. fflush_stdout_and_exit(EXIT_SUCCESS);
  106. }
  107. int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  108. int xxd_main(int argc UNUSED_PARAM, char **argv)
  109. {
  110. char buf[80];
  111. dumper_t *dumper;
  112. char *opt_l, *opt_s;
  113. unsigned bytes = 2;
  114. unsigned cols = 0;
  115. unsigned opt;
  116. dumper = alloc_dumper();
  117. opt = getopt32(argv, "^" "l:s:aprg:+c:+" "\0" "?1" /* 1 argument max */,
  118. &opt_l, &opt_s, &bytes, &cols
  119. );
  120. argv += optind;
  121. dumper->dump_vflag = ALL;
  122. // if (opt & OPT_a)
  123. // dumper->dump_vflag = SKIPNUL; ..does not exist
  124. if (opt & OPT_l) {
  125. dumper->dump_length = xstrtou_range(
  126. opt_l,
  127. /*base:*/ 0,
  128. /*lo:*/ 0, /*hi:*/ INT_MAX
  129. );
  130. }
  131. if (opt & OPT_s) {
  132. dumper->dump_skip = xstrtoull_range(
  133. opt_s,
  134. /*base:*/ 0,
  135. /*lo:*/ 0, /*hi:*/ OFF_T_MAX
  136. );
  137. //BUGGY for /proc/version (unseekable?)
  138. }
  139. if (opt & OPT_p) {
  140. if (cols == 0)
  141. cols = 30;
  142. bytes = cols; /* -p ignores -gN */
  143. } else {
  144. if (cols == 0)
  145. cols = 16;
  146. bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
  147. }
  148. if (opt & OPT_r) {
  149. reverse(opt, cols, argv[0]);
  150. }
  151. if (bytes < 1 || bytes >= cols) {
  152. sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx"
  153. bb_dump_add(dumper, buf);
  154. }
  155. else if (bytes == 1) {
  156. sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx "
  157. bb_dump_add(dumper, buf);
  158. }
  159. else {
  160. /* Format "print byte" with and without trailing space */
  161. #define BS "/1 \"%02x \""
  162. #define B "/1 \"%02x\""
  163. unsigned i;
  164. char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
  165. char *p = bigbuf;
  166. for (i = 1; i <= cols; i++) {
  167. if (i == cols || i % bytes)
  168. p = stpcpy(p, B);
  169. else
  170. p = stpcpy(p, BS);
  171. }
  172. // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx"
  173. // todo: can be more clever and use
  174. // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats
  175. //bb_error_msg("ADDED:'%s'", bigbuf);
  176. bb_dump_add(dumper, bigbuf);
  177. free(bigbuf);
  178. }
  179. if (!(opt & OPT_p)) {
  180. sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
  181. bb_dump_add(dumper, buf);
  182. } else {
  183. bb_dump_add(dumper, "\"\n\"");
  184. dumper->eofstring = "\n";
  185. }
  186. return bb_dump_dump(dumper, argv);
  187. }