tls: in AES-GCM decoding, avoid memmove
[oweals/busybox.git] / util-linux / hexdump_xxd.c
index 813e7fe57793ad6f429bf1f6d1d03511b141cb82..6cf6d029789f6e2bdd2415035c9a6d15c22ccb54 100644 (file)
@@ -7,11 +7,11 @@
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 //config:config XXD
-//config:      bool "xxd"
+//config:      bool "xxd (8.9 kb)"
 //config:      default y
 //config:      help
-//config:        The xxd utility is used to display binary data in a readable
-//config:        way that is comparable to the output from most hex editors.
+//config:      The xxd utility is used to display binary data in a readable
+//config:      way that is comparable to the output from most hex editors.
 
 //applet:IF_XXD(APPLET_NOEXEC(xxd, xxd, BB_DIR_USR_BIN, BB_SUID_DROP, xxd))
 
 //usage:       "Hex dump FILE (or stdin)\n"
 //usage:     "\n       -g N            Bytes per group"
 //usage:     "\n       -c N            Bytes per line"
+//usage:     "\n       -p              Show only hex bytes, assumes -c30"
 // exactly the same help text lines in hexdump and xxd:
-//usage:     "\n       -l LENGTH       Interpret only LENGTH bytes of input"
+//usage:     "\n       -l LENGTH       Show only first LENGTH bytes"
 //usage:     "\n       -s OFFSET       Skip OFFSET bytes"
+// TODO: implement -r (see hexdump -R)
 
 #include "libbb.h"
 #include "dump.h"
@@ -70,11 +72,13 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
 #define OPT_l (1 << 0)
 #define OPT_s (1 << 1)
 #define OPT_a (1 << 2)
-       opt_complementary = "?1"; /* 1 argument max */
-       opt = getopt32(argv, "l:s:ag:+c:+", &opt_l, &opt_s, &bytes, &cols);
+#define OPT_p (1 << 3)
+       opt = getopt32(argv, "^" "l:s:apg:+c:+" "\0" "?1" /* 1 argument max */,
+                       &opt_l, &opt_s, &bytes, &cols
+       );
        argv += optind;
 
-//     dumper->dump_vflag = ALL; // default
+       dumper->dump_vflag = ALL;
 //     if (opt & OPT_a)
 //             dumper->dump_vflag = SKIPNUL; ..does not exist
        if (opt & OPT_l) {
@@ -93,9 +97,16 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
                //BUGGY for /proc/version (unseekable?)
        }
 
-       bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
-       if (cols == 0)
-               cols = 16;
+       if (opt & OPT_p) {
+               if (cols == 0)
+                       cols = 30;
+               bytes = cols; /* -p ignores -gN */
+       } else {
+               if (cols == 0)
+                       cols = 16;
+               bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
+       }
+
        if (bytes < 1 || bytes >= cols) {
                sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx"
                bb_dump_add(dumper, buf);
@@ -109,7 +120,7 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
 #define BS "/1 \"%02x \""
 #define B  "/1 \"%02x\""
                unsigned i;
-               char *bigbuf = xmalloc(1 + cols * (sizeof(BS)-1));
+               char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
                char *p = bigbuf;
                for (i = 1; i <= cols; i++) {
                        if (i == cols || i % bytes)
@@ -119,14 +130,18 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
                }
                // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx"
                // todo: can be more clever and use
-               // one "cols-1/B" format instead of many "B B B..." formats
+               // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats
                //bb_error_msg("ADDED:'%s'", bigbuf);
                bb_dump_add(dumper, bigbuf);
                free(bigbuf);
        }
 
-       sprintf(buf, "\"  \"%u/1 \"%%_p\"\"\n\"", cols); // "  ASCII\n"
-       bb_dump_add(dumper, buf);
+       if (!(opt & OPT_p)) {
+               sprintf(buf, "\"  \"%u/1 \"%%_p\"\"\n\"", cols); // "  ASCII\n"
+               bb_dump_add(dumper, buf);
+       } else {
+               bb_dump_add(dumper, "\"\n\"");
+       }
 
        return bb_dump_dump(dumper, argv);
 }