hexdump_xxd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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: "[-pri] [-g N] [-c N] [-n LEN] [-s OFS] [-o 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. //usage: "\n -i C include file style"
  47. // exactly the same help text lines in hexdump and xxd:
  48. //usage: "\n -l LENGTH Show only first LENGTH bytes"
  49. //usage: "\n -s OFFSET Skip OFFSET bytes"
  50. //usage: "\n -o OFFSET Add OFFSET to displayed offset"
  51. //usage: "\n -r Reverse (with -p, assumes no offsets in input)"
  52. #include "libbb.h"
  53. #include "dump.h"
  54. /* This is a NOEXEC applet. Be very careful! */
  55. #define OPT_l (1 << 0)
  56. #define OPT_s (1 << 1)
  57. #define OPT_a (1 << 2)
  58. #define OPT_p (1 << 3)
  59. #define OPT_i (1 << 4)
  60. #define OPT_r (1 << 5)
  61. #define OPT_g (1 << 6)
  62. #define OPT_c (1 << 7)
  63. #define OPT_o (1 << 8)
  64. static void reverse(unsigned opt, const char *filename)
  65. {
  66. FILE *fp;
  67. char *buf;
  68. fp = filename ? xfopen_for_read(filename) : stdin;
  69. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  70. char *p;
  71. p = buf;
  72. if (!(opt & OPT_p)) {
  73. /* skip address */
  74. while (isxdigit(*p)) p++;
  75. /* NB: for xxd -r, first hex portion is address even without colon */
  76. /* If it's there, skip it: */
  77. if (*p == ':') p++;
  78. //TODO: seek (or zero-pad if unseekable) to the address position
  79. //NOTE: -s SEEK value should be added to the address before seeking
  80. }
  81. /* Process hex bytes optionally separated by whitespace */
  82. for (;;) {
  83. uint8_t val, c;
  84. nibble1:
  85. p = skip_whitespace(p);
  86. c = *p++;
  87. if (isdigit(c))
  88. val = c - '0';
  89. else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
  90. val = (c|0x20) - ('a' - 10);
  91. else {
  92. /* xxd V1.10 is inconsistent here.
  93. * echo -e "31 !3 0a 0a" | xxd -r -p
  94. * is "10<a0>" (no <cr>) - "!" is ignored,
  95. * but
  96. * echo -e "31 !!343434\n30 0a" | xxd -r -p
  97. * is "10<cr>" - "!!" drops rest of the line.
  98. * We will ignore all invalid chars:
  99. */
  100. if (c != '\0')
  101. goto nibble1;
  102. break;
  103. }
  104. val <<= 4;
  105. /* Works the same with xxd V1.10:
  106. * echo "31 09 32 0a" | xxd -r -p
  107. * echo "31 0 9 32 0a" | xxd -r -p
  108. * thus allow whitespace even within the byte:
  109. */
  110. nibble2:
  111. p = skip_whitespace(p);
  112. c = *p++;
  113. if (isdigit(c))
  114. val |= c - '0';
  115. else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
  116. val |= (c|0x20) - ('a' - 10);
  117. else {
  118. if (c != '\0') {
  119. /* "...3<not_hex_char>..." ignores both chars */
  120. goto nibble1;
  121. }
  122. /* Nibbles can join even through newline:
  123. * echo -e "31 3\n2 0a" | xxd -r -p
  124. * is "12<cr>".
  125. */
  126. free(buf);
  127. p = buf = xmalloc_fgetline(fp);
  128. if (!buf)
  129. break;
  130. goto nibble2;
  131. }
  132. putchar(val);
  133. }
  134. free(buf);
  135. }
  136. //fclose(fp);
  137. fflush_stdout_and_exit(EXIT_SUCCESS);
  138. }
  139. static void print_C_style(const char *p, const char *hdr)
  140. {
  141. printf(hdr, isdigit(p[0]) ? "__" : "");
  142. while (*p) {
  143. bb_putchar(isalnum(*p) ? *p : '_');
  144. p++;
  145. }
  146. }
  147. int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  148. int xxd_main(int argc UNUSED_PARAM, char **argv)
  149. {
  150. char buf[80];
  151. dumper_t *dumper;
  152. char *opt_l, *opt_s, *opt_o;
  153. unsigned bytes = 2;
  154. unsigned cols = 0;
  155. unsigned opt;
  156. int r;
  157. dumper = alloc_dumper();
  158. opt = getopt32(argv, "^" "l:s:apirg:+c:+o:" "\0" "?1" /* 1 argument max */,
  159. &opt_l, &opt_s, &bytes, &cols, &opt_o
  160. );
  161. argv += optind;
  162. dumper->dump_vflag = ALL;
  163. // if (opt & OPT_a)
  164. // dumper->dump_vflag = SKIPNUL; ..does not exist
  165. if (opt & OPT_l) {
  166. dumper->dump_length = xstrtou_range(
  167. opt_l,
  168. /*base:*/ 0,
  169. /*lo:*/ 0, /*hi:*/ INT_MAX
  170. );
  171. }
  172. if (opt & OPT_s) {
  173. dumper->dump_skip = xstrtoull_range(
  174. opt_s,
  175. /*base:*/ 0,
  176. /*lo:*/ 0, /*hi:*/ OFF_T_MAX
  177. );
  178. //BUGGY for /proc/version (unseekable?)
  179. }
  180. if (opt & OPT_r) {
  181. reverse(opt, argv[0]);
  182. }
  183. if (opt & OPT_o) {
  184. /* -o accepts negative numbers too */
  185. dumper->xxd_displayoff = xstrtoll(opt_o, /*base:*/ 0);
  186. }
  187. if (opt & OPT_p) {
  188. if (cols == 0)
  189. cols = 30;
  190. bytes = cols; /* -p ignores -gN */
  191. } else {
  192. if (cols == 0)
  193. cols = (opt & OPT_i) ? 12 : 16;
  194. if (opt & OPT_i) {
  195. bytes = 1; // -i ignores -gN
  196. // output is " 0xXX, 0xXX, 0xXX...", add leading space
  197. bb_dump_add(dumper, "\" \"");
  198. } else
  199. bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
  200. }
  201. if (bytes < 1 || bytes >= cols) {
  202. sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "XX"
  203. bb_dump_add(dumper, buf);
  204. }
  205. else if (bytes == 1) {
  206. if (opt & OPT_i)
  207. sprintf(buf, "%u/1 \" 0x%%02x,\"", cols); // cols * " 0xXX,"
  208. //TODO: compat: omit the last comma after the very last byte
  209. else
  210. sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "XX "
  211. bb_dump_add(dumper, buf);
  212. }
  213. else {
  214. /* Format "print byte" with and without trailing space */
  215. #define BS "/1 \"%02x \""
  216. #define B "/1 \"%02x\""
  217. unsigned i;
  218. char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
  219. char *p = bigbuf;
  220. for (i = 1; i <= cols; i++) {
  221. if (i == cols || i % bytes)
  222. p = stpcpy(p, B);
  223. else
  224. p = stpcpy(p, BS);
  225. }
  226. // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx"
  227. // todo: can be more clever and use
  228. // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats
  229. //bb_error_msg("ADDED:'%s'", bigbuf);
  230. bb_dump_add(dumper, bigbuf);
  231. free(bigbuf);
  232. }
  233. if (!(opt & (OPT_p|OPT_i))) {
  234. sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
  235. bb_dump_add(dumper, buf);
  236. } else {
  237. bb_dump_add(dumper, "\"\n\"");
  238. dumper->xxd_eofstring = "\n";
  239. }
  240. if ((opt & OPT_i) && argv[0]) {
  241. print_C_style(argv[0], "unsigned char %s");
  242. printf("[] = {\n");
  243. }
  244. r = bb_dump_dump(dumper, argv);
  245. if (r == 0 && (opt & OPT_i) && argv[0]) {
  246. print_C_style(argv[0], "};\nunsigned int %s");
  247. printf("_len = %"OFF_FMT"u;\n", dumper->address);
  248. }
  249. return r;
  250. }