blob: 719feaf4ef692713b558f69914af42b617346d36 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath60281112001-11-02 11:39:46 +00002/*
3 * hexdump implementation for busybox
4 * Based on code from util-linux v 2.11l
5 *
6 * Copyright (c) 1989
Denys Vlasenkofb132e42010-10-29 11:46:52 +02007 * The Regents of the University of California. All rights reserved.
Glenn L McGrath60281112001-11-02 11:39:46 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath60281112001-11-02 11:39:46 +000010 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config:config HEXDUMP
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020012//config: bool "hexdump (8.8 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013//config: default y
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//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.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010017//config:
18//config:config FEATURE_HEXDUMP_REVERSE
19//config: bool "Support -R, reverse of 'hexdump -Cv'"
20//config: default y
21//config: depends on HEXDUMP
22//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: The hexdump utility is used to display binary data in an ascii
24//config: readable way. This option creates binary data from an ascii input.
25//config: NB: this option is non-standard. It's unwise to use it in scripts
26//config: aimed to be portable.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010027//config:
28//config:config HD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020029//config: bool "hd (8 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010030//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010031//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020032//config: hd is an alias to hexdump -C.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010033
34//applet:IF_HEXDUMP(APPLET_NOEXEC(hexdump, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hexdump))
35//applet:IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
36
37//kbuild:lib-$(CONFIG_HEXDUMP) += hexdump.o
38//kbuild:lib-$(CONFIG_HD) += hexdump.o
Glenn L McGrath60281112001-11-02 11:39:46 +000039
Pere Orga5bc8c002011-04-11 03:29:49 +020040//usage:#define hexdump_trivial_usage
41//usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..."
42//usage:#define hexdump_full_usage "\n\n"
43//usage: "Display FILEs (or stdin) in a user specified format\n"
Denys Vlasenko62a9b182017-01-25 16:50:30 +010044//usage: "\n -b 1-byte octal display"
45//usage: "\n -c 1-byte character display"
46//usage: "\n -d 2-byte decimal display"
47//usage: "\n -o 2-byte octal display"
48//usage: "\n -x 2-byte hex display"
49//usage: "\n -C hex+ASCII 16 bytes per line"
50//usage: "\n -v Show all (no dup folding)"
51//usage: "\n -e FORMAT_STR Example: '16/1 \"%02x|\"\"\\n\"'"
Pere Orga5bc8c002011-04-11 03:29:49 +020052//usage: "\n -f FORMAT_FILE"
Denys Vlasenko0f436472017-01-25 01:58:00 +010053// exactly the same help text lines in hexdump and xxd:
Denys Vlasenko62a9b182017-01-25 16:50:30 +010054//usage: "\n -n LENGTH Show only first LENGTH bytes"
Pere Orga5bc8c002011-04-11 03:29:49 +020055//usage: "\n -s OFFSET Skip OFFSET bytes"
Pere Orga5bc8c002011-04-11 03:29:49 +020056//usage: IF_FEATURE_HEXDUMP_REVERSE(
57//usage: "\n -R Reverse of 'hexdump -Cv'")
Denys Vlasenko62a9b182017-01-25 16:50:30 +010058// TODO: NONCOMPAT!!! move -R to xxd -r
Pere Orga5bc8c002011-04-11 03:29:49 +020059//usage:
60//usage:#define hd_trivial_usage
61//usage: "FILE..."
62//usage:#define hd_full_usage "\n\n"
63//usage: "hd is an alias for hexdump -C"
64
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000065#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000066#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000067
Denis Vlasenko99912ca2007-04-10 15:43:37 +000068/* This is a NOEXEC applet. Be very careful! */
69
Denis Vlasenko55f79122008-07-16 11:00:16 +000070static void bb_dump_addfile(dumper_t *dumper, char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000071{
Denis Vlasenko084266e2008-07-26 23:08:31 +000072 char *p;
73 FILE *fp;
74 char *buf;
75
76 fp = xfopen_for_read(name);
77 while ((buf = xmalloc_fgetline(fp)) != NULL) {
78 p = skip_whitespace(buf);
79 if (*p && (*p != '#')) {
80 bb_dump_add(dumper, p);
81 }
82 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000083 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000084 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000085}
86
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000087static const char *const add_strings[] = {
Denys Vlasenko0f436472017-01-25 01:58:00 +010088 "\"%07.7_ax \"16/1 \"%03o \"\"\n\"", /* b */
89 "\"%07.7_ax \"16/1 \"%3_c \"\"\n\"", /* c */
90 "\"%07.7_ax \"8/2 \" %05u \"\"\n\"", /* d */
91 "\"%07.7_ax \"8/2 \" %06o \"\"\n\"", /* o */
92 "\"%07.7_ax \"8/2 \" %04x \"\"\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000093};
94
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000095static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000096
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000097static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000098
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000099int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +0000100int hexdump_main(int argc, char **argv)
101{
Denis Vlasenko55f79122008-07-16 11:00:16 +0000102 dumper_t *dumper = alloc_dumper();
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +0000104 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000105#if ENABLE_FEATURE_HEXDUMP_REVERSE
106 FILE *fp;
107 smallint rdump = 0;
108#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000109
Denys Vlasenko5b966c62016-11-23 11:53:12 +0100110 if (ENABLE_HD
111 && (!ENABLE_HEXDUMP || !applet_name[2])
112 ) { /* we are "hd" */
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000113 ch = 'C';
114 goto hd_applet;
115 }
116
117 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000118 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +0000120 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000121 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000123 if ((p - hexdump_opts) < 5) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000124 bb_dump_add(dumper, add_first);
125 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000126 }
127 /* Save a little bit of space below by omitting the 'else's. */
128 if (ch == 'C') {
129 hd_applet:
Denys Vlasenko0f436472017-01-25 01:58:00 +0100130 bb_dump_add(dumper, "\"%08.8_Ax\n\""); // final address line after dump
Denys Vlasenko8a2657c2017-01-25 03:07:39 +0100131 //------------------- "address " 8 * "xx " " " 8 * "xx "
132 bb_dump_add(dumper, "\"%08.8_ax \"8/1 \"%02x \"\" \"8/1 \"%02x \"");
Denys Vlasenko0f436472017-01-25 01:58:00 +0100133 //------------------- " |ASCII...........|\n"
134 bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000135 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000136 if (ch == 'e') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000137 bb_dump_add(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000138 } /* else */
139 if (ch == 'f') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000140 bb_dump_addfile(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000141 } /* else */
142 if (ch == 'n') {
Denys Vlasenko77832482010-08-12 14:14:45 +0200143 dumper->dump_length = xatoi_positive(optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000144 } /* else */
Denys Vlasenko1854bc12010-04-06 23:33:28 +0200145 if (ch == 's') { /* compat: -s accepts hex numbers too */
Denys Vlasenko2c248062013-03-27 15:18:32 +0100146 dumper->dump_skip = xstrtoull_range_sfx(
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100147 optarg,
148 /*base:*/ 0,
149 /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +0200150 bkm_suffixes
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100151 );
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000152 } /* else */
153 if (ch == 'v') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000154 dumper->dump_vflag = ALL;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000155 }
156#if ENABLE_FEATURE_HEXDUMP_REVERSE
157 if (ch == 'R') {
158 rdump = 1;
159 }
160#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000161 }
162
Denis Vlasenko55f79122008-07-16 11:00:16 +0000163 if (!dumper->fshead) {
164 bb_dump_add(dumper, add_first);
Denys Vlasenko0f436472017-01-25 01:58:00 +0100165 bb_dump_add(dumper, "\"%07.7_ax \"8/2 \"%04x \"\"\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000166 }
167
168 argv += optind;
169
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000170#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko55f79122008-07-16 11:00:16 +0000171 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000172#else
173 if (!rdump) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000174 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000175 }
176
177 /* -R: reverse of 'hexdump -Cv' */
178 fp = stdin;
179 if (!*argv) {
180 argv--;
181 goto jump_in;
182 }
183
184 do {
185 char *buf;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000186 fp = xfopen_for_read(*argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000187 jump_in:
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000188 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000189 p = buf;
190 while (1) {
191 /* skip address or previous byte */
192 while (isxdigit(*p)) p++;
193 while (*p == ' ') p++;
194 /* '|' char will break the line */
195 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
196 break;
197 putchar(ch);
198 }
199 free(buf);
200 }
201 fclose(fp);
202 } while (*++argv);
203
204 fflush_stdout_and_exit(EXIT_SUCCESS);
205#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000206}