diff options
Diffstat (limited to 'util-linux/hexdump_xxd.c')
-rw-r--r-- | util-linux/hexdump_xxd.c | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/util-linux/hexdump_xxd.c b/util-linux/hexdump_xxd.c index 817250c69..aa215569f 100644 --- a/util-linux/hexdump_xxd.c +++ b/util-linux/hexdump_xxd.c | |||
@@ -41,12 +41,13 @@ | |||
41 | // -u use upper case hex letters. | 41 | // -u use upper case hex letters. |
42 | 42 | ||
43 | //usage:#define xxd_trivial_usage | 43 | //usage:#define xxd_trivial_usage |
44 | //usage: "[-pr] [-g N] [-c N] [-n LEN] [-s OFS] [-o OFS] [FILE]" | 44 | //usage: "[-pri] [-g N] [-c N] [-n LEN] [-s OFS] [-o OFS] [FILE]" |
45 | //usage:#define xxd_full_usage "\n\n" | 45 | //usage:#define xxd_full_usage "\n\n" |
46 | //usage: "Hex dump FILE (or stdin)\n" | 46 | //usage: "Hex dump FILE (or stdin)\n" |
47 | //usage: "\n -g N Bytes per group" | 47 | //usage: "\n -g N Bytes per group" |
48 | //usage: "\n -c N Bytes per line" | 48 | //usage: "\n -c N Bytes per line" |
49 | //usage: "\n -p Show only hex bytes, assumes -c30" | 49 | //usage: "\n -p Show only hex bytes, assumes -c30" |
50 | //usage: "\n -i C include file style" | ||
50 | // exactly the same help text lines in hexdump and xxd: | 51 | // exactly the same help text lines in hexdump and xxd: |
51 | //usage: "\n -l LENGTH Show only first LENGTH bytes" | 52 | //usage: "\n -l LENGTH Show only first LENGTH bytes" |
52 | //usage: "\n -s OFFSET Skip OFFSET bytes" | 53 | //usage: "\n -s OFFSET Skip OFFSET bytes" |
@@ -62,10 +63,11 @@ | |||
62 | #define OPT_s (1 << 1) | 63 | #define OPT_s (1 << 1) |
63 | #define OPT_a (1 << 2) | 64 | #define OPT_a (1 << 2) |
64 | #define OPT_p (1 << 3) | 65 | #define OPT_p (1 << 3) |
65 | #define OPT_r (1 << 4) | 66 | #define OPT_i (1 << 4) |
66 | #define OPT_g (1 << 5) | 67 | #define OPT_r (1 << 5) |
67 | #define OPT_c (1 << 6) | 68 | #define OPT_g (1 << 6) |
68 | #define OPT_o (1 << 7) | 69 | #define OPT_c (1 << 7) |
70 | #define OPT_o (1 << 8) | ||
69 | 71 | ||
70 | static void reverse(unsigned opt, unsigned cols, const char *filename) | 72 | static void reverse(unsigned opt, unsigned cols, const char *filename) |
71 | { | 73 | { |
@@ -126,6 +128,15 @@ static void reverse(unsigned opt, unsigned cols, const char *filename) | |||
126 | fflush_stdout_and_exit(EXIT_SUCCESS); | 128 | fflush_stdout_and_exit(EXIT_SUCCESS); |
127 | } | 129 | } |
128 | 130 | ||
131 | static void print_C_style(const char *p, const char *hdr) | ||
132 | { | ||
133 | printf(hdr, isdigit(p[0]) ? "__" : ""); | ||
134 | while (*p) { | ||
135 | bb_putchar(isalnum(*p) ? *p : '_'); | ||
136 | p++; | ||
137 | } | ||
138 | } | ||
139 | |||
129 | int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 140 | int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
130 | int xxd_main(int argc UNUSED_PARAM, char **argv) | 141 | int xxd_main(int argc UNUSED_PARAM, char **argv) |
131 | { | 142 | { |
@@ -135,10 +146,11 @@ int xxd_main(int argc UNUSED_PARAM, char **argv) | |||
135 | unsigned bytes = 2; | 146 | unsigned bytes = 2; |
136 | unsigned cols = 0; | 147 | unsigned cols = 0; |
137 | unsigned opt; | 148 | unsigned opt; |
149 | int r; | ||
138 | 150 | ||
139 | dumper = alloc_dumper(); | 151 | dumper = alloc_dumper(); |
140 | 152 | ||
141 | opt = getopt32(argv, "^" "l:s:aprg:+c:+o:" "\0" "?1" /* 1 argument max */, | 153 | opt = getopt32(argv, "^" "l:s:apirg:+c:+o:" "\0" "?1" /* 1 argument max */, |
142 | &opt_l, &opt_s, &bytes, &cols, &opt_o | 154 | &opt_l, &opt_s, &bytes, &cols, &opt_o |
143 | ); | 155 | ); |
144 | argv += optind; | 156 | argv += optind; |
@@ -173,8 +185,11 @@ int xxd_main(int argc UNUSED_PARAM, char **argv) | |||
173 | bytes = cols; /* -p ignores -gN */ | 185 | bytes = cols; /* -p ignores -gN */ |
174 | } else { | 186 | } else { |
175 | if (cols == 0) | 187 | if (cols == 0) |
176 | cols = 16; | 188 | cols = (opt & OPT_i) ? 12 : 16; |
177 | bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: " | 189 | if (opt & OPT_i) |
190 | bytes = 1; /* -i ignores -gN */ | ||
191 | else | ||
192 | bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: " | ||
178 | } | 193 | } |
179 | 194 | ||
180 | if (opt & OPT_r) { | 195 | if (opt & OPT_r) { |
@@ -186,7 +201,10 @@ int xxd_main(int argc UNUSED_PARAM, char **argv) | |||
186 | bb_dump_add(dumper, buf); | 201 | bb_dump_add(dumper, buf); |
187 | } | 202 | } |
188 | else if (bytes == 1) { | 203 | else if (bytes == 1) { |
189 | sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx " | 204 | if (opt & OPT_i) |
205 | sprintf(buf, "%u/1 \" 0x%%02x,\"", cols); // cols * " 0xxx," | ||
206 | else | ||
207 | sprintf(buf, "%u/1 \"%%02x \"", cols); // cols * "xx " | ||
190 | bb_dump_add(dumper, buf); | 208 | bb_dump_add(dumper, buf); |
191 | } | 209 | } |
192 | else { | 210 | else { |
@@ -210,7 +228,7 @@ int xxd_main(int argc UNUSED_PARAM, char **argv) | |||
210 | free(bigbuf); | 228 | free(bigbuf); |
211 | } | 229 | } |
212 | 230 | ||
213 | if (!(opt & OPT_p)) { | 231 | if (!(opt & (OPT_p|OPT_i))) { |
214 | sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n" | 232 | sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n" |
215 | bb_dump_add(dumper, buf); | 233 | bb_dump_add(dumper, buf); |
216 | } else { | 234 | } else { |
@@ -218,5 +236,14 @@ int xxd_main(int argc UNUSED_PARAM, char **argv) | |||
218 | dumper->xxd_eofstring = "\n"; | 236 | dumper->xxd_eofstring = "\n"; |
219 | } | 237 | } |
220 | 238 | ||
221 | return bb_dump_dump(dumper, argv); | 239 | if ((opt & OPT_i) && argv[0]) { |
240 | print_C_style(argv[0], "unsigned char %s"); | ||
241 | printf("[] = {\n"); | ||
242 | } | ||
243 | r = bb_dump_dump(dumper, argv); | ||
244 | if (r == 0 && (opt & OPT_i) && argv[0]) { | ||
245 | print_C_style(argv[0], "};\nunsigned int %s"); | ||
246 | printf("_len = %"OFF_FMT"u;\n", dumper->address); | ||
247 | } | ||
248 | return r; | ||
222 | } | 249 | } |