blob: 4a7f641db06e8148beb8f87b581714efc9546c13
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * hexdump implementation for busybox |
4 | * Based on code from util-linux v 2.11l |
5 | * |
6 | * Copyright (c) 1989 |
7 | * The Regents of the University of California. All rights reserved. |
8 | * |
9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
10 | */ |
11 | //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: help |
32 | //config: hd is an alias to hexdump -C. |
33 | |
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 |
39 | |
40 | //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" |
44 | //usage: "\n -b One-byte octal display" |
45 | //usage: "\n -c One-byte character display" |
46 | //usage: "\n -C Canonical hex+ASCII, 16 bytes per line" |
47 | //usage: "\n -d Two-byte decimal display" |
48 | //usage: "\n -e FORMAT_STRING" |
49 | //usage: "\n -f FORMAT_FILE" |
50 | //usage: "\n -n LENGTH Interpret only LENGTH bytes of input" |
51 | //usage: "\n -o Two-byte octal display" |
52 | //usage: "\n -s OFFSET Skip OFFSET bytes" |
53 | //usage: "\n -v Display all input data" |
54 | //usage: "\n -x Two-byte hexadecimal display" |
55 | //usage: IF_FEATURE_HEXDUMP_REVERSE( |
56 | //usage: "\n -R Reverse of 'hexdump -Cv'") |
57 | //usage: |
58 | //usage:#define hd_trivial_usage |
59 | //usage: "FILE..." |
60 | //usage:#define hd_full_usage "\n\n" |
61 | //usage: "hd is an alias for hexdump -C" |
62 | |
63 | #include "libbb.h" |
64 | #include "dump.h" |
65 | |
66 | /* This is a NOEXEC applet. Be very careful! */ |
67 | |
68 | static void bb_dump_addfile(dumper_t *dumper, char *name) |
69 | { |
70 | char *p; |
71 | FILE *fp; |
72 | char *buf; |
73 | |
74 | fp = xfopen_for_read(name); |
75 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
76 | p = skip_whitespace(buf); |
77 | if (*p && (*p != '#')) { |
78 | bb_dump_add(dumper, p); |
79 | } |
80 | free(buf); |
81 | } |
82 | fclose(fp); |
83 | } |
84 | |
85 | static const char *const add_strings[] = { |
86 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
87 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
88 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
89 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
90 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
91 | }; |
92 | |
93 | static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\""; |
94 | |
95 | static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R"); |
96 | |
97 | int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
98 | int hexdump_main(int argc, char **argv) |
99 | { |
100 | dumper_t *dumper = alloc_dumper(); |
101 | const char *p; |
102 | int ch; |
103 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
104 | FILE *fp; |
105 | smallint rdump = 0; |
106 | #endif |
107 | |
108 | if (ENABLE_HD |
109 | && (!ENABLE_HEXDUMP || !applet_name[2]) |
110 | ) { /* we are "hd" */ |
111 | ch = 'C'; |
112 | goto hd_applet; |
113 | } |
114 | |
115 | /* We cannot use getopt32: in hexdump options are cumulative. |
116 | * E.g. "hexdump -C -C file" should dump each line twice */ |
117 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
118 | p = strchr(hexdump_opts, ch); |
119 | if (!p) |
120 | bb_show_usage(); |
121 | if ((p - hexdump_opts) < 5) { |
122 | bb_dump_add(dumper, add_first); |
123 | bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]); |
124 | } |
125 | /* Save a little bit of space below by omitting the 'else's. */ |
126 | if (ch == 'C') { |
127 | hd_applet: |
128 | bb_dump_add(dumper, "\"%08.8_Ax\n\""); |
129 | bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); |
130 | bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\""); |
131 | } |
132 | if (ch == 'e') { |
133 | bb_dump_add(dumper, optarg); |
134 | } /* else */ |
135 | if (ch == 'f') { |
136 | bb_dump_addfile(dumper, optarg); |
137 | } /* else */ |
138 | if (ch == 'n') { |
139 | dumper->dump_length = xatoi_positive(optarg); |
140 | } /* else */ |
141 | if (ch == 's') { /* compat: -s accepts hex numbers too */ |
142 | dumper->dump_skip = xstrtoull_range_sfx( |
143 | optarg, |
144 | /*base:*/ 0, |
145 | /*lo:*/ 0, /*hi:*/ OFF_T_MAX, |
146 | bkm_suffixes |
147 | ); |
148 | } /* else */ |
149 | if (ch == 'v') { |
150 | dumper->dump_vflag = ALL; |
151 | } |
152 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
153 | if (ch == 'R') { |
154 | rdump = 1; |
155 | } |
156 | #endif |
157 | } |
158 | |
159 | if (!dumper->fshead) { |
160 | bb_dump_add(dumper, add_first); |
161 | bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
162 | } |
163 | |
164 | argv += optind; |
165 | |
166 | #if !ENABLE_FEATURE_HEXDUMP_REVERSE |
167 | return bb_dump_dump(dumper, argv); |
168 | #else |
169 | if (!rdump) { |
170 | return bb_dump_dump(dumper, argv); |
171 | } |
172 | |
173 | /* -R: reverse of 'hexdump -Cv' */ |
174 | fp = stdin; |
175 | if (!*argv) { |
176 | argv--; |
177 | goto jump_in; |
178 | } |
179 | |
180 | do { |
181 | char *buf; |
182 | fp = xfopen_for_read(*argv); |
183 | jump_in: |
184 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
185 | p = buf; |
186 | while (1) { |
187 | /* skip address or previous byte */ |
188 | while (isxdigit(*p)) p++; |
189 | while (*p == ' ') p++; |
190 | /* '|' char will break the line */ |
191 | if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1) |
192 | break; |
193 | putchar(ch); |
194 | } |
195 | free(buf); |
196 | } |
197 | fclose(fp); |
198 | } while (*++argv); |
199 | |
200 | fflush_stdout_and_exit(EXIT_SUCCESS); |
201 | #endif |
202 | } |
203 |