blob: a38fe05e7cd726be36b578f30f07b98abc5fb398 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000013#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000014
Denis Vlasenko99912ca2007-04-10 15:43:37 +000015/* This is a NOEXEC applet. Be very careful! */
16
Denis Vlasenko55f79122008-07-16 11:00:16 +000017static void bb_dump_addfile(dumper_t *dumper, char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000018{
Denis Vlasenko084266e2008-07-26 23:08:31 +000019 char *p;
20 FILE *fp;
21 char *buf;
22
23 fp = xfopen_for_read(name);
24 while ((buf = xmalloc_fgetline(fp)) != NULL) {
25 p = skip_whitespace(buf);
26 if (*p && (*p != '#')) {
27 bb_dump_add(dumper, p);
28 }
29 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000030 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000031 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000032}
33
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000034static const char *const add_strings[] = {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020035 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
36 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
37 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
38 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
39 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000040};
41
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000042static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000043
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000044static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000045
46static const struct suffix_mult suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +000047 { "b", 512 },
48 { "k", 1024 },
49 { "m", 1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020050 { "", 0 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000051};
52
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000053int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000054int hexdump_main(int argc, char **argv)
55{
Denis Vlasenko55f79122008-07-16 11:00:16 +000056 dumper_t *dumper = alloc_dumper();
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000058 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000059#if ENABLE_FEATURE_HEXDUMP_REVERSE
60 FILE *fp;
61 smallint rdump = 0;
62#endif
Glenn L McGrath60281112001-11-02 11:39:46 +000063
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000064 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
65 ch = 'C';
66 goto hd_applet;
67 }
68
69 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000070 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +000072 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000073 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000075 if ((p - hexdump_opts) < 5) {
Denis Vlasenko55f79122008-07-16 11:00:16 +000076 bb_dump_add(dumper, add_first);
77 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000078 }
79 /* Save a little bit of space below by omitting the 'else's. */
80 if (ch == 'C') {
81 hd_applet:
Denis Vlasenko55f79122008-07-16 11:00:16 +000082 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
83 bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
84 bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +000085 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000086 if (ch == 'e') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000087 bb_dump_add(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000088 } /* else */
89 if (ch == 'f') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000090 bb_dump_addfile(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000091 } /* else */
92 if (ch == 'n') {
Denys Vlasenko77832482010-08-12 14:14:45 +020093 dumper->dump_length = xatoi_positive(optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000094 } /* else */
Denys Vlasenko1854bc12010-04-06 23:33:28 +020095 if (ch == 's') { /* compat: -s accepts hex numbers too */
96 dumper->dump_skip = xstrtoul_range_sfx(optarg, /*base:*/ 0, /*lo:*/ 0, /*hi:*/ LONG_MAX, suffixes);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000097 } /* else */
98 if (ch == 'v') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000099 dumper->dump_vflag = ALL;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000100 }
101#if ENABLE_FEATURE_HEXDUMP_REVERSE
102 if (ch == 'R') {
103 rdump = 1;
104 }
105#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000106 }
107
Denis Vlasenko55f79122008-07-16 11:00:16 +0000108 if (!dumper->fshead) {
109 bb_dump_add(dumper, add_first);
110 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000111 }
112
113 argv += optind;
114
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000115#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko55f79122008-07-16 11:00:16 +0000116 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000117#else
118 if (!rdump) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000119 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000120 }
121
122 /* -R: reverse of 'hexdump -Cv' */
123 fp = stdin;
124 if (!*argv) {
125 argv--;
126 goto jump_in;
127 }
128
129 do {
130 char *buf;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000131 fp = xfopen_for_read(*argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000132 jump_in:
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000133 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000134 p = buf;
135 while (1) {
136 /* skip address or previous byte */
137 while (isxdigit(*p)) p++;
138 while (*p == ' ') p++;
139 /* '|' char will break the line */
140 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
141 break;
142 putchar(ch);
143 }
144 free(buf);
145 }
146 fclose(fp);
147 } while (*++argv);
148
149 fflush_stdout_and_exit(EXIT_SUCCESS);
150#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000151}