blob: 9a312f95c64b2e6d6b56b9850749859087db3f00 [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"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage: "\n -b One-byte octal display"
17//usage: "\n -c One-byte character display"
18//usage: "\n -C Canonical hex+ASCII, 16 bytes per line"
19//usage: "\n -d Two-byte decimal display"
20//usage: "\n -e FORMAT_STRING"
21//usage: "\n -f FORMAT_FILE"
22//usage: "\n -n LENGTH Interpret only LENGTH bytes of input"
23//usage: "\n -o Two-byte octal display"
24//usage: "\n -s OFFSET Skip OFFSET bytes"
25//usage: "\n -v Display all input data"
26//usage: "\n -x Two-byte hexadecimal display"
27//usage: IF_FEATURE_HEXDUMP_REVERSE(
28//usage: "\n -R Reverse of 'hexdump -Cv'")
29//usage:
30//usage:#define hd_trivial_usage
31//usage: "FILE..."
32//usage:#define hd_full_usage "\n\n"
33//usage: "hd is an alias for hexdump -C"
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000036#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000037
Denis Vlasenko99912ca2007-04-10 15:43:37 +000038/* This is a NOEXEC applet. Be very careful! */
39
Denis Vlasenko55f79122008-07-16 11:00:16 +000040static void bb_dump_addfile(dumper_t *dumper, char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000041{
Denis Vlasenko084266e2008-07-26 23:08:31 +000042 char *p;
43 FILE *fp;
44 char *buf;
45
46 fp = xfopen_for_read(name);
47 while ((buf = xmalloc_fgetline(fp)) != NULL) {
48 p = skip_whitespace(buf);
49 if (*p && (*p != '#')) {
50 bb_dump_add(dumper, p);
51 }
52 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000053 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000054 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000055}
56
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000057static const char *const add_strings[] = {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020058 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
59 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
60 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
61 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
62 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000063};
64
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000065static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000066
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000067static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000068
69static const struct suffix_mult suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +000070 { "b", 512 },
71 { "k", 1024 },
72 { "m", 1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020073 { "", 0 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000074};
75
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000076int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000077int hexdump_main(int argc, char **argv)
78{
Denis Vlasenko55f79122008-07-16 11:00:16 +000079 dumper_t *dumper = alloc_dumper();
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000081 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000082#if ENABLE_FEATURE_HEXDUMP_REVERSE
83 FILE *fp;
84 smallint rdump = 0;
85#endif
Glenn L McGrath60281112001-11-02 11:39:46 +000086
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000087 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
88 ch = 'C';
89 goto hd_applet;
90 }
91
92 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000093 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +000095 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000096 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000098 if ((p - hexdump_opts) < 5) {
Denis Vlasenko55f79122008-07-16 11:00:16 +000099 bb_dump_add(dumper, add_first);
100 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000101 }
102 /* Save a little bit of space below by omitting the 'else's. */
103 if (ch == 'C') {
104 hd_applet:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000105 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
106 bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
107 bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000108 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000109 if (ch == 'e') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000110 bb_dump_add(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000111 } /* else */
112 if (ch == 'f') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000113 bb_dump_addfile(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000114 } /* else */
115 if (ch == 'n') {
Denys Vlasenko77832482010-08-12 14:14:45 +0200116 dumper->dump_length = xatoi_positive(optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000117 } /* else */
Denys Vlasenko1854bc12010-04-06 23:33:28 +0200118 if (ch == 's') { /* compat: -s accepts hex numbers too */
119 dumper->dump_skip = xstrtoul_range_sfx(optarg, /*base:*/ 0, /*lo:*/ 0, /*hi:*/ LONG_MAX, suffixes);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000120 } /* else */
121 if (ch == 'v') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000122 dumper->dump_vflag = ALL;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000123 }
124#if ENABLE_FEATURE_HEXDUMP_REVERSE
125 if (ch == 'R') {
126 rdump = 1;
127 }
128#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000129 }
130
Denis Vlasenko55f79122008-07-16 11:00:16 +0000131 if (!dumper->fshead) {
132 bb_dump_add(dumper, add_first);
133 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000134 }
135
136 argv += optind;
137
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000138#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko55f79122008-07-16 11:00:16 +0000139 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000140#else
141 if (!rdump) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000142 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000143 }
144
145 /* -R: reverse of 'hexdump -Cv' */
146 fp = stdin;
147 if (!*argv) {
148 argv--;
149 goto jump_in;
150 }
151
152 do {
153 char *buf;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000154 fp = xfopen_for_read(*argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000155 jump_in:
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000156 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000157 p = buf;
158 while (1) {
159 /* skip address or previous byte */
160 while (isxdigit(*p)) p++;
161 while (*p == ' ') p++;
162 /* '|' char will break the line */
163 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
164 break;
165 putchar(ch);
166 }
167 free(buf);
168 }
169 fclose(fp);
170 } while (*++argv);
171
172 fflush_stdout_and_exit(EXIT_SUCCESS);
173#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000174}