blob: 29bbc663341bedff0775c23ee92fa3b001c777e4 [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 Vlasenko5d553bc2020-12-13 19:18:28 +010044//usage: "[-pr] [-g N] [-c N] [-n LEN] [-s 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 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 Vlasenko32e1f692020-10-25 16:06:45 +010053//usage: "\n -r Reverse (with -p, assumes no offsets in input)"
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
Denys Vlasenko32e1f692020-10-25 16:06:45 +010060#define OPT_l (1 << 0)
61#define OPT_s (1 << 1)
62#define OPT_a (1 << 2)
63#define OPT_p (1 << 3)
64#define OPT_r (1 << 4)
65
66static void reverse(unsigned opt, unsigned cols, const char *filename)
67{
68 FILE *fp;
69 char *buf;
70
71 fp = filename ? xfopen_for_read(filename) : stdin;
72
73 while ((buf = xmalloc_fgetline(fp)) != NULL) {
74 char *p = buf;
75 unsigned cnt = cols;
76
77 if (!(opt & OPT_p)) {
78 /* skip address */
79 while (isxdigit(*p)) p++;
80 /* NB: for xxd -r, first hex portion is address even without colon */
81 /* If it's there, skip it: */
82 if (*p == ':') p++;
83
84//TODO: seek (or zero-pad if unseekable) to the address position
85//NOTE: -s SEEK value should be added to the address before seeking
86 }
87
88 /* Process hex bytes optionally separated by whitespace */
89 do {
90 uint8_t val, c;
91
92 p = skip_whitespace(p);
93
94 c = *p++;
95 if (isdigit(c))
96 val = c - '0';
97 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
98 val = (c|0x20) - ('a' - 10);
99 else
100 break;
101 val <<= 4;
102
103 /* Works the same with xxd V1.10:
104 * echo "31 09 32 0a" | xxd -r -p
105 * echo "31 0 9 32 0a" | xxd -r -p
106 * thus allow whitespace even within the byte:
107 */
108 p = skip_whitespace(p);
109
110 c = *p++;
111 if (isdigit(c))
112 val |= c - '0';
113 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
114 val |= (c|0x20) - ('a' - 10);
115 else
116 break;
117 putchar(val);
118 } while (!(opt & OPT_p) || --cnt != 0);
119 free(buf);
120 }
121 //fclose(fp);
122 fflush_stdout_and_exit(EXIT_SUCCESS);
123}
124
Denys Vlasenko0f436472017-01-25 01:58:00 +0100125int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
126int xxd_main(int argc UNUSED_PARAM, char **argv)
127{
128 char buf[80];
129 dumper_t *dumper;
130 char *opt_l, *opt_s;
131 unsigned bytes = 2;
132 unsigned cols = 0;
133 unsigned opt;
134
135 dumper = alloc_dumper();
136
Denys Vlasenko32e1f692020-10-25 16:06:45 +0100137 opt = getopt32(argv, "^" "l:s:aprg:+c:+" "\0" "?1" /* 1 argument max */,
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200138 &opt_l, &opt_s, &bytes, &cols
139 );
Denys Vlasenko0f436472017-01-25 01:58:00 +0100140 argv += optind;
141
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100142 dumper->dump_vflag = ALL;
Denys Vlasenko0f436472017-01-25 01:58:00 +0100143// if (opt & OPT_a)
144// dumper->dump_vflag = SKIPNUL; ..does not exist
145 if (opt & OPT_l) {
146 dumper->dump_length = xstrtou_range(
147 opt_l,
148 /*base:*/ 0,
149 /*lo:*/ 0, /*hi:*/ INT_MAX
150 );
151 }
152 if (opt & OPT_s) {
153 dumper->dump_skip = xstrtoull_range(
154 opt_s,
155 /*base:*/ 0,
156 /*lo:*/ 0, /*hi:*/ OFF_T_MAX
157 );
158 //BUGGY for /proc/version (unseekable?)
159 }
160
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100161 if (opt & OPT_p) {
162 if (cols == 0)
163 cols = 30;
164 bytes = cols; /* -p ignores -gN */
165 } else {
166 if (cols == 0)
167 cols = 16;
168 bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
169 }
170
Denys Vlasenko32e1f692020-10-25 16:06:45 +0100171 if (opt & OPT_r) {
172 reverse(opt, cols, argv[0]);
173 }
174
Denys Vlasenko0f436472017-01-25 01:58:00 +0100175 if (bytes < 1 || bytes >= cols) {
176 sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx"
177 bb_dump_add(dumper, buf);
178 }
179 else if (bytes == 1) {
180 sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx "
181 bb_dump_add(dumper, buf);
182 }
183 else {
184/* Format "print byte" with and without trailing space */
185#define BS "/1 \"%02x \""
186#define B "/1 \"%02x\""
187 unsigned i;
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100188 char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
Denys Vlasenko0f436472017-01-25 01:58:00 +0100189 char *p = bigbuf;
190 for (i = 1; i <= cols; i++) {
191 if (i == cols || i % bytes)
192 p = stpcpy(p, B);
193 else
194 p = stpcpy(p, BS);
195 }
196 // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx"
197 // todo: can be more clever and use
Denys Vlasenko7dd906a2017-01-25 17:00:38 +0100198 // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats
Denys Vlasenko0f436472017-01-25 01:58:00 +0100199 //bb_error_msg("ADDED:'%s'", bigbuf);
200 bb_dump_add(dumper, bigbuf);
201 free(bigbuf);
202 }
203
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100204 if (!(opt & OPT_p)) {
205 sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
206 bb_dump_add(dumper, buf);
Denys Vlasenko7dd906a2017-01-25 17:00:38 +0100207 } else {
208 bb_dump_add(dumper, "\"\n\"");
Denys Vlasenkodac5b832020-10-20 18:54:36 +0200209 dumper->eofstring = "\n";
Denys Vlasenko62a9b182017-01-25 16:50:30 +0100210 }
Denys Vlasenko0f436472017-01-25 01:58:00 +0100211
212 return bb_dump_dump(dumper, argv);
213}