blob: 5e1ef69383fd19b3551cb324e90c7bb0a9c75d51 [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
12//config: bool "hexdump"
13//config: default y
14//config: help
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.
17//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
23//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.
27//config:
28//config:config HD
29//config: bool "hd"
30//config: default y
31//config: depends on HEXDUMP
32//config: help
33//config: hd is an alias to hexdump -C.
34
35//applet:IF_HEXDUMP(APPLET_NOEXEC(hexdump, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hexdump))
36//applet:IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
37
38//kbuild:lib-$(CONFIG_HEXDUMP) += hexdump.o
39//kbuild:lib-$(CONFIG_HD) += hexdump.o
Glenn L McGrath60281112001-11-02 11:39:46 +000040
Pere Orga5bc8c002011-04-11 03:29:49 +020041//usage:#define hexdump_trivial_usage
42//usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..."
43//usage:#define hexdump_full_usage "\n\n"
44//usage: "Display FILEs (or stdin) in a user specified format\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020045//usage: "\n -b One-byte octal display"
46//usage: "\n -c One-byte character display"
47//usage: "\n -C Canonical hex+ASCII, 16 bytes per line"
48//usage: "\n -d Two-byte decimal display"
49//usage: "\n -e FORMAT_STRING"
50//usage: "\n -f FORMAT_FILE"
51//usage: "\n -n LENGTH Interpret only LENGTH bytes of input"
52//usage: "\n -o Two-byte octal display"
53//usage: "\n -s OFFSET Skip OFFSET bytes"
54//usage: "\n -v Display all input data"
55//usage: "\n -x Two-byte hexadecimal display"
56//usage: IF_FEATURE_HEXDUMP_REVERSE(
57//usage: "\n -R Reverse of 'hexdump -Cv'")
58//usage:
59//usage:#define hd_trivial_usage
60//usage: "FILE..."
61//usage:#define hd_full_usage "\n\n"
62//usage: "hd is an alias for hexdump -C"
63
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000064#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000065#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000066
Denis Vlasenko99912ca2007-04-10 15:43:37 +000067/* This is a NOEXEC applet. Be very careful! */
68
Denis Vlasenko55f79122008-07-16 11:00:16 +000069static void bb_dump_addfile(dumper_t *dumper, char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000070{
Denis Vlasenko084266e2008-07-26 23:08:31 +000071 char *p;
72 FILE *fp;
73 char *buf;
74
75 fp = xfopen_for_read(name);
76 while ((buf = xmalloc_fgetline(fp)) != NULL) {
77 p = skip_whitespace(buf);
78 if (*p && (*p != '#')) {
79 bb_dump_add(dumper, p);
80 }
81 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000082 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000083 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000084}
85
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000086static const char *const add_strings[] = {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020087 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
88 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
89 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
90 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
91 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000092};
93
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000094static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000095
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000096static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000097
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000098int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000099int hexdump_main(int argc, char **argv)
100{
Denis Vlasenko55f79122008-07-16 11:00:16 +0000101 dumper_t *dumper = alloc_dumper();
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +0000103 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000104#if ENABLE_FEATURE_HEXDUMP_REVERSE
105 FILE *fp;
106 smallint rdump = 0;
107#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000108
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000109 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
110 ch = 'C';
111 goto hd_applet;
112 }
113
114 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000115 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +0000117 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000118 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000120 if ((p - hexdump_opts) < 5) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000121 bb_dump_add(dumper, add_first);
122 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000123 }
124 /* Save a little bit of space below by omitting the 'else's. */
125 if (ch == 'C') {
126 hd_applet:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000127 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
128 bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
129 bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000130 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000131 if (ch == 'e') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000132 bb_dump_add(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000133 } /* else */
134 if (ch == 'f') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000135 bb_dump_addfile(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000136 } /* else */
137 if (ch == 'n') {
Denys Vlasenko77832482010-08-12 14:14:45 +0200138 dumper->dump_length = xatoi_positive(optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000139 } /* else */
Denys Vlasenko1854bc12010-04-06 23:33:28 +0200140 if (ch == 's') { /* compat: -s accepts hex numbers too */
Denys Vlasenko2c248062013-03-27 15:18:32 +0100141 dumper->dump_skip = xstrtoull_range_sfx(
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100142 optarg,
143 /*base:*/ 0,
144 /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +0200145 bkm_suffixes
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100146 );
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000147 } /* else */
148 if (ch == 'v') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000149 dumper->dump_vflag = ALL;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000150 }
151#if ENABLE_FEATURE_HEXDUMP_REVERSE
152 if (ch == 'R') {
153 rdump = 1;
154 }
155#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000156 }
157
Denis Vlasenko55f79122008-07-16 11:00:16 +0000158 if (!dumper->fshead) {
159 bb_dump_add(dumper, add_first);
160 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000161 }
162
163 argv += optind;
164
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000165#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko55f79122008-07-16 11:00:16 +0000166 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000167#else
168 if (!rdump) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000169 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000170 }
171
172 /* -R: reverse of 'hexdump -Cv' */
173 fp = stdin;
174 if (!*argv) {
175 argv--;
176 goto jump_in;
177 }
178
179 do {
180 char *buf;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000181 fp = xfopen_for_read(*argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000182 jump_in:
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000183 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000184 p = buf;
185 while (1) {
186 /* skip address or previous byte */
187 while (isxdigit(*p)) p++;
188 while (*p == ' ') p++;
189 /* '|' char will break the line */
190 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
191 break;
192 putchar(ch);
193 }
194 free(buf);
195 }
196 fclose(fp);
197 } while (*++argv);
198
199 fflush_stdout_and_exit(EXIT_SUCCESS);
200#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000201}