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 |
| 44 | //usage: "[OPTIONS] [FILE]" |
| 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 | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 50 | // exactly the same help text lines in hexdump and xxd: |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 51 | //usage: "\n -l LENGTH Show only first LENGTH bytes" |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 52 | //usage: "\n -s OFFSET Skip OFFSET bytes" |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame^] | 53 | //usage: "\n -r Reverse (with -p, assumes no offsets in input)" |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 54 | // TODO: implement -r (see hexdump -R) |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 55 | |
| 56 | #include "libbb.h" |
| 57 | #include "dump.h" |
| 58 | |
| 59 | /* This is a NOEXEC applet. Be very careful! */ |
| 60 | |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame^] | 61 | #define OPT_l (1 << 0) |
| 62 | #define OPT_s (1 << 1) |
| 63 | #define OPT_a (1 << 2) |
| 64 | #define OPT_p (1 << 3) |
| 65 | #define OPT_r (1 << 4) |
| 66 | |
| 67 | static void reverse(unsigned opt, unsigned cols, const char *filename) |
| 68 | { |
| 69 | FILE *fp; |
| 70 | char *buf; |
| 71 | |
| 72 | fp = filename ? xfopen_for_read(filename) : stdin; |
| 73 | |
| 74 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
| 75 | char *p = buf; |
| 76 | unsigned cnt = cols; |
| 77 | |
| 78 | if (!(opt & OPT_p)) { |
| 79 | /* skip address */ |
| 80 | while (isxdigit(*p)) p++; |
| 81 | /* NB: for xxd -r, first hex portion is address even without colon */ |
| 82 | /* If it's there, skip it: */ |
| 83 | if (*p == ':') p++; |
| 84 | |
| 85 | //TODO: seek (or zero-pad if unseekable) to the address position |
| 86 | //NOTE: -s SEEK value should be added to the address before seeking |
| 87 | } |
| 88 | |
| 89 | /* Process hex bytes optionally separated by whitespace */ |
| 90 | do { |
| 91 | uint8_t val, c; |
| 92 | |
| 93 | p = skip_whitespace(p); |
| 94 | |
| 95 | c = *p++; |
| 96 | if (isdigit(c)) |
| 97 | val = c - '0'; |
| 98 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 99 | val = (c|0x20) - ('a' - 10); |
| 100 | else |
| 101 | break; |
| 102 | val <<= 4; |
| 103 | |
| 104 | /* Works the same with xxd V1.10: |
| 105 | * echo "31 09 32 0a" | xxd -r -p |
| 106 | * echo "31 0 9 32 0a" | xxd -r -p |
| 107 | * thus allow whitespace even within the byte: |
| 108 | */ |
| 109 | p = skip_whitespace(p); |
| 110 | |
| 111 | c = *p++; |
| 112 | if (isdigit(c)) |
| 113 | val |= c - '0'; |
| 114 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 115 | val |= (c|0x20) - ('a' - 10); |
| 116 | else |
| 117 | break; |
| 118 | putchar(val); |
| 119 | } while (!(opt & OPT_p) || --cnt != 0); |
| 120 | free(buf); |
| 121 | } |
| 122 | //fclose(fp); |
| 123 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 124 | } |
| 125 | |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 126 | int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 127 | int xxd_main(int argc UNUSED_PARAM, char **argv) |
| 128 | { |
| 129 | char buf[80]; |
| 130 | dumper_t *dumper; |
| 131 | char *opt_l, *opt_s; |
| 132 | unsigned bytes = 2; |
| 133 | unsigned cols = 0; |
| 134 | unsigned opt; |
| 135 | |
| 136 | dumper = alloc_dumper(); |
| 137 | |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame^] | 138 | opt = getopt32(argv, "^" "l:s:aprg:+c:+" "\0" "?1" /* 1 argument max */, |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 139 | &opt_l, &opt_s, &bytes, &cols |
| 140 | ); |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 141 | argv += optind; |
| 142 | |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 143 | dumper->dump_vflag = ALL; |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 144 | // if (opt & OPT_a) |
| 145 | // dumper->dump_vflag = SKIPNUL; ..does not exist |
| 146 | if (opt & OPT_l) { |
| 147 | dumper->dump_length = xstrtou_range( |
| 148 | opt_l, |
| 149 | /*base:*/ 0, |
| 150 | /*lo:*/ 0, /*hi:*/ INT_MAX |
| 151 | ); |
| 152 | } |
| 153 | if (opt & OPT_s) { |
| 154 | dumper->dump_skip = xstrtoull_range( |
| 155 | opt_s, |
| 156 | /*base:*/ 0, |
| 157 | /*lo:*/ 0, /*hi:*/ OFF_T_MAX |
| 158 | ); |
| 159 | //BUGGY for /proc/version (unseekable?) |
| 160 | } |
| 161 | |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 162 | if (opt & OPT_p) { |
| 163 | if (cols == 0) |
| 164 | cols = 30; |
| 165 | bytes = cols; /* -p ignores -gN */ |
| 166 | } else { |
| 167 | if (cols == 0) |
| 168 | cols = 16; |
| 169 | bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: " |
| 170 | } |
| 171 | |
Denys Vlasenko | 32e1f69 | 2020-10-25 16:06:45 +0100 | [diff] [blame^] | 172 | if (opt & OPT_r) { |
| 173 | reverse(opt, cols, argv[0]); |
| 174 | } |
| 175 | |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 176 | if (bytes < 1 || bytes >= cols) { |
| 177 | sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx" |
| 178 | bb_dump_add(dumper, buf); |
| 179 | } |
| 180 | else if (bytes == 1) { |
| 181 | sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx " |
| 182 | bb_dump_add(dumper, buf); |
| 183 | } |
| 184 | else { |
| 185 | /* Format "print byte" with and without trailing space */ |
| 186 | #define BS "/1 \"%02x \"" |
| 187 | #define B "/1 \"%02x\"" |
| 188 | unsigned i; |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 189 | char *bigbuf = xmalloc(cols * (sizeof(BS)-1)); |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 190 | char *p = bigbuf; |
| 191 | for (i = 1; i <= cols; i++) { |
| 192 | if (i == cols || i % bytes) |
| 193 | p = stpcpy(p, B); |
| 194 | else |
| 195 | p = stpcpy(p, BS); |
| 196 | } |
| 197 | // for -g3, this results in B B BS B B BS... B = "xxxxxx xxxxxx .....xx" |
| 198 | // todo: can be more clever and use |
Denys Vlasenko | 7dd906a | 2017-01-25 17:00:38 +0100 | [diff] [blame] | 199 | // 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] | 200 | //bb_error_msg("ADDED:'%s'", bigbuf); |
| 201 | bb_dump_add(dumper, bigbuf); |
| 202 | free(bigbuf); |
| 203 | } |
| 204 | |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 205 | if (!(opt & OPT_p)) { |
| 206 | sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n" |
| 207 | bb_dump_add(dumper, buf); |
Denys Vlasenko | 7dd906a | 2017-01-25 17:00:38 +0100 | [diff] [blame] | 208 | } else { |
| 209 | bb_dump_add(dumper, "\"\n\""); |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 210 | dumper->eofstring = "\n"; |
Denys Vlasenko | 62a9b18 | 2017-01-25 16:50:30 +0100 | [diff] [blame] | 211 | } |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 212 | |
| 213 | return bb_dump_dump(dumper, argv); |
| 214 | } |