blob: 1f5d57d74878a57f503192efea16cf023781e61e [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 */
11
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#define hexdump_trivial_usage
13//usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..."
14//usage:#define hexdump_full_usage "\n\n"
15//usage: "Display FILEs (or stdin) in a user specified format\n"
16//usage: "\nOptions:"
17//usage: "\n -b One-byte octal display"
18//usage: "\n -c One-byte character display"
19//usage: "\n -C Canonical hex+ASCII, 16 bytes per line"
20//usage: "\n -d Two-byte decimal display"
21//usage: "\n -e FORMAT_STRING"
22//usage: "\n -f FORMAT_FILE"
23//usage: "\n -n LENGTH Interpret only LENGTH bytes of input"
24//usage: "\n -o Two-byte octal display"
25//usage: "\n -s OFFSET Skip OFFSET bytes"
26//usage: "\n -v Display all input data"
27//usage: "\n -x Two-byte hexadecimal display"
28//usage: IF_FEATURE_HEXDUMP_REVERSE(
29//usage: "\n -R Reverse of 'hexdump -Cv'")
30//usage:
31//usage:#define hd_trivial_usage
32//usage: "FILE..."
33//usage:#define hd_full_usage "\n\n"
34//usage: "hd is an alias for hexdump -C"
35
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000036#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000037#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000038
Denis Vlasenko99912ca2007-04-10 15:43:37 +000039/* This is a NOEXEC applet. Be very careful! */
40
Denis Vlasenko55f79122008-07-16 11:00:16 +000041static void bb_dump_addfile(dumper_t *dumper, char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000042{
Denis Vlasenko084266e2008-07-26 23:08:31 +000043 char *p;
44 FILE *fp;
45 char *buf;
46
47 fp = xfopen_for_read(name);
48 while ((buf = xmalloc_fgetline(fp)) != NULL) {
49 p = skip_whitespace(buf);
50 if (*p && (*p != '#')) {
51 bb_dump_add(dumper, p);
52 }
53 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000054 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000055 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000056}
57
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000058static const char *const add_strings[] = {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020059 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
60 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
61 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
62 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
63 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000064};
65
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000066static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000067
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000068static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000069
70static const struct suffix_mult suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +000071 { "b", 512 },
72 { "k", 1024 },
73 { "m", 1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020074 { "", 0 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000075};
76
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000077int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000078int hexdump_main(int argc, char **argv)
79{
Denis Vlasenko55f79122008-07-16 11:00:16 +000080 dumper_t *dumper = alloc_dumper();
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000082 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000083#if ENABLE_FEATURE_HEXDUMP_REVERSE
84 FILE *fp;
85 smallint rdump = 0;
86#endif
Glenn L McGrath60281112001-11-02 11:39:46 +000087
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000088 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
89 ch = 'C';
90 goto hd_applet;
91 }
92
93 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000094 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +000096 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000097 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000099 if ((p - hexdump_opts) < 5) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000100 bb_dump_add(dumper, add_first);
101 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000102 }
103 /* Save a little bit of space below by omitting the 'else's. */
104 if (ch == 'C') {
105 hd_applet:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000106 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
107 bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
108 bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000109 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000110 if (ch == 'e') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000111 bb_dump_add(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000112 } /* else */
113 if (ch == 'f') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000114 bb_dump_addfile(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000115 } /* else */
116 if (ch == 'n') {
Denys Vlasenko77832482010-08-12 14:14:45 +0200117 dumper->dump_length = xatoi_positive(optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000118 } /* else */
Denys Vlasenko1854bc12010-04-06 23:33:28 +0200119 if (ch == 's') { /* compat: -s accepts hex numbers too */
120 dumper->dump_skip = xstrtoul_range_sfx(optarg, /*base:*/ 0, /*lo:*/ 0, /*hi:*/ LONG_MAX, suffixes);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000121 } /* else */
122 if (ch == 'v') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000123 dumper->dump_vflag = ALL;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000124 }
125#if ENABLE_FEATURE_HEXDUMP_REVERSE
126 if (ch == 'R') {
127 rdump = 1;
128 }
129#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000130 }
131
Denis Vlasenko55f79122008-07-16 11:00:16 +0000132 if (!dumper->fshead) {
133 bb_dump_add(dumper, add_first);
134 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000135 }
136
137 argv += optind;
138
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000139#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko55f79122008-07-16 11:00:16 +0000140 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000141#else
142 if (!rdump) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000143 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000144 }
145
146 /* -R: reverse of 'hexdump -Cv' */
147 fp = stdin;
148 if (!*argv) {
149 argv--;
150 goto jump_in;
151 }
152
153 do {
154 char *buf;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000155 fp = xfopen_for_read(*argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000156 jump_in:
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000157 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000158 p = buf;
159 while (1) {
160 /* skip address or previous byte */
161 while (isxdigit(*p)) p++;
162 while (*p == ' ') p++;
163 /* '|' char will break the line */
164 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
165 break;
166 putchar(ch);
167 }
168 free(buf);
169 }
170 fclose(fp);
171 } while (*++argv);
172
173 fflush_stdout_and_exit(EXIT_SUCCESS);
174#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000175}