Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * xxd implementation for busybox |
| 4 | * |
| 5 | * Copyright (c) 2017 Denys Vlasenko <vda.linux@gmail.com> |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 8 | */ |
| 9 | //config:config XXD |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "xxd (8.9 kb)" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: The xxd utility is used to display binary data in a readable |
| 14 | //config: way that is comparable to the output from most hex editors. |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 15 | |
| 16 | //applet:IF_XXD(APPLET_NOEXEC(xxd, xxd, BB_DIR_USR_BIN, BB_SUID_DROP, xxd)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_XXD) += hexdump_xxd.o |
| 19 | |
| 20 | // $ xxd --version |
| 21 | // xxd V1.10 27oct98 by Juergen Weigert |
| 22 | // $ xxd --help |
| 23 | // Usage: |
| 24 | // xxd [options] [infile [outfile]] |
| 25 | // or |
| 26 | // xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]] |
| 27 | // Options: |
| 28 | // -a toggle autoskip: A single '*' replaces nul-lines. Default off. |
| 29 | // -b binary digit dump (incompatible with -ps,-i,-r). Default hex. |
| 30 | // -c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30). |
| 31 | // -E show characters in EBCDIC. Default ASCII. |
| 32 | // -e little-endian dump (incompatible with -ps,-i,-r). |
| 33 | // -g number of octets per group in normal output. Default 2 (-e: 4). |
| 34 | // -i output in C include file style. |
| 35 | // -l len stop after <len> octets. |
| 36 | // -o off add <off> to the displayed file position. |
| 37 | // -ps output in postscript plain hexdump style. |
| 38 | // -r reverse operation: convert (or patch) hexdump into binary. |
| 39 | // -r -s off revert with <off> added to file positions found in hexdump. |
| 40 | // -s [+][-]seek start at <seek> bytes abs. (or +: rel.) infile offset. |
| 41 | // -u use upper case hex letters. |
| 42 | |
| 43 | //usage:#define xxd_trivial_usage |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 44 | //usage: "[-pri] [-g N] [-c N] [-n LEN] [-s OFS] [-o OFS] [FILE]" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 45 | //usage:#define xxd_full_usage "\n\n" |
| 46 | //usage: "Hex dump FILE (or stdin)\n" |
| 47 | //usage: "\n -g N Bytes per group" |
| 48 | //usage: "\n -c N Bytes per line" |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 49 | //usage: "\n -p Show only hex bytes, assumes -c30" |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 50 | //usage: "\n -i C include file style" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 51 | // exactly the same help text lines in hexdump and xxd: |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 52 | //usage: "\n -l LENGTH Show only first LENGTH bytes" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 53 | //usage: "\n -s OFFSET Skip OFFSET bytes" |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 54 | //usage: "\n -o OFFSET Add OFFSET to displayed offset" |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 55 | //usage: "\n -r Reverse (with -p, assumes no offsets in input)" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 56 | |
| 57 | #include "libbb.h" |
| 58 | #include "dump.h" |
| 59 | |
| 60 | /* This is a NOEXEC applet. Be very careful! */ |
| 61 | |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 62 | #define OPT_l (1 << 0) |
| 63 | #define OPT_s (1 << 1) |
| 64 | #define OPT_a (1 << 2) |
| 65 | #define OPT_p (1 << 3) |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 66 | #define OPT_i (1 << 4) |
| 67 | #define OPT_r (1 << 5) |
| 68 | #define OPT_g (1 << 6) |
| 69 | #define OPT_c (1 << 7) |
| 70 | #define OPT_o (1 << 8) |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 71 | |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 72 | static void reverse(unsigned opt, const char *filename) |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 73 | { |
| 74 | FILE *fp; |
| 75 | char *buf; |
| 76 | |
| 77 | fp = filename ? xfopen_for_read(filename) : stdin; |
| 78 | |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 79 | get_new_line: |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 80 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 81 | char *p; |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 82 | |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 83 | p = buf; |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 84 | if (!(opt & OPT_p)) { |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 85 | skip_address: |
| 86 | p = skip_whitespace(p); |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 87 | while (isxdigit(*p)) p++; |
| 88 | /* NB: for xxd -r, first hex portion is address even without colon */ |
| 89 | /* If it's there, skip it: */ |
| 90 | if (*p == ':') p++; |
| 91 | |
| 92 | //TODO: seek (or zero-pad if unseekable) to the address position |
| 93 | //NOTE: -s SEEK value should be added to the address before seeking |
| 94 | } |
| 95 | |
| 96 | /* Process hex bytes optionally separated by whitespace */ |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 97 | for (;;) { |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 98 | uint8_t val, c; |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 99 | int badchar = 0; |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 100 | nibble1: |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 101 | if (opt & OPT_p) |
| 102 | p = skip_whitespace(p); |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 103 | c = *p++; |
| 104 | if (isdigit(c)) |
| 105 | val = c - '0'; |
| 106 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 107 | val = (c|0x20) - ('a' - 10); |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 108 | else { |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 109 | /* xxd V1.10 allows one non-hexnum char: |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 110 | * echo -e "31 !3 0a 0a" | xxd -r -p |
| 111 | * is "10<a0>" (no <cr>) - "!" is ignored, |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 112 | * but stops for more than one: |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 113 | * echo -e "31 !!343434\n30 0a" | xxd -r -p |
| 114 | * is "10<cr>" - "!!" drops rest of the line. |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 115 | * Note: this also covers whitespace chars: |
| 116 | * xxxxxxxx: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>? |
| 117 | * detects this ^ - skips this one space |
| 118 | * xxxxxxxx: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>? |
| 119 | * detects this ^^ - skips the rest |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 120 | */ |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 121 | if (c == '\0' || badchar) |
| 122 | break; |
| 123 | badchar++; |
| 124 | goto nibble1; |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 125 | } |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 126 | val <<= 4; |
| 127 | |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 128 | nibble2: |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 129 | if (opt & OPT_p) { |
| 130 | /* Works the same with xxd V1.10: |
| 131 | * echo "31 09 32 0a" | xxd -r -p |
| 132 | * echo "31 0 9 32 0a" | xxd -r -p |
| 133 | * thus allow whitespace (even multiple chars) |
| 134 | * after byte's 1st char: |
| 135 | */ |
| 136 | p = skip_whitespace(p); |
| 137 | } |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 138 | |
| 139 | c = *p++; |
| 140 | if (isdigit(c)) |
| 141 | val |= c - '0'; |
| 142 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 143 | val |= (c|0x20) - ('a' - 10); |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 144 | else { |
| 145 | if (c != '\0') { |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 146 | /* "...3<not_hex_char>...": ignore "3", |
| 147 | * skip everything up to next hexchar or newline: |
| 148 | */ |
| 149 | while (!isxdigit(*p)) { |
| 150 | if (*p == '\0') { |
| 151 | free(buf); |
| 152 | goto get_new_line; |
| 153 | } |
| 154 | p++; |
| 155 | } |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 156 | goto nibble1; |
| 157 | } |
| 158 | /* Nibbles can join even through newline: |
| 159 | * echo -e "31 3\n2 0a" | xxd -r -p |
| 160 | * is "12<cr>". |
| 161 | */ |
| 162 | free(buf); |
| 163 | p = buf = xmalloc_fgetline(fp); |
| 164 | if (!buf) |
| 165 | break; |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 166 | if (!(opt & OPT_p)) /* -p and !-p: different behavior */ |
| 167 | goto skip_address; |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 168 | goto nibble2; |
| 169 | } |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 170 | putchar(val); |
Denys Vlasenko | f318ada | 2022-08-22 15:40:47 +0200 | [diff] [blame^] | 171 | } /* for(;;) */ |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 172 | free(buf); |
| 173 | } |
| 174 | //fclose(fp); |
Denys Vlasenko | 31f45c1 | 2022-01-04 23:31:58 +0100 | [diff] [blame] | 175 | fflush_stdout_and_exit_SUCCESS(); |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 178 | static void print_C_style(const char *p, const char *hdr) |
| 179 | { |
| 180 | printf(hdr, isdigit(p[0]) ? "__" : ""); |
| 181 | while (*p) { |
| 182 | bb_putchar(isalnum(*p) ? *p : '_'); |
| 183 | p++; |
| 184 | } |
| 185 | } |
| 186 | |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 187 | int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 188 | int xxd_main(int argc UNUSED_PARAM, char **argv) |
| 189 | { |
| 190 | char buf[80]; |
| 191 | dumper_t *dumper; |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 192 | char *opt_l, *opt_s, *opt_o; |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 193 | unsigned bytes = 2; |
| 194 | unsigned cols = 0; |
| 195 | unsigned opt; |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 196 | int r; |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 197 | |
| 198 | dumper = alloc_dumper(); |
| 199 | |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 200 | opt = getopt32(argv, "^" "l:s:apirg:+c:+o:" "\0" "?1" /* 1 argument max */, |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 201 | &opt_l, &opt_s, &bytes, &cols, &opt_o |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 202 | ); |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 203 | argv += optind; |
| 204 | |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 205 | dumper->dump_vflag = ALL; |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 206 | // if (opt & OPT_a) |
| 207 | // dumper->dump_vflag = SKIPNUL; ..does not exist |
| 208 | if (opt & OPT_l) { |
| 209 | dumper->dump_length = xstrtou_range( |
| 210 | opt_l, |
| 211 | /*base:*/ 0, |
| 212 | /*lo:*/ 0, /*hi:*/ INT_MAX |
| 213 | ); |
| 214 | } |
| 215 | if (opt & OPT_s) { |
| 216 | dumper->dump_skip = xstrtoull_range( |
| 217 | opt_s, |
| 218 | /*base:*/ 0, |
| 219 | /*lo:*/ 0, /*hi:*/ OFF_T_MAX |
| 220 | ); |
| 221 | //BUGGY for /proc/version (unseekable?) |
| 222 | } |
| 223 | |
Denys Vlasenko | 86ba007 | 2021-10-08 23:03:54 +0200 | [diff] [blame] | 224 | if (opt & OPT_r) { |
| 225 | reverse(opt, argv[0]); |
| 226 | } |
| 227 | |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 228 | if (opt & OPT_o) { |
| 229 | /* -o accepts negative numbers too */ |
| 230 | dumper->xxd_displayoff = xstrtoll(opt_o, /*base:*/ 0); |
| 231 | } |
| 232 | |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 233 | if (opt & OPT_p) { |
| 234 | if (cols == 0) |
| 235 | cols = 30; |
| 236 | bytes = cols; /* -p ignores -gN */ |
| 237 | } else { |
| 238 | if (cols == 0) |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 239 | cols = (opt & OPT_i) ? 12 : 16; |
Denys Vlasenko | 294d0c8 | 2021-06-25 00:16:04 +0200 | [diff] [blame] | 240 | if (opt & OPT_i) { |
| 241 | bytes = 1; // -i ignores -gN |
| 242 | // output is " 0xXX, 0xXX, 0xXX...", add leading space |
| 243 | bb_dump_add(dumper, "\" \""); |
| 244 | } else |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 245 | bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: " |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 248 | if (bytes < 1 || bytes >= cols) { |
Denys Vlasenko | 294d0c8 | 2021-06-25 00:16:04 +0200 | [diff] [blame] | 249 | sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "XX" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 250 | bb_dump_add(dumper, buf); |
| 251 | } |
| 252 | else if (bytes == 1) { |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 253 | if (opt & OPT_i) |
Denys Vlasenko | 294d0c8 | 2021-06-25 00:16:04 +0200 | [diff] [blame] | 254 | sprintf(buf, "%u/1 \" 0x%%02x,\"", cols); // cols * " 0xXX," |
| 255 | //TODO: compat: omit the last comma after the very last byte |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 256 | else |
Denys Vlasenko | 294d0c8 | 2021-06-25 00:16:04 +0200 | [diff] [blame] | 257 | sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "XX " |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 258 | bb_dump_add(dumper, buf); |
| 259 | } |
| 260 | else { |
| 261 | /* Format "print byte" with and without trailing space */ |
| 262 | #define BS "/1 \"%02x \"" |
| 263 | #define B "/1 \"%02x\"" |
| 264 | unsigned i; |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 265 | char *bigbuf = xmalloc(cols * (sizeof(BS)-1)); |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 266 | char *p = bigbuf; |
| 267 | for (i = 1; i <= cols; i++) { |
| 268 | if (i == cols || i % bytes) |
| 269 | p = stpcpy(p, B); |
| 270 | else |
| 271 | p = stpcpy(p, BS); |
| 272 | } |
| 273 | // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx" |
| 274 | // todo: can be more clever and use |
Denys Vlasenko | 7dd906a | 2017-01-25 17:00:38 +0100 | [diff] [blame] | 275 | // one 'bytes-1/1 "%02x"' format instead of many "B B B..." formats |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 276 | //bb_error_msg("ADDED:'%s'", bigbuf); |
| 277 | bb_dump_add(dumper, bigbuf); |
| 278 | free(bigbuf); |
| 279 | } |
| 280 | |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 281 | if (!(opt & (OPT_p|OPT_i))) { |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 282 | sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n" |
| 283 | bb_dump_add(dumper, buf); |
Denys Vlasenko | 7dd906a | 2017-01-25 17:00:38 +0100 | [diff] [blame] | 284 | } else { |
| 285 | bb_dump_add(dumper, "\"\n\""); |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 286 | dumper->xxd_eofstring = "\n"; |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 287 | } |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 288 | |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 289 | if ((opt & OPT_i) && argv[0]) { |
| 290 | print_C_style(argv[0], "unsigned char %s"); |
| 291 | printf("[] = {\n"); |
| 292 | } |
| 293 | r = bb_dump_dump(dumper, argv); |
| 294 | if (r == 0 && (opt & OPT_i) && argv[0]) { |
| 295 | print_C_style(argv[0], "};\nunsigned int %s"); |
| 296 | printf("_len = %"OFF_FMT"u;\n", dumper->address); |
| 297 | } |
| 298 | return r; |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 299 | } |