blob: 57e7e8db7913d0a7a7b214ae4c28905ed794c1f6 [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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config:config HEXDUMP
Denys Vlasenkob097a842018-12-28 03:20:17 +010012//config: bool "hexdump (8.6 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013//config: default y
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//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.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010017//config:
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018//config:config HD
Denys Vlasenkob097a842018-12-28 03:20:17 +010019//config: bool "hd (7.8 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010020//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010021//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: hd is an alias to hexdump -C.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010023
24//applet:IF_HEXDUMP(APPLET_NOEXEC(hexdump, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hexdump))
25//applet:IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
26
27//kbuild:lib-$(CONFIG_HEXDUMP) += hexdump.o
28//kbuild:lib-$(CONFIG_HD) += hexdump.o
Glenn L McGrath60281112001-11-02 11:39:46 +000029
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage:#define hexdump_trivial_usage
Denys Vlasenko5d553bc2020-12-13 19:18:28 +010031//usage: "[-bcdoxCv] [-e FMT] [-f FMT_FILE] [-n LEN] [-s OFS] [FILE]..."
Pere Orga5bc8c002011-04-11 03:29:49 +020032//usage:#define hexdump_full_usage "\n\n"
33//usage: "Display FILEs (or stdin) in a user specified format\n"
Denys Vlasenko62a9b182017-01-25 16:50:30 +010034//usage: "\n -b 1-byte octal display"
35//usage: "\n -c 1-byte character display"
36//usage: "\n -d 2-byte decimal display"
37//usage: "\n -o 2-byte octal display"
38//usage: "\n -x 2-byte hex display"
39//usage: "\n -C hex+ASCII 16 bytes per line"
40//usage: "\n -v Show all (no dup folding)"
41//usage: "\n -e FORMAT_STR Example: '16/1 \"%02x|\"\"\\n\"'"
Pere Orga5bc8c002011-04-11 03:29:49 +020042//usage: "\n -f FORMAT_FILE"
Denys Vlasenko0f436472017-01-25 01:58:00 +010043// exactly the same help text lines in hexdump and xxd:
Denys Vlasenko62a9b182017-01-25 16:50:30 +010044//usage: "\n -n LENGTH Show only first LENGTH bytes"
Pere Orga5bc8c002011-04-11 03:29:49 +020045//usage: "\n -s OFFSET Skip OFFSET bytes"
Pere Orga5bc8c002011-04-11 03:29:49 +020046//usage:
47//usage:#define hd_trivial_usage
48//usage: "FILE..."
49//usage:#define hd_full_usage "\n\n"
50//usage: "hd is an alias for hexdump -C"
51
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000052#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000053#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000054
Denis Vlasenko99912ca2007-04-10 15:43:37 +000055/* This is a NOEXEC applet. Be very careful! */
56
Denis Vlasenko55f79122008-07-16 11:00:16 +000057static void bb_dump_addfile(dumper_t *dumper, char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000058{
Denis Vlasenko084266e2008-07-26 23:08:31 +000059 char *p;
60 FILE *fp;
61 char *buf;
62
63 fp = xfopen_for_read(name);
64 while ((buf = xmalloc_fgetline(fp)) != NULL) {
65 p = skip_whitespace(buf);
66 if (*p && (*p != '#')) {
67 bb_dump_add(dumper, p);
68 }
69 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000070 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000071 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000072}
73
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000074static const char *const add_strings[] = {
Denys Vlasenko0f436472017-01-25 01:58:00 +010075 "\"%07.7_ax \"16/1 \"%03o \"\"\n\"", /* b */
76 "\"%07.7_ax \"16/1 \"%3_c \"\"\n\"", /* c */
77 "\"%07.7_ax \"8/2 \" %05u \"\"\n\"", /* d */
78 "\"%07.7_ax \"8/2 \" %06o \"\"\n\"", /* o */
79 "\"%07.7_ax \"8/2 \" %04x \"\"\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000080};
81
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000082static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000083
Denys Vlasenko32e1f692020-10-25 16:06:45 +010084static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v";
Manuel Novoa III cad53642003-03-19 09:13:01 +000085
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000086int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000087int hexdump_main(int argc, char **argv)
88{
Denis Vlasenko55f79122008-07-16 11:00:16 +000089 dumper_t *dumper = alloc_dumper();
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000091 int ch;
Glenn L McGrath60281112001-11-02 11:39:46 +000092
Denys Vlasenko5b966c62016-11-23 11:53:12 +010093 if (ENABLE_HD
94 && (!ENABLE_HEXDUMP || !applet_name[2])
95 ) { /* we are "hd" */
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000096 ch = 'C';
97 goto hd_applet;
98 }
99
100 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000101 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +0000103 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000104 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000106 if ((p - hexdump_opts) < 5) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000107 bb_dump_add(dumper, add_first);
108 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000109 }
110 /* Save a little bit of space below by omitting the 'else's. */
111 if (ch == 'C') {
112 hd_applet:
Denys Vlasenko0f436472017-01-25 01:58:00 +0100113 bb_dump_add(dumper, "\"%08.8_Ax\n\""); // final address line after dump
Denys Vlasenko8a2657c2017-01-25 03:07:39 +0100114 //------------------- "address " 8 * "xx " " " 8 * "xx "
115 bb_dump_add(dumper, "\"%08.8_ax \"8/1 \"%02x \"\" \"8/1 \"%02x \"");
Denys Vlasenko0f436472017-01-25 01:58:00 +0100116 //------------------- " |ASCII...........|\n"
117 bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000118 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000119 if (ch == 'e') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000120 bb_dump_add(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000121 } /* else */
122 if (ch == 'f') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000123 bb_dump_addfile(dumper, optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000124 } /* else */
125 if (ch == 'n') {
Denys Vlasenko77832482010-08-12 14:14:45 +0200126 dumper->dump_length = xatoi_positive(optarg);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000127 } /* else */
Denys Vlasenko1854bc12010-04-06 23:33:28 +0200128 if (ch == 's') { /* compat: -s accepts hex numbers too */
Denys Vlasenko2c248062013-03-27 15:18:32 +0100129 dumper->dump_skip = xstrtoull_range_sfx(
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100130 optarg,
131 /*base:*/ 0,
132 /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +0200133 bkm_suffixes
Denys Vlasenkoef6747e2013-03-27 15:15:33 +0100134 );
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000135 } /* else */
136 if (ch == 'v') {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000137 dumper->dump_vflag = ALL;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000138 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000139 }
140
Denis Vlasenko55f79122008-07-16 11:00:16 +0000141 if (!dumper->fshead) {
142 bb_dump_add(dumper, add_first);
Denys Vlasenko0f436472017-01-25 01:58:00 +0100143 bb_dump_add(dumper, "\"%07.7_ax \"8/2 \"%04x \"\"\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000144 }
145
146 argv += optind;
147
Denis Vlasenko55f79122008-07-16 11:00:16 +0000148 return bb_dump_dump(dumper, argv);
Glenn L McGrath60281112001-11-02 11:39:46 +0000149}