"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * hexdump implementation for busybox |
| 4 | * Based on code from util-linux v 2.11l |
| 5 | * |
| 6 | * Copyright (c) 1989 |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 7 | * The Regents of the University of California. All rights reserved. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config:config HEXDUMP |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 12 | //config: bool "hexdump (8.6 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 17 | //config: |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 18 | //config:config HD |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 19 | //config: bool "hd (7.8 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 20 | //config: default y |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 21 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 22 | //config: hd is an alias to hexdump -C. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 23 | |
| 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 McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 29 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage:#define hexdump_trivial_usage |
Denys Vlasenko | 5d553bc | 2020-12-13 19:18:28 +0100 | [diff] [blame] | 31 | //usage: "[-bcdoxCv] [-e FMT] [-f FMT_FILE] [-n LEN] [-s OFS] [FILE]..." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 32 | //usage:#define hexdump_full_usage "\n\n" |
| 33 | //usage: "Display FILEs (or stdin) in a user specified format\n" |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 34 | //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 Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 42 | //usage: "\n -f FORMAT_FILE" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 43 | // exactly the same help text lines in hexdump and xxd: |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 44 | //usage: "\n -n LENGTH Show only first LENGTH bytes" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 45 | //usage: "\n -s OFFSET Skip OFFSET bytes" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 46 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 52 | #include "libbb.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | #include "dump.h" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 55 | /* This is a NOEXEC applet. Be very careful! */ |
| 56 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 57 | static void bb_dump_addfile(dumper_t *dumper, char *name) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 58 | { |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 59 | 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 McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 70 | } |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 71 | fclose(fp); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Denys Vlasenko | ca466f3 | 2022-02-06 19:53:10 +0100 | [diff] [blame] | 74 | static const char *const add_strings[] ALIGN_PTR = { |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 75 | "\"%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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 82 | static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\""; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 84 | static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v"; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 86 | int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 87 | int hexdump_main(int argc, char **argv) |
| 88 | { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 89 | dumper_t *dumper = alloc_dumper(); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 91 | int ch; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 92 | |
Denys Vlasenko | 5b966c6 | 2016-11-23 11:53:12 +0100 | [diff] [blame] | 93 | if (ENABLE_HD |
| 94 | && (!ENABLE_HEXDUMP || !applet_name[2]) |
| 95 | ) { /* we are "hd" */ |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 96 | ch = 'C'; |
| 97 | goto hd_applet; |
| 98 | } |
| 99 | |
| 100 | /* We cannot use getopt32: in hexdump options are cumulative. |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 101 | * E.g. "hexdump -C -C file" should dump each line twice */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
Denis Vlasenko | 4c196a8 | 2006-09-23 15:53:01 +0000 | [diff] [blame] | 103 | p = strchr(hexdump_opts, ch); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 104 | if (!p) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | bb_show_usage(); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 106 | if ((p - hexdump_opts) < 5) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 107 | bb_dump_add(dumper, add_first); |
| 108 | bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 109 | } |
| 110 | /* Save a little bit of space below by omitting the 'else's. */ |
| 111 | if (ch == 'C') { |
| 112 | hd_applet: |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 113 | bb_dump_add(dumper, "\"%08.8_Ax\n\""); // final address line after dump |
Denys Vlasenko | 8a2657c | 2017-01-25 03:07:39 +0100 | [diff] [blame] | 114 | //------------------- "address " 8 * "xx " " " 8 * "xx " |
| 115 | bb_dump_add(dumper, "\"%08.8_ax \"8/1 \"%02x \"\" \"8/1 \"%02x \""); |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 116 | //------------------- " |ASCII...........|\n" |
| 117 | bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 118 | } |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 119 | if (ch == 'e') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 120 | bb_dump_add(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 121 | } /* else */ |
| 122 | if (ch == 'f') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 123 | bb_dump_addfile(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 124 | } /* else */ |
| 125 | if (ch == 'n') { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 126 | dumper->dump_length = xatoi_positive(optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 127 | } /* else */ |
Denys Vlasenko | 1854bc1 | 2010-04-06 23:33:28 +0200 | [diff] [blame] | 128 | if (ch == 's') { /* compat: -s accepts hex numbers too */ |
Denys Vlasenko | 2c24806 | 2013-03-27 15:18:32 +0100 | [diff] [blame] | 129 | dumper->dump_skip = xstrtoull_range_sfx( |
Denys Vlasenko | ef6747e | 2013-03-27 15:15:33 +0100 | [diff] [blame] | 130 | optarg, |
| 131 | /*base:*/ 0, |
| 132 | /*lo:*/ 0, /*hi:*/ OFF_T_MAX, |
Denys Vlasenko | c72b43c | 2013-07-13 23:49:45 +0200 | [diff] [blame] | 133 | bkm_suffixes |
Denys Vlasenko | ef6747e | 2013-03-27 15:15:33 +0100 | [diff] [blame] | 134 | ); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 135 | } /* else */ |
| 136 | if (ch == 'v') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 137 | dumper->dump_vflag = ALL; |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 138 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 141 | if (!dumper->fshead) { |
| 142 | bb_dump_add(dumper, add_first); |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 143 | bb_dump_add(dumper, "\"%07.7_ax \"8/2 \"%04x \"\"\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | argv += optind; |
| 147 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 148 | return bb_dump_dump(dumper, argv); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 149 | } |