blob: 8ac12f0820b261668464705fefe5a0c1ca87cc4e [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
7 * The Regents of the University of California. All rights reserved.
8 *
Rob Landleyea224be2006-06-18 20:20:07 +00009 * Licensed under GPLv2 or later, see file License in this tarball for details.
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 Vlasenko5415c852008-07-21 23:05:26 +000019 parser_t *parser = config_open2(name, xfopen_for_read);
20 while (config_read(parser, &name, 1, 1, "# \t", 0)) {
21 bb_dump_add(dumper, name);
Glenn L McGrath60281112001-11-02 11:39:46 +000022 }
Denis Vlasenko5415c852008-07-21 23:05:26 +000023 config_close(parser);
Glenn L McGrath60281112001-11-02 11:39:46 +000024}
25
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000026static const char *const add_strings[] = {
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000027 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
28 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
29 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
30 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
31 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000032};
33
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000034static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000035
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000036static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" USE_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000037
38static const struct suffix_mult suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +000039 { "b", 512 },
40 { "k", 1024 },
41 { "m", 1024*1024 },
42 { }
Manuel Novoa III cad53642003-03-19 09:13:01 +000043};
44
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000045int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000046int hexdump_main(int argc, char **argv)
47{
Denis Vlasenko55f79122008-07-16 11:00:16 +000048 dumper_t *dumper = alloc_dumper();
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000050 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000051#if ENABLE_FEATURE_HEXDUMP_REVERSE
52 FILE *fp;
53 smallint rdump = 0;
54#endif
Glenn L McGrath60281112001-11-02 11:39:46 +000055
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000056 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
57 ch = 'C';
58 goto hd_applet;
59 }
60
61 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000062 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +000064 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000065 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000067 if ((p - hexdump_opts) < 5) {
Denis Vlasenko55f79122008-07-16 11:00:16 +000068 bb_dump_add(dumper, add_first);
69 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000070 }
71 /* Save a little bit of space below by omitting the 'else's. */
72 if (ch == 'C') {
73 hd_applet:
Denis Vlasenko55f79122008-07-16 11:00:16 +000074 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
75 bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
76 bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +000077 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000078 if (ch == 'e') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000079 bb_dump_add(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000080 } /* else */
81 if (ch == 'f') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000082 bb_dump_addfile(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000083 } /* else */
84 if (ch == 'n') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000085 dumper->dump_length = xatoi_u(optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000086 } /* else */
87 if (ch == 's') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000088 dumper->dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000089 } /* else */
90 if (ch == 'v') {
Denis Vlasenko55f79122008-07-16 11:00:16 +000091 dumper->dump_vflag = ALL;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000092 }
93#if ENABLE_FEATURE_HEXDUMP_REVERSE
94 if (ch == 'R') {
95 rdump = 1;
96 }
97#endif
Glenn L McGrath60281112001-11-02 11:39:46 +000098 }
99
Denis Vlasenko55f79122008-07-16 11:00:16 +0000100 if (!dumper->fshead) {
101 bb_dump_add(dumper, add_first);
102 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000103 }
104
105 argv += optind;
106
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000107#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko55f79122008-07-16 11:00:16 +0000108 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000109#else
110 if (!rdump) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000111 return bb_dump_dump(dumper, argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000112 }
113
114 /* -R: reverse of 'hexdump -Cv' */
115 fp = stdin;
116 if (!*argv) {
117 argv--;
118 goto jump_in;
119 }
120
121 do {
122 char *buf;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000123 fp = xfopen_for_read(*argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000124 jump_in:
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000125 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000126 p = buf;
127 while (1) {
128 /* skip address or previous byte */
129 while (isxdigit(*p)) p++;
130 while (*p == ' ') p++;
131 /* '|' char will break the line */
132 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
133 break;
134 putchar(ch);
135 }
136 free(buf);
137 }
138 fclose(fp);
139 } while (*++argv);
140
141 fflush_stdout_and_exit(EXIT_SUCCESS);
142#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000143}