blob: aa215569f93b5cff2bed4ef8306f5b11e8176f43 [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "xxd (8.9 kb)"
Denys Vlasenko0f436472017-01-25 01:58:00 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//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.
Denys Vlasenko0f436472017-01-25 01:58:00 +010015
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
Denys Vlasenko2c436672021-06-18 00:59:17 +020044//usage: "[-pri] [-g N] [-c N] [-n LEN] [-s OFS] [-o OFS] [FILE]"
Denys Vlasenko0f436472017-01-25 01:58:00 +010045//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 Vlasenko2c436672021-06-18 00:59:17 +020050//usage: "\n -i C include file style"
Denys Vlasenko0f436472017-01-25 01:58:00 +010051// exactly the same help text lines in hexdump and xxd:
Denys Vlasenko62a9b182017-01-25 16:50:30 +010052//usage: "\n -l LENGTH Show only first LENGTH bytes"
Denys Vlasenko0f436472017-01-25 01:58:00 +010053//usage: "\n -s OFFSET Skip OFFSET bytes"
Denys Vlasenko4d161612021-06-17 23:53:30 +020054//usage: "\n -o OFFSET Add OFFSET to displayed offset"
Denys Vlasenko32e1f692020-10-25 16:06:45 +010055//usage: "\n -r Reverse (with -p, assumes no offsets in input)"
Denys Vlasenko0f436472017-01-25 01:58:00 +010056
57#include "libbb.h"
58#include "dump.h"
59
60/* This is a NOEXEC applet. Be very careful! */
61
Denys Vlasenko32e1f692020-10-25 16:06:45 +010062#define OPT_l (1 << 0)
63#define OPT_s (1 << 1)
64#define OPT_a (1 << 2)
65#define OPT_p (1 << 3)
Denys Vlasenko2c436672021-06-18 00:59:17 +020066#define OPT_i (1 << 4)
67#define OPT_r (1 << 5)
68#define OPT_g (1 << 6)
69#define OPT_c (1 << 7)
70#define OPT_o (1 << 8)
Denys Vlasenko32e1f692020-10-25 16:06:45 +010071
72static void reverse(unsigned opt, unsigned cols, const char *filename)
73{
74 FILE *fp;
75 char *buf;
76
77 fp = filename ? xfopen_for_read(filename) : stdin;
78
79 while ((buf = xmalloc_fgetline(fp)) != NULL) {
80 char *p = buf;
81 unsigned cnt = cols;
82
83 if (!(opt & OPT_p)) {
84 /* skip address */
85 while (isxdigit(*p)) p++;
86 /* NB: for xxd -r, first hex portion is address even without colon */
87 /* If it's there, skip it: */
88 if (*p == ':') p++;
89
90//TODO: seek (or zero-pad if unseekable) to the address position
91//NOTE: -s SEEK value should be added to the address before seeking
92 }
93
94 /* Process hex bytes optionally separated by whitespace */
95 do {
96 uint8_t val, c;
97
98 p = skip_whitespace(p);
99
100 c = *p++;
101 if (isdigit(c))
102 val = c - '0';
103 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
104 val = (c|0x20) - ('a' - 10);
105 else
106 break;
107 val <<= 4;
108
109 /* Works the same with xxd V1.10:
110 * echo "31 09 32 0a" | xxd -r -p
111 * echo "31 0 9 32 0a" | xxd -r -p
112 * thus allow whitespace even within the byte:
113 */
114 p = skip_whitespace(p);
115
116 c = *p++;
117 if (isdigit(c))
118 val |= c - '0';
119 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
120 val |= (c|0x20) - ('a' - 10);
121 else
122 break;
123 putchar(val);
124 } while (!(opt & OPT_p) || --cnt != 0);
125 free(buf);
126 }
127 //fclose(fp);
128 fflush_stdout_and_exit(EXIT_SUCCESS);
129}
130
Denys Vlasenko2c436672021-06-18 00:59:17 +0200131static void print_C_style(const char *p, const char *hdr)
132{
133 printf(hdr, isdigit(p[0]) ? "__" : "");
134 while (*p) {
135 bb_putchar(isalnum(*p) ? *p : '_');
136 p++;
137 }
138}
139
Denys Vlasenko0f436472017-01-25 01:58:00 +0100140int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
141int xxd_main(int argc UNUSED_PARAM, char **argv)
142{
143 char buf[80];
144 dumper_t *dumper;
Denys Vlasenko4d161612021-06-17 23:53:30 +0200145 char *opt_l, *opt_s, *opt_o;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100146 unsigned bytes = 2;
147 unsigned cols = 0;
148 unsigned opt;
Denys Vlasenko2c436672021-06-18 00:59:17 +0200149 int r;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100150
151 dumper = alloc_dumper();
152
Denys Vlasenko2c436672021-06-18 00:59:17 +0200153 opt = getopt32(argv, "^" "l:s:apirg:+c:+o:" "\0" "?1" /* 1 argument max */,
Denys Vlasenko4d161612021-06-17 23:53:30 +0200154 &opt_l, &opt_s, &bytes, &cols, &opt_o
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200155 );
Denys Vlasenko0f436472017-01-25 01:58:00 +0100156 argv += optind;
157
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100158 dumper->dump_vflag = ALL;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100159// if (opt & OPT_a)
160// dumper->dump_vflag = SKIPNUL; ..does not exist
161 if (opt & OPT_l) {
162 dumper->dump_length = xstrtou_range(
163 opt_l,
164 /*base:*/ 0,
165 /*lo:*/ 0, /*hi:*/ INT_MAX
166 );
167 }
168 if (opt & OPT_s) {
169 dumper->dump_skip = xstrtoull_range(
170 opt_s,
171 /*base:*/ 0,
172 /*lo:*/ 0, /*hi:*/ OFF_T_MAX
173 );
174 //BUGGY for /proc/version (unseekable?)
175 }
176
Denys Vlasenko4d161612021-06-17 23:53:30 +0200177 if (opt & OPT_o) {
178 /* -o accepts negative numbers too */
179 dumper->xxd_displayoff = xstrtoll(opt_o, /*base:*/ 0);
180 }
181
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100182 if (opt & OPT_p) {
183 if (cols == 0)
184 cols = 30;
185 bytes = cols; /* -p ignores -gN */
186 } else {
187 if (cols == 0)
Denys Vlasenko2c436672021-06-18 00:59:17 +0200188 cols = (opt & OPT_i) ? 12 : 16;
189 if (opt & OPT_i)
190 bytes = 1; /* -i ignores -gN */
191 else
192 bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100193 }
194
Denys Vlasenko32e1f692020-10-25 16:06:45 +0100195 if (opt & OPT_r) {
196 reverse(opt, cols, argv[0]);
197 }
198
Denys Vlasenko0f436472017-01-25 01:58:00 +0100199 if (bytes < 1 || bytes >= cols) {
200 sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx"
201 bb_dump_add(dumper, buf);
202 }
203 else if (bytes == 1) {
Denys Vlasenko2c436672021-06-18 00:59:17 +0200204 if (opt & OPT_i)
205 sprintf(buf, "%u/1 \" 0x%%02x,\"", cols); // cols * " 0xxx,"
206 else
207 sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx "
Denys Vlasenko0f436472017-01-25 01:58:00 +0100208 bb_dump_add(dumper, buf);
209 }
210 else {
211/* Format "print byte" with and without trailing space */
212#define BS "/1 \"%02x \""
213#define B "/1 \"%02x\""
214 unsigned i;
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100215 char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
Denys Vlasenko0f436472017-01-25 01:58:00 +0100216 char *p = bigbuf;
217 for (i = 1; i <= cols; i++) {
218 if (i == cols || i % bytes)
219 p = stpcpy(p, B);
220 else
221 p = stpcpy(p, BS);
222 }
223 // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx"
224 // todo: can be more clever and use
Denys Vlasenko7dd906a2017-01-25 17:00:38 +0100225 // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats
Denys Vlasenko0f436472017-01-25 01:58:00 +0100226 //bb_error_msg("ADDED:'%s'", bigbuf);
227 bb_dump_add(dumper, bigbuf);
228 free(bigbuf);
229 }
230
Denys Vlasenko2c436672021-06-18 00:59:17 +0200231 if (!(opt & (OPT_p|OPT_i))) {
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100232 sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
233 bb_dump_add(dumper, buf);
Denys Vlasenko7dd906a2017-01-25 17:00:38 +0100234 } else {
235 bb_dump_add(dumper, "\"\n\"");
Denys Vlasenko4d161612021-06-17 23:53:30 +0200236 dumper->xxd_eofstring = "\n";
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100237 }
Denys Vlasenko0f436472017-01-25 01:58:00 +0100238
Denys Vlasenko2c436672021-06-18 00:59:17 +0200239 if ((opt & OPT_i) && argv[0]) {
240 print_C_style(argv[0], "unsigned char %s");
241 printf("[] = {\n");
242 }
243 r = bb_dump_dump(dumper, argv);
244 if (r == 0 && (opt & OPT_i) && argv[0]) {
245 print_C_style(argv[0], "};\nunsigned int %s");
246 printf("_len = %"OFF_FMT"u;\n", dumper->address);
247 }
248 return r;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100249}