1
/* vi: set sw=4 ts=4: */
3
* hexdump implementation for busybox
4
* Based on code from util-linux v 2.11l
7
* The Regents of the University of California. All rights reserved.
9
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
11
//config:config HEXDUMP
12
//config: bool "hexdump (8.7 kb)"
15
//config: The hexdump utility is used to display binary data in a readable
16
//config: way that is comparable to the output from most hex editors.
19
//config: bool "hd (8.3 kb)"
22
//config: hd is an alias to hexdump -C.
24
//applet:IF_HEXDUMP(APPLET_NOEXEC(hexdump, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hexdump))
25
//applet:IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
27
//kbuild:lib-$(CONFIG_HEXDUMP) += hexdump.o
28
//kbuild:lib-$(CONFIG_HD) += hexdump.o
30
//usage:#define hexdump_trivial_usage
31
//usage: "[-bcdoxCv] [-e FMT] [-f FMT_FILE] [-n LEN] [-s OFS] [FILE]..."
32
//usage:#define hexdump_full_usage "\n\n"
33
//usage: "Display FILEs (or stdin) in a user specified format\n"
34
//usage: "\n -b 1-byte octal display"
35
//usage: "\n -c 1-byte character display"
36
//usage: "\n -d 2-byte decimal display"
37
//usage: "\n -o 2-byte octal display"
38
//usage: "\n -x 2-byte hex display"
39
//usage: "\n -C hex+ASCII 16 bytes per line"
40
//usage: "\n -v Show all (no dup folding)"
41
//usage: "\n -e FORMAT_STR Example: '16/1 \"%02x|\"\"\\n\"'"
42
//usage: "\n -f FORMAT_FILE"
43
// exactly the same help text lines in hexdump and xxd:
44
//usage: "\n -n LENGTH Show only first LENGTH bytes"
45
//usage: "\n -s OFFSET Skip OFFSET bytes"
47
//usage:#define hd_trivial_usage
49
//usage:#define hd_full_usage "\n\n"
50
//usage: "hd is an alias for hexdump -C"
55
/* This is a NOEXEC applet. Be very careful! */
57
static void bb_dump_addfile(dumper_t *dumper, char *name)
63
fp = xfopen_for_read(name);
64
while ((buf = xmalloc_fgetline(fp)) != NULL) {
65
p = skip_whitespace(buf);
66
if (*p && (*p != '#')) {
67
bb_dump_add(dumper, p);
74
static const char *const add_strings[] ALIGN_PTR = {
75
"16/1 \" %03o" , /* b */
76
"16/1 \" %3_c" , /* c */
77
"8/2 \" %05u" , /* d */
78
"8/2 \" %06o" , /* o */
79
"8/2 \" %04x", /* x */
82
static void add_format(dumper_t *dumper, const char *fmt)
84
char fmtbuf[sizeof("\"%07_ax\"" "%s\"" "\"\n\"") + 16];
85
sprintf(fmtbuf, "\"%%07_ax\"" "%s\"" "\"\n\"", fmt);
86
bb_dump_add(dumper, "\"%07_Ax\n\"");
87
bb_dump_add(dumper, fmtbuf);
90
static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v";
92
int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
93
int hexdump_main(int argc, char **argv)
95
dumper_t *dumper = alloc_dumper();
100
&& (!ENABLE_HEXDUMP || !applet_name[2])
101
) { /* we are "hd" */
106
/* We cannot use getopt32: in hexdump options are cumulative.
107
* E.g. "hexdump -C -C file" should dump each line twice */
108
while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
109
p = strchr(hexdump_opts, ch);
112
if ((p - hexdump_opts) < 5) {
113
add_format(dumper, add_strings[(int)(p - hexdump_opts)]);
115
/* Save a little bit of space below by omitting the 'else's. */
118
bb_dump_add(dumper, "\"%08_Ax\n\""); // final address line after dump
119
//------------------- "address " 8 * " xx" " " 8 * " xx"
120
bb_dump_add(dumper, "\"%08_ax \"8/1 \" %02x\"\" \"8/1 \" %02x\"");
121
//------------------- " |ASCII...........|\n"
122
bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
125
bb_dump_add(dumper, optarg);
128
bb_dump_addfile(dumper, optarg);
131
dumper->dump_length = xatoi_positive(optarg);
133
if (ch == 's') { /* compat: -s accepts hex numbers too */
134
dumper->dump_skip = xstrtoull_range_sfx(
137
/*lo:*/ 0, /*hi:*/ OFF_T_MAX,
142
dumper->dump_vflag = ALL;
146
if (!dumper->fshead) {
147
add_format(dumper, "8/2 \" %04x");
152
return bb_dump_dump(dumper, argv);