xxd: implement -i "C style output"

function                                             old     new   delta
xxd_main                                             710     888    +178
.rodata                                           103252  103331     +79
print_C_style                                          -      78     +78
packed_usage                                       33639   33652     +13
next                                                 276     278      +2
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 4/0 up/down: 350/0)             Total: 350 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2021-06-18 00:59:17 +02:00
parent 25fe2d50bd
commit 2c436679fb
3 changed files with 53 additions and 26 deletions

View File

@@ -41,12 +41,13 @@
// -u use upper case hex letters.
//usage:#define xxd_trivial_usage
//usage: "[-pr] [-g N] [-c N] [-n LEN] [-s OFS] [-o OFS] [FILE]"
//usage: "[-pri] [-g N] [-c N] [-n LEN] [-s OFS] [-o OFS] [FILE]"
//usage:#define xxd_full_usage "\n\n"
//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"
//usage: "\n -i C include file style"
// exactly the same help text lines in hexdump and xxd:
//usage: "\n -l LENGTH Show only first LENGTH bytes"
//usage: "\n -s OFFSET Skip OFFSET bytes"
@@ -62,10 +63,11 @@
#define OPT_s (1 << 1)
#define OPT_a (1 << 2)
#define OPT_p (1 << 3)
#define OPT_r (1 << 4)
#define OPT_g (1 << 5)
#define OPT_c (1 << 6)
#define OPT_o (1 << 7)
#define OPT_i (1 << 4)
#define OPT_r (1 << 5)
#define OPT_g (1 << 6)
#define OPT_c (1 << 7)
#define OPT_o (1 << 8)
static void reverse(unsigned opt, unsigned cols, const char *filename)
{
@@ -126,6 +128,15 @@ static void reverse(unsigned opt, unsigned cols, const char *filename)
fflush_stdout_and_exit(EXIT_SUCCESS);
}
static void print_C_style(const char *p, const char *hdr)
{
printf(hdr, isdigit(p[0]) ? "__" : "");
while (*p) {
bb_putchar(isalnum(*p) ? *p : '_');
p++;
}
}
int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int xxd_main(int argc UNUSED_PARAM, char **argv)
{
@@ -135,10 +146,11 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
unsigned bytes = 2;
unsigned cols = 0;
unsigned opt;
int r;
dumper = alloc_dumper();
opt = getopt32(argv, "^" "l:s:aprg:+c:+o:" "\0" "?1" /* 1 argument max */,
opt = getopt32(argv, "^" "l:s:apirg:+c:+o:" "\0" "?1" /* 1 argument max */,
&opt_l, &opt_s, &bytes, &cols, &opt_o
);
argv += optind;
@@ -173,8 +185,11 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
bytes = cols; /* -p ignores -gN */
} else {
if (cols == 0)
cols = 16;
bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
cols = (opt & OPT_i) ? 12 : 16;
if (opt & OPT_i)
bytes = 1; /* -i ignores -gN */
else
bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
}
if (opt & OPT_r) {
@@ -186,7 +201,10 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
bb_dump_add(dumper, buf);
}
else if (bytes == 1) {
sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx "
if (opt & OPT_i)
sprintf(buf, "%u/1 \" 0x%%02x,\"", cols); // cols * " 0xxx,"
else
sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx "
bb_dump_add(dumper, buf);
}
else {
@@ -210,7 +228,7 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
free(bigbuf);
}
if (!(opt & OPT_p)) {
if (!(opt & (OPT_p|OPT_i))) {
sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
bb_dump_add(dumper, buf);
} else {
@@ -218,5 +236,14 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
dumper->xxd_eofstring = "\n";
}
return bb_dump_dump(dumper, argv);
if ((opt & OPT_i) && argv[0]) {
print_C_style(argv[0], "unsigned char %s");
printf("[] = {\n");
}
r = bb_dump_dump(dumper, argv);
if (r == 0 && (opt & OPT_i) && argv[0]) {
print_C_style(argv[0], "};\nunsigned int %s");
printf("_len = %"OFF_FMT"u;\n", dumper->address);
}
return r;
}