"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 | */ |
| 11 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 12 | //usage:#define hexdump_trivial_usage |
| 13 | //usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..." |
| 14 | //usage:#define hexdump_full_usage "\n\n" |
| 15 | //usage: "Display FILEs (or stdin) in a user specified format\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 16 | //usage: "\n -b One-byte octal display" |
| 17 | //usage: "\n -c One-byte character display" |
| 18 | //usage: "\n -C Canonical hex+ASCII, 16 bytes per line" |
| 19 | //usage: "\n -d Two-byte decimal display" |
| 20 | //usage: "\n -e FORMAT_STRING" |
| 21 | //usage: "\n -f FORMAT_FILE" |
| 22 | //usage: "\n -n LENGTH Interpret only LENGTH bytes of input" |
| 23 | //usage: "\n -o Two-byte octal display" |
| 24 | //usage: "\n -s OFFSET Skip OFFSET bytes" |
| 25 | //usage: "\n -v Display all input data" |
| 26 | //usage: "\n -x Two-byte hexadecimal display" |
| 27 | //usage: IF_FEATURE_HEXDUMP_REVERSE( |
| 28 | //usage: "\n -R Reverse of 'hexdump -Cv'") |
| 29 | //usage: |
| 30 | //usage:#define hd_trivial_usage |
| 31 | //usage: "FILE..." |
| 32 | //usage:#define hd_full_usage "\n\n" |
| 33 | //usage: "hd is an alias for hexdump -C" |
| 34 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | #include "dump.h" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 38 | /* This is a NOEXEC applet. Be very careful! */ |
| 39 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 40 | static void bb_dump_addfile(dumper_t *dumper, char *name) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 41 | { |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 42 | char *p; |
| 43 | FILE *fp; |
| 44 | char *buf; |
| 45 | |
| 46 | fp = xfopen_for_read(name); |
| 47 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
| 48 | p = skip_whitespace(buf); |
| 49 | if (*p && (*p != '#')) { |
| 50 | bb_dump_add(dumper, p); |
| 51 | } |
| 52 | free(buf); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 53 | } |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 54 | fclose(fp); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 57 | static const char *const add_strings[] = { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 58 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
| 59 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
| 60 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
| 61 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
| 62 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 65 | static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\""; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 67 | static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | |
| 69 | static const struct suffix_mult suffixes[] = { |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 70 | { "b", 512 }, |
| 71 | { "k", 1024 }, |
| 72 | { "m", 1024*1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 73 | { "", 0 } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 76 | int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 77 | int hexdump_main(int argc, char **argv) |
| 78 | { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 79 | dumper_t *dumper = alloc_dumper(); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 81 | int ch; |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 82 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
| 83 | FILE *fp; |
| 84 | smallint rdump = 0; |
| 85 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 87 | if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */ |
| 88 | ch = 'C'; |
| 89 | goto hd_applet; |
| 90 | } |
| 91 | |
| 92 | /* We cannot use getopt32: in hexdump options are cumulative. |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 93 | * E.g. "hexdump -C -C file" should dump each line twice */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
Denis Vlasenko | 4c196a8 | 2006-09-23 15:53:01 +0000 | [diff] [blame] | 95 | p = strchr(hexdump_opts, ch); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 96 | if (!p) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | bb_show_usage(); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 98 | if ((p - hexdump_opts) < 5) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 99 | bb_dump_add(dumper, add_first); |
| 100 | bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 101 | } |
| 102 | /* Save a little bit of space below by omitting the 'else's. */ |
| 103 | if (ch == 'C') { |
| 104 | hd_applet: |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 105 | bb_dump_add(dumper, "\"%08.8_Ax\n\""); |
| 106 | bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); |
| 107 | bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 108 | } |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 109 | if (ch == 'e') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 110 | bb_dump_add(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 111 | } /* else */ |
| 112 | if (ch == 'f') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 113 | bb_dump_addfile(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 114 | } /* else */ |
| 115 | if (ch == 'n') { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 116 | dumper->dump_length = xatoi_positive(optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 117 | } /* else */ |
Denys Vlasenko | 1854bc1 | 2010-04-06 23:33:28 +0200 | [diff] [blame] | 118 | if (ch == 's') { /* compat: -s accepts hex numbers too */ |
Denys Vlasenko | 2c24806 | 2013-03-27 15:18:32 +0100 | [diff] [blame] | 119 | dumper->dump_skip = xstrtoull_range_sfx( |
Denys Vlasenko | ef6747e | 2013-03-27 15:15:33 +0100 | [diff] [blame] | 120 | optarg, |
| 121 | /*base:*/ 0, |
| 122 | /*lo:*/ 0, /*hi:*/ OFF_T_MAX, |
| 123 | suffixes |
| 124 | ); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 125 | } /* else */ |
| 126 | if (ch == 'v') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 127 | dumper->dump_vflag = ALL; |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 128 | } |
| 129 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
| 130 | if (ch == 'R') { |
| 131 | rdump = 1; |
| 132 | } |
| 133 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 136 | if (!dumper->fshead) { |
| 137 | bb_dump_add(dumper, add_first); |
| 138 | bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | argv += optind; |
| 142 | |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 143 | #if !ENABLE_FEATURE_HEXDUMP_REVERSE |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 144 | return bb_dump_dump(dumper, argv); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 145 | #else |
| 146 | if (!rdump) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 147 | return bb_dump_dump(dumper, argv); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* -R: reverse of 'hexdump -Cv' */ |
| 151 | fp = stdin; |
| 152 | if (!*argv) { |
| 153 | argv--; |
| 154 | goto jump_in; |
| 155 | } |
| 156 | |
| 157 | do { |
| 158 | char *buf; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 159 | fp = xfopen_for_read(*argv); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 160 | jump_in: |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 161 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 162 | p = buf; |
| 163 | while (1) { |
| 164 | /* skip address or previous byte */ |
| 165 | while (isxdigit(*p)) p++; |
| 166 | while (*p == ' ') p++; |
| 167 | /* '|' char will break the line */ |
| 168 | if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1) |
| 169 | break; |
| 170 | putchar(ch); |
| 171 | } |
| 172 | free(buf); |
| 173 | } |
| 174 | fclose(fp); |
| 175 | } while (*++argv); |
| 176 | |
| 177 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 178 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 179 | } |