blob: be4b4f3543ed5bbae8c4e19b424e84a6e53e19c6 [file] [log] [blame]
Denys Vlasenko0f436472017-01-25 01:58:00 +01001/* 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"
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
16//applet:IF_XXD(APPLET_NOEXEC(xxd, xxd, BB_DIR_USR_BIN, BB_SUID_DROP, xxd))
17
18//kbuild:lib-$(CONFIG_XXD) += hexdump_xxd.o
19
20// $ xxd --version
21// xxd V1.10 27oct98 by Juergen Weigert
22// $ xxd --help
23// Usage:
24// xxd [options] [infile [outfile]]
25// or
26// xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
27// Options:
28// -a toggle autoskip: A single '*' replaces nul-lines. Default off.
29// -b binary digit dump (incompatible with -ps,-i,-r). Default hex.
30// -c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
31// -E show characters in EBCDIC. Default ASCII.
32// -e little-endian dump (incompatible with -ps,-i,-r).
33// -g number of octets per group in normal output. Default 2 (-e: 4).
34// -i output in C include file style.
35// -l len stop after <len> octets.
36// -o off add <off> to the displayed file position.
37// -ps output in postscript plain hexdump style.
38// -r reverse operation: convert (or patch) hexdump into binary.
39// -r -s off revert with <off> added to file positions found in hexdump.
40// -s [+][-]seek start at <seek> bytes abs. (or +: rel.) infile offset.
41// -u use upper case hex letters.
42
43//usage:#define xxd_trivial_usage
44//usage: "[OPTIONS] [FILE]"
45//usage:#define xxd_full_usage "\n\n"
46//usage: "Hex dump FILE (or stdin)\n"
47//usage: "\n -g N Bytes per group"
48//usage: "\n -c N Bytes per line"
Denys Vlasenko62a9b182017-01-25 16:50:30 +010049//usage: "\n -p Show only hex bytes, assumes -c30"
Denys Vlasenko0f436472017-01-25 01:58:00 +010050// exactly the same help text lines in hexdump and xxd:
Denys Vlasenko62a9b182017-01-25 16:50:30 +010051//usage: "\n -l LENGTH Show only first LENGTH bytes"
Denys Vlasenko0f436472017-01-25 01:58:00 +010052//usage: "\n -s OFFSET Skip OFFSET bytes"
Denys Vlasenko62a9b182017-01-25 16:50:30 +010053// TODO: implement -r (see hexdump -R)
Denys Vlasenko0f436472017-01-25 01:58:00 +010054
55#include "libbb.h"
56#include "dump.h"
57
58/* This is a NOEXEC applet. Be very careful! */
59
60int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
61int xxd_main(int argc UNUSED_PARAM, char **argv)
62{
63 char buf[80];
64 dumper_t *dumper;
65 char *opt_l, *opt_s;
66 unsigned bytes = 2;
67 unsigned cols = 0;
68 unsigned opt;
69
70 dumper = alloc_dumper();
71
72#define OPT_l (1 << 0)
73#define OPT_s (1 << 1)
74#define OPT_a (1 << 2)
Denys Vlasenko62a9b182017-01-25 16:50:30 +010075#define OPT_p (1 << 3)
Denys Vlasenko0f436472017-01-25 01:58:00 +010076 opt_complementary = "?1"; /* 1 argument max */
Denys Vlasenko62a9b182017-01-25 16:50:30 +010077 opt = getopt32(argv, "l:s:apg:+c:+", &opt_l, &opt_s, &bytes, &cols);
Denys Vlasenko0f436472017-01-25 01:58:00 +010078 argv += optind;
79
Denys Vlasenko62a9b182017-01-25 16:50:30 +010080 dumper->dump_vflag = ALL;
Denys Vlasenko0f436472017-01-25 01:58:00 +010081// if (opt & OPT_a)
82// dumper->dump_vflag = SKIPNUL; ..does not exist
83 if (opt & OPT_l) {
84 dumper->dump_length = xstrtou_range(
85 opt_l,
86 /*base:*/ 0,
87 /*lo:*/ 0, /*hi:*/ INT_MAX
88 );
89 }
90 if (opt & OPT_s) {
91 dumper->dump_skip = xstrtoull_range(
92 opt_s,
93 /*base:*/ 0,
94 /*lo:*/ 0, /*hi:*/ OFF_T_MAX
95 );
96 //BUGGY for /proc/version (unseekable?)
97 }
98
Denys Vlasenko62a9b182017-01-25 16:50:30 +010099 if (opt & OPT_p) {
100 if (cols == 0)
101 cols = 30;
102 bytes = cols; /* -p ignores -gN */
103 } else {
104 if (cols == 0)
105 cols = 16;
106 bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
107 }
108
Denys Vlasenko0f436472017-01-25 01:58:00 +0100109 if (bytes < 1 || bytes >= cols) {
110 sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx"
111 bb_dump_add(dumper, buf);
112 }
113 else if (bytes == 1) {
114 sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx "
115 bb_dump_add(dumper, buf);
116 }
117 else {
118/* Format "print byte" with and without trailing space */
119#define BS "/1 \"%02x \""
120#define B "/1 \"%02x\""
121 unsigned i;
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100122 char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
Denys Vlasenko0f436472017-01-25 01:58:00 +0100123 char *p = bigbuf;
124 for (i = 1; i <= cols; i++) {
125 if (i == cols || i % bytes)
126 p = stpcpy(p, B);
127 else
128 p = stpcpy(p, BS);
129 }
130 // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx"
131 // todo: can be more clever and use
Denys Vlasenko7dd906a2017-01-25 17:00:38 +0100132 // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats
Denys Vlasenko0f436472017-01-25 01:58:00 +0100133 //bb_error_msg("ADDED:'%s'", bigbuf);
134 bb_dump_add(dumper, bigbuf);
135 free(bigbuf);
136 }
137
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100138 if (!(opt & OPT_p)) {
139 sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
140 bb_dump_add(dumper, buf);
Denys Vlasenko7dd906a2017-01-25 17:00:38 +0100141 } else {
142 bb_dump_add(dumper, "\"\n\"");
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100143 }
Denys Vlasenko0f436472017-01-25 01:58:00 +0100144
145 return bb_dump_dump(dumper, argv);
146}